KDb

KDbRecordData.h
1/* This file is part of the KDE project
2 Copyright (C) 2002 Lucijan Busch <lucijan@gmx.at>
3 Copyright (C) 2003 Daniel Molkentin <molkentin@kde.org>
4 Copyright (C) 2003-2016 Jarosław Staniek <staniek@kde.org>
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public License
17 along with this program; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20
21 Original Author: Till Busch <till@bux.at>
22 Original Project: buX (www.bux.at)
23*/
24
25#ifndef KDB_RECORDDATA_H
26#define KDB_RECORDDATA_H
27
28#include <QVariant>
29#include "kdb_export.h"
30
31//! @short Structure for storing single record with type information.
32//! @todo consider forking QVariant to a non-shared Variant class, with type information stored elsewhere.
33//! @todo Variant should have toQVariant() method
34//! @todo look if we can have zero-copy strategy for SQLite and other backends
35//! @todo look if we can use memory allocator for strings, etc.
36class KDB_EXPORT KDbRecordData
37{
38public:
39 /*! Creates a new empty record. */
40 inline KDbRecordData() : m_data(nullptr), m_numCols(0) {}
41
42 /*! Creates a new record data with @a numCols columns.
43 Values are initialized to null. */
44 inline explicit KDbRecordData(int numCols)
45 : m_numCols(numCols)
46 {
47 if (m_numCols > 0) {
48 m_data = static_cast<QVariant **>(malloc(m_numCols * sizeof(QVariant *)));
49 memset(m_data, 0, m_numCols * sizeof(QVariant *));
50 } else {
51 m_data = nullptr;
52 }
53 }
54
55 inline ~KDbRecordData() {
56 if (m_numCols > 0) {
57 for (int i = 0; i < m_numCols; i++)
58 delete m_data[i];
59 free(m_data);
60 }
61 }
62
63 inline bool isEmpty() const { return m_numCols == 0; }
64
65 inline int size() const { return m_numCols; }
66
67 inline int count() const { return m_numCols; }
68
69 /*! @return the value at position @a i.
70 @a i must be a valid index. i.e. 0 <= i < size().
71 @see value(), operator[](). */
72 inline const QVariant& at(int i) const {
73 if (!m_data[i])
74 return s_null;
75 return *m_data[i];
76 }
77
78 /*! @return the value at position @a i as a modifiable reference.
79 @a i must be a valid index, i.e. 0 <= i < size().
80 @see at(), value(). */
81 inline QVariant& operator[](int i) {
82 if (!m_data[i]) {
83 return *(m_data[i] = new QVariant);
84 }
85 return *m_data[i];
86 }
87
88 /*! @return true id value at position @a i is null.
89 @a i must be a valid index, i.e. 0 <= i < size().
90 @see at(), value(). */
91 inline bool isNull(int i) const { return !m_data[i] || m_data[i]->isNull(); }
92
93 /*! Overloaded function.*/
94 inline const QVariant& operator[](int i) const {
95 if (!m_data[i])
96 return s_null;
97 return *m_data[i];
98 }
99
100 /*! @return the value at index position @a i in the vector.
101 If the index @a i is out of bounds, the function returns a default-constructed value.
102 If you are certain that @a i is within bounds, you can use at() or operator[] instead, which is
103 slightly faster. */
104 inline QVariant value(int i) const {
105 if (!m_data || i < 0 || i >= m_numCols || !m_data[i])
106 return QVariant();
107 return *m_data[i];
108 }
109
110 /*! @return the value at index position @a i in the vector.
111 This is an overloaded function.
112 If the index @a i is out of bounds, the function returns a @a defaultValue.
113 If you are certain that i is within bounds, you can use at() instead, which is slightly faster. */
114 inline QVariant value(int i, const QVariant& defaultValue) const {
115 if (!m_data || i < 0 || i >= m_numCols)
116 return defaultValue;
117 if (!m_data[i])
118 return QVariant();
119 return *m_data[i];
120 }
121
122 /*! Sets all column values to null, current number of columns is preserved. */
123 void clearValues();
124
125 /*! Clears all columns, the record is set empty. */
126 void clear();
127
128 /*! Resizes the record to @a numCols.
129 If @a numCols differ from size(), new with record with all values set to null is created.
130 If @a numCols equals size() nothing is performed. */
131 void resize(int numCols);
132
133 //! Converts this record to QList<QVariant>
134 QList<QVariant> toList() const;
135
136private:
137 Q_DISABLE_COPY(KDbRecordData)
138 QVariant **m_data;
139 int m_numCols;
140 static QVariant s_null;
141};
142
143//! Sends information about record data @a data to debug output @a dbg.
144KDB_EXPORT QDebug operator<<(QDebug dbg, const KDbRecordData& data);
145
146#endif
Structure for storing single record with type information.
QVariant value(int i, const QVariant &defaultValue) const
KDbRecordData(int numCols)
const QVariant & at(int i) const
QVariant value(int i) const
bool isNull(int i) const
const QVariant & operator[](int i) const
QVariant & operator[](int i)
KTEXTEDITOR_EXPORT QDebug operator<<(QDebug s, const MovingCursor &cursor)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 17 2024 11:56:02 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.