Qt 6.x
The Qt SDK
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
23};
25
26
67 : d(new QSqlRecordPrivate)
68{
69}
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
173{
174 return value(indexOf(name));
175}
176
185{
186 return d->fields.value(index).name();
187}
188
199{
200 QStringView tableName;
202 const qsizetype idx = name.indexOf(u'.');
203 if (idx != -1) {
204 tableName = fieldName.left(idx);
205 fieldName = fieldName.mid(idx + 1);
206 }
207 const int cnt = count();
208 for (int i = 0; i < cnt; ++i) {
209 // Check the passed in name first in case it is an alias using a dot.
210 // Then check if both the table and field match when there is a table name specified.
211 const auto &currentField = d->fields.at(i);
212 const auto &currentFieldName = currentField.name();
213 if (currentFieldName.compare(name, Qt::CaseInsensitive) == 0
214 || (idx != -1 && currentFieldName.compare(fieldName, Qt::CaseInsensitive) == 0
215 && currentField.tableName().compare(tableName, Qt::CaseInsensitive) == 0)) {
216 return i;
217 }
218 }
219 return -1;
220}
221
228{
229 return d->fields.value(index);
230}
231
236{
237 return field(indexOf(name));
238}
239
240
248{
249 detach();
250 d->fields.append(field);
251}
252
258void QSqlRecord::insert(int pos, const QSqlField& field)
259{
260 detach();
261 d->fields.insert(pos, field);
262}
263
271void QSqlRecord::replace(int pos, const QSqlField& field)
272{
273 if (!d->contains(pos))
274 return;
275
276 detach();
277 d->fields[pos] = field;
278}
279
288{
289 if (!d->contains(pos))
290 return;
291
292 detach();
293 d->fields.remove(pos);
294}
295
303{
304 detach();
305 d->fields.clear();
306}
307
316{
317 return d->fields.isEmpty();
318}
319
320
327{
328 return indexOf(name) >= 0;
329}
330
339{
340 detach();
341 for (QSqlField &f : d->fields)
342 f.clear();
343}
344
354void QSqlRecord::setGenerated(const QString& name, bool generated)
355{
356 setGenerated(indexOf(name), generated);
357}
358
367void QSqlRecord::setGenerated(int index, bool generated)
368{
369 if (!d->contains(index))
370 return;
371 detach();
372 d->fields[index].setGenerated(generated);
373}
374
382{
383 return d->fields.value(index).isNull();
384}
385
393{
394 return isNull(indexOf(name));
395}
396
404{
405 if (!d->contains(index))
406 return;
407 detach();
408 d->fields[index].clear();
409}
410
418{
420}
421
422
430{
431 return isGenerated(indexOf(name));
432}
433
442{
443 return d->fields.value(index).isGenerated();
444}
445
453{
454 return d->fields.size();
455}
456
465{
466 if (!d->contains(index))
467 return;
468 detach();
469 d->fields[index].setValue(val);
470}
471
472
481{
483}
484
485
488void QSqlRecord::detach()
489{
490 d.detach();
491}
492
493#ifndef QT_NO_DEBUG_STREAM
495{
496 QDebugStateSaver saver(dbg);
497 dbg.nospace();
498 const int count = r.count();
499 dbg << "QSqlRecord(" << count << ')';
500 for (int i = 0; i < count; ++i) {
501 dbg.nospace();
502 dbg << '\n' << qSetFieldWidth(2) << Qt::right << i << Qt::left << qSetFieldWidth(0) << ':';
503 dbg.space();
504 dbg << r.field(i) << r.value(i).toString();
505 }
506 return dbg;
507}
508#endif
509
516{
517 QSqlRecord retValues(keyFields);
518
519 for (int i = retValues.count() - 1; i >= 0; --i)
520 retValues.setValue(i, value(retValues.fieldName(i)));
521
522 return retValues;
523}
524
\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...
Definition qlist.h:74
qsizetype size() const noexcept
Definition qlist.h:386
bool isEmpty() const noexcept
Definition qlist.h:390
iterator insert(qsizetype i, parameter_type t)
Definition qlist.h:471
const_reference at(qsizetype i) const noexcept
Definition qlist.h:429
T value(qsizetype i) const
Definition qlist.h:661
void remove(qsizetype i, qsizetype n=1)
Definition qlist.h:787
void append(parameter_type t)
Definition qlist.h:441
void clear()
Definition qlist.h:417
\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 true if the field is generated; otherwise returns false.
QVariant value() const
Returns the value of the field as a QVariant.
Definition qsqlfield.h:37
bool isNull() const
Returns true if the field's value is NULL; otherwise returns false.
QString name() const
Returns the name of the field.
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
This is an overloaded member function, provided for convenience. It differs from the above function o...
QSqlField field(int i) const
Returns the field at position index.
bool contains(const QString &name) const
Returns true if there is a field in the record called name; otherwise returns false.
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
This is an overloaded member function, provided for convenience. It differs from the above function o...
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)
Sets the generated flag for the field called name to generated.
int indexOf(const QString &name) const
Returns the position of the field called name within the record, or -1 if it cannot be found.
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:76
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
QString mid(qsizetype position, qsizetype n=-1) const
Returns a string that contains n characters of this string, starting at the specified position index.
Definition qstring.cpp:5204
QString left(qsizetype n) const
Returns a substring that contains the n leftmost characters of the string.
Definition qstring.cpp:5161
\inmodule QtCore
Definition qvariant.h:64
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:70
QSharedPointer< T > other(t)
[5]