Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qsqlfield.cpp
Go to the documentation of this file.
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
// Qt-Security score:significant reason:default
4
5
#
include
"qsqlfield.h"
6
#
include
"qdebug.h"
7
8
QT_BEGIN_NAMESPACE
9
10
class
QSqlFieldPrivate
:
public
QSharedData
11
{
12
public
:
13
QSqlFieldPrivate
(
const
QString &name,
14
QMetaType type,
const
QString &tableName) :
15
nm
(
name
),
table
(
tableName
),
def
(
QVariant
()),
type
(
type
),
16
req
(
QSqlField
::
Unknown
),
len
(-1),
prec
(-1),
tp
(-1),
17
ro
(
false
),
gen
(
true
),
autoval
(
false
)
18
{}
19
20
bool
operator
==(
const
QSqlFieldPrivate
& other)
const
21
{
22
return
(nm == other.nm
23
&& table == other.table
24
&& def == other.def
25
&& type == other.type
26
&& req == other.req
27
&& len == other.len
28
&& prec == other.prec
29
&& ro == other.ro
30
&& gen == other.gen
31
&& autoval == other.autoval);
32
}
33
34
QString
nm
;
35
QString
table
;
36
QVariant
def
;
37
QMetaType
type
;
38
QSqlField
::
RequiredStatus
req
;
39
int
len
;
40
int
prec
;
41
int
tp
;
42
bool
ro
: 1;
43
bool
gen
: 1;
44
bool
autoval
: 1;
45
};
46
QT_DEFINE_QESDP_SPECIALIZATION_DTOR(QSqlFieldPrivate)
47
48
49
/*!
50
\class QSqlField
51
\brief The QSqlField class manipulates the fields in SQL database tables
52
and views.
53
54
\ingroup database
55
\ingroup shared
56
\inmodule QtSql
57
58
QSqlField represents the characteristics of a single column in a
59
database table or view, such as the data type and column name. A
60
field also contains the value of the database column, which can be
61
viewed or changed.
62
63
Field data values are stored as QVariants. Using an incompatible
64
type is not permitted. For example:
65
66
\snippet sqldatabase/sqldatabase.cpp 2
67
68
However, the field will attempt to cast certain data types to the
69
field data type where possible:
70
71
\snippet sqldatabase/sqldatabase.cpp 3
72
73
QSqlField objects are rarely created explicitly in application
74
code. They are usually accessed indirectly through \l{QSqlRecord}s
75
that already contain a list of fields. For example:
76
77
\snippet sqldatabase/sqldatabase.cpp 4
78
\dots
79
\snippet sqldatabase/sqldatabase.cpp 5
80
\snippet sqldatabase/sqldatabase.cpp 6
81
82
A QSqlField object can provide some meta-data about the field, for
83
example, its name(), variant metaType(), length(), precision(),
84
defaultValue(), typeID(), and its requiredStatus(),
85
isGenerated() and isReadOnly(). The field's data can be
86
checked to see if it isNull(), and its value() retrieved. When
87
editing the data can be set with setValue() or set to NULL with
88
clear().
89
90
\sa QSqlRecord
91
*/
92
93
/*!
94
\enum QSqlField::RequiredStatus
95
96
Specifies whether the field is required or optional.
97
98
\value Required The field must be specified when inserting records.
99
\value Optional The fields doesn't have to be specified when inserting records.
100
\value Unknown The database driver couldn't determine whether the field is required or
101
optional.
102
103
\sa requiredStatus
104
*/
105
106
/*!
107
\fn QSqlField::QSqlField(const QString &fieldName, QVariant::Type type, const QString &table)
108
\deprecated [6.0] Use the constructor taking a QMetaType instead.
109
\overload
110
111
Constructs an empty field called \a fieldName of variant type \a
112
type in \a table.
113
*/
114
115
/*!
116
\fn void QSqlField::swap(QSqlField &other)
117
\since 6.6
118
\memberswap{field}
119
*/
120
121
/*!
122
\since 6.0
123
124
\overload
125
Constructs an empty field called \a fieldName of type \a
126
type in \a table.
127
*/
128
QSqlField::QSqlField(
const
QString &fieldName, QMetaType type,
const
QString &table)
129
: val(QVariant(type,
nullptr
)),
130
d(
new
QSqlFieldPrivate(fieldName, type, table))
131
{
132
}
133
134
/*!
135
Constructs a copy of \a other.
136
*/
137
138
QSqlField::QSqlField(
const
QSqlField &other)
139
=
default
;
140
141
/*!
142
Sets the field equal to \a other.
143
*/
144
145
QSqlField& QSqlField::operator=(
const
QSqlField& other)
146
=
default
;
147
148
/*! \fn bool QSqlField::operator!=(const QSqlField &other) const
149
Returns \c true if the field is unequal to \a other; otherwise returns
150
false.
151
*/
152
153
/*!
154
Returns \c true if the field is equal to \a other; otherwise returns
155
false.
156
*/
157
bool
QSqlField::operator==(
const
QSqlField& other)
const
158
{
159
return
((d == other.d || *d == *other.d)
160
&& val == other.val);
161
}
162
163
/*!
164
Destroys the object and frees any allocated resources.
165
*/
166
167
QSqlField::~QSqlField()
168
=
default
;
169
170
/*!
171
Sets \l requiredStatus to \a required.
172
*/
173
void
QSqlField::setRequiredStatus(RequiredStatus required)
174
{
175
detach();
176
d->req = required;
177
}
178
179
/*! \fn void QSqlField::setRequired(bool required)
180
181
Sets the required status of this field to \l Required if \a
182
required is true; otherwise sets it to \l Optional.
183
184
\sa requiredStatus
185
*/
186
187
/*!
188
Sets \l length to \a fieldLength.
189
*/
190
void
QSqlField::setLength(
int
fieldLength)
191
{
192
detach();
193
d->len = fieldLength;
194
}
195
196
/*!
197
Sets \l precision to \a precision.
198
*/
199
void
QSqlField::setPrecision(
int
precision)
200
{
201
detach();
202
d->prec = precision;
203
}
204
205
/*!
206
\property QSqlField::defaultValue
207
\since 6.8
208
209
This property holds the default value for this field.
210
Only some database drivers supports this property. Currently
211
those are SQLite, PostgreSQL, Oracle and MySQL/MariaDB.
212
*/
213
214
/*!
215
Sets \l defaultValue to \a value.
216
*/
217
void
QSqlField::setDefaultValue(
const
QVariant &value)
218
{
219
detach();
220
d->def = value;
221
}
222
223
#
if
QT_DEPRECATED_SINCE
(
6
,
8
)
224
/*!
225
\internal
226
\deprecated [6.8] This internal value is no longer used.
227
*/
228
void
QSqlField::setSqlType(
int
type)
229
{
230
detach();
231
d->tp = type;
232
}
233
#
endif
234
235
/*!
236
Sets \l generated to \a gen.
237
*/
238
void
QSqlField::setGenerated(
bool
gen)
239
{
240
detach();
241
d->gen = gen;
242
}
243
244
245
/*!
246
\property QSqlField::value
247
\since 6.8
248
249
This property holds the \a value as a QVariant
250
251
Setting a \a value to a read-only QSqlField is a no-op.
252
If the data type of \a value differs from the field's current
253
data type, an attempt is made to cast it to the proper type. This
254
preserves the data type of the field in the case of assignment,
255
e.g. a QString to an integer data type.
256
257
To set the value to NULL, use clear().
258
*/
259
260
/*!
261
\fn QVariant QSqlField::value() const
262
263
Returns the value of \l value.
264
*/
265
/*!
266
Sets \l value to \a value.
267
*/
268
void
QSqlField::setValue(
const
QVariant& value)
269
{
270
if
(isReadOnly())
271
return
;
272
val = value;
273
}
274
275
/*!
276
Clears the value of the field and sets it to NULL.
277
If the field is read-only, nothing happens.
278
*/
279
280
void
QSqlField::clear()
281
{
282
if
(isReadOnly())
283
return
;
284
val = QVariant(d->type,
nullptr
);
285
}
286
287
288
/*!
289
Sets \l name to \a name.
290
*/
291
void
QSqlField::setName(
const
QString& name)
292
{
293
detach();
294
d->nm = name;
295
}
296
297
/*!
298
Sets \l readOnly to \a readOnly.
299
*/
300
void
QSqlField::setReadOnly(
bool
readOnly)
301
{
302
detach();
303
d->ro = readOnly;
304
}
305
306
/*!
307
\property QSqlField::name
308
309
This property holds the name of the field.
310
This can be the column name or a user given alias.
311
*/
312
313
/*!
314
Returns the value of \l name.
315
*/
316
QString QSqlField::name()
const
317
{
318
return
d->nm;
319
}
320
321
/*!
322
\property QSqlField::metaType
323
\since 6.8
324
325
This property holds the field's type as stored in the database.
326
Note that the actual value might have a different type,
327
Numerical values that are too large to store in a long
328
int or double are usually stored as strings to prevent
329
precision loss.
330
331
\sa QSqlDatabase::numericalPrecisionPolicy
332
*/
333
334
/*!
335
Returns the value of \l metaType.
336
*/
337
QMetaType QSqlField::metaType()
const
338
{
339
return
d->type;
340
}
341
342
/*!
343
Sets \l metaType to \a type.
344
*/
345
void
QSqlField::setMetaType(QMetaType type)
346
{
347
detach();
348
d->type = type;
349
if
(!val.isValid())
350
val = QVariant(type,
nullptr
);
351
}
352
353
/*!
354
\fn QVariant::Type QSqlField::type() const
355
\deprecated [6.0] Use metaType() instead.
356
357
Returns the field's type as stored in the database.
358
Note that the actual value might have a different type,
359
Numerical values that are too large to store in a long
360
int or double are usually stored as strings to prevent
361
precision loss.
362
363
\sa metaType
364
*/
365
366
/*!
367
\fn void QSqlField::setType(QVariant::Type type)
368
\deprecated [6.0] Use setMetaType() instead.
369
370
Sets the field's variant type to \a type.
371
372
\sa metaType
373
*/
374
375
/*!
376
\property QSqlField::readOnly
377
\since 6.8
378
379
When this property is \c true then this QSqlField cannot be modified.
380
A read-only field cannot have its value set with setValue() and
381
cannot be cleared to NULL with clear().
382
*/
383
384
/*!
385
Returns the value of \l readOnly.
386
*/
387
bool
QSqlField::isReadOnly()
const
388
{
389
return
d->ro;
390
}
391
392
/*!
393
Returns \c true if the field's value is NULL; otherwise returns
394
false.
395
396
\sa value
397
*/
398
bool
QSqlField::isNull()
const
399
{
400
return
val.isNull();
401
}
402
403
/*! \internal
404
*/
405
void
QSqlField::detach()
406
{
407
d.detach();
408
}
409
410
/*!
411
\property QSqlField::requiredStatus
412
\since 6.8
413
414
This property holds the RequiredStatus of the field.
415
An \c INSERT will fail if a required field does not have a value.
416
417
\sa RequiredStatus
418
*/
419
420
/*!
421
Returns the value of \l requiredStatus.
422
*/
423
QSqlField::RequiredStatus QSqlField::requiredStatus()
const
424
{
425
return
d->req;
426
}
427
428
/*!
429
\property QSqlField::length
430
\since 6.8
431
432
This property holds the field's length.
433
434
If the value is negative, it means that the information
435
is not available from the database.
436
For strings this is the maximum number of characters the string
437
can hold; the meaning varies for other types.
438
*/
439
440
/*!
441
Returns the value of \l length.
442
*/
443
int
QSqlField::length()
const
444
{
445
return
d->len;
446
}
447
448
/*!
449
\property QSqlField::precision
450
\since 6.8
451
452
This property holds the field's precision; this is only meaningful
453
for numeric types.
454
455
If the returned value is negative, it means that the information
456
is not available from the database.
457
*/
458
/*!
459
Returns the value of \l precision.
460
*/
461
int
QSqlField::precision()
const
462
{
463
return
d->prec;
464
}
465
466
/*!
467
Sets the value of \l defaultValue.
468
*/
469
QVariant QSqlField::defaultValue()
const
470
{
471
return
d->def;
472
}
473
474
#
if
QT_DEPRECATED_SINCE
(
6
,
8
)
475
/*!
476
\internal
477
\deprecated [6.8] This internal value is no longer used.
478
479
Returns the type ID for the field.
480
481
If the returned value is negative, it means that the information
482
is not available from the database.
483
*/
484
int
QSqlField::typeID()
const
485
{
486
return
d->tp;
487
}
488
#
endif
489
490
/*!
491
\property QSqlField::generated
492
\since 6.8
493
494
This property holds the generated state. If \a generated is \c false,
495
no SQL will be generated for this field; otherwise, Qt classes such as
496
QSqlQueryModel and QSqlTableModel will generate SQL for this field.
497
*/
498
499
/*!
500
Returns the value of \l generated.
501
*/
502
bool
QSqlField::isGenerated()
const
503
{
504
return
d->gen;
505
}
506
507
/*!
508
Returns \c true if the field's variant type is valid; otherwise
509
returns \c false.
510
*/
511
bool
QSqlField::isValid()
const
512
{
513
return
d->type.isValid();
514
}
515
516
#
ifndef
QT_NO_DEBUG_STREAM
517
QDebug
operator
<<(QDebug dbg,
const
QSqlField &f)
518
{
519
QDebugStateSaver saver(dbg);
520
dbg.nospace();
521
dbg <<
"QSqlField("
<< f.name() <<
", "
<< f.metaType().name();
522
dbg <<
", tableName: "
<< (f.tableName().isEmpty() ? QStringLiteral(
"(not specified)"
) : f.tableName());
523
if
(f.length() >= 0)
524
dbg <<
", length: "
<< f.length();
525
if
(f.precision() >= 0)
526
dbg <<
", precision: "
<< f.precision();
527
if
(f.requiredStatus() != QSqlField::Unknown)
528
dbg <<
", required: "
529
<< (f.requiredStatus() == QSqlField::Required ?
"yes"
:
"no"
);
530
dbg <<
", generated: "
<< (f.isGenerated() ?
"yes"
:
"no"
);
531
if
(!f.defaultValue().isNull())
532
dbg <<
", defaultValue: \""
<< f.defaultValue() <<
'\"'
;
533
dbg <<
", autoValue: "
<< f.isAutoValue()
534
<<
", readOnly: "
<< f.isReadOnly() <<
')'
;
535
return
dbg;
536
}
537
#
endif
538
539
/*!
540
\property QSqlField::autoValue
541
\since 6.8
542
543
If the value is auto-generated by the database,
544
for example auto-increment primary key values, this value is \c true.
545
546
\note When writing a new record to the database, an \c autoValue field will
547
only hold the new value generated by the database once the record has been
548
submitted to the database. When using the ODBC driver, due to limitations
549
in the ODBC API, the \c isAutoValue() field is only populated in a
550
QSqlField resulting from a QSqlRecord obtained by executing a \c SELECT
551
query. It is \c false for a QSqlField resulting from a QSqlRecord returned
552
from QSqlDatabase::record() or QSqlDatabase::primaryIndex().
553
*/
554
555
/*!
556
Returns the value of \l autoValue.
557
*/
558
bool
QSqlField::isAutoValue()
const
559
{
560
return
d->autoval;
561
}
562
563
/*!
564
Sets \l autoValue to \a autoVal.
565
*/
566
void
QSqlField::setAutoValue(
bool
autoVal)
567
{
568
detach();
569
d->autoval = autoVal;
570
}
571
572
/*!
573
Sets \l tableName to \a tableName.
574
*/
575
void
QSqlField::setTableName(
const
QString &tableName)
576
{
577
detach();
578
d->table = tableName;
579
}
580
581
/*!
582
\property QSqlField::tableName
583
\since 6.8
584
585
This property holds the tableName of the field.
586
587
\note When using the QPSQL driver, due to limitations in the libpq library,
588
the \c tableName() field is not populated in a QSqlField resulting
589
from a QSqlRecord obtained by QSqlQuery::record() of a forward-only query.
590
*/
591
/*!
592
Returns the \l tableName.
593
*/
594
QString QSqlField::tableName()
const
595
{
596
return
d->table;
597
}
598
599
QT_END_NAMESPACE
600
601
#
include
"moc_qsqlfield.cpp"
QSqlFieldPrivate
Definition
qsqlfield.cpp:11
QSqlFieldPrivate::ro
bool ro
Definition
qsqlfield.cpp:42
QSqlFieldPrivate::type
QMetaType type
Definition
qsqlfield.cpp:37
QSqlFieldPrivate::autoval
bool autoval
Definition
qsqlfield.cpp:44
QSqlFieldPrivate::prec
int prec
Definition
qsqlfield.cpp:40
QSqlFieldPrivate::table
QString table
Definition
qsqlfield.cpp:35
QSqlFieldPrivate::tp
int tp
Definition
qsqlfield.cpp:41
QSqlFieldPrivate::len
int len
Definition
qsqlfield.cpp:39
QSqlFieldPrivate::nm
QString nm
Definition
qsqlfield.cpp:34
QSqlFieldPrivate::def
QVariant def
Definition
qsqlfield.cpp:36
QSqlFieldPrivate::gen
bool gen
Definition
qsqlfield.cpp:43
QSqlFieldPrivate::operator==
bool operator==(const QSqlFieldPrivate &other) const
Definition
qsqlfield.cpp:20
QSqlFieldPrivate::QSqlFieldPrivate
QSqlFieldPrivate(const QString &name, QMetaType type, const QString &tableName)
Definition
qsqlfield.cpp:13
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
operator<<
QDebug operator<<(QDebug dbg, const QFileInfo &fi)
Definition
qfileinfo.cpp:1830
qtbase
src
sql
kernel
qsqlfield.cpp
Generated on
for Qt by
1.18.0