models/qsqlquerymodel.cpp

Switch to Source codePreprocessed file
LineSource CodeCoverage
1 -
2 -
3 -
4 -
5 -
6 -
7void QSqlQueryModelPrivate::prefetch(int limit) -
8{ -
9 QSqlQueryModel * const q = q_func(); -
10 -
11 if (atEnd || limit <= bottom.row() || bottom.column() == -1)
-
12 return;
-
13 -
14 QModelIndex newBottom; -
15 const int oldBottomRow = qMax(bottom.row(), 0); -
16 -
17 -
18 if (query.seek(limit)) {
-
19 newBottom = q->createIndex(limit, bottom.column()); -
20 } else {
-
21 -
22 int i = oldBottomRow; -
23 if (query.seek(i)) {
-
24 while (query.next())
-
25 ++i;
-
26 newBottom = q->createIndex(i, bottom.column()); -
27 } else {
-
28 -
29 newBottom = q->createIndex(-1, bottom.column()); -
30 }
-
31 atEnd = true; -
32 }
-
33 if (newBottom.row() >= 0 && newBottom.row() > bottom.row()) {
-
34 q->beginInsertRows(QModelIndex(), bottom.row() + 1, newBottom.row()); -
35 bottom = newBottom; -
36 q->endInsertRows(); -
37 } else {
-
38 bottom = newBottom; -
39 }
-
40} -
41 -
42QSqlQueryModelPrivate::~QSqlQueryModelPrivate() -
43{ -
44} -
45 -
46void QSqlQueryModelPrivate::initColOffsets(int size) -
47{ -
48 colOffsets.resize(size); -
49 memset(colOffsets.data(), 0, colOffsets.size() * sizeof(int)); -
50}
-
51 -
52int QSqlQueryModelPrivate::columnInQuery(int modelColumn) const -
53{ -
54 if (modelColumn < 0 || modelColumn >= rec.count() || !rec.isGenerated(modelColumn) || modelColumn >= colOffsets.size())
evaluated: modelColumn < 0
TRUEFALSE
yes
Evaluation Count:15
yes
Evaluation Count:3254
partially evaluated: modelColumn >= rec.count()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:3254
evaluated: !rec.isGenerated(modelColumn)
TRUEFALSE
yes
Evaluation Count:12
yes
Evaluation Count:3242
partially evaluated: modelColumn >= colOffsets.size()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:3242
0-3254
55 return -1;
executed: return -1;
Execution Count:27
27
56 return modelColumn - colOffsets[modelColumn];
executed: return modelColumn - colOffsets[modelColumn];
Execution Count:3242
3242
57} -
58QSqlQueryModel::QSqlQueryModel(QObject *parent) -
59 : QAbstractTableModel(*new QSqlQueryModelPrivate, parent) -
60{ -
61}
-
62 -
63 -
64 -
65QSqlQueryModel::QSqlQueryModel(QSqlQueryModelPrivate &dd, QObject *parent) -
66 : QAbstractTableModel(dd, parent) -
67{ -
68}
-
69 -
70 -
71 -
72 -
73 -
74 -
75QSqlQueryModel::~QSqlQueryModel() -
76{ -
77} -
78void QSqlQueryModel::fetchMore(const QModelIndex &parent) -
79{ -
80 QSqlQueryModelPrivate * const d = d_func(); -
81 if (parent.isValid())
-
82 return;
-
83 d->prefetch(qMax(d->bottom.row(), 0) + 255); -
84}
-
85bool QSqlQueryModel::canFetchMore(const QModelIndex &parent) const -
86{ -
87 const QSqlQueryModelPrivate * const d = d_func(); -
88 return (!parent.isValid() && !d->atEnd);
-
89} -
90 -
91 -
92 -
93void QSqlQueryModel::beginInsertRows(const QModelIndex &parent, int first, int last) -
94{ -
95 QSqlQueryModelPrivate * const d = d_func(); -
96 if (!d->nestedResetLevel)
-
97 QAbstractTableModel::beginInsertRows(parent, first, last);
-
98}
-
99 -
100 -
101 -
102void QSqlQueryModel::endInsertRows() -
103{ -
104 QSqlQueryModelPrivate * const d = d_func(); -
105 if (!d->nestedResetLevel)
-
106 QAbstractTableModel::endInsertRows();
-
107}
-
108 -
109 -
110 -
111void QSqlQueryModel::beginRemoveRows(const QModelIndex &parent, int first, int last) -
112{ -
113 QSqlQueryModelPrivate * const d = d_func(); -
114 if (!d->nestedResetLevel)
-
115 QAbstractTableModel::beginRemoveRows(parent, first, last);
-
116}
-
117 -
118 -
119 -
120void QSqlQueryModel::endRemoveRows() -
121{ -
122 QSqlQueryModelPrivate * const d = d_func(); -
123 if (!d->nestedResetLevel)
-
124 QAbstractTableModel::endRemoveRows();
-
125}
-
126 -
127 -
128 -
129void QSqlQueryModel::beginInsertColumns(const QModelIndex &parent, int first, int last) -
130{ -
131 QSqlQueryModelPrivate * const d = d_func(); -
132 if (!d->nestedResetLevel)
-
133 QAbstractTableModel::beginInsertColumns(parent, first, last);
-
134}
-
135 -
136 -
137 -
138void QSqlQueryModel::endInsertColumns() -
139{ -
140 QSqlQueryModelPrivate * const d = d_func(); -
141 if (!d->nestedResetLevel)
-
142 QAbstractTableModel::endInsertColumns();
-
143}
-
144 -
145 -
146 -
147void QSqlQueryModel::beginRemoveColumns(const QModelIndex &parent, int first, int last) -
148{ -
149 QSqlQueryModelPrivate * const d = d_func(); -
150 if (!d->nestedResetLevel)
-
151 QAbstractTableModel::beginRemoveColumns(parent, first, last);
-
152}
-
153 -
154 -
155 -
156void QSqlQueryModel::endRemoveColumns() -
157{ -
158 QSqlQueryModelPrivate * const d = d_func(); -
159 if (!d->nestedResetLevel)
-
160 QAbstractTableModel::endRemoveColumns();
-
161}
-
162 -
163 -
164 -
165void QSqlQueryModel::beginResetModel() -
166{ -
167 QSqlQueryModelPrivate * const d = d_func(); -
168 if (!d->nestedResetLevel)
-
169 QAbstractTableModel::beginResetModel();
-
170 ++d->nestedResetLevel; -
171}
-
172 -
173 -
174 -
175void QSqlQueryModel::endResetModel() -
176{ -
177 QSqlQueryModelPrivate * const d = d_func(); -
178 --d->nestedResetLevel; -
179 if (!d->nestedResetLevel)
-
180 QAbstractTableModel::endResetModel();
-
181}
-
182int QSqlQueryModel::rowCount(const QModelIndex &index) const -
183{ -
184 const QSqlQueryModelPrivate * const d = d_func(); -
185 return index.isValid() ? 0 : d->bottom.row() + 1;
-
186} -
187 -
188 -
189 -
190int QSqlQueryModel::columnCount(const QModelIndex &index) const -
191{ -
192 const QSqlQueryModelPrivate * const d = d_func(); -
193 return index.isValid() ? 0 : d->rec.count();
-
194} -
195QVariant QSqlQueryModel::data(const QModelIndex &item, int role) const -
196{ -
197 const QSqlQueryModelPrivate * const d = d_func(); -
198 if (!item.isValid())
-
199 return QVariant();
-
200 -
201 QVariant v; -
202 if (role & ~(Qt::DisplayRole | Qt::EditRole))
-
203 return v;
-
204 -
205 if (!d->rec.isGenerated(item.column()))
-
206 return v;
-
207 QModelIndex dItem = indexInQuery(item); -
208 if (dItem.row() > d->bottom.row())
-
209 const_cast<QSqlQueryModelPrivate *>(d)->prefetch(dItem.row());
-
210 -
211 if (!d->query.seek(dItem.row())) {
-
212 d->error = d->query.lastError(); -
213 return v;
-
214 } -
215 -
216 return d->query.value(dItem.column());
-
217} -
218 -
219 -
220 -
221 -
222 -
223QVariant QSqlQueryModel::headerData(int section, Qt::Orientation orientation, int role) const -
224{ -
225 const QSqlQueryModelPrivate * const d = d_func(); -
226 if (orientation == Qt::Horizontal) {
evaluated: orientation == Qt::Horizontal
TRUEFALSE
yes
Evaluation Count:32
yes
Evaluation Count:1601
32-1601
227 QVariant val = d->headers.value(section).value(role); -
228 if (role == Qt::DisplayRole && !val.isValid())
evaluated: role == Qt::DisplayRole
TRUEFALSE
yes
Evaluation Count:27
yes
Evaluation Count:5
partially evaluated: !val.isValid()
TRUEFALSE
yes
Evaluation Count:27
no
Evaluation Count:0
0-27
229 val = d->headers.value(section).value(Qt::EditRole);
executed: val = d->headers.value(section).value(Qt::EditRole);
Execution Count:27
27
230 if (val.isValid())
evaluated: val.isValid()
TRUEFALSE
yes
Evaluation Count:2
yes
Evaluation Count:30
2-30
231 return val;
executed: return val;
Execution Count:2
2
232 QModelIndex dItem = indexInQuery(createIndex(0, section));if (role == Qt::DisplayRole && d->rec.count() > section && dItem.column()d->columnInQuery(section) != -1)
evaluated: role == Qt::DisplayRole
TRUEFALSE
yes
Evaluation Count:25
yes
Evaluation Count:5
evaluated: d->rec.count() > section
TRUEFALSE
yes
Evaluation Count:20
yes
Evaluation Count:5
evaluated: d->columnInQuery(section) != -1
TRUEFALSE
yes
Evaluation Count:16
yes
Evaluation Count:4
4-25
233 return d->rec.fieldName(section);
executed: return d->rec.fieldName(section);
Execution Count:16
16
234 }
executed: }
Execution Count:14
14
235 return QAbstractItemModel::headerData(section, orientation, role);
executed: return QAbstractItemModel::headerData(section, orientation, role);
Execution Count:1615
1615
236} -
237void QSqlQueryModel::queryChange() -
238{ -
239 -
240} -
241void QSqlQueryModel::setQuery(const QSqlQuery &query) -
242{ -
243 QSqlQueryModelPrivate * const d = d_func(); -
244 beginResetModel(); -
245 -
246 QSqlRecord newRec = query.record(); -
247 bool columnsChanged = (newRec != d->rec); -
248 -
249 if (d->colOffsets.size() != newRec.count() || columnsChanged)
-
250 d->initColOffsets(newRec.count());
-
251 -
252 d->bottom = QModelIndex(); -
253 d->error = QSqlError(); -
254 d->query = query; -
255 d->rec = newRec; -
256 d->atEnd = true; -
257 -
258 if (query.isForwardOnly()) {
-
259 d->error = QSqlError(QLatin1String("Forward-only queries " -
260 "cannot be used in a data model"), -
261 QString(), QSqlError::ConnectionError); -
262 endResetModel(); -
263 return;
-
264 } -
265 -
266 if (!query.isActive()) {
-
267 d->error = query.lastError(); -
268 endResetModel(); -
269 return;
-
270 } -
271 -
272 if (query.driver()->hasFeature(QSqlDriver::QuerySize) && d->query.size() > 0) {
-
273 d->bottom = createIndex(d->query.size() - 1, d->rec.count() - 1); -
274 } else {
-
275 d->bottom = createIndex(-1, d->rec.count() - 1); -
276 d->atEnd = false; -
277 }
-
278 -
279 -
280 -
281 fetchMore(); -
282 -
283 endResetModel(); -
284 queryChange(); -
285}
-
286void QSqlQueryModel::setQuery(const QString &query, const QSqlDatabase &db) -
287{ -
288 setQuery(QSqlQuery(query, db)); -
289}
-
290 -
291 -
292 -
293 -
294void QSqlQueryModel::clear() -
295{ -
296 QSqlQueryModelPrivate * const d = d_func(); -
297 d->error = QSqlError(); -
298 d->atEnd = true; -
299 d->query.clear(); -
300 d->rec.clear(); -
301 d->colOffsets.clear(); -
302 d->bottom = QModelIndex(); -
303 d->headers.clear(); -
304}
-
305bool QSqlQueryModel::setHeaderData(int section, Qt::Orientation orientation, -
306 const QVariant &value, int role) -
307{ -
308 QSqlQueryModelPrivate * const d = d_func(); -
309 if (orientation != Qt::Horizontal || section < 0 || columnCount() <= section)
-
310 return false;
-
311 -
312 if (d->headers.size() <= section)
-
313 d->headers.resize(qMax(section + 1, 16));
-
314 d->headers[section][role] = value; -
315 headerDataChanged(orientation, section, section); -
316 return true;
-
317} -
318 -
319 -
320 -
321 -
322 -
323 -
324QSqlQuery QSqlQueryModel::query() const -
325{ -
326 const QSqlQueryModelPrivate * const d = d_func(); -
327 return d->query;
-
328} -
329 -
330 -
331 -
332 -
333 -
334 -
335 -
336QSqlError QSqlQueryModel::lastError() const -
337{ -
338 const QSqlQueryModelPrivate * const d = d_func(); -
339 return d->error;
-
340} -
341 -
342 -
343 -
344 -
345 -
346 -
347 -
348void QSqlQueryModel::setLastError(const QSqlError &error) -
349{ -
350 QSqlQueryModelPrivate * const d = d_func(); -
351 d->error = error; -
352}
-
353QSqlRecord QSqlQueryModel::record(int row) const -
354{ -
355 const QSqlQueryModelPrivate * const d = d_func(); -
356 if (row < 0)
-
357 return d->rec;
-
358 -
359 QSqlRecord rec = d->rec; -
360 for (int i = 0; i < rec.count(); ++i)
-
361 rec.setValue(i, data(createIndex(row, i), Qt::EditRole));
-
362 return rec;
-
363} -
364QSqlRecord QSqlQueryModel::record() const -
365{ -
366 const QSqlQueryModelPrivate * const d = d_func(); -
367 return d->rec;
-
368} -
369bool QSqlQueryModel::insertColumns(int column, int count, const QModelIndex &parent) -
370{ -
371 QSqlQueryModelPrivate * const d = d_func(); -
372 if (count <= 0 || parent.isValid() || column < 0 || column > d->rec.count())
-
373 return false;
-
374 -
375 beginInsertColumns(parent, column, column + count - 1); -
376 for (int c = 0; c < count; ++c) {
-
377 QSqlField field; -
378 field.setReadOnly(true); -
379 field.setGenerated(false); -
380 d->rec.insert(column, field); -
381 if (d->colOffsets.size() < d->rec.count()) {
-
382 int nVal = d->colOffsets.isEmpty() ? 0 : d->colOffsets[d->colOffsets.size() - 1];
-
383 d->colOffsets.append(nVal); -
384 qt_noop(); -
385 }
-
386 for (int i = column + 1; i < d->colOffsets.count(); ++i)
-
387 ++d->colOffsets[i];
-
388 }
-
389 endInsertColumns(); -
390 return true;
-
391} -
392bool QSqlQueryModel::removeColumns(int column, int count, const QModelIndex &parent) -
393{ -
394 QSqlQueryModelPrivate * const d = d_func(); -
395 if (count <= 0 || parent.isValid() || column < 0 || column >= d->rec.count())
-
396 return false;
-
397 -
398 beginRemoveColumns(parent, column, column + count - 1); -
399 -
400 int i; -
401 for (i = 0; i < count; ++i)
-
402 d->rec.remove(column);
-
403 for (i = column; i < d->colOffsets.count(); ++i)
-
404 d->colOffsets[i] -= count;
-
405 -
406 endRemoveColumns(); -
407 return true;
-
408} -
409QModelIndex QSqlQueryModel::indexInQuery(const QModelIndex &item) const -
410{ -
411 const QSqlQueryModelPrivate * const d = d_func(); -
412 if (item.column() < 0 || item.column() >= d->rec.count()-
|| !int modelColumn = d->rec.isGeneratedcolumnInQuery(item.column())
||item.column() >= d->colOffsets.size())());
413 if (modelColumn < 0)
evaluated: modelColumn < 0
TRUEFALSE
yes
Evaluation Count:23
yes
Evaluation Count:3226
23-3226
414 return QModelIndex();
executed: return QModelIndex();
Execution Count:23
23
415 return createIndex(item.row(), item.column() - d->colOffsets[item.column()],modelColumn, item.internalPointer());
executed: return createIndex(item.row(), modelColumn, item.internalPointer());
Execution Count:3226
3226
416} -
417 -
418 -
419 -
Switch to Source codePreprocessed file

Generated by Squish Coco Non-Commercial