~ubuntu-branches/ubuntu/precise/koffice/precise

« back to all changes in this revision

Viewing changes to kexi/kexidb/field.h

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2010-09-21 15:36:35 UTC
  • mfrom: (1.4.1 upstream) (60.2.11 maverick)
  • Revision ID: james.westby@ubuntu.com-20100921153635-6tejqkiro2u21ydi
Tags: 1:2.2.2-0ubuntu3
Add kubuntu_03_fix-crash-on-closing-sqlite-connection-2.2.2.diff and
kubuntu_04_support-large-memo-values-for-msaccess-2.2.2.diff as
recommended by upstream http://kexi-
project.org/wiki/wikiview/index.php@Kexi2.2_Patches.html#sqlite_stab
ility

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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>
 
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 <QVariant>
 
26
#include <QString>
 
27
#include <QPair>
 
28
#include <QVector>
 
29
#include <QStringList>
 
30
#include <QHash>
 
31
 
 
32
#include <kexiutils/utils.h>
 
33
#include "kexidb_export.h"
 
34
 
 
35
namespace KexiDB
 
36
{
 
37
 
 
38
class TableSchema;
 
39
class QuerySchema;
 
40
class FieldList;
 
41
class BaseExpr;
 
42
 
 
43
//! Meta-data for a field
 
44
/*! KexiDB::Field provides information about single database field.
 
45
 
 
46
 Field class has defined following members:
 
47
 - name
 
48
 - type
 
49
 - database constraints
 
50
 - additional options
 
51
 - length (make sense mostly for string types)
 
52
 - precision (for floating-point type)
 
53
 - defaultValue
 
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)
 
57
 
 
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
 
61
 
 
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.
 
64
 
 
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).
 
68
 
 
69
 Using expression class for fields allos to define expressions within queries like
 
70
 "SELECT AVG(price) FROM products"
 
71
 
 
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.
 
75
 
 
76
*/
 
77
class KEXI_DB_EXPORT Field
 
78
{
 
79
public:
 
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
 
85
 
 
86
    /*! Unified (most common used) types of fields. */
 
87
    enum Type {
 
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 */
 
95
        Date = 6,        /*!< */
 
96
        DateTime = 7,    /*!< */
 
97
        Time = 8,        /*!< */
 
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 */
 
103
 
 
104
        LastType = 13,   /*!< This line should be at the end of the list of types! */
 
105
 
 
106
        Null = 64,       /*!< Used for fields that are "NULL" expressions. */
 
107
 
 
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 */
 
114
    };
 
115
 
 
116
//TODO: make this configurable
 
117
    static uint defaultTextLength() {
 
118
        return 200;
 
119
    }
 
120
 
 
121
    /*! Type groups for fields. */
 
122
    enum TypeGroup {
 
123
        InvalidGroup = 0,
 
124
        TextGroup = 1,
 
125
        IntegerGroup = 2,
 
126
        FloatGroup = 3,
 
127
        BooleanGroup = 4,
 
128
        DateTimeGroup = 5,
 
129
        BLOBGroup = 6, /* large binary object */
 
130
 
 
131
        LastTypeGroup = 6 // This line should be at the end of the enum!
 
132
    };
 
133
 
 
134
    /*! Possible constraints defined for a field. */
 
135
    enum Constraints {
 
136
        NoConstraints = 0,
 
137
        AutoInc = 1,
 
138
        Unique = 2,
 
139
        PrimaryKey = 4,
 
140
        ForeignKey = 8,
 
141
        NotNull = 16,
 
142
        NotEmpty = 32, //!< only legal for string-like and blob fields
 
143
        Indexed = 64
 
144
    };
 
145
 
 
146
    /*! Possible options defined for a field. */
 
147
    enum Options {
 
148
        NoOptions = 0,
 
149
        Unsigned = 1
 
150
    };
 
151
 
 
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);
 
155
 
 
156
    /*! Creates a database field without any properties set.
 
157
     These should be set later. */
 
158
    Field();
 
159
 
 
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(),
 
168
          uint width = 0);
 
169
 
 
170
    /*! Copy constructor. */
 
171
    Field(const Field& f);
 
172
 
 
173
    virtual ~Field();
 
174
 
 
175
    //! Converts type \a type to QVariant equivalent as accurate as possible
 
176
    static QVariant::Type variantType(uint type);
 
177
 
 
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);
 
181
 
 
182
    /*! \return list of all available i18n'd type names. */
 
183
    static QStringList typeNames();
 
184
 
 
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);
 
189
 
 
190
    /*! \return type for a given \a typeString */
 
191
    static Type typeForString(const QString& typeString);
 
192
 
 
193
    /*! \return type group for a given \a typeGroupString */
 
194
    static TypeGroup typeGroupForString(const QString& typeGroupString);
 
