models/qsqltablemodel.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
2** -
3** Copyright (C) 2012 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 "qsqltablemodel.h" -
43 -
44#include "qsqldriver.h" -
45#include "qsqlerror.h" -
46#include "qsqlfield.h" -
47#include "qsqlindex.h" -
48#include "qsqlquery.h" -
49#include "qsqlrecord.h" -
50#include "qsqlresult.h" -
51 -
52#include "qsqltablemodel_p.h" -
53 -
54#include <qdebug.h> -
55 -
56QT_BEGIN_NAMESPACE -
57 -
58typedef QSqlTableModelSql Sql; -
59 -
60/*! \internal -
61 Populates our record with values. -
62*/ -
63QSqlRecord QSqlTableModelPrivate::record(const QVector<QVariant> &values) const -
64{ -
65 QSqlRecord r = rec;
never executed (the execution status of this line is deduced): QSqlRecord r = rec;
-
66 for (int i = 0; i < r.count() && i < values.count(); ++i)
never evaluated: i < r.count()
never evaluated: i < values.count()
0
67 r.setValue(i, values.at(i));
never executed: r.setValue(i, values.at(i));
0
68 return r;
never executed: return r;
0
69} -
70 -
71int QSqlTableModelPrivate::nameToIndex(const QString &name) const -
72{ -
73 return rec.indexOf(strippedFieldName(name));
executed: return rec.indexOf(strippedFieldName(name));
Execution Count:110
110
74} -
75 -
76QString QSqlTableModelPrivate::strippedFieldName(const QString &name) const -
77{ -
78 QString fieldname = name;
executed (the execution status of this line is deduced): QString fieldname = name;
-
79 if (db.driver()->isIdentifierEscaped(fieldname, QSqlDriver::FieldName))
evaluated: db.driver()->isIdentifierEscaped(fieldname, QSqlDriver::FieldName)
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:123
4-123
80 fieldname = db.driver()->stripDelimiters(fieldname, QSqlDriver::FieldName);
executed: fieldname = db.driver()->stripDelimiters(fieldname, QSqlDriver::FieldName);
Execution Count:4
4
81 return fieldname;
executed: return fieldname;
Execution Count:127
127
82} -
83 -
84int QSqlTableModelPrivate::insertCount(int maxRow) const -
85{ -
86 int cnt = 0;
executed (the execution status of this line is deduced): int cnt = 0;
-
87 CacheMap::ConstIterator i = cache.constBegin();
executed (the execution status of this line is deduced): CacheMap::ConstIterator i = cache.constBegin();
-
88 const CacheMap::ConstIterator e = cache.constEnd();
executed (the execution status of this line is deduced): const CacheMap::ConstIterator e = cache.constEnd();
-
89 for ( ; i != e && (maxRow < 0 || i.key() <= maxRow); ++i)
evaluated: i != e
TRUEFALSE
yes
Evaluation Count:2027
yes
Evaluation Count:12671
evaluated: maxRow < 0
TRUEFALSE
yes
Evaluation Count:1584
yes
Evaluation Count:443
evaluated: i.key() <= maxRow
TRUEFALSE
yes
Evaluation Count:365
yes
Evaluation Count:78
78-12671
90 if (i.value().insert())
evaluated: i.value().insert()
TRUEFALSE
yes
Evaluation Count:1152
yes
Evaluation Count:797
797-1152
91 ++cnt;
executed: ++cnt;
Execution Count:1152
1152
92 -
93 return cnt;
executed: return cnt;
Execution Count:12749
12749
94} -
95 -
96void QSqlTableModelPrivate::initRecordAndPrimaryIndex() -
97{ -
98 rec = db.record(tableName);
executed (the execution status of this line is deduced): rec = db.record(tableName);
-
99 primaryIndex = db.primaryIndex(tableName);
executed (the execution status of this line is deduced): primaryIndex = db.primaryIndex(tableName);
-
100 initColOffsets(rec.count());
executed (the execution status of this line is deduced): initColOffsets(rec.count());
-
101}
executed: }
Execution Count:178
178
102 -
103void QSqlTableModelPrivate::clear() -
104{ -
105 sortColumn = -1;
executed (the execution status of this line is deduced): sortColumn = -1;
-
106 sortOrder = Qt::AscendingOrder;
executed (the execution status of this line is deduced): sortOrder = Qt::AscendingOrder;
-
107 tableName.clear();
executed (the execution status of this line is deduced): tableName.clear();
-
108 editQuery.clear();
executed (the execution status of this line is deduced): editQuery.clear();
-
109 cache.clear();
executed (the execution status of this line is deduced): cache.clear();
-
110 primaryIndex.clear();
executed (the execution status of this line is deduced): primaryIndex.clear();
-
111 rec.clear();
executed (the execution status of this line is deduced): rec.clear();
-
112 filter.clear();
executed (the execution status of this line is deduced): filter.clear();
-
113}
executed: }
Execution Count:183
183
114 -
115void QSqlTableModelPrivate::clearCache() -
116{ -
117 cache.clear();
executed (the execution status of this line is deduced): cache.clear();
-
118}
executed: }
Execution Count:297
297
119 -
120void QSqlTableModelPrivate::revertCachedRow(int row) -
121{ -
122 Q_Q(QSqlTableModel);
executed (the execution status of this line is deduced): QSqlTableModel * const q = q_func();
-
123 ModifiedRow r = cache.value(row);
executed (the execution status of this line is deduced): ModifiedRow r = cache.value(row);
-
124 -
125 switch (r.op()) { -
126 case QSqlTableModelPrivate::None: -
127 Q_ASSERT_X(false, "QSqlTableModelPrivate::revertCachedRow()", "Invalid entry in cache map");
never executed (the execution status of this line is deduced): qt_noop();
-
128 return;
never executed: return;
0
129 case QSqlTableModelPrivate::Update: -
130 case QSqlTableModelPrivate::Delete: -
131 if (!r.submitted()) {
evaluated: !r.submitted()
TRUEFALSE
yes
Evaluation Count:13
yes
Evaluation Count:3
3-13
132 cache[row].revert();
executed (the execution status of this line is deduced): cache[row].revert();
-
133 emit q->dataChanged(q->createIndex(row, 0),
executed (the execution status of this line is deduced): q->dataChanged(q->createIndex(row, 0),
-
134 q->createIndex(row, q->columnCount() - 1));
executed (the execution status of this line is deduced): q->createIndex(row, q->columnCount() - 1));
-
135 }
executed: }
Execution Count:13
13
136 break;
executed: break;
Execution Count:16
16
137 case QSqlTableModelPrivate::Insert: { -
138 QMap<int, QSqlTableModelPrivate::ModifiedRow>::Iterator it = cache.find(row);
executed (the execution status of this line is deduced): QMap<int, QSqlTableModelPrivate::ModifiedRow>::Iterator it = cache.find(row);
-
139 if (it == cache.end())
partially evaluated: it == cache.end()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:29
0-29
140 return;
never executed: return;
0
141 q->beginRemoveRows(QModelIndex(), row, row);
executed (the execution status of this line is deduced): q->beginRemoveRows(QModelIndex(), row, row);
-
142 it = cache.erase(it);
executed (the execution status of this line is deduced): it = cache.erase(it);
-
143 while (it != cache.end()) {
evaluated: it != cache.end()
TRUEFALSE
yes
Evaluation Count:5
yes
Evaluation Count:29
5-29
144 int oldKey = it.key();
executed (the execution status of this line is deduced): int oldKey = it.key();
-
145 const QSqlTableModelPrivate::ModifiedRow oldValue = it.value();
executed (the execution status of this line is deduced): const QSqlTableModelPrivate::ModifiedRow oldValue = it.value();
-
146 cache.erase(it);
executed (the execution status of this line is deduced): cache.erase(it);
-
147 it = cache.insert(oldKey - 1, oldValue);
executed (the execution status of this line is deduced): it = cache.insert(oldKey - 1, oldValue);
-
148 ++it;
executed (the execution status of this line is deduced): ++it;
-
149 }
executed: }
Execution Count:5
5
150 q->endRemoveRows();
executed (the execution status of this line is deduced): q->endRemoveRows();
-
151 break; }
executed: break;
Execution Count:29
29
152 } -
153}
executed: }
Execution Count:45
45
154 -
155bool QSqlTableModelPrivate::exec(const QString &stmt, bool prepStatement, -
156 const QSqlRecord &rec, const QSqlRecord &whereValues) -
157{ -
158 if (stmt.isEmpty())
partially evaluated: stmt.isEmpty()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:107
0-107
159 return false;
never executed: return false;
0
160 -
161 // lazy initialization of editQuery -
162 if (editQuery.driver() != db.driver())
evaluated: editQuery.driver() != db.driver()
TRUEFALSE
yes
Evaluation Count:45
yes
Evaluation Count:62
45-62
163 editQuery = QSqlQuery(db);
executed: editQuery = QSqlQuery(db);
Execution Count:45
45
164 -
165 // workaround for In-Process databases - remove all read locks -
166 // from the table to make sure the editQuery succeeds -
167 if (db.driver()->hasFeature(QSqlDriver::SimpleLocking))
partially evaluated: db.driver()->hasFeature(QSqlDriver::SimpleLocking)
TRUEFALSE
yes
Evaluation Count:107
no
Evaluation Count:0
0-107
168 const_cast<QSqlResult *>(query.result())->detachFromResultSet();
executed: const_cast<QSqlResult *>(query.result())->detachFromResultSet();
Execution Count:107
107
169 -
170 if (prepStatement) {
partially evaluated: prepStatement
TRUEFALSE
yes
Evaluation Count:107
no
Evaluation Count:0
0-107
171 if (editQuery.lastQuery() != stmt) {
evaluated: editQuery.lastQuery() != stmt
TRUEFALSE
yes
Evaluation Count:70
yes
Evaluation Count:37
37-70
172 if (!editQuery.prepare(stmt)) {
partially evaluated: !editQuery.prepare(stmt)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:70
0-70
173 error = editQuery.lastError();
never executed (the execution status of this line is deduced): error = editQuery.lastError();
-
174 return false;
never executed: return false;
0
175 } -
176 }
executed: }
Execution Count:70
70
177 int i;
executed (the execution status of this line is deduced): int i;
-
178 for (i = 0; i < rec.count(); ++i)
evaluated: i < rec.count()
TRUEFALSE
yes
Evaluation Count:267
yes
Evaluation Count:107
107-267
179 if (rec.isGenerated(i))
evaluated: rec.isGenerated(i)
TRUEFALSE
yes
Evaluation Count:176
yes
Evaluation Count:91
91-176
180 editQuery.addBindValue(rec.value(i));
executed: editQuery.addBindValue(rec.value(i));
Execution Count:176
176
181 for (i = 0; i < whereValues.count(); ++i)
evaluated: i < whereValues.count()
TRUEFALSE
yes
Evaluation Count:144
yes
Evaluation Count:107
107-144
182 if (whereValues.isGenerated(i) && !whereValues.isNull(i))
partially evaluated: whereValues.isGenerated(i)
TRUEFALSE
yes
Evaluation Count:144
no
Evaluation Count:0
evaluated: !whereValues.isNull(i)
TRUEFALSE
yes
Evaluation Count:139
yes
Evaluation Count:5
0-144
183 editQuery.addBindValue(whereValues.value(i));
executed: editQuery.addBindValue(whereValues.value(i));
Execution Count:139
139
184 -
185 if (!editQuery.exec()) {
evaluated: !editQuery.exec()
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:104
3-104
186 error = editQuery.lastError();
executed (the execution status of this line is deduced): error = editQuery.lastError();
-
187 return false;
executed: return false;
Execution Count:3
3
188 } -
189 } else {
executed: }
Execution Count:104
104
190 if (!editQuery.exec(stmt)) {
never evaluated: !editQuery.exec(stmt)
0
191 error = editQuery.lastError();
never executed (the execution status of this line is deduced): error = editQuery.lastError();
-
192 return false;
never executed: return false;
0
193 } -
194 }
never executed: }
0
195 return true;
executed: return true;
Execution Count:104
104
196} -
197 -
198QSqlRecord QSqlTableModelPrivate::primaryValues(const QSqlRecord &rec, const QSqlRecord &pIndex) -
199{ -
200 QSqlRecord pValues(pIndex);
executed (the execution status of this line is deduced): QSqlRecord pValues(pIndex);
-
201 -
202 for (int i = pValues.count() - 1; i >= 0; --i)
evaluated: i >= 0
TRUEFALSE
yes
Evaluation Count:260
yes
Evaluation Count:121
121-260
203 pValues.setValue(i, rec.value(pValues.fieldName(i)));
executed: pValues.setValue(i, rec.value(pValues.fieldName(i)));
Execution Count:260
260
204 -
205 return pValues;
executed: return pValues;
Execution Count:121
121
206} -
207 -
208QSqlRecord QSqlTableModelPrivate::primaryValues(int row) const -
209{ -
210 Q_Q(const QSqlTableModel);
executed (the execution status of this line is deduced): const QSqlTableModel * const q = q_func();
-
211 -
212 const QSqlRecord &pIndex = primaryIndex.isEmpty() ? rec : primaryIndex;
evaluated: primaryIndex.isEmpty()
TRUEFALSE
yes
Evaluation Count:70
yes
Evaluation Count:51
51-70
213 -
214 ModifiedRow mr = cache.value(row);
executed (the execution status of this line is deduced): ModifiedRow mr = cache.value(row);
-
215 if (mr.op() != None)
evaluated: mr.op() != None
TRUEFALSE
yes
Evaluation Count:119
yes
Evaluation Count:2
2-119
216 return mr.primaryValues(pIndex);
executed: return mr.primaryValues(pIndex);
Execution Count:119
119
217 else -
218 return primaryValues(q->QSqlQueryModel::record(row), pIndex);
executed: return primaryValues(q->QSqlQueryModel::record(row), pIndex);
Execution Count:2
2
219} -
220 -
221/*! -
222 \class QSqlTableModel -
223 \brief The QSqlTableModel class provides an editable data model -
224 for a single database table. -
225 -
226 \ingroup database -
227 \inmodule QtSql -
228 -
229 QSqlTableModel is a high-level interface for reading and writing -
230 database records from a single table. It is build on top of the -
231 lower-level QSqlQuery and can be used to provide data to view -
232 classes such as QTableView. For example: -
233 -
234 \snippet sqldatabase/sqldatabase.cpp 24 -
235 -
236 We set the SQL table's name and the edit strategy, then we set up -
237 the labels displayed in the view header. The edit strategy -
238 dictates when the changes done by the user in the view are -
239 actually applied to the database. The possible values are \l -
240 OnFieldChange, \l OnRowChange, and \l OnManualSubmit. -
241 -
242 QSqlTableModel can also be used to access a database -
243 programmatically, without binding it to a view: -
244 -
245 \snippet sqldatabase/sqldatabase.cpp 21 -
246 -
247 The code snippet above extracts the \c salary field from record 4 in -
248 the result set of the query \c{SELECT * from employee}. -
249 -
250 It is possible to set filters using setFilter(), or modify the -
251 sort order using setSort(). At the end, you must call select() to -
252 populate the model with data. -
253 -
254 The \l{tablemodel} example illustrates how to use -
255 QSqlTableModel as the data source for a QTableView. -
256 -
257 QSqlTableModel provides no direct support for foreign keys. Use -
258 the QSqlRelationalTableModel and QSqlRelationalDelegate if you -
259 want to resolve foreign keys. -
260 -
261 \sa QSqlRelationalTableModel, QSqlQuery, {Model/View Programming}, -
262 {Table Model Example}, {Cached Table Example} -
263*/ -
264 -
265/*! -
266 \fn QSqlTableModel::beforeDelete(int row) -
267 -
268 This signal is emitted by deleteRowFromTable() before the \a row -
269 is deleted from the currently active database table. -
270*/ -
271 -
272/*! -
273 \fn void QSqlTableModel::primeInsert(int row, QSqlRecord &record) -
274 -
275 This signal is emitted by insertRows(), when an insertion is -
276 initiated in the given \a row of the currently active database -
277 table. The \a record parameter can be written to (since it is a -
278 reference), for example to populate some fields with default -
279 values and set the generated flags of the fields. Do not try to -
280 edit the record via other means such as setData() or setRecord() -
281 while handling this signal. -
282*/ -
283 -
284/*! -
285 \fn QSqlTableModel::beforeInsert(QSqlRecord &record) -
286 -
287 This signal is emitted by insertRowIntoTable() before a new row is -
288 inserted into the currently active database table. The values that -
289 are about to be inserted are stored in \a record and can be -
290 modified before they will be inserted. -
291*/ -
292 -
293/*! -
294 \fn QSqlTableModel::beforeUpdate(int row, QSqlRecord &record) -
295 -
296 This signal is emitted by updateRowInTable() before the \a row is -
297 updated in the currently active database table with the values -
298 from \a record. -
299 -
300 Note that only values that are marked as generated will be updated. -
301 The generated flag can be set with \l QSqlRecord::setGenerated() -
302 and checked with \l QSqlRecord::isGenerated(). -
303 -
304 \sa QSqlRecord::isGenerated() -
305*/ -
306 -
307/*! -
308 Creates an empty QSqlTableModel and sets the parent to \a parent -
309 and the database connection to \a db. If \a db is not valid, the -
310 default database connection will be used. -
311 -
312 The default edit strategy is \l OnRowChange. -
313*/ -
314QSqlTableModel::QSqlTableModel(QObject *parent, QSqlDatabase db) -
315 : QSqlQueryModel(*new QSqlTableModelPrivate, parent) -
316{ -
317 Q_D(QSqlTableModel);
executed (the execution status of this line is deduced): QSqlTableModelPrivate * const d = d_func();
-
318 d->db = db.isValid() ? db : QSqlDatabase::database();
evaluated: db.isValid()
TRUEFALSE
yes
Evaluation Count:85
yes
Evaluation Count:51
51-85
319}
executed: }
Execution Count:136
136
320 -
321/*! \internal -
322*/ -
323QSqlTableModel::QSqlTableModel(QSqlTableModelPrivate &dd, QObject *parent, QSqlDatabase db) -
324 : QSqlQueryModel(dd, parent) -
325{ -
326 Q_D(QSqlTableModel);
executed (the execution status of this line is deduced): QSqlTableModelPrivate * const d = d_func();
-
327 d->db = db.isValid() ? db : QSqlDatabase::database();
partially evaluated: db.isValid()
TRUEFALSE
yes
Evaluation Count:31
no
Evaluation Count:0
0-31
328}
executed: }
Execution Count:31
31
329 -
330/*! -
331 Destroys the object and frees any allocated resources. -
332*/ -
333QSqlTableModel::~QSqlTableModel() -
334{ -
335} -
336 -
337/*! -
338 Sets the database table on which the model operates to \a -
339 tableName. Does not select data from the table, but fetches its -
340 field information. -
341 -
342 To populate the model with the table's data, call select(). -
343 -
344 Error information can be retrieved with \l lastError(). -
345 -
346 \sa select(), setFilter(), lastError() -
347*/ -
348void QSqlTableModel::setTable(const QString &tableName) -
349{ -
350 Q_D(QSqlTableModel);
executed (the execution status of this line is deduced): QSqlTableModelPrivate * const d = d_func();
-
351 clear();
executed (the execution status of this line is deduced): clear();
-
352 d->tableName = tableName;
executed (the execution status of this line is deduced): d->tableName = tableName;
-
353 d->initRecordAndPrimaryIndex();
executed (the execution status of this line is deduced): d->initRecordAndPrimaryIndex();
-
354 -
355 if (d->rec.count() == 0)
evaluated: d->rec.count() == 0
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:175
1-175
356 d->error = QSqlError(QLatin1String("Unable to find table ") + d->tableName, QString(),
executed: d->error = QSqlError(QLatin1String("Unable to find table ") + d->tableName, QString(), QSqlError::StatementError);
Execution Count:1
1
357 QSqlError::StatementError);
executed: d->error = QSqlError(QLatin1String("Unable to find table ") + d->tableName, QString(), QSqlError::StatementError);
Execution Count:1
1
358}
executed: }
Execution Count:176
176
359 -
360/*! -
361 Returns the name of the currently selected table. -
362*/ -
363QString QSqlTableModel::tableName() const -
364{ -
365 Q_D(const QSqlTableModel);
executed (the execution status of this line is deduced): const QSqlTableModelPrivate * const d = d_func();
-
366 return d->tableName;
executed: return d->tableName;
Execution Count:475
475
367} -
368 -
369/*! -
370 Populates the model with data from the table that was set via setTable(), using the -
371 specified filter and sort condition, and returns true if successful; otherwise -
372 returns false. -
373 -
374 \note Calling select() will revert any unsubmitted changes and remove any inserted columns. -
375 -
376 \sa setTable(), setFilter(), selectStatement() -
377*/ -
378bool QSqlTableModel::select() -
379{ -
380 Q_D(QSqlTableModel);
executed (the execution status of this line is deduced): QSqlTableModelPrivate * const d = d_func();
-
381 const QString query = selectStatement();
executed (the execution status of this line is deduced): const QString query = selectStatement();
-
382 if (query.isEmpty())
evaluated: query.isEmpty()
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:297
1-297
383 return false;
executed: return false;
Execution Count:1
1
384 -
385 beginResetModel();
executed (the execution status of this line is deduced): beginResetModel();
-
386 -
387 d->clearCache();
executed (the execution status of this line is deduced): d->clearCache();
-
388 -
389 QSqlQuery qu(query, d->db);
executed (the execution status of this line is deduced): QSqlQuery qu(query, d->db);
-
390 setQuery(qu);
executed (the execution status of this line is deduced): setQuery(qu);
-
391 -
392 if (!qu.isActive() || lastError().isValid()) {
evaluated: !qu.isActive()
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:295
partially evaluated: lastError().isValid()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:295
0-295
393 // something went wrong - revert to non-select state -
394 d->initRecordAndPrimaryIndex();
executed (the execution status of this line is deduced): d->initRecordAndPrimaryIndex();
-
395 endResetModel();
executed (the execution status of this line is deduced): endResetModel();
-
396 return false;
executed: return false;
Execution Count:2
2
397 } -
398 endResetModel();
executed (the execution status of this line is deduced): endResetModel();
-
399 return true;
executed: return true;
Execution Count:295
295
400} -
401 -
402/*! -
403 \since 5.0 -
404 -
405 Refreshes \a row in the model with values from the database table row matching -
406 on primary key values. Without a primary key, all column values must match. If -
407 no matching row is found, the model will show an empty row. -
408 -
409 Returns true if successful; otherwise returns false. -
410 -
411 \sa select() -
412*/ -
413bool QSqlTableModel::selectRow(int row) -
414{ -
415 Q_D(QSqlTableModel);
executed (the execution status of this line is deduced): QSqlTableModelPrivate * const d = d_func();
-
416 -
417 if (row < 0 || row >= rowCount())
partially evaluated: row < 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:56
partially evaluated: row >= rowCount()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:56
0-56
418 return false;
never executed: return false;
0
419 -
420 const int table_sort_col = d->sortColumn;
executed (the execution status of this line is deduced): const int table_sort_col = d->sortColumn;
-
421 d->sortColumn = -1;
executed (the execution status of this line is deduced): d->sortColumn = -1;
-
422 const QString table_filter = d->filter;
executed (the execution status of this line is deduced): const QString table_filter = d->filter;
-
423 d->filter = d->db.driver()->sqlStatement(QSqlDriver::WhereStatement,
executed (the execution status of this line is deduced): d->filter = d->db.driver()->sqlStatement(QSqlDriver::WhereStatement,
-
424 d->tableName,
executed (the execution status of this line is deduced): d->tableName,
-
425 d->primaryValues(row),
executed (the execution status of this line is deduced): d->primaryValues(row),
-
426 false);
executed (the execution status of this line is deduced): false);
-
427 static const QString wh = Sql::where() + Sql::sp(); -
428 if (d->filter.startsWith(wh, Qt::CaseInsensitive))
partially evaluated: d->filter.startsWith(wh, Qt::CaseInsensitive)
TRUEFALSE
yes
Evaluation Count:56
no
Evaluation Count:0
0-56
429 d->filter.remove(0, wh.length());
executed: d->filter.remove(0, wh.length());
Execution Count:56
56
430 -
431 QString stmt;
executed (the execution status of this line is deduced): QString stmt;
-
432 -
433 if (!d->filter.isEmpty())
partially evaluated: !d->filter.isEmpty()
TRUEFALSE
yes
Evaluation Count:56
no
Evaluation Count:0
0-56
434 stmt = selectStatement();
executed: stmt = selectStatement();
Execution Count:56
56
435 -
436 d->sortColumn = table_sort_col;
executed (the execution status of this line is deduced): d->sortColumn = table_sort_col;
-
437 d->filter = table_filter;
executed (the execution status of this line is deduced): d->filter = table_filter;
-
438 -
439 if (stmt.isEmpty())
partially evaluated: stmt.isEmpty()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:56
0-56
440 return false;
never executed: return false;
0
441 -
442 bool exists;
executed (the execution status of this line is deduced): bool exists;
-
443 QSqlRecord newValues;
executed (the execution status of this line is deduced): QSqlRecord newValues;
-
444 -
445 { -
446 QSqlQuery q(d->db);
executed (the execution status of this line is deduced): QSqlQuery q(d->db);
-
447 q.setForwardOnly(true);
executed (the execution status of this line is deduced): q.setForwardOnly(true);
-
448 if (!q.exec(stmt))
partially evaluated: !q.exec(stmt)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:56
0-56
449 return false;
never executed: return false;
0
450 -
451 exists = q.next();
executed (the execution status of this line is deduced): exists = q.next();
-
452 newValues = q.record();
executed (the execution status of this line is deduced): newValues = q.record();
-
453 } -
454 -
455 bool needsAddingToCache = !exists || d->cache.contains(row);
evaluated: !exists
TRUEFALSE
yes
Evaluation Count:9
yes
Evaluation Count:47
evaluated: d->cache.contains(row)
TRUEFALSE
yes
Evaluation Count:45
yes
Evaluation Count:2
2-47
456 -
457 if (!needsAddingToCache) {
evaluated: !needsAddingToCache
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:54
2-54
458 const QSqlRecord curValues = record(row);
executed (the execution status of this line is deduced): const QSqlRecord curValues = record(row);
-
459 needsAddingToCache = curValues.count() != newValues.count();
executed (the execution status of this line is deduced): needsAddingToCache = curValues.count() != newValues.count();
-
460 if (!needsAddingToCache) {
partially evaluated: !needsAddingToCache
TRUEFALSE
yes
Evaluation Count:2
no
Evaluation Count:0
0-2
461 // Look for changed values. Primary key fields are customarily first -
462 // and probably change less often than other fields, so start at the end. -
463 for (int f = curValues.count() - 1; f >= 0; --f) {
partially evaluated: f >= 0
TRUEFALSE
yes
Evaluation Count:2
no
Evaluation Count:0
0-2
464 if (curValues.value(f) != newValues.value(f))
evaluated: curValues.value(f) != newValues.value(f)
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:1
1
465 needsAddingToCache = true;
executed: needsAddingToCache = true;
Execution Count:1
1
466 break;
executed: break;
Execution Count:2
2
467 } -
468 }
executed: }
Execution Count:2
2
469 }
executed: }
Execution Count:2
2
470 -
471 if (needsAddingToCache) {
evaluated: needsAddingToCache
TRUEFALSE
yes
Evaluation Count:55
yes
Evaluation Count:1
1-55
472 d->cache[row].refresh(exists, newValues);
executed (the execution status of this line is deduced): d->cache[row].refresh(exists, newValues);
-
473 emit headerDataChanged(Qt::Vertical, row, row);
executed (the execution status of this line is deduced): headerDataChanged(Qt::Vertical, row, row);
-
474 emit dataChanged(createIndex(row, 0), createIndex(row, columnCount() - 1));
executed (the execution status of this line is deduced): dataChanged(createIndex(row, 0), createIndex(row, columnCount() - 1));
-
475 }
executed: }
Execution Count:55
55
476 -
477 return true;
executed: return true;
Execution Count:56
56
478} -
479 -
480/*! -
481 \reimp -
482*/ -
483QVariant QSqlTableModel::data(const QModelIndex &index, int role) const -
484{ -
485 Q_D(const QSqlTableModel);
executed (the execution status of this line is deduced): const QSqlTableModelPrivate * const d = d_func();
-
486 if (!index.isValid() || (role != Qt::DisplayRole && role != Qt::EditRole))
evaluated: !index.isValid()
TRUEFALSE
yes
Evaluation Count:318
yes
Evaluation Count:2535
evaluated: role != Qt::DisplayRole
TRUEFALSE
yes
Evaluation Count:827
yes
Evaluation Count:1708
evaluated: role != Qt::EditRole
TRUEFALSE
yes
Evaluation Count:9
yes
Evaluation Count:818
9-2535
487 return QVariant();
executed: return QVariant();
Execution Count:327
327
488 -
489 const QSqlTableModelPrivate::ModifiedRow mrow = d->cache.value(index.row());
executed (the execution status of this line is deduced): const QSqlTableModelPrivate::ModifiedRow mrow = d->cache.value(index.row());
-
490 if (mrow.op() != QSqlTableModelPrivate::None)
evaluated: mrow.op() != QSqlTableModelPrivate::None
TRUEFALSE
yes
Evaluation Count:358
yes
Evaluation Count:2168
358-2168
491 return mrow.rec().value(index.column());
executed: return mrow.rec().value(index.column());
Execution Count:358
358
492 -
493 return QSqlQueryModel::data(index, role);
executed: return QSqlQueryModel::data(index, role);
Execution Count:2168
2168
494} -
495 -
496/*! -
497 \reimp -
498*/ -
499QVariant QSqlTableModel::headerData(int section, Qt::Orientation orientation, int role) const -
500{ -
501 Q_D(const QSqlTableModel);
executed (the execution status of this line is deduced): const QSqlTableModelPrivate * const d = d_func();
-
502 if (orientation == Qt::Vertical && role == Qt::DisplayRole) {
partially evaluated: orientation == Qt::Vertical
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:2
never evaluated: role == Qt::DisplayRole
0-2
503 const QSqlTableModelPrivate::Op op = d->cache.value(section).op();
never executed (the execution status of this line is deduced): const QSqlTableModelPrivate::Op op = d->cache.value(section).op();
-
504 if (op == QSqlTableModelPrivate::Insert)
never evaluated: op == QSqlTableModelPrivate::Insert
0
505 return QLatin1String("*");
never executed: return QLatin1String("*");
0
506 else if (op == QSqlTableModelPrivate::Delete)
never evaluated: op == QSqlTableModelPrivate::Delete
0
507 return QLatin1String("!");
never executed: return QLatin1String("!");
0
508 } -
509 return QSqlQueryModel::headerData(section, orientation, role);
executed: return QSqlQueryModel::headerData(section, orientation, role);
Execution Count:2
2
510} -
511 -
512/*! -
513 \overload -
514 \since 5.0 -
515 -
516 Returns true if the model contains modified values that have not been -
517 committed to the datase, otherwise false. -
518*/ -
519bool QSqlTableModel::isDirty() const -
520{ -
521 Q_D(const QSqlTableModel);
executed (the execution status of this line is deduced): const QSqlTableModelPrivate * const d = d_func();
-
522 QSqlTableModelPrivate::CacheMap::ConstIterator i = d->cache.constBegin();
executed (the execution status of this line is deduced): QSqlTableModelPrivate::CacheMap::ConstIterator i = d->cache.constBegin();
-
523 const QSqlTableModelPrivate::CacheMap::ConstIterator e = d->cache.constEnd();
executed (the execution status of this line is deduced): const QSqlTableModelPrivate::CacheMap::ConstIterator e = d->cache.constEnd();
-
524 for (; i != e; i++)
evaluated: i != e
TRUEFALSE
yes
Evaluation Count:58
yes
Evaluation Count:124
58-124
525 if (!i.value().submitted())
evaluated: !i.value().submitted()
TRUEFALSE
yes
Evaluation Count:28
yes
Evaluation Count:30
28-30
526 return true;
executed: return true;
Execution Count:28
28
527 return false;
executed: return false;
Execution Count:124
124
528} -
529 -
530/*! -
531 Returns true if the value at the index \a index is dirty, otherwise false. -
532 Dirty values are values that were modified in the model -
533 but not yet written into the database. -
534 -
535 If \a index is invalid or points to a non-existing row, false is returned. -
536*/ -
537bool QSqlTableModel::isDirty(const QModelIndex &index) const -
538{ -
539 Q_D(const QSqlTableModel);
executed (the execution status of this line is deduced): const QSqlTableModelPrivate * const d = d_func();
-
540 if (!index.isValid())
partially evaluated: !index.isValid()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:68
0-68
541 return false;
never executed: return false;
0
542 -
543 const QSqlTableModelPrivate::ModifiedRow row = d->cache.value(index.row());
executed (the execution status of this line is deduced): const QSqlTableModelPrivate::ModifiedRow row = d->cache.value(index.row());
-
544 if (row.submitted())
evaluated: row.submitted()
TRUEFALSE
yes
Evaluation Count:53
yes
Evaluation Count:15
15-53
545 return false;
executed: return false;
Execution Count:53
53
546 -
547 return row.op() == QSqlTableModelPrivate::Insert
executed: return row.op() == QSqlTableModelPrivate::Insert || row.op() == QSqlTableModelPrivate::Delete || (row.op() == QSqlTableModelPrivate::Update && row.rec().isGenerated(index.column()));
Execution Count:15
15
548 || row.op() == QSqlTableModelPrivate::Delete
executed: return row.op() == QSqlTableModelPrivate::Insert || row.op() == QSqlTableModelPrivate::Delete || (row.op() == QSqlTableModelPrivate::Update && row.rec().isGenerated(index.column()));
Execution Count:15
15
549 || (row.op() == QSqlTableModelPrivate::Update
executed: return row.op() == QSqlTableModelPrivate::Insert || row.op() == QSqlTableModelPrivate::Delete || (row.op() == QSqlTableModelPrivate::Update && row.rec().isGenerated(index.column()));
Execution Count:15
15
550 && row.rec().isGenerated(index.column()));
executed: return row.op() == QSqlTableModelPrivate::Insert || row.op() == QSqlTableModelPrivate::Delete || (row.op() == QSqlTableModelPrivate::Update && row.rec().isGenerated(index.column()));
Execution Count:15
15
551} -
552 -
553/*! -
554 Sets the data for the item \a index for the role \a role to \a -
555 value. -
556 -
557 For edit strategy OnFieldChange, an index may receive a change -
558 only if no other index has a cached change. Changes are -
559 submitted immediately. However, rows that have not yet been -
560 inserted in the database may be freely changed and are not -
561 submitted automatically. Submitted changes are not reverted upon -
562 failure. -
563 -
564 For OnRowChange, an index may receive a change only if no other -
565 row has a cached change. Changes are not submitted automatically. -
566 -
567 Returns true if \a value is equal to the current value. However, -
568 the value will not be submitted to the database. -
569 -
570 Returns true if the value could be set or false on error, for -
571 example if \a index is out of bounds. -
572 -
573 \sa editStrategy(), data(), submit(), submitAll(), revertRow() -
574*/ -
575bool QSqlTableModel::setData(const QModelIndex &index, const QVariant &value, int role) -
576{ -
577 Q_D(QSqlTableModel);
executed (the execution status of this line is deduced): QSqlTableModelPrivate * const d = d_func();
-
578 if (d->busyInsertingRows)
partially evaluated: d->busyInsertingRows
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:250
0-250
579 return false;
never executed: return false;
0
580 -
581 if (role != Qt::EditRole)
evaluated: role != Qt::EditRole
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:248
2-248
582 return QSqlQueryModel::setData(index, value, role);
executed: return QSqlQueryModel::setData(index, value, role);
Execution Count:2
2
583 -
584 if (!index.isValid() || index.column() >= d->rec.count() || index.row() >= rowCount())
evaluated: !index.isValid()
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:245
partially evaluated: index.column() >= d->rec.count()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:245
partially evaluated: index.row() >= rowCount()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:245
0-245
585 return false;
executed: return false;
Execution Count:3
3
586 -
587 if (!(flags(index) & Qt::ItemIsEditable))
evaluated: !(flags(index) & Qt::ItemIsEditable)
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:242
3-242
588 return false;
executed: return false;
Execution Count:3
3
589 -
590 if (QSqlTableModel::data(index, role) == value)
evaluated: QSqlTableModel::data(index, role) == value
TRUEFALSE
yes
Evaluation Count:24
yes
Evaluation Count:218
24-218
591 return true;
executed: return true;
Execution Count:24
24
592 -
593 QSqlTableModelPrivate::ModifiedRow &row = d->cache[index.row()];
executed (the execution status of this line is deduced): QSqlTableModelPrivate::ModifiedRow &row = d->cache[index.row()];
-
594 -
595 if (row.op() == QSqlTableModelPrivate::None)
evaluated: row.op() == QSqlTableModelPrivate::None
TRUEFALSE
yes
Evaluation Count:41
yes
Evaluation Count:177
41-177
596 row = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Update,
executed: row = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Update, QSqlQueryModel::record(index.row()));
Execution Count:41
41
597 QSqlQueryModel::record(index.row()));
executed: row = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Update, QSqlQueryModel::record(index.row()));
Execution Count:41
41
598 -
599 row.setValue(index.column(), value);
executed (the execution status of this line is deduced): row.setValue(index.column(), value);
-
600 emit dataChanged(index, index);
executed (the execution status of this line is deduced): dataChanged(index, index);
-
601 -
602 if (d->strategy == OnFieldChange && row.op() != QSqlTableModelPrivate::Insert)
evaluated: d->strategy == OnFieldChange
TRUEFALSE
yes
Evaluation Count:12
yes
Evaluation Count:206
evaluated: row.op() != QSqlTableModelPrivate::Insert
TRUEFALSE
yes
Evaluation Count:7
yes
Evaluation Count:5
5-206
603 return submit();
executed: return submit();
Execution Count:7
7
604 -
605 return true;
executed: return true;
Execution Count:211
211
606} -
607 -
608/*! -
609 This function simply calls QSqlQueryModel::setQuery(\a query). -
610 You should normally not call it on a QSqlTableModel. Instead, use -
611 setTable(), setSort(), setFilter(), etc., to set up the query. -
612 -
613 \sa selectStatement() -
614*/ -
615void QSqlTableModel::setQuery(const QSqlQuery &query) -
616{ -
617 QSqlQueryModel::setQuery(query);
executed (the execution status of this line is deduced): QSqlQueryModel::setQuery(query);
-
618}
executed: }
Execution Count:297
297
619 -
620/*! -
621 Updates the given \a row in the currently active database table -
622 with the specified \a values. Returns true if successful; otherwise -
623 returns false. -
624 -
625 This is a low-level method that operates directly on the database -
626 and should not be called directly. Use setData() to update values. -
627 The model will decide depending on its edit strategy when to modify -
628 the database. -
629 -
630 Note that only values that have the generated-flag set are updated. -
631 The generated-flag can be set with QSqlRecord::setGenerated() and -
632 tested with QSqlRecord::isGenerated(). -
633 -
634 \sa QSqlRecord::isGenerated(), setData() -
635*/ -
636bool QSqlTableModel::updateRowInTable(int row, const QSqlRecord &values) -
637{ -
638 Q_D(QSqlTableModel);
executed (the execution status of this line is deduced): QSqlTableModelPrivate * const d = d_func();
-
639 QSqlRecord rec(values);
executed (the execution status of this line is deduced): QSqlRecord rec(values);
-
640 emit beforeUpdate(row, rec);
executed (the execution status of this line is deduced): beforeUpdate(row, rec);
-
641 -
642 const QSqlRecord whereValues = d->primaryValues(row);
executed (the execution status of this line is deduced): const QSqlRecord whereValues = d->primaryValues(row);
-
643 const bool prepStatement = d->db.driver()->hasFeature(QSqlDriver::PreparedQueries);
executed (the execution status of this line is deduced): const bool prepStatement = d->db.driver()->hasFeature(QSqlDriver::PreparedQueries);
-
644 const QString stmt = d->db.driver()->sqlStatement(QSqlDriver::UpdateStatement, d->tableName,
executed (the execution status of this line is deduced): const QString stmt = d->db.driver()->sqlStatement(QSqlDriver::UpdateStatement, d->tableName,
-
645 rec, prepStatement);
executed (the execution status of this line is deduced): rec, prepStatement);
-
646 const QString where = d->db.driver()->sqlStatement(QSqlDriver::WhereStatement, d->tableName,
executed (the execution status of this line is deduced): const QString where = d->db.driver()->sqlStatement(QSqlDriver::WhereStatement, d->tableName,
-
647 whereValues, prepStatement);
executed (the execution status of this line is deduced): whereValues, prepStatement);
-
648 -
649 if (stmt.isEmpty() || where.isEmpty() || row < 0 || row >= rowCount()) {
partially evaluated: stmt.isEmpty()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:45
partially evaluated: where.isEmpty()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:45
partially evaluated: row < 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:45
partially evaluated: row >= rowCount()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:45
0-45
650 d->error = QSqlError(QLatin1String("No Fields to update"), QString(),
never executed (the execution status of this line is deduced): d->error = QSqlError(QLatin1String("No Fields to update"), QString(),
-
651 QSqlError::StatementError);
never executed (the execution status of this line is deduced): QSqlError::StatementError);
-
652 return false;
never executed: return false;
0
653 } -
654 -
655 return d->exec(Sql::concat(stmt, where), prepStatement, rec, whereValues);
executed: return d->exec(Sql::concat(stmt, where), prepStatement, rec, whereValues);
Execution Count:45
45
656} -
657 -
658 -
659/*! -
660 Inserts the values \a values into the currently active database table. -
661 -
662 This is a low-level method that operates directly on the database -
663 and should not be called directly. Use insertRow() and setData() -
664 to insert values. The model will decide depending on its edit strategy -
665 when to modify the database. -
666 -
667 Returns true if the values could be inserted, otherwise false. -
668 Error information can be retrieved with \l lastError(). -
669 -
670 \sa lastError(), insertRow(), insertRows() -
671*/ -
672bool QSqlTableModel::insertRowIntoTable(const QSqlRecord &values) -
673{ -
674 Q_D(QSqlTableModel);
executed (the execution status of this line is deduced): QSqlTableModelPrivate * const d = d_func();
-
675 QSqlRecord rec = values;
executed (the execution status of this line is deduced): QSqlRecord rec = values;
-
676 emit beforeInsert(rec);
executed (the execution status of this line is deduced): beforeInsert(rec);
-
677 -
678 const bool prepStatement = d->db.driver()->hasFeature(QSqlDriver::PreparedQueries);
executed (the execution status of this line is deduced): const bool prepStatement = d->db.driver()->hasFeature(QSqlDriver::PreparedQueries);
-
679 const QString stmt = d->db.driver()->sqlStatement(QSqlDriver::InsertStatement, d->tableName,
executed (the execution status of this line is deduced): const QString stmt = d->db.driver()->sqlStatement(QSqlDriver::InsertStatement, d->tableName,
-
680 rec, prepStatement);
executed (the execution status of this line is deduced): rec, prepStatement);
-
681 -
682 if (stmt.isEmpty()) {
evaluated: stmt.isEmpty()
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:42
1-42
683 d->error = QSqlError(QLatin1String("No Fields to update"), QString(),
executed (the execution status of this line is deduced): d->error = QSqlError(QLatin1String("No Fields to update"), QString(),
-
684 QSqlError::StatementError);
executed (the execution status of this line is deduced): QSqlError::StatementError);
-
685 return false;
executed: return false;
Execution Count:1
1
686 } -
687 -
688 return d->exec(stmt, prepStatement, rec, QSqlRecord() /* no where values */);
executed: return d->exec(stmt, prepStatement, rec, QSqlRecord() );
Execution Count:42
42
689} -
690 -
691/*! -
692 Deletes the given \a row from the currently active database table. -
693 -
694 This is a low-level method that operates directly on the database -
695 and should not be called directly. Use removeRow() or removeRows() -
696 to delete values. The model will decide depending on its edit strategy -
697 when to modify the database. -
698 -
699 Returns true if the row was deleted; otherwise returns false. -
700 -
701 \sa removeRow(), removeRows() -
702*/ -
703bool QSqlTableModel::deleteRowFromTable(int row) -
704{ -
705 Q_D(QSqlTableModel);
executed (the execution status of this line is deduced): QSqlTableModelPrivate * const d = d_func();
-
706 emit beforeDelete(row);
executed (the execution status of this line is deduced): beforeDelete(row);
-
707 -
708 const QSqlRecord whereValues = d->primaryValues(row);
executed (the execution status of this line is deduced): const QSqlRecord whereValues = d->primaryValues(row);
-
709 const bool prepStatement = d->db.driver()->hasFeature(QSqlDriver::PreparedQueries);
executed (the execution status of this line is deduced): const bool prepStatement = d->db.driver()->hasFeature(QSqlDriver::PreparedQueries);
-
710 const QString stmt = d->db.driver()->sqlStatement(QSqlDriver::DeleteStatement,
executed (the execution status of this line is deduced): const QString stmt = d->db.driver()->sqlStatement(QSqlDriver::DeleteStatement,
-
711 d->tableName,
executed (the execution status of this line is deduced): d->tableName,
-
712 QSqlRecord(),
executed (the execution status of this line is deduced): QSqlRecord(),
-
713 prepStatement);
executed (the execution status of this line is deduced): prepStatement);
-
714 const QString where = d->db.driver()->sqlStatement(QSqlDriver::WhereStatement,
executed (the execution status of this line is deduced): const QString where = d->db.driver()->sqlStatement(QSqlDriver::WhereStatement,
-
715 d->tableName,
executed (the execution status of this line is deduced): d->tableName,
-
716 whereValues,
executed (the execution status of this line is deduced): whereValues,
-
717 prepStatement);
executed (the execution status of this line is deduced): prepStatement);
-
718 -
719 if (stmt.isEmpty() || where.isEmpty()) {
partially evaluated: stmt.isEmpty()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:20
partially evaluated: where.isEmpty()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:20
0-20
720 d->error = QSqlError(QLatin1String("Unable to delete row"), QString(),
never executed (the execution status of this line is deduced): d->error = QSqlError(QLatin1String("Unable to delete row"), QString(),
-
721 QSqlError::StatementError);
never executed (the execution status of this line is deduced): QSqlError::StatementError);
-
722 return false;
never executed: return false;
0
723 } -
724 -
725 return d->exec(Sql::concat(stmt, where), prepStatement, QSqlRecord() /* no new values */, whereValues);
executed: return d->exec(Sql::concat(stmt, where), prepStatement, QSqlRecord() , whereValues);
Execution Count:20
20
726} -
727 -
728/*! -
729 Submits all pending changes and returns true on success. -
730 Returns false on error, detailed error information can be -
731 obtained with lastError(). -
732 -
733 In OnManualSubmit, on success the model will be repopulated. -
734 Any views presenting it will lose their selections. -
735 -
736 Note: In OnManualSubmit mode, already submitted changes won't -
737 be cleared from the cache when submitAll() fails. This allows -
738 transactions to be rolled back and resubmitted without -
739 losing data. -
740 -
741 \sa revertAll(), lastError() -
742*/ -
743bool QSqlTableModel::submitAll() -
744{ -
745 Q_D(QSqlTableModel);
executed (the execution status of this line is deduced): QSqlTableModelPrivate * const d = d_func();
-
746 -
747 bool success = true;
executed (the execution status of this line is deduced): bool success = true;
-
748 -
749 foreach (int row, d->cache.keys()) {
executed (the execution status of this line is deduced): for (QForeachContainer<__typeof__(d->cache.keys())> _container_(d->cache.keys()); !_container_.brk && _container_.i != _container_.e; __extension__ ({ ++_container_.brk; ++_container_.i; })) for (int row = *_container_.i;; __extension__ ({--_container_.brk; break;})) {
-
750 // be sure cache *still* contains the row since overriden selectRow() could have called select() -
751 QSqlTableModelPrivate::CacheMap::iterator it = d->cache.find(row);
executed (the execution status of this line is deduced): QSqlTableModelPrivate::CacheMap::iterator it = d->cache.find(row);
-
752 if (it == d->cache.end())
partially evaluated: it == d->cache.end()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:138
0-138
753 continue;
never executed: continue;
0
754 -
755 QSqlTableModelPrivate::ModifiedRow &mrow = it.value();
executed (the execution status of this line is deduced): QSqlTableModelPrivate::ModifiedRow &mrow = it.value();
-
756 if (mrow.submitted())
evaluated: mrow.submitted()
TRUEFALSE
yes
Evaluation Count:30
yes
Evaluation Count:108
30-108
757 continue;
executed: continue;
Execution Count:30
30
758 -
759 switch (mrow.op()) { -
760 case QSqlTableModelPrivate::Insert: -
761 success = insertRowIntoTable(mrow.rec());
executed (the execution status of this line is deduced): success = insertRowIntoTable(mrow.rec());
-
762 break;
executed: break;
Execution Count:43
43
763 case QSqlTableModelPrivate::Update: -
764 success = updateRowInTable(row, mrow.rec());
executed (the execution status of this line is deduced): success = updateRowInTable(row, mrow.rec());
-
765 break;
executed: break;
Execution Count:45
45
766 case QSqlTableModelPrivate::Delete: -
767 success = deleteRowFromTable(row);
executed (the execution status of this line is deduced): success = deleteRowFromTable(row);
-
768 break;
executed: break;
Execution Count:20
20
769 case QSqlTableModelPrivate::None: -
770 Q_ASSERT_X(false, "QSqlTableModel::submitAll()", "Invalid cache operation");
never executed (the execution status of this line is deduced): qt_noop();
-
771 break;
never executed: break;
0
772 } -
773 -
774 if (success) {
evaluated: success
TRUEFALSE
yes
Evaluation Count:104
yes
Evaluation Count:4
4-104
775 mrow.setSubmitted();
executed (the execution status of this line is deduced): mrow.setSubmitted();
-
776 if (d->strategy != OnManualSubmit)
evaluated: d->strategy != OnManualSubmit
TRUEFALSE
yes
Evaluation Count:54
yes
Evaluation Count:50
50-54
777 success = selectRow(row);
executed: success = selectRow(row);
Execution Count:54
54
778 }
executed: }
Execution Count:104
104
779 -
780 if (!success)
evaluated: !success
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:104
4-104
781 break;
executed: break;
Execution Count:4
4
782 }
executed: }
Execution Count:104
104
783 -
784 if (success) {
evaluated: success
TRUEFALSE
yes
Evaluation Count:107
yes
Evaluation Count:4
4-107
785 if (d->strategy == OnManualSubmit)
evaluated: d->strategy == OnManualSubmit
TRUEFALSE
yes
Evaluation Count:31
yes
Evaluation Count:76
31-76
786 success = select();
executed: success = select();
Execution Count:31
31
787 }
executed: }
Execution Count:107
107
788 -
789 return success;
executed: return success;
Execution Count:111
111
790} -
791 -
792/*! -
793 This reimplemented slot is called by the item delegates when the -
794 user stopped editing the current row. -
795 -
796 Submits the currently edited row if the model's strategy is set -
797 to OnRowChange or OnFieldChange. Does nothing for the OnManualSubmit -
798 strategy. -
799 -
800 Use submitAll() to submit all pending changes for the -
801 OnManualSubmit strategy. -
802 -
803 Returns true on success; otherwise returns false. Use lastError() -
804 to query detailed error information. -
805 -
806 Does not automatically repopulate the model. Submitted rows are -
807 refreshed from the database on success. -
808 -
809 \sa revert(), revertRow(), submitAll(), revertAll(), lastError() -
810*/ -
811bool QSqlTableModel::submit() -
812{ -
813 Q_D(QSqlTableModel);
executed (the execution status of this line is deduced): QSqlTableModelPrivate * const d = d_func();
-
814 if (d->strategy == OnRowChange || d->strategy == OnFieldChange)
evaluated: d->strategy == OnRowChange
TRUEFALSE
yes
Evaluation Count:28
yes
Evaluation Count:20
evaluated: d->strategy == OnFieldChange
TRUEFALSE
yes
Evaluation Count:19
yes
Evaluation Count:1
1-28
815 return submitAll();
executed: return submitAll();
Execution Count:47
47
816 return true;
executed: return true;
Execution Count:1
1
817} -
818 -
819/*! -
820 This reimplemented slot is called by the item delegates when the -
821 user canceled editing the current row. -
822 -
823 Reverts the changes if the model's strategy is set to -
824 OnRowChange. Does nothing for the other edit strategies. -
825 -
826 Use revertAll() to revert all pending changes for the -
827 OnManualSubmit strategy or revertRow() to revert a specific row. -
828 -
829 \sa submit(), submitAll(), revertRow(), revertAll() -
830*/ -
831void QSqlTableModel::revert() -
832{ -
833 Q_D(QSqlTableModel);
executed (the execution status of this line is deduced): QSqlTableModelPrivate * const d = d_func();
-
834 if (d->strategy == OnRowChange)
partially evaluated: d->strategy == OnRowChange
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1
0-1
835 revertAll();
never executed: revertAll();
0
836}
executed: }
Execution Count:1
1
837 -
838/*! -
839 \enum QSqlTableModel::EditStrategy -
840 -
841 This enum type describes which strategy to choose when editing values in the database. -
842 -
843 \value OnFieldChange All changes to the model will be applied immediately to the database. -
844 \value OnRowChange Changes to a row will be applied when the user selects a different row. -
845 \value OnManualSubmit All changes will be cached in the model until either submitAll() -
846 or revertAll() is called. -
847 -
848 Note: To prevent inserting only partly initialized rows into the database, -
849 \c OnFieldChange will behave like \c OnRowChange for newly inserted rows. -
850 -
851 \sa setEditStrategy() -
852*/ -
853 -
854 -
855/*! -
856 Sets the strategy for editing values in the database to \a -
857 strategy. -
858 -
859 This will revert any pending changes. -
860 -
861 \sa editStrategy(), revertAll() -
862*/ -
863void QSqlTableModel::setEditStrategy(EditStrategy strategy) -
864{ -
865 Q_D(QSqlTableModel);
executed (the execution status of this line is deduced): QSqlTableModelPrivate * const d = d_func();
-
866 revertAll();
executed (the execution status of this line is deduced): revertAll();
-
867 d->strategy = strategy;
executed (the execution status of this line is deduced): d->strategy = strategy;
-
868}
executed: }
Execution Count:100
100
869 -
870/*! -
871 Returns the current edit strategy. -
872 -
873 \sa setEditStrategy() -
874*/ -
875QSqlTableModel::EditStrategy QSqlTableModel::editStrategy() const -
876{ -
877 Q_D(const QSqlTableModel);
executed (the execution status of this line is deduced): const QSqlTableModelPrivate * const d = d_func();
-
878 return d->strategy;
executed: return d->strategy;
Execution Count:13
13
879} -
880 -
881/*! -
882 Reverts all pending changes. -
883 -
884 \sa revert(), revertRow(), submitAll() -
885*/ -
886void QSqlTableModel::revertAll() -
887{ -
888 Q_D(QSqlTableModel);
executed (the execution status of this line is deduced): QSqlTableModelPrivate * const d = d_func();
-
889 -
890 const QList<int> rows(d->cache.keys());
executed (the execution status of this line is deduced): const QList<int> rows(d->cache.keys());
-
891 for (int i = rows.size() - 1; i >= 0; --i)
evaluated: i >= 0
TRUEFALSE
yes
Evaluation Count:28
yes
Evaluation Count:117
28-117
892 revertRow(rows.value(i));
executed: revertRow(rows.value(i));
Execution Count:28
28
893}
executed: }
Execution Count:117
117
894 -
895/*! -
896 Reverts all changes for the specified \a row. -
897 -
898 \sa revert(), revertAll(), submit(), submitAll() -
899*/ -
900void QSqlTableModel::revertRow(int row) -
901{ -
902 if (row < 0)
partially evaluated: row < 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:45
0-45
903 return;
never executed: return;
0
904 -
905 Q_D(QSqlTableModel);
executed (the execution status of this line is deduced): QSqlTableModelPrivate * const d = d_func();
-
906 d->revertCachedRow(row);
executed (the execution status of this line is deduced): d->revertCachedRow(row);
-
907}
executed: }
Execution Count:45
45
908 -
909/*! -
910 Returns the primary key for the current table, or an empty -
911 QSqlIndex if the table is not set or has no primary key. -
912 -
913 \sa setTable(), setPrimaryKey(), QSqlDatabase::primaryIndex() -
914*/ -
915QSqlIndex QSqlTableModel::primaryKey() const -
916{ -
917 Q_D(const QSqlTableModel);
executed (the execution status of this line is deduced): const QSqlTableModelPrivate * const d = d_func();
-
918 return d->primaryIndex;
executed: return d->primaryIndex;
Execution Count:1
1
919} -
920 -
921/*! -
922 Protected method that allows subclasses to set the primary key to -
923 \a key. -
924 -
925 Normally, the primary index is set automatically whenever you -
926 call setTable(). -
927 -
928 \sa primaryKey(), QSqlDatabase::primaryIndex() -
929*/ -
930void QSqlTableModel::setPrimaryKey(const QSqlIndex &key) -
931{ -
932 Q_D(QSqlTableModel);
never executed (the execution status of this line is deduced): QSqlTableModelPrivate * const d = d_func();
-
933 d->primaryIndex = key;
never executed (the execution status of this line is deduced): d->primaryIndex = key;
-
934}
never executed: }
0
935 -
936/*! -
937 Returns a pointer to the used QSqlDatabase or 0 if no database was set. -
938*/ -
939QSqlDatabase QSqlTableModel::database() const -
940{ -
941 Q_D(const QSqlTableModel);
executed (the execution status of this line is deduced): const QSqlTableModelPrivate * const d = d_func();
-
942 return d->db;
executed: return d->db;
Execution Count:447
447
943} -
944 -
945/*! -
946 Sorts the data by \a column with the sort order \a order. -
947 This will immediately select data, use setSort() -
948 to set a sort order without populating the model with data. -
949 -
950 \sa setSort(), select(), orderByClause() -
951*/ -
952void QSqlTableModel::sort(int column, Qt::SortOrder order) -
953{ -
954 setSort(column, order);
executed (the execution status of this line is deduced): setSort(column, order);
-
955 select();
executed (the execution status of this line is deduced): select();
-
956}
executed: }
Execution Count:12
12
957 -
958/*! -
959 Sets the sort order for \a column to \a order. This does not -
960 affect the current data, to refresh the data using the new -
961 sort order, call select(). -
962 -
963 \sa select(), orderByClause() -
964*/ -
965void QSqlTableModel::setSort(int column, Qt::SortOrder order) -
966{ -
967 Q_D(QSqlTableModel);
executed (the execution status of this line is deduced): QSqlTableModelPrivate * const d = d_func();
-
968 d->sortColumn = column;
executed (the execution status of this line is deduced): d->sortColumn = column;
-
969 d->sortOrder = order;
executed (the execution status of this line is deduced): d->sortOrder = order;
-
970}
executed: }
Execution Count:65
65
971 -
972/*! -
973 Returns an SQL \c{ORDER BY} clause based on the currently set -
974 sort order. -
975 -
976 \sa setSort(), selectStatement() -
977*/ -
978QString QSqlTableModel::orderByClause() const -
979{ -
980 Q_D(const QSqlTableModel);
executed (the execution status of this line is deduced): const QSqlTableModelPrivate * const d = d_func();
-
981 QSqlField f = d->rec.field(d->sortColumn);
executed (the execution status of this line is deduced): QSqlField f = d->rec.field(d->sortColumn);
-
982 if (!f.isValid())
evaluated: !f.isValid()
TRUEFALSE
yes
Evaluation Count:229
yes
Evaluation Count:120
120-229
983 return QString();
executed: return QString();
Execution Count:229
229
984 -
985 //we can safely escape the field because it would have been obtained from the database -
986 //and have the correct case -
987 QString field = d->db.driver()->escapeIdentifier(f.name(), QSqlDriver::FieldName);
executed (the execution status of this line is deduced): QString field = d->db.driver()->escapeIdentifier(f.name(), QSqlDriver::FieldName);
-
988 field.prepend(QLatin1Char('.')).prepend(d->tableName);
executed (the execution status of this line is deduced): field.prepend(QLatin1Char('.')).prepend(d->tableName);
-
989 field = d->sortOrder == Qt::AscendingOrder ? Sql::asc(field) : Sql::desc(field);
evaluated: d->sortOrder == Qt::AscendingOrder
TRUEFALSE
yes
Evaluation Count:115
yes
Evaluation Count:5
5-115
990 return Sql::orderBy(field);
executed: return Sql::orderBy(field);
Execution Count:120
120
991} -
992 -
993/*! -
994 Returns the index of the field \a fieldName, or -1 if no corresponding field -
995 exists in the model. -
996*/ -
997int QSqlTableModel::fieldIndex(const QString &fieldName) const -
998{ -
999 Q_D(const QSqlTableModel);
never executed (the execution status of this line is deduced): const QSqlTableModelPrivate * const d = d_func();
-
1000 return d->rec.indexOf(fieldName);
never executed: return d->rec.indexOf(fieldName);
0
1001} -
1002 -
1003/*! -
1004 Returns the SQL \c SELECT statement used internally to populate -
1005 the model. The statement includes the filter and the \c{ORDER BY} -
1006 clause. -
1007 -
1008 \sa filter(), orderByClause() -
1009*/ -
1010QString QSqlTableModel::selectStatement() const -
1011{ -
1012 Q_D(const QSqlTableModel);
executed (the execution status of this line is deduced): const QSqlTableModelPrivate * const d = d_func();
-
1013 if (d->tableName.isEmpty()) {
partially evaluated: d->tableName.isEmpty()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:270
0-270
1014 d->error = QSqlError(QLatin1String("No table name given"), QString(),
never executed (the execution status of this line is deduced): d->error = QSqlError(QLatin1String("No table name given"), QString(),
-
1015 QSqlError::StatementError);
never executed (the execution status of this line is deduced): QSqlError::StatementError);
-
1016 return QString();
never executed: return QString();
0
1017 } -
1018 if (d->rec.isEmpty()) {
evaluated: d->rec.isEmpty()
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:269
1-269
1019 d->error = QSqlError(QLatin1String("Unable to find table ") + d->tableName, QString(),
executed (the execution status of this line is deduced): d->error = QSqlError(QLatin1String("Unable to find table ") + d->tableName, QString(),
-
1020 QSqlError::StatementError);
executed (the execution status of this line is deduced): QSqlError::StatementError);
-
1021 return QString();
executed: return QString();
Execution Count:1
1
1022 } -
1023 -
1024 const QString stmt = d->db.driver()->sqlStatement(QSqlDriver::SelectStatement,
executed (the execution status of this line is deduced): const QString stmt = d->db.driver()->sqlStatement(QSqlDriver::SelectStatement,
-
1025 d->tableName,
executed (the execution status of this line is deduced): d->tableName,
-
1026 d->rec,
executed (the execution status of this line is deduced): d->rec,
-
1027 false);
executed (the execution status of this line is deduced): false);
-
1028 if (stmt.isEmpty()) {
partially evaluated: stmt.isEmpty()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:269
0-269
1029 d->error = QSqlError(QLatin1String("Unable to select fields from table ") + d->tableName,
never executed (the execution status of this line is deduced): d->error = QSqlError(QLatin1String("Unable to select fields from table ") + d->tableName,
-
1030 QString(), QSqlError::StatementError);
never executed (the execution status of this line is deduced): QString(), QSqlError::StatementError);
-
1031 return stmt;
never executed: return stmt;
0
1032 } -
1033 return Sql::concat(Sql::concat(stmt, Sql::where(d->filter)), orderByClause());
executed: return Sql::concat(Sql::concat(stmt, Sql::where(d->filter)), orderByClause());
Execution Count:269
269
1034} -
1035 -
1036/*! -
1037 Removes \a count columns from the \a parent model, starting at -
1038 index \a column. -
1039 -
1040 Returns if the columns were successfully removed; otherwise -
1041 returns false. -
1042 -
1043 \sa removeRows() -
1044*/ -
1045bool QSqlTableModel::removeColumns(int column, int count, const QModelIndex &parent) -
1046{ -
1047 Q_D(QSqlTableModel);
executed (the execution status of this line is deduced): QSqlTableModelPrivate * const d = d_func();
-
1048 if (parent.isValid() || column < 0 || column + count > d->rec.count())
partially evaluated: parent.isValid()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:5
partially evaluated: column < 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:5
partially evaluated: column + count > d->rec.count()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:5
0-5
1049 return false;
never executed: return false;
0
1050 for (int i = 0; i < count; ++i)
evaluated: i < count
TRUEFALSE
yes
Evaluation Count:7
yes
Evaluation Count:5
5-7
1051 d->rec.remove(column);
executed: d->rec.remove(column);
Execution Count:7
7
1052 if (d->query.isActive())
partially evaluated: d->query.isActive()
TRUEFALSE
yes
Evaluation Count:5
no
Evaluation Count:0
0-5
1053 return select();
executed: return select();
Execution Count:5
5
1054 return true;
never executed: return true;
0
1055} -
1056 -
1057/*! -
1058 Removes \a count rows starting at \a row. Since this model -
1059 does not support hierarchical structures, \a parent must be -
1060 an invalid model index. -
1061 -
1062 When the edit strategy is OnManualSubmit, deletion of rows from -
1063 the database is delayed until submitAll() is called. -
1064 -
1065 For OnFieldChange and OnRowChange, only one row may be deleted -
1066 at a time and only if no other row has a cached change. Deletions -
1067 are submitted immediately to the database. The model retains a -
1068 blank row for successfully deleted row until refreshed with select(). -
1069 -
1070 After failed deletion, the operation is not reverted in the model. -
1071 The application may resubmit or revert. -
1072 -
1073 Inserted but not yet successfully submitted rows in the range to be -
1074 removed are immediately removed from the model. -
1075 -
1076 Before a row is deleted from the database, the beforeDelete() -
1077 signal is emitted. -
1078 -
1079 If row < 0 or row + count > rowCount(), no action is taken and -
1080 false is returned. Returns true if all rows could be removed; -
1081 otherwise returns false. Detailed database error information -
1082 can be retrieved using lastError(). -
1083 -
1084 \sa removeColumns(), insertRows() -
1085*/ -
1086bool QSqlTableModel::removeRows(int row, int count, const QModelIndex &parent) -
1087{ -
1088 Q_D(QSqlTableModel);
executed (the execution status of this line is deduced): QSqlTableModelPrivate * const d = d_func();
-
1089 if (parent.isValid() || row < 0 || count <= 0)
evaluated: parent.isValid()
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:38
evaluated: row < 0
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:34
evaluated: count <= 0
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:32
2-38
1090 return false;
executed: return false;
Execution Count:8
8
1091 else if (row + count > rowCount())
evaluated: row + count > rowCount()
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:30
2-30
1092 return false;
executed: return false;
Execution Count:2
2
1093 else if (!count)
partially evaluated: !count
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:30
0-30
1094 return true;
never executed: return true;
0
1095 -
1096 if (d->strategy != OnManualSubmit)
evaluated: d->strategy != OnManualSubmit
TRUEFALSE
yes
Evaluation Count:15
yes
Evaluation Count:15
15
1097 if (count > 1 || (d->cache.value(row).submitted() && isDirty()))
evaluated: count > 1
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:14
evaluated: d->cache.value(row).submitted()
TRUEFALSE
yes
Evaluation Count:12
yes
Evaluation Count:2
evaluated: isDirty()
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:9
1-14
1098 return false;
executed: return false;
Execution Count:4
4
1099 -
1100 // Iterate backwards so we don't have to worry about removed rows causing -
1101 // higher cache entries to shift downwards. -
1102 for (int idx = row + count - 1; idx >= row; --idx) {
evaluated: idx >= row
TRUEFALSE
yes
Evaluation Count:34
yes
Evaluation Count:26
26-34
1103 QSqlTableModelPrivate::ModifiedRow& mrow = d->cache[idx];
executed (the execution status of this line is deduced): QSqlTableModelPrivate::ModifiedRow& mrow = d->cache[idx];
-
1104 if (mrow.op() == QSqlTableModelPrivate::Insert) {
evaluated: mrow.op() == QSqlTableModelPrivate::Insert
TRUEFALSE
yes
Evaluation Count:13
yes
Evaluation Count:21
13-21
1105 revertRow(idx);
executed (the execution status of this line is deduced): revertRow(idx);
-
1106 } else {
executed: }
Execution Count:13
13
1107 if (mrow.op() == QSqlTableModelPrivate::None)
evaluated: mrow.op() == QSqlTableModelPrivate::None
TRUEFALSE
yes
Evaluation Count:19
yes
Evaluation Count:2
2-19
1108 mrow = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Delete,
executed: mrow = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Delete, QSqlQueryModel::record(idx));
Execution Count:19
19
1109 QSqlQueryModel::record(idx));
executed: mrow = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Delete, QSqlQueryModel::record(idx));
Execution Count:19
19
1110 else -
1111 mrow.setOp(QSqlTableModelPrivate::Delete);
executed: mrow.setOp(QSqlTableModelPrivate::Delete);
Execution Count:2
2
1112 if (d->strategy == OnManualSubmit)
evaluated: d->strategy == OnManualSubmit
TRUEFALSE
yes
Evaluation Count:12
yes
Evaluation Count:9
9-12
1113 emit headerDataChanged(Qt::Vertical, idx, idx);
executed: headerDataChanged(Qt::Vertical, idx, idx);
Execution Count:12
12
1114 }
executed: }
Execution Count:21
21
1115 } -
1116 -
1117 if (d->strategy != OnManualSubmit)
evaluated: d->strategy != OnManualSubmit
TRUEFALSE
yes
Evaluation Count:11
yes
Evaluation Count:15
11-15
1118 return submit();
executed: return submit();
Execution Count:11
11
1119 -
1120 return true;
executed: return true;
Execution Count:15
15
1121} -
1122 -
1123/*! -
1124 Inserts \a count empty rows at position \a row. Note that \a -
1125 parent must be invalid, since this model does not support -
1126 parent-child relations. -
1127 -
1128 For edit strategies OnFieldChange and OnRowChange, only one row -
1129 may be inserted at a time and the model may not contain other -
1130 cached changes. -
1131 -
1132 The primeInsert() signal will be emitted for each new row. -
1133 Connect to it if you want to initialize the new row with default -
1134 values. -
1135 -
1136 Does not submit rows, regardless of edit strategy. -
1137 -
1138 Returns false if the parameters are out of bounds or the row cannot be -
1139 inserted; otherwise returns true. -
1140 -
1141 \sa primeInsert(), insertRecord() -
1142*/ -
1143bool QSqlTableModel::insertRows(int row, int count, const QModelIndex &parent) -
1144{ -
1145 Q_D(QSqlTableModel);
executed (the execution status of this line is deduced): QSqlTableModelPrivate * const d = d_func();
-
1146 if (row < 0 || count <= 0 || row > rowCount() || parent.isValid())
partially evaluated: row < 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:63
partially evaluated: count <= 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:63
partially evaluated: row > rowCount()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:63
partially evaluated: parent.isValid()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:63
0-63
1147 return false;
never executed: return false;
0
1148 -
1149 if (d->strategy != OnManualSubmit)
evaluated: d->strategy != OnManualSubmit
TRUEFALSE
yes
Evaluation Count:26
yes
Evaluation Count:37
26-37
1150 if (count != 1 || isDirty())
partially evaluated: count != 1
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:26
evaluated: isDirty()
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:23
0-26
1151 return false;
executed: return false;
Execution Count:3
3
1152 -
1153 d->busyInsertingRows = true;
executed (the execution status of this line is deduced): d->busyInsertingRows = true;
-
1154 beginInsertRows(parent, row, row + count - 1);
executed (the execution status of this line is deduced): beginInsertRows(parent, row, row + count - 1);
-
1155 -
1156 if (d->strategy != OnManualSubmit)
evaluated: d->strategy != OnManualSubmit
TRUEFALSE
yes
Evaluation Count:23
yes
Evaluation Count:37
23-37
1157 d->cache.empty();
executed: d->cache.empty();
Execution Count:23
23
1158 -
1159 if (!d->cache.isEmpty()) {
evaluated: !d->cache.isEmpty()
TRUEFALSE
yes
Evaluation Count:19
yes
Evaluation Count:41
19-41
1160 QMap<int, QSqlTableModelPrivate::ModifiedRow>::Iterator it = d->cache.end();
executed (the execution status of this line is deduced): QMap<int, QSqlTableModelPrivate::ModifiedRow>::Iterator it = d->cache.end();
-
1161 while (it != d->cache.begin() && (--it).key() >= row) {
evaluated: it != d->cache.begin()
TRUEFALSE
yes
Evaluation Count:20
yes
Evaluation Count:2
evaluated: (--it).key() >= row
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:17
2-20
1162 int oldKey = it.key();
executed (the execution status of this line is deduced): int oldKey = it.key();
-
1163 const QSqlTableModelPrivate::ModifiedRow oldValue = it.value();
executed (the execution status of this line is deduced): const QSqlTableModelPrivate::ModifiedRow oldValue = it.value();
-
1164 d->cache.erase(it);
executed (the execution status of this line is deduced): d->cache.erase(it);
-
1165 it = d->cache.insert(oldKey + count, oldValue);
executed (the execution status of this line is deduced): it = d->cache.insert(oldKey + count, oldValue);
-
1166 }
executed: }
Execution Count:3
3
1167 }
executed: }
Execution Count:19
19
1168 -
1169 for (int i = 0; i < count; ++i) {
evaluated: i < count
TRUEFALSE
yes
Evaluation Count:77
yes
Evaluation Count:60
60-77
1170 d->cache[row + i] = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Insert,
executed (the execution status of this line is deduced): d->cache[row + i] = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Insert,
-
1171 d->rec);
executed (the execution status of this line is deduced): d->rec);
-
1172 emit primeInsert(row + i, d->cache[row + i].recRef());
executed (the execution status of this line is deduced): primeInsert(row + i, d->cache[row + i].recRef());
-
1173 }
executed: }
Execution Count:77
77
1174 -
1175 endInsertRows();
executed (the execution status of this line is deduced): endInsertRows();
-
1176 d->busyInsertingRows = false;
executed (the execution status of this line is deduced): d->busyInsertingRows = false;
-
1177 return true;
executed: return true;
Execution Count:60
60
1178} -
1179 -
1180/*! -
1181 Inserts the \a record at position \a row. If \a row is negative, -
1182 the record will be appended to the end. Calls insertRows() and -
1183 setRecord() internally. -
1184 -
1185 Returns true if the record could be inserted, otherwise false. -
1186 -
1187 Changes are submitted immediately for OnFieldChange and -
1188 OnRowChange. Failure does not leave a new row in the model. -
1189 -
1190 \sa insertRows(), removeRows(), setRecord() -
1191*/ -
1192bool QSqlTableModel::insertRecord(int row, const QSqlRecord &record) -
1193{ -
1194 if (row < 0)
evaluated: row < 0
TRUEFALSE
yes
Evaluation Count:7
yes
Evaluation Count:19
7-19
1195 row = rowCount();
executed: row = rowCount();
Execution Count:7
7
1196 if (!insertRow(row, QModelIndex()))
partially evaluated: !insertRow(row, QModelIndex())
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:26
0-26
1197 return false;
never executed: return false;
0
1198 if (!setRecord(row, record)) {
partially evaluated: !setRecord(row, record)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:26
0-26
1199 revertRow(row);
never executed (the execution status of this line is deduced): revertRow(row);
-
1200 return false;
never executed: return false;
0
1201 } -
1202 return true;
executed: return true;
Execution Count:26
26
1203} -
1204 -
1205/*! \reimp -
1206*/ -
1207int QSqlTableModel::rowCount(const QModelIndex &parent) const -
1208{ -
1209 Q_D(const QSqlTableModel);
executed (the execution status of this line is deduced): const QSqlTableModelPrivate * const d = d_func();
-
1210 -
1211 if (parent.isValid())
evaluated: parent.isValid()
TRUEFALSE
yes
Evaluation Count:1028
yes
Evaluation Count:10597
1028-10597
1212 return 0;
executed: return 0;
Execution Count:1028
1028
1213 -
1214 return QSqlQueryModel::rowCount() + d->insertCount();
executed: return QSqlQueryModel::rowCount() + d->insertCount();
Execution Count:10597
10597
1215} -
1216 -
1217/*! -
1218 Returns the index of the value in the database result set for the -
1219 given \a item in the model. -
1220 -
1221 The return value is identical to \a item if no columns or rows -
1222 have been inserted, removed, or moved around. -
1223 -
1224 Returns an invalid model index if \a item is out of bounds or if -
1225 \a item does not point to a value in the result set. -
1226 -
1227 \sa QSqlQueryModel::indexInQuery() -
1228*/ -
1229QModelIndex QSqlTableModel::indexInQuery(const QModelIndex &item) const -
1230{ -
1231 Q_D(const QSqlTableModel);
executed (the execution status of this line is deduced): const QSqlTableModelPrivate * const d = d_func();
-
1232 if (d->cache.value(item.row()).insert())
partially evaluated: d->cache.value(item.row()).insert()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:2152
0-2152
1233 return QModelIndex();
never executed: return QModelIndex();
0
1234 -
1235 const int rowOffset = d->insertCount(item.row());
executed (the execution status of this line is deduced): const int rowOffset = d->insertCount(item.row());
-
1236 return QSqlQueryModel::indexInQuery(createIndex(item.row() - rowOffset, item.column(), item.internalPointer()));
executed: return QSqlQueryModel::indexInQuery(createIndex(item.row() - rowOffset, item.column(), item.internalPointer()));
Execution Count:2152
2152
1237} -
1238 -
1239/*! -
1240 Returns the currently set filter. -
1241 -
1242 \sa setFilter(), select() -
1243*/ -
1244QString QSqlTableModel::filter() const -
1245{ -
1246 Q_D(const QSqlTableModel);
executed (the execution status of this line is deduced): const QSqlTableModelPrivate * const d = d_func();
-
1247 return d->filter;
executed: return d->filter;
Execution Count:86
86
1248} -
1249 -
1250/*! -
1251 Sets the current filter to \a filter. -
1252 -
1253 The filter is a SQL \c WHERE clause without the keyword \c WHERE -
1254 (for example, \c{name='Josephine')}. -
1255 -
1256 If the model is already populated with data from a database, -
1257 the model re-selects it with the new filter. Otherwise, the filter -
1258 will be applied the next time select() is called. -
1259 -
1260 \sa filter(), select(), selectStatement(), orderByClause() -
1261*/ -
1262void QSqlTableModel::setFilter(const QString &filter) -
1263{ -
1264 Q_D(QSqlTableModel);
executed (the execution status of this line is deduced): QSqlTableModelPrivate * const d = d_func();
-
1265 d->filter = filter;
executed (the execution status of this line is deduced): d->filter = filter;
-
1266 if (d->query.isActive())
evaluated: d->query.isActive()
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:4
3-4
1267 select();
executed: select();
Execution Count:3
3
1268}
executed: }
Execution Count:7
7
1269 -
1270/*! \reimp -
1271*/ -
1272void QSqlTableModel::clear() -
1273{ -
1274 Q_D(QSqlTableModel);
executed (the execution status of this line is deduced): QSqlTableModelPrivate * const d = d_func();
-
1275 d->clear();
executed (the execution status of this line is deduced): d->clear();
-
1276 QSqlQueryModel::clear();
executed (the execution status of this line is deduced): QSqlQueryModel::clear();
-
1277}
executed: }
Execution Count:183
183
1278 -
1279/*! \reimp -
1280*/ -
1281Qt::ItemFlags QSqlTableModel::flags(const QModelIndex &index) const -
1282{ -
1283 Q_D(const QSqlTableModel);
executed (the execution status of this line is deduced): const QSqlTableModelPrivate * const d = d_func();
-
1284 if (index.internalPointer() || index.column() < 0 || index.column() >= d->rec.count()
partially evaluated: index.internalPointer()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:247
evaluated: index.column() < 0
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:246
partially evaluated: index.column() >= d->rec.count()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:246
0-247
1285 || index.row() < 0)
partially evaluated: index.row() < 0
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:246
0-246
1286 return 0;
executed: return 0;
Execution Count:1
1
1287 -
1288 bool editable = true;
executed (the execution status of this line is deduced): bool editable = true;
-
1289 -
1290 if (d->rec.field(index.column()).isReadOnly()) {
partially evaluated: d->rec.field(index.column()).isReadOnly()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:246
0-246
1291 editable = false;
never executed (the execution status of this line is deduced): editable = false;
-
1292 }
never executed: }
0
1293 else { -
1294 const QSqlTableModelPrivate::ModifiedRow mrow = d->cache.value(index.row());
executed (the execution status of this line is deduced): const QSqlTableModelPrivate::ModifiedRow mrow = d->cache.value(index.row());
-
1295 if (mrow.op() == QSqlTableModelPrivate::Delete) {
partially evaluated: mrow.op() == QSqlTableModelPrivate::Delete
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:246
0-246
1296 editable = false;
never executed (the execution status of this line is deduced): editable = false;
-
1297 }
never executed: }
0
1298 else if (d->strategy == OnFieldChange) {
evaluated: d->strategy == OnFieldChange
TRUEFALSE
yes
Evaluation Count:14
yes
Evaluation Count:232
14-232
1299 if (mrow.op() != QSqlTableModelPrivate::Insert)
evaluated: mrow.op() != QSqlTableModelPrivate::Insert
TRUEFALSE
yes
Evaluation Count:9
yes
Evaluation Count:5
5-9
1300 if (!isDirty(index) && isDirty())
partially evaluated: !isDirty(index)
TRUEFALSE
yes
Evaluation Count:9
no
Evaluation Count:0
evaluated: isDirty()
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:8
0-9
1301 editable = false;
executed: editable = false;
Execution Count:1
1
1302 }
executed: }
Execution Count:14
14
1303 else if (d->strategy == OnRowChange) {
evaluated: d->strategy == OnRowChange
TRUEFALSE
yes
Evaluation Count:45
yes
Evaluation Count:187
45-187
1304 if (mrow.submitted() && isDirty())
evaluated: mrow.submitted()
TRUEFALSE
yes
Evaluation Count:28
yes
Evaluation Count:17
evaluated: isDirty()
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:25
3-28
1305 editable = false;
executed: editable = false;
Execution Count:3
3
1306 }
executed: }
Execution Count:45
45
1307 } -
1308 -
1309 if (!editable)
evaluated: !editable
TRUEFALSE
yes
Evaluation Count:4
yes
Evaluation Count:242
4-242
1310 return QSqlQueryModel::flags(index);
executed: return QSqlQueryModel::flags(index);
Execution Count:4
4
1311 else -
1312 return QSqlQueryModel::flags(index) | Qt::ItemIsEditable;
executed: return QSqlQueryModel::flags(index) | Qt::ItemIsEditable;
Execution Count:242
242
1313} -
1314 -
1315QSqlRecord QSqlTableModel::record() const -
1316{ -
1317 return QSqlQueryModel::record();
executed: return QSqlQueryModel::record();
Execution Count:13
13
1318} -
1319 -
1320/*! -
1321\since 5.0 -
1322 Returns the record at \a row in the model. -
1323 -
1324 If \a row is the index of a valid row, the record -
1325 will be populated with values from that row. -
1326 -
1327 If the model is not initialized, an empty record will be -
1328 returned. -
1329 -
1330 \sa QSqlRecord::isEmpty() -
1331*/ -
1332QSqlRecord QSqlTableModel::record(int row) const -
1333{ -
1334 Q_D(const QSqlTableModel);
executed (the execution status of this line is deduced): const QSqlTableModelPrivate * const d = d_func();
-
1335 -
1336 // the query gets the values from virtual data() -
1337 QSqlRecord rec = QSqlQueryModel::record(row);
executed (the execution status of this line is deduced): QSqlRecord rec = QSqlQueryModel::record(row);
-
1338 -
1339 // get generated flags from the cache -
1340 const QSqlTableModelPrivate::ModifiedRow mrow = d->cache.value(row);
executed (the execution status of this line is deduced): const QSqlTableModelPrivate::ModifiedRow mrow = d->cache.value(row);
-
1341 if (mrow.op() != QSqlTableModelPrivate::None) {
evaluated: mrow.op() != QSqlTableModelPrivate::None
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:160
3-160
1342 const QSqlRecord crec = mrow.rec();
executed (the execution status of this line is deduced): const QSqlRecord crec = mrow.rec();
-
1343 for (int i = 0, cnt = rec.count(); i < cnt; ++i)
evaluated: i < cnt
TRUEFALSE
yes
Evaluation Count:9
yes
Evaluation Count:3
3-9
1344 rec.setGenerated(i, crec.isGenerated(i));
executed: rec.setGenerated(i, crec.isGenerated(i));
Execution Count:9
9
1345 }
executed: }
Execution Count:3
3
1346 -
1347 return rec;
executed: return rec;
Execution Count:163
163
1348} -
1349 -
1350/*! -
1351 Applies \a values to the \a row in the model. The source and -
1352 target fields are mapped by field name, not by position in -
1353 the record. -
1354 -
1355 Note that the generated flags in \a values are preserved -
1356 and determine whether the corresponding fields are used when -
1357 changes are submitted to the database. The caller should -
1358 remember to set the generated flag to FALSE for fields -
1359 where the database is meant to supply the value, such as an -
1360 automatically incremented ID. -
1361 -
1362 For edit strategies OnFieldChange and OnRowChange, a row may -
1363 receive a change only if no other row has a cached change. -
1364 Changes are submitted immediately. Submitted changes are not -
1365 reverted upon failure. -
1366 -
1367 Returns true if all the values could be set; otherwise returns -
1368 false. -
1369 -
1370 \sa record(), editStrategy() -
1371*/ -
1372bool QSqlTableModel::setRecord(int row, const QSqlRecord &values) -
1373{ -
1374 Q_D(QSqlTableModel);
executed (the execution status of this line is deduced): QSqlTableModelPrivate * const d = d_func();
-
1375 Q_ASSERT_X(row >= 0, "QSqlTableModel::setRecord()", "Cannot set a record to a row less than 0");
executed (the execution status of this line is deduced): qt_noop();
-
1376 if (d->busyInsertingRows)
partially evaluated: d->busyInsertingRows
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:47
0-47
1377 return false;
never executed: return false;
0
1378 -
1379 if (row >= rowCount())
partially evaluated: row >= rowCount()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:47
0-47
1380 return false;
never executed: return false;
0
1381 -
1382 if (d->cache.value(row).op() == QSqlTableModelPrivate::Delete)
partially evaluated: d->cache.value(row).op() == QSqlTableModelPrivate::Delete
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:47
0-47
1383 return false;
never executed: return false;
0
1384 -
1385 if (d->strategy != OnManualSubmit && d->cache.value(row).submitted() && isDirty())
evaluated: d->strategy != OnManualSubmit
TRUEFALSE
yes
Evaluation Count:22
yes
Evaluation Count:25
evaluated: d->cache.value(row).submitted()
TRUEFALSE
yes
Evaluation Count:10
yes
Evaluation Count:12
evaluated: isDirty()
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:7
3-25
1386 return false;
executed: return false;
Execution Count:3
3
1387 -
1388 // Check field names and remember mapping -
1389 typedef QMap<int, int> Map;
executed (the execution status of this line is deduced): typedef QMap<int, int> Map;
-
1390 Map map;
executed (the execution status of this line is deduced): Map map;
-
1391 for (int i = 0; i < values.count(); ++i) {
evaluated: i < values.count()
TRUEFALSE
yes
Evaluation Count:126
yes
Evaluation Count:44
44-126
1392 int idx = d->nameToIndex(values.fieldName(i));
executed (the execution status of this line is deduced): int idx = d->nameToIndex(values.fieldName(i));
-
1393 if (idx == -1)
partially evaluated: idx == -1
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:126
0-126
1394 return false;
never executed: return false;
0
1395 map[i] = idx;
executed (the execution status of this line is deduced): map[i] = idx;
-
1396 }
executed: }
Execution Count:126
126
1397 -
1398 QSqlTableModelPrivate::ModifiedRow &mrow = d->cache[row];
executed (the execution status of this line is deduced): QSqlTableModelPrivate::ModifiedRow &mrow = d->cache[row];
-
1399 if (mrow.op() == QSqlTableModelPrivate::None)
evaluated: mrow.op() == QSqlTableModelPrivate::None
TRUEFALSE
yes
Evaluation Count:13
yes
Evaluation Count:31
13-31
1400 mrow = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Update,
executed: mrow = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Update, QSqlQueryModel::record(row));
Execution Count:13
13
1401 QSqlQueryModel::record(row));
executed: mrow = QSqlTableModelPrivate::ModifiedRow(QSqlTableModelPrivate::Update, QSqlQueryModel::record(row));
Execution Count:13
13
1402 -
1403 Map::const_iterator i = map.constBegin();
executed (the execution status of this line is deduced): Map::const_iterator i = map.constBegin();
-
1404 const Map::const_iterator e = map.constEnd();
executed (the execution status of this line is deduced): const Map::const_iterator e = map.constEnd();
-
1405 for ( ; i != e; ++i) {
evaluated: i != e
TRUEFALSE
yes
Evaluation Count:126
yes
Evaluation Count:44
44-126
1406 // have to use virtual setData() here rather than mrow.setValue() -
1407 EditStrategy strategy = d->strategy;
executed (the execution status of this line is deduced): EditStrategy strategy = d->strategy;
-
1408 d->strategy = OnManualSubmit;
executed (the execution status of this line is deduced): d->strategy = OnManualSubmit;
-
1409 QModelIndex cIndex = createIndex(row, i.value());
executed (the execution status of this line is deduced): QModelIndex cIndex = createIndex(row, i.value());
-
1410 setData(cIndex, values.value(i.key()));
executed (the execution status of this line is deduced): setData(cIndex, values.value(i.key()));
-
1411 d->strategy = strategy;
executed (the execution status of this line is deduced): d->strategy = strategy;
-
1412 // setData() sets generated to TRUE, but source record should prevail. -
1413 if (!values.isGenerated(i.key()))
partially evaluated: !values.isGenerated(i.key())
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:126
0-126
1414 mrow.recRef().setGenerated(i.value(), false);
never executed: mrow.recRef().setGenerated(i.value(), false);
0
1415 }
executed: }
Execution Count:126
126
1416 -
1417 if (d->strategy != OnManualSubmit)
evaluated: d->strategy != OnManualSubmit
TRUEFALSE
yes
Evaluation Count:19
yes
Evaluation Count:25
19-25
1418 return submit();
executed: return submit();
Execution Count:19
19
1419 -
1420 return true;
executed: return true;
Execution Count:25
25
1421} -
1422 -
1423QT_END_NAMESPACE -
1424 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial