Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qsqlrecord.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qsqlrecord.h"
5
6#include "qatomic.h"
7#include "qdebug.h"
8#include "qlist.h"
9#include "qsqlfield.h"
10#include "qstring.h"
11
13
15{
16public:
17 inline bool contains(qsizetype index) const
18 {
19 return index >= 0 && index < fields.size();
20 }
21
22 QList<QSqlField> fields;
23};
25
26
70
79 = default;
80
120 = default;
121
127 = default;
128
129
146{
147 return d->fields == other.d->fields;
148}
149
159{
160 return d->fields.value(index).value();
161}
162
167{
168 return value(QStringView(name));
169}
170
183
192{
193 return d->fields.value(index).name();
194}
195
200{
201 return indexOf(QStringView(name));
202}
203
213{
214 QStringView tableName;
216 const qsizetype idx = name.indexOf(u'.');
217 if (idx != -1) {
218 tableName = fieldName.left(idx);
219 fieldName = fieldName.mid(idx + 1);
220 }
221 const int cnt = count();
222 for (int i = 0; i < cnt; ++i) {
223 // Check the passed in name first in case it is an alias using a dot.
224 // Then check if both the table and field match when there is a table name specified.
225 const auto &currentField = d->fields.at(i);
226 const auto &currentFieldName = currentField.name();
227 if (currentFieldName.compare(name, Qt::CaseInsensitive) == 0
228 || (idx != -1 && currentFieldName.compare(fieldName, Qt::CaseInsensitive) == 0
229 && currentField.tableName().compare(tableName, Qt::CaseInsensitive) == 0)) {
230 return i;
231 }
232 }
233 return -1;
234}
235
242{
243 return d->fields.value(index);
244}
245
250{
251 return field(QStringView(name));
252}
253
265
266
274{
275 detach();
276 d->fields.append(field);
277}
278
284void QSqlRecord::insert(int pos, const QSqlField &field)
285{
286 detach();
287 d->fields.insert(pos, field);
288}
289
297void QSqlRecord::replace(int pos, const QSqlField &field)
298{
299 if (!d->contains(pos))
300 return;
301
302 detach();
303 d->fields[pos] = field;
304}
305
314{
315 if (!d->contains(pos))
316 return;
317
318 detach();
319 d->fields.remove(pos);
320}
321
329{
330 detach();
331 d->fields.clear();
332}
333
342{
343 return d->fields.isEmpty();
344}
345
346
351{
352 return contains(QStringView(name));
353}
354
360{
361 return indexOf(name) >= 0;
362}
363
372{
373 detach();
374 for (QSqlField &f : d->fields)
375 f.clear();
376}
377
381void QSqlRecord::setGenerated(const QString &name, bool generated)
382{
383 setGenerated(QStringView(name), generated);
384}
394{
395 setGenerated(indexOf(name), generated);
396}
397
404void QSqlRecord::setGenerated(int index, bool generated)
405{
406 if (!d->contains(index))
407 return;
408 detach();
409 d->fields[index].setGenerated(generated);
410}
411
419{
420 return d->fields.value(index).isNull();
421}
422
427{
428 return isNull(QStringView(name));
429}
430
440{
441 return isNull(indexOf(name));
442}
443
451{
452 if (!d->contains(index))
453 return;
454 detach();
455 d->fields[index].clear();
456}
457
465
476
481{
483}
484
497
505{
506 return d->fields.value(index).isGenerated();
507}
508
516{
517 return d->fields.size();
518}
519
528{
529 if (!d->contains(index))
530 return;
531 detach();
532 d->fields[index].setValue(val);
533}
534
539{
541}
554
555
558void QSqlRecord::detach()
559{
560 d.detach();
561}
562
563#ifndef QT_NO_DEBUG_STREAM
565{
566 QDebugStateSaver saver(dbg);
567 dbg.nospace();
568 const int count = r.count();
569 dbg << "QSqlRecord(" << count << ')';
570 for (int i = 0; i < count; ++i) {
571 dbg.nospace();
572 dbg << '\n' << qSetFieldWidth(2) << Qt::right << i << Qt::left << qSetFieldWidth(0) << ':';
573 dbg.space();
574 dbg << r.field(i) << r.value(i).toString();
575 }
576 return dbg;
577}
578#endif
579
586{
587 QSqlRecord retValues(keyFields);
588
589 for (int i = retValues.count() - 1; i >= 0; --i)
590 retValues.setValue(i, value(retValues.fieldName(i)));
591
592 return retValues;
593}
594
\inmodule QtCore
\inmodule QtCore
void detach()
If the shared data object's reference count is greater than 1, this function creates a deep copy of t...
qsizetype size() const noexcept
Definition qlist.h:397
bool isEmpty() const noexcept
Definition qlist.h:401
iterator insert(qsizetype i, parameter_type t)
Definition qlist.h:488
const_reference at(qsizetype i) const noexcept
Definition qlist.h:446
T value(qsizetype i) const
Definition qlist.h:664
void remove(qsizetype i, qsizetype n=1)
Definition qlist.h:794
void append(parameter_type t)
Definition qlist.h:458
void clear()
Definition qlist.h:434
\inmodule QtCore
Definition qshareddata.h:19
The QSqlField class manipulates the fields in SQL database tables and views.
Definition qsqlfield.h:19
bool isGenerated() const
Returns the value of \l generated.
QString name
This property holds the name of the field.
Definition qsqlfield.h:26
bool isNull() const
Returns true if the field's value is NULL; otherwise returns false.
QVariant value
Definition qsqlfield.h:24
bool contains(qsizetype index) const
QList< QSqlField > fields
The QSqlRecord class encapsulates a database record.
Definition qsqlrecord.h:20
void clear()
Removes all the record's fields.
QVariant value(int i) const
Returns the value of the field located at position index in the record.
bool isNull(int i) const
Returns true if the field index is null or if there is no field at position index; otherwise returns ...
QSqlField field(int i) const
Returns the field at position index.
bool contains(const QString &name) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool isEmpty() const
Returns true if there are no fields in the record; otherwise returns false.
int count() const
Returns the number of fields in the record.
void remove(int pos)
Removes the field at position pos.
void replace(int pos, const QSqlField &field)
Replaces the field at position pos with the given field.
void append(const QSqlField &field)
Append a copy of field field to the end of the record.
~QSqlRecord()
Destroys the object and frees any allocated resources.
QSqlRecord keyValues(const QSqlRecord &keyFields) const
void insert(int pos, const QSqlField &field)
Inserts the field field at position pos in the record.
void setValue(int i, const QVariant &val)
Sets the value of the field at position index to val.
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...
void setNull(int i)
Sets the value of field index to null.
QSqlRecord()
Constructs an empty record.
QString fieldName(int i) const
Returns the name of the field at position index.
QSqlRecord & operator=(const QSqlRecord &other)
Sets the record equal to other.
void setGenerated(const QString &name, bool generated)
This is an overloaded member function, provided for convenience. It differs from the above function o...
int indexOf(const QString &name) const
This is an overloaded member function, provided for convenience. It differs from the above function o...
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...
void clearValues()
Clears the value of all fields in the record and sets each field to null.
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
QString left(qsizetype n) const &
Definition qstring.h:363
QString mid(qsizetype position, qsizetype n=-1) const &
Definition qstring.cpp:5300
\inmodule QtCore
Definition qvariant.h:65
Combined button and popup list for selecting options.
QTextStream & right(QTextStream &stream)
Calls QTextStream::setFieldAlignment(QTextStream::AlignRight) on stream and returns stream.
@ CaseInsensitive
QTextStream & left(QTextStream &stream)
Calls QTextStream::setFieldAlignment(QTextStream::AlignLeft) on stream and returns stream.
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
GLuint index
[2]
GLboolean r
[2]
GLenum GLenum GLsizei count
GLfloat GLfloat f
GLuint name
GLuint GLfloat * val
#define QT_DEFINE_QESDP_SPECIALIZATION_DTOR(Class)
QDebug operator<<(QDebug dbg, const QSqlRecord &r)
QTextStreamManipulator qSetFieldWidth(int width)
ptrdiff_t qsizetype
Definition qtypes.h:165
QSharedPointer< T > other(t)
[5]