Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/sql/models/qsqlquerymodel.cpp |
Source code | Switch to Preprocessed file |
Line | Source | Count | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
1 | /**************************************************************************** | - | ||||||||||||||||||||||||
2 | ** | - | ||||||||||||||||||||||||
3 | ** Copyright (C) 2015 The Qt Company Ltd. | - | ||||||||||||||||||||||||
4 | ** Contact: http://www.qt.io/licensing/ | - | ||||||||||||||||||||||||
5 | ** | - | ||||||||||||||||||||||||
6 | ** This file is part of the QtSql module of the Qt Toolkit. | - | ||||||||||||||||||||||||
7 | ** | - | ||||||||||||||||||||||||
8 | ** $QT_BEGIN_LICENSE:LGPL21$ | - | ||||||||||||||||||||||||
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 The Qt Company. For licensing terms | - | ||||||||||||||||||||||||
14 | ** and conditions see http://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||||||||
15 | ** information use the contact form at http://www.qt.io/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 or version 3 as published by the Free | - | ||||||||||||||||||||||||
20 | ** Software Foundation and appearing in the file LICENSE.LGPLv21 and | - | ||||||||||||||||||||||||
21 | ** LICENSE.LGPLv3 included in the packaging of this file. Please review the | - | ||||||||||||||||||||||||
22 | ** following information to ensure the GNU Lesser General Public License | - | ||||||||||||||||||||||||
23 | ** requirements will be met: https://www.gnu.org/licenses/lgpl.html and | - | ||||||||||||||||||||||||
24 | ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - | ||||||||||||||||||||||||
25 | ** | - | ||||||||||||||||||||||||
26 | ** As a special exception, The Qt Company gives you certain additional | - | ||||||||||||||||||||||||
27 | ** rights. These rights are described in The Qt Company LGPL Exception | - | ||||||||||||||||||||||||
28 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - | ||||||||||||||||||||||||
29 | ** | - | ||||||||||||||||||||||||
30 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||||||||
31 | ** | - | ||||||||||||||||||||||||
32 | ****************************************************************************/ | - | ||||||||||||||||||||||||
33 | - | |||||||||||||||||||||||||
34 | #include "qsqlquerymodel.h" | - | ||||||||||||||||||||||||
35 | #include "qsqlquerymodel_p.h" | - | ||||||||||||||||||||||||
36 | - | |||||||||||||||||||||||||
37 | #include <qdebug.h> | - | ||||||||||||||||||||||||
38 | #include <qsqldriver.h> | - | ||||||||||||||||||||||||
39 | #include <qsqlfield.h> | - | ||||||||||||||||||||||||
40 | - | |||||||||||||||||||||||||
41 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||||||||
42 | - | |||||||||||||||||||||||||
43 | #define QSQL_PREFETCH 255 | - | ||||||||||||||||||||||||
44 | - | |||||||||||||||||||||||||
45 | void QSqlQueryModelPrivate::prefetch(int limit) | - | ||||||||||||||||||||||||
46 | { | - | ||||||||||||||||||||||||
47 | Q_Q(QSqlQueryModel); | - | ||||||||||||||||||||||||
48 | - | |||||||||||||||||||||||||
49 | if (atEnd || limit <= bottom.row() || bottom.column() == -1)
| 0-388 | ||||||||||||||||||||||||
50 | return; executed 3 times by 2 tests: return; Executed by:
| 3 | ||||||||||||||||||||||||
51 | - | |||||||||||||||||||||||||
52 | QModelIndex newBottom; | - | ||||||||||||||||||||||||
53 | const int oldBottomRow = qMax(bottom.row(), 0); | - | ||||||||||||||||||||||||
54 | - | |||||||||||||||||||||||||
55 | // try to seek directly | - | ||||||||||||||||||||||||
56 | if (query.seek(limit)) {
| 130-257 | ||||||||||||||||||||||||
57 | newBottom = q->createIndex(limit, bottom.column()); | - | ||||||||||||||||||||||||
58 | } else { executed 130 times by 3 tests: end of block Executed by:
| 130 | ||||||||||||||||||||||||
59 | // have to seek back to our old position for MS Access | - | ||||||||||||||||||||||||
60 | int i = oldBottomRow; | - | ||||||||||||||||||||||||
61 | if (query.seek(i)) {
| 11-246 | ||||||||||||||||||||||||
62 | while (query.next())
| 246-526 | ||||||||||||||||||||||||
63 | ++i; executed 526 times by 4 tests: ++i; Executed by:
| 526 | ||||||||||||||||||||||||
64 | newBottom = q->createIndex(i, bottom.column()); | - | ||||||||||||||||||||||||
65 | } else { executed 246 times by 4 tests: end of block Executed by:
| 246 | ||||||||||||||||||||||||
66 | // empty or invalid query | - | ||||||||||||||||||||||||
67 | newBottom = q->createIndex(-1, bottom.column()); | - | ||||||||||||||||||||||||
68 | } executed 11 times by 2 tests: end of block Executed by:
| 11 | ||||||||||||||||||||||||
69 | atEnd = true; // this is the end. | - | ||||||||||||||||||||||||
70 | } executed 257 times by 4 tests: end of block Executed by:
| 257 | ||||||||||||||||||||||||
71 | if (newBottom.row() >= 0 && newBottom.row() > bottom.row()) {
| 0-376 | ||||||||||||||||||||||||
72 | q->beginInsertRows(QModelIndex(), bottom.row() + 1, newBottom.row()); | - | ||||||||||||||||||||||||
73 | bottom = newBottom; | - | ||||||||||||||||||||||||
74 | q->endInsertRows(); | - | ||||||||||||||||||||||||
75 | } else { executed 376 times by 4 tests: end of block Executed by:
| 376 | ||||||||||||||||||||||||
76 | bottom = newBottom; | - | ||||||||||||||||||||||||
77 | } executed 11 times by 2 tests: end of block Executed by:
| 11 | ||||||||||||||||||||||||
78 | } | - | ||||||||||||||||||||||||
79 | - | |||||||||||||||||||||||||
80 | QSqlQueryModelPrivate::~QSqlQueryModelPrivate() | - | ||||||||||||||||||||||||
81 | { | - | ||||||||||||||||||||||||
82 | } | - | ||||||||||||||||||||||||
83 | - | |||||||||||||||||||||||||
84 | void QSqlQueryModelPrivate::initColOffsets(int size) | - | ||||||||||||||||||||||||
85 | { | - | ||||||||||||||||||||||||
86 | colOffsets.resize(size); | - | ||||||||||||||||||||||||
87 | memset(colOffsets.data(), 0, colOffsets.size() * sizeof(int)); | - | ||||||||||||||||||||||||
88 | } executed 438 times by 4 tests: end of block Executed by:
| 438 | ||||||||||||||||||||||||
89 | - | |||||||||||||||||||||||||
90 | int QSqlQueryModelPrivate::columnInQuery(int modelColumn) const | - | ||||||||||||||||||||||||
91 | { | - | ||||||||||||||||||||||||
92 | if (modelColumn < 0 || modelColumn >= rec.count() || !rec.isGenerated(modelColumn) || modelColumn >= colOffsets.size())
| 0-3254 | ||||||||||||||||||||||||
93 | return -1; executed 27 times by 1 test: return -1; Executed by:
| 27 | ||||||||||||||||||||||||
94 | return modelColumn - colOffsets[modelColumn]; executed 3242 times by 4 tests: return modelColumn - colOffsets[modelColumn]; Executed by:
| 3242 | ||||||||||||||||||||||||
95 | } | - | ||||||||||||||||||||||||
96 | - | |||||||||||||||||||||||||
97 | /*! | - | ||||||||||||||||||||||||
98 | \class QSqlQueryModel | - | ||||||||||||||||||||||||
99 | \brief The QSqlQueryModel class provides a read-only data model for SQL | - | ||||||||||||||||||||||||
100 | result sets. | - | ||||||||||||||||||||||||
101 | - | |||||||||||||||||||||||||
102 | \ingroup database | - | ||||||||||||||||||||||||
103 | \inmodule QtSql | - | ||||||||||||||||||||||||
104 | - | |||||||||||||||||||||||||
105 | QSqlQueryModel is a high-level interface for executing SQL | - | ||||||||||||||||||||||||
106 | statements and traversing the result set. It is built on top of | - | ||||||||||||||||||||||||
107 | the lower-level QSqlQuery and can be used to provide data to | - | ||||||||||||||||||||||||
108 | view classes such as QTableView. For example: | - | ||||||||||||||||||||||||
109 | - | |||||||||||||||||||||||||
110 | \snippet sqldatabase/sqldatabase.cpp 16 | - | ||||||||||||||||||||||||
111 | - | |||||||||||||||||||||||||
112 | We set the model's query, then we set up the labels displayed in | - | ||||||||||||||||||||||||
113 | the view header. | - | ||||||||||||||||||||||||
114 | - | |||||||||||||||||||||||||
115 | QSqlQueryModel can also be used to access a database | - | ||||||||||||||||||||||||
116 | programmatically, without binding it to a view: | - | ||||||||||||||||||||||||
117 | - | |||||||||||||||||||||||||
118 | \snippet sqldatabase/sqldatabase.cpp 21 | - | ||||||||||||||||||||||||
119 | - | |||||||||||||||||||||||||
120 | The code snippet above extracts the \c salary field from record 4 in | - | ||||||||||||||||||||||||
121 | the result set of the query \c{SELECT * from employee}. Assuming | - | ||||||||||||||||||||||||
122 | that \c salary is column 2, we can rewrite the last line as follows: | - | ||||||||||||||||||||||||
123 | - | |||||||||||||||||||||||||
124 | \snippet sqldatabase/sqldatabase.cpp 22 | - | ||||||||||||||||||||||||
125 | - | |||||||||||||||||||||||||
126 | The model is read-only by default. To make it read-write, you | - | ||||||||||||||||||||||||
127 | must subclass it and reimplement setData() and flags(). Another | - | ||||||||||||||||||||||||
128 | option is to use QSqlTableModel, which provides a read-write | - | ||||||||||||||||||||||||
129 | model based on a single database table. | - | ||||||||||||||||||||||||
130 | - | |||||||||||||||||||||||||
131 | The \l{querymodel} example illustrates how to use | - | ||||||||||||||||||||||||
132 | QSqlQueryModel to display the result of a query. It also shows | - | ||||||||||||||||||||||||
133 | how to subclass QSqlQueryModel to customize the contents of the | - | ||||||||||||||||||||||||
134 | data before showing it to the user, and how to create a | - | ||||||||||||||||||||||||
135 | read-write model based on QSqlQueryModel. | - | ||||||||||||||||||||||||
136 | - | |||||||||||||||||||||||||
137 | If the database doesn't return the number of selected rows in | - | ||||||||||||||||||||||||
138 | a query, the model will fetch rows incrementally. | - | ||||||||||||||||||||||||
139 | See fetchMore() for more information. | - | ||||||||||||||||||||||||
140 | - | |||||||||||||||||||||||||
141 | \sa QSqlTableModel, QSqlRelationalTableModel, QSqlQuery, | - | ||||||||||||||||||||||||
142 | {Model/View Programming}, {Query Model Example} | - | ||||||||||||||||||||||||
143 | */ | - | ||||||||||||||||||||||||
144 | - | |||||||||||||||||||||||||
145 | /*! | - | ||||||||||||||||||||||||
146 | Creates an empty QSqlQueryModel with the given \a parent. | - | ||||||||||||||||||||||||
147 | */ | - | ||||||||||||||||||||||||
148 | QSqlQueryModel::QSqlQueryModel(QObject *parent) | - | ||||||||||||||||||||||||
149 | : QAbstractTableModel(*new QSqlQueryModelPrivate, parent) | - | ||||||||||||||||||||||||
150 | { | - | ||||||||||||||||||||||||
151 | } executed 113 times by 2 tests: end of block Executed by:
| 113 | ||||||||||||||||||||||||
152 | - | |||||||||||||||||||||||||
153 | /*! \internal | - | ||||||||||||||||||||||||
154 | */ | - | ||||||||||||||||||||||||
155 | QSqlQueryModel::QSqlQueryModel(QSqlQueryModelPrivate &dd, QObject *parent) | - | ||||||||||||||||||||||||
156 | : QAbstractTableModel(dd, parent) | - | ||||||||||||||||||||||||
157 | { | - | ||||||||||||||||||||||||
158 | } executed 174 times by 3 tests: end of block Executed by:
| 174 | ||||||||||||||||||||||||
159 | - | |||||||||||||||||||||||||
160 | /*! | - | ||||||||||||||||||||||||
161 | Destroys the object and frees any allocated resources. | - | ||||||||||||||||||||||||
162 | - | |||||||||||||||||||||||||
163 | \sa clear() | - | ||||||||||||||||||||||||
164 | */ | - | ||||||||||||||||||||||||
165 | QSqlQueryModel::~QSqlQueryModel() | - | ||||||||||||||||||||||||
166 | { | - | ||||||||||||||||||||||||
167 | } | - | ||||||||||||||||||||||||
168 | - | |||||||||||||||||||||||||
169 | /*! | - | ||||||||||||||||||||||||
170 | \since 4.1 | - | ||||||||||||||||||||||||
171 | - | |||||||||||||||||||||||||
172 | Fetches more rows from a database. | - | ||||||||||||||||||||||||
173 | This only affects databases that don't report back the size of a query | - | ||||||||||||||||||||||||
174 | (see QSqlDriver::hasFeature()). | - | ||||||||||||||||||||||||
175 | - | |||||||||||||||||||||||||
176 | To force fetching of the entire result set, you can use the following: | - | ||||||||||||||||||||||||
177 | - | |||||||||||||||||||||||||
178 | \snippet code/src_sql_models_qsqlquerymodel.cpp 0 | - | ||||||||||||||||||||||||
179 | - | |||||||||||||||||||||||||
180 | \a parent should always be an invalid QModelIndex. | - | ||||||||||||||||||||||||
181 | - | |||||||||||||||||||||||||
182 | \sa canFetchMore() | - | ||||||||||||||||||||||||
183 | */ | - | ||||||||||||||||||||||||
184 | void QSqlQueryModel::fetchMore(const QModelIndex &parent) | - | ||||||||||||||||||||||||
185 | { | - | ||||||||||||||||||||||||
186 | Q_D(QSqlQueryModel); | - | ||||||||||||||||||||||||
187 | if (parent.isValid())
| 0-390 | ||||||||||||||||||||||||
188 | return; never executed: return; | 0 | ||||||||||||||||||||||||
189 | d->prefetch(qMax(d->bottom.row(), 0) + QSQL_PREFETCH); | - | ||||||||||||||||||||||||
190 | } executed 390 times by 4 tests: end of block Executed by:
| 390 | ||||||||||||||||||||||||
191 | - | |||||||||||||||||||||||||
192 | /*! | - | ||||||||||||||||||||||||
193 | \since 4.1 | - | ||||||||||||||||||||||||
194 | - | |||||||||||||||||||||||||
195 | Returns \c true if it is possible to read more rows from the database. | - | ||||||||||||||||||||||||
196 | This only affects databases that don't report back the size of a query | - | ||||||||||||||||||||||||
197 | (see QSqlDriver::hasFeature()). | - | ||||||||||||||||||||||||
198 | - | |||||||||||||||||||||||||
199 | \a parent should always be an invalid QModelIndex. | - | ||||||||||||||||||||||||
200 | - | |||||||||||||||||||||||||
201 | \sa fetchMore() | - | ||||||||||||||||||||||||
202 | */ | - | ||||||||||||||||||||||||
203 | bool QSqlQueryModel::canFetchMore(const QModelIndex &parent) const | - | ||||||||||||||||||||||||
204 | { | - | ||||||||||||||||||||||||
205 | Q_D(const QSqlQueryModel); | - | ||||||||||||||||||||||||
206 | return (!parent.isValid() && !d->atEnd); executed 1029 times by 2 tests: return (!parent.isValid() && !d->atEnd); Executed by:
| 0-1029 | ||||||||||||||||||||||||
207 | } | - | ||||||||||||||||||||||||
208 | - | |||||||||||||||||||||||||
209 | /*! \internal | - | ||||||||||||||||||||||||
210 | */ | - | ||||||||||||||||||||||||
211 | void QSqlQueryModel::beginInsertRows(const QModelIndex &parent, int first, int last) | - | ||||||||||||||||||||||||
212 | { | - | ||||||||||||||||||||||||
213 | Q_D(QSqlQueryModel); | - | ||||||||||||||||||||||||
214 | if (!d->nestedResetLevel)
| 85-364 | ||||||||||||||||||||||||
215 | QAbstractTableModel::beginInsertRows(parent, first, last); executed 85 times by 4 tests: QAbstractTableModel::beginInsertRows(parent, first, last); Executed by:
| 85 | ||||||||||||||||||||||||
216 | } executed 449 times by 4 tests: end of block Executed by:
| 449 | ||||||||||||||||||||||||
217 | - | |||||||||||||||||||||||||
218 | /*! \internal | - | ||||||||||||||||||||||||
219 | */ | - | ||||||||||||||||||||||||
220 | void QSqlQueryModel::endInsertRows() | - | ||||||||||||||||||||||||
221 | { | - | ||||||||||||||||||||||||
222 | Q_D(QSqlQueryModel); | - | ||||||||||||||||||||||||
223 | if (!d->nestedResetLevel)
| 85-364 | ||||||||||||||||||||||||
224 | QAbstractTableModel::endInsertRows(); executed 85 times by 4 tests: QAbstractTableModel::endInsertRows(); Executed by:
| 85 | ||||||||||||||||||||||||
225 | } executed 449 times by 4 tests: end of block Executed by:
| 449 | ||||||||||||||||||||||||
226 | - | |||||||||||||||||||||||||
227 | /*! \internal | - | ||||||||||||||||||||||||
228 | */ | - | ||||||||||||||||||||||||
229 | void QSqlQueryModel::beginRemoveRows(const QModelIndex &parent, int first, int last) | - | ||||||||||||||||||||||||
230 | { | - | ||||||||||||||||||||||||
231 | Q_D(QSqlQueryModel); | - | ||||||||||||||||||||||||
232 | if (!d->nestedResetLevel)
| 0-32 | ||||||||||||||||||||||||
233 | QAbstractTableModel::beginRemoveRows(parent, first, last); executed 32 times by 2 tests: QAbstractTableModel::beginRemoveRows(parent, first, last); Executed by:
| 32 | ||||||||||||||||||||||||
234 | } executed 32 times by 2 tests: end of block Executed by:
| 32 | ||||||||||||||||||||||||
235 | - | |||||||||||||||||||||||||
236 | /*! \internal | - | ||||||||||||||||||||||||
237 | */ | - | ||||||||||||||||||||||||
238 | void QSqlQueryModel::endRemoveRows() | - | ||||||||||||||||||||||||
239 | { | - | ||||||||||||||||||||||||
240 | Q_D(QSqlQueryModel); | - | ||||||||||||||||||||||||
241 | if (!d->nestedResetLevel)
| 0-32 | ||||||||||||||||||||||||
242 | QAbstractTableModel::endRemoveRows(); executed 32 times by 2 tests: QAbstractTableModel::endRemoveRows(); Executed by:
| 32 | ||||||||||||||||||||||||
243 | } executed 32 times by 2 tests: end of block Executed by:
| 32 | ||||||||||||||||||||||||
244 | - | |||||||||||||||||||||||||
245 | /*! \internal | - | ||||||||||||||||||||||||
246 | */ | - | ||||||||||||||||||||||||
247 | void QSqlQueryModel::beginInsertColumns(const QModelIndex &parent, int first, int last) | - | ||||||||||||||||||||||||
248 | { | - | ||||||||||||||||||||||||
249 | Q_D(QSqlQueryModel); | - | ||||||||||||||||||||||||
250 | if (!d->nestedResetLevel)
| 0-10 | ||||||||||||||||||||||||
251 | QAbstractTableModel::beginInsertColumns(parent, first, last); executed 10 times by 2 tests: QAbstractTableModel::beginInsertColumns(parent, first, last); Executed by:
| 10 | ||||||||||||||||||||||||
252 | } executed 10 times by 2 tests: end of block Executed by:
| 10 | ||||||||||||||||||||||||
253 | - | |||||||||||||||||||||||||
254 | /*! \internal | - | ||||||||||||||||||||||||
255 | */ | - | ||||||||||||||||||||||||
256 | void QSqlQueryModel::endInsertColumns() | - | ||||||||||||||||||||||||
257 | { | - | ||||||||||||||||||||||||
258 | Q_D(QSqlQueryModel); | - | ||||||||||||||||||||||||
259 | if (!d->nestedResetLevel)
| 0-10 | ||||||||||||||||||||||||
260 | QAbstractTableModel::endInsertColumns(); executed 10 times by 2 tests: QAbstractTableModel::endInsertColumns(); Executed by:
| 10 | ||||||||||||||||||||||||
261 | } executed 10 times by 2 tests: end of block Executed by:
| 10 | ||||||||||||||||||||||||
262 | - | |||||||||||||||||||||||||
263 | /*! \internal | - | ||||||||||||||||||||||||
264 | */ | - | ||||||||||||||||||||||||
265 | void QSqlQueryModel::beginRemoveColumns(const QModelIndex &parent, int first, int last) | - | ||||||||||||||||||||||||
266 | { | - | ||||||||||||||||||||||||
267 | Q_D(QSqlQueryModel); | - | ||||||||||||||||||||||||
268 | if (!d->nestedResetLevel)
| 0-4 | ||||||||||||||||||||||||
269 | QAbstractTableModel::beginRemoveColumns(parent, first, last); executed 4 times by 1 test: QAbstractTableModel::beginRemoveColumns(parent, first, last); Executed by:
| 4 | ||||||||||||||||||||||||
270 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||||||||||||||
271 | - | |||||||||||||||||||||||||
272 | /*! \internal | - | ||||||||||||||||||||||||
273 | */ | - | ||||||||||||||||||||||||
274 | void QSqlQueryModel::endRemoveColumns() | - | ||||||||||||||||||||||||
275 | { | - | ||||||||||||||||||||||||
276 | Q_D(QSqlQueryModel); | - | ||||||||||||||||||||||||
277 | if (!d->nestedResetLevel)
| 0-4 | ||||||||||||||||||||||||
278 | QAbstractTableModel::endRemoveColumns(); executed 4 times by 1 test: QAbstractTableModel::endRemoveColumns(); Executed by:
| 4 | ||||||||||||||||||||||||
279 | } executed 4 times by 1 test: end of block Executed by:
| 4 | ||||||||||||||||||||||||
280 | - | |||||||||||||||||||||||||
281 | /*! \internal | - | ||||||||||||||||||||||||
282 | */ | - | ||||||||||||||||||||||||
283 | void QSqlQueryModel::beginResetModel() | - | ||||||||||||||||||||||||
284 | { | - | ||||||||||||||||||||||||
285 | Q_D(QSqlQueryModel); | - | ||||||||||||||||||||||||
286 | if (!d->nestedResetLevel)
| 538-574 | ||||||||||||||||||||||||
287 | QAbstractTableModel::beginResetModel(); executed 574 times by 4 tests: QAbstractTableModel::beginResetModel(); Executed by:
| 574 | ||||||||||||||||||||||||
288 | ++d->nestedResetLevel; | - | ||||||||||||||||||||||||
289 | } executed 1112 times by 4 tests: end of block Executed by:
| 1112 | ||||||||||||||||||||||||
290 | - | |||||||||||||||||||||||||
291 | /*! \internal | - | ||||||||||||||||||||||||
292 | */ | - | ||||||||||||||||||||||||
293 | void QSqlQueryModel::endResetModel() | - | ||||||||||||||||||||||||
294 | { | - | ||||||||||||||||||||||||
295 | Q_D(QSqlQueryModel); | - | ||||||||||||||||||||||||
296 | --d->nestedResetLevel; | - | ||||||||||||||||||||||||
297 | if (!d->nestedResetLevel)
| 538-574 | ||||||||||||||||||||||||
298 | QAbstractTableModel::endResetModel(); executed 574 times by 4 tests: QAbstractTableModel::endResetModel(); Executed by:
| 574 | ||||||||||||||||||||||||
299 | } executed 1112 times by 4 tests: end of block Executed by:
| 1112 | ||||||||||||||||||||||||
300 | - | |||||||||||||||||||||||||
301 | /*! \fn int QSqlQueryModel::rowCount(const QModelIndex &parent) const | - | ||||||||||||||||||||||||
302 | \since 4.1 | - | ||||||||||||||||||||||||
303 | - | |||||||||||||||||||||||||
304 | If the database supports returning the size of a query | - | ||||||||||||||||||||||||
305 | (see QSqlDriver::hasFeature()), the number of rows of the current | - | ||||||||||||||||||||||||
306 | query is returned. Otherwise, returns the number of rows | - | ||||||||||||||||||||||||
307 | currently cached on the client. | - | ||||||||||||||||||||||||
308 | - | |||||||||||||||||||||||||
309 | \a parent should always be an invalid QModelIndex. | - | ||||||||||||||||||||||||
310 | - | |||||||||||||||||||||||||
311 | \sa canFetchMore(), QSqlDriver::hasFeature() | - | ||||||||||||||||||||||||
312 | */ | - | ||||||||||||||||||||||||
313 | int QSqlQueryModel::rowCount(const QModelIndex &index) const | - | ||||||||||||||||||||||||
314 | { | - | ||||||||||||||||||||||||
315 | Q_D(const QSqlQueryModel); | - | ||||||||||||||||||||||||
316 | return index.isValid() ? 0 : d->bottom.row() + 1; executed 22178 times by 4 tests: return index.isValid() ? 0 : d->bottom.row() + 1; Executed by:
| 1028-22178 | ||||||||||||||||||||||||
317 | } | - | ||||||||||||||||||||||||
318 | - | |||||||||||||||||||||||||
319 | /*! \reimp | - | ||||||||||||||||||||||||
320 | */ | - | ||||||||||||||||||||||||
321 | int QSqlQueryModel::columnCount(const QModelIndex &index) const | - | ||||||||||||||||||||||||
322 | { | - | ||||||||||||||||||||||||
323 | Q_D(const QSqlQueryModel); | - | ||||||||||||||||||||||||
324 | return index.isValid() ? 0 : d->rec.count(); executed 20241 times by 4 tests: return index.isValid() ? 0 : d->rec.count(); Executed by:
| 2-20241 | ||||||||||||||||||||||||
325 | } | - | ||||||||||||||||||||||||
326 | - | |||||||||||||||||||||||||
327 | /*! | - | ||||||||||||||||||||||||
328 | Returns the value for the specified \a item and \a role. | - | ||||||||||||||||||||||||
329 | - | |||||||||||||||||||||||||
330 | If \a item is out of bounds or if an error occurred, an invalid | - | ||||||||||||||||||||||||
331 | QVariant is returned. | - | ||||||||||||||||||||||||
332 | - | |||||||||||||||||||||||||
333 | \sa lastError() | - | ||||||||||||||||||||||||
334 | */ | - | ||||||||||||||||||||||||
335 | QVariant QSqlQueryModel::data(const QModelIndex &item, int role) const | - | ||||||||||||||||||||||||
336 | { | - | ||||||||||||||||||||||||
337 | Q_D(const QSqlQueryModel); | - | ||||||||||||||||||||||||
338 | if (!item.isValid())
| 519-3240 | ||||||||||||||||||||||||
339 | return QVariant(); executed 519 times by 2 tests: return QVariant(); Executed by:
| 519 | ||||||||||||||||||||||||
340 | - | |||||||||||||||||||||||||
341 | QVariant v; | - | ||||||||||||||||||||||||
342 | if (role & ~(Qt::DisplayRole | Qt::EditRole))
| 9-3231 | ||||||||||||||||||||||||
343 | return v; executed 9 times by 1 test: return v; Executed by:
| 9 | ||||||||||||||||||||||||
344 | - | |||||||||||||||||||||||||
345 | if (!d->rec.isGenerated(item.column()))
| 20-3211 | ||||||||||||||||||||||||
346 | return v; executed 20 times by 2 tests: return v; Executed by:
| 20 | ||||||||||||||||||||||||
347 | QModelIndex dItem = indexInQuery(item); | - | ||||||||||||||||||||||||
348 | if (dItem.row() > d->bottom.row())
| 0-3211 | ||||||||||||||||||||||||
349 | const_cast<QSqlQueryModelPrivate *>(d)->prefetch(dItem.row()); never executed: const_cast<QSqlQueryModelPrivate *>(d)->prefetch(dItem.row()); | 0 | ||||||||||||||||||||||||
350 | - | |||||||||||||||||||||||||
351 | if (!d->query.seek(dItem.row())) {
| 0-3211 | ||||||||||||||||||||||||
352 | d->error = d->query.lastError(); | - | ||||||||||||||||||||||||
353 | return v; never executed: return v; | 0 | ||||||||||||||||||||||||
354 | } | - | ||||||||||||||||||||||||
355 | - | |||||||||||||||||||||||||
356 | return d->query.value(dItem.column()); executed 3211 times by 4 tests: return d->query.value(dItem.column()); Executed by:
| 3211 | ||||||||||||||||||||||||
357 | } | - | ||||||||||||||||||||||||
358 | - | |||||||||||||||||||||||||
359 | /*! | - | ||||||||||||||||||||||||
360 | Returns the header data for the given \a role in the \a section | - | ||||||||||||||||||||||||
361 | of the header with the specified \a orientation. | - | ||||||||||||||||||||||||
362 | */ | - | ||||||||||||||||||||||||
363 | QVariant QSqlQueryModel::headerData(int section, Qt::Orientation orientation, int role) const | - | ||||||||||||||||||||||||
364 | { | - | ||||||||||||||||||||||||
365 | Q_D(const QSqlQueryModel); | - | ||||||||||||||||||||||||
366 | if (orientation == Qt::Horizontal) {
| 32-1601 | ||||||||||||||||||||||||
367 | QVariant val = d->headers.value(section).value(role); | - | ||||||||||||||||||||||||
368 | if (role == Qt::DisplayRole && !val.isValid())
| 0-27 | ||||||||||||||||||||||||
369 | val = d->headers.value(section).value(Qt::EditRole); executed 27 times by 3 tests: val = d->headers.value(section).value(Qt::EditRole); Executed by:
| 27 | ||||||||||||||||||||||||
370 | if (val.isValid())
| 2-30 | ||||||||||||||||||||||||
371 | return val; executed 2 times by 1 test: return val; Executed by:
| 2 | ||||||||||||||||||||||||
372 | if (role == Qt::DisplayRole && d->rec.count() > section && d->columnInQuery(section) != -1)
| 4-25 | ||||||||||||||||||||||||
373 | return d->rec.fieldName(section); executed 16 times by 3 tests: return d->rec.fieldName(section); Executed by:
| 16 | ||||||||||||||||||||||||
374 | } executed 14 times by 3 tests: end of block Executed by:
| 14 | ||||||||||||||||||||||||
375 | return QAbstractItemModel::headerData(section, orientation, role); executed 1615 times by 3 tests: return QAbstractItemModel::headerData(section, orientation, role); Executed by:
| 1615 | ||||||||||||||||||||||||
376 | } | - | ||||||||||||||||||||||||
377 | - | |||||||||||||||||||||||||
378 | /*! | - | ||||||||||||||||||||||||
379 | This virtual function is called whenever the query changes. The | - | ||||||||||||||||||||||||
380 | default implementation does nothing. | - | ||||||||||||||||||||||||
381 | - | |||||||||||||||||||||||||
382 | query() returns the new query. | - | ||||||||||||||||||||||||
383 | - | |||||||||||||||||||||||||
384 | \sa query(), setQuery() | - | ||||||||||||||||||||||||
385 | */ | - | ||||||||||||||||||||||||
386 | void QSqlQueryModel::queryChange() | - | ||||||||||||||||||||||||
387 | { | - | ||||||||||||||||||||||||
388 | // do nothing | - | ||||||||||||||||||||||||
389 | } | - | ||||||||||||||||||||||||
390 | - | |||||||||||||||||||||||||
391 | /*! | - | ||||||||||||||||||||||||
392 | Resets the model and sets the data provider to be the given \a | - | ||||||||||||||||||||||||
393 | query. Note that the query must be active and must not be | - | ||||||||||||||||||||||||
394 | isForwardOnly(). | - | ||||||||||||||||||||||||
395 | - | |||||||||||||||||||||||||
396 | lastError() can be used to retrieve verbose information if there | - | ||||||||||||||||||||||||
397 | was an error setting the query. | - | ||||||||||||||||||||||||
398 | - | |||||||||||||||||||||||||
399 | \note Calling setQuery() will remove any inserted columns. | - | ||||||||||||||||||||||||
400 | - | |||||||||||||||||||||||||
401 | \sa query(), QSqlQuery::isActive(), QSqlQuery::setForwardOnly(), lastError() | - | ||||||||||||||||||||||||
402 | */ | - | ||||||||||||||||||||||||
403 | void QSqlQueryModel::setQuery(const QSqlQuery &query) | - | ||||||||||||||||||||||||
404 | { | - | ||||||||||||||||||||||||
405 | Q_D(QSqlQueryModel); | - | ||||||||||||||||||||||||
406 | beginResetModel(); | - | ||||||||||||||||||||||||
407 | - | |||||||||||||||||||||||||
408 | QSqlRecord newRec = query.record(); | - | ||||||||||||||||||||||||
409 | bool columnsChanged = (newRec != d->rec); | - | ||||||||||||||||||||||||
410 | - | |||||||||||||||||||||||||
411 | if (d->colOffsets.size() != newRec.count() || columnsChanged)
| 66-316 | ||||||||||||||||||||||||
412 | d->initColOffsets(newRec.count()); executed 253 times by 4 tests: d->initColOffsets(newRec.count()); Executed by:
| 253 | ||||||||||||||||||||||||
413 | - | |||||||||||||||||||||||||
414 | d->bottom = QModelIndex(); | - | ||||||||||||||||||||||||
415 | d->error = QSqlError(); | - | ||||||||||||||||||||||||
416 | d->query = query; | - | ||||||||||||||||||||||||
417 | d->rec = newRec; | - | ||||||||||||||||||||||||
418 | d->atEnd = true; | - | ||||||||||||||||||||||||
419 | - | |||||||||||||||||||||||||
420 | if (query.isForwardOnly()) {
| 0-382 | ||||||||||||||||||||||||
421 | d->error = QSqlError(QLatin1String("Forward-only queries " | - | ||||||||||||||||||||||||
422 | "cannot be used in a data model"), | - | ||||||||||||||||||||||||
423 | QString(), QSqlError::ConnectionError); | - | ||||||||||||||||||||||||
424 | endResetModel(); | - | ||||||||||||||||||||||||
425 | return; never executed: return; | 0 | ||||||||||||||||||||||||
426 | } | - | ||||||||||||||||||||||||
427 | - | |||||||||||||||||||||||||
428 | if (!query.isActive()) {
| 7-375 | ||||||||||||||||||||||||
429 | d->error = query.lastError(); | - | ||||||||||||||||||||||||
430 | endResetModel(); | - | ||||||||||||||||||||||||
431 | return; executed 7 times by 2 tests: return; Executed by:
| 7 | ||||||||||||||||||||||||
432 | } | - | ||||||||||||||||||||||||
433 | - | |||||||||||||||||||||||||
434 | if (query.driver()->hasFeature(QSqlDriver::QuerySize) && d->query.size() > 0) {
| 0-375 | ||||||||||||||||||||||||
435 | d->bottom = createIndex(d->query.size() - 1, d->rec.count() - 1); | - | ||||||||||||||||||||||||
436 | } else { never executed: end of block | 0 | ||||||||||||||||||||||||
437 | d->bottom = createIndex(-1, d->rec.count() - 1); | - | ||||||||||||||||||||||||
438 | d->atEnd = false; | - | ||||||||||||||||||||||||
439 | } executed 375 times by 4 tests: end of block Executed by:
| 375 | ||||||||||||||||||||||||
440 | - | |||||||||||||||||||||||||
441 | - | |||||||||||||||||||||||||
442 | // fetchMore does the rowsInserted stuff for incremental models | - | ||||||||||||||||||||||||
443 | fetchMore(); | - | ||||||||||||||||||||||||
444 | - | |||||||||||||||||||||||||
445 | endResetModel(); | - | ||||||||||||||||||||||||
446 | queryChange(); | - | ||||||||||||||||||||||||
447 | } executed 375 times by 4 tests: end of block Executed by:
| 375 | ||||||||||||||||||||||||
448 | - | |||||||||||||||||||||||||
449 | /*! \overload | - | ||||||||||||||||||||||||
450 | - | |||||||||||||||||||||||||
451 | Executes the query \a query for the given database connection \a | - | ||||||||||||||||||||||||
452 | db. If no database (or an invalid database) is specified, the | - | ||||||||||||||||||||||||
453 | default connection is used. | - | ||||||||||||||||||||||||
454 | - | |||||||||||||||||||||||||
455 | lastError() can be used to retrieve verbose information if there | - | ||||||||||||||||||||||||
456 | was an error setting the query. | - | ||||||||||||||||||||||||
457 | - | |||||||||||||||||||||||||
458 | Example: | - | ||||||||||||||||||||||||
459 | \snippet code/src_sql_models_qsqlquerymodel.cpp 1 | - | ||||||||||||||||||||||||
460 | - | |||||||||||||||||||||||||
461 | \sa query(), queryChange(), lastError() | - | ||||||||||||||||||||||||
462 | */ | - | ||||||||||||||||||||||||
463 | void QSqlQueryModel::setQuery(const QString &query, const QSqlDatabase &db) | - | ||||||||||||||||||||||||
464 | { | - | ||||||||||||||||||||||||
465 | setQuery(QSqlQuery(query, db)); | - | ||||||||||||||||||||||||
466 | } executed 57 times by 2 tests: end of block Executed by:
| 57 | ||||||||||||||||||||||||
467 | - | |||||||||||||||||||||||||
468 | /*! | - | ||||||||||||||||||||||||
469 | Clears the model and releases any acquired resource. | - | ||||||||||||||||||||||||
470 | */ | - | ||||||||||||||||||||||||
471 | void QSqlQueryModel::clear() | - | ||||||||||||||||||||||||
472 | { | - | ||||||||||||||||||||||||
473 | Q_D(QSqlQueryModel); | - | ||||||||||||||||||||||||
474 | beginResetModel(); | - | ||||||||||||||||||||||||
475 | d->error = QSqlError(); | - | ||||||||||||||||||||||||
476 | d->atEnd = true; | - | ||||||||||||||||||||||||
477 | d->query.clear(); | - | ||||||||||||||||||||||||
478 | d->rec.clear(); | - | ||||||||||||||||||||||||
479 | d->colOffsets.clear(); | - | ||||||||||||||||||||||||
480 | d->bottom = QModelIndex(); | - | ||||||||||||||||||||||||
481 | d->headers.clear(); | - | ||||||||||||||||||||||||
482 | endResetModel(); | - | ||||||||||||||||||||||||
483 | } executed 191 times by 4 tests: end of block Executed by:
| 191 | ||||||||||||||||||||||||
484 | - | |||||||||||||||||||||||||
485 | /*! | - | ||||||||||||||||||||||||
486 | Sets the caption for a horizontal header for the specified \a role to | - | ||||||||||||||||||||||||
487 | \a value. This is useful if the model is used to | - | ||||||||||||||||||||||||
488 | display data in a view (e.g., QTableView). | - | ||||||||||||||||||||||||
489 | - | |||||||||||||||||||||||||
490 | Returns \c true if \a orientation is Qt::Horizontal and | - | ||||||||||||||||||||||||
491 | the \a section refers to a valid section; otherwise returns | - | ||||||||||||||||||||||||
492 | false. | - | ||||||||||||||||||||||||
493 | - | |||||||||||||||||||||||||
494 | Note that this function cannot be used to modify values in the | - | ||||||||||||||||||||||||
495 | database since the model is read-only. | - | ||||||||||||||||||||||||
496 | - | |||||||||||||||||||||||||
497 | \sa data() | - | ||||||||||||||||||||||||
498 | */ | - | ||||||||||||||||||||||||
499 | bool QSqlQueryModel::setHeaderData(int section, Qt::Orientation orientation, | - | ||||||||||||||||||||||||
500 | const QVariant &value, int role) | - | ||||||||||||||||||||||||
501 | { | - | ||||||||||||||||||||||||
502 | Q_D(QSqlQueryModel); | - | ||||||||||||||||||||||||
503 | if (orientation != Qt::Horizontal || section < 0 || columnCount() <= section)
| 4-18 | ||||||||||||||||||||||||
504 | return false; executed 16 times by 2 tests: return false; Executed by:
| 16 | ||||||||||||||||||||||||
505 | - | |||||||||||||||||||||||||
506 | if (d->headers.size() <= section)
| 2-4 | ||||||||||||||||||||||||
507 | d->headers.resize(qMax(section + 1, 16)); executed 4 times by 3 tests: d->headers.resize(qMax(section + 1, 16)); Executed by:
| 4 | ||||||||||||||||||||||||
508 | d->headers[section][role] = value; | - | ||||||||||||||||||||||||
509 | emit headerDataChanged(orientation, section, section); | - | ||||||||||||||||||||||||
510 | return true; executed 6 times by 3 tests: return true; Executed by:
| 6 | ||||||||||||||||||||||||
511 | } | - | ||||||||||||||||||||||||
512 | - | |||||||||||||||||||||||||
513 | /*! | - | ||||||||||||||||||||||||
514 | Returns the QSqlQuery associated with this model. | - | ||||||||||||||||||||||||
515 | - | |||||||||||||||||||||||||
516 | \sa setQuery() | - | ||||||||||||||||||||||||
517 | */ | - | ||||||||||||||||||||||||
518 | QSqlQuery QSqlQueryModel::query() const | - | ||||||||||||||||||||||||
519 | { | - | ||||||||||||||||||||||||
520 | Q_D(const QSqlQueryModel); | - | ||||||||||||||||||||||||
521 | return d->query; never executed: return d->query; | 0 | ||||||||||||||||||||||||
522 | } | - | ||||||||||||||||||||||||
523 | - | |||||||||||||||||||||||||
524 | /*! | - | ||||||||||||||||||||||||
525 | Returns information about the last error that occurred on the | - | ||||||||||||||||||||||||
526 | database. | - | ||||||||||||||||||||||||
527 | - | |||||||||||||||||||||||||
528 | \sa query() | - | ||||||||||||||||||||||||
529 | */ | - | ||||||||||||||||||||||||
530 | QSqlError QSqlQueryModel::lastError() const | - | ||||||||||||||||||||||||
531 | { | - | ||||||||||||||||||||||||
532 | Q_D(const QSqlQueryModel); | - | ||||||||||||||||||||||||
533 | return d->error; executed 796 times by 4 tests: return d->error; Executed by:
| 796 | ||||||||||||||||||||||||
534 | } | - | ||||||||||||||||||||||||
535 | - | |||||||||||||||||||||||||
536 | /*! | - | ||||||||||||||||||||||||
537 | Protected function which allows derived classes to set the value of | - | ||||||||||||||||||||||||
538 | the last error that occurred on the database to \a error. | - | ||||||||||||||||||||||||
539 | - | |||||||||||||||||||||||||
540 | \sa lastError() | - | ||||||||||||||||||||||||
541 | */ | - | ||||||||||||||||||||||||
542 | void QSqlQueryModel::setLastError(const QSqlError &error) | - | ||||||||||||||||||||||||
543 | { | - | ||||||||||||||||||||||||
544 | Q_D(QSqlQueryModel); | - | ||||||||||||||||||||||||
545 | d->error = error; | - | ||||||||||||||||||||||||
546 | } never executed: end of block | 0 | ||||||||||||||||||||||||
547 | - | |||||||||||||||||||||||||
548 | /*! | - | ||||||||||||||||||||||||
549 | Returns the record containing information about the fields of the | - | ||||||||||||||||||||||||
550 | current query. If \a row is the index of a valid row, the record | - | ||||||||||||||||||||||||
551 | will be populated with values from that row. | - | ||||||||||||||||||||||||
552 | - | |||||||||||||||||||||||||
553 | If the model is not initialized, an empty record will be | - | ||||||||||||||||||||||||
554 | returned. | - | ||||||||||||||||||||||||
555 | - | |||||||||||||||||||||||||
556 | \sa QSqlRecord::isEmpty() | - | ||||||||||||||||||||||||
557 | */ | - | ||||||||||||||||||||||||
558 | QSqlRecord QSqlQueryModel::record(int row) const | - | ||||||||||||||||||||||||
559 | { | - | ||||||||||||||||||||||||
560 | Q_D(const QSqlQueryModel); | - | ||||||||||||||||||||||||
561 | if (row < 0)
| 0-250 | ||||||||||||||||||||||||
562 | return d->rec; never executed: return d->rec; | 0 | ||||||||||||||||||||||||
563 | - | |||||||||||||||||||||||||
564 | QSqlRecord rec = d->rec; | - | ||||||||||||||||||||||||
565 | for (int i = 0; i < rec.count(); ++i)
| 250-606 | ||||||||||||||||||||||||
566 | rec.setValue(i, data(createIndex(row, i), Qt::EditRole)); executed 606 times by 3 tests: rec.setValue(i, data(createIndex(row, i), Qt::EditRole)); Executed by:
| 606 | ||||||||||||||||||||||||
567 | return rec; executed 250 times by 3 tests: return rec; Executed by:
| 250 | ||||||||||||||||||||||||
568 | } | - | ||||||||||||||||||||||||
569 | - | |||||||||||||||||||||||||
570 | /*! \overload | - | ||||||||||||||||||||||||
571 | - | |||||||||||||||||||||||||
572 | Returns an empty record containing information about the fields | - | ||||||||||||||||||||||||
573 | of the current query. | - | ||||||||||||||||||||||||
574 | - | |||||||||||||||||||||||||
575 | If the model is not initialized, an empty record will be | - | ||||||||||||||||||||||||
576 | returned. | - | ||||||||||||||||||||||||
577 | - | |||||||||||||||||||||||||
578 | \sa QSqlRecord::isEmpty() | - | ||||||||||||||||||||||||
579 | */ | - | ||||||||||||||||||||||||
580 | QSqlRecord QSqlQueryModel::record() const | - | ||||||||||||||||||||||||
581 | { | - | ||||||||||||||||||||||||
582 | Q_D(const QSqlQueryModel); | - | ||||||||||||||||||||||||
583 | return d->rec; executed 21 times by 3 tests: return d->rec; Executed by:
| 21 | ||||||||||||||||||||||||
584 | } | - | ||||||||||||||||||||||||
585 | - | |||||||||||||||||||||||||
586 | /*! | - | ||||||||||||||||||||||||
587 | Inserts \a count columns into the model at position \a column. The | - | ||||||||||||||||||||||||
588 | \a parent parameter must always be an invalid QModelIndex, since | - | ||||||||||||||||||||||||
589 | the model does not support parent-child relationships. | - | ||||||||||||||||||||||||
590 | - | |||||||||||||||||||||||||
591 | Returns \c true if \a column is within bounds; otherwise returns \c false. | - | ||||||||||||||||||||||||
592 | - | |||||||||||||||||||||||||
593 | By default, inserted columns are empty. To fill them with data, | - | ||||||||||||||||||||||||
594 | reimplement data() and handle any inserted column separately: | - | ||||||||||||||||||||||||
595 | - | |||||||||||||||||||||||||
596 | \snippet sqldatabase/sqldatabase.cpp 23 | - | ||||||||||||||||||||||||
597 | - | |||||||||||||||||||||||||
598 | \sa removeColumns() | - | ||||||||||||||||||||||||
599 | */ | - | ||||||||||||||||||||||||
600 | bool QSqlQueryModel::insertColumns(int column, int count, const QModelIndex &parent) | - | ||||||||||||||||||||||||
601 | { | - | ||||||||||||||||||||||||
602 | Q_D(QSqlQueryModel); | - | ||||||||||||||||||||||||
603 | if (count <= 0 || parent.isValid() || column < 0 || column > d->rec.count())
| 0-14 | ||||||||||||||||||||||||
604 | return false; executed 4 times by 1 test: return false; Executed by:
| 4 | ||||||||||||||||||||||||
605 | - | |||||||||||||||||||||||||
606 | beginInsertColumns(parent, column, column + count - 1); | - | ||||||||||||||||||||||||
607 | for (int c = 0; c < count; ++c) {
| 10 | ||||||||||||||||||||||||
608 | QSqlField field; | - | ||||||||||||||||||||||||
609 | field.setReadOnly(true); | - | ||||||||||||||||||||||||
610 | field.setGenerated(false); | - | ||||||||||||||||||||||||
611 | d->rec.insert(column, field); | - | ||||||||||||||||||||||||
612 | if (d->colOffsets.size() < d->rec.count()) {
| 1-9 | ||||||||||||||||||||||||
613 | int nVal = d->colOffsets.isEmpty() ? 0 : d->colOffsets[d->colOffsets.size() - 1];
| 0-9 | ||||||||||||||||||||||||
614 | d->colOffsets.append(nVal); | - | ||||||||||||||||||||||||
615 | Q_ASSERT(d->colOffsets.size() >= d->rec.count()); | - | ||||||||||||||||||||||||
616 | } executed 9 times by 2 tests: end of block Executed by:
| 9 | ||||||||||||||||||||||||
617 | for (int i = column + 1; i < d->colOffsets.count(); ++i)
| 10-22 | ||||||||||||||||||||||||
618 | ++d->colOffsets[i]; executed 22 times by 2 tests: ++d->colOffsets[i]; Executed by:
| 22 | ||||||||||||||||||||||||
619 | } executed 10 times by 2 tests: end of block Executed by:
| 10 | ||||||||||||||||||||||||
620 | endInsertColumns(); | - | ||||||||||||||||||||||||
621 | return true; executed 10 times by 2 tests: return true; Executed by:
| 10 | ||||||||||||||||||||||||
622 | } | - | ||||||||||||||||||||||||
623 | - | |||||||||||||||||||||||||
624 | /*! | - | ||||||||||||||||||||||||
625 | Removes \a count columns from the model starting from position \a | - | ||||||||||||||||||||||||
626 | column. The \a parent parameter must always be an invalid | - | ||||||||||||||||||||||||
627 | QModelIndex, since the model does not support parent-child | - | ||||||||||||||||||||||||
628 | relationships. | - | ||||||||||||||||||||||||
629 | - | |||||||||||||||||||||||||
630 | Removing columns effectively hides them. It does not affect the | - | ||||||||||||||||||||||||
631 | underlying QSqlQuery. | - | ||||||||||||||||||||||||
632 | - | |||||||||||||||||||||||||
633 | Returns \c true if the columns were removed; otherwise returns \c false. | - | ||||||||||||||||||||||||
634 | */ | - | ||||||||||||||||||||||||
635 | bool QSqlQueryModel::removeColumns(int column, int count, const QModelIndex &parent) | - | ||||||||||||||||||||||||
636 | { | - | ||||||||||||||||||||||||
637 | Q_D(QSqlQueryModel); | - | ||||||||||||||||||||||||
638 | if (count <= 0 || parent.isValid() || column < 0 || column >= d->rec.count())
| 0-7 | ||||||||||||||||||||||||
639 | return false; executed 3 times by 1 test: return false; Executed by:
| 3 | ||||||||||||||||||||||||
640 | - | |||||||||||||||||||||||||
641 | beginRemoveColumns(parent, column, column + count - 1); | - | ||||||||||||||||||||||||
642 | - | |||||||||||||||||||||||||
643 | int i; | - | ||||||||||||||||||||||||
644 | for (i = 0; i < count; ++i)
| 4 | ||||||||||||||||||||||||
645 | d->rec.remove(column); executed 4 times by 1 test: d->rec.remove(column); Executed by:
| 4 | ||||||||||||||||||||||||
646 | for (i = column; i < d->colOffsets.count(); ++i)
| 4-9 | ||||||||||||||||||||||||
647 | d->colOffsets[i] -= count; executed 9 times by 1 test: d->colOffsets[i] -= count; Executed by:
| 9 | ||||||||||||||||||||||||
648 | - | |||||||||||||||||||||||||
649 | endRemoveColumns(); | - | ||||||||||||||||||||||||
650 | return true; executed 4 times by 1 test: return true; Executed by:
| 4 | ||||||||||||||||||||||||
651 | } | - | ||||||||||||||||||||||||
652 | - | |||||||||||||||||||||||||
653 | /*! | - | ||||||||||||||||||||||||
654 | Returns the index of the value in the database result set for the | - | ||||||||||||||||||||||||
655 | given \a item in the model. | - | ||||||||||||||||||||||||
656 | - | |||||||||||||||||||||||||
657 | The return value is identical to \a item if no columns or rows | - | ||||||||||||||||||||||||
658 | have been inserted, removed, or moved around. | - | ||||||||||||||||||||||||
659 | - | |||||||||||||||||||||||||
660 | Returns an invalid model index if \a item is out of bounds or if | - | ||||||||||||||||||||||||
661 | \a item does not point to a value in the result set. | - | ||||||||||||||||||||||||
662 | - | |||||||||||||||||||||||||
663 | \sa QSqlTableModel::indexInQuery(), insertColumns(), removeColumns() | - | ||||||||||||||||||||||||
664 | */ | - | ||||||||||||||||||||||||
665 | QModelIndex QSqlQueryModel::indexInQuery(const QModelIndex &item) const | - | ||||||||||||||||||||||||
666 | { | - | ||||||||||||||||||||||||
667 | Q_D(const QSqlQueryModel); | - | ||||||||||||||||||||||||
668 | int modelColumn = d->columnInQuery(item.column()); | - | ||||||||||||||||||||||||
669 | if (modelColumn < 0)
| 23-3226 | ||||||||||||||||||||||||
670 | return QModelIndex(); executed 23 times by 1 test: return QModelIndex(); Executed by:
| 23 | ||||||||||||||||||||||||
671 | return createIndex(item.row(), modelColumn, item.internalPointer()); executed 3226 times by 4 tests: return createIndex(item.row(), modelColumn, item.internalPointer()); Executed by:
| 3226 | ||||||||||||||||||||||||
672 | } | - | ||||||||||||||||||||||||
673 | - | |||||||||||||||||||||||||
674 | QT_END_NAMESPACE | - | ||||||||||||||||||||||||
Source code | Switch to Preprocessed file |