kexi

field.h
1/* This file is part of the KDE project
2 Copyright (C) 2002 Lucijan Busch <lucijan@gmx.at>
3 Copyright (C) 2002 Joseph Wenninger <jowenn@kde.org>
4 Copyright (C) 2003-2006 Jaroslaw Staniek <js@iidea.pl>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22#ifndef KEXIDB_FIELD_H
23#define KEXIDB_FIELD_H
24
25#include <tqvariant.h>
26#include <tqstring.h>
27#include <tqpair.h>
28#include <tqvaluevector.h>
29#include <tqptrvector.h>
30#include "kexidb/kexidb_export.h"
31namespace KexiDB {
32
33class TableSchema;
34class QuerySchema;
35class FieldList;
36class BaseExpr;
37
39
72class KEXI_DB_EXPORT Field
73{
74 public:
75 typedef TQPtrList<Field> List;
76 typedef TQPtrVector<Field> Vector;
77 typedef TQPtrListIterator<Field> ListIterator;
78 typedef TQPair<Field*,Field*> Pair;
79 typedef TQPtrList<Pair> PairList;
80
82 enum Type
83 {
84 InvalidType = 0,
85 Byte = 1,
86 ShortInteger = 2,
87 Integer = 3,
88 BigInteger = 4,
89 Boolean = 5,
90 Date = 6,
91 DateTime = 7,
92 Time = 8,
93 Float = 9,
94 Double = 10,
95 Text = 11,
96 LongText = 12,
97 BLOB = 13,
99 LastType = 13,
101 Null = 64,
104 Asterisk = 128,
107 Enum = 129,
108 Map = 130
109 };
110
111//TODO: make this configurable
112 static uint defaultTextLength() { return 200; }
113
116 {
117 InvalidGroup = 0,
118 TextGroup = 1,
119 IntegerGroup = 2,
120 FloatGroup = 3,
121 BooleanGroup = 4,
122 DateTimeGroup = 5,
123 BLOBGroup = 6, /* large binary object */
124
125 LastTypeGroup = 6 // This line should be at the end of the enum!
126 };
127
130 {
131 NoConstraints = 0,
132 AutoInc = 1,
133 Unique = 2,
134 PrimaryKey = 4,
135 ForeignKey = 8,
136 NotNull = 16,
137 NotEmpty = 32,
138 Indexed = 64
139 };
140
143 {
144 NoOptions = 0,
145 Unsigned = 1
146 };
147
150 Field(TableSchema *tableSchema);
151
154 Field();
155
157 Field(const TQString& name, Type ctype,
158 uint cconst=NoConstraints,
159 uint options = NoOptions,
160 uint length=0, uint precision=0,
161 TQVariant defaultValue=TQVariant(),
162 const TQString& caption = TQString(),
163 const TQString& description = TQString(),
164 uint width = 0);
165
167 Field(const Field& f);
168
169 virtual ~Field();
170
172 static TQVariant::Type variantType(uint type);
173
176 static TQString typeName(uint type);
177
181 static TQString typeString(uint type);
182
184 static Type typeForString(const TQString& typeString);
185
187 static TypeGroup typeGroupForString(const TQString& typeGroupString);
188
190 static TypeGroup typeGroup(uint type);
191
194 static TQString typeGroupName(uint typeGroup);
195
199 static TQString typeGroupString(uint typeGroup);
200
201 /* ! \return the name of this field */
202 inline TQString name() const { return m_name; }
203
207 virtual TableSchema* table() const;
208
214 virtual void setTable(TableSchema *table);
215
220 QuerySchema* query() const;
221
228 void setQuery(QuerySchema *query);
229
231 inline bool isAutoIncrement() const { return constraints() & AutoInc; }
232
234 inline bool isPrimaryKey() const { return constraints() & PrimaryKey; }
235
237 inline bool isUniqueKey() const { return constraints() & Unique; }
238
240 inline bool isForeignKey() const { return constraints() & ForeignKey; }
241
243 inline bool isNotNull() const { return constraints() & NotNull; }
244
246 inline bool isNotEmpty() const { return constraints() & NotEmpty; }
247
249 inline bool isIndexed() const { return constraints() & Indexed; }
250
252 inline bool isNumericType() const { return Field::isNumericType(type()); }
253
256 static bool isNumericType(uint type);
257
259 inline bool isIntegerType() const { return Field::isIntegerType(type()); }
260
263 static bool isIntegerType(uint type);
264
266 inline bool isFPNumericType() const { return Field::isFPNumericType(type()); }
267
270 static bool isFPNumericType(uint type);
271
273 inline bool isDateTimeType() const { return Field::isDateTimeType(type()); }
274
277 static bool isDateTimeType(uint type);
278
280 inline bool isTextType() const { return Field::isTextType(type()); }
281
284 static bool isTextType(uint type);
285
286 uint options() const { return m_options; }
287
288 void setOptions(uint options) { m_options = options; }
289
291 inline TQVariant::Type variantType() const { return variantType(type()); }
292
295 Type type() const;
296
298 inline TQString typeName() const { return Field::typeName(type()); }
299
301 inline TypeGroup typeGroup() const { return Field::typeGroup(type()); }
302
304 inline TQString typeGroupName() const { return Field::typeGroupName(type()); }
305
308 inline TQString typeString() const { return Field::typeString(type()); }
309
312 inline TQString typeGroupString() const { return Field::typeGroupString(type()); }
313
318 inline TQString subType() const { return m_subType; }
319
322 inline void setSubType(const TQString& subType) { m_subType = subType; }
323
326 inline TQVariant defaultValue() const { return m_defaultValue; }
327
330 inline uint length() const { return m_length; }
331
334 inline uint precision() const { return m_precision; }
335
343 inline uint scale() const { return m_length; }
344
346
361 inline int visibleDecimalPlaces() const { return m_visibleDecimalPlaces; }
362
364 inline uint constraints() const { return m_constraints; }
365
368 inline int order() const { return m_order; }
369
371 inline TQString caption() const { return m_caption; }
372
374 inline TQString captionOrName() const { return m_caption.isEmpty() ? m_name : m_caption; }
375
377 inline TQString description() const { return m_desc; }
378
381 inline uint width() const { return m_width; }
382
384 inline bool isUnsigned() const { return m_options & Unsigned; }
385
388 inline bool hasEmptyProperty() const { return Field::hasEmptyProperty(type()); }
389
392 static bool hasEmptyProperty(uint type);
393
396 inline bool isAutoIncrementAllowed() const { return Field::isAutoIncrementAllowed(type()); }
397
400 static bool isAutoIncrementAllowed(uint type);
401
404 void setType(Type t);
405
407 void setName(const TQString& name);
408
413 void setConstraints(uint c);
414
417 void setLength(uint l);
418
421 void setScale(uint s);
422
425 void setVisibleDecimalPlaces(int p);
426
428 void setPrecision(uint p);
429
431 void setUnsigned(bool u);
432
435 void setDefaultValue(const TQVariant& def);
436
441 bool setDefaultValue(const TQCString& def);
442
445 void setAutoIncrement(bool a);
446
456 void setPrimaryKey(bool p);
457
461 void setUniqueKey(bool u);
462
465 void setForeignKey(bool f);
466
471 void setNotNull(bool n);
472
477 void setNotEmpty(bool n);
478
490 void setIndexed(bool s);
491
493 void setCaption(const TQString& caption) { m_caption=caption; }
494
496 void setDescription(const TQString& description) { m_desc=description; }
497
500 void setWidth(uint w) { m_width=w; }
501
510 virtual bool isQueryAsterisk() const { return false; }
511
513 virtual TQString debugString() const;
514
516 void debug();
517
521 inline KexiDB::BaseExpr *expression() { return m_expr; }
522
532 void setExpression(KexiDB::BaseExpr *expr);
533
537 inline bool isExpression() const { return m_expr!=NULL; }
538
539//<TMP>
541 TQValueVector<TQString> enumHints() const { return m_hints; }
542 TQString enumHint(uint num) { return (num < m_hints.size()) ? m_hints.at(num) : TQString(); }
544 void setEnumHints(const TQValueVector<TQString> &l) { m_hints = l; }
545//</TMP>
546
549 TQVariant customProperty(const TQCString& propertyName,
550 const TQVariant& defaultValue = TQVariant()) const;
551
553 void setCustomProperty(const TQCString& propertyName, const TQVariant& value);
554
556 typedef TQMap<TQCString,TQVariant> CustomPropertiesMap;
557
560 return m_customProperties ? *m_customProperties : CustomPropertiesMap(); }
561
562 protected:
568 Field(QuerySchema *querySchema, BaseExpr* expr = 0);
569
571 void init();
572
574 virtual Field* copy() const;
575
578 TQString m_name;
579 TQString m_subType;
580 uint m_constraints;
581 uint m_length;
582 uint m_precision;
584 uint m_options;
585 TQVariant m_defaultValue;
586 int m_order;
587 TQString m_caption;
588 TQString m_desc;
589 uint m_width;
590 TQValueVector<TQString> m_hints;
591
592 KexiDB::BaseExpr *m_expr;
593 CustomPropertiesMap* m_customProperties;
594
596 class KEXI_DB_EXPORT FieldTypeNames : public TQValueVector<TQString> {
597 public:
598 FieldTypeNames();
599 void init();
600 TQMap<TQString,Type> str2num;
601 protected:
602 bool m_initialized : 1;
603 };
604
606 class KEXI_DB_EXPORT FieldTypeGroupNames : public TQValueVector<TQString> {
607 public:
608 FieldTypeGroupNames();
609 void init();
610 TQMap<TQString,TypeGroup> str2num;
611 protected:
612 bool m_initialized : 1;
613 };
614
616 static FieldTypeNames m_typeNames;
617
619 static FieldTypeGroupNames m_typeGroupNames;
620
621 private:
622 Type m_type;
623
624 friend class Connection;
625 friend class FieldList;
626 friend class TableSchema;
627 friend class QuerySchema;
628};
629
630} //namespace KexiDB
631
632#endif
A base class for all expressions.
Definition expression.h:67
Provides database connection, allowing queries and data modification.
Definition connection.h:62
Meta-data for a field.
Definition field.h:73
bool isPrimaryKey() const
Definition field.h:234
TQString typeName() const
Definition field.h:298
int order() const
Definition field.h:368
KexiDB::BaseExpr * expression()
Definition field.h:521
TQPair< Field *, Field * > Pair
fields pair
Definition field.h:78
bool isNotNull() const
Definition field.h:243
bool isIndexed() const
Definition field.h:249
TQValueVector< TQString > enumHints() const
Definition field.h:541
bool isAutoIncrementAllowed() const
Definition field.h:396
TQString caption() const
Definition field.h:371
void setDescription(const TQString &description)
Definition field.h:496
void setCaption(const TQString &caption)
Definition field.h:493
TQString captionOrName() const
Definition field.h:374
TQVariant::Type variantType() const
Converts field's type to TQVariant equivalent as accurate as possible.
Definition field.h:291
const CustomPropertiesMap customProperties() const
Definition field.h:559
TQString typeString() const
Definition field.h:308
TQString typeGroupString() const
Definition field.h:312
void setEnumHints(const TQValueVector< TQString > &l)
Definition field.h:544
bool hasEmptyProperty() const
Definition field.h:388
TQPtrList< Field > List
list of fields
Definition field.h:75
void setSubType(const TQString &subType)
Definition field.h:322
FieldList * m_parent
In most cases this points to a TableSchema object that field is assigned.
Definition field.h:576
uint m_length
also used for storing scale for floating point types
Definition field.h:581
bool isFPNumericType() const
Definition field.h:266
TQPtrList< Pair > PairList
list of fields pair
Definition field.h:79
bool isIntegerType() const
Definition field.h:259
bool isAutoIncrement() const
Definition field.h:231
TQPtrListIterator< Field > ListIterator
iterator for list of fields
Definition field.h:77
virtual bool isQueryAsterisk() const
Definition field.h:510
static FieldTypeGroupNames m_typeGroupNames
real i18n'd type group names (and not-i18n'd group name strings)
Definition field.h:619
bool isNotEmpty() const
Definition field.h:246
bool isNumericType() const
Definition field.h:252
TypeGroup typeGroup() const
Definition field.h:301
bool isExpression() const
Definition field.h:537
uint scale() const
Definition field.h:343
int visibleDecimalPlaces() const
Definition field.h:361
TQString typeGroupName() const
Definition field.h:304
TQPtrVector< Field > Vector
vector of fields
Definition field.h:76
bool isTextType() const
Definition field.h:280
bool isForeignKey() const
Definition field.h:240
int m_visibleDecimalPlaces
used in visibleDecimalPlaces()
Definition field.h:583
TQString description() const
Definition field.h:377
TQMap< TQCString, TQVariant > CustomPropertiesMap
A data type used for handling custom properties of a field.
Definition field.h:556
uint precision() const
Definition field.h:334
TQString subType() const
Definition field.h:318
void setWidth(uint w)
Definition field.h:500
TQVariant defaultValue() const
Definition field.h:326
bool isUnsigned() const
if the type has the unsigned attribute
Definition field.h:384
bool isUniqueKey() const
Definition field.h:237
bool isDateTimeType() const
Definition field.h:273
uint constraints() const
Definition field.h:364
static FieldTypeNames m_typeNames
real i18n'd type names (and not-i18n'd type name strings)
Definition field.h:616
uint width() const
Definition field.h:381
uint length() const
Definition field.h:330
KexiDB::QuerySchema provides information about database query.
High-level database connectivity library with database backend drivers.
KEXI_DB_EXPORT void debug(const QuerySchemaParameterList &list)
Shows debug information for list.