~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/sql/kernel/qsqlfield.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the sql module of the Qt Toolkit.
 
6
**
 
7
** This file may be distributed under the terms of the Q Public License
 
8
** as defined by Trolltech AS of Norway and appearing in the file
 
9
** LICENSE.QPL included in the packaging of this file.
 
10
**
 
11
** This file may be distributed and/or modified under the terms of the
 
12
** GNU General Public License version 2 as published by the Free Software
 
13
** Foundation and appearing in the file LICENSE.GPL included in the
 
14
** packaging of this file.
 
15
**
 
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
 
17
**   information about Qt Commercial License Agreements.
 
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
 
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "qsqlfield.h"
 
30
#include "qatomic.h"
 
31
#include "qdebug.h"
 
32
 
 
33
class QSqlFieldPrivate
 
34
{
 
35
public:
 
36
    QSqlFieldPrivate(const QString &name,
 
37
              QVariant::Type type) :
 
38
        ref(1), nm(name), ro(false), type(type), req(QSqlField::Unknown),
 
39
        len(-1), prec(-1), tp(-1), gen(true), autoval(false)
 
40
    {
 
41
    }
 
42
 
 
43
    QSqlFieldPrivate(const QSqlFieldPrivate &other)
 
44
        : ref(1),
 
45
          nm(other.nm),
 
46
          ro(other.ro),
 
47
          type(other.type),
 
48
          req(other.req),
 
49
          len(other.len),
 
50
          prec(other.prec),
 
51
          def(other.def),
 
52
          tp(other.tp),
 
53
          gen(other.gen),
 
54
          autoval(other.autoval)
 
55
    {}
 
56
 
 
57
    bool operator==(const QSqlFieldPrivate& other) const
 
58
    {
 
59
        return (nm == other.nm
 
60
                && ro == other.ro
 
61
                && type == other.type
 
62
                && req == other.req
 
63
                && len == other.len
 
64
                && prec == other.prec
 
65
                && def == other.def
 
66
                && tp == other.tp
 
67
                && gen == other.gen
 
68
                && autoval == other.autoval);
 
69
    }
 
70
 
 
71
    QAtomic ref;
 
72
    QString nm;
 
73
    uint ro: 1;
 
74
    QVariant::Type type;
 
75
    QSqlField::RequiredStatus req;
 
76
    int len;
 
77
    int prec;
 
78
    QVariant def;
 
79
    int tp;
 
80
    uint gen: 1;
 
81
    uint autoval: 1;
 
82
};
 
83
 
 
84
 
 
85
/*!
 
86
    \class QSqlField
 
87
    \brief The QSqlField class manipulates the fields in SQL database tables
 
88
    and views.
 
89
 
 
90
    \ingroup database
 
91
    \module sql
 
92
 
 
93
    QSqlField represents the characteristics of a single column in a
 
94
    database table or view, such as the data type and column name. A
 
95
    field also contains the value of the database column, which can be
 
96
    viewed or changed.
 
97
 
 
98
    Field data values are stored as QVariants. Using an incompatible
 
99
    type is not permitted. For example:
 
100
 
 
101
    \quotefromfile snippets/sqldatabase/sqldatabase.cpp
 
102
    \skipto QSqlField_snippets
 
103
    \skipto QSqlField field
 
104
    \printuntil setValue
 
105
 
 
106
    However, the field will attempt to cast certain data types to the
 
107
    field data type where possible:
 
108
 
 
109
    \skipto QSqlField field
 
110
    \printuntil setValue
 
111
 
 
112
    QSqlField objects are rarely created explicitly in application
 
113
    code. They are usually accessed indirectly through \l{QSqlRecord}s
 
114
    that already contain a list of fields. For example:
 
115
 
 
116
    \skipto QSqlQuery query
 
117
    \printline QSqlQuery query
 
118
    \dots
 
119
    \printline QSqlRecord record
 
120
    \printline QSqlField field
 
121
 
 
122
    A QSqlField object can provide some meta-data about the field, for
 
123
    example, its name(), variant type(), length(), precision(),
 
124
    defaultValue(), typeID(), and its requiredStatus(),
 
125
    isGenerated() and isReadOnly(). The field's data can be
 
126
    checked to see if it isNull(), and its value() retrieved. When
 
127
    editing the data can be set with setValue() or set to NULL with
 
128
    clear().
 
129
 
 
130
    \sa QSqlRecord
 
131
*/
 
