models/qsqlquerymodel.cpp

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

Generated by Squish Coco Non-Commercial