Qt 4.8
qsqlrecord.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSql module of the Qt Toolkit.
7 **
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.
16 **
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.
24 **
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.
28 **
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.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qsqlrecord.h"
43 
44 #include "qdebug.h"
45 #include "qstringlist.h"
46 #include "qatomic.h"
47 #include "qsqlfield.h"
48 #include "qstring.h"
49 #include "qvector.h"
50 
52 
54 {
55 public:
58 
59  inline bool contains(int index) { return index >= 0 && index < fields.count(); }
60  QString createField(int index, const QString &prefix) const;
61 
64 };
65 
67 {
68  ref = 1;
69 }
70 
72 {
73  ref = 1;
74 }
75 
80 {
81  QString f;
82  if (!prefix.isEmpty())
83  f = prefix + QLatin1Char('.');
84  f += fields.at(index).name();
85  return f;
86 }
87 
132 {
133  d = new QSqlRecordPrivate();
134 }
135 
144 {
145  d = other.d;
146  d->ref.ref();
147 }
148 
157 {
158  qAtomicAssign(d, other.d);
159  return *this;
160 }
161 
167 {
168  if (!d->ref.deref())
169  delete d;
170 }
171 
190 bool QSqlRecord::operator==(const QSqlRecord &other) const
191 {
192  return d->fields == other.d->fields;
193 }
194 
204 {
205  return d->fields.value(index).value();
206 }
207 
221 {
222  return value(indexOf(name));
223 }
224 
233 {
234  return d->fields.value(index).name();
235 }
236 
247 {
248  QString nm = name.toUpper();
249  for (int i = 0; i < count(); ++i) {
250  if (d->fields.at(i).name().toUpper() == nm) // TODO: case-insensitive comparison
251  return i;
252  }
253  return -1;
254 }
255 
256 #ifdef QT3_SUPPORT
257 
261 const QSqlField* QSqlRecord::fieldPtr(int index) const
262 {
263  if (!d->contains(index))
264  return 0;
265 
266  return &d->fields.at(index);
267 }
268 
274 const QSqlField* QSqlRecord::fieldPtr(const QString& name) const
275 {
276  int i = indexOf(name);
277  if (!d->contains(i))
278  return 0;
279 
280  return &d->fields.at(i);
281 }
282 #endif //QT3_SUPPORT
283 
290 {
291  return d->fields.value(index);
292 }
293 
301 {
302  return field(indexOf(name));
303 }
304 
305 
312 void QSqlRecord::append(const QSqlField& field)
313 {
314  detach();
315  d->fields.append(field);
316 }
317 
323 void QSqlRecord::insert(int pos, const QSqlField& field)
324 {
325  detach();
326  d->fields.insert(pos, field);
327 }
328 
336 void QSqlRecord::replace(int pos, const QSqlField& field)
337 {
338  if (!d->contains(pos))
339  return;
340 
341  detach();
342  d->fields[pos] = field;
343 }
344 
352 void QSqlRecord::remove(int pos)
353 {
354  if (!d->contains(pos))
355  return;
356 
357  detach();
358  d->fields.remove(pos);
359 }
360 
368 {
369  detach();
370  d->fields.clear();
371 }
372 
381 {
382  return d->fields.isEmpty();
383 }
384 
385 
391 bool QSqlRecord::contains(const QString& name) const
392 {
393  return indexOf(name) >= 0;
394 }
395 
404 {
405  detach();
406  int count = d->fields.count();
407  for (int i = 0; i < count; ++i)
408  d->fields[i].clear();
409 }
410 
420 void QSqlRecord::setGenerated(const QString& name, bool generated)
421 {
422  setGenerated(indexOf(name), generated);
423 }
424 
436 void QSqlRecord::setGenerated(int index, bool generated)
437 {
438  if (!d->contains(index))
439  return;
440  detach();
441  d->fields[index].setGenerated(generated);
442 }
443 
453 bool QSqlRecord::isNull(int index) const
454 {
455  return d->fields.value(index).isNull();
456 }
457 
464 bool QSqlRecord::isNull(const QString& name) const
465 {
466  return isNull(indexOf(name));
467 }
468 
476 {
477  if (!d->contains(index))
478  return;
479  detach();
480  d->fields[index].clear();
481 }
482 
492 void QSqlRecord::setNull(const QString& name)
493 {
494  setNull(indexOf(name));
495 }
496 
497 
504 bool QSqlRecord::isGenerated(const QString& name) const
505 {
506  return isGenerated(indexOf(name));
507 }
508 
520 {
521  return d->fields.value(index).isGenerated();
522 }
523 
524 #ifdef QT3_SUPPORT
525 
533 QString QSqlRecord::toString(const QString& prefix, const QString& sep) const
534 {
535  QString pflist;
536  bool comma = false;
537  for (int i = 0; i < count(); ++i) {
538  if (!d->fields.value(i).isGenerated()) {
539  if (comma)
540  pflist += sep + QLatin1Char(' ');
541  pflist += d->createField(i, prefix);
542  comma = true;
543  }
544  }
545  return pflist;
546 }
547 
556 QStringList QSqlRecord::toStringList(const QString& prefix) const
557 {
558  QStringList s;
559  for (int i = 0; i < count(); ++i) {
560  if (!d->fields.value(i).isGenerated())
561  s += d->createField(i, prefix);
562  }
563  return s;
564 }
565 #endif // QT3_SUPPORT
566 
573 int QSqlRecord::count() const
574 {
575  return d->fields.count();
576 }
577 
585 void QSqlRecord::setValue(int index, const QVariant& val)
586 {
587  if (!d->contains(index))
588  return;
589  detach();
590  d->fields[index].setValue(val);
591 }
592 
593 
604 void QSqlRecord::setValue(const QString& name, const QVariant& val)
605 {
606  setValue(indexOf(name), val);
607 }
608 
609 
613 {
614  qAtomicDetach(d);
615 }
616 
617 #ifndef QT_NO_DEBUG_STREAM
619 {
620  dbg << "QSqlRecord(" << r.count() << ')';
621  for (int i = 0; i < r.count(); ++i)
622  dbg << '\n' << QString::fromLatin1("%1:").arg(i, 2) << r.field(i) << r.value(i).toString();
623  return dbg;
624 }
625 #endif
626 
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
double d
Definition: qnumeric_p.h:62
void qAtomicDetach(T *&d)
This is a helper for the detach method of implicitly shared classes.
Definition: qatomic.h:214
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
The QAtomicInt class provides platform-independent atomic operations on integers. ...
Definition: qatomic.h:55
int count(const T &t) const
Returns the number of occurrences of value in the vector.
Definition: qvector.h:742
QDebug operator<<(QDebug dbg, const QSqlRecord &r)
Definition: qsqlrecord.cpp:618
QString toUpper() const Q_REQUIRED_RESULT
Returns an uppercase copy of the string.
Definition: qstring.cpp:5483
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
bool isEmpty() const
Returns true if there are no fields in the record; otherwise returns false.
Definition: qsqlrecord.cpp:380
bool isNull(int i) const
Returns true if the field index is null or if there is no field at position index; otherwise returns ...
Definition: qsqlrecord.cpp:453
The QSqlRecord class encapsulates a database record.
Definition: qsqlrecord.h:58
The QString class provides a Unicode character string.
Definition: qstring.h:83
QSqlRecord & operator=(const QSqlRecord &other)
Sets the record equal to other.
Definition: qsqlrecord.cpp:156
QSqlRecord()
Constructs an empty record.
Definition: qsqlrecord.cpp:131
QVector< QSqlField > fields
Definition: qsqlrecord.cpp:62
void clearValues()
Clears the value of all fields in the record and sets each field to null.
Definition: qsqlrecord.cpp:403
void detach()
Definition: qsqlrecord.cpp:612
QString name() const
Returns the name of the field.
Definition: qsqlfield.cpp:380
bool isGenerated(int i) const
Returns true if the record has a field at position index and this field is to be generated (the defau...
Definition: qsqlrecord.cpp:519
static QString toString(Register *reg, int type, bool *ok=0)
~QSqlRecord()
Destroys the object and frees any allocated resources.
Definition: qsqlrecord.cpp:166
void insert(int pos, const QSqlField &field)
Inserts the field field at position pos in the record.
Definition: qsqlrecord.cpp:323
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
const char * name
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
QSqlField field(int i) const
Returns the field at position index.
Definition: qsqlrecord.cpp:289
const T & at(int i) const
Returns the item at index position i in the vector.
Definition: qvector.h:350
void remove(int pos)
Removes the field at position pos.
Definition: qsqlrecord.cpp:352
int count() const
Returns the number of fields in the record.
Definition: qsqlrecord.cpp:573
QString arg(qlonglong a, int fieldwidth=0, int base=10, const QChar &fillChar=QLatin1Char(' ')) const Q_REQUIRED_RESULT
Definition: qstring.cpp:7186
bool contains(int index)
Definition: qsqlrecord.cpp:59
QString createField(int index, const QString &prefix) const
Definition: qsqlrecord.cpp:79
static QString fromLatin1(const char *, int size=-1)
Returns a QString initialized with the first size characters of the Latin-1 string str...
Definition: qstring.cpp:4188
QAtomicInt ref
Definition: qsqlrecord.cpp:63
void setNull(int i)
Sets the value of field index to null.
Definition: qsqlrecord.cpp:475
void replace(int pos, const QSqlField &field)
Replaces the field at position pos with the given field.
Definition: qsqlrecord.cpp:336
void append(const QSqlField &field)
Append a copy of field field to the end of the record.
Definition: qsqlrecord.cpp:312
void qAtomicAssign(T *&d, T *x)
This is a helper for the assignment operators of implicitly shared classes.
Definition: qatomic.h:195
bool contains(const QString &name) const
Returns true if there is a field in the record called name; otherwise returns false.
Definition: qsqlrecord.cpp:391
void clear()
Removes all the record&#39;s fields.
Definition: qsqlrecord.cpp:367
quint16 index
bool operator==(const QSqlRecord &other) const
Returns true if this object is identical to other (i.e., has the same fields in the same order); othe...
Definition: qsqlrecord.cpp:190
QSqlRecordPrivate * d
Definition: qsqlrecord.h:112
void setValue(int i, const QVariant &val)
Sets the value of the field at position index to val.
Definition: qsqlrecord.cpp:585
The QSqlField class manipulates the fields in SQL database tables and views.
Definition: qsqlfield.h:56
QString fieldName(int i) const
Returns the name of the field at position index.
Definition: qsqlrecord.cpp:232
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
int indexOf(const QString &name) const
Returns the position of the field called name within the record, or -1 if it cannot be found...
Definition: qsqlrecord.cpp:246
QVariant value(int i) const
Returns the value of the field located at position index in the record.
Definition: qsqlrecord.cpp:203
static bool isNull(const QVariant::Private *d)
Definition: qvariant.cpp:300
void setGenerated(const QString &name, bool generated)
Sets the generated flag for the field called name to generated.
Definition: qsqlrecord.cpp:420