Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
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
9
11{
12public:
13 QSqlFieldPrivate(const QString &name,
14 QMetaType type, const QString &tableName) :
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
39 int len;
40 int prec;
41 int tp;
42 bool ro: 1;
43 bool gen: 1;
44 bool autoval: 1;
45};
46QT_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*/
128QSqlField::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
138QSqlField::QSqlField(const QSqlField &other)
139 = default;
140
141/*!
142 Sets the field equal to \a other.
143*/
144
145QSqlField& 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*/
157bool 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
167QSqlField::~QSqlField()
168 = default;
169
170/*!
171 Sets \l requiredStatus to \a required.
172*/
173void 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*/
190void QSqlField::setLength(int fieldLength)
191{
192 detach();
193 d->len = fieldLength;
194}
195
196/*!
197 Sets \l precision to \a precision.
198*/
199void 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*/
217void 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*/
228void QSqlField::setSqlType(int type)
229{
230 detach();
231 d->tp = type;
232}
233#endif
234
235/*!
236 Sets \l generated to \a gen.
237*/
238void 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*/
268void 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
280void 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*/
291void QSqlField::setName(const QString& name)
292{
293 detach();
294 d->nm = name;
295}
296
297/*!
298 Sets \l readOnly to \a readOnly.
299*/
300void 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*/
316QString 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*/
337QMetaType QSqlField::metaType() const
338{
339 return d->type;
340}
341
342/*!
343 Sets \l metaType to \a type.
344*/
345void 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*/
387bool 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*/
398bool QSqlField::isNull() const
399{
400 return val.isNull();
401}
402
403/*! \internal
404*/
405void 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*/
423QSqlField::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*/
443int 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*/
461int QSqlField::precision() const
462{
463 return d->prec;
464}
465
466/*!
467 Sets the value of \l defaultValue.
468*/
469QVariant 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*/
484int 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*/
502bool 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*/
511bool QSqlField::isValid() const
512{
513 return d->type.isValid();
514}
515
516#ifndef QT_NO_DEBUG_STREAM
517QDebug 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 using the ODBC driver, due to limitations in the ODBC API,
547 the \c isAutoValue() field is only populated in a QSqlField resulting from a
548 QSqlRecord obtained by executing a \c SELECT query. It is \c false in a QSqlField
549 resulting from a QSqlRecord returned from QSqlDatabase::record() or
550 QSqlDatabase::primaryIndex().
551*/
552
553/*!
554 Returns the value of \l autoValue.
555*/
556bool QSqlField::isAutoValue() const
557{
558 return d->autoval;
559}
560
561/*!
562 Sets \l autoValue to \a autoVal.
563*/
564void QSqlField::setAutoValue(bool autoVal)
565{
566 detach();
567 d->autoval = autoVal;
568}
569
570/*!
571 Sets \l tableName to \a tableName.
572*/
573void QSqlField::setTableName(const QString &tableName)
574{
575 detach();
576 d->table = tableName;
577}
578
579/*!
580 \property QSqlField::tableName
581 \since 6.8
582
583 This property holds the tableName of the field.
584
585 \note When using the QPSQL driver, due to limitations in the libpq library,
586 the \c tableName() field is not populated in a QSqlField resulting
587 from a QSqlRecord obtained by QSqlQuery::record() of a forward-only query.
588*/
589/*!
590 Returns the \l tableName.
591*/
592QString QSqlField::tableName() const
593{
594 return d->table;
595}
596
597QT_END_NAMESPACE
598
599#include "moc_qsqlfield.cpp"
bool operator==(const QSqlFieldPrivate &other) const
Definition qsqlfield.cpp:20
QSqlFieldPrivate(const QString &name, QMetaType type, const QString &tableName)
Definition qsqlfield.cpp:13
QDebug operator<<(QDebug dbg, const QFileInfo &fi)