00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #ifndef KEXIDB_FIELD_H
00023 #define KEXIDB_FIELD_H
00024
00025 #include <qvariant.h>
00026 #include <qstring.h>
00027 #include <qpair.h>
00028 #include <qvaluevector.h>
00029 #include <qptrvector.h>
00030 #include "kexidb/kexidb_export.h"
00031 namespace KexiDB {
00032
00033 class TableSchema;
00034 class QuerySchema;
00035 class FieldList;
00036 class BaseExpr;
00037
00039
00072 class KEXI_DB_EXPORT Field
00073 {
00074 public:
00075 typedef QPtrList<Field> List;
00076 typedef QPtrVector<Field> Vector;
00077 typedef QPtrListIterator<Field> ListIterator;
00078 typedef QPair<Field*,Field*> Pair;
00079 typedef QPtrList<Pair> PairList;
00080
00082 enum Type
00083 {
00084 InvalidType = 0,
00085 Byte = 1,
00086 ShortInteger = 2,
00087 Integer = 3,
00088 BigInteger = 4,
00089 Boolean = 5,
00090 Date = 6,
00091 DateTime = 7,
00092 Time = 8,
00093 Float = 9,
00094 Double = 10,
00095 Text = 11,
00096 LongText = 12,
00097 BLOB = 13,
00099 LastType = 13,
00101 Null = 64,
00103
00104 Asterisk = 128,
00107 Enum = 129,
00108 Map = 130
00109 };
00110
00111
00112 static uint defaultTextLength() { return 200; }
00113
00115 enum TypeGroup
00116 {
00117 InvalidGroup = 0,
00118 TextGroup = 1,
00119 IntegerGroup = 2,
00120 FloatGroup = 3,
00121 BooleanGroup = 4,
00122 DateTimeGroup = 5,
00123 BLOBGroup = 6,
00124
00125 LastTypeGroup = 6
00126 };
00127
00129 enum Constraints
00130 {
00131 NoConstraints = 0,
00132 AutoInc = 1,
00133 Unique = 2,
00134 PrimaryKey = 4,
00135 ForeignKey = 8,
00136 NotNull = 16,
00137 NotEmpty = 32,
00138 Indexed = 64
00139 };
00140
00142 enum Options
00143 {
00144 NoOptions = 0,
00145 Unsigned = 1
00146 };
00147
00150 Field(TableSchema *tableSchema);
00151
00154 Field();
00155
00157 Field(const QString& name, Type ctype,
00158 uint cconst=NoConstraints,
00159 uint options = NoOptions,
00160 uint length=0, uint precision=0,
00161 QVariant defaultValue=QVariant(),
00162 const QString& caption = QString::null,
00163 const QString& description = QString::null,
00164 uint width = 0);
00165
00167 Field(const Field& f);
00168
00169 virtual ~Field();
00170
00172 static QVariant::Type variantType(uint type);
00173
00176 static QString typeName(uint type);
00177
00181 static QString typeString(uint type);
00182
00184 static Type typeForString(const QString& typeString);
00185
00187 static TypeGroup typeGroupForString(const QString& typeGroupString);
00188
00190 static TypeGroup typeGroup(uint type);
00191
00194 static QString typeGroupName(uint typeGroup);
00195
00199 static QString typeGroupString(uint typeGroup);
00200
00201
00202 inline QString name() const { return m_name; }
00203
00207 virtual TableSchema* table() const;
00208
00214 virtual void setTable(TableSchema *table);
00215
00220 QuerySchema* query() const;
00221
00228 void setQuery(QuerySchema *query);
00229
00231 inline bool isAutoIncrement() const { return constraints() & AutoInc; }
00232
00234 inline bool isPrimaryKey() const { return constraints() & PrimaryKey; }
00235
00237 inline bool isUniqueKey() const { return constraints() & Unique; }
00238
00240 inline bool isForeignKey() const { return constraints() & ForeignKey; }
00241
00243 inline bool isNotNull() const { return constraints() & NotNull; }
00244
00246 inline bool isNotEmpty() const { return constraints() & NotEmpty; }
00247
00249 inline bool isIndexed() const { return constraints() & Indexed; }
00250
00252 inline bool isNumericType() const { return Field::isNumericType(type()); }
00253
00256 static bool isNumericType(uint type);
00257
00259 inline bool isIntegerType() const { return Field::isIntegerType(type()); }
00260
00263 static bool isIntegerType(uint type);
00264
00266 inline bool isFPNumericType() const { return Field::isFPNumericType(type()); }
00267
00270 static bool isFPNumericType(uint type);
00271
00273 inline bool isDateTimeType() const { return Field::isDateTimeType(type()); }
00274
00277 static bool isDateTimeType(uint type);
00278
00280 inline bool isTextType() const { return Field::isTextType(type()); }
00281
00284 static bool isTextType(uint type);
00285
00286 uint options() const { return m_options; }
00287
00288 void setOptions(uint options) { m_options = options; }
00289
00291 inline QVariant::Type variantType() const { return variantType(type()); }
00292
00295 Type type() const;
00296
00298 inline QString typeName() const { return Field::typeName(type()); }
00299
00301 inline TypeGroup typeGroup() const { return Field::typeGroup(type()); }
00302
00304 inline QString typeGroupName() const { return Field::typeGroupName(type()); }
00305
00308 inline QString typeString() const { return Field::typeString(type()); }
00309
00312 inline QString typeGroupString() const { return Field::typeGroupString(type()); }
00313
00318 inline QString subType() const { return m_subType; }
00319
00322 inline void setSubType(const QString& subType) { m_subType = subType; }
00323
00326 inline QVariant defaultValue() const { return m_defaultValue; }
00327
00330 inline uint length() const { return m_length; }
00331
00334 inline uint precision() const { return m_precision; }
00335
00343 inline uint scale() const { return m_length; }
00344
00346
00361 inline int visibleDecimalPlaces() const { return m_visibleDecimalPlaces; }
00362
00364 inline uint constraints() const { return m_constraints; }
00365
00368 inline int order() const { return m_order; }
00369
00371 inline QString caption() const { return m_caption; }
00372
00374 inline QString captionOrName() const { return m_caption.isEmpty() ? m_name : m_caption; }
00375
00377 inline QString description() const { return m_desc; }
00378
00381 inline uint width() const { return m_width; }
00382
00384 inline bool isUnsigned() const { return m_options & Unsigned; }
00385
00388 inline bool hasEmptyProperty() const { return Field::hasEmptyProperty(type()); }
00389
00392 static bool hasEmptyProperty(uint type);
00393
00396 inline bool isAutoIncrementAllowed() const { return Field::isAutoIncrementAllowed(type()); }
00397
00400 static bool isAutoIncrementAllowed(uint type);
00401
00404 void setType(Type t);
00405
00407 void setName(const QString& name);
00408
00413 void setConstraints(uint c);
00414
00417 void setLength(uint l);
00418
00421 void setScale(uint s);
00422
00425 void setVisibleDecimalPlaces(int p);
00426
00428 void setPrecision(uint p);
00429
00431 void setUnsigned(bool u);
00432
00435 void setDefaultValue(const QVariant& def);
00436
00441 bool setDefaultValue(const QCString& def);
00442
00445 void setAutoIncrement(bool a);
00446
00456 void setPrimaryKey(bool p);
00457
00461 void setUniqueKey(bool u);
00462
00465 void setForeignKey(bool f);
00466
00471 void setNotNull(bool n);
00472
00477 void setNotEmpty(bool n);
00478
00490 void setIndexed(bool s);
00491
00493 void setCaption(const QString& caption) { m_caption=caption; }
00494
00496 void setDescription(const QString& description) { m_desc=description; }
00497
00500 void setWidth(uint w) { m_width=w; }
00501
00510 virtual bool isQueryAsterisk() const { return false; }
00511
00513 virtual QString debugString() const;
00514
00516 void debug();
00517
00521 inline KexiDB::BaseExpr *expression() { return m_expr; }
00522
00532 void setExpression(KexiDB::BaseExpr *expr);
00533
00537 inline bool isExpression() const { return m_expr!=NULL; }
00538
00539
00541 QValueVector<QString> enumHints() const { return m_hints; }
00542 QString enumHint(uint num) { return (num < m_hints.size()) ? m_hints.at(num) : QString::null; }
00544 void setEnumHints(const QValueVector<QString> &l) { m_hints = l; }
00545
00546
00549 QVariant customProperty(const QCString& propertyName,
00550 const QVariant& defaultValue = QVariant()) const;
00551
00553 void setCustomProperty(const QCString& propertyName, const QVariant& value);
00554
00556 typedef QMap<QCString,QVariant> CustomPropertiesMap;
00557
00559 inline const CustomPropertiesMap customProperties() const {
00560 return m_customProperties ? *m_customProperties : CustomPropertiesMap(); }
00561
00562 protected:
00568 Field(QuerySchema *querySchema, BaseExpr* expr = 0);
00569
00571 void init();
00572
00574 virtual Field* copy() const;
00575
00576 FieldList *m_parent;
00577
00578 QString m_name;
00579 QString m_subType;
00580 uint m_constraints;
00581 uint m_length;
00582 uint m_precision;
00583 int m_visibleDecimalPlaces;
00584 uint m_options;
00585 QVariant m_defaultValue;
00586 int m_order;
00587 QString m_caption;
00588 QString m_desc;
00589 uint m_width;
00590 QValueVector<QString> m_hints;
00591
00592 KexiDB::BaseExpr *m_expr;
00593 CustomPropertiesMap* m_customProperties;
00594
00596 class KEXI_DB_EXPORT FieldTypeNames : public QValueVector<QString> {
00597 public:
00598 FieldTypeNames();
00599 void init();
00600 QMap<QString,Type> str2num;
00601 protected:
00602 bool m_initialized : 1;
00603 };
00604
00606 class KEXI_DB_EXPORT FieldTypeGroupNames : public QValueVector<QString> {
00607 public:
00608 FieldTypeGroupNames();
00609 void init();
00610 QMap<QString,TypeGroup> str2num;
00611 protected:
00612 bool m_initialized : 1;
00613 };
00614
00616 static FieldTypeNames m_typeNames;
00617
00619 static FieldTypeGroupNames m_typeGroupNames;
00620
00621 private:
00622 Type m_type;
00623
00624 friend class Connection;
00625 friend class FieldList;
00626 friend class TableSchema;
00627 friend class QuerySchema;
00628 };
00629
00630 }
00631
00632 #endif