source: trunk/src/sql/kernel/qsqlfield.cpp@ 754

Last change on this file since 754 was 651, checked in by Dmitry A. Kuminov, 15 years ago

trunk: Merged in qt 4.6.2 sources.

File size: 13.8 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtSql module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qsqlfield.h"
43#include "qatomic.h"
44#include "qdebug.h"
45
46QT_BEGIN_NAMESPACE
47
48class QSqlFieldPrivate
49{
50public:
51 QSqlFieldPrivate(const QString &name,
52 QVariant::Type type) :
53 ref(1), nm(name), ro(false), type(type), req(QSqlField::Unknown),
54 len(-1), prec(-1), tp(-1), gen(true), autoval(false)
55 {
56 }
57
58 QSqlFieldPrivate(const QSqlFieldPrivate &other)
59 : ref(1),
60 nm(other.nm),
61 ro(other.ro),
62 type(other.type),
63 req(other.req),
64 len(other.len),
65 prec(other.prec),
66 def(other.def),
67 tp(other.tp),
68 gen(other.gen),
69 autoval(other.autoval)
70 {}
71
72 bool operator==(const QSqlFieldPrivate& other) const
73 {
74 return (nm == other.nm
75 && ro == other.ro
76 && type == other.type
77 && req == other.req
78 && len == other.len
79 && prec == other.prec
80 && def == other.def
81 && gen == other.gen
82 && autoval == other.autoval);
83 }
84
85 QAtomicInt ref;
86 QString nm;
87 uint ro: 1;
88 QVariant::Type type;
89 QSqlField::RequiredStatus req;
90 int len;
91 int prec;
92 QVariant def;
93 int tp;
94 uint gen: 1;
95 uint autoval: 1;
96};
97
98
99/*!
100 \class QSqlField
101 \brief The QSqlField class manipulates the fields in SQL database tables
102 and views.
103
104 \ingroup database
105 \ingroup shared
106 \inmodule QtSql
107
108 QSqlField represents the characteristics of a single column in a
109 database table or view, such as the data type and column name. A
110 field also contains the value of the database column, which can be
111 viewed or changed.
112
113 Field data values are stored as QVariants. Using an incompatible
114 type is not permitted. For example:
115
116 \snippet doc/src/snippets/sqldatabase/sqldatabase.cpp 2
117
118 However, the field will attempt to cast certain data types to the
119 field data type where possible:
120
121 \snippet doc/src/snippets/sqldatabase/sqldatabase.cpp 3
122
123 QSqlField objects are rarely created explicitly in application
124 code. They are usually accessed indirectly through \l{QSqlRecord}s
125 that already contain a list of fields. For example:
126
127 \snippet doc/src/snippets/sqldatabase/sqldatabase.cpp 4
128 \dots
129 \snippet doc/src/snippets/sqldatabase/sqldatabase.cpp 5
130 \snippet doc/src/snippets/sqldatabase/sqldatabase.cpp 6
131
132 A QSqlField object can provide some meta-data about the field, for
133 example, its name(), variant type(), length(), precision(),
134 defaultValue(), typeID(), and its requiredStatus(),
135 isGenerated() and isReadOnly(). The field's data can be
136 checked to see if it isNull(), and its value() retrieved. When
137 editing the data can be set with setValue() or set to NULL with
138 clear().
139
140 \sa QSqlRecord
141*/
142
143/*!
144 \enum QSqlField::RequiredStatus
145
146 Specifies whether the field is required or optional.
147
148 \value Required The field must be specified when inserting records.
149 \value Optional The fields doesn't have to be specified when inserting records.
150 \value Unknown The database driver couldn't determine whether the field is required or
151 optional.
152
153 \sa requiredStatus()
154*/
155
156/*!
157 Constructs an empty field called \a fieldName of variant type \a
158 type.
159
160 \sa setRequiredStatus() setLength() setPrecision() setDefaultValue() setGenerated() setReadOnly()
161*/
162QSqlField::QSqlField(const QString& fieldName, QVariant::Type type)
163{
164 d = new QSqlFieldPrivate(fieldName, type);
165}
166
167/*!
168 Constructs a copy of \a other.
169*/
170
171QSqlField::QSqlField(const QSqlField& other)
172{
173 d = other.d;
174 d->ref.ref();
175 val = other.val;
176}
177
178/*!
179 Sets the field equal to \a other.
180*/
181
182QSqlField& QSqlField::operator=(const QSqlField& other)
183{
184 qAtomicAssign(d, other.d);
185 val = other.val;
186 return *this;
187}
188
189
190/*! \fn bool QSqlField::operator!=(const QSqlField &other) const
191 Returns true if the field is unequal to \a other; otherwise returns
192 false.
193*/
194
195/*!
196 Returns true if the field is equal to \a other; otherwise returns
197 false.
198*/
199bool QSqlField::operator==(const QSqlField& other) const
200{
201 return ((d == other.d || *d == *other.d)
202 && val == other.val);
203}
204
205/*!
206 Destroys the object and frees any allocated resources.
207*/
208
209QSqlField::~QSqlField()
210{
211 if (!d->ref.deref())
212 delete d;
213}
214
215/*!
216 Sets the required status of this field to \a required.
217
218 \sa requiredStatus() setType() setLength() setPrecision() setDefaultValue() setGenerated() setReadOnly()
219*/
220void QSqlField::setRequiredStatus(RequiredStatus required)
221{
222 detach();
223 d->req = required;
224}
225
226/*! \fn void QSqlField::setRequired(bool required)
227
228 Sets the required status of this field to \l Required if \a
229 required is true; otherwise sets it to \l Optional.
230
231 \sa setRequiredStatus(), requiredStatus()
232*/
233
234/*!
235 Sets the field's length to \a fieldLength. For strings this is the
236 maximum number of characters the string can hold; the meaning
237 varies for other types.
238
239 \sa length() setType() setRequiredStatus() setPrecision() setDefaultValue() setGenerated() setReadOnly()
240*/
241void QSqlField::setLength(int fieldLength)
242{
243 detach();
244 d->len = fieldLength;
245}
246
247/*!
248 Sets the field's \a precision. This only affects numeric fields.
249
250 \sa precision() setType() setRequiredStatus() setLength() setDefaultValue() setGenerated() setReadOnly()
251*/
252void QSqlField::setPrecision(int precision)
253{
254 detach();
255 d->prec = precision;
256}
257
258/*!
259 Sets the default value used for this field to \a value.
260
261 \sa defaultValue() value() setType() setRequiredStatus() setLength() setPrecision() setGenerated() setReadOnly()
262*/
263void QSqlField::setDefaultValue(const QVariant &value)
264{
265 detach();
266 d->def = value;
267}
268
269/*!
270 \internal
271*/
272void QSqlField::setSqlType(int type)
273{
274 detach();
275 d->tp = type;
276}
277
278/*!
279 Sets the generated state. If \a gen is false, no SQL will
280 be generated for this field; otherwise, Qt classes such as
281 QSqlQueryModel and QSqlTableModel will generate SQL for this
282 field.
283
284 \sa isGenerated() setType() setRequiredStatus() setLength() setPrecision() setDefaultValue() setReadOnly()
285*/
286void QSqlField::setGenerated(bool gen)
287{
288 detach();
289 d->gen = gen;
290}
291
292
293/*!
294 Sets the value of the field to \a value. If the field is read-only
295 (isReadOnly() returns true), nothing happens.
296
297 If the data type of \a value differs from the field's current
298 data type, an attempt is made to cast it to the proper type. This
299 preserves the data type of the field in the case of assignment,
300 e.g. a QString to an integer data type.
301
302 To set the value to NULL, use clear().
303
304 \sa value() isReadOnly() defaultValue()
305*/
306
307void QSqlField::setValue(const QVariant& value)
308{
309 if (isReadOnly())
310 return;
311 val = value;
312}
313
314/*!
315 Clears the value of the field and sets it to NULL.
316 If the field is read-only, nothing happens.
317
318 \sa setValue() isReadOnly() requiredStatus()
319*/
320
321void QSqlField::clear()
322{
323 if (isReadOnly())
324 return;
325 val = QVariant(type());
326}
327
328/*!
329 Sets the name of the field to \a name.
330
331 \sa name()
332*/
333
334void QSqlField::setName(const QString& name)
335{
336 detach();
337 d->nm = name;
338}
339