1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtSql module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include "qsqlrecord.h"
45 #include "qstringlist.h"
47 #include "qsqlfield.h"
53 class QSqlRecordPrivate
57 QSqlRecordPrivate(const QSqlRecordPrivate &other);
59 inline bool contains(int index) { return index >= 0 && index < fields.count(); }
60 QString createField(int index, const QString &prefix) const;
62 QVector<QSqlField> fields;
66 QSqlRecordPrivate::QSqlRecordPrivate() : ref(1)
70 QSqlRecordPrivate::QSqlRecordPrivate(const QSqlRecordPrivate &other): fields(other.fields), ref(1)
77 QString QSqlRecordPrivate::createField(int index, const QString &prefix) const
80 if (!prefix.isEmpty())
81 f = prefix + QLatin1Char('.');
82 f += fields.at(index).name();
88 \brief The QSqlRecord class encapsulates a database record.
94 The QSqlRecord class encapsulates the functionality and
95 characteristics of a database record (usually a row in a table or
96 view within the database). QSqlRecord supports adding and
97 removing fields as well as setting and retrieving field values.
99 The values of a record's fields' can be set by name or position
100 with setValue(); if you want to set a field to null use
101 setNull(). To find the position of a field by name use indexOf(),
102 and to find the name of a field at a particular position use
103 fieldName(). Use field() to retrieve a QSqlField object for a
104 given field. Use contains() to see if the record contains a
105 particular field name.
107 When queries are generated to be executed on the database only
108 those fields for which isGenerated() is true are included in the
111 A record can have fields added with append() or insert(), replaced
112 with replace(), and removed with remove(). All the fields can be
113 removed with clear(). The number of fields is given by count();
114 all their values can be cleared (to null) using clearValues().
116 \sa QSqlField, QSqlQuery::record()
121 Constructs an empty record.
123 \sa isEmpty(), append(), insert()
126 QSqlRecord::QSqlRecord()
128 d = new QSqlRecordPrivate();
132 Constructs a copy of \a other.
134 QSqlRecord is \l{implicitly shared}. This means you can make copies
135 of a record in \l{constant time}.
138 QSqlRecord::QSqlRecord(const QSqlRecord& other)
145 Sets the record equal to \a other.
147 QSqlRecord is \l{implicitly shared}. This means you can make copies
148 of a record in \l{constant time}.
151 QSqlRecord& QSqlRecord::operator=(const QSqlRecord& other)
153 qAtomicAssign(d, other.d);
158 Destroys the object and frees any allocated resources.
161 QSqlRecord::~QSqlRecord()
168 \fn bool QSqlRecord::operator!=(const QSqlRecord &other) const
170 Returns true if this object is not identical to \a other;
171 otherwise returns false.
177 Returns true if this object is identical to \a other (i.e., has
178 the same fields in the same order); otherwise returns false.
182 bool QSqlRecord::operator==(const QSqlRecord &other) const
184 return d->fields == other.d->fields;
188 Returns the value of the field located at position \a index in
189 the record. If \a index is out of bounds, an invalid QVariant
192 \sa fieldName(), isNull()
195 QVariant QSqlRecord::value(int index) const
197 return d->fields.value(index).value();
203 Returns the value of the field called \a name in the record. If
204 field \a name does not exist an invalid variant is returned.
209 QVariant QSqlRecord::value(const QString& name) const
211 return value(indexOf(name));
215 Returns the name of the field at position \a index. If the field
216 does not exist, an empty string is returned.
221 QString QSqlRecord::fieldName(int index) const
223 return d->fields.value(index).name();
227 Returns the position of the field called \a name within the
228 record, or -1 if it cannot be found. Field names are not
229 case-sensitive. If more than one field matches, the first one is
235 int QSqlRecord::indexOf(const QString& name) const
237 QString nm = name.toUpper();
238 for (int i = 0; i < count(); ++i) {
239 if (d->fields.at(i).name().toUpper() == nm) // TODO: case-insensitive comparison
246 Returns the field at position \a index. If the \a index
247 is out of range, function returns
248 a \l{default-constructed value}.
250 QSqlField QSqlRecord::field(int index) const
252 return d->fields.value(index);
256 Returns the field called \a name.
258 QSqlField QSqlRecord::field(const QString &name) const
260 return field(indexOf(name));
265 Append a copy of field \a field to the end of the record.
267 \sa insert(), replace(), remove()
270 void QSqlRecord::append(const QSqlField& field)
273 d->fields.append(field);
277 Inserts the field \a field at position \a pos in the record.
279 \sa append(), replace(), remove()
281 void QSqlRecord::insert(int pos, const QSqlField& field)
284 d->fields.insert(pos, field);
288 Replaces the field at position \a pos with the given \a field. If
289 \a pos is out of range, nothing happens.
291 \sa append(), insert(), remove()
294 void QSqlRecord::replace(int pos, const QSqlField& field)
296 if (!d->contains(pos))
300 d->fields[pos] = field;
304 Removes the field at position \a pos. If \a pos is out of range,
307 \sa append(), insert(), replace()
310 void QSqlRecord::remove(int pos)
312 if (!d->contains(pos))
316 d->fields.remove(pos);
320 Removes all the record's fields.
322 \sa clearValues(), isEmpty()
325 void QSqlRecord::clear()
332 Returns true if there are no fields in the record; otherwise
335 \sa append(), insert(), clear()
338 bool QSqlRecord::isEmpty() const
340 return d->fields.isEmpty();
345 Returns true if there is a field in the record called \a name;
346 otherwise returns false.
349 bool QSqlRecord::contains(const QString& name) const
351 return indexOf(name) >= 0;
355 Clears the value of all fields in the record and sets each field
361 void QSqlRecord::clearValues()
364 int count = d->fields.count();
365 for (int i = 0; i < count; ++i)
366 d->fields[i].clear();
370 Sets the generated flag for the field called \a name to \a
371 generated. If the field does not exist, nothing happens. Only
372 fields that have \a generated set to true are included in the SQL
373 that is generated by QSqlQueryModel for example.
378 void QSqlRecord::setGenerated(const QString& name, bool generated)
380 setGenerated(indexOf(name), generated);
386 Sets the generated flag for the field \a index to \a generated.
391 void QSqlRecord::setGenerated(int index, bool generated)
393 if (!d->contains(index))
396 d->fields[index].setGenerated(generated);
402 Returns true if the field \a index is null or if there is no field at
403 position \a index; otherwise returns false.
405 bool QSqlRecord::isNull(int index) const
407 return d->fields.value(index).isNull();
411 Returns true if the field called \a name is null or if there is no
412 field called \a name; otherwise returns false.
416 bool QSqlRecord::isNull(const QString& name) const
418 return isNull(indexOf(name));
422 Sets the value of field \a index to null. If the field does not exist,
427 void QSqlRecord::setNull(int index)
429 if (!d->contains(index))
432 d->fields[index].clear();
438 Sets the value of the field called \a name to null. If the field
439 does not exist, nothing happens.
441 void QSqlRecord::setNull(const QString& name)
443 setNull(indexOf(name));
448 Returns true if the record has a field called \a name and this
449 field is to be generated (the default); otherwise returns false.
453 bool QSqlRecord::isGenerated(const QString& name) const
455 return isGenerated(indexOf(name));
460 Returns true if the record has a field at position \a index and this
461 field is to be generated (the default); otherwise returns false.
465 bool QSqlRecord::isGenerated(int index) const
467 return d->fields.value(index).isGenerated();
471 Returns the number of fields in the record.
476 int QSqlRecord::count() const
478 return d->fields.count();
482 Sets the value of the field at position \a index to \a val. If the
483 field does not exist, nothing happens.
488 void QSqlRecord::setValue(int index, const QVariant& val)
490 if (!d->contains(index))
493 d->fields[index].setValue(val);
500 Sets the value of the field called \a name to \a val. If the field
501 does not exist, nothing happens.
504 void QSqlRecord::setValue(const QString& name, const QVariant& val)
506 setValue(indexOf(name), val);
512 void QSqlRecord::detach()
517 #ifndef QT_NO_DEBUG_STREAM
518 QDebug operator<<(QDebug dbg, const QSqlRecord &r)
520 dbg << "QSqlRecord(" << r.count() << ')';
521 for (int i = 0; i < r.count(); ++i)
522 dbg << '\n' << QString::fromLatin1("%1:").arg(i, 2) << r.field(i) << r.value(i).toString();