132
 
 
133
/*!
 
134
    \enum QSqlField::RequiredStatus
 
135
 
 
136
    Specifies whether the field is required or optional.
 
137
 
 
138
    \value Required  The field must be specified when inserting records.
 
139
    \value Optional  The fields doesn't have to be specified when inserting records.
 
140
    \value Unknown  The database driver couldn't determine whether the field is required or
 
141
                    optional.
 
142
 
 
143
    \sa requiredStatus()
 
144
*/
 
145
 
 
146
/*!
 
147
    Constructs an empty field called \a fieldName of variant type \a
 
148
    type.
 
149
 
 
150
    \sa setRequiredStatus() setLength() setPrecision() setDefaultValue() setGenerated() setReadOnly()
 
151
*/
 
152
QSqlField::QSqlField(const QString& fieldName, QVariant::Type type)
 
153
{
 
154
    d = new QSqlFieldPrivate(fieldName, type);
 
155
}
 
156
 
 
157
/*!
 
158
    Constructs a copy of \a other.
 
159
*/
 
160
 
 
161
QSqlField::QSqlField(const QSqlField& other)
 
162
{
 
163
    d = other.d;
 
164
    d->ref.ref();
 
165
    val = other.val;
 
166
}
 
167
 
 
168
/*!
 
169
    Sets the field equal to \a other.
 
170
*/
 
171
 
 
172
QSqlField& QSqlField::operator=(const QSqlField& other)
 
173
{
 
174
    qAtomicAssign(d, other.d);
 
175
    val = other.val;
 
176
    return *this;
 
177
}
 
178
 
 
179
 
 
180
/*! \fn bool QSqlField::operator!=(const QSqlField &other) const
 
181
    Returns true if the field is unequal to \a other; otherwise returns
 
182
    false.
 
183
*/
 
184
 
 
185
/*!
 
186
    Returns true if the field is equal to \a other; otherwise returns
 
187
    false.
 
188
*/
 
189
bool QSqlField::operator==(const QSqlField& other) const
 
190
{
 
191
    return ((d == other.d || *d == *other.d)
 
192
            && val == other.val);
 
193
}
 
194
 
 
195
/*!
 
196
    Destroys the object and frees any allocated resources.
 
197
*/
 
198
 
 
199
QSqlField::~QSqlField()
 
200
{
 
201
    if (!d->ref.deref())
 
202
        delete d;
 
203
}
 
204
 
 
205
/*!
 
206
    Sets the required status of this field to \a required.
 
207
 
 
208
    \sa requiredStatus() setType() setLength() setPrecision() setDefaultValue() setGenerated() setReadOnly()
 
209
*/
 
210
void QSqlField::setRequiredStatus(RequiredStatus required)
 
211
{
 
212
    detach();
 
213
    d->req = required;
 
214
}
 
215
 
 
216
/*! \fn void QSqlField::setRequired(bool required)
 
217
 
 
218
    Sets the required status of this field to \l Required if \a
 
219
    required is true; otherwise sets it to \l Optional.
 
220
 
 
221
    \sa setRequiredStatus(), requiredStatus()
 
222
*/
 
223
 
 
224
/*!
 
225
    Sets the field's length to \a fieldLength. For strings this is the
 
226
    maximum number of characters the string can hold; the meaning
 
227
    varies for other types.
 
228
 
 
229
    \sa length() setType() setRequiredStatus() setPrecision() setDefaultValue() setGenerated() setReadOnly()
 
230
*/
 
231
void QSqlField::setLength(int fieldLength)
 
232
{
 
233
    detach();
 
234
    d->len = fieldLength;
 
235
}
 
236
 
 
237
/*!
 
238
    Sets the field's \a precision. This only affects numeric fields.
 
239
 
 
240
    \sa precision() setType() setRequiredStatus() setLength() setDefaultValue() setGenerated() setReadOnly()
 
241
*/
 
242
void QSqlField::setPrecision(int precision)
 
243
{
 
244
    detach();
 
245
    d->prec = precision;
 
246
}
 
247
 
 
248
/*!
 
249
    Sets the default value used for this field to \a value.
 
250
 
 
251
    \sa defaultValue() value() setType() setRequiredStatus() setLength() setPrecision() setGenerated() setReadOnly()
 
252
*/
 
253
void QSqlField::setDefaultValue(const QVariant &value)
 
254
{
 
255
    detach();
 
256
    d->def = value;
 
257
}
 
258
 
 
259
/*!
 
260
    \internal
 
261
*/
 
262
void QSqlField::setSqlType(int type)
 
263
{
 
264
    detach();
 
265
    d->tp = type;
 
266
}
 