195
 
 
196
    /*! \return group for \a type */
 
197
    static TypeGroup typeGroup(uint type);
 
198
 
 
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);
 
202
 
 
203
    /*! \return list of all available i18n'd type group names. */
 
204
    static QStringList typeGroupNames();
 
205
 
 
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);
 
210
 
 
211
    /* ! \return the name of this field */
 
212
    inline QString name() const {
 
213
        return m_name;
 
214
    }
 
215
 
 
216
    /*! \return table schema of table that owns this field
 
217
     or null if it has no table assigned.
 
218
     @see query() */
 
219
    virtual TableSchema* table() const;
 
220
 
 
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.
 
225
     @see setQuery() */
 
226
    virtual void setTable(TableSchema *table);
 
227
 
 
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.
 
231
     @see table() */
 
232
    QuerySchema* query() const;
 
233
 
 
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.
 
239
     @see setQuery() */
 
240
    void setQuery(QuerySchema *query);
 
241
 
 
242
    /*! \return true if the field is autoincrement (e.g. integer/numeric) */
 
243
    inline bool isAutoIncrement() const {
 
244
        return constraints() & AutoInc;
 
245
    }
 
246
 
 
247
    /*! \return true if the field is member of single-field primary key */
 
248
    inline bool isPrimaryKey() const {
 
249
        return constraints() & PrimaryKey;
 
250
    }
 
251
 
 
252
    /*! \return true if the field is member of single-field unique key */
 
253
    inline bool isUniqueKey() const {
 
254
        return constraints() & Unique;
 
255
    }
 
256
 
 
257
    /*! \return true if the field is member of single-field foreign key */
 
258
    inline bool isForeignKey() const {
 
259
        return constraints() & ForeignKey;
 
260
    }
 
261
 
 
262
    /*! \return true if the field is not allowed to be null */
 
263
    inline bool isNotNull() const {
 
264
        return constraints() & NotNull;
 
265
    }
 
266
 
 
267
    /*! \return true if the field is not allowed to be null */
 
268
    inline bool isNotEmpty() const {
 
269
        return constraints() & NotEmpty;
 
270
    }
 
271
 
 
272
    /*! \return true if the field is indexed using single-field database index. */
 
273
    inline bool isIndexed() const {
 
274
        return constraints() & Indexed;
 
275
    }
 
276
 
 
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());
 
280
    }
 
281
 
 
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);
 
285
 
 
286
    /*! \return true if the field is of any integer type */
 
287
    inline bool isIntegerType() const {
 
288
        return Field::isIntegerType(type());
 
289
    }
 
290
 
 
291
    /*! static version of isIntegerType() method
 
292
     *! \return true if the field is of any integer type */
 
293
    static bool isIntegerType(uint type);
 
294
 
 
295
    /*! \return true if the field is of any floating point numeric type */
 
296
    inline bool isFPNumericType() const {
 
297
        return Field::isFPNumericType(type());
 
298
    }
 
299
 
 
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);
 
303
 
 
304
    /*! \return true if the field is of any date or time related type */
 
305
    inline bool isDateTimeType() const {
 
306
        return Field::isDateTimeType(type());
 
307
    }
 
308
 
 
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);
 
312
 
 
313
    /*! @return true if the field is of any text type */
 
314
    inline bool isTextType() const {
 
315
        return Field::isTextType(type());
 
316
    }
 
317
 
 
318
    /*! static version of isTextType() method
 
319
     *! \return true if the field is of any text type */
 
320
    static bool isTextType(uint type);
 
321
 
 
322
    uint options() const {
 
323
        return m_options;
 
324
    }
 
325
 
 
326
    void setOptions(uint options) {
 
327
        m_options = options;
 
328
    }
 
329
 
 
330
    //! Converts field's type to QVariant equivalent as accurate as possible
 
331
    inline QVariant::Type variantType() const {
 
332
        return variantType(type());
 
333
    }
 
334
 
 
335
    /*! \return a type for this field. If there's expression assigned,
 
336
     type of the expression is returned instead. */
 
337
    Type type() const;
 
338
 
 
339
    //! \return a i18n'd type name for this field
 
340
    inline QString typeName() const {
 
341
        return Field::typeName(type());
 
342
    }
 
343
 
 
344
    //! \return type group for this field
 
345
    inline TypeGroup typeGroup() const {
 
346
        return Field::typeGroup(type());
 
347
    }
 
348
 
 
349
    //! \return a i18n'd type group name for this field
 
350
    inline QString typeGroupName() const {
 
351
        return Field::typeGroupName(type());
 
352
    }
 
353
 
 
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());
 
358
    }
 
359
 
 
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());
 
364
    }
 
365
 
 
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 {
 
371
        return m_subType;
 
372
    }
 
373
 
 
374
    /*! Sets (optional) subtype for this field.
 
375
     \sa subType() */
 
376
    inline void setSubType(const QString& subType) {
 
377
        m_subType = subType;
 
378
    }
 
379
 
 
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;
 
384
    }
 
385
 
 
386
    /*! \return length of text, only meaningful if the field type is text.
 
387
     0 means "default length". */
 
388
    inline uint length() const {
 
389
        return m_length;
 
390
    }
 
391
 
 
392
    /*! \return precision for numeric and other fields that have both length (scale)
 
393
     and precision (floating point types). */
 
394
    inline uint precision() const {
 
395
        return m_precision;
 
396
    }
 
397
 
 
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 {
 
406
        return m_length;
 
407
    }
 
408
 
 
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).
 
413
 
 
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.
 
417
 
 
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.
 
420
 
 
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.
 
424
    */
 
425
    inline int visibleDecimalPlaces() const {
 
426
        return m_visibleDecimalPlaces;
 
427
    }
 
428
 
 
429
    /*! \return the constraints defined for this field. */
 
430
    inline uint constraints() const {
 
431
        return m_constraints;
 
432
    }
 
433
 
 
434
    /*! \return order of this field in containing table (counting starts from 0)
 
435
    (-1 if unspecified). */
 
436
    inline int order() const {
 
437
        return m_order;
 
438
    }
 
439
 
 
440
    /*! \return caption of this field. */
 
441
    inline QString caption() const {
 
442
        return m_caption;
 
443
    }
 
444
 
 
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;
 
448
    }
 
449
 
 
450
    /*! \return description text for this field. */
 
451
    inline QString description() const {
 
452
        return m_desc;
 
453
    }
 
454
 
 
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 {
 
458
        return m_width;
 
459
    }
 
460
 
 
461
    //! if the type has the unsigned attribute
 
462
    inline bool isUnsigned() const {
 
463
        return m_options & Unsigned;
 
464
    }
 
465
 
 
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());
 
470
    }
 
471
 
 
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);
 
475
 
 
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());
 
480
    }
 
481
 
 
482
    /*! static version of isAutoIncrementAllowed() method
 
483
     \return true if this field type can be auto-incremented. */
 
484
    static bool isAutoIncrementAllowed(uint type);
 
485
 
 
486
    /*! Sets type \a t for this field. This does nothing if there's already expression assigned,
 
487
     see expression(). */
 
488
    void setType(Type t);
 
489
 
 
490
    /*! Sets name \a name for this field. */
 
491
    void setName(const QString& name);
 
492
 
 
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);
 
498
 
 
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);
 
502
 
 
503
    /*! Sets scale for this field. Only works for floating-point types.
 
504
     @see scale() */
 
505
    void setScale(uint s);
 
506
 
 
507
    /*! Sets number of decimal places that should be visible to the user.
 
508
     @see visibleDecimalPlaces() */
 
509
    void setVisibleDecimalPlaces(int p);
 
510
 
 
511
    /*! Sets scale for this field. Only works for floating-point types. */
 
512
    void setPrecision(uint p);
 
513
 
 
514
    /*! Sets unsigned flag for this field. Only works for integer types. */
 
515
    void setUnsigned(bool u);
 
516
 
 
517
    /*! Sets default value for this field. Setting null value removes the default value.
 
518
     @see defaultValue() */
 
519
    void setDefaultValue(const QVariant& def);
 
520
 
 
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);
 
526
 
 
527
    /*! Sets auto increment flag. Only available to set true,
 
528
     if isAutoIncrementAllowed() is true. */
 
529
    void setAutoIncrement(bool a);
 
530
 
 
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:
 
534
     - setUniqueKey(true)
 
535
     - setNotNull(true)
 
536
     - setNotEmpty(true)
 
537
     - setIndexed(true)
 
538
 
 
539
     Setting this to false implies setting setAutoIncrement(false). */
 
540
    void setPrimaryKey(bool p);
 
541
 
 
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);
 
546
 
 
547
    /*! Sets whether the field has to be declared with single-field foreign key.
 
548
     Used in IndexSchema::setForeigKey(). */
 
549
    void setForeignKey(bool f);
 
550
 
 
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);
 
556
 
 
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);
 
562
 
 
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)
 
569
     - setNotNull(false)
 
570
     - setNotEmpty(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);
 
575
 
 
576
    /*! Sets caption for this field to \a caption. */
 
577
    void setCaption(const QString& caption) {
 
578
        m_caption = caption;
 
579
    }
 
580
 
 
581
    /*! Sets description for this field to \a description. */
 
582
    void setDescription(const QString& description) {
 
583
        m_desc = description;
 
584
    }
 
585
 
 
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) {
 
589
        m_width = w;
 
590
    }
 
591
 
 
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.
 
599
    */
 
600
    inline bool isQueryAsterisk() const {
 
601
        return m_type == Field::Asterisk;
 
602
    }
 
603
 
 
604
    /*! \return string for debugging purposes. */
 
605
    virtual QString debugString() const;
 
606
 
 
607
    /*! Shows debug information about this field. */
 
608
    void debug();
 
609
 
 
610
    /*! \return KexiDB::BaseExpr object if the field value is an
 
611
     expression.  Unless the expression is set with setExpression(), it is null.
 
612
    */
 
613
    inline KexiDB::BaseExpr *expression() {
 
614
        return m_expr;
 
615
    }
 
616
 
 
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.
 
622
 
 
623
     Because the field defines an expression, it should be assigned to a query,
 
624
     not to a table.
 
625
    */
 
626
    void setExpression(KexiDB::BaseExpr *expr);
 
627
 
 
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;
 
633
    }
 
634
 
 
635
//<TMP>
 
636
    /*! \return the hints for enum fields. */
 
637
    QVector<QString> enumHints() const {
 
638
        return m_hints;
 
639
    }
 
640
    QString enumHint(uint num) {
 
641
        return (num < (uint)m_hints.size()) ? m_hints.at(num) : QString();
 
642
    }
 
643
    /*! sets the hint for enum fields */
 
644
    void setEnumHints(const QVector<QString> &l) {
 
645
        m_hints = l;
 
646
    }
 
647
//</TMP>
 
648
 
 
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;
 
653
 
 
654
    //! Sets value \a value for custom property \a propertyName
 
655
    void setCustomProperty(const QByteArray& propertyName, const QVariant& value);
 
656
 
 
657
    //! A data type used for handling custom properties of a field
 
658
    typedef QHash<QByteArray, QVariant> CustomPropertiesMap;
 
659
 
 
660
    //! \return all custom properties
 
661
    inline const CustomPropertiesMap customProperties() const {
 
662
        return m_customProperties ? *m_customProperties : CustomPropertiesMap();
 
663
    }
 
664
 
 
665
protected:
 
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);
 
672
 
 
673
    /*! @internal Used by constructors. */
 
674
    void init();
 
675
 
 
676
    //! \return a deep copy of this object. Used in @ref FieldList(const FieldList& fl).
 
677
    virtual Field* copy() const;
 
678
 
 
679
    FieldList *m_parent; //!< In most cases this points to a TableSchema
 
680
    //!< object that field is assigned.
 
681
    QString m_name;
 
682
    QString m_subType;
 
683
    uint m_constraints;
 
684
    uint m_length; //!< also used for storing scale for floating point types
 
685
    uint m_precision;
 
686
    int m_visibleDecimalPlaces; //!< used in visibleDecimalPlaces()
 
687
    uint m_options;
 
688
    QVariant m_defaultValue;
 
689
    int m_order;
 
690
    QString m_caption;
 
691
    QString m_desc;
 
692
    uint m_width;
 
693
    QVector<QString> m_hints;
 
694
 
 
695
    KexiDB::BaseExpr *m_expr;
 
696
    CustomPropertiesMap* m_customProperties;
 
697
 
 
698
    //! @internal Used in m_typeNames member to handle i18n'd type names
 
699
    class KEXI_DB_EXPORT FieldTypeNames : public QVector<QString>
 
700
    {
 
701
    public:
 
702
        FieldTypeNames();
 
703
        void init();
 
704
        QHash<QString, Type> str2num;
 
705
        QStringList names;
 
706
    protected:
 
707
    bool m_initialized : 1;
 
708
    };
 
709
 
 
710
    //! @internal Used in m_typeGroupNames member to handle i18n'd type group names
 
711
    class KEXI_DB_EXPORT FieldTypeGroupNames : public QVector<QString>
 
712
    {
 
713
    public:
 
714
        FieldTypeGroupNames();
 
715
        void init();
 
716
        QHash<QString, TypeGroup> str2num;
 
717
        QStringList names;
 
718
    protected:
 
719
    bool m_initialized : 1;
 
720
    };
 
721
 
 
722
    //! real i18n'd type names (and not-i18n'd type name strings)
 
723
    static FieldTypeNames m_typeNames;
 
724
 
 
725
    //! real i18n'd type group names (and not-i18n'd group name strings)
 
726
    static FieldTypeGroupNames m_typeGroupNames;
 
727
 
 
728
private:
 
729
    Type m_type;
 
730
 
 
731
    friend class Connection;
 
732
    friend class FieldList;
 
733
    friend class TableSchema;
 
734
    friend class QuerySchema;
 
735
};
 
736
 
 
737
} //namespace KexiDB
 
738
 
 
739
#endif