~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/sql/kernel/qsqlrecord.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
 
4
**
 
5
** This file is part of the sql module of the Qt Toolkit.
 
6
**
 
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.
 
10
**
 
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.
 
15
**
 
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.
 
20
**
 
21
** Contact info@trolltech.com if any conditions of this licensing are
 
22
** not clear to you.
 
23
**
 
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.
 
26
**
 
27
****************************************************************************/
 
28
 
 
29
#include "qsqlrecord.h"
 
30
 
 
31
#include "qdebug.h"
 
32
#include "qstringlist.h"
 
33
#include "qatomic.h"
 
34
#include "qsqlfield.h"
 
35
#include "qstring.h"
 
36
#include "qvector.h"
 
37
 
 
38
class QSqlRecordPrivate
 
39
{
 
40
public:
 
41
    QSqlRecordPrivate();
 
42
    QSqlRecordPrivate(const QSqlRecordPrivate &other);
 
43
 
 
44
    inline bool contains(int index) { return index >= 0 && index < fields.count(); }
 
45
    QString createField(int index, const QString &prefix) const;
 
46
 
 
47
    QVector<QSqlField> fields;
 
48
    QAtomic ref;
 
49
};
 
50
 
 
51
QSqlRecordPrivate::QSqlRecordPrivate()
 
52
{
 
53
    ref = 1;
 
54
}
 
55
 
 
56
QSqlRecordPrivate::QSqlRecordPrivate(const QSqlRecordPrivate &other): fields(other.fields)
 
57
{
 
58
    ref = 1;
 
59
}
 
60
 
 
61
/*! \internal
 
62
    Just for compat
 
63
*/
 
64
QString QSqlRecordPrivate::createField(int index, const QString &prefix) const
 
65
{
 
66
    QString f;
 
67
    if (!prefix.isEmpty())
 
68
        f = prefix + QLatin1Char('.');
 
69
    f += fields.at(index).name();
 
70
    return f;
 
71
}
 
72
 
 
73
/*!
 
74
    \class QSqlRecord
 
75
    \brief The QSqlRecord class encapsulates a database record.
 
76
 
 
77
    \ingroup database
 
78
    \module sql
 
79
 
 
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.
 
84
 
 
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.
 
92
 
 
93
    When queries are generated to be executed on the database only
 
94
    those fields for which isGenerated() is true are included in the
 
95
    generated SQL.
 
96
 
 
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().
 
101
 
 
102
    \sa QSqlField, QSqlQuery::record()
 
103
*/
 
104
 
 
105
 
 
106
/*!
 
107
    Constructs an empty record.
 
108
 
 
109
    \sa isEmpty(), append(), insert()
 
110
*/
 
111
 
 
112
QSqlRecord::QSqlRecord()
 
113
{
 
114
    d = new QSqlRecordPrivate();
 
115
}
 
116
 
 
117
/*!
 
118
    Constructs a copy of \a other.
 
119
 
 
120
    QSqlRecord is \l{implicitly shared}. This means you can make copies
 
121
    of a record in \l{constant time}.
 
122
*/
 
123
 
 
124
QSqlRecord::QSqlRecord(const QSqlRecord& other)
 
125
{
 
126
    d = other.d;
 
127
    d->ref.ref();
 
128
}
 
129
 
 
130
/*!
 
131
    Sets the record equal to \a other.
 
132
 
 
133
    QSqlRecord is \l{implicitly shared}. This means you can make copies
 
134
    of a record in \l{constant time}.
 
135
*/
 
136
 
 
137
QSqlRecord& QSqlRecord::operator=(const QSqlRecord& other)
 
138
{
 
139
    qAtomicAssign(d, other.d);
 
140
    return *this;
 
141
}
 
142
 
 
143
/*!
 
144
    Destroys the object and frees any allocated resources.
 
145
*/
 
146
 
 
147
QSqlRecord::~QSqlRecord()
 
148
{
 
149
    if (!d->ref.deref())
 
150
        delete d;
 
151
}
 