267
 
 
268
/*!
 
269
    Sets the generated state. If \a gen is false, no SQL will
 
270
    be generated for this field; otherwise, Qt classes such as
 
271
    QSqlQueryModel and QSqlTableModel will generate SQL for this
 
272
    field.
 
273
 
 
274
    \sa isGenerated() setType() setRequiredStatus() setLength() setPrecision() setDefaultValue() setReadOnly()
 
275
*/
 
276
void QSqlField::setGenerated(bool gen)
 
277
{
 
278
    detach();
 
279
    d->gen = gen;
 
280
}
 
281
 
 
282
 
 
283
/*!
 
284
    Sets the value of the field to \a value. If the field is read-only
 
285
    (isReadOnly() returns true), nothing happens.
 
286
 
 
287
    If the data type of \a value differs from the field's current
 
288
    data type, an attempt is made to cast it to the proper type. This
 
289
    preserves the data type of the field in the case of assignment,
 
290
    e.g. a QString to an integer data type.
 
291
 
 
292
    To set the value to NULL, use clear().
 
293
 
 
294
    \sa value() isReadOnly() defaultValue()
 
295
*/
 
296
 
 
297
void QSqlField::setValue(const QVariant& value)
 
298
{
 
299
    if (isReadOnly())
 
300
        return;
 
301
    val = value;
 
302
}
 
303
 
 
304
/*!
 
305
    Clears the value of the field and sets it to NULL.
 
306
    If the field is read-only, nothing happens.
 
307
 
 
308
    \sa setValue() isReadOnly() requiredStatus()
 
309
*/
 
310
 
 
311
void QSqlField::clear()
 
312
{
 
313
    if (isReadOnly())
 
314
        return;
 
315
    val = QVariant(type());
 
316
}
 
317
 
 
318
/*!
 
319
    Sets the name of the field to \a name.
 
320
 
 
321
    \sa name()
 
322
*/
 
323
 
 
324
void QSqlField::setName(const QString& name)
 
325
{
 
326
    detach();
 
327
    d->nm = name;
 
328
}
 
329
 
 
330
/*!
 
331
    Sets the read only flag of the field's value to \a readOnly. A
 
332
    read-only field cannot have its value set with setValue() and
 
333
    cannot be cleared to NULL with clear().
 
334
*/
 
335
void QSqlField::setReadOnly(bool readOnly)
 
336
{
 
337
    detach();
 
338
    d->ro = readOnly;
 
339
}
 
340
 
 
341
/*!
 
342
    \fn QVariant QSqlField::value() const
 
343
 
 
344
    Returns the value of the field as a QVariant.
 
345
 
 
346
    Use isNull() to check if the field's value is NULL.
 
347
*/
 
348
 
 
349
/*!
 
350
    Returns the name of the field.
 
351
 
 
352
    \sa setName()
 
353
*/
 
354
QString QSqlField::name() const
 
355
{
 
356
    return d->nm;
 
357
}
 
358
 
 
359
/*!
 
360
    Returns the field's type as stored in the database.
 
361
    Note that the actual value might have a different type,
 
362
    Numerical values that are too large to store in a long
 
363
    int or double are usually stored as strings to prevent
 
364
    precision loss.
 
365
 
 
366
    \sa setType()
 
367
*/
 
368
QVariant::Type QSqlField::type() const
 
369
{
 
370
    return d->type;
 
371
}
 
372
 
 
373
/*!
 
374
    Set's the field's variant type to \a type.
 
375
 
 
376
    \sa type() setRequiredStatus() setLength() setPrecision() setDefaultValue() setGenerated() setReadOnly()
 
377
*/
 
378
void QSqlField::setType(QVariant::Type type)
 
379
{
 
380
    detach();
 
381
    d->type = type;
 
382
}
 
383
 
 
384
 
 
385
/*!
 
386
    Returns true if the field's value is read-only; otherwise returns
 
387
    false.
 
388
 
 
389
    \sa setReadOnly() type() requiredStatus() length() precision() defaultValue() isGenerated()
 
390
*/
 
391
bool QSqlField::isReadOnly() const
 
392
{ return d->ro; }
 
393
 
 
394
/*!
 
395
    Returns true if the field's value is NULL; otherwise returns
 
396
    false.
 
397
 
 
398
    \sa value()
 
399
*/
 
400
bool QSqlField::isNull() const
 
401
{ return val.isNull(); }
 
402
 
 
403
/*! \internal
 
404
*/
 
405
void QSqlField::detach()
 
406
{
 
407
    qAtomicDetach(d);
 
408
}
 
