![]() |
Qt 6.x
The Qt SDK
|
The QSqlField class manipulates the fields in SQL database tables and views. More...
#include <qsqlfield.h>
Public Types | |
enum | RequiredStatus { Unknown = -1 , Optional = 0 , Required = 1 } |
Specifies whether the field is required or optional. More... | |
Public Member Functions | |
QSqlField (const QString &fieldName=QString(), QMetaType type=QMetaType(), const QString &tableName=QString()) | |
QSqlField (const QSqlField &other) | |
Constructs a copy of other. | |
QSqlField & | operator= (const QSqlField &other) |
Sets the field equal to other. | |
QSqlField (QSqlField &&other) noexcept=default | |
~QSqlField () | |
Destroys the object and frees any allocated resources. | |
void | swap (QSqlField &other) noexcept |
bool | operator== (const QSqlField &other) const |
Returns true if the field is equal to other; otherwise returns false. | |
bool | operator!= (const QSqlField &other) const |
Returns true if the field is unequal to other; otherwise returns false. | |
void | setValue (const QVariant &value) |
Sets the value of the field to value. | |
QVariant | value () const |
Returns the value of the field as a QVariant. | |
void | setName (const QString &name) |
Sets the name of the field to name. | |
QString | name () const |
Returns the name of the field. | |
void | setTableName (const QString &tableName) |
Sets the tableName of the field to table. | |
QString | tableName () const |
Returns the tableName of the field. | |
bool | isNull () const |
Returns true if the field's value is NULL; otherwise returns false. | |
void | setReadOnly (bool readOnly) |
Sets the read only flag of the field's value to readOnly. | |
bool | isReadOnly () const |
Returns true if the field's value is read-only; otherwise returns false. | |
void | clear () |
Clears the value of the field and sets it to NULL. | |
bool | isAutoValue () const |
Returns true if the value is auto-generated by the database, for example auto-increment primary key values. | |
QMetaType | metaType () const |
Returns the field's type as stored in the database. | |
void | setMetaType (QMetaType type) |
Set's the field's variant type to type. | |
void | setRequiredStatus (RequiredStatus status) |
Sets the required status of this field to required. | |
void | setRequired (bool required) |
Sets the required status of this field to \l Required if required is true; otherwise sets it to \l Optional. | |
void | setLength (int fieldLength) |
Sets the field's length to fieldLength. | |
void | setPrecision (int precision) |
Sets the field's precision. | |
void | setDefaultValue (const QVariant &value) |
Sets the default value used for this field to value. | |
void | setSqlType (int type) |
void | setGenerated (bool gen) |
Sets the generated state. | |
void | setAutoValue (bool autoVal) |
Marks the field as an auto-generated value if autoVal is true. | |
RequiredStatus | requiredStatus () const |
Returns true if this is a required field; otherwise returns false . | |
int | length () const |
Returns the field's length. | |
int | precision () const |
Returns the field's precision; this is only meaningful for numeric types. | |
QVariant | defaultValue () const |
Returns the field's default value (which may be NULL). | |
int | typeID () const |
bool | isGenerated () const |
Returns true if the field is generated; otherwise returns false. | |
bool | isValid () const |
Returns true if the field's variant type is valid; otherwise returns false . | |
The QSqlField class manipulates the fields in SQL database tables and views.
\inmodule QtSql
QSqlField represents the characteristics of a single column in a database table or view, such as the data type and column name. A field also contains the value of the database column, which can be viewed or changed.
Field data values are stored as QVariants. Using an incompatible type is not permitted. For example:
However, the field will attempt to cast certain data types to the field data type where possible:
QSqlField objects are rarely created explicitly in application code. They are usually accessed indirectly through \l{QSqlRecord}s that already contain a list of fields. For example:
\dots
A QSqlField object can provide some meta-data about the field, for example, its name(), variant type(), length(), precision(), defaultValue(), typeID(), and its requiredStatus(), isGenerated() and isReadOnly(). The field's data can be checked to see if it isNull(), and its value() retrieved. When editing the data can be set with setValue() or set to NULL with clear().
Definition at line 18 of file qsqlfield.h.
Specifies whether the field is required or optional.
\value Required The field must be specified when inserting records. \value Optional The fields doesn't have to be specified when inserting records. \value Unknown The database driver couldn't determine whether the field is required or optional.
Enumerator | |
---|---|
Unknown | |
Optional | |
Required |
Definition at line 21 of file qsqlfield.h.
|
explicit |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Constructs an empty field called fieldName of type type in table.
Definition at line 127 of file qsqlfield.cpp.
|
default |
Constructs a copy of other.
|
defaultnoexcept |
|
default |
Destroys the object and frees any allocated resources.
void QSqlField::clear | ( | ) |
Clears the value of the field and sets it to NULL.
If the field is read-only, nothing happens.
Definition at line 280 of file qsqlfield.cpp.
References isReadOnly(), and QSqlFieldPrivate::type.
QVariant QSqlField::defaultValue | ( | ) | const |
Returns the field's default value (which may be NULL).
Definition at line 451 of file qsqlfield.cpp.
References QSqlFieldPrivate::def.
bool QSqlField::isAutoValue | ( | ) | const |
Returns true
if the value is auto-generated by the database, for example auto-increment primary key values.
isAutoValue()
field is only populated in a QSqlField resulting from a QSqlRecord obtained by executing a SELECT
query. It is false
in a QSqlField resulting from a QSqlRecord returned from QSqlDatabase::record() or QSqlDatabase::primaryIndex().Definition at line 527 of file qsqlfield.cpp.
References QSqlFieldPrivate::autoval.
bool QSqlField::isGenerated | ( | ) | const |
Returns true
if the field is generated; otherwise returns false.
Definition at line 476 of file qsqlfield.cpp.
References QSqlFieldPrivate::gen.
Referenced by QSqlRecord::isGenerated().
bool QSqlField::isNull | ( | ) | const |
Returns true
if the field's value is NULL; otherwise returns false.
Definition at line 394 of file qsqlfield.cpp.
Referenced by QDB2Driver::formatValue(), QMYSQLDriver::formatValue(), QODBCDriver::formatValue(), QPSQLDriver::formatValue(), QSqlDriver::formatValue(), and QSqlRecord::isNull().
bool QSqlField::isReadOnly | ( | ) | const |
Returns true
if the field's value is read-only; otherwise returns false.
Definition at line 385 of file qsqlfield.cpp.
References QSqlFieldPrivate::ro.
Referenced by clear(), and setValue().
bool QSqlField::isValid | ( | ) | const |
Returns true
if the field's variant type is valid; otherwise returns false
.
Definition at line 485 of file qsqlfield.cpp.
References QMetaType::isValid(), and QSqlFieldPrivate::type.
int QSqlField::length | ( | ) | const |
Returns the field's length.
If the returned value is negative, it means that the information is not available from the database.
Definition at line 425 of file qsqlfield.cpp.
References QSqlFieldPrivate::len.
QMetaType QSqlField::metaType | ( | ) | const |
Returns the field's type as stored in the database.
Note that the actual value might have a different type, Numerical values that are too large to store in a long int or double are usually stored as strings to prevent precision loss.
Definition at line 337 of file qsqlfield.cpp.
References QSqlFieldPrivate::type.
Referenced by QDB2Driver::formatValue(), QIBaseDriver::formatValue(), QMYSQLDriver::formatValue(), QODBCDriver::formatValue(), QPSQLDriver::formatValue(), QSqlDriver::formatValue(), and QMimerSQLResult::record().
QString QSqlField::name | ( | ) | const |
Returns the name of the field.
Definition at line 323 of file qsqlfield.cpp.
References QSqlFieldPrivate::nm.
Referenced by QSqlRecord::fieldName(), QSqlRecord::indexOf(), and setName().
|
inline |
Returns true
if the field is unequal to other; otherwise returns false.
Definition at line 34 of file qsqlfield.h.
References operator==(), and other().
bool QSqlField::operator== | ( | const QSqlField & | other | ) | const |
Returns true
if the field is equal to other; otherwise returns false.
Definition at line 156 of file qsqlfield.cpp.
References other().
int QSqlField::precision | ( | ) | const |
Returns the field's precision; this is only meaningful for numeric types.
If the returned value is negative, it means that the information is not available from the database.
Definition at line 440 of file qsqlfield.cpp.
References QSqlFieldPrivate::prec.
Referenced by QMYSQLDriver::formatValue(), and setPrecision().
QSqlField::RequiredStatus QSqlField::requiredStatus | ( | ) | const |
Returns true
if this is a required field; otherwise returns false
.
An INSERT
will fail if a required field does not have a value.
Definition at line 411 of file qsqlfield.cpp.
References QSqlFieldPrivate::req.
void QSqlField::setAutoValue | ( | bool | autoVal | ) |
Marks the field as an auto-generated value if autoVal is true.
Definition at line 538 of file qsqlfield.cpp.
References QSqlFieldPrivate::autoval.
Referenced by qGetTableInfo().
Sets the default value used for this field to value.
Definition at line 221 of file qsqlfield.cpp.
References QSqlFieldPrivate::def, and value().
Referenced by qGetTableInfo().
void QSqlField::setGenerated | ( | bool | gen | ) |
Sets the generated state.
If gen is false, no SQL will be generated for this field; otherwise, Qt classes such as QSqlQueryModel and QSqlTableModel will generate SQL for this field.
Definition at line 245 of file qsqlfield.cpp.
References QSqlFieldPrivate::gen.
Referenced by QSqlQueryModel::insertColumns().
void QSqlField::setLength | ( | int | fieldLength | ) |
Sets the field's length to fieldLength.
For strings this is the maximum number of characters the string can hold; the meaning varies for other types.
Definition at line 197 of file qsqlfield.cpp.
References QSqlFieldPrivate::len.
Set's the field's variant type to type.
Definition at line 348 of file qsqlfield.cpp.
References QSqlFieldPrivate::type.
Referenced by QMimerSQLResult::record().
Sets the name of the field to name.
Definition at line 293 of file qsqlfield.cpp.
References name(), and QSqlFieldPrivate::nm.
Referenced by QMimerSQLResult::record().
void QSqlField::setPrecision | ( | int | precision | ) |
Sets the field's precision.
This only affects numeric fields.
Definition at line 209 of file qsqlfield.cpp.
References QSqlFieldPrivate::prec, and precision().
Referenced by QMimerSQLDriver::record().
void QSqlField::setReadOnly | ( | bool | readOnly | ) |
Sets the read only flag of the field's value to readOnly.
A read-only field cannot have its value set with setValue() and cannot be cleared to NULL with clear().
Definition at line 304 of file qsqlfield.cpp.
References QSqlFieldPrivate::ro.
Referenced by QSqlQueryModel::insertColumns().
|
inline |
Sets the required status of this field to \l Required if required is true; otherwise sets it to \l Optional.
Definition at line 67 of file qsqlfield.h.
References Required.
Referenced by qGetTableInfo(), and QMimerSQLDriver::record().
void QSqlField::setRequiredStatus | ( | RequiredStatus | required | ) |
Sets the required status of this field to required.
Definition at line 175 of file qsqlfield.cpp.
References QSqlFieldPrivate::req.
void QSqlField::setSqlType | ( | int | type | ) |
Definition at line 230 of file qsqlfield.cpp.
References QSqlFieldPrivate::tp.
Referenced by QSQLiteResultPrivate::initColumns(), and QMimerSQLResult::record().
Sets the tableName of the field to table.
Definition at line 549 of file qsqlfield.cpp.
References QSqlFieldPrivate::table.
Referenced by QDB2Driver::record().
Sets the value of the field to value.
If the field is read-only (isReadOnly() returns true
), nothing happens.
If the data type of value differs from the field's current data type, an attempt is made to cast it to the proper type. This preserves the data type of the field in the case of assignment, e.g. a QString to an integer data type.
To set the value to NULL, use clear().
Definition at line 266 of file qsqlfield.cpp.
References isReadOnly(), and value().
Referenced by QSqlField_snippets(), and QMimerSQLResult::record().
QString QSqlField::tableName | ( | ) | const |
Returns the tableName of the field.
tableName()
field is not populated in a QSqlField resulting from a QSqlRecord obtained by QSqlQuery::record() of a forward-only query.Definition at line 564 of file qsqlfield.cpp.
References QSqlFieldPrivate::table.
int QSqlField::typeID | ( | ) | const |
Returns the type ID for the field.
If the returned value is negative, it means that the information is not available from the database.
Definition at line 464 of file qsqlfield.cpp.
References QSqlFieldPrivate::tp.
Referenced by QOCIDriver::formatValue().
|
inline |
Returns the value of the field as a QVariant.
Use isNull() to check if the field's value is NULL.
Definition at line 37 of file qsqlfield.h.
Referenced by QDB2Driver::formatValue(), QIBaseDriver::formatValue(), QMYSQLDriver::formatValue(), QOCIDriver::formatValue(), QODBCDriver::formatValue(), QPSQLDriver::formatValue(), QSqlDriver::formatValue(), setDefaultValue(), setValue(), and QSqlRecord::value().