152
 
 
153
/*!
 
154
    \fn bool QSqlRecord::operator!=(const QSqlRecord &other) const
 
155
 
 
156
    Returns true if this object is not identical to \a other;
 
157
    otherwise returns false.
 
158
 
 
159
    \sa operator==()
 
160
*/
 
161
 
 
162
/*!
 
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.
 
165
 
 
166
    \sa operator!=()
 
167
*/
 
168
bool QSqlRecord::operator==(const QSqlRecord &other) const
 
169
{
 
170
    return d->fields == other.d->fields;
 
171
}
 
172
 
 
173
/*!
 
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
 
176
    is returned.
 
177
 
 
178
    \sa fieldName() isNull()
 
179
*/
 
180
 
 
181
QVariant QSqlRecord::value(int index) const
 
182
{
 
183
    return d->fields.value(index).value();
 
184
}
 
185
 
 
186
/*!
 
187
    \overload
 
188
 
 
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.
 
191
 
 
192
    \sa indexOf()
 
193
*/
 
194
 
 
195
QVariant QSqlRecord::value(const QString& name) const
 
196
{
 
197
    return value(indexOf(name));
 
198
}
 
199
 
 
200
/*!
 
201
    Returns the name of the field at position \a index. If the field
 
202
    does not exist, an empty string is returned.
 
203
 
 
204
    \sa indexOf()
 
205
*/
 
206
 
 
207
QString QSqlRecord::fieldName(int index) const
 
208
{
 
209
    return d->fields.value(index).name();
 
210
}
 
211
 
 
212
/*!
 
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
 
216
    returned.
 
217
 
 
218
    \sa fieldName()
 
219
*/
 
220
 
 
221
int QSqlRecord::indexOf(const QString& name) const
 
222
{
 
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
 
226
            return i;
 
227
    }
 
228
    return -1;
 
229
}
 
230
 
 
231
#ifdef QT3_SUPPORT
 
232
/*!
 
233
    \obsolete
 
234
    Use field() instead
 
235
*/
 
236
const QSqlField* QSqlRecord::fieldPtr(int index) const
 
237
{
 
238
    if (!d->contains(index))
 
239
        return 0;
 
240
 
 
241
    return &d->fields.at(index);
 
242
}
 
243
 
 
244
/*!
 
245
    \obsolete
 
246
    Use field() instead
 
247
*/
 
248
 
 
249
const QSqlField* QSqlRecord::fieldPtr(const QString& name) const
 
250
{
 
251
    int i = indexOf(name);
 
252
    if (!d->contains(i))
 
253
        return 0;
 
254
 
 
255
    return &d->fields.at(i);
 
256
}
 
257
#endif //QT3_SUPPORT
 
258
 
 
259
/*!
 
260
    Returns the field at position \a index. If the position is out of
 
261
    range, an empty field is returned.
 
262
 */
 
263
QSqlField QSqlRecord::field(int index) const
 
264
{
 
265
    return d->fields.value(index);
 
266
}
 
267
 
 
268
/*! \overload
 
269
    Returns the field called \a name.
 
270
 */
 
271
QSqlField QSqlRecord::field(const QString &name) const
 
272
{
 
273
    return field(indexOf(name));
 
274
}
 
275
 
 
276
 
 
277
/*!
 
278
    Append a copy of field \a field to the end of the record.
 
279
 
 
280
    \sa insert() replace() remove()
 
281
*/
 
282
 
 
283
void QSqlRecord::append(const QSqlField& field)
 
284
{
 
285
    detach();
 
286
    d->fields.append(field);
 
287
}
 
288
 
 
289
/*!
 
290
    Inserts the field \a field at position \a pos in the record.
 
291
 
 
292
    \sa append() replace() remove()
 
293
 */
 
294
void QSqlRecord::insert(int pos, const QSqlField& field)
 
295
{
 
296
   detach();
 
297
   d->fields.insert(pos, field);
 
298
}
 
299
 
 
300
/*!
 
301
    Replaces the field at position \a pos with the given \a field. If
 
302
    \a pos is out of range, nothing happens.
 
303
 
 
304
    \sa append() insert() remove()
 
305
*/
 
