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
00090 Boolean = 5,
00091 Date = 6,
00092 DateTime = 7,
00093 Time = 8,
00094 Float = 9,
00095 Double = 10,
00096 Text = 11,
00097 LongText = 12,
00098 BLOB = 13,
00100 LastType = 13,
00102 Null = 64,
00104
00105 Asterisk = 128,
00108 Enum = 129,
00109 Map = 130
00110 };
00111
00112
00113 static uint defaultTextLength() { return 200; }
00114
00116 enum TypeGroup
00117 {
00118 InvalidGroup = 0,
00119 TextGroup = 1,
00120 IntegerGroup = 2,
00121 FloatGroup = 3,
00122 BooleanGroup = 4,
00123 DateTimeGroup = 5,
00124 BLOBGroup = 6,
00125
00126 LastTypeGroup = 6
00127 };
00128
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
00141 enum Options
00142 {
00143 NoOptions = 0,
00144 Unsigned = 1
00145 };
00146
00147 Field(TableSchema *tableSchema);
00148 Field(QuerySchema *querySchema, BaseExpr* expr = 0);
00149 Field();
00150
00151 Field(const QString& name, Type ctype,
00152 uint cconst=NoConstraints,
00153 uint options = NoOptions,
00154 uint length=0, uint precision=0,
00155 QVariant defaultValue=QVariant(),
00156 const QString& caption = QString::null,
00157 const QString& description = QString::null,
00158 uint width = 0);
00159
00161 Field(const Field& f);
00162
00163 virtual ~Field();
00164
00166 static QVariant::Type variantType(uint type);
00167
00170 static QString typeName(uint type);
00171
00175 static QString typeString(uint type);
00176
00178 static Type typeForString(const QString typeString);
00179
00181 static TypeGroup typeGroupForString(const QString typeGroupString);
00182
00184 static TypeGroup typeGroup(uint type);
00185
00188 static QString typeGroupName(uint typeGroup);
00189
00193 static QString typeGroupString(uint typeGroup);
00194
00195
00196 inline QString name() const { return m_name; }
00197
00201 virtual TableSchema* table() const;
00202
00208 virtual void setTable(TableSchema *table);
00209
00214 QuerySchema* query() const;
00215
00222 void setQuery(QuerySchema *query);
00223
00225 inline bool isAutoIncrement() const { return constraints() & AutoInc; }
00226
00228 inline bool isPrimaryKey() const { return constraints() & PrimaryKey; }
00229
00231 inline bool isUniqueKey() const { return constraints() & Unique; }
00232
00234 inline bool isForeignKey() const { return constraints() & ForeignKey; }
00235
00237 inline bool isNotNull() const { return constraints() & NotNull; }
00238
00240 inline bool isNotEmpty() const { return constraints() & NotEmpty; }
00241
00243 inline bool isIndexed() const { return constraints() & Indexed; }
00244
00246 inline bool isNumericType() const { return Field::isNumericType(type()); }
00247
00250 static bool isNumericType(uint type);
00251
00253 inline bool isIntegerType() const { return Field::isIntegerType(type()); }
00254
00257 static bool isIntegerType(uint type);
00258
00260 inline bool isFPNumericType() const { return Field::isFPNumericType(type()); }
00261
00264 static bool isFPNumericType(uint type);
00265
00267 inline bool isDateTimeType() const { return Field::isDateTimeType(type()); }
00268
00271 static bool isDateTimeType(uint type);
00272
00274 inline bool isTextType() const { return Field::isTextType(type()); }
00275
00278 static bool isTextType(uint type);
00279
00280 uint options() const { return m_options; }
00281
00282 void setOptions(uint options) { m_options = options; }
00283
00284 inline QVariant::Type variantType() const { return variantType(type()); }
00285
00288 Type type() const;
00289
00290 inline QString typeName() const { return Field::typeName(type()); }
00291
00292 inline TypeGroup typeGroup() const { return Field::typeGroup(type()); }
00293
00294 inline QString typeGroupName() const { return Field::typeGroupName(type()); }
00295
00296 inline QString typeString() const { return Field::typeString(type()); }
00297
00298 inline QString typeGroupString() const { return Field::typeGroupString(type()); }
00299
00305 inline QString subType() const { return m_subType; }
00306
00309 inline void setSubType(const QString& subType) { m_subType = subType; }
00310
00311 inline QVariant defaultValue() const { return m_defaultValue; }
00312
00314 inline uint length() const { return m_length; }
00315
00318 inline uint precision() const { return m_precision; }
00319
00327 inline uint scale() const { return m_length; }
00328
00330 inline uint constraints() const { return m_constraints; }
00331
00334 inline int order() const { return m_order; }
00335
00337 inline QString caption() const { return m_caption; }
00338
00340 inline QString captionOrName() const { return m_caption.isEmpty() ? m_name : m_caption; }
00341
00343 inline QString description() const { return m_desc; }
00344
00347 inline uint width() const { return m_width; }
00348
00350 inline bool isUnsigned() const { return m_options & Unsigned; }
00351
00352
00353
00356 inline bool hasEmptyProperty() const { return Field::hasEmptyProperty(type()); }
00357
00360 static bool hasEmptyProperty(uint type);
00361
00364 inline bool isAutoIncrementAllowed() const { return Field::isAutoIncrementAllowed(type()); }
00365
00368 static bool isAutoIncrementAllowed(uint type);
00369
00372 void setType(Type t);
00373
00375 void setName(const QString& name);
00376
00381 void setConstraints(uint c);
00382
00384 void setLength(uint l);
00385
00387 void setScale(uint s);
00388
00390 void setPrecision(uint p);
00391
00392 void setUnsigned(bool u);
00393
00394
00395
00396 void setDefaultValue(const QVariant& def);
00397
00402 bool setDefaultValue(const QCString& def);
00403
00406 void setAutoIncrement(bool a);
00407
00418 void setPrimaryKey(bool p);
00419
00423 void setUniqueKey(bool u);
00424
00427 void setForeignKey(bool f);
00428
00433 void setNotNull(bool n);
00434
00439 void setNotEmpty(bool n);
00440
00452 void setIndexed(bool s);
00453
00454 void setCaption(const QString& caption) { m_caption=caption; }
00455
00456 void setDescription(const QString& description) { m_desc=description; }
00457
00458 void setWidth(uint w) { m_width=w; }
00459
00468 inline bool isQueryAsterisk() const { return type() == Asterisk; }
00469
00471 virtual QString debugString();
00472
00474 void debug();
00475
00479 inline KexiDB::BaseExpr *expression() { return m_expr; }
00480
00490 void setExpression(KexiDB::BaseExpr *expr);
00491
00495 inline bool isExpression() const { return m_expr!=NULL; }
00496
00497
00499 QValueVector<QString> enumHints() const { return m_hints; }
00500 QString enumHint(uint num) { return (num < m_hints.size()) ? m_hints.at(num) : QString::null; }
00502 void setEnumHints(const QValueVector<QString> &l) { m_hints = l; }
00503
00504
00505 protected:
00507 void init();
00508
00509 FieldList *m_parent;
00510
00511 QString m_name;
00512 QString m_subType;
00513 uint m_constraints;
00514 uint m_length;
00515 uint m_precision;
00516 uint m_options;
00517 QVariant m_defaultValue;
00518 int m_order;
00519 QString m_caption;
00520 QString m_desc;
00521 uint m_width;
00522 QValueVector<QString> m_hints;
00523
00524 KexiDB::BaseExpr *m_expr;
00525
00526 class KEXI_DB_EXPORT FieldTypeNames : public QValueVector<QString> {
00527 public:
00528 FieldTypeNames();
00529 void init();
00530 QMap<QString,Type> str2num;
00531 protected:
00532 bool m_initialized : 1;
00533 };
00534 class KEXI_DB_EXPORT FieldTypeGroupNames : public QValueVector<QString> {
00535 public:
00536 FieldTypeGroupNames();
00537 void init();
00538 QMap<QString,TypeGroup> str2num;
00539 protected:
00540 bool m_initialized : 1;
00541 };
00542
00544 static FieldTypeNames m_typeNames;
00545
00547 static FieldTypeGroupNames m_typeGroupNames;
00548
00549 private:
00550 Type m_type;
00551
00552 friend class Connection;
00553 friend class FieldList;
00554 friend class TableSchema;
00555 friend class QuerySchema;
00556 };
00557
00558 }
00559
00560 #endif