Qt 4.8
Public Types | Public Functions | Private Functions | Properties | List of all members
QSqlField Class Reference

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 Functions

void clear ()
 Clears the value of the field and sets it to NULL. More...
 
QVariant defaultValue () const
 Returns the field's default value (which may be NULL). More...
 
bool isAutoValue () const
 Returns true if the value is auto-generated by the database, for example auto-increment primary key values. More...
 
bool isGenerated () const
 Returns true if the field is generated; otherwise returns false. More...
 
bool isNull () const
 Returns true if the field's value is NULL; otherwise returns false. More...
 
bool isReadOnly () const
 Returns true if the field's value is read-only; otherwise returns false. More...
 
bool isValid () const
 Returns true if the field's variant type is valid; otherwise returns false. More...
 
int length () const
 Returns the field's length. More...
 
QString name () const
 Returns the name of the field. More...
 
bool operator!= (const QSqlField &other) const
 Returns true if the field is unequal to other; otherwise returns false. More...
 
QSqlFieldoperator= (const QSqlField &other)
 Sets the field equal to other. More...
 
bool operator== (const QSqlField &other) const
 Returns true if the field is equal to other; otherwise returns false. More...
 
int precision () const
 Returns the field's precision; this is only meaningful for numeric types. More...
 
 QSqlField (const QString &fieldName=QString(), QVariant::Type type=QVariant::Invalid)
 Constructs an empty field called fieldName of variant type type. More...
 
 QSqlField (const QSqlField &other)
 Constructs a copy of other. More...
 
RequiredStatus requiredStatus () const
 Returns true if this is a required field; otherwise returns false. More...
 
void setAutoValue (bool autoVal)
 Marks the field as an auto-generated value if autoVal is true. More...
 
void setDefaultValue (const QVariant &value)
 Sets the default value used for this field to value. More...
 
void setGenerated (bool gen)
 Sets the generated state. More...
 
void setLength (int fieldLength)
 Sets the field's length to fieldLength. More...
 
void setName (const QString &name)
 Sets the name of the field to name. More...
 
void setPrecision (int precision)
 Sets the field's precision. More...
 
void setReadOnly (bool readOnly)
 Sets the read only flag of the field's value to readOnly. More...
 
void setRequired (bool required)
 Sets the required status of this field to Required if required is true; otherwise sets it to Optional . More...
 
void setRequiredStatus (RequiredStatus status)
 Sets the required status of this field to required. More...
 
void setSqlType (int type)
 
void setType (QVariant::Type type)
 Set's the field's variant type to type. More...
 
void setValue (const QVariant &value)
 Sets the value of the field to value. More...
 
QVariant::Type type () const
 Returns the field's type as stored in the database. More...
 
int typeID () const
 Returns the type ID for the field. More...
 
QVariant value () const
 Returns the value of the field as a QVariant. More...
 
 ~QSqlField ()
 Destroys the object and frees any allocated resources. More...
 

Private Functions

void detach ()
 

Properties

QSqlFieldPrivated
 
QVariant val
 

Detailed Description

The QSqlField class manipulates the fields in SQL database tables and views.

Attention
Module: 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:

QSqlField field("age", QVariant::Int);
field.setValue(QPixmap()); // WRONG

However, the field will attempt to cast certain data types to the field data type where possible:

QSqlField field("age", QVariant::Int);
field.setValue(QString("123")); // casts QString to int

QSqlField objects are rarely created explicitly in application code. They are usually accessed indirectly through QSqlRecord that already contain a list of fields. For example:

QSqlQuery query;

...

QSqlRecord record = query.record();
QSqlField field = record.field("country");

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().

See also
QSqlRecord

Definition at line 56 of file qsqlfield.h.

Enumerations

◆ RequiredStatus

Specifies whether the field is required or optional.

  • Required The field must be specified when inserting records.
  • Optional The fields doesn't have to be specified when inserting records.
  • Unknown The database driver couldn't determine whether the field is required or optional.
See also
requiredStatus()
Enumerator
Unknown 
Optional 
Required 

Definition at line 59 of file qsqlfield.h.

Constructors and Destructors

◆ QSqlField() [1/2]

QSqlField::QSqlField ( const QString fieldName = QString(),
QVariant::Type  type = QVariant::Invalid 
)