306
 
 
307
void QSqlRecord::replace(int pos, const QSqlField& field)
 
308
{
 
309
    if (!d->contains(pos))
 
310
        return;
 
311
 
 
312
    detach();
 
313
    d->fields[pos] = field;
 
314
}
 
315
 
 
316
/*!
 
317
    Removes the field at position \a pos. If \a pos is out of range,
 
318
    nothing happens.
 
319
 
 
320
    \sa append() insert() replace()
 
321
*/
 
322
 
 
323
void QSqlRecord::remove(int pos)
 
324
{
 
325
    if (!d->contains(pos))
 
326
        return;
 
327
 
 
328
    detach();
 
329
    d->fields.remove(pos);
 
330
}
 
331
 
 
332
/*!
 
333
    Removes all the record's fields.
 
334
 
 
335
    \sa clearValues() isEmpty()
 
336
*/
 
337
 
 
338
void QSqlRecord::clear()
 
339
{
 
340
    detach();
 
341
    d->fields.clear();
 
342
}
 
343
 
 
344
/*!
 
345
    Returns true if there are no fields in the record; otherwise
 
346
    returns false.
 
347
 
 
348
    \sa append() insert() clear()
 
349
*/
 
350
 
 
351
bool QSqlRecord::isEmpty() const
 
352
{
 
353
    return d->fields.isEmpty();
 
354
}
 
355
 
 
356
 
 
357
/*!
 
358
    Returns true if there is a field in the record called \a name;
 
359
    otherwise returns false.
 
360
*/
 
361
 
 
362
bool QSqlRecord::contains(const QString& name) const
 
363
{
 
364
    return indexOf(name) >= 0;
 
365
}
 
366
 
 
367
/*!
 
368
    Clears the value of all fields in the record and sets each field
 
369
    to null.
 
370
 
 
371
    \sa setValue()
 
372
*/
 
373
 
 
374
void QSqlRecord::clearValues()
 
375
{
 
376
    detach();
 
377
    int count = d->fields.count();
 
378
    for (int i = 0; i < count; ++i)
 
379
        d->fields[i].clear();
 
380
}
 
381
 
 
382
/*!
 
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.
 
387
 
 
388
    \sa isGenerated()
 
389
*/
 
390
 
 
391
void QSqlRecord::setGenerated(const QString& name, bool generated)
 
392
{
 
393
    setGenerated(indexOf(name), generated);
 
394
}
 
395
 
 
396
/*!
 
397
    \overload
 
398
 
 
399
    Sets the generated flag for the field \a index to \a generated.
 
400
 
 
401
    \sa isGenerated()
 
402
*/
 
403
 
 
404
void QSqlRecord::setGenerated(int index, bool generated)
 
405
{
 
406
    if (!d->contains(index))
 
407
        return;
 
408
    detach();
 
409
    d->fields[index].setGenerated(generated);
 
410
}
 
411
 
 
412
/*!
 
413
    \overload
 
414
 
 
415
    Returns true if the field \a index is null or if there is no field at
 
416
    position \a index; otherwise returns false.
 
417
*/
 
418
bool QSqlRecord::isNull(int index) const
 
419
{
 
420
    return d->fields.value(index).isNull();
 
421
}
 
422
 
 
423
/*!
 
424
    Returns true if the field called \a name is null or if there is no
 
425
    field called \a name; otherwise returns false.
 
426
 
 
427
    \sa setNull()
 
428
*/
 
429
bool QSqlRecord::isNull(const QString& name) const
 
430
{
 
431
    return isNull(indexOf(name));
 
432
}
 
433
 
 
434
/*!
 
435
    Sets the value of field \a index to null. If the field does not exist,
 
436
    nothing happens.
 
437
 
 
438
    \sa setValue()
 
439
*/
 
440
void QSqlRecord::setNull(int index)
 
441
{
 
442
    if (!d->contains(index))
 
443
        return;
 
444
    detach();
 
445
    d->fields[index].clear();
 
446
}
 
