1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the sql module of the Qt Toolkit.
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.
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.
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.
21
** Contact info@trolltech.com if any conditions of this licensing are
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.
27
****************************************************************************/
29
#include "qsqlfield.h"
33
class QSqlFieldPrivate
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)
43
QSqlFieldPrivate(const QSqlFieldPrivate &other)
54
autoval(other.autoval)
57
bool operator==(const QSqlFieldPrivate& other) const
59
return (nm == other.nm
68
&& autoval == other.autoval);
75
QSqlField::RequiredStatus req;
87
\brief The QSqlField class manipulates the fields in SQL database tables
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
98
Field data values are stored as QVariants. Using an incompatible
99
type is not permitted. For example:
101
\quotefromfile snippets/sqldatabase/sqldatabase.cpp
102
\skipto QSqlField_snippets
103
\skipto QSqlField field
106
However, the field will attempt to cast certain data types to the
107
field data type where possible:
109
\skipto QSqlField field
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:
116
\skipto QSqlQuery query
117
\printline QSqlQuery query
119
\printline QSqlRecord record
120
\printline QSqlField field
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
134
\enum QSqlField::RequiredStatus
136
Specifies whether the field is required or optional.
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
147
Constructs an empty field called \a fieldName of variant type \a
150
\sa setRequiredStatus() setLength() setPrecision() setDefaultValue() setGenerated() setReadOnly()
152
QSqlField::QSqlField(const QString& fieldName, QVariant::Type type)
154
d = new QSqlFieldPrivate(fieldName, type);
158
Constructs a copy of \a other.
161
QSqlField::QSqlField(const QSqlField& other)
169
Sets the field equal to \a other.
172
QSqlField& QSqlField::operator=(const QSqlField& other)
174
qAtomicAssign(d, other.d);
180
/*! \fn bool QSqlField::operator!=(const QSqlField &other) const
181
Returns true if the field is unequal to \a other; otherwise returns
186
Returns true if the field is equal to \a other; otherwise returns
189
bool QSqlField::operator==(const QSqlField& other) const
191
return ((d == other.d || *d == *other.d)
192
&& val == other.val);
196
Destroys the object and frees any allocated resources.
199
QSqlField::~QSqlField()
206
Sets the required status of this field to \a required.
208
\sa requiredStatus() setType() setLength() setPrecision() setDefaultValue() setGenerated() setReadOnly()
210
void QSqlField::setRequiredStatus(RequiredStatus required)
216
/*! \fn void QSqlField::setRequired(bool required)
218
Sets the required status of this field to \l Required if \a
219
required is true; otherwise sets it to \l Optional.
221
\sa setRequiredStatus(), requiredStatus()
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.
229
\sa length() setType() setRequiredStatus() setPrecision() setDefaultValue() setGenerated() setReadOnly()
231
void QSqlField::setLength(int fieldLength)
234
d->len = fieldLength;
238
Sets the field's \a precision. This only affects numeric fields.
240
\sa precision() setType() setRequiredStatus() setLength() setDefaultValue() setGenerated() setReadOnly()
242
void QSqlField::setPrecision(int precision)
249
Sets the default value used for this field to \a value.
251
\sa defaultValue() value() setType() setRequiredStatus() setLength() setPrecision() setGenerated() setReadOnly()
253
void QSqlField::setDefaultValue(const QVariant &value)
262
void QSqlField::setSqlType(int type)
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
274
\sa isGenerated() setType() setRequiredStatus() setLength() setPrecision() setDefaultValue() setReadOnly()
276
void QSqlField::setGenerated(bool gen)
284
Sets the value of the field to \a value. If the field is read-only
285
(isReadOnly() returns true), nothing happens.
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.
292
To set the value to NULL, use clear().
294
\sa value() isReadOnly() defaultValue()
297
void QSqlField::setValue(const QVariant& value)
305
Clears the value of the field and sets it to NULL.
306
If the field is read-only, nothing happens.
308
\sa setValue() isReadOnly() requiredStatus()
311
void QSqlField::clear()
315
val = QVariant(type());
319
Sets the name of the field to \a name.
324
void QSqlField::setName(const QString& name)
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().
335
void QSqlField::setReadOnly(bool readOnly)
342
\fn QVariant QSqlField::value() const
344
Returns the value of the field as a QVariant.
346
Use isNull() to check if the field's value is NULL.
350
Returns the name of the field.
354
QString QSqlField::name() const
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
368
QVariant::Type QSqlField::type() const
374
Set's the field's variant type to \a type.
376
\sa type() setRequiredStatus() setLength() setPrecision() setDefaultValue() setGenerated() setReadOnly()
378
void QSqlField::setType(QVariant::Type type)
386
Returns true if the field's value is read-only; otherwise returns
389
\sa setReadOnly() type() requiredStatus() length() precision() defaultValue() isGenerated()
391
bool QSqlField::isReadOnly() const
395
Returns true if the field's value is NULL; otherwise returns
400
bool QSqlField::isNull() const
401
{ return val.isNull(); }
405
void QSqlField::detach()
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.
414
\sa setRequiredStatus() type() length() precision() defaultValue() isGenerated()
416
QSqlField::RequiredStatus QSqlField::requiredStatus() const
422
Returns the field's length.
424
\sa setLength() type() requiredStatus() precision() defaultValue() isGenerated()
426
int QSqlField::length() const
432
Returns the field's precision; this is only meaningful for numeric
435
\sa setPrecision() type() requiredStatus() length() defaultValue() isGenerated()
437
int QSqlField::precision() const
443
Returns the field's default value (which may be NULL).
445
\sa setDefaultValue() type() requiredStatus() length() precision() isGenerated()
447
QVariant QSqlField::defaultValue() const
455
Returns the type ID for the field.
457
int QSqlField::typeID() const
463
Returns true if the field is generated; otherwise returns
466
\sa setGenerated() type() requiredStatus() length() precision() defaultValue()
468
bool QSqlField::isGenerated() const
474
Returns true if the field's variant type is valid; otherwise
477
bool QSqlField::isValid() const
479
return d->type != QVariant::Invalid;
482
#ifndef QT_NO_DEBUG_STREAM
483
QDebug operator<<(QDebug dbg, const QSqlField &f)
485
#ifndef Q_BROKEN_DEBUG_STREAM
486
dbg.nospace() << "QSqlField(" << f.name() << ", " << QVariant::typeToName(f.type());
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");
496
dbg.nospace() << ", typeID: " << f.typeID();
497
if (!f.defaultValue().isNull())
498
dbg.nospace() << ", auto-value: \"" << f.defaultValue() << "\"";
499
dbg.nospace() << ")";
502
qWarning("This compiler doesn't support streaming QSqlField to QDebug");
510
\fn void QSqlField::setNull()
516
Returns true if the value is auto-generated by the database,
517
for example auto-increment primary key values.
521
bool QSqlField::isAutoValue() const
527
Marks the field as an auto-generated value if \a autoVal
532
void QSqlField::setAutoValue(bool autoVal)
535
d->autoval = autoVal;