kernel/qsqlfield.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
2** -
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
5** -
6** This file is part of the QtSql module of the Qt Toolkit. -
7** -
8** $QT_BEGIN_LICENSE:LGPL$ -
9** Commercial License Usage -
10** Licensees holding valid commercial Qt licenses may use this file in -
11** accordance with the commercial license agreement provided with the -
12** Software or, alternatively, in accordance with the terms contained in -
13** a written agreement between you and Digia. For licensing terms and -
14** conditions see http://qt.digia.com/licensing. For further information -
15** use the contact form at http://qt.digia.com/contact-us. -
16** -
17** GNU Lesser General Public License Usage -
18** Alternatively, this file may be used under the terms of the GNU Lesser -
19** General Public License version 2.1 as published by the Free Software -
20** Foundation and appearing in the file LICENSE.LGPL included in the -
21** packaging of this file. Please review the following information to -
22** ensure the GNU Lesser General Public License version 2.1 requirements -
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -
24** -
25** In addition, as a special exception, Digia gives you certain additional -
26** rights. These rights are described in the Digia Qt LGPL Exception -
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -
28** -
29** GNU General Public License Usage -
30** Alternatively, this file may be used under the terms of the GNU -
31** General Public License version 3.0 as published by the Free Software -
32** Foundation and appearing in the file LICENSE.GPL included in the -
33** packaging of this file. Please review the following information to -
34** ensure the GNU General Public License version 3.0 requirements will be -
35** met: http://www.gnu.org/copyleft/gpl.html. -
36** -
37** -
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 }
executed: }
Execution Count:6131
6131
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 {}
executed: }
Execution Count:1071
1071
71 -
72 bool operator==(const QSqlFieldPrivate& other) const -
73 { -
74 return (nm == other.nm
executed: return (nm == other.nm && ro == other.ro && type == other.type && req == other.req && len == other.len && prec == other.prec && def == other.def && gen == other.gen && autoval == other.autoval);
Execution Count:569
569
75 && ro == other.ro
executed: return (nm == other.nm && ro == other.ro && type == other.type && req == other.req && len == other.len && prec == other.prec && def == other.def && gen == other.gen && autoval == other.autoval);
Execution Count:569
569
76 && type == other.type
executed: return (nm == other.nm && ro == other.ro && type == other.type && req == other.req && len == other.len && prec == other.prec && def == other.def && gen == other.gen && autoval == other.autoval);
Execution Count:569
569
77 && req == other.req
executed: return (nm == other.nm && ro == other.ro && type == other.type && req == other.req && len == other.len && prec == other.prec && def == other.def && gen == other.gen && autoval == other.autoval);
Execution Count:569
569
78 && len == other.len
executed: return (nm == other.nm && ro == other.ro && type == other.type && req == other.req && len == other.len && prec == other.prec && def == other.def && gen == other.gen && autoval == other.autoval);
Execution Count:569
569
79 && prec == other.prec
executed: return (nm == other.nm && ro == other.ro && type == other.type && req == other.req && len == other.len && prec == other.prec && def == other.def && gen == other.gen && autoval == other.autoval);
Execution Count:569
569
80 && def == other.def
executed: return (nm == other.nm && ro == other.ro && type == other.type && req == other.req && len == other.len && prec == other.prec && def == other.def && gen == other.gen && autoval == other.autoval);
Execution Count:569
569
81 && gen == other.gen
executed: return (nm == other.nm && ro == other.ro && type == other.type && req == other.req && len == other.len && prec == other.prec && def == other.def && gen == other.gen && autoval == other.autoval);
Execution Count:569
569
82 && autoval == other.autoval);
executed: return (nm == other.nm && ro == other.ro && type == other.type && req == other.req && len == other.len && prec == other.prec && def == other.def && gen == other.gen && autoval == other.autoval);
Execution Count:569
569
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 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 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 sqldatabase/sqldatabase.cpp 4 -
128 \dots -
129 \snippet sqldatabase/sqldatabase.cpp 5 -
130 \snippet 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(), -
161 setGenerated(), setReadOnly() -
162*/ -
163QSqlField::QSqlField(const QString& fieldName, QVariant::Type type) -
164{ -
165 d = new QSqlFieldPrivate(fieldName, type);
executed (the execution status of this line is deduced): d = new QSqlFieldPrivate(fieldName, type);
-
166 val = QVariant(type);
executed (the execution status of this line is deduced): val = QVariant(type);
-
167}
executed: }
Execution Count:6131
6131
168 -
169/*! -
170 Constructs a copy of \a other. -
171*/ -
172 -
173QSqlField::QSqlField(const QSqlField& other) -
174{ -
175 d = other.d;
executed (the execution status of this line is deduced): d = other.d;
-
176 d->ref.ref();
executed (the execution status of this line is deduced): d->ref.ref();
-
177 val = other.val;
executed (the execution status of this line is deduced): val = other.val;
-
178}
executed: }
Execution Count:31252
31252
179 -
180/*! -
181 Sets the field equal to \a other. -
182*/ -
183 -
184QSqlField& QSqlField::operator=(const QSqlField& other) -
185{ -
186 qAtomicAssign(d, other.d);
executed (the execution status of this line is deduced): qAtomicAssign(d, other.d);
-
187 val = other.val;
executed (the execution status of this line is deduced): val = other.val;
-
188 return *this;
executed: return *this;
Execution Count:233
233
189} -
190 -
191 -
192/*! \fn bool QSqlField::operator!=(const QSqlField &other) const -
193 Returns true if the field is unequal to \a other; otherwise returns -
194 false. -
195*/ -
196 -
197/*! -
198 Returns true if the field is equal to \a other; otherwise returns -
199 false. -
200*/ -
201bool QSqlField::operator==(const QSqlField& other) const -
202{ -
203 return ((d == other.d || *d == *other.d)
executed: return ((d == other.d || *d == *other.d) && val == other.val);
Execution Count:595
595
204 && val == other.val);
executed: return ((d == other.d || *d == *other.d) && val == other.val);
Execution Count:595
595
205} -
206 -
207/*! -
208 Destroys the object and frees any allocated resources. -
209*/ -
210 -
211QSqlField::~QSqlField() -
212{ -
213 if (!d->ref.deref())
evaluated: !d->ref.deref()
TRUEFALSE
yes
Evaluation Count:7085
yes
Evaluation Count:30298
7085-30298
214 delete d;
executed: delete d;
Execution Count:7085
7085
215}
executed: }
Execution Count:37383
37383
216 -
217/*! -
218 Sets the required status of this field to \a required. -
219 -
220 \sa requiredStatus(), setType(), setLength(), setPrecision(), -
221 setDefaultValue(), setGenerated(), setReadOnly() -
222*/ -
223void QSqlField::setRequiredStatus(RequiredStatus required) -
224{ -
225 detach();
executed (the execution status of this line is deduced): detach();
-
226 d->req = required;
executed (the execution status of this line is deduced): d->req = required;
-
227}
executed: }
Execution Count:967
967
228 -
229/*! \fn void QSqlField::setRequired(bool required) -
230 -
231 Sets the required status of this field to \l Required if \a -
232 required is true; otherwise sets it to \l Optional. -
233 -
234 \sa setRequiredStatus(), requiredStatus() -
235*/ -
236 -
237/*! -
238 Sets the field's length to \a fieldLength. For strings this is the -
239 maximum number of characters the string can hold; the meaning -
240 varies for other types. -
241 -
242 \sa length(), setType(), setRequiredStatus(), setPrecision(), -
243 setDefaultValue(), setGenerated(), setReadOnly() -
244*/ -
245void QSqlField::setLength(int fieldLength) -
246{ -
247 detach();
executed (the execution status of this line is deduced): detach();
-
248 d->len = fieldLength;
executed (the execution status of this line is deduced): d->len = fieldLength;
-
249}
executed: }
Execution Count:3
3
250 -
251/*! -
252 Sets the field's \a precision. This only affects numeric fields. -
253 -
254 \sa precision(), setType(), setRequiredStatus(), setLength(), -
255 setDefaultValue(), setGenerated(), setReadOnly() -
256*/ -
257void QSqlField::setPrecision(int precision) -
258{ -
259 detach();
executed (the execution status of this line is deduced): detach();
-
260 d->prec = precision;
executed (the execution status of this line is deduced): d->prec = precision;
-
261}
executed: }
Execution Count:3
3
262 -
263/*! -
264 Sets the default value used for this field to \a value. -
265 -
266 \sa defaultValue(), value(), setType(), setRequiredStatus(), -
267 setLength(), setPrecision(), setGenerated(), setReadOnly() -
268*/ -
269void QSqlField::setDefaultValue(const QVariant &value) -
270{ -
271 detach();
executed (the execution status of this line is deduced): detach();
-
272 d->def = value;
executed (the execution status of this line is deduced): d->def = value;
-
273}
executed: }
Execution Count:964
964
274 -
275/*! -
276 \internal -
277*/ -
278void QSqlField::setSqlType(int type) -
279{ -
280 detach();
executed (the execution status of this line is deduced): detach();
-
281 d->tp = type;
executed (the execution status of this line is deduced): d->tp = type;
-
282}
executed: }
Execution Count:4547
4547
283 -
284/*! -
285 Sets the generated state. If \a gen is false, no SQL will -
286 be generated for this field; otherwise, Qt classes such as -
287 QSqlQueryModel and QSqlTableModel will generate SQL for this -
288 field. -
289 -
290 \sa isGenerated(), setType(), setRequiredStatus(), setLength(), -
291 setPrecision(), setDefaultValue(), setReadOnly() -
292*/ -
293void QSqlField::setGenerated(bool gen) -
294{ -
295 detach();
executed (the execution status of this line is deduced): detach();
-
296 d->gen = gen;
executed (the execution status of this line is deduced): d->gen = gen;
-
297}
executed: }
Execution Count:1702
1702
298 -
299 -
300/*! -
301 Sets the value of the field to \a value. If the field is read-only -
302 (isReadOnly() returns true), nothing happens. -
303 -
304 If the data type of \a value differs from the field's current -
305 data type, an attempt is made to cast it to the proper type. This -
306 preserves the data type of the field in the case of assignment, -
307 e.g. a QString to an integer data type. -
308 -
309 To set the value to NULL, use clear(). -
310 -
311 \sa value(), isReadOnly(), defaultValue() -
312*/ -
313 -
314void QSqlField::setValue(const QVariant& value) -
315{ -
316 if (isReadOnly())
evaluated: isReadOnly()
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:1527
4-1527
317 return;
executed: return;
Execution Count:4
4
318 val = value;
executed (the execution status of this line is deduced): val = value;
-
319}
executed: }
Execution Count:1527
1527
320 -
321/*! -
322 Clears the value of the field and sets it to NULL. -
323 If the field is read-only, nothing happens. -
324 -
325 \sa setValue(), isReadOnly(), requiredStatus() -
326*/ -
327 -
328void QSqlField::clear() -
329{ -
330 if (isReadOnly())
evaluated: isReadOnly()
TRUEFALSE
yes
Evaluation Count:14
yes
Evaluation Count:129
14-129
331 return;
executed: return;
Execution Count:14
14
332 val = QVariant(type());
executed (the execution status of this line is deduced): val = QVariant(type());
-
333}
executed: }
Execution Count:129
129
334 -
335/*! -
336 Sets the name of the field to \a name. -
337 -
338 \sa name() -
339*/ -
340 -
341void QSqlField::setName(const QString& name) -
342{ -
343 detach();
executed (the execution status of this line is deduced): detach();
-
344 d->nm = name;
executed (the execution status of this line is deduced): d->nm = name;
-
345}
executed: }
Execution Count:7
7
346 -
347/*! -
348 Sets the read only flag of the field's value to \a readOnly. A -
349 read-only field cannot have its value set with setValue() and -
350 cannot be cleared to NULL with clear(). -
351*/ -
352void QSqlField::setReadOnly(bool readOnly) -
353{ -
354 detach();
executed (the execution status of this line is deduced): detach();
-
355 d->ro = readOnly;
executed (the execution status of this line is deduced): d->ro = readOnly;
-
356}
executed: }
Execution Count:32
32
357 -
358/*! -
359 \fn QVariant QSqlField::value() const -
360 -
361 Returns the value of the field as a QVariant. -
362 -
363 Use isNull() to check if the field's value is NULL. -
364*/ -
365 -
366/*! -
367 Returns the name of the field. -
368 -
369 \sa setName() -
370*/ -
371QString QSqlField::name() const -
372{ -
373 return d->nm;
executed: return d->nm;
Execution Count:4380
4380
374} -
375 -
376/*! -
377 Returns the field's type as stored in the database. -
378 Note that the actual value might have a different type, -
379 Numerical values that are too large to store in a long -
380 int or double are usually stored as strings to prevent -
381 precision loss. -
382 -
383 \sa setType() -
384*/ -
385QVariant::Type QSqlField::type() const -
386{ -
387 return d->type;
executed: return d->type;
Execution Count:307
307
388} -
389 -
390/*! -
391 Set's the field's variant type to \a type. -
392 -
393 \sa type(), setRequiredStatus(), setLength(), setPrecision(), -
394 setDefaultValue(), setGenerated(), setReadOnly() -
395*/ -
396void QSqlField::setType(QVariant::Type type) -
397{ -
398 detach();
never executed (the execution status of this line is deduced): detach();
-
399 d->type = type;
never executed (the execution status of this line is deduced): d->type = type;
-
400 if (!val.isValid())
never evaluated: !val.isValid()
0
401 val = QVariant(type);
never executed: val = QVariant(type);
0
402}
never executed: }
0
403 -
404 -
405/*! -
406 Returns true if the field's value is read-only; otherwise returns -
407 false. -
408 -
409 \sa setReadOnly(), type(), requiredStatus(), length(), precision(), -
410 defaultValue(), isGenerated() -
411*/ -
412bool QSqlField::isReadOnly() const -
413{ return d->ro; }
executed: return d->ro;
Execution Count:1943
1943
414 -
415/*! -
416 Returns true if the field's value is NULL; otherwise returns -
417 false. -
418 -
419 \sa value() -
420*/ -
421bool QSqlField::isNull() const -
422{ return val.isNull(); }
executed: return val.isNull();
Execution Count:558
558
423 -
424/*! \internal -
425*/ -
426void QSqlField::detach() -
427{ -
428 qAtomicDetach(d);
executed (the execution status of this line is deduced): qAtomicDetach(d);
-
429}
executed: }
Execution Count:8246
8246
430 -
431/*! -
432 Returns true if this is a required field; otherwise returns false. -
433 An \c INSERT will fail if a required field does not have a value. -
434 -
435 \sa setRequiredStatus(), type(), length(), precision(), defaultValue(), -
436 isGenerated() -
437*/ -
438QSqlField::RequiredStatus QSqlField::requiredStatus() const -
439{ -
440 return d->req;
executed: return d->req;
Execution Count:3
3
441} -
442 -
443/*! -
444 Returns the field's length. -
445 -
446 If the returned value is negative, it means that the information -
447 is not available from the database. -
448 -
449 \sa setLength(), type(), requiredStatus(), precision(), defaultValue(), -
450 isGenerated() -
451*/ -
452int QSqlField::length() const -
453{ -
454 return d->len;
executed: return d->len;
Execution Count:3
3
455} -
456 -
457/*! -
458 Returns the field's precision; this is only meaningful for numeric -
459 types. -
460 -
461 If the returned value is negative, it means that the information -
462 is not available from the database. -
463 -
464 \sa setPrecision(), type(), requiredStatus(), length(), defaultValue(), -
465 isGenerated() -
466*/ -
467int QSqlField::precision() const -
468{ -
469 return d->prec;
executed: return d->prec;
Execution Count:3
3
470} -
471 -
472/*! -
473 Returns the field's default value (which may be NULL). -
474 -
475 \sa setDefaultValue(), type(), requiredStatus(), length(), precision(), -
476 isGenerated() -
477*/ -
478QVariant QSqlField::defaultValue() const -
479{ -
480 return d->def;
never executed: return d->def;
0
481} -
482 -
483/*! -
484 \internal -
485 -
486 Returns the type ID for the field. -
487 -
488 If the returned value is negative, it means that the information -
489 is not available from the database. -
490*/ -
491int QSqlField::typeID() const -
492{ -
493 return d->tp;
never executed: return d->tp;
0
494} -
495 -
496/*! -
497 Returns true if the field is generated; otherwise returns -
498 false. -
499 -
500 \sa setGenerated(), type(), requiredStatus(), length(), precision(), -
501 defaultValue() -
502*/ -
503bool QSqlField::isGenerated() const -
504{ -
505 return d->gen;
executed: return d->gen;
Execution Count:8263
8263
506} -
507 -
508/*! -
509 Returns true if the field's variant type is valid; otherwise -
510 returns false. -
511*/ -
512bool QSqlField::isValid() const -
513{ -
514 return d->type != QVariant::Invalid;
executed: return d->type != QVariant::Invalid;
Execution Count:373
373
515} -
516 -
517#ifndef QT_NO_DEBUG_STREAM -
518QDebug operator<<(QDebug dbg, const QSqlField &f) -
519{ -
520 dbg.nospace() << "QSqlField(" << f.name() << ", " << QMetaType::typeName(f.type());
never executed (the execution status of this line is deduced): dbg.nospace() << "QSqlField(" << f.name() << ", " << QMetaType::typeName(f.type());
-
521 if (f.length() >= 0)
never evaluated: f.length() >= 0
0
522 dbg.nospace() << ", length: " << f.length();
never executed: dbg.nospace() << ", length: " << f.length();
0
523 if (f.precision() >= 0)
never evaluated: f.precision() >= 0
0
524 dbg.nospace() << ", precision: " << f.precision();
never executed: dbg.nospace() << ", precision: " << f.precision();
0
525 if (f.requiredStatus() != QSqlField::Unknown)
never evaluated: f.requiredStatus() != QSqlField::Unknown
0
526 dbg.nospace() << ", required: "
never executed: dbg.nospace() << ", required: " << (f.requiredStatus() == QSqlField::Required ? "yes" : "no");
0
527 << (f.requiredStatus() == QSqlField::Required ? "yes" : "no");
never executed: dbg.nospace() << ", required: " << (f.requiredStatus() == QSqlField::Required ? "yes" : "no");
0
528 dbg.nospace() << ", generated: " << (f.isGenerated() ? "yes" : "no");
never executed (the execution status of this line is deduced): dbg.nospace() << ", generated: " << (f.isGenerated() ? "yes" : "no");
-
529 if (f.typeID() >= 0)
never evaluated: f.typeID() >= 0
0
530 dbg.nospace() << ", typeID: " << f.typeID();
never executed: dbg.nospace() << ", typeID: " << f.typeID();
0
531 if (!f.defaultValue().isNull())
never evaluated: !f.defaultValue().isNull()
0
532 dbg.nospace() << ", auto-value: \"" << f.defaultValue() << '\"';
never executed: dbg.nospace() << ", auto-value: \"" << f.defaultValue() << '\"';
0
533 dbg.nospace() << ')';
never executed (the execution status of this line is deduced): dbg.nospace() << ')';
-
534 return dbg.space();
never executed: return dbg.space();
0
535} -
536#endif -
537 -
538/*! -
539 Returns true if the value is auto-generated by the database, -
540 for example auto-increment primary key values. -
541 -
542 \sa setAutoValue() -
543*/ -
544bool QSqlField::isAutoValue() const -
545{ -
546 return d->autoval;
executed: return d->autoval;
Execution Count:460
460
547} -
548 -
549/*! -
550 Marks the field as an auto-generated value if \a autoVal -
551 is true. -
552 -
553 \sa isAutoValue() -
554 */ -
555void QSqlField::setAutoValue(bool autoVal) -
556{ -
557 detach();
executed (the execution status of this line is deduced): detach();
-
558 d->autoval = autoVal;
executed (the execution status of this line is deduced): d->autoval = autoVal;
-
559}
executed: }
Execution Count:21
21
560 -
561QT_END_NAMESPACE -
562 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial