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-2007 Jarosław Staniek <staniek@kde.org>
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.
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.
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.
22
#ifndef KEXIDB_FIELD_H
23
#define KEXIDB_FIELD_H
29
#include <QStringList>
32
#include <kexiutils/utils.h>
33
#include "kexidb_export.h"
43
//! Meta-data for a field
44
/*! KexiDB::Field provides information about single database field.
46
Field class has defined following members:
49
- database constraints
51
- length (make sense mostly for string types)
52
- precision (for floating-point type)
54
- caption (user readable name that can be e.g. translated)
55
- description (user readable name additional text, can be useful for developers)
56
- width (a hint for displaying in tabular mode or as text box)
58
Field can also have assigned expression (see KexiDB::BaseExpr class,
59
and expression() method).
60
If an expression is defined, then field's name is
62
Note that aliases for fields are defined within query, not in Field object,
63
because the same field can be used in different queries with different alias.
65
Notes for advanced use: Field obeject is designed to be owned by a parent object.
66
Such a parent object can be KexiDB::TableSchema, if the field defines single table column,
67
or KexiDB::QuerySchema, if the field defines an expression (KexiDB::BaseExpr class).
69
Using expression class for fields allos to define expressions within queries like
70
"SELECT AVG(price) FROM products"
72
You can choose whether your field is owned by query or table,
73
using appropriate constructor, or using parameterless constructor and
74
calling setTable() or setQuery() later.
77
class KEXI_DB_EXPORT Field
80
typedef KexiUtils::AutodeletedList<Field*> List; //!< list of fields
81
typedef QVector<Field*> Vector; //!< vector of fields
82
typedef QList<Field*>::ConstIterator ListIterator; //!< iterator for list of fields
83
typedef QPair<Field*, Field*> Pair; //!< fields pair
84
typedef QList<Pair> PairList; //!< list of fields pair
86
/*! Unified (most common used) types of fields. */
88
InvalidType = 0, /*!< Unsupported/Unimplemented type */
89
FirstType = 1, /*! First type */
90
Byte = 1, /*!< 1 byte, signed or unsigned */
91
ShortInteger = 2,/*!< 2 bytes, signed or unsigned */
92
Integer = 3, /*!< 4 bytes, signed or unsigned */
93
BigInteger = 4, /*!< 8 bytes, signed or unsigned */
94
Boolean = 5, /*!< 0 or 1 */
98
Float = 9, /*!< 4 bytes */
99
Double = 10, /*!< 8 bytes */
100
Text = 11, /*!< Other name: Varchar; no more than 200 bytes, for efficiency */
101
LongText = 12, /*!< Other name: Memo. More than 200 bytes*/
102
BLOB = 13, /*!< Large binary object */
104
LastType = 13, /*!< This line should be at the end of the list of types! */
106
Null = 64, /*!< Used for fields that are "NULL" expressions. */
108
//! Special, internal types:
109
Asterisk = 128, /*!< Used in QueryAsterisk subclass objects only,
110
not used in table definitions,
111
but only in query definitions */
112
Enum = 129, /*!< An integer internal with a string list of hints */
113
Map = 130 /*!< Mapping from string to string list (more generic than Enum */
116
//TODO: make this configurable
117
static uint defaultTextLength() {
121
/*! Type groups for fields. */
129
BLOBGroup = 6, /* large binary object */
131
LastTypeGroup = 6 // This line should be at the end of the enum!
134
/*! Possible constraints defined for a field. */
142
NotEmpty = 32, //!< only legal for string-like and blob fields
146
/*! Possible options defined for a field. */
152
/*! Creates a database field as a child of \a tableSchema table
153
No other properties are set (even the name), so these should be set later. */
154
Field(TableSchema *tableSchema);
156
/*! Creates a database field without any properties set.
157
These should be set later. */
160
/*! Creates a database field with specified properties. */
161
Field(const QString& name, Type ctype,
162
uint cconst = NoConstraints,
163
uint options = NoOptions,
164
uint length = 0, uint precision = 0,
165
QVariant defaultValue = QVariant(),
166
const QString& caption = QString(),
167
const QString& description = QString(),
170
/*! Copy constructor. */
171
Field(const Field& f);
175
//! Converts type \a type to QVariant equivalent as accurate as possible
176
static QVariant::Type variantType(uint type);
178
/*! \return a i18n'd type name for \a type (\a type has to be an element from Field::Type,
179
not greater than Field::LastType) */
180
static QString typeName(uint type);
182
/*! \return list of all available i18n'd type names. */
183
static QStringList typeNames();
185
/*! \return type string for \a type, e.g. "Integer" for Integer type
186
(not-i18n'd, \a type has to be an element from Field::Type,
187
not greater than Field::LastType) */
188
static QString typeString(uint type);
190
/*! \return type for a given \a typeString */
191
static Type typeForString(const QString& typeString);
193
/*! \return type group for a given \a typeGroupString */
194
static TypeGroup typeGroupForString(const QString& typeGroupString);
196
/*! \return group for \a type */
197
static TypeGroup typeGroup(uint type);
199
/*! \return a i18n'd group name for \a typeGroup
200
(\a typeGroup has to be an element from Field::TypeGroup) */
201
static QString typeGroupName(uint typeGroup);
203
/*! \return list of all available i18n'd type group names. */
204
static QStringList typeGroupNames();
206
/*! \return type group string for \a typeGroup, e.g. "IntegerGroup" for IntegerGroup type
207
(not-i18n'd, \a type has to be an element from Field::Type,
208
not greater than Field::LastType) */
209
static QString typeGroupString(uint typeGroup);
211
/* ! \return the name of this field */
212
inline QString name() const {
216
/*! \return table schema of table that owns this field
217
or null if it has no table assigned.
219
virtual TableSchema* table() const;
221
/*! Sets \a table schema of table that owns this field.
222
This does not adds the field to \a table object.
223
You do not need to call this method by hand.
224
Call TableSchema::addField(Field *field) instead.
226
virtual void setTable(TableSchema *table);
228
/*! For special use when the field defines expression.
229
\return query schema of query that owns this field
230
or null if it has no query assigned.
232
QuerySchema* query() const;
234
/*! For special use when field defines expression.
235
Sets \a query schema of query that owns this field.
236
This does not adds the field to \a query object.
237
You do not need to call this method by hand.
238
Call QuerySchema::addField() instead.
240
void setQuery(QuerySchema *query);
242
/*! \return true if the field is autoincrement (e.g. integer/numeric) */
243
inline bool isAutoIncrement() const {
244
return constraints() & AutoInc;
247
/*! \return true if the field is member of single-field primary key */
248
inline bool isPrimaryKey() const {
249
return constraints() & PrimaryKey;
252
/*! \return true if the field is member of single-field unique key */
253
inline bool isUniqueKey() const {
254
return constraints() & Unique;
257
/*! \return true if the field is member of single-field foreign key */
258
inline bool isForeignKey() const {
259
return constraints() & ForeignKey;
262
/*! \return true if the field is not allowed to be null */
263
inline bool isNotNull() const {
264
return constraints() & NotNull;
267
/*! \return true if the field is not allowed to be null */
268
inline bool isNotEmpty() const {
269
return constraints() & NotEmpty;
272
/*! \return true if the field is indexed using single-field database index. */
273
inline bool isIndexed() const {
274
return constraints() & Indexed;
277
/*! \return true if the field is of any numeric type (integer or floating point) */
278
inline bool isNumericType() const {
279
return Field::isNumericType(type());
282
/*! static version of isNumericType() method
283
*! \return true if the field is of any numeric type (integer or floating point)*/
284
static bool isNumericType(uint type);
286
/*! \return true if the field is of any integer type */
287
inline bool isIntegerType() const {
288
return Field::isIntegerType(type());
291
/*! static version of isIntegerType() method
292
*! \return true if the field is of any integer type */
293
static bool isIntegerType(uint type);
295
/*! \return true if the field is of any floating point numeric type */
296
inline bool isFPNumericType() const {
297
return Field::isFPNumericType(type());
300
/*! static version of isFPNumericType() method
301
*! \return true if the field is of any floating point numeric type */
302
static bool isFPNumericType(uint type);
304
/*! \return true if the field is of any date or time related type */
305
inline bool isDateTimeType() const {
306
return Field::isDateTimeType(type());
309
/*! static version of isDateTimeType() method
310
*! \return true if the field is of any date or time related type */
311
static bool isDateTimeType(uint type);
313
/*! @return true if the field is of any text type */
314
inline bool isTextType() const {
315
return Field::isTextType(type());
318
/*! static version of isTextType() method
319
*! \return true if the field is of any text type */
320
static bool isTextType(uint type);
322
uint options() const {
326
void setOptions(uint options) {
330
//! Converts field's type to QVariant equivalent as accurate as possible
331
inline QVariant::Type variantType() const {
332
return variantType(type());
335
/*! \return a type for this field. If there's expression assigned,
336
type of the expression is returned instead. */
339
//! \return a i18n'd type name for this field
340
inline QString typeName() const {
341
return Field::typeName(type());
344
//! \return type group for this field
345
inline TypeGroup typeGroup() const {
346
return Field::typeGroup(type());
349
//! \return a i18n'd type group name for this field
350
inline QString typeGroupName() const {
351
return Field::typeGroupName(type());
354
//! \return a type string for this field,
355
//! for example "Integer" string for Field::Integer type.
356
inline QString typeString() const {
357
return Field::typeString(type());
360
//! \return a type group string for this field,
361
//! for example "Integer" string for Field::IntegerGroup.
362
inline QString typeGroupString() const {
363
return Field::typeGroupString(type());
366
/*! \return (optional) subtype for this field.
367
Subtype is a string providing additional hint for field's type.
368
E.g. for BLOB type, it can be a MIME type or certain QVariant type name,
369
for example: "QPixmap", "QColor" or "QFont" */
370
inline QString subType() const {
374
/*! Sets (optional) subtype for this field.
376
inline void setSubType(const QString& subType) {
380
//! \return default value for this field. Null value means there
381
//! is no default value declared. The variant value is compatible with field's type.
382
inline QVariant defaultValue() const {
383
return m_defaultValue;
386
/*! \return length of text, only meaningful if the field type is text.
387
0 means "default length". */
388
inline uint length() const {
392
/*! \return precision for numeric and other fields that have both length (scale)
393
and precision (floating point types). */
394
inline uint precision() const {
398
/*! \return scale for numeric and other fields that have both length (scale)
399
and precision (floating point types).
400
The scale of a numeric is the count of decimal digits in the fractional part,
401
to the right of the decimal point. The precision of a numeric is the total count
402
of significant digits in the whole number, that is, the number of digits
403
to both sides of the decimal point. So the number 23.5141 has a precision
404
of 6 and a scale of 4. Integers can be considered to have a scale of zero. */
405
inline uint scale() const {
409
//! @todo should we keep extended properties here or move them to a QVariant dictionary?
410
/*! \return number of decimal places that should be visible to the user,
411
e.g. within table view widget, form or printout.
412
Only meaningful if the field type is floating point or (in the future: decimal or currency).
414
- Any value less than 0 (-1 is the default) means that there should be displayed all digits
415
of the fractional part, except the ending zeros. This is known as "auto" mode.
416
For example, 12.345000 becomes 12.345.
418
- Value of 0 means that all the fractional part should be hidden (as well as the dot or comma).
419
For example, 12.345000 becomes 12.
421
- Value N > 0 means that the fractional part should take exactly N digits.
422
If the fractional part is shorter than N, additional zeros are appended.
423
For example, "12.345" becomes "12.345000" if N=6.
425
inline int visibleDecimalPlaces() const {
426
return m_visibleDecimalPlaces;
429
/*! \return the constraints defined for this field. */
430
inline uint constraints() const {
431
return m_constraints;
434
/*! \return order of this field in containing table (counting starts from 0)
435
(-1 if unspecified). */
436
inline int order() const {
440
/*! \return caption of this field. */
441
inline QString caption() const {
445
/*! \return caption of this field or - if empty - return its name. */
446
inline QString captionOrName() const {
447
return m_caption.isEmpty() ? m_name : m_caption;
450
/*! \return description text for this field. */
451
inline QString description() const {
455
/*! \return width of this field (usually in pixels or points)
456
0 (the default) means there is no hint for the width. */
457
inline uint width() const {
461
//! if the type has the unsigned attribute
462
inline bool isUnsigned() const {
463
return m_options & Unsigned;
466
/*! \return true if this field has EMPTY property (i.e. it is of type
467
string or is a BLOB). */
468
inline bool hasEmptyProperty() const {
469
return Field::hasEmptyProperty(type());
472
/*! static version of hasEmptyProperty() method
473
\return true if this field type has EMPTY property (i.e. it is string or BLOB type) */
474
static bool hasEmptyProperty(uint type);
476
/*! \return true if this field can be auto-incremented.
477
Actually, returns true for integer field type. \sa IntegerType, isAutoIncrement() */
478
inline bool isAutoIncrementAllowed() const {
479
return Field::isAutoIncrementAllowed(type());
482
/*! static version of isAutoIncrementAllowed() method
483
\return true if this field type can be auto-incremented. */
484
static bool isAutoIncrementAllowed(uint type);
486
/*! Sets type \a t for this field. This does nothing if there's already expression assigned,
488
void setType(Type t);
490
/*! Sets name \a name for this field. */
491
void setName(const QString& name);
493
/*! Sets constraints to \a c. If PrimaryKey is set in \a c, also
494
constraits implied by being primary key are enforced (see setPrimaryKey()).
495
If Indexed is not set in \a c, constraits implied by not being are
496
enforced as well (see setIndexed()). */
497
void setConstraints(uint c);
499
/*! Sets length for this field. Only works for Text Type (even not LongText!).
500
0 means "default length". @see length() */
501
void setLength(uint l);
503
/*! Sets scale for this field. Only works for floating-point types.
505
void setScale(uint s);
507
/*! Sets number of decimal places that should be visible to the user.
508
@see visibleDecimalPlaces() */
509
void setVisibleDecimalPlaces(int p);
511
/*! Sets scale for this field. Only works for floating-point types. */
512
void setPrecision(uint p);
514
/*! Sets unsigned flag for this field. Only works for integer types. */
515
void setUnsigned(bool u);
517
/*! Sets default value for this field. Setting null value removes the default value.
518
@see defaultValue() */
519
void setDefaultValue(const QVariant& def);
521
/*! Sets default value decoded from QByteArray.
522
Decoding errors are detected (value is strictly checked against field type)
523
- if one is encountered, default value is cleared (defaultValue()==QVariant()).
524
\return true if given value was valid for field type. */
525
bool setDefaultValue(const QByteArray& def);
527
/*! Sets auto increment flag. Only available to set true,
528
if isAutoIncrementAllowed() is true. */
529
void setAutoIncrement(bool a);
531
/*! Specifies whether the field is single-field primary key or not
532
(KexiDB::PrimeryKey item).
533
Use this with caution. Setting this to true implies setting:
539
Setting this to false implies setting setAutoIncrement(false). */
540
void setPrimaryKey(bool p);
542
/*! Specifies whether the field has single-field unique constraint or not
543
(KexiDB::Unique item). Setting this to true implies setting Indexed flag
544
to true (setIndexed(true)), because index is required it control unique constraint. */
545
void setUniqueKey(bool u);
547
/*! Sets whether the field has to be declared with single-field foreign key.
548
Used in IndexSchema::setForeigKey(). */
549
void setForeignKey(bool f);
551
/*! Specifies whether the field has single-field unique constraint or not
552
(KexiDB::NotNull item). Setting this to true implies setting Indexed flag
553
to true (setIndexed(true)), because index is required it control
554
not null constraint. */
555
void setNotNull(bool n);
557
/*! Specifies whether the field has single-field unique constraint or not
558
(KexiDB::NotEmpty item). Setting this to true implies setting Indexed flag
559
to true (setIndexed(true)), because index is required it control
560
not empty constraint. */
561
void setNotEmpty(bool n);
563
/*! Specifies whether the field is indexed (KexiDB::Indexed item)
564
(by single-field implicit index) or not.
565
Use this with caution. Since index is used to control unique,
566
not null/empty constratins, setting this to false implies setting:
567
- setPrimaryKey(false)
568
- setUniqueKey(false)
571
because above flags need index to be present.
572
Similarly, setting one of the above flags to true, will automatically
573
do setIndexed(true) for the same reason. */
574
void setIndexed(bool s);
576
/*! Sets caption for this field to \a caption. */
577
void setCaption(const QString& caption) {
581
/*! Sets description for this field to \a description. */
582
void setDescription(const QString& description) {
583
m_desc = description;
586
/*! Sets visible width for this field to \a w
587
(usually in pixels or points). 0 means there is no hint for the width. */
588
void setWidth(uint w) {
592
/*! There can be added asterisks (QueryAsterisk objects)
593
to query schemas' field list. QueryAsterisk subclasses Field class,
594
and to check if the given object (pointed by Field*)
595
is asterisk or just ordinary field definition,
596
you can call this method. This is just effective version of QObject::isA().
597
Every QueryAsterisk object returns true here,
598
and every Field object returns false.
600
inline bool isQueryAsterisk() const {
601
return m_type == Field::Asterisk;
604
/*! \return string for debugging purposes. */
605
virtual QString debugString() const;
607
/*! Shows debug information about this field. */
610
/*! \return KexiDB::BaseExpr object if the field value is an
611
expression. Unless the expression is set with setExpression(), it is null.
613
inline KexiDB::BaseExpr *expression() {
617
/*! Sets expression data \a expr. If there was
618
already expression set, it is destroyed before new assignment.
619
This Field object becames owner of \a expr object,
620
so you do not have to worry about deleting it later.
621
If the \a expr is null, current field's expression is deleted, if exists.
623
Because the field defines an expression, it should be assigned to a query,
626
void setExpression(KexiDB::BaseExpr *expr);
628
/*! \return true if there is expression defined for this field.
629
This method is provided for better readibility
630
- does the same as expression()!=NULL but */
631
inline bool isExpression() const {
632
return m_expr != NULL;
636
/*! \return the hints for enum fields. */
637
QVector<QString> enumHints() const {
640
QString enumHint(uint num) {
641
return (num < (uint)m_hints.size()) ? m_hints.at(num) : QString();
643
/*! sets the hint for enum fields */
644
void setEnumHints(const QVector<QString> &l) {
649
/*! \return custom property \a propertyName.
650
If there is no such a property, \a defaultValue is returned. */
651
QVariant customProperty(const QByteArray& propertyName,
652
const QVariant& defaultValue = QVariant()) const;
654
//! Sets value \a value for custom property \a propertyName
655
void setCustomProperty(const QByteArray& propertyName, const QVariant& value);
657
//! A data type used for handling custom properties of a field
658
typedef QHash<QByteArray, QVariant> CustomPropertiesMap;
660
//! \return all custom properties
661
inline const CustomPropertiesMap customProperties() const {
662
return m_customProperties ? *m_customProperties : CustomPropertiesMap();
666
/*! Creates a database field as a child of \a querySchema table
667
Assigns \a expr expression to this field, if present.
668
Used internally by query schemas, e.g. to declare asterisks or
669
to add expression columns.
670
No other properties are set, so these should be set later. */
671
Field(QuerySchema *querySchema, BaseExpr* expr = 0);
673
/*! @internal Used by constructors. */
676
//! \return a deep copy of this object. Used in @ref FieldList(const FieldList& fl).
677
virtual Field* copy() const;
679
FieldList *m_parent; //!< In most cases this points to a TableSchema
680
//!< object that field is assigned.
684
uint m_length; //!< also used for storing scale for floating point types
686
int m_visibleDecimalPlaces; //!< used in visibleDecimalPlaces()
688
QVariant m_defaultValue;
693
QVector<QString> m_hints;
695
KexiDB::BaseExpr *m_expr;
696
CustomPropertiesMap* m_customProperties;
698
//! @internal Used in m_typeNames member to handle i18n'd type names
699
class KEXI_DB_EXPORT FieldTypeNames : public QVector<QString>
704
QHash<QString, Type> str2num;
707
bool m_initialized : 1;
710
//! @internal Used in m_typeGroupNames member to handle i18n'd type group names
711
class KEXI_DB_EXPORT FieldTypeGroupNames : public QVector<QString>
714
FieldTypeGroupNames();
716
QHash<QString, TypeGroup> str2num;
719
bool m_initialized : 1;
722
//! real i18n'd type names (and not-i18n'd type name strings)
723
static FieldTypeNames m_typeNames;
725
//! real i18n'd type group names (and not-i18n'd group name strings)
726
static FieldTypeGroupNames m_typeGroupNames;
731
friend class Connection;
732
friend class FieldList;
733
friend class TableSchema;
734
friend class QuerySchema;