Constructs an empty field called fieldName of variant type type.

See also
setRequiredStatus() setLength() setPrecision() setDefaultValue() setGenerated() setReadOnly()

Definition at line 168 of file qsqlfield.cpp.

169 {
170  d = new QSqlFieldPrivate(fieldName, type);
171  val = QVariant(type);
172 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QSqlFieldPrivate * d
Definition: qsqlfield.h:108
QVariant val
Definition: qsqlfield.h:107
QVariant::Type type() const
Returns the field&#39;s type as stored in the database.
Definition: qsqlfield.cpp:394

◆ QSqlField() [2/2]

QSqlField::QSqlField ( const QSqlField other)

Constructs a copy of other.

Definition at line 178 of file qsqlfield.cpp.

179 {
180  d = other.d;
181  d->ref.ref();
182  val = other.val;
183 }
QSqlFieldPrivate * d
Definition: qsqlfield.h:108
QVariant val
Definition: qsqlfield.h:107
bool ref()
Atomically increments the value of this QAtomicInt.
QAtomicInt ref
Definition: qsqlfield.cpp:85

◆ ~QSqlField()

QSqlField::~QSqlField ( )

Destroys the object and frees any allocated resources.

Definition at line 219 of file qsqlfield.cpp.

220 {
221  if (!d->ref.deref())
222  delete d;
223 }
QSqlFieldPrivate * d
Definition: qsqlfield.h:108
QAtomicInt ref
Definition: qsqlfield.cpp:85
bool deref()
Atomically decrements the value of this QAtomicInt.

Functions

◆ clear()

void QSqlField::clear ( void  )

Clears the value of the field and sets it to NULL.

If the field is read-only, nothing happens.

See also
setValue() isReadOnly() requiredStatus()

Definition at line 334 of file qsqlfield.cpp.

Referenced by QSqlResult::exec(), and qCreateParamString().

335 {
336  if (isReadOnly())
337  return;
338  val = QVariant(type());
339 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QVariant val
Definition: qsqlfield.h:107
bool isReadOnly() const
Returns true if the field&#39;s value is read-only; otherwise returns false.
Definition: qsqlfield.cpp:419
QVariant::Type type() const
Returns the field&#39;s type as stored in the database.
Definition: qsqlfield.cpp:394

◆ defaultValue()

QVariant QSqlField::defaultValue ( ) const

Returns the field's default value (which may be NULL).

See also
setDefaultValue() type() requiredStatus() length() precision() isGenerated()

Definition at line 481 of file qsqlfield.cpp.

Referenced by operator<<().

482 {
483  return d->def;
484 }
QSqlFieldPrivate * d
Definition: qsqlfield.h:108

◆ detach()

void QSqlField::detach ( )
private
Warning
This function is not part of the public interface.

Definition at line 433 of file qsqlfield.cpp.

434 {
435  qAtomicDetach(d);
436 }
QSqlFieldPrivate * d
Definition: qsqlfield.h:108
void qAtomicDetach(T *&d)
This is a helper for the detach method of implicitly shared classes.
Definition: qatomic.h:214

◆ isAutoValue()

bool QSqlField::isAutoValue ( ) const

Returns true if the value is auto-generated by the database, for example auto-increment primary key values.

See also
setAutoValue()

Definition at line 564 of file qsqlfield.cpp.

565 {
566  return d->autoval;
567 }
QSqlFieldPrivate * d
Definition: qsqlfield.h:108

◆ isGenerated()

bool QSqlField::isGenerated ( ) const

Returns true if the field is generated; otherwise returns false.

See also
setGenerated() type() requiredStatus() length() precision() defaultValue()

Definition at line 508 of file qsqlfield.cpp.

Referenced by operator<<().

509 {
510  return d->gen;
511 }
QSqlFieldPrivate * d
Definition: qsqlfield.h:108

◆ isNull()

bool QSqlField::isNull ( ) const

Returns true if the field's value is NULL; otherwise returns false.

See also
value()

Definition at line 428 of file qsqlfield.cpp.

Referenced by QDB2Driver::formatValue(), QSqlDriver::formatValue(), QTDSDriver::formatValue(), QMYSQLDriver::formatValue(), QODBCDriver::formatValue(), and QPSQLDriver::formatValue().

429 { return val.isNull(); }
bool isNull() const
Returns true if this is a NULL variant, false otherwise.
Definition: qvariant.cpp:3102
QVariant val
Definition: qsqlfield.h:107

◆ isReadOnly()

bool QSqlField::isReadOnly ( ) const

Returns true if the field's value is read-only; otherwise returns false.

See also
setReadOnly() type() requiredStatus() length() precision() defaultValue() isGenerated()

Definition at line 419 of file qsqlfield.cpp.

420 { return d->ro; }
QSqlFieldPrivate * d
Definition: qsqlfield.h:108

◆ isValid()

bool QSqlField::isValid ( ) const

Returns true if the field's variant type is valid; otherwise returns false.

Definition at line 517 of file qsqlfield.cpp.

Referenced by QDB2Result::data(), and QSqlTableModel::orderByClause().

518 {
519  return d->type != QVariant::Invalid;
520 }
QSqlFieldPrivate * d
Definition: qsqlfield.h:108
QVariant::Type type
Definition: qsqlfield.cpp:88

◆ length()

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.

See also
setLength() type() requiredStatus() precision() defaultValue() isGenerated()

Definition at line 457 of file qsqlfield.cpp.

Referenced by QDB2Result::data(), QODBCResult::data(), and operator<<().

458 {
459  return d->len;
460 }
QSqlFieldPrivate * d
Definition: qsqlfield.h:108

◆ name()

QString QSqlField::name ( ) const

Returns the name of the field.

See also
setName()

Definition at line 380 of file qsqlfield.cpp.

Referenced by QSqlRecordPrivate::createField(), QSqlIndex::createField(), operator<<(), and QSqlTableModel::orderByClause().

381 {
382  return d->nm;
383 }
QSqlFieldPrivate * d
Definition: qsqlfield.h:108

◆ operator!=()

bool QSqlField::operator!= ( const QSqlField other) const
inline

Returns true if the field is unequal to other; otherwise returns false.

Definition at line 67 of file qsqlfield.h.

67 { return !operator==(other); }
bool operator==(const QSqlField &other) const
Returns true if the field is equal to other; otherwise returns false.
Definition: qsqlfield.cpp:209

◆ operator=()

QSqlField & QSqlField::operator= ( const QSqlField other)

Sets the field equal to other.

Definition at line 189 of file qsqlfield.cpp.

190 {
191  qAtomicAssign(d, other.d);
192  val = other.val;
193  return *this;
194 }
QSqlFieldPrivate * d
Definition: qsqlfield.h:108
QVariant val
Definition: qsqlfield.h:107
void qAtomicAssign(T *&d, T *x)
This is a helper for the assignment operators of implicitly shared classes.
Definition: qatomic.h:195

◆ operator==()

bool QSqlField::operator== ( const QSqlField other) const

Returns true if the field is equal to other; otherwise returns false.

Definition at line 209 of file qsqlfield.cpp.

210 {
211  return ((d == other.d || *d == *other.d)
212  && val == other.val);
213 }
QSqlFieldPrivate * d
Definition: qsqlfield.h:108
QVariant val
Definition: qsqlfield.h:107

◆ precision()

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.

See also
setPrecision() type() requiredStatus() length() defaultValue() isGenerated()

Definition at line 471 of file qsqlfield.cpp.

Referenced by operator<<().

472 {
473  return d->prec;
474 }
QSqlFieldPrivate * d
Definition: qsqlfield.h:108

◆ requiredStatus()

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.

See also
setRequiredStatus() type() length() precision() defaultValue() isGenerated()

Definition at line 444 of file qsqlfield.cpp.

Referenced by operator<<().

445 {
446  return d->req;
447 }
QSqlFieldPrivate * d
Definition: qsqlfield.h:108
QSqlField::RequiredStatus req
Definition: qsqlfield.cpp:89

◆ setAutoValue()

void QSqlField::setAutoValue ( bool  autoVal)

Marks the field as an auto-generated value if autoVal is true.

See also
isAutoValue()

Definition at line 575 of file qsqlfield.cpp.

Referenced by qExtractSecurityPolicyFromString(), qGetTableInfo(), and qToField().

576 {
577  detach();
578  d->autoval = autoVal;
579 }
QSqlFieldPrivate * d
Definition: qsqlfield.h:108
void detach()
Definition: qsqlfield.cpp:433

◆ setDefaultValue()

void QSqlField::setDefaultValue ( const QVariant value)

Sets the default value used for this field to value.

See also
defaultValue() value() setType() setRequiredStatus() setLength() setPrecision() setGenerated() setReadOnly()

Definition at line 276 of file qsqlfield.cpp.

Referenced by QPSQLDriver::record().

277 {
278  detach();
279  d->def = value;
280 }
QSqlFieldPrivate * d
Definition: qsqlfield.h:108
QVariant value() const
Returns the value of the field as a QVariant.
Definition: qsqlfield.h:71
void detach()
Definition: qsqlfield.cpp:433

◆ setGenerated()

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.

See also
isGenerated() setType() setRequiredStatus() setLength() setPrecision() setDefaultValue() setReadOnly()

Definition at line 299 of file qsqlfield.cpp.

Referenced by QSqlQueryModel::insertColumns().

300 {
301  detach();
302  d->gen = gen;
303 }
QSqlFieldPrivate * d
Definition: qsqlfield.h:108
void detach()
Definition: qsqlfield.cpp:433

◆ setLength()

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.

See also
length() setType() setRequiredStatus() setPrecision() setDefaultValue() setGenerated() setReadOnly()

Definition at line 254 of file qsqlfield.cpp.

Referenced by qFromOraInf(), qMakeFieldInfo(), qToField(), QIBaseResult::record(), QPSQLResult::record(), QIBaseDriver::record(), QTDSDriver::record(), QPSQLDriver::record(), and QTDSResult::reset().

255 {
256  detach();
257  d->len = fieldLength;
258 }
QSqlFieldPrivate * d
Definition: qsqlfield.h:108
void detach()
Definition: qsqlfield.cpp:433

◆ setName()

void QSqlField::setName ( const QString name)

Sets the name of the field to name.

See also
name()

Definition at line 347 of file qsqlfield.cpp.

Referenced by QPSQLResult::record().

348 {
349  detach();
350  d->nm = name;
351 }
QSqlFieldPrivate * d
Definition: qsqlfield.h:108
QString name() const
Returns the name of the field.
Definition: qsqlfield.cpp:380
void detach()
Definition: qsqlfield.cpp:433

◆ setPrecision()

void QSqlField::setPrecision ( int  precision)

Sets the field's precision.

This only affects numeric fields.

See also
precision() setType() setRequiredStatus() setLength() setDefaultValue() setGenerated() setReadOnly()

Definition at line 265 of file qsqlfield.cpp.

Referenced by qFromOraInf(), qMakeFieldInfo(), qToField(), QPSQLResult::record(), QTDSDriver::record(), and QPSQLDriver::record().

266 {
267  detach();
268  d->prec = precision;
269 }
QSqlFieldPrivate * d
Definition: qsqlfield.h:108
int precision() const
Returns the field&#39;s precision; this is only meaningful for numeric types.
Definition: qsqlfield.cpp:471
void detach()
Definition: qsqlfield.cpp:433

◆ setReadOnly()

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 358 of file qsqlfield.cpp.

Referenced by QSqlQueryModel::insertColumns().

359 {
360  detach();
361  d->ro = readOnly;
362 }
QSqlFieldPrivate * d
Definition: qsqlfield.h:108
void detach()
Definition: qsqlfield.cpp:433

◆ setRequired()

void QSqlField::setRequired ( bool  required)
inline

Sets the required status of this field to Required if required is true; otherwise sets it to Optional .

See also
setRequiredStatus(), requiredStatus()

Definition at line 84 of file qsqlfield.h.

Referenced by qFromOraInf(), qMakeFieldInfo(), qToField(), QOCIDriver::record(), and QPSQLDriver::record().

85  { setRequiredStatus(required ? Required : Optional); }
void setRequiredStatus(RequiredStatus status)
Sets the required status of this field to required.
Definition: qsqlfield.cpp:230

◆ setRequiredStatus()

void QSqlField::setRequiredStatus ( RequiredStatus  required)

Sets the required status of this field to required.

See also
requiredStatus() setType() setLength() setPrecision() setDefaultValue() setGenerated() setReadOnly()

Definition at line 230 of file qsqlfield.cpp.

231 {
232  detach();
233  d->req = required;
234 }
QSqlFieldPrivate * d
Definition: qsqlfield.h:108
QSqlField::RequiredStatus req
Definition: qsqlfield.cpp:89
void detach()
Definition: qsqlfield.cpp:433

◆ setSqlType()

void QSqlField::setSqlType ( int  type)
Warning
This function is not part of the public interface.

Definition at line 285 of file qsqlfield.cpp.

Referenced by QSQLiteResultPrivate::initColumns(), qFromOraInf(), qMakeFieldInfo(), qToField(), QPSQLResult::record(), QTDSDriver::record(), QPSQLDriver::record(), and QTDSResult::reset().

286 {
287  detach();
288  d->tp = type;
289 }
QSqlFieldPrivate * d
Definition: qsqlfield.h:108
QVariant::Type type() const
Returns the field&#39;s type as stored in the database.
Definition: qsqlfield.cpp:394
void detach()
Definition: qsqlfield.cpp:433

◆ setType()

void QSqlField::setType ( QVariant::Type  type)

Set's the field's variant type to type.

See also
type() setRequiredStatus() setLength() setPrecision() setDefaultValue() setGenerated() setReadOnly()

Definition at line 404 of file qsqlfield.cpp.

Referenced by qCreateParamString(), and QPSQLResult::record().

405 {
406  detach();
407  d->type = type;
408  if (!val.isValid())
409  val = QVariant(type);
410 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QSqlFieldPrivate * d
Definition: qsqlfield.h:108
QVariant val
Definition: qsqlfield.h:107
QVariant::Type type
Definition: qsqlfield.cpp:88
QVariant::Type type() const
Returns the field&#39;s type as stored in the database.
Definition: qsqlfield.cpp:394
void detach()
Definition: qsqlfield.cpp:433
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
Definition: qvariant.h:485

◆ setValue()

void QSqlField::setValue ( const QVariant value)

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().

See also
value() isReadOnly() defaultValue()

Definition at line 320 of file qsqlfield.cpp.

Referenced by QSqlResult::exec(), and qCreateParamString().

321 {
322  if (isReadOnly())
323  return;
324  val = value;
325 }
QVariant val
Definition: qsqlfield.h:107
QVariant value() const
Returns the value of the field as a QVariant.
Definition: qsqlfield.h:71
bool isReadOnly() const
Returns true if the field&#39;s value is read-only; otherwise returns false.
Definition: qsqlfield.cpp:419

◆ type()

QVariant::Type QSqlField::type ( ) 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.

See also
setType()

Definition at line 394 of file qsqlfield.cpp.

Referenced by QDB2Result::data(), QODBCResult::data(), QDB2Driver::formatValue(), QSqlDriver::formatValue(), QIBaseDriver::formatValue(), QOCIDriver::formatValue(), QTDSDriver::formatValue(), QMYSQLDriver::formatValue(), QODBCDriver::formatValue(), QPSQLDriver::formatValue(), QTDSResult::gotoNext(), operator<<(), and QSQLite2Driver::primaryIndex().

395 {
396  return d->type;
397 }
QSqlFieldPrivate * d
Definition: qsqlfield.h:108
QVariant::Type type
Definition: qsqlfield.cpp:88

◆ typeID()

int QSqlField::typeID ( ) const

Returns the type ID for the field.

Warning
This function is not part of the public interface.

If the returned value is negative, it means that the information is not available from the database.

Definition at line 497 of file qsqlfield.cpp.

Referenced by operator<<().

498 {
499  return d->tp;
500 }
QSqlFieldPrivate * d
Definition: qsqlfield.h:108

◆ value()

QVariant QSqlField::value ( ) const
inline

Properties

◆ d

QSqlFieldPrivate* QSqlField::d
private

Definition at line 108 of file qsqlfield.h.

Referenced by operator=(), operator==(), and QSqlField().

◆ val

QVariant QSqlField::val
private

Definition at line 107 of file qsqlfield.h.

Referenced by operator=(), operator==(), and QSqlField().


The documentation for this class was generated from the following files: