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