1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtSql module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
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()
71 QSqlRecordPrivate::QSqlRecordPrivate(const QSqlRecordPrivate &other): fields(other.fields)
79 QString QSqlRecordPrivate::createField(int index, const QString &prefix) const
82 if (!prefix.isEmpty())
83 f = prefix + QLatin1Char('.');
84 f += fields.at(index).name();
90 \brief The QSqlRecord class encapsulates a database record.
96 The QSqlRecord class encapsulates the functionality and
97 characteristics of a database record (usually a row in a table or
98 view within the database). QSqlRecord supports adding and
99 removing fields as well as setting and retrieving field values.
101 The values of a record's fields' can be set by name or position
102 with setValue(); if you want to set a field to null use
103 setNull(). To find the position of a field by name use indexOf(),
104 and to find the name of a field at a particular position use
105 fieldName(). Use field() to retrieve a QSqlField object for a
106 given field. Use contains() to see if the record contains a
107 particular field name.
109 When queries are generated to be executed on the database only
110 those fields for which isGenerated() is true are included in the
113 A record can have fields added with append() or insert(), replaced
114 with replace(), and removed with remove(). All the fields can be
115 removed with clear(). The number of fields is given by count();
116 all their values can be cleared (to null) using clearValues().
118 \sa QSqlField, QSqlQuery::record()
123 Constructs an empty record.
125 \sa isEmpty(), append(), insert()
128 QSqlRecord::QSqlRecord()
130 d = new QSqlRecordPrivate();
134 Constructs a copy of \a other.
136 QSqlRecord is \l{implicitly shared}. This means you can make copies
137 of a record in \l{constant time}.
140 QSqlRecord::QSqlRecord(const QSqlRecord& other)
147 Sets the record equal to \a other.
149 QSqlRecord is \l{implicitly shared}. This means you can make copies
150 of a record in \l{constant time}.
153 QSqlRecord& QSqlRecord::operator=(const QSqlRecord& other)
155 qAtomicAssign(d, other.d);
160 Destroys the object and frees any allocated resources.
163 QSqlRecord::~QSqlRecord()
170 \fn bool QSqlRecord::operator!=(const QSqlRecord &other) const
172 Returns true if this object is not identical to \a other;
173 otherwise returns false.
179 Returns true if this object is identical to \a other (i.e., has
180 the same fields in the same order); otherwise returns false.
184 bool QSqlRecord::operator==(const QSqlRecord &other) const
186 return d->fields == other.d->fields;
190 Returns the value of the field located at position \a index in
191 the record. If \a index is out of bounds, an invalid QVariant
194 \sa fieldName() isNull()
197 QVariant QSqlRecord::value(int index) const
199 return d->fields.value(index).value();
205 Returns the value of the field called \a name in the record. If
206 field \a name does not exist an invalid variant is returned.
211 QVariant QSqlRecord::value(const QString& name) const
213 return value(indexOf(name));
217 Returns the name of the field at position \a index. If the field
218 does not exist, an empty string is returned.
223 QString QSqlRecord::fieldName(int index) const
225 return d->fields.value(index).name();
229 Returns the position of the field called \a name within the
230 record, or -1 if it cannot be found. Field names are not
231 case-sensitive. If more than one field matches, the first one is
237 int QSqlRecord::indexOf(const QString& name) const
239 QString nm = name.toUpper();
240 for (int i = 0; i < count(); ++i) {
241 if (d->fields.at(i).name().toUpper() == nm) // TODO: case-insensitive comparison
248 Returns the field at position \a index. If the position is out of
249 range, an empty field is returned.
251 QSqlField QSqlRecord::field(int index) const
253 return d->fields.value(index);
257 Returns the field called \a name.
259 QSqlField QSqlRecord::field(const QString &name) const
261 return field(indexOf(name));
266 Append a copy of field \a field to the end of the record.
268 \sa insert() replace() remove()
271 void QSqlRecord::append(const QSqlField& field)
274 d->fields.append(field);
278 Inserts the field \a field at position \a pos in the record.
280 \sa append() replace() remove()
282 void QSqlRecord::insert(int pos, const QSqlField& field)
285 d->fields.insert(pos, field);
289 Replaces the field at position \a pos with the given \a field. If
290 \a pos is out of range, nothing happens.
292 \sa append() insert() remove()
295 void QSqlRecord::replace(int pos, const QSqlField& field)
297 if (!d->contains(pos))
301 d->fields[pos] = field;
305 Removes the field at position \a pos. If \a pos is out of range,
308 \sa append() insert() replace()
311 void QSqlRecord::remove(int pos)
313 if (!d->contains(pos))
317 d->fields.remove(pos);
321 Removes all the record's fields.
323 \sa clearValues() isEmpty()
326 void QSqlRecord::clear()
333 Returns true if there are no fields in the record; otherwise
336 \sa append() insert() clear()
339 bool QSqlRecord::isEmpty() const
341 return d->fields.isEmpty();
346 Returns true if there is a field in the record called \a name;
347 otherwise returns false.
350 bool QSqlRecord::contains(const QString& name) const
352 return indexOf(name) >= 0;
356 Clears the value of all fields in the record and sets each field
362 void QSqlRecord::clearValues()
365 int count = d->fields.count();
366 for (int i = 0; i < count; ++i)
367 d->fields[i].clear();
371 Sets the generated flag for the field called \a name to \a
372 generated. If the field does not exist, nothing happens. Only
373 fields that have \a generated set to true are included in the SQL
374 that is generated by QSqlQueryModel for example.
379 void QSqlRecord::setGenerated(const QString& name, bool generated)
381 setGenerated(indexOf(name), generated);
387 Sets the generated flag for the field \a index to \a generated.
392 void QSqlRecord::setGenerated(int index, bool generated)
394 if (!d->contains(index))
397 d->fields[index].setGenerated(generated);
403 Returns true if the field \a index is null or if there is no field at
404 position \a index; otherwise returns false.
406 bool QSqlRecord::isNull(int index) const
408 return d->fields.value(index).isNull();
412 Returns true if the field called \a name is null or if there is no
413 field called \a name; otherwise returns false.
417 bool QSqlRecord::isNull(const QString& name) const
419 return isNull(indexOf(name));
423 Sets the value of field \a index to null. If the field does not exist,
428 void QSqlRecord::setNull(int index)
430 if (!d->contains(index))
433 d->fields[index].clear();
439 Sets the value of the field called \a name to null. If the field
440 does not exist, nothing happens.
442 void QSqlRecord::setNull(const QString& name)
444 setNull(indexOf(name));
449 Returns true if the record has a field called \a name and this
450 field is to be generated (the default); otherwise returns false.
454 bool QSqlRecord::isGenerated(const QString& name) const
456 return isGenerated(indexOf(name));
461 Returns true if the record has a field at position \a index and this
462 field is to be generated (the default); otherwise returns false.
466 bool QSqlRecord::isGenerated(int index) const
468 return d->fields.value(index).isGenerated();
472 Returns the number of fields in the record.
477 int QSqlRecord::count() const
479 return d->fields.count();
483 Sets the value of the field at position \a index to \a val. If the
484 field does not exist, nothing happens.
489 void QSqlRecord::setValue(int index, const QVariant& val)
491 if (!d->contains(index))
494 d->fields[index].setValue(val);
501 Sets the value of the field called \a name to \a val. If the field
502 does not exist, nothing happens.
505 void QSqlRecord::setValue(const QString& name, const QVariant& val)
507 setValue(indexOf(name), val);
513 void QSqlRecord::detach()
518 #ifndef QT_NO_DEBUG_STREAM
519 QDebug operator<<(QDebug dbg, const QSqlRecord &r)
521 dbg << "QSqlRecord(" << r.count() << ')';
522 for (int i = 0; i < r.count(); ++i)
523 dbg << '\n' << QString::fromLatin1("%1:").arg(i, 2) << r.field(i) << r.value(i).toString();
529 \fn int QSqlRecord::position(const QString& name) const
531 Use indexOf() instead.