Qt 6.x
The Qt SDK
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
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
4#include "qsqlfield.h"
5#include "qdebug.h"
6
8
10{
11public:
13 QMetaType type, const QString &tableName) :
14 nm(name), table(tableName), def(QVariant()), type(type),
15 req(QSqlField::Unknown), len(-1), prec(-1), tp(-1),
16 ro(false), gen(true), autoval(false)
17 {}
18
20 {
21 return (nm == other.nm
22 && table == other.table
23 && def == other.def
24 && type == other.type
25 && req == other.req
26 && len == other.len
27 && prec == other.prec
28 && ro == other.ro
29 && gen == other.gen
30 && autoval == other.autoval);
31 }
32
38 int len;
39 int prec;
40 int tp;
41 bool ro: 1;
42 bool gen: 1;
43 bool autoval: 1;
44};
46
47
48
128 : val(QVariant(type, nullptr)),
129 d(new QSqlFieldPrivate(fieldName, type, table))
130{
131}
132
138 = default;
139
145 = default;
146
157{
158 return ((d == other.d || *d == *other.d)
159 && val == other.val);
160}
161
167 = default;
168
176{
177 detach();
178 d->req = required;
179}
180
197void QSqlField::setLength(int fieldLength)
198{
199 detach();
200 d->len = fieldLength;
201}
202
210{
211 detach();
212 d->prec = precision;
213}
214
222{
223 detach();
224 d->def = value;
225}
226
231{
232 detach();
233 d->tp = type;
234}
235
246{
247 detach();
248 d->gen = gen;
249}
250
251
267{
268 if (isReadOnly())
269 return;
270 val = value;
271}
272
281{
282 if (isReadOnly())
283 return;
284 val = QVariant(d->type, nullptr);
285}
286
294{
295 detach();
296 d->nm = name;
297}
298
304void QSqlField::setReadOnly(bool readOnly)
305{
306 detach();
307 d->ro = readOnly;
308}
309
324{
325 return d->nm;
326}
327
338{
339 return d->type;
340}
341
349{
350 detach();
351 d->type = type;
352 if (!val.isValid())
353 val = QVariant(type, nullptr);
354}
355
386{ return d->ro; }
387
395{ return val.isNull(); }
396
399void QSqlField::detach()
400{
401 d.detach();
402}
403
412{
413 return d->req;
414}
415
426{
427 return d->len;
428}
429
441{
442 return d->prec;
443}
444
452{
453 return d->def;
454}
455
465{
466 return d->tp;
467}
468
477{
478 return d->gen;
479}
480
486{
487 return d->type.isValid();
488}
489
490#ifndef QT_NO_DEBUG_STREAM
492{
493 QDebugStateSaver saver(dbg);
494 dbg.nospace();
495 dbg << "QSqlField(" << f.name() << ", " << f.metaType().name();
496 dbg << ", tableName: " << (f.tableName().isEmpty() ? QStringLiteral("(not specified)") : f.tableName());
497 if (f.length() >= 0)
498 dbg << ", length: " << f.length();
499 if (f.precision() >= 0)
500 dbg << ", precision: " << f.precision();
501 if (f.requiredStatus() != QSqlField::Unknown)
502 dbg << ", required: "
503 << (f.requiredStatus() == QSqlField::Required ? "yes" : "no");
504 dbg << ", generated: " << (f.isGenerated() ? "yes" : "no");
505 if (f.typeID() >= 0)
506 dbg << ", typeID: " << f.typeID();
507 if (!f.defaultValue().isNull())
508 dbg << ", defaultValue: \"" << f.defaultValue() << '\"';
509 dbg << ", autoValue: " << f.isAutoValue()
510 << ", readOnly: " << f.isReadOnly() << ')';
511 return dbg;
512}
513#endif
514
528{
529 return d->autoval;
530}
531
538void QSqlField::setAutoValue(bool autoVal)
539{
540 detach();
541 d->autoval = autoVal;
542}
543
550{
551 detach();
552 d->table = table;
553}
554
565{
566 return d->table;
567}
568
\inmodule QtCore
\inmodule QtCore
void detach()
If the shared data object's reference count is greater than 1, this function creates a deep copy of t...
\inmodule QtCore
Definition qmetatype.h:320
bool isValid() const
\inmodule QtCore
Definition qshareddata.h:19
QSqlField::RequiredStatus req
Definition qsqlfield.cpp:37
bool operator==(const QSqlFieldPrivate &other) const
Definition qsqlfield.cpp:19
QSqlFieldPrivate(const QString &name, QMetaType type, const QString &tableName)
Definition qsqlfield.cpp:12
The QSqlField class manipulates the fields in SQL database tables and views.
Definition qsqlfield.h:19
QSqlField(const QString &fieldName=QString(), QMetaType type=QMetaType(), const QString &tableName=QString())
RequiredStatus
Specifies whether the field is required or optional.
Definition qsqlfield.h:21
void setAutoValue(bool autoVal)
Marks the field as an auto-generated value if autoVal is true.
bool isGenerated() const
Returns true if the field is generated; otherwise returns false.
void setRequiredStatus(RequiredStatus status)
Sets the required status of this field to required.
QMetaType metaType() const
Returns the field's type as stored in the database.
bool isValid() const
Returns true if the field's variant type is valid; otherwise returns false.
bool operator==(const QSqlField &other) const
Returns true if the field is equal to other; otherwise returns false.
void setValue(const QVariant &value)
Sets the value of the field to value.
void setLength(int fieldLength)
Sets the field's length to fieldLength.
void setDefaultValue(const QVariant &value)
Sets the default value used for this field to value.
void setName(const QString &name)
Sets the name of the field to name.
int typeID() const
RequiredStatus requiredStatus() const
Returns true if this is a required field; otherwise returns false.
QString tableName() const
Returns the tableName of the field.
void setMetaType(QMetaType type)
Set's the field's variant type to type.
int precision() const
Returns the field's precision; this is only meaningful for numeric types.
void setReadOnly(bool readOnly)
Sets the read only flag of the field's value to readOnly.
void clear()
Clears the value of the field and sets it to NULL.
~QSqlField()
Destroys the object and frees any allocated resources.
QVariant value() const
Returns the value of the field as a QVariant.
Definition qsqlfield.h:37
bool isNull() const
Returns true if the field's value is NULL; otherwise returns false.
QVariant defaultValue() const
Returns the field's default value (which may be NULL).
bool isAutoValue() const
Returns true if the value is auto-generated by the database, for example auto-increment primary key v...
bool isReadOnly() const
Returns true if the field's value is read-only; otherwise returns false.
void setPrecision(int precision)
Sets the field's precision.
void setSqlType(int type)
void setTableName(const QString &tableName)
Sets the tableName of the field to table.
void setGenerated(bool gen)
Sets the generated state.
QString name() const
Returns the name of the field.
int length() const
Returns the field's length.
QSqlField & operator=(const QSqlField &other)
Sets the field equal to other.
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
\inmodule QtCore
Definition qvariant.h:64
Combined button and popup list for selecting options.
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
GLfloat GLfloat f
GLenum type
GLuint name
GLuint GLfloat * val
GLenum GLsizei len
GLenum GLenum GLsizei void * table
GLenum GLint GLint * precision
#define QT_DEFINE_QESDP_SPECIALIZATION_DTOR(Class)
QDebug operator<<(QDebug dbg, const QSqlField &f)
#define QStringLiteral(str)
QSharedPointer< T > other(t)
[5]