447
 
 
448
/*!
 
449
    \overload
 
450
 
 
451
    Sets the value of the field called \a name to null. If the field
 
452
    does not exist, nothing happens.
 
453
*/
 
454
void QSqlRecord::setNull(const QString& name)
 
455
{
 
456
    setNull(indexOf(name));
 
457
}
 
458
 
 
459
 
 
460
/*!
 
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.
 
463
 
 
464
    \sa setGenerated()
 
465
*/
 
466
bool QSqlRecord::isGenerated(const QString& name) const
 
467
{
 
468
    return isGenerated(indexOf(name));
 
469
}
 
470
 
 
471
/*! \overload
 
472
 
 
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.
 
475
 
 
476
    \sa setGenerated()
 
477
*/
 
478
bool QSqlRecord::isGenerated(int index) const
 
479
{
 
480
    return d->fields.value(index).isGenerated();
 
481
}
 
482
 
 
483
#ifdef QT3_SUPPORT
 
484
/*!
 
485
    Returns a list of all the record's field names as a string
 
486
    separated by \a sep.
 
487
 
 
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.
 
490
*/
 
491
 
 
492
QString QSqlRecord::toString(const QString& prefix, const QString& sep) const
 
493
{
 
494
    QString pflist;
 
495
    bool comma = false;
 
496
    for (int i = 0; i < count(); ++i) {
 
497
        if (!d->fields.value(i).isGenerated()) {
 
498
            if (comma)
 
499
                pflist += sep + QLatin1Char(' ');
 
500
            pflist += d->createField(i, prefix);
 
501
            comma = true;
 
502
        }
 
503
    }
 
504
    return pflist;
 
505
}
 
506
 
 
507
/*!
 
508
    Returns a list of all the record's field names, each having the
 
509
    prefix \a prefix.
 
510
 
 
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.
 
513
*/
 
514
 
 
515
QStringList QSqlRecord::toStringList(const QString& prefix) const
 
516
{
 
517
    QStringList s;
 
518
    for (int i = 0; i < count(); ++i) {
 
519
        if (!d->fields.value(i).isGenerated())
 
520
            s += d->createField(i, prefix);
 
521
    }
 
522
    return s;
 
523
}
 
524
#endif // QT3_SUPPORT
 
525
 
 
526
/*!
 
527
    Returns the number of fields in the record.
 
528
 
 
529
    \sa isEmpty()
 
530
*/
 
531
 
 
532
int QSqlRecord::count() const
 
533
{
 
534
    return d->fields.count();
 
535
}
 
536
 
 
537
/*!
 
538
    Sets the value of the field at position \a index to \a val. If the
 
539
    field does not exist, nothing happens.
 
540
 
 
541
    \sa setNull()
 
542
*/
 
543
 
 
544
void QSqlRecord::setValue(int index, const QVariant& val)
 
545
{
 
546
    if (!d->contains(index))
 
547
        return;
 
548
    detach();
 
549
    d->fields[index].setValue(val);
 
550
}
 
551
 
 
552
 
 
553
/*!
 
554
    \overload
 
555
 
 
556
    Sets the value of the field called \a name to \a val. If the field
 
557
    does not exist, nothing happens.
 
558
*/
 
559
 
 
560
void QSqlRecord::setValue(const QString& name, const QVariant& val)
 
561
{
 
562
    setValue(indexOf(name), val);
 
563
}
 
564
 
 
565
 
 
566
/*! \internal
 
567
*/
 
568
void QSqlRecord::detach()
 
569
{
 
570
    qAtomicDetach(d);
 
571
}
 
572
 
 
573
#ifndef QT_NO_DEBUG_STREAM
 
574
QDebug operator<<(QDebug dbg, const QSqlRecord &r)
 
575
{
 
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)
 
579
                      << r.field(i);
 
580
    return dbg.space();
 
581
}
 
582
#endif
 
583
 
 
584
/*!
 
585
    \fn int QSqlRecord::position(const QString& name) const
 
586
 
 
587
    Use indexOf() instead.
 
588
*/