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