1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the sql module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
29
#include "qsqlrecord.h"
32
#include "qstringlist.h"
34
#include "qsqlfield.h"
38
class QSqlRecordPrivate
42
QSqlRecordPrivate(const QSqlRecordPrivate &other);
44
inline bool contains(int index) { return index >= 0 && index < fields.count(); }
45
QString createField(int index, const QString &prefix) const;
47
QVector<QSqlField> fields;
51
QSqlRecordPrivate::QSqlRecordPrivate()
56
QSqlRecordPrivate::QSqlRecordPrivate(const QSqlRecordPrivate &other): fields(other.fields)
64
QString QSqlRecordPrivate::createField(int index, const QString &prefix) const
67
if (!prefix.isEmpty())
68
f = prefix + QLatin1Char('.');
69
f += fields.at(index).name();
75
\brief The QSqlRecord class encapsulates a database record.
80
The QSqlRecord class encapsulates the functionality and
81
characteristics of a database record (usually a row in a table or
82
view within the database). QSqlRecord supports adding and
83
removing fields as well as setting and retrieving field values.
85
The values of a record's fields' can be set by name or position
86
with setValue(); if you want to set a field to null use
87
setNull(). To find the position of a field by name use indexOf(),
88
and to find the name of a field at a particular position use
89
fieldName(). Use field() to retrieve a QSqlField object for a
90
given field. Use contains() to see if the record contains a
91
particular field name.
93
When queries are generated to be executed on the database only
94
those fields for which isGenerated() is true are included in the
97
A record can have fields added with append() or insert(), replaced
98
with replace(), and removed with remove(). All the fields can be
99
removed with clear(). The number of fields is given by count();
100
all their values can be cleared (to null) using clearValues().
102
\sa QSqlField, QSqlQuery::record()
107
Constructs an empty record.
109
\sa isEmpty(), append(), insert()
112
QSqlRecord::QSqlRecord()
114
d = new QSqlRecordPrivate();
118
Constructs a copy of \a other.
120
QSqlRecord is \l{implicitly shared}. This means you can make copies
121
of a record in \l{constant time}.
124
QSqlRecord::QSqlRecord(const QSqlRecord& other)
131
Sets the record equal to \a other.
133
QSqlRecord is \l{implicitly shared}. This means you can make copies
134
of a record in \l{constant time}.
137
QSqlRecord& QSqlRecord::operator=(const QSqlRecord& other)
139
qAtomicAssign(d, other.d);
144
Destroys the object and frees any allocated resources.
147
QSqlRecord::~QSqlRecord()
154
\fn bool QSqlRecord::operator!=(const QSqlRecord &other) const
156
Returns true if this object is not identical to \a other;
157
otherwise returns false.
163
Returns true if this object is identical to \a other (i.e., has
164
the same fields in the same order); otherwise returns false.
168
bool QSqlRecord::operator==(const QSqlRecord &other) const
170
return d->fields == other.d->fields;
174
Returns the value of the field located at position \a index in
175
the record. If \a index is out of bounds, an invalid QVariant
178
\sa fieldName() isNull()
181
QVariant QSqlRecord::value(int index) const
183
return d->fields.value(index).value();
189
Returns the value of the field called \a name in the record. If
190
field \a name does not exist an invalid variant is returned.
195
QVariant QSqlRecord::value(const QString& name) const
197
return value(indexOf(name));
201
Returns the name of the field at position \a index. If the field
202
does not exist, an empty string is returned.
207
QString QSqlRecord::fieldName(int index) const
209
return d->fields.value(index).name();
213
Returns the position of the field called \a name within the
214
record, or -1 if it cannot be found. Field names are not
215
case-sensitive. If more than one field matches, the first one is
221
int QSqlRecord::indexOf(const QString& name) const
223
QString nm = name.toUpper();
224
for (int i = 0; i < count(); ++i) {
225
if (d->fields.at(i).name().toUpper() == nm) // TODO: case-insensitive comparison
236
const QSqlField* QSqlRecord::fieldPtr(int index) const
238
if (!d->contains(index))
241
return &d->fields.at(index);
249
const QSqlField* QSqlRecord::fieldPtr(const QString& name) const
251
int i = indexOf(name);
255
return &d->fields.at(i);
260
Returns the field at position \a index. If the position is out of
261
range, an empty field is returned.
263
QSqlField QSqlRecord::field(int index) const
265
return d->fields.value(index);
269
Returns the field called \a name.
271
QSqlField QSqlRecord::field(const QString &name) const
273
return field(indexOf(name));
278
Append a copy of field \a field to the end of the record.
280
\sa insert() replace() remove()
283
void QSqlRecord::append(const QSqlField& field)
286
d->fields.append(field);
290
Inserts the field \a field at position \a pos in the record.
292
\sa append() replace() remove()
294
void QSqlRecord::insert(int pos, const QSqlField& field)
297
d->fields.insert(pos, field);
301
Replaces the field at position \a pos with the given \a field. If
302
\a pos is out of range, nothing happens.
304
\sa append() insert() remove()
307
void QSqlRecord::replace(int pos, const QSqlField& field)
309
if (!d->contains(pos))
313
d->fields[pos] = field;
317
Removes the field at position \a pos. If \a pos is out of range,
320
\sa append() insert() replace()
323
void QSqlRecord::remove(int pos)
325
if (!d->contains(pos))
329
d->fields.remove(pos);
333
Removes all the record's fields.
335
\sa clearValues() isEmpty()
338
void QSqlRecord::clear()
345
Returns true if there are no fields in the record; otherwise
348
\sa append() insert() clear()
351
bool QSqlRecord::isEmpty() const
353
return d->fields.isEmpty();
358
Returns true if there is a field in the record called \a name;
359
otherwise returns false.
362
bool QSqlRecord::contains(const QString& name) const
364
return indexOf(name) >= 0;
368
Clears the value of all fields in the record and sets each field
374
void QSqlRecord::clearValues()
377
int count = d->fields.count();
378
for (int i = 0; i < count; ++i)
379
d->fields[i].clear();
383
Sets the generated flag for the field called \a name to \a
384
generated. If the field does not exist, nothing happens. Only
385
fields that have \a generated set to true are included in the SQL
386
that is generated by QSqlQueryModel for example.
391
void QSqlRecord::setGenerated(const QString& name, bool generated)
393
setGenerated(indexOf(name), generated);
399
Sets the generated flag for the field \a index to \a generated.
404
void QSqlRecord::setGenerated(int index, bool generated)
406
if (!d->contains(index))
409
d->fields[index].setGenerated(generated);
415
Returns true if the field \a index is null or if there is no field at
416
position \a index; otherwise returns false.
418
bool QSqlRecord::isNull(int index) const
420
return d->fields.value(index).isNull();
424
Returns true if the field called \a name is null or if there is no
425
field called \a name; otherwise returns false.
429
bool QSqlRecord::isNull(const QString& name) const
431
return isNull(indexOf(name));
435
Sets the value of field \a index to null. If the field does not exist,
440
void QSqlRecord::setNull(int index)
442
if (!d->contains(index))
445
d->fields[index].clear();
451
Sets the value of the field called \a name to null. If the field
452
does not exist, nothing happens.
454
void QSqlRecord::setNull(const QString& name)
456
setNull(indexOf(name));
461
Returns true if the record has a field called \a name and this
462
field is to be generated (the default); otherwise returns false.
466
bool QSqlRecord::isGenerated(const QString& name) const
468
return isGenerated(indexOf(name));
473
Returns true if the record has a field at position \a index and this
474
field is to be generated (the default); otherwise returns false.
478
bool QSqlRecord::isGenerated(int index) const
480
return d->fields.value(index).isGenerated();
485
Returns a list of all the record's field names as a string
488
In the unlikely event that you used this function in Qt 3, you
489
can simulate it using the rest of the QSqlRecord public API.
492
QString QSqlRecord::toString(const QString& prefix, const QString& sep) const
496
for (int i = 0; i < count(); ++i) {
497
if (!d->fields.value(i).isGenerated()) {
499
pflist += sep + QLatin1Char(' ');
500
pflist += d->createField(i, prefix);
508
Returns a list of all the record's field names, each having the
511
In the unlikely event that you used this function in Qt 3, you
512
can simulate it using the rest of the QSqlRecord public API.
515
QStringList QSqlRecord::toStringList(const QString& prefix) const
518
for (int i = 0; i < count(); ++i) {
519
if (!d->fields.value(i).isGenerated())
520
s += d->createField(i, prefix);
524
#endif // QT3_SUPPORT
527
Returns the number of fields in the record.
532
int QSqlRecord::count() const
534
return d->fields.count();
538
Sets the value of the field at position \a index to \a val. If the
539
field does not exist, nothing happens.
544
void QSqlRecord::setValue(int index, const QVariant& val)
546
if (!d->contains(index))
549
d->fields[index].setValue(val);
556
Sets the value of the field called \a name to \a val. If the field
557
does not exist, nothing happens.
560
void QSqlRecord::setValue(const QString& name, const QVariant& val)
562
setValue(indexOf(name), val);
568
void QSqlRecord::detach()
573
#ifndef QT_NO_DEBUG_STREAM
574
QDebug operator<<(QDebug dbg, const QSqlRecord &r)
576
dbg.nospace() << "QSqlRecord(" << r.count() << ")";
577
for (int i = 0; i < r.count(); ++i)
578
dbg.nospace() << QLatin1String("\n ") << QString::fromLatin1("%1: ").arg(i, 2)
585
\fn int QSqlRecord::position(const QString& name) const
587
Use indexOf() instead.