409
 
 
410
/*!
 
411
    Returns true if this is a required field; otherwise returns false.
 
412
    An \c INSERT will fail if a required field does not have a value.
 
413
 
 
414
    \sa setRequiredStatus() type() length() precision() defaultValue() isGenerated()
 
415
*/
 
416
QSqlField::RequiredStatus QSqlField::requiredStatus() const
 
417
{
 
418
    return d->req;
 
419
}
 
420
 
 
421
/*!
 
422
    Returns the field's length.
 
423
 
 
424
    \sa setLength() type() requiredStatus() precision() defaultValue() isGenerated()
 
425
*/
 
426
int QSqlField::length() const
 
427
{
 
428
    return d->len;
 
429
}
 
430
 
 
431
/*!
 
432
    Returns the field's precision; this is only meaningful for numeric
 
433
    types.
 
434
 
 
435
    \sa setPrecision() type() requiredStatus() length() defaultValue() isGenerated()
 
436
*/
 
437
int QSqlField::precision() const
 
438
{
 
439
    return d->prec;
 
440
}
 
441
 
 
442
/*!
 
443
    Returns the field's default value (which may be NULL).
 
444
 
 
445
    \sa setDefaultValue() type() requiredStatus() length() precision() isGenerated()
 
446
*/
 
447
QVariant QSqlField::defaultValue() const
 
448
{
 
449
    return d->def;
 
450
}
 
451
 
 
452
/*!
 
453
    \internal
 
454
 
 
455
    Returns the type ID for the field.
 
456
*/
 
457
int QSqlField::typeID() const
 
458
{
 
459
    return d->tp;
 
460
}
 
461
 
 
462
/*!
 
463
    Returns true if the field is generated; otherwise returns
 
464
    false.
 
465
 
 
466
    \sa setGenerated() type() requiredStatus() length() precision() defaultValue()
 
467
*/
 
468
bool QSqlField::isGenerated() const
 
469
{
 
470
    return d->gen;
 
471
}
 
472
 
 
473
/*!
 
474
    Returns true if the field's variant type is valid; otherwise
 
475
    returns false.
 
476
*/
 
477
bool QSqlField::isValid() const
 
478
{
 
479
    return d->type != QVariant::Invalid;
 
480
}
 
481
 
 
482
#ifndef QT_NO_DEBUG_STREAM
 
483
QDebug operator<<(QDebug dbg, const QSqlField &f)
 
484
{
 
485
#ifndef Q_BROKEN_DEBUG_STREAM
 
486
    dbg.nospace() << "QSqlField(" << f.name() << ", " << QVariant::typeToName(f.type());
 
487
    if (f.length() >= 0)
 
488
        dbg.nospace() << ", length: " << f.length();
 
489
    if (f.precision() >= 0)
 
490
        dbg.nospace() << ", precision: " << f.precision();
 
491
    if (f.requiredStatus() != QSqlField::Unknown)
 
492
        dbg.nospace() << ", required: "
 
493
                      << (f.requiredStatus() == QSqlField::Required ? "yes" : "no");
 
494
    dbg.nospace() << ", generated: " << (f.isGenerated() ? "yes" : "no");
 
495
    if (f.typeID() >= 0)
 
496
        dbg.nospace() << ", typeID: " << f.typeID();
 
497
    if (!f.defaultValue().isNull())
 
498
        dbg.nospace() << ", auto-value: \"" << f.defaultValue() << "\"";
 
499
    dbg.nospace() << ")";
 
500
    return dbg.space();
 
501
#else
 
502
    qWarning("This compiler doesn't support streaming QSqlField to QDebug");
 
503
    return dbg;
 
504
    Q_UNUSED(f);
 
505
#endif
 
506
}
 
507
#endif
 
508
 
 
509
/*!
 
510
    \fn void QSqlField::setNull()
 
511
 
 
512
    Use clear() instead.
 
513
*/
 
514
 
 
515
/*!
 
516
    Returns true if the value is auto-generated by the database,
 
517
    for example auto-increment primary key values.
 
518
 
 
519
    \sa setAutoValue()
 
520
*/
 
521
bool QSqlField::isAutoValue() const
 
522
{
 
523
    return d->autoval;
 
524
}
 
525
 
 
526
/*!
 
527
    Marks the field as an auto-generated value if \a autoVal
 
528
    is true.
 
529
 
 
530
    \sa isAutoValue()
 
531
 */
 
532
void QSqlField::setAutoValue(bool autoVal)
 
533
{
 
534
    detach();
 
535
    d->autoval = autoVal;
 
536
}
 
537