| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/sql/kernel/qsqlquery.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 "qsqlquery.h" | - | ||||||||||||||||||
| 41 | - | |||||||||||||||||||
| 42 | //#define QT_DEBUG_SQL | - | ||||||||||||||||||
| 43 | - | |||||||||||||||||||
| 44 | #include "qdebug.h" | - | ||||||||||||||||||
| 45 | #include "qelapsedtimer.h" | - | ||||||||||||||||||
| 46 | #include "qatomic.h" | - | ||||||||||||||||||
| 47 | #include "qsqlrecord.h" | - | ||||||||||||||||||
| 48 | #include "qsqlresult.h" | - | ||||||||||||||||||
| 49 | #include "qsqldriver.h" | - | ||||||||||||||||||
| 50 | #include "qsqldatabase.h" | - | ||||||||||||||||||
| 51 | #include "private/qsqlnulldriver_p.h" | - | ||||||||||||||||||
| 52 | #include "qvector.h" | - | ||||||||||||||||||
| 53 | #include "qmap.h" | - | ||||||||||||||||||
| 54 | - | |||||||||||||||||||
| 55 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
| 56 | - | |||||||||||||||||||
| 57 | class QSqlQueryPrivate | - | ||||||||||||||||||
| 58 | { | - | ||||||||||||||||||
| 59 | public: | - | ||||||||||||||||||
| 60 | QSqlQueryPrivate(QSqlResult* result); | - | ||||||||||||||||||
| 61 | ~QSqlQueryPrivate(); | - | ||||||||||||||||||
| 62 | QAtomicInt ref; | - | ||||||||||||||||||
| 63 | QSqlResult* sqlResult; | - | ||||||||||||||||||
| 64 | - | |||||||||||||||||||
| 65 | static QSqlQueryPrivate* shared_null(); | - | ||||||||||||||||||
| 66 | }; | - | ||||||||||||||||||
| 67 | - | |||||||||||||||||||
| 68 | Q_GLOBAL_STATIC_WITH_ARGS(QSqlQueryPrivate, nullQueryPrivate, (0)) executed 8 times by 8 tests: end of blockExecuted by:
executed 8 times by 8 tests: guard.store(QtGlobalStatic::Destroyed);Executed by:
executed 1440 times by 8 tests: return &holder.value;Executed by:
| 0-1440 | ||||||||||||||||||
| 69 | Q_GLOBAL_STATIC(QSqlNullDriver, nullDriver) executed 9 times by 9 tests: end of blockExecuted by:
executed 9 times by 9 tests: guard.store(QtGlobalStatic::Destroyed);Executed by:
executed 9 times by 9 tests: return &holder.value;Executed by:
| 0-9 | ||||||||||||||||||
| 70 | Q_GLOBAL_STATIC_WITH_ARGS(QSqlNullResult, nullResult, (nullDriver())) executed 9 times by 9 tests: end of blockExecuted by:
executed 9 times by 9 tests: guard.store(QtGlobalStatic::Destroyed);Executed by:
executed 2198 times by 17 tests: return &holder.value;Executed by:
| 0-2198 | ||||||||||||||||||
| 71 | - | |||||||||||||||||||
| 72 | QSqlQueryPrivate* QSqlQueryPrivate::shared_null() | - | ||||||||||||||||||
| 73 | { | - | ||||||||||||||||||
| 74 | QSqlQueryPrivate *null = nullQueryPrivate(); | - | ||||||||||||||||||
| 75 | null->ref.ref(); | - | ||||||||||||||||||
| 76 | return null; executed 1440 times by 8 tests: return null;Executed by:
| 1440 | ||||||||||||||||||
| 77 | } | - | ||||||||||||||||||
| 78 | - | |||||||||||||||||||
| 79 | /*! | - | ||||||||||||||||||
| 80 | \internal | - | ||||||||||||||||||
| 81 | */ | - | ||||||||||||||||||
| 82 | QSqlQueryPrivate::QSqlQueryPrivate(QSqlResult* result) | - | ||||||||||||||||||
| 83 | : ref(1), sqlResult(result) | - | ||||||||||||||||||
| 84 | { | - | ||||||||||||||||||
| 85 | if (!sqlResult)
| 8-2182 | ||||||||||||||||||
| 86 | sqlResult = nullResult(); executed 8 times by 8 tests: sqlResult = nullResult();Executed by:
| 8 | ||||||||||||||||||
| 87 | } executed 2190 times by 9 tests: end of blockExecuted by:
| 2190 | ||||||||||||||||||
| 88 | - | |||||||||||||||||||
| 89 | QSqlQueryPrivate::~QSqlQueryPrivate() | - | ||||||||||||||||||
| 90 | { | - | ||||||||||||||||||
| 91 | QSqlResult *nr = nullResult(); | - | ||||||||||||||||||
| 92 | if (!nr || sqlResult == nr)
| 0-2190 | ||||||||||||||||||
| 93 | return; executed 8 times by 8 tests: return;Executed by:
| 8 | ||||||||||||||||||
| 94 | delete sqlResult; | - | ||||||||||||||||||
| 95 | } executed 2182 times by 9 tests: end of blockExecuted by:
| 2182 | ||||||||||||||||||
| 96 | - | |||||||||||||||||||
| 97 | /*! | - | ||||||||||||||||||
| 98 | \class QSqlQuery | - | ||||||||||||||||||
| 99 | \brief The QSqlQuery class provides a means of executing and | - | ||||||||||||||||||
| 100 | manipulating SQL statements. | - | ||||||||||||||||||
| 101 | - | |||||||||||||||||||
| 102 | \ingroup database | - | ||||||||||||||||||
| 103 | \ingroup shared | - | ||||||||||||||||||
| 104 | - | |||||||||||||||||||
| 105 | \inmodule QtSql | - | ||||||||||||||||||
| 106 | - | |||||||||||||||||||
| 107 | QSqlQuery encapsulates the functionality involved in creating, | - | ||||||||||||||||||
| 108 | navigating and retrieving data from SQL queries which are | - | ||||||||||||||||||
| 109 | executed on a \l QSqlDatabase. It can be used to execute DML | - | ||||||||||||||||||
| 110 | (data manipulation language) statements, such as \c SELECT, \c | - | ||||||||||||||||||
| 111 | INSERT, \c UPDATE and \c DELETE, as well as DDL (data definition | - | ||||||||||||||||||
| 112 | language) statements, such as \c{CREATE} \c{TABLE}. It can also | - | ||||||||||||||||||
| 113 | be used to execute database-specific commands which are not | - | ||||||||||||||||||
| 114 | standard SQL (e.g. \c{SET DATESTYLE=ISO} for PostgreSQL). | - | ||||||||||||||||||
| 115 | - | |||||||||||||||||||
| 116 | Successfully executed SQL statements set the query's state to | - | ||||||||||||||||||
| 117 | active so that isActive() returns \c true. Otherwise the query's | - | ||||||||||||||||||
| 118 | state is set to inactive. In either case, when executing a new SQL | - | ||||||||||||||||||
| 119 | statement, the query is positioned on an invalid record. An active | - | ||||||||||||||||||
| 120 | query must be navigated to a valid record (so that isValid() | - | ||||||||||||||||||
| 121 | returns \c true) before values can be retrieved. | - | ||||||||||||||||||
| 122 | - | |||||||||||||||||||
| 123 | For some databases, if an active query that is a \c{SELECT} | - | ||||||||||||||||||
| 124 | statement exists when you call \l{QSqlDatabase::}{commit()} or | - | ||||||||||||||||||
| 125 | \l{QSqlDatabase::}{rollback()}, the commit or rollback will | - | ||||||||||||||||||
| 126 | fail. See isActive() for details. | - | ||||||||||||||||||
| 127 | - | |||||||||||||||||||
| 128 | \target QSqlQuery examples | - | ||||||||||||||||||
| 129 | - | |||||||||||||||||||
| 130 | Navigating records is performed with the following functions: | - | ||||||||||||||||||
| 131 | - | |||||||||||||||||||
| 132 | \list | - | ||||||||||||||||||
| 133 | \li next() | - | ||||||||||||||||||
| 134 | \li previous() | - | ||||||||||||||||||
| 135 | \li first() | - | ||||||||||||||||||
| 136 | \li last() | - | ||||||||||||||||||
| 137 | \li seek() | - | ||||||||||||||||||
| 138 | \endlist | - | ||||||||||||||||||
| 139 | - | |||||||||||||||||||
| 140 | These functions allow the programmer to move forward, backward | - | ||||||||||||||||||
| 141 | or arbitrarily through the records returned by the query. If you | - | ||||||||||||||||||
| 142 | only need to move forward through the results (e.g., by using | - | ||||||||||||||||||
| 143 | next()), you can use setForwardOnly(), which will save a | - | ||||||||||||||||||
| 144 | significant amount of memory overhead and improve performance on | - | ||||||||||||||||||
| 145 | some databases. Once an active query is positioned on a valid | - | ||||||||||||||||||
| 146 | record, data can be retrieved using value(). All data is | - | ||||||||||||||||||
| 147 | transferred from the SQL backend using QVariants. | - | ||||||||||||||||||
| 148 | - | |||||||||||||||||||
| 149 | For example: | - | ||||||||||||||||||
| 150 | - | |||||||||||||||||||
| 151 | \snippet sqldatabase/sqldatabase.cpp 7 | - | ||||||||||||||||||
| 152 | - | |||||||||||||||||||
| 153 | To access the data returned by a query, use value(int). Each | - | ||||||||||||||||||
| 154 | field in the data returned by a \c SELECT statement is accessed | - | ||||||||||||||||||
| 155 | by passing the field's position in the statement, starting from | - | ||||||||||||||||||
| 156 | 0. This makes using \c{SELECT *} queries inadvisable because the | - | ||||||||||||||||||
| 157 | order of the fields returned is indeterminate. | - | ||||||||||||||||||
| 158 | - | |||||||||||||||||||
| 159 | For the sake of efficiency, there are no functions to access a | - | ||||||||||||||||||
| 160 | field by name (unless you use prepared queries with names, as | - | ||||||||||||||||||
| 161 | explained below). To convert a field name into an index, use | - | ||||||||||||||||||
| 162 | record().\l{QSqlRecord::indexOf()}{indexOf()}, for example: | - | ||||||||||||||||||
| 163 | - | |||||||||||||||||||
| 164 | \snippet sqldatabase/sqldatabase.cpp 8 | - | ||||||||||||||||||
| 165 | - | |||||||||||||||||||
| 166 | QSqlQuery supports prepared query execution and the binding of | - | ||||||||||||||||||
| 167 | parameter values to placeholders. Some databases don't support | - | ||||||||||||||||||
| 168 | these features, so for those, Qt emulates the required | - | ||||||||||||||||||
| 169 | functionality. For example, the Oracle and ODBC drivers have | - | ||||||||||||||||||
| 170 | proper prepared query support, and Qt makes use of it; but for | - | ||||||||||||||||||
| 171 | databases that don't have this support, Qt implements the feature | - | ||||||||||||||||||
| 172 | itself, e.g. by replacing placeholders with actual values when a | - | ||||||||||||||||||
| 173 | query is executed. Use numRowsAffected() to find out how many rows | - | ||||||||||||||||||
| 174 | were affected by a non-\c SELECT query, and size() to find how | - | ||||||||||||||||||
| 175 | many were retrieved by a \c SELECT. | - | ||||||||||||||||||
| 176 | - | |||||||||||||||||||
| 177 | Oracle databases identify placeholders by using a colon-name | - | ||||||||||||||||||
| 178 | syntax, e.g \c{:name}. ODBC simply uses \c ? characters. Qt | - | ||||||||||||||||||
| 179 | supports both syntaxes, with the restriction that you can't mix | - | ||||||||||||||||||
| 180 | them in the same query. | - | ||||||||||||||||||
| 181 | - | |||||||||||||||||||
| 182 | You can retrieve the values of all the fields in a single variable | - | ||||||||||||||||||
| 183 | (a map) using boundValues(). | - | ||||||||||||||||||
| 184 | - | |||||||||||||||||||
| 185 | \section1 Approaches to Binding Values | - | ||||||||||||||||||
| 186 | - | |||||||||||||||||||
| 187 | Below we present the same example using each of the four | - | ||||||||||||||||||
| 188 | different binding approaches, as well as one example of binding | - | ||||||||||||||||||
| 189 | values to a stored procedure. | - | ||||||||||||||||||
| 190 | - | |||||||||||||||||||
| 191 | \b{Named binding using named placeholders:} | - | ||||||||||||||||||
| 192 | - | |||||||||||||||||||
| 193 | \snippet sqldatabase/sqldatabase.cpp 9 | - | ||||||||||||||||||
| 194 | - | |||||||||||||||||||
| 195 | \b{Positional binding using named placeholders:} | - | ||||||||||||||||||
| 196 | - | |||||||||||||||||||
| 197 | \snippet sqldatabase/sqldatabase.cpp 10 | - | ||||||||||||||||||
| 198 | - | |||||||||||||||||||
| 199 | \b{Binding values using positional placeholders (version 1):} | - | ||||||||||||||||||
| 200 | - | |||||||||||||||||||
| 201 | \snippet sqldatabase/sqldatabase.cpp 11 | - | ||||||||||||||||||
| 202 | - | |||||||||||||||||||
| 203 | \b{Binding values using positional placeholders (version 2):} | - | ||||||||||||||||||
| 204 | - | |||||||||||||||||||
| 205 | \snippet sqldatabase/sqldatabase.cpp 12 | - | ||||||||||||||||||
| 206 | - | |||||||||||||||||||
| 207 | \b{Binding values to a stored procedure:} | - | ||||||||||||||||||
| 208 | - | |||||||||||||||||||
| 209 | This code calls a stored procedure called \c AsciiToInt(), passing | - | ||||||||||||||||||
| 210 | it a character through its in parameter, and taking its result in | - | ||||||||||||||||||
| 211 | the out parameter. | - | ||||||||||||||||||
| 212 | - | |||||||||||||||||||
| 213 | \snippet sqldatabase/sqldatabase.cpp 13 | - | ||||||||||||||||||
| 214 | - | |||||||||||||||||||
| 215 | Note that unbound parameters will retain their values. | - | ||||||||||||||||||
| 216 | - | |||||||||||||||||||
| 217 | Stored procedures that uses the return statement to return values, | - | ||||||||||||||||||
| 218 | or return multiple result sets, are not fully supported. For specific | - | ||||||||||||||||||
| 219 | details see \l{SQL Database Drivers}. | - | ||||||||||||||||||
| 220 | - | |||||||||||||||||||
| 221 | \warning You must load the SQL driver and open the connection before a | - | ||||||||||||||||||
| 222 | QSqlQuery is created. Also, the connection must remain open while the | - | ||||||||||||||||||
| 223 | query exists; otherwise, the behavior of QSqlQuery is undefined. | - | ||||||||||||||||||
| 224 | - | |||||||||||||||||||
| 225 | \sa QSqlDatabase, QSqlQueryModel, QSqlTableModel, QVariant | - | ||||||||||||||||||
| 226 | */ | - | ||||||||||||||||||
| 227 | - | |||||||||||||||||||
| 228 | /*! | - | ||||||||||||||||||
| 229 | Constructs a QSqlQuery object which uses the QSqlResult \a result | - | ||||||||||||||||||
| 230 | to communicate with a database. | - | ||||||||||||||||||
| 231 | */ | - | ||||||||||||||||||
| 232 | - | |||||||||||||||||||
| 233 | QSqlQuery::QSqlQuery(QSqlResult *result) | - | ||||||||||||||||||
| 234 | { | - | ||||||||||||||||||
| 235 | d = new QSqlQueryPrivate(result); | - | ||||||||||||||||||
| 236 | } executed 2182 times by 9 tests: end of blockExecuted by:
| 2182 | ||||||||||||||||||
| 237 | - | |||||||||||||||||||
| 238 | /*! | - | ||||||||||||||||||
| 239 | Destroys the object and frees any allocated resources. | - | ||||||||||||||||||
| 240 | */ | - | ||||||||||||||||||
| 241 | - | |||||||||||||||||||
| 242 | QSqlQuery::~QSqlQuery() | - | ||||||||||||||||||
| 243 | { | - | ||||||||||||||||||
| 244 | if (!d->ref.deref())
| 1604-2019 | ||||||||||||||||||
| 245 | delete d; executed 1604 times by 9 tests: delete d;Executed by:
| 1604 | ||||||||||||||||||
| 246 | } executed 3623 times by 9 tests: end of blockExecuted by:
| 3623 | ||||||||||||||||||
| 247 | - | |||||||||||||||||||
| 248 | /*! | - | ||||||||||||||||||
| 249 | Constructs a copy of \a other. | - | ||||||||||||||||||
| 250 | */ | - | ||||||||||||||||||
| 251 | - | |||||||||||||||||||
| 252 | QSqlQuery::QSqlQuery(const QSqlQuery& other) | - | ||||||||||||||||||
| 253 | { | - | ||||||||||||||||||
| 254 | d = other.d; | - | ||||||||||||||||||
| 255 | d->ref.ref(); | - | ||||||||||||||||||
| 256 | } executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||||||||
| 257 | - | |||||||||||||||||||
| 258 | /*! | - | ||||||||||||||||||
| 259 | \internal | - | ||||||||||||||||||
| 260 | */ | - | ||||||||||||||||||
| 261 | static void qInit(QSqlQuery *q, const QString& query, QSqlDatabase db) | - | ||||||||||||||||||
| 262 | { | - | ||||||||||||||||||
| 263 | QSqlDatabase database = db; | - | ||||||||||||||||||
| 264 | if (!database.isValid())
| 667-773 | ||||||||||||||||||
| 265 | database = QSqlDatabase::database(QLatin1String(QSqlDatabase::defaultConnection), false); executed 773 times by 5 tests: database = QSqlDatabase::database(QLatin1String(QSqlDatabase::defaultConnection), false);Executed by:
| 773 | ||||||||||||||||||
| 266 | if (database.isValid()) {
| 260-1180 | ||||||||||||||||||
| 267 | *q = QSqlQuery(database.driver()->createResult()); | - | ||||||||||||||||||
| 268 | } executed 1180 times by 8 tests: end of blockExecuted by:
| 1180 | ||||||||||||||||||
| 269 | if (!query.isEmpty())
| 540-900 | ||||||||||||||||||
| 270 | q->exec(query); executed 540 times by 6 tests: q->exec(query);Executed by:
| 540 | ||||||||||||||||||
| 271 | } executed 1440 times by 8 tests: end of blockExecuted by:
| 1440 | ||||||||||||||||||
| 272 | - | |||||||||||||||||||
| 273 | /*! | - | ||||||||||||||||||
| 274 | Constructs a QSqlQuery object using the SQL \a query and the | - | ||||||||||||||||||
| 275 | database \a db. If \a db is not specified, or is invalid, the application's | - | ||||||||||||||||||
| 276 | default database is used. If \a query is not an empty string, it | - | ||||||||||||||||||
| 277 | will be executed. | - | ||||||||||||||||||
| 278 | - | |||||||||||||||||||
| 279 | \sa QSqlDatabase | - | ||||||||||||||||||
| 280 | */ | - | ||||||||||||||||||
| 281 | QSqlQuery::QSqlQuery(const QString& query, QSqlDatabase db) | - | ||||||||||||||||||
| 282 | { | - | ||||||||||||||||||
| 283 | d = QSqlQueryPrivate::shared_null(); | - | ||||||||||||||||||
| 284 | qInit(this, query, db); | - | ||||||||||||||||||
| 285 | } executed 1109 times by 7 tests: end of blockExecuted by:
| 1109 | ||||||||||||||||||
| 286 | - | |||||||||||||||||||
| 287 | /*! | - | ||||||||||||||||||
| 288 | Constructs a QSqlQuery object using the database \a db. | - | ||||||||||||||||||
| 289 | If \a db is invalid, the application's default database will be used. | - | ||||||||||||||||||
| 290 | - | |||||||||||||||||||
| 291 | \sa QSqlDatabase | - | ||||||||||||||||||
| 292 | */ | - | ||||||||||||||||||
| 293 | - | |||||||||||||||||||
| 294 | QSqlQuery::QSqlQuery(QSqlDatabase db) | - | ||||||||||||||||||
| 295 | { | - | ||||||||||||||||||
| 296 | d = QSqlQueryPrivate::shared_null(); | - | ||||||||||||||||||
| 297 | qInit(this, QString(), db); | - | ||||||||||||||||||
| 298 | } executed 331 times by 7 tests: end of blockExecuted by:
| 331 | ||||||||||||||||||
| 299 | - | |||||||||||||||||||
| 300 | - | |||||||||||||||||||
| 301 | /*! | - | ||||||||||||||||||
| 302 | Assigns \a other to this object. | - | ||||||||||||||||||
| 303 | */ | - | ||||||||||||||||||
| 304 | - | |||||||||||||||||||
| 305 | QSqlQuery& QSqlQuery::operator=(const QSqlQuery& other) | - | ||||||||||||||||||
| 306 | { | - | ||||||||||||||||||
| 307 | qAtomicAssign(d, other.d); | - | ||||||||||||||||||
| 308 | return *this; executed 2022 times by 8 tests: return *this;Executed by:
| 2022 | ||||||||||||||||||
| 309 | } | - | ||||||||||||||||||
| 310 | - | |||||||||||||||||||
| 311 | /*! | - | ||||||||||||||||||
| 312 | Returns \c true if the query is not \l{isActive()}{active}, | - | ||||||||||||||||||
| 313 | the query is not positioned on a valid record, | - | ||||||||||||||||||
| 314 | there is no such \a field, or the \a field is null; otherwise \c false. | - | ||||||||||||||||||
| 315 | Note that for some drivers, isNull() will not return accurate | - | ||||||||||||||||||
| 316 | information until after an attempt is made to retrieve data. | - | ||||||||||||||||||
| 317 | - | |||||||||||||||||||
| 318 | \sa isActive(), isValid(), value() | - | ||||||||||||||||||
| 319 | */ | - | ||||||||||||||||||
| 320 | - | |||||||||||||||||||
| 321 | bool QSqlQuery::isNull(int field) const | - | ||||||||||||||||||
| 322 | { | - | ||||||||||||||||||
| 323 | return !d->sqlResult->isActive() executed 10 times by 1 test: return !d->sqlResult->isActive() || !d->sqlResult->isValid() || d->sqlResult->isNull(field);Executed by:
| 10 | ||||||||||||||||||
| 324 | || !d->sqlResult->isValid() executed 10 times by 1 test: return !d->sqlResult->isActive() || !d->sqlResult->isValid() || d->sqlResult->isNull(field);Executed by:
| 10 | ||||||||||||||||||
| 325 | || d->sqlResult->isNull(field); executed 10 times by 1 test: return !d->sqlResult->isActive() || !d->sqlResult->isValid() || d->sqlResult->isNull(field);Executed by:
| 10 | ||||||||||||||||||
| 326 | } | - | ||||||||||||||||||
| 327 | - | |||||||||||||||||||
| 328 | /*! | - | ||||||||||||||||||
| 329 | \overload | - | ||||||||||||||||||
| 330 | - | |||||||||||||||||||
| 331 | Returns \c true if there is no field with this \a name; otherwise | - | ||||||||||||||||||
| 332 | returns isNull(int index) for the corresponding field index. | - | ||||||||||||||||||
| 333 | - | |||||||||||||||||||
| 334 | This overload is less efficient than \l{QSqlQuery::}{isNull()} | - | ||||||||||||||||||
| 335 | */ | - | ||||||||||||||||||
| 336 | - | |||||||||||||||||||
| 337 | bool QSqlQuery::isNull(const QString &name) const | - | ||||||||||||||||||
| 338 | { | - | ||||||||||||||||||
| 339 | int index = d->sqlResult->record().indexOf(name); | - | ||||||||||||||||||
| 340 | if (index > -1)
| 1-4 | ||||||||||||||||||
| 341 | return isNull(index); executed 4 times by 1 test: return isNull(index);Executed by:
| 4 | ||||||||||||||||||
| 342 | qWarning("QSqlQuery::isNull: unknown field name '%s'", qPrintable(name)); | - | ||||||||||||||||||
| 343 | return true; executed 1 time by 1 test: return true;Executed by:
| 1 | ||||||||||||||||||
| 344 | } | - | ||||||||||||||||||
| 345 | - | |||||||||||||||||||
| 346 | /*! | - | ||||||||||||||||||
| 347 | - | |||||||||||||||||||
| 348 | Executes the SQL in \a query. Returns \c true and sets the query state | - | ||||||||||||||||||
| 349 | to \l{isActive()}{active} if the query was successful; otherwise | - | ||||||||||||||||||
| 350 | returns \c false. The \a query string must use syntax appropriate for | - | ||||||||||||||||||
| 351 | the SQL database being queried (for example, standard SQL). | - | ||||||||||||||||||
| 352 | - | |||||||||||||||||||
| 353 | After the query is executed, the query is positioned on an \e | - | ||||||||||||||||||
| 354 | invalid record and must be navigated to a valid record before data | - | ||||||||||||||||||
| 355 | values can be retrieved (for example, using next()). | - | ||||||||||||||||||
| 356 | - | |||||||||||||||||||
| 357 | Note that the last error for this query is reset when exec() is | - | ||||||||||||||||||
| 358 | called. | - | ||||||||||||||||||
| 359 | - | |||||||||||||||||||
| 360 | For SQLite, the query string can contain only one statement at a time. | - | ||||||||||||||||||
| 361 | If more than one statement is given, the function returns \c false. | - | ||||||||||||||||||
| 362 | - | |||||||||||||||||||
| 363 | Example: | - | ||||||||||||||||||
| 364 | - | |||||||||||||||||||
| 365 | \snippet sqldatabase/sqldatabase.cpp 34 | - | ||||||||||||||||||
| 366 | - | |||||||||||||||||||
| 367 | \sa isActive(), isValid(), next(), previous(), first(), last(), | - | ||||||||||||||||||
| 368 | seek() | - | ||||||||||||||||||
| 369 | */ | - | ||||||||||||||||||
| 370 | - | |||||||||||||||||||
| 371 | bool QSqlQuery::exec(const QString& query) | - | ||||||||||||||||||
| 372 | { | - | ||||||||||||||||||
| 373 | #ifdef QT_DEBUG_SQL | - | ||||||||||||||||||
| 374 | QElapsedTimer t; | - | ||||||||||||||||||
| 375 | t.start(); | - | ||||||||||||||||||
| 376 | #endif | - | ||||||||||||||||||
| 377 | if (d->ref.load() != 1) {
| 5-2553 | ||||||||||||||||||
| 378 | bool fo = isForwardOnly(); | - | ||||||||||||||||||
| 379 | *this = QSqlQuery(driver()->createResult()); | - | ||||||||||||||||||
| 380 | d->sqlResult->setNumericalPrecisionPolicy(d->sqlResult->numericalPrecisionPolicy()); | - | ||||||||||||||||||
| 381 | setForwardOnly(fo); | - | ||||||||||||||||||
| 382 | } else { executed 5 times by 1 test: end of blockExecuted by:
| 5 | ||||||||||||||||||
| 383 | d->sqlResult->clear(); | - | ||||||||||||||||||
| 384 | d->sqlResult->setActive(false); | - | ||||||||||||||||||
| 385 | d->sqlResult->setLastError(QSqlError()); | - | ||||||||||||||||||
| 386 | d->sqlResult->setAt(QSql::BeforeFirstRow); | - | ||||||||||||||||||
| 387 | d->sqlResult->setNumericalPrecisionPolicy(d->sqlResult->numericalPrecisionPolicy()); | - | ||||||||||||||||||
| 388 | } executed 2553 times by 9 tests: end of blockExecuted by:
| 2553 | ||||||||||||||||||
| 389 | d->sqlResult->setQuery(query.trimmed()); | - | ||||||||||||||||||
| 390 | if (!driver()->isOpen() || driver()->isOpenError()) {
| 0-2556 | ||||||||||||||||||
| 391 | qWarning("QSqlQuery::exec: database not open"); | - | ||||||||||||||||||
| 392 | return false; executed 2 times by 1 test: return false;Executed by:
| 2 | ||||||||||||||||||
| 393 | } | - | ||||||||||||||||||
| 394 | if (query.isEmpty()) {
| 0-2556 | ||||||||||||||||||
| 395 | qWarning("QSqlQuery::exec: empty query"); | - | ||||||||||||||||||
| 396 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 397 | } | - | ||||||||||||||||||
| 398 | - | |||||||||||||||||||
| 399 | bool retval = d->sqlResult->reset(query); | - | ||||||||||||||||||
| 400 | #ifdef QT_DEBUG_SQL | - | ||||||||||||||||||
| 401 | qDebug().nospace() << "Executed query (" << t.elapsed() << "ms, " << d->sqlResult->size() | - | ||||||||||||||||||
| 402 | << " results, " << d->sqlResult->numRowsAffected() | - | ||||||||||||||||||
| 403 | << " affected): " << d->sqlResult->lastQuery(); | - | ||||||||||||||||||
| 404 | #endif | - | ||||||||||||||||||
| 405 | return retval; executed 2556 times by 9 tests: return retval;Executed by:
| 2556 | ||||||||||||||||||
| 406 | } | - | ||||||||||||||||||
| 407 | - | |||||||||||||||||||
| 408 | /*! | - | ||||||||||||||||||
| 409 | Returns the value of field \a index in the current record. | - | ||||||||||||||||||
| 410 | - | |||||||||||||||||||
| 411 | The fields are numbered from left to right using the text of the | - | ||||||||||||||||||
| 412 | \c SELECT statement, e.g. in | - | ||||||||||||||||||
| 413 | - | |||||||||||||||||||
| 414 | \snippet code/src_sql_kernel_qsqlquery.cpp 0 | - | ||||||||||||||||||
| 415 | - | |||||||||||||||||||
| 416 | field 0 is \c forename and field 1 is \c | - | ||||||||||||||||||
| 417 | surname. Using \c{SELECT *} is not recommended because the order | - | ||||||||||||||||||
| 418 | of the fields in the query is undefined. | - | ||||||||||||||||||
| 419 | - | |||||||||||||||||||
| 420 | An invalid QVariant is returned if field \a index does not | - | ||||||||||||||||||
| 421 | exist, if the query is inactive, or if the query is positioned on | - | ||||||||||||||||||
| 422 | an invalid record. | - | ||||||||||||||||||
| 423 | - | |||||||||||||||||||
| 424 | \sa previous(), next(), first(), last(), seek(), isActive(), isValid() | - | ||||||||||||||||||
| 425 | */ | - | ||||||||||||||||||
| 426 | - | |||||||||||||||||||
| 427 | QVariant QSqlQuery::value(int index) const | - | ||||||||||||||||||
| 428 | { | - | ||||||||||||||||||
| 429 | if (isActive() && isValid() && (index > -1))
| 0-9138 | ||||||||||||||||||
| 430 | return d->sqlResult->data(index); executed 9138 times by 8 tests: return d->sqlResult->data(index);Executed by:
| 9138 | ||||||||||||||||||
| 431 | qWarning("QSqlQuery::value: not positioned on a valid record"); | - | ||||||||||||||||||
| 432 | return QVariant(); never executed: return QVariant(); | 0 | ||||||||||||||||||
| 433 | } | - | ||||||||||||||||||
| 434 | - | |||||||||||||||||||
| 435 | /*! | - | ||||||||||||||||||
| 436 | \overload | - | ||||||||||||||||||
| 437 | - | |||||||||||||||||||
| 438 | Returns the value of the field called \a name in the current record. | - | ||||||||||||||||||
| 439 | If field \a name does not exist an invalid variant is returned. | - | ||||||||||||||||||
| 440 | - | |||||||||||||||||||
| 441 | This overload is less efficient than \l{QSqlQuery::}{value()} | - | ||||||||||||||||||
| 442 | */ | - | ||||||||||||||||||
| 443 | - | |||||||||||||||||||
| 444 | QVariant QSqlQuery::value(const QString& name) const | - | ||||||||||||||||||
| 445 | { | - | ||||||||||||||||||
| 446 | int index = d->sqlResult->record().indexOf(name); | - | ||||||||||||||||||
| 447 | if (index > -1)
| 0-5 | ||||||||||||||||||
| 448 | return value(index); executed 5 times by 1 test: return value(index);Executed by:
| 5 | ||||||||||||||||||
| 449 | qWarning("QSqlQuery::value: unknown field name '%s'", qPrintable(name)); | - | ||||||||||||||||||
| 450 | return QVariant(); never executed: return QVariant(); | 0 | ||||||||||||||||||
| 451 | } | - | ||||||||||||||||||
| 452 | - | |||||||||||||||||||
| 453 | /*! | - | ||||||||||||||||||
| 454 | Returns the current internal position of the query. The first | - | ||||||||||||||||||
| 455 | record is at position zero. If the position is invalid, the | - | ||||||||||||||||||
| 456 | function returns QSql::BeforeFirstRow or | - | ||||||||||||||||||
| 457 | QSql::AfterLastRow, which are special negative values. | - | ||||||||||||||||||
| 458 | - | |||||||||||||||||||
| 459 | \sa previous(), next(), first(), last(), seek(), isActive(), isValid() | - | ||||||||||||||||||
| 460 | */ | - | ||||||||||||||||||
| 461 | - | |||||||||||||||||||
| 462 | int QSqlQuery::at() const | - | ||||||||||||||||||
| 463 | { | - | ||||||||||||||||||
| 464 | return d->sqlResult->at(); executed 10980 times by 9 tests: return d->sqlResult->at();Executed by:
| 10980 | ||||||||||||||||||
| 465 | } | - | ||||||||||||||||||
| 466 | - | |||||||||||||||||||
| 467 | /*! | - | ||||||||||||||||||
| 468 | Returns the text of the current query being used, or an empty | - | ||||||||||||||||||
| 469 | string if there is no current query text. | - | ||||||||||||||||||
| 470 | - | |||||||||||||||||||
| 471 | \sa executedQuery() | - | ||||||||||||||||||
| 472 | */ | - | ||||||||||||||||||
| 473 | - | |||||||||||||||||||
| 474 | QString QSqlQuery::lastQuery() const | - | ||||||||||||||||||
| 475 | { | - | ||||||||||||||||||
| 476 | return d->sqlResult->lastQuery(); executed 124 times by 3 tests: return d->sqlResult->lastQuery();Executed by:
| 124 | ||||||||||||||||||
| 477 | } | - | ||||||||||||||||||
| 478 | - | |||||||||||||||||||
| 479 | /*! | - | ||||||||||||||||||
| 480 | Returns the database driver associated with the query. | - | ||||||||||||||||||
| 481 | */ | - | ||||||||||||||||||
| 482 | - | |||||||||||||||||||
| 483 | const QSqlDriver *QSqlQuery::driver() const | - | ||||||||||||||||||
| 484 | { | - | ||||||||||||||||||
| 485 | return d->sqlResult->driver(); executed 6714 times by 9 tests: return d->sqlResult->driver();Executed by:
| 6714 | ||||||||||||||||||
| 486 | } | - | ||||||||||||||||||
| 487 | - | |||||||||||||||||||
| 488 | /*! | - | ||||||||||||||||||
| 489 | Returns the result associated with the query. | - | ||||||||||||||||||
| 490 | */ | - | ||||||||||||||||||
| 491 | - | |||||||||||||||||||
| 492 | const QSqlResult* QSqlQuery::result() const | - | ||||||||||||||||||
| 493 | { | - | ||||||||||||||||||
| 494 | return d->sqlResult; executed 155 times by 3 tests: return d->sqlResult;Executed by:
| 155 | ||||||||||||||||||
| 495 | } | - | ||||||||||||||||||
| 496 | - | |||||||||||||||||||
| 497 | /*! | - | ||||||||||||||||||
| 498 | Retrieves the record at position \a index, if available, and | - | ||||||||||||||||||
| 499 | positions the query on the retrieved record. The first record is at | - | ||||||||||||||||||
| 500 | position 0. Note that the query must be in an \l{isActive()} | - | ||||||||||||||||||
| 501 | {active} state and isSelect() must return true before calling this | - | ||||||||||||||||||
| 502 | function. | - | ||||||||||||||||||
| 503 | - | |||||||||||||||||||
| 504 | If \a relative is false (the default), the following rules apply: | - | ||||||||||||||||||
| 505 | - | |||||||||||||||||||
| 506 | \list | - | ||||||||||||||||||
| 507 | - | |||||||||||||||||||
| 508 | \li If \a index is negative, the result is positioned before the | - | ||||||||||||||||||
| 509 | first record and false is returned. | - | ||||||||||||||||||
| 510 | - | |||||||||||||||||||
| 511 | \li Otherwise, an attempt is made to move to the record at position | - | ||||||||||||||||||
| 512 | \a index. If the record at position \a index could not be retrieved, | - | ||||||||||||||||||
| 513 | the result is positioned after the last record and false is | - | ||||||||||||||||||
| 514 | returned. If the record is successfully retrieved, true is returned. | - | ||||||||||||||||||
| 515 | - | |||||||||||||||||||
| 516 | \endlist | - | ||||||||||||||||||
| 517 | - | |||||||||||||||||||
| 518 | If \a relative is true, the following rules apply: | - | ||||||||||||||||||
| 519 | - | |||||||||||||||||||
| 520 | \list | - | ||||||||||||||||||
| 521 | - | |||||||||||||||||||
| 522 | \li If the result is currently positioned before the first record and: | - | ||||||||||||||||||
| 523 | \list | - | ||||||||||||||||||
| 524 | \li \a index is negative or zero, there is no change, and false is | - | ||||||||||||||||||
| 525 | returned. | - | ||||||||||||||||||
| 526 | \li \a index is positive, an attempt is made to position the result | - | ||||||||||||||||||
| 527 | at absolute position \a index - 1, following the sames rule for non | - | ||||||||||||||||||
| 528 | relative seek, above. | - | ||||||||||||||||||
| 529 | \endlist | - | ||||||||||||||||||
| 530 | - | |||||||||||||||||||
| 531 | \li If the result is currently positioned after the last record and: | - | ||||||||||||||||||
| 532 | \list | - | ||||||||||||||||||
| 533 | \li \a index is positive or zero, there is no change, and false is | - | ||||||||||||||||||
| 534 | returned. | - | ||||||||||||||||||
| 535 | \li \a index is negative, an attempt is made to position the result | - | ||||||||||||||||||
| 536 | at \a index + 1 relative position from last record, following the | - | ||||||||||||||||||
| 537 | rule below. | - | ||||||||||||||||||
| 538 | \endlist | - | ||||||||||||||||||
| 539 | - | |||||||||||||||||||
| 540 | \li If the result is currently located somewhere in the middle, and | - | ||||||||||||||||||
| 541 | the relative offset \a index moves the result below zero, the result | - | ||||||||||||||||||
| 542 | is positioned before the first record and false is returned. | - | ||||||||||||||||||
| 543 | - | |||||||||||||||||||
| 544 | \li Otherwise, an attempt is made to move to the record \a index | - | ||||||||||||||||||
| 545 | records ahead of the current record (or \a index records behind the | - | ||||||||||||||||||
| 546 | current record if \a index is negative). If the record at offset \a | - | ||||||||||||||||||
| 547 | index could not be retrieved, the result is positioned after the | - | ||||||||||||||||||
| 548 | last record if \a index >= 0, (or before the first record if \a | - | ||||||||||||||||||
| 549 | index is negative), and false is returned. If the record is | - | ||||||||||||||||||
| 550 | successfully retrieved, true is returned. | - | ||||||||||||||||||
| 551 | - | |||||||||||||||||||
| 552 | \endlist | - | ||||||||||||||||||
| 553 | - | |||||||||||||||||||
| 554 | \sa next(), previous(), first(), last(), at(), isActive(), isValid() | - | ||||||||||||||||||
| 555 | */ | - | ||||||||||||||||||
| 556 | bool QSqlQuery::seek(int index, bool relative) | - | ||||||||||||||||||
| 557 | { | - | ||||||||||||||||||
| 558 | if (!isSelect() || !isActive())
| 0-3877 | ||||||||||||||||||
| 559 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 560 | int actualIdx; | - | ||||||||||||||||||
| 561 | if (!relative) { // arbitrary seek
| 3-3874 | ||||||||||||||||||
| 562 | if (index < 0) {
| 2-3872 | ||||||||||||||||||
| 563 | d->sqlResult->setAt(QSql::BeforeFirstRow); | - | ||||||||||||||||||
| 564 | return false; executed 2 times by 1 test: return false;Executed by:
| 2 | ||||||||||||||||||
| 565 | } | - | ||||||||||||||||||
| 566 | actualIdx = index; | - | ||||||||||||||||||
| 567 | } else { executed 3872 times by 5 tests: end of blockExecuted by:
| 3872 | ||||||||||||||||||
| 568 | switch (at()) { // relative seek | - | ||||||||||||||||||
| 569 | case QSql::BeforeFirstRow: executed 1 time by 1 test: case QSql::BeforeFirstRow:Executed by:
| 1 | ||||||||||||||||||
| 570 | if (index > 0)
| 0-1 | ||||||||||||||||||
| 571 | actualIdx = index - 1; executed 1 time by 1 test: actualIdx = index - 1;Executed by:
| 1 | ||||||||||||||||||
| 572 | else { | - | ||||||||||||||||||
| 573 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 574 | } | - | ||||||||||||||||||
| 575 | break; executed 1 time by 1 test: break;Executed by:
| 1 | ||||||||||||||||||
| 576 | case QSql::AfterLastRow: executed 1 time by 1 test: case QSql::AfterLastRow:Executed by:
| 1 | ||||||||||||||||||
| 577 | if (index < 0) {
| 0-1 | ||||||||||||||||||
| 578 | d->sqlResult->fetchLast(); | - | ||||||||||||||||||
| 579 | actualIdx = at() + index + 1; | - | ||||||||||||||||||
| 580 | } else { executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||||||||
| 581 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 582 | } | - | ||||||||||||||||||
| 583 | break; executed 1 time by 1 test: break;Executed by:
| 1 | ||||||||||||||||||
| 584 | default: executed 1 time by 1 test: default:Executed by:
| 1 | ||||||||||||||||||
| 585 | if ((at() + index) < 0) {
| 0-1 | ||||||||||||||||||
| 586 | d->sqlResult->setAt(QSql::BeforeFirstRow); | - | ||||||||||||||||||
| 587 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 588 | } | - | ||||||||||||||||||
| 589 | actualIdx = at() + index; | - | ||||||||||||||||||
| 590 | break; executed 1 time by 1 test: break;Executed by:
| 1 | ||||||||||||||||||
| 591 | } | - | ||||||||||||||||||
| 592 | } | - | ||||||||||||||||||
| 593 | // let drivers optimize | - | ||||||||||||||||||
| 594 | if (isForwardOnly() && actualIdx < at()) {
| 1-3873 | ||||||||||||||||||
| 595 | qWarning("QSqlQuery::seek: cannot seek backwards in a forward only query"); | - | ||||||||||||||||||
| 596 | return false; executed 1 time by 1 test: return false;Executed by:
| 1 | ||||||||||||||||||
| 597 | } | - | ||||||||||||||||||
| 598 | if (actualIdx == (at() + 1) && at() != QSql::BeforeFirstRow) {
| 3-2650 | ||||||||||||||||||
| 599 | if (!d->sqlResult->fetchNext()) {
| 0-1221 | ||||||||||||||||||
| 600 | d->sqlResult->setAt(QSql::AfterLastRow); | - | ||||||||||||||||||
| 601 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 602 | } | - | ||||||||||||||||||
| 603 | return true; executed 1221 times by 4 tests: return true;Executed by:
| 1221 | ||||||||||||||||||
| 604 | } | - | ||||||||||||||||||
| 605 | if (actualIdx == (at() - 1)) {
| 39-2614 | ||||||||||||||||||
| 606 | if (!d->sqlResult->fetchPrevious()) {
| 0-39 | ||||||||||||||||||
| 607 | d->sqlResult->setAt(QSql::BeforeFirstRow); | - | ||||||||||||||||||
| 608 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 609 | } | - | ||||||||||||||||||
| 610 | return true; executed 39 times by 3 tests: return true;Executed by:
| 39 | ||||||||||||||||||
| 611 | } | - | ||||||||||||||||||
| 612 | if (!d->sqlResult->fetch(actualIdx)) {
| 270-2344 | ||||||||||||||||||
| 613 | d->sqlResult->setAt(QSql::AfterLastRow); | - | ||||||||||||||||||
| 614 | return false; executed 270 times by 5 tests: return false;Executed by:
| 270 | ||||||||||||||||||
| 615 | } | - | ||||||||||||||||||
| 616 | return true; executed 2344 times by 5 tests: return true;Executed by:
| 2344 | ||||||||||||||||||
| 617 | } | - | ||||||||||||||||||
| 618 | - | |||||||||||||||||||
| 619 | /*! | - | ||||||||||||||||||
| 620 | - | |||||||||||||||||||
| 621 | Retrieves the next record in the result, if available, and positions | - | ||||||||||||||||||
| 622 | the query on the retrieved record. Note that the result must be in | - | ||||||||||||||||||
| 623 | the \l{isActive()}{active} state and isSelect() must return true | - | ||||||||||||||||||
| 624 | before calling this function or it will do nothing and return false. | - | ||||||||||||||||||
| 625 | - | |||||||||||||||||||
| 626 | The following rules apply: | - | ||||||||||||||||||
| 627 | - | |||||||||||||||||||
| 628 | \list | - | ||||||||||||||||||
| 629 | - | |||||||||||||||||||
| 630 | \li If the result is currently located before the first record, | - | ||||||||||||||||||
| 631 | e.g. immediately after a query is executed, an attempt is made to | - | ||||||||||||||||||
| 632 | retrieve the first record. | - | ||||||||||||||||||
| 633 | - | |||||||||||||||||||
| 634 | \li If the result is currently located after the last record, there | - | ||||||||||||||||||
| 635 | is no change and false is returned. | - | ||||||||||||||||||
| 636 | - | |||||||||||||||||||
| 637 | \li If the result is located somewhere in the middle, an attempt is | - | ||||||||||||||||||
| 638 | made to retrieve the next record. | - | ||||||||||||||||||
| 639 | - | |||||||||||||||||||
| 640 | \endlist | - | ||||||||||||||||||
| 641 | - | |||||||||||||||||||
| 642 | If the record could not be retrieved, the result is positioned after | - | ||||||||||||||||||
| 643 | the last record and false is returned. If the record is successfully | - | ||||||||||||||||||
| 644 | retrieved, true is returned. | - | ||||||||||||||||||
| 645 | - | |||||||||||||||||||
| 646 | \sa previous(), first(), last(), seek(), at(), isActive(), isValid() | - | ||||||||||||||||||
| 647 | */ | - | ||||||||||||||||||
| 648 | bool QSqlQuery::next() | - | ||||||||||||||||||
| 649 | { | - | ||||||||||||||||||
| 650 | if (!isSelect() || !isActive())
| 0-3171 | ||||||||||||||||||
| 651 | return false; executed 4 times by 2 tests: return false;Executed by:
| 4 | ||||||||||||||||||
| 652 | bool b = false; | - | ||||||||||||||||||
| 653 | switch (at()) { | - | ||||||||||||||||||
| 654 | case QSql::BeforeFirstRow: executed 687 times by 8 tests: case QSql::BeforeFirstRow:Executed by:
| 687 | ||||||||||||||||||
| 655 | b = d->sqlResult->fetchFirst(); | - | ||||||||||||||||||
| 656 | return b; executed 687 times by 8 tests: return b;Executed by:
| 687 | ||||||||||||||||||
| 657 | case QSql::AfterLastRow: executed 25 times by 8 tests: case QSql::AfterLastRow:Executed by:
| 25 | ||||||||||||||||||
| 658 | return false; executed 25 times by 8 tests: return false;Executed by:
| 25 | ||||||||||||||||||
| 659 | default: executed 2459 times by 8 tests: default:Executed by:
| 2459 | ||||||||||||||||||
| 660 | if (!d->sqlResult->fetchNext()) {
| 821-1638 | ||||||||||||||||||
| 661 | d->sqlResult->setAt(QSql::AfterLastRow); | - | ||||||||||||||||||
| 662 | return false; executed 821 times by 8 tests: return false;Executed by:
| 821 | ||||||||||||||||||
| 663 | } | - | ||||||||||||||||||
| 664 | return true; executed 1638 times by 8 tests: return true;Executed by:
| 1638 | ||||||||||||||||||
| 665 | } | - | ||||||||||||||||||
| 666 | } | - | ||||||||||||||||||
| 667 | - | |||||||||||||||||||
| 668 | /*! | - | ||||||||||||||||||
| 669 | - | |||||||||||||||||||
| 670 | Retrieves the previous record in the result, if available, and | - | ||||||||||||||||||
| 671 | positions the query on the retrieved record. Note that the result | - | ||||||||||||||||||
| 672 | must be in the \l{isActive()}{active} state and isSelect() must | - | ||||||||||||||||||
| 673 | return true before calling this function or it will do nothing and | - | ||||||||||||||||||
| 674 | return false. | - | ||||||||||||||||||
| 675 | - | |||||||||||||||||||
| 676 | The following rules apply: | - | ||||||||||||||||||
| 677 | - | |||||||||||||||||||
| 678 | \list | - | ||||||||||||||||||
| 679 | - | |||||||||||||||||||
| 680 | \li If the result is currently located before the first record, there | - | ||||||||||||||||||
| 681 | is no change and false is returned. | - | ||||||||||||||||||
| 682 | - | |||||||||||||||||||
| 683 | \li If the result is currently located after the last record, an | - | ||||||||||||||||||
| 684 | attempt is made to retrieve the last record. | - | ||||||||||||||||||
| 685 | - | |||||||||||||||||||
| 686 | \li If the result is somewhere in the middle, an attempt is made to | - | ||||||||||||||||||
| 687 | retrieve the previous record. | - | ||||||||||||||||||
| 688 | - | |||||||||||||||||||
| 689 | \endlist | - | ||||||||||||||||||
| 690 | - | |||||||||||||||||||
| 691 | If the record could not be retrieved, the result is positioned | - | ||||||||||||||||||
| 692 | before the first record and false is returned. If the record is | - | ||||||||||||||||||
| 693 | successfully retrieved, true is returned. | - | ||||||||||||||||||
| 694 | - | |||||||||||||||||||
| 695 | \sa next(), first(), last(), seek(), at(), isActive(), isValid() | - | ||||||||||||||||||
| 696 | */ | - | ||||||||||||||||||
| 697 | bool QSqlQuery::previous() | - | ||||||||||||||||||
| 698 | { | - | ||||||||||||||||||
| 699 | if (!isSelect() || !isActive())
| 0-3 | ||||||||||||||||||
| 700 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 701 | if (isForwardOnly()) {
| 1-2 | ||||||||||||||||||
| 702 | qWarning("QSqlQuery::seek: cannot seek backwards in a forward only query"); | - | ||||||||||||||||||
| 703 | return false; executed 1 time by 1 test: return false;Executed by:
| 1 | ||||||||||||||||||
| 704 | } | - | ||||||||||||||||||
| 705 | - | |||||||||||||||||||
| 706 | bool b = false; | - | ||||||||||||||||||
| 707 | switch (at()) { | - | ||||||||||||||||||
| 708 | case QSql::BeforeFirstRow: never executed: case QSql::BeforeFirstRow: | 0 | ||||||||||||||||||
| 709 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 710 | case QSql::AfterLastRow: executed 1 time by 1 test: case QSql::AfterLastRow:Executed by:
| 1 | ||||||||||||||||||
| 711 | b = d->sqlResult->fetchLast(); | - | ||||||||||||||||||
| 712 | return b; executed 1 time by 1 test: return b;Executed by:
| 1 | ||||||||||||||||||
| 713 | default: executed 1 time by 1 test: default:Executed by:
| 1 | ||||||||||||||||||
| 714 | if (!d->sqlResult->fetchPrevious()) {
| 0-1 | ||||||||||||||||||
| 715 | d->sqlResult->setAt(QSql::BeforeFirstRow); | - | ||||||||||||||||||
| 716 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 717 | } | - | ||||||||||||||||||
| 718 | return true; executed 1 time by 1 test: return true;Executed by:
| 1 | ||||||||||||||||||
| 719 | } | - | ||||||||||||||||||
| 720 | } | - | ||||||||||||||||||
| 721 | - | |||||||||||||||||||
| 722 | /*! | - | ||||||||||||||||||
| 723 | Retrieves the first record in the result, if available, and | - | ||||||||||||||||||
| 724 | positions the query on the retrieved record. Note that the result | - | ||||||||||||||||||
| 725 | must be in the \l{isActive()}{active} state and isSelect() must | - | ||||||||||||||||||
| 726 | return true before calling this function or it will do nothing and | - | ||||||||||||||||||
| 727 | return false. Returns \c true if successful. If unsuccessful the query | - | ||||||||||||||||||
| 728 | position is set to an invalid position and false is returned. | - | ||||||||||||||||||
| 729 | - | |||||||||||||||||||
| 730 | \sa next(), previous(), last(), seek(), at(), isActive(), isValid() | - | ||||||||||||||||||
| 731 | */ | - | ||||||||||||||||||
| 732 | bool QSqlQuery::first() | - | ||||||||||||||||||
| 733 | { | - | ||||||||||||||||||
| 734 | if (!isSelect() || !isActive())
| 0-18 | ||||||||||||||||||
| 735 | return false; executed 1 time by 1 test: return false;Executed by:
| 1 | ||||||||||||||||||
| 736 | if (isForwardOnly() && at() > QSql::BeforeFirstRow) {
| 1-15 | ||||||||||||||||||
| 737 | qWarning("QSqlQuery::seek: cannot seek backwards in a forward only query"); | - | ||||||||||||||||||
| 738 | return false; executed 2 times by 1 test: return false;Executed by:
| 2 | ||||||||||||||||||
| 739 | } | - | ||||||||||||||||||
| 740 | bool b = false; | - | ||||||||||||||||||
| 741 | b = d->sqlResult->fetchFirst(); | - | ||||||||||||||||||
| 742 | return b; executed 16 times by 2 tests: return b;Executed by:
| 16 | ||||||||||||||||||
| 743 | } | - | ||||||||||||||||||
| 744 | - | |||||||||||||||||||
| 745 | /*! | - | ||||||||||||||||||
| 746 | - | |||||||||||||||||||
| 747 | Retrieves the last record in the result, if available, and positions | - | ||||||||||||||||||
| 748 | the query on the retrieved record. Note that the result must be in | - | ||||||||||||||||||
| 749 | the \l{isActive()}{active} state and isSelect() must return true | - | ||||||||||||||||||
| 750 | before calling this function or it will do nothing and return false. | - | ||||||||||||||||||
| 751 | Returns \c true if successful. If unsuccessful the query position is | - | ||||||||||||||||||
| 752 | set to an invalid position and false is returned. | - | ||||||||||||||||||
| 753 | - | |||||||||||||||||||
| 754 | \sa next(), previous(), first(), seek(), at(), isActive(), isValid() | - | ||||||||||||||||||
| 755 | */ | - | ||||||||||||||||||
| 756 | - | |||||||||||||||||||
| 757 | bool QSqlQuery::last() | - | ||||||||||||||||||
| 758 | { | - | ||||||||||||||||||
| 759 | if (!isSelect() || !isActive())
| 0-8 | ||||||||||||||||||
| 760 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 761 | bool b = false; | - | ||||||||||||||||||
| 762 | b = d->sqlResult->fetchLast(); | - | ||||||||||||||||||
| 763 | return b; executed 8 times by 1 test: return b;Executed by:
| 8 | ||||||||||||||||||
| 764 | } | - | ||||||||||||||||||
| 765 | - | |||||||||||||||||||
| 766 | /*! | - | ||||||||||||||||||
| 767 | Returns the size of the result (number of rows returned), or -1 if | - | ||||||||||||||||||
| 768 | the size cannot be determined or if the database does not support | - | ||||||||||||||||||
| 769 | reporting information about query sizes. Note that for non-\c SELECT | - | ||||||||||||||||||
| 770 | statements (isSelect() returns \c false), size() will return -1. If the | - | ||||||||||||||||||
| 771 | query is not active (isActive() returns \c false), -1 is returned. | - | ||||||||||||||||||
| 772 | - | |||||||||||||||||||
| 773 | To determine the number of rows affected by a non-\c SELECT | - | ||||||||||||||||||
| 774 | statement, use numRowsAffected(). | - | ||||||||||||||||||
| 775 | - | |||||||||||||||||||
| 776 | \sa isActive(), numRowsAffected(), QSqlDriver::hasFeature() | - | ||||||||||||||||||
| 777 | */ | - | ||||||||||||||||||
| 778 | int QSqlQuery::size() const | - | ||||||||||||||||||
| 779 | { | - | ||||||||||||||||||
| 780 | if (isActive() && d->sqlResult->driver()->hasFeature(QSqlDriver::QuerySize))
| 0-5 | ||||||||||||||||||
| 781 | return d->sqlResult->size(); never executed: return d->sqlResult->size(); | 0 | ||||||||||||||||||
| 782 | return -1; executed 6 times by 1 test: return -1;Executed by:
| 6 | ||||||||||||||||||
| 783 | } | - | ||||||||||||||||||
| 784 | - | |||||||||||||||||||
| 785 | /*! | - | ||||||||||||||||||
| 786 | Returns the number of rows affected by the result's SQL statement, | - | ||||||||||||||||||
| 787 | or -1 if it cannot be determined. Note that for \c SELECT | - | ||||||||||||||||||
| 788 | statements, the value is undefined; use size() instead. If the query | - | ||||||||||||||||||
| 789 | is not \l{isActive()}{active}, -1 is returned. | - | ||||||||||||||||||
| 790 | - | |||||||||||||||||||
| 791 | \sa size(), QSqlDriver::hasFeature() | - | ||||||||||||||||||
| 792 | */ | - | ||||||||||||||||||
| 793 | - | |||||||||||||||||||
| 794 | int QSqlQuery::numRowsAffected() const | - | ||||||||||||||||||
| 795 | { | - | ||||||||||||||||||
| 796 | if (isActive())
| 2-19 | ||||||||||||||||||
| 797 | return d->sqlResult->numRowsAffected(); executed 19 times by 1 test: return d->sqlResult->numRowsAffected();Executed by:
| 19 | ||||||||||||||||||
| 798 | return -1; executed 2 times by 1 test: return -1;Executed by:
| 2 | ||||||||||||||||||
| 799 | } | - | ||||||||||||||||||
| 800 | - | |||||||||||||||||||
| 801 | /*! | - | ||||||||||||||||||
| 802 | Returns error information about the last error (if any) that | - | ||||||||||||||||||
| 803 | occurred with this query. | - | ||||||||||||||||||
| 804 | - | |||||||||||||||||||
| 805 | \sa QSqlError, QSqlDatabase::lastError() | - | ||||||||||||||||||
| 806 | */ | - | ||||||||||||||||||
| 807 | - | |||||||||||||||||||
| 808 | QSqlError QSqlQuery::lastError() const | - | ||||||||||||||||||
| 809 | { | - | ||||||||||||||||||
| 810 | return d->sqlResult->lastError(); executed 11093 times by 7 tests: return d->sqlResult->lastError();Executed by:
| 11093 | ||||||||||||||||||
| 811 | } | - | ||||||||||||||||||
| 812 | - | |||||||||||||||||||
| 813 | /*! | - | ||||||||||||||||||
| 814 | Returns \c true if the query is currently positioned on a valid | - | ||||||||||||||||||
| 815 | record; otherwise returns \c false. | - | ||||||||||||||||||
| 816 | */ | - | ||||||||||||||||||
| 817 | - | |||||||||||||||||||
| 818 | bool QSqlQuery::isValid() const | - | ||||||||||||||||||
| 819 | { | - | ||||||||||||||||||
| 820 | return d->sqlResult->isValid(); executed 9645 times by 8 tests: return d->sqlResult->isValid();Executed by:
| 9645 | ||||||||||||||||||
| 821 | } | - | ||||||||||||||||||
| 822 | - | |||||||||||||||||||
| 823 | /*! | - | ||||||||||||||||||
| 824 | - | |||||||||||||||||||
| 825 | Returns \c true if the query is \e{active}. An active QSqlQuery is one | - | ||||||||||||||||||
| 826 | that has been \l{QSqlQuery::exec()} {exec()'d} successfully but not | - | ||||||||||||||||||
| 827 | yet finished with. When you are finished with an active query, you | - | ||||||||||||||||||
| 828 | can make the query inactive by calling finish() or clear(), or | - | ||||||||||||||||||
| 829 | you can delete the QSqlQuery instance. | - | ||||||||||||||||||
| 830 | - | |||||||||||||||||||
| 831 | \note Of particular interest is an active query that is a \c{SELECT} | - | ||||||||||||||||||
| 832 | statement. For some databases that support transactions, an active | - | ||||||||||||||||||
| 833 | query that is a \c{SELECT} statement can cause a \l{QSqlDatabase::} | - | ||||||||||||||||||
| 834 | {commit()} or a \l{QSqlDatabase::} {rollback()} to fail, so before | - | ||||||||||||||||||
| 835 | committing or rolling back, you should make your active \c{SELECT} | - | ||||||||||||||||||
| 836 | statement query inactive using one of the ways listed above. | - | ||||||||||||||||||
| 837 | - | |||||||||||||||||||
| 838 | \sa isSelect() | - | ||||||||||||||||||
| 839 | */ | - | ||||||||||||||||||
| 840 | bool QSqlQuery::isActive() const | - | ||||||||||||||||||
| 841 | { | - | ||||||||||||||||||
| 842 | return d->sqlResult->isActive(); executed 16973 times by 9 tests: return d->sqlResult->isActive();Executed by:
| 16973 | ||||||||||||||||||
| 843 | } | - | ||||||||||||||||||
| 844 | - | |||||||||||||||||||
| 845 | /*! | - | ||||||||||||||||||
| 846 | Returns \c true if the current query is a \c SELECT statement; | - | ||||||||||||||||||
| 847 | otherwise returns \c false. | - | ||||||||||||||||||
| 848 | */ | - | ||||||||||||||||||
| 849 | - | |||||||||||||||||||
| 850 | bool QSqlQuery::isSelect() const | - | ||||||||||||||||||
| 851 | { | - | ||||||||||||||||||
| 852 | return d->sqlResult->isSelect(); executed 7084 times by 9 tests: return d->sqlResult->isSelect();Executed by:
| 7084 | ||||||||||||||||||
| 853 | } | - | ||||||||||||||||||
| 854 | - | |||||||||||||||||||
| 855 | /*! | - | ||||||||||||||||||
| 856 | Returns \c true if you can only scroll forward through a result set; | - | ||||||||||||||||||
| 857 | otherwise returns \c false. | - | ||||||||||||||||||
| 858 | - | |||||||||||||||||||
| 859 | \sa setForwardOnly(), next() | - | ||||||||||||||||||
| 860 | */ | - | ||||||||||||||||||
| 861 | bool QSqlQuery::isForwardOnly() const | - | ||||||||||||||||||
| 862 | { | - | ||||||||||||||||||
| 863 | return d->sqlResult->isForwardOnly(); executed 4291 times by 6 tests: return d->sqlResult->isForwardOnly();Executed by:
| 4291 | ||||||||||||||||||
| 864 | } | - | ||||||||||||||||||
| 865 | - | |||||||||||||||||||
| 866 | /*! | - | ||||||||||||||||||
| 867 | Sets forward only mode to \a forward. If \a forward is true, only | - | ||||||||||||||||||
| 868 | next() and seek() with positive values, are allowed for navigating | - | ||||||||||||||||||
| 869 | the results. | - | ||||||||||||||||||
| 870 | - | |||||||||||||||||||
| 871 | Forward only mode can be (depending on the driver) more memory | - | ||||||||||||||||||
| 872 | efficient since results do not need to be cached. It will also | - | ||||||||||||||||||
| 873 | improve performance on some databases. For this to be true, you must | - | ||||||||||||||||||
| 874 | call \c setForwardOnly() before the query is prepared or executed. | - | ||||||||||||||||||
| 875 | Note that the constructor that takes a query and a database may | - | ||||||||||||||||||
| 876 | execute the query. | - | ||||||||||||||||||
| 877 | - | |||||||||||||||||||
| 878 | Forward only mode is off by default. | - | ||||||||||||||||||
| 879 | - | |||||||||||||||||||
| 880 | Setting forward only to false is a suggestion to the database engine, | - | ||||||||||||||||||
| 881 | which has the final say on whether a result set is forward only or | - | ||||||||||||||||||
| 882 | scrollable. isForwardOnly() will always return the correct status of | - | ||||||||||||||||||
| 883 | the result set. | - | ||||||||||||||||||
| 884 | - | |||||||||||||||||||
| 885 | \note Calling setForwardOnly after execution of the query will result | - | ||||||||||||||||||
| 886 | in unexpected results at best, and crashes at worst. | - | ||||||||||||||||||
| 887 | - | |||||||||||||||||||
| 888 | \sa isForwardOnly(), next(), seek(), QSqlResult::setForwardOnly() | - | ||||||||||||||||||
| 889 | */ | - | ||||||||||||||||||
| 890 | void QSqlQuery::setForwardOnly(bool forward) | - | ||||||||||||||||||
| 891 | { | - | ||||||||||||||||||
| 892 | d->sqlResult->setForwardOnly(forward); | - | ||||||||||||||||||
| 893 | } executed 647 times by 9 tests: end of blockExecuted by:
| 647 | ||||||||||||||||||
| 894 | - | |||||||||||||||||||
| 895 | /*! | - | ||||||||||||||||||
| 896 | Returns a QSqlRecord containing the field information for the | - | ||||||||||||||||||
| 897 | current query. If the query points to a valid row (isValid() returns | - | ||||||||||||||||||
| 898 | true), the record is populated with the row's values. An empty | - | ||||||||||||||||||
| 899 | record is returned when there is no active query (isActive() returns | - | ||||||||||||||||||
| 900 | false). | - | ||||||||||||||||||
| 901 | - | |||||||||||||||||||
| 902 | To retrieve values from a query, value() should be used since | - | ||||||||||||||||||
| 903 | its index-based lookup is faster. | - | ||||||||||||||||||
| 904 | - | |||||||||||||||||||
| 905 | In the following example, a \c{SELECT * FROM} query is executed. | - | ||||||||||||||||||
| 906 | Since the order of the columns is not defined, QSqlRecord::indexOf() | - | ||||||||||||||||||
| 907 | is used to obtain the index of a column. | - | ||||||||||||||||||
| 908 | - | |||||||||||||||||||
| 909 | \snippet code/src_sql_kernel_qsqlquery.cpp 1 | - | ||||||||||||||||||
| 910 | - | |||||||||||||||||||
| 911 | \sa value() | - | ||||||||||||||||||
| 912 | */ | - | ||||||||||||||||||
| 913 | QSqlRecord QSqlQuery::record() const | - | ||||||||||||||||||
| 914 | { | - | ||||||||||||||||||
| 915 | QSqlRecord rec = d->sqlResult->record(); | - | ||||||||||||||||||
| 916 | - | |||||||||||||||||||
| 917 | if (isValid()) {
| 100-403 | ||||||||||||||||||
| 918 | for (int i = 0; i < rec.count(); ++i)
| 100-241 | ||||||||||||||||||
| 919 | rec.setValue(i, value(i)); executed 241 times by 5 tests: rec.setValue(i, value(i));Executed by:
| 241 | ||||||||||||||||||
| 920 | } executed 100 times by 5 tests: end of blockExecuted by:
| 100 | ||||||||||||||||||
| 921 | return rec; executed 503 times by 7 tests: return rec;Executed by:
| 503 | ||||||||||||||||||
| 922 | } | - | ||||||||||||||||||
| 923 | - | |||||||||||||||||||
| 924 | /*! | - | ||||||||||||||||||
| 925 | Clears the result set and releases any resources held by the | - | ||||||||||||||||||
| 926 | query. Sets the query state to inactive. You should rarely if ever | - | ||||||||||||||||||
| 927 | need to call this function. | - | ||||||||||||||||||
| 928 | */ | - | ||||||||||||||||||
| 929 | void QSqlQuery::clear() | - | ||||||||||||||||||
| 930 | { | - | ||||||||||||||||||
| 931 | *this = QSqlQuery(driver()->createResult()); | - | ||||||||||||||||||
| 932 | } executed 398 times by 7 tests: end of blockExecuted by:
| 398 | ||||||||||||||||||
| 933 | - | |||||||||||||||||||
| 934 | /*! | - | ||||||||||||||||||
| 935 | Prepares the SQL query \a query for execution. Returns \c true if the | - | ||||||||||||||||||
| 936 | query is prepared successfully; otherwise returns \c false. | - | ||||||||||||||||||
| 937 | - | |||||||||||||||||||
| 938 | The query may contain placeholders for binding values. Both Oracle | - | ||||||||||||||||||
| 939 | style colon-name (e.g., \c{:surname}), and ODBC style (\c{?}) | - | ||||||||||||||||||
| 940 | placeholders are supported; but they cannot be mixed in the same | - | ||||||||||||||||||
| 941 | query. See the \l{QSqlQuery examples}{Detailed Description} for | - | ||||||||||||||||||
| 942 | examples. | - | ||||||||||||||||||
| 943 | - | |||||||||||||||||||
| 944 | Portability note: Some databases choose to delay preparing a query | - | ||||||||||||||||||
| 945 | until it is executed the first time. In this case, preparing a | - | ||||||||||||||||||
| 946 | syntactically wrong query succeeds, but every consecutive exec() | - | ||||||||||||||||||
| 947 | will fail. | - | ||||||||||||||||||
| 948 | - | |||||||||||||||||||
| 949 | For SQLite, the query string can contain only one statement at a time. | - | ||||||||||||||||||
| 950 | If more than one statement is given, the function returns \c false. | - | ||||||||||||||||||
| 951 | - | |||||||||||||||||||
| 952 | Example: | - | ||||||||||||||||||
| 953 | - | |||||||||||||||||||
| 954 | \snippet sqldatabase/sqldatabase.cpp 9 | - | ||||||||||||||||||
| 955 | - | |||||||||||||||||||
| 956 | \sa exec(), bindValue(), addBindValue() | - | ||||||||||||||||||
| 957 | */ | - | ||||||||||||||||||
| 958 | bool QSqlQuery::prepare(const QString& query) | - | ||||||||||||||||||
| 959 | { | - | ||||||||||||||||||
| 960 | if (d->ref.load() != 1) {
| 2-230 | ||||||||||||||||||
| 961 | bool fo = isForwardOnly(); | - | ||||||||||||||||||
| 962 | *this = QSqlQuery(driver()->createResult()); | - | ||||||||||||||||||
| 963 | setForwardOnly(fo); | - | ||||||||||||||||||
| 964 | d->sqlResult->setNumericalPrecisionPolicy(d->sqlResult->numericalPrecisionPolicy()); | - | ||||||||||||||||||
| 965 | } else { executed 2 times by 1 test: end of blockExecuted by:
| 2 | ||||||||||||||||||
| 966 | d->sqlResult->setActive(false); | - | ||||||||||||||||||
| 967 | d->sqlResult->setLastError(QSqlError()); | - | ||||||||||||||||||
| 968 | d->sqlResult->setAt(QSql::BeforeFirstRow); | - | ||||||||||||||||||
| 969 | d->sqlResult->setNumericalPrecisionPolicy(d->sqlResult->numericalPrecisionPolicy()); | - | ||||||||||||||||||
| 970 | } executed 230 times by 7 tests: end of blockExecuted by:
| 230 | ||||||||||||||||||
| 971 | if (!driver()) {
| 0-232 | ||||||||||||||||||
| 972 | qWarning("QSqlQuery::prepare: no driver"); | - | ||||||||||||||||||
| 973 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 974 | } | - | ||||||||||||||||||
| 975 | if (!driver()->isOpen() || driver()->isOpenError()) {
| 0-232 | ||||||||||||||||||
| 976 | qWarning("QSqlQuery::prepare: database not open"); | - | ||||||||||||||||||
| 977 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 978 | } | - | ||||||||||||||||||
| 979 | if (query.isEmpty()) {
| 0-232 | ||||||||||||||||||
| 980 | qWarning("QSqlQuery::prepare: empty query"); | - | ||||||||||||||||||
| 981 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 982 | } | - | ||||||||||||||||||
| 983 | #ifdef QT_DEBUG_SQL | - | ||||||||||||||||||
| 984 | qDebug("\n QSqlQuery::prepare: %s", query.toLocal8Bit().constData()); | - | ||||||||||||||||||
| 985 | #endif | - | ||||||||||||||||||
| 986 | return d->sqlResult->savePrepare(query); executed 232 times by 7 tests: return d->sqlResult->savePrepare(query);Executed by:
| 232 | ||||||||||||||||||
| 987 | } | - | ||||||||||||||||||
| 988 | - | |||||||||||||||||||
| 989 | /*! | - | ||||||||||||||||||
| 990 | Executes a previously prepared SQL query. Returns \c true if the query | - | ||||||||||||||||||
| 991 | executed successfully; otherwise returns \c false. | - | ||||||||||||||||||
| 992 | - | |||||||||||||||||||
| 993 | Note that the last error for this query is reset when exec() is | - | ||||||||||||||||||
| 994 | called. | - | ||||||||||||||||||
| 995 | - | |||||||||||||||||||
| 996 | \sa prepare(), bindValue(), addBindValue(), boundValue(), boundValues() | - | ||||||||||||||||||
| 997 | */ | - | ||||||||||||||||||
| 998 | bool QSqlQuery::exec() | - | ||||||||||||||||||
| 999 | { | - | ||||||||||||||||||
| 1000 | #ifdef QT_DEBUG_SQL | - | ||||||||||||||||||
| 1001 | QElapsedTimer t; | - | ||||||||||||||||||
| 1002 | t.start(); | - | ||||||||||||||||||
| 1003 | #endif | - | ||||||||||||||||||
| 1004 | d->sqlResult->resetBindCount(); | - | ||||||||||||||||||
| 1005 | - | |||||||||||||||||||
| 1006 | if (d->sqlResult->lastError().isValid())
| 24656-92072 | ||||||||||||||||||
| 1007 | d->sqlResult->setLastError(QSqlError()); executed 92072 times by 2 tests: d->sqlResult->setLastError(QSqlError());Executed by:
| 92072 | ||||||||||||||||||
| 1008 | - | |||||||||||||||||||
| 1009 | bool retval = d->sqlResult->exec(); | - | ||||||||||||||||||
| 1010 | #ifdef QT_DEBUG_SQL | - | ||||||||||||||||||
| 1011 | qDebug().nospace() << "Executed prepared query (" << t.elapsed() << "ms, " | - | ||||||||||||||||||
| 1012 | << d->sqlResult->size() << " results, " << d->sqlResult->numRowsAffected() | - | ||||||||||||||||||
| 1013 | << " affected): " << d->sqlResult->lastQuery(); | - | ||||||||||||||||||
| 1014 | #endif | - | ||||||||||||||||||
| 1015 | return retval; executed 116728 times by 7 tests: return retval;Executed by:
| 116728 | ||||||||||||||||||
| 1016 | } | - | ||||||||||||||||||
| 1017 | - | |||||||||||||||||||
| 1018 | /*! \enum QSqlQuery::BatchExecutionMode | - | ||||||||||||||||||
| 1019 | - | |||||||||||||||||||
| 1020 | \value ValuesAsRows - Updates multiple rows. Treats every entry in a QVariantList as a value for updating the next row. | - | ||||||||||||||||||
| 1021 | \value ValuesAsColumns - Updates a single row. Treats every entry in a QVariantList as a single value of an array type. | - | ||||||||||||||||||
| 1022 | */ | - | ||||||||||||||||||
| 1023 | - | |||||||||||||||||||
| 1024 | /*! | - | ||||||||||||||||||
| 1025 | \since 4.2 | - | ||||||||||||||||||
| 1026 | - | |||||||||||||||||||
| 1027 | Executes a previously prepared SQL query in a batch. All the bound | - | ||||||||||||||||||
| 1028 | parameters have to be lists of variants. If the database doesn't | - | ||||||||||||||||||
| 1029 | support batch executions, the driver will simulate it using | - | ||||||||||||||||||
| 1030 | conventional exec() calls. | - | ||||||||||||||||||
| 1031 | - | |||||||||||||||||||
| 1032 | Returns \c true if the query is executed successfully; otherwise | - | ||||||||||||||||||
| 1033 | returns \c false. | - | ||||||||||||||||||
| 1034 | - | |||||||||||||||||||
| 1035 | Example: | - | ||||||||||||||||||
| 1036 | - | |||||||||||||||||||
| 1037 | \snippet code/src_sql_kernel_qsqlquery.cpp 2 | - | ||||||||||||||||||
| 1038 | - | |||||||||||||||||||
| 1039 | The example above inserts four new rows into \c myTable: | - | ||||||||||||||||||
| 1040 | - | |||||||||||||||||||
| 1041 | \snippet code/src_sql_kernel_qsqlquery.cpp 3 | - | ||||||||||||||||||
| 1042 | - | |||||||||||||||||||
| 1043 | To bind NULL values, a null QVariant of the relevant type has to be | - | ||||||||||||||||||
| 1044 | added to the bound QVariantList; for example, \c | - | ||||||||||||||||||
| 1045 | {QVariant(QVariant::String)} should be used if you are using | - | ||||||||||||||||||
| 1046 | strings. | - | ||||||||||||||||||
| 1047 | - | |||||||||||||||||||
| 1048 | \note Every bound QVariantList must contain the same amount of | - | ||||||||||||||||||
| 1049 | variants. | - | ||||||||||||||||||
| 1050 | - | |||||||||||||||||||
| 1051 | \note The type of the QVariants in a list must not change. For | - | ||||||||||||||||||
| 1052 | example, you cannot mix integer and string variants within a | - | ||||||||||||||||||
| 1053 | QVariantList. | - | ||||||||||||||||||
| 1054 | - | |||||||||||||||||||
| 1055 | The \a mode parameter indicates how the bound QVariantList will be | - | ||||||||||||||||||
| 1056 | interpreted. If \a mode is \c ValuesAsRows, every variant within | - | ||||||||||||||||||
| 1057 | the QVariantList will be interpreted as a value for a new row. \c | - | ||||||||||||||||||
| 1058 | ValuesAsColumns is a special case for the Oracle driver. In this | - | ||||||||||||||||||
| 1059 | mode, every entry within a QVariantList will be interpreted as | - | ||||||||||||||||||
| 1060 | array-value for an IN or OUT value within a stored procedure. Note | - | ||||||||||||||||||
| 1061 | that this will only work if the IN or OUT value is a table-type | - | ||||||||||||||||||
| 1062 | consisting of only one column of a basic type, for example \c{TYPE | - | ||||||||||||||||||
| 1063 | myType IS TABLE OF VARCHAR(64) INDEX BY BINARY_INTEGER;} | - | ||||||||||||||||||
| 1064 | - | |||||||||||||||||||
| 1065 | \sa prepare(), bindValue(), addBindValue() | - | ||||||||||||||||||
| 1066 | */ | - | ||||||||||||||||||
| 1067 | bool QSqlQuery::execBatch(BatchExecutionMode mode) | - | ||||||||||||||||||
| 1068 | { | - | ||||||||||||||||||
| 1069 | d->sqlResult->resetBindCount(); | - | ||||||||||||||||||
| 1070 | return d->sqlResult->execBatch(mode == ValuesAsColumns); executed 2 times by 1 test: return d->sqlResult->execBatch(mode == ValuesAsColumns);Executed by:
| 2 | ||||||||||||||||||
| 1071 | } | - | ||||||||||||||||||
| 1072 | - | |||||||||||||||||||
| 1073 | /*! | - | ||||||||||||||||||
| 1074 | Set the placeholder \a placeholder to be bound to value \a val in | - | ||||||||||||||||||
| 1075 | the prepared statement. Note that the placeholder mark (e.g \c{:}) | - | ||||||||||||||||||
| 1076 | must be included when specifying the placeholder name. If \a | - | ||||||||||||||||||
| 1077 | paramType is QSql::Out or QSql::InOut, the placeholder will be | - | ||||||||||||||||||
| 1078 | overwritten with data from the database after the exec() call. | - | ||||||||||||||||||
| 1079 | In this case, sufficient space must be pre-allocated to store | - | ||||||||||||||||||
| 1080 | the result into. | - | ||||||||||||||||||
| 1081 | - | |||||||||||||||||||
| 1082 | To bind a NULL value, use a null QVariant; for example, use | - | ||||||||||||||||||
| 1083 | \c {QVariant(QVariant::String)} if you are binding a string. | - | ||||||||||||||||||
| 1084 | - | |||||||||||||||||||
| 1085 | \sa addBindValue(), prepare(), exec(), boundValue(), boundValues() | - | ||||||||||||||||||
| 1086 | */ | - | ||||||||||||||||||
| 1087 | void QSqlQuery::bindValue(const QString& placeholder, const QVariant& val, | - | ||||||||||||||||||
| 1088 | QSql::ParamType paramType | - | ||||||||||||||||||
| 1089 | ) | - | ||||||||||||||||||
| 1090 | { | - | ||||||||||||||||||
| 1091 | d->sqlResult->bindValue(placeholder, val, paramType); | - | ||||||||||||||||||
| 1092 | } executed 48 times by 2 tests: end of blockExecuted by:
| 48 | ||||||||||||||||||
| 1093 | - | |||||||||||||||||||
| 1094 | /*! | - | ||||||||||||||||||
| 1095 | Set the placeholder in position \a pos to be bound to value \a val | - | ||||||||||||||||||
| 1096 | in the prepared statement. Field numbering starts at 0. If \a | - | ||||||||||||||||||
| 1097 | paramType is QSql::Out or QSql::InOut, the placeholder will be | - | ||||||||||||||||||
| 1098 | overwritten with data from the database after the exec() call. | - | ||||||||||||||||||
| 1099 | */ | - | ||||||||||||||||||
| 1100 | void QSqlQuery::bindValue(int pos, const QVariant& val, QSql::ParamType paramType) | - | ||||||||||||||||||
| 1101 | { | - | ||||||||||||||||||
| 1102 | d->sqlResult->bindValue(pos, val, paramType); | - | ||||||||||||||||||
| 1103 | } executed 89 times by 4 tests: end of blockExecuted by:
| 89 | ||||||||||||||||||
| 1104 | - | |||||||||||||||||||
| 1105 | /*! | - | ||||||||||||||||||
| 1106 | Adds the value \a val to the list of values when using positional | - | ||||||||||||||||||
| 1107 | value binding. The order of the addBindValue() calls determines | - | ||||||||||||||||||
| 1108 | which placeholder a value will be bound to in the prepared query. | - | ||||||||||||||||||
| 1109 | If \a paramType is QSql::Out or QSql::InOut, the placeholder will be | - | ||||||||||||||||||
| 1110 | overwritten with data from the database after the exec() call. | - | ||||||||||||||||||
| 1111 | - | |||||||||||||||||||
| 1112 | To bind a NULL value, use a null QVariant; for example, use \c | - | ||||||||||||||||||
| 1113 | {QVariant(QVariant::String)} if you are binding a string. | - | ||||||||||||||||||
| 1114 | - | |||||||||||||||||||
| 1115 | \sa bindValue(), prepare(), exec(), boundValue(), boundValues() | - | ||||||||||||||||||
| 1116 | */ | - | ||||||||||||||||||
| 1117 | void QSqlQuery::addBindValue(const QVariant& val, QSql::ParamType paramType) | - | ||||||||||||||||||
| 1118 | { | - | ||||||||||||||||||
| 1119 | d->sqlResult->addBindValue(val, paramType); | - | ||||||||||||||||||
| 1120 | } executed 233364 times by 5 tests: end of blockExecuted by:
| 233364 | ||||||||||||||||||
| 1121 | - | |||||||||||||||||||
| 1122 | /*! | - | ||||||||||||||||||
| 1123 | Returns the value for the \a placeholder. | - | ||||||||||||||||||
| 1124 | - | |||||||||||||||||||
| 1125 | \sa boundValues(), bindValue(), addBindValue() | - | ||||||||||||||||||
| 1126 | */ | - | ||||||||||||||||||
| 1127 | QVariant QSqlQuery::boundValue(const QString& placeholder) const | - | ||||||||||||||||||
| 1128 | { | - | ||||||||||||||||||
| 1129 | return d->sqlResult->boundValue(placeholder); never executed: return d->sqlResult->boundValue(placeholder); | 0 | ||||||||||||||||||
| 1130 | } | - | ||||||||||||||||||
| 1131 | - | |||||||||||||||||||
| 1132 | /*! | - | ||||||||||||||||||
| 1133 | Returns the value for the placeholder at position \a pos. | - | ||||||||||||||||||
| 1134 | */ | - | ||||||||||||||||||
| 1135 | QVariant QSqlQuery::boundValue(int pos) const | - | ||||||||||||||||||
| 1136 | { | - | ||||||||||||||||||
| 1137 | return d->sqlResult->boundValue(pos); executed 1 time by 1 test: return d->sqlResult->boundValue(pos);Executed by:
| 1 | ||||||||||||||||||
| 1138 | } | - | ||||||||||||||||||
| 1139 | - | |||||||||||||||||||
| 1140 | /*! | - | ||||||||||||||||||
| 1141 | Returns a map of the bound values. | - | ||||||||||||||||||
| 1142 | - | |||||||||||||||||||
| 1143 | With named binding, the bound values can be examined in the | - | ||||||||||||||||||
| 1144 | following ways: | - | ||||||||||||||||||
| 1145 | - | |||||||||||||||||||
| 1146 | \snippet sqldatabase/sqldatabase.cpp 14 | - | ||||||||||||||||||
| 1147 | - | |||||||||||||||||||
| 1148 | With positional binding, the code becomes: | - | ||||||||||||||||||
| 1149 | - | |||||||||||||||||||
| 1150 | \snippet sqldatabase/sqldatabase.cpp 15 | - | ||||||||||||||||||
| 1151 | - | |||||||||||||||||||
| 1152 | \sa boundValue(), bindValue(), addBindValue() | - | ||||||||||||||||||
| 1153 | */ | - | ||||||||||||||||||
| 1154 | QMap<QString,QVariant> QSqlQuery::boundValues() const | - | ||||||||||||||||||
| 1155 | { | - | ||||||||||||||||||
| 1156 | QMap<QString,QVariant> map; | - | ||||||||||||||||||
| 1157 | - | |||||||||||||||||||
| 1158 | const QVector<QVariant> values(d->sqlResult->boundValues()); | - | ||||||||||||||||||
| 1159 | for (int i = 0; i < values.count(); ++i)
| 53-122 | ||||||||||||||||||
| 1160 | map[d->sqlResult->boundValueName(i)] = values.at(i); executed 122 times by 1 test: map[d->sqlResult->boundValueName(i)] = values.at(i);Executed by:
| 122 | ||||||||||||||||||
| 1161 | return map; executed 53 times by 1 test: return map;Executed by:
| 53 | ||||||||||||||||||
| 1162 | } | - | ||||||||||||||||||
| 1163 | - | |||||||||||||||||||
| 1164 | /*! | - | ||||||||||||||||||
| 1165 | Returns the last query that was successfully executed. | - | ||||||||||||||||||
| 1166 | - | |||||||||||||||||||
| 1167 | In most cases this function returns the same string as lastQuery(). | - | ||||||||||||||||||
| 1168 | If a prepared query with placeholders is executed on a DBMS that | - | ||||||||||||||||||
| 1169 | does not support it, the preparation of this query is emulated. The | - | ||||||||||||||||||
| 1170 | placeholders in the original query are replaced with their bound | - | ||||||||||||||||||
| 1171 | values to form a new query. This function returns the modified | - | ||||||||||||||||||
| 1172 | query. It is mostly useful for debugging purposes. | - | ||||||||||||||||||
| 1173 | - | |||||||||||||||||||
| 1174 | \sa lastQuery() | - | ||||||||||||||||||
| 1175 | */ | - | ||||||||||||||||||
| 1176 | QString QSqlQuery::executedQuery() const | - | ||||||||||||||||||
| 1177 | { | - | ||||||||||||||||||
| 1178 | return d->sqlResult->executedQuery(); executed 1 time by 1 test: return d->sqlResult->executedQuery();Executed by:
| 1 | ||||||||||||||||||
| 1179 | } | - | ||||||||||||||||||
| 1180 | - | |||||||||||||||||||
| 1181 | /*! | - | ||||||||||||||||||
| 1182 | Returns the object ID of the most recent inserted row if the | - | ||||||||||||||||||
| 1183 | database supports it. An invalid QVariant will be returned if the | - | ||||||||||||||||||
| 1184 | query did not insert any value or if the database does not report | - | ||||||||||||||||||
| 1185 | the id back. If more than one row was touched by the insert, the | - | ||||||||||||||||||
| 1186 | behavior is undefined. | - | ||||||||||||||||||
| 1187 | - | |||||||||||||||||||
| 1188 | For MySQL databases the row's auto-increment field will be returned. | - | ||||||||||||||||||
| 1189 | - | |||||||||||||||||||
| 1190 | \note For this function to work in PSQL, the table table must | - | ||||||||||||||||||
| 1191 | contain OIDs, which may not have been created by default. Check the | - | ||||||||||||||||||
| 1192 | \c default_with_oids configuration variable to be sure. | - | ||||||||||||||||||
| 1193 | - | |||||||||||||||||||
| 1194 | \sa QSqlDriver::hasFeature() | - | ||||||||||||||||||
| 1195 | */ | - | ||||||||||||||||||
| 1196 | QVariant QSqlQuery::lastInsertId() const | - | ||||||||||||||||||
| 1197 | { | - | ||||||||||||||||||
| 1198 | return d->sqlResult->lastInsertId(); executed 7 times by 2 tests: return d->sqlResult->lastInsertId();Executed by:
| 7 | ||||||||||||||||||
| 1199 | } | - | ||||||||||||||||||
| 1200 | - | |||||||||||||||||||
| 1201 | /*! | - | ||||||||||||||||||
| 1202 | - | |||||||||||||||||||
| 1203 | Instruct the database driver to return numerical values with a | - | ||||||||||||||||||
| 1204 | precision specified by \a precisionPolicy. | - | ||||||||||||||||||
| 1205 | - | |||||||||||||||||||
| 1206 | The Oracle driver, for example, can retrieve numerical values as | - | ||||||||||||||||||
| 1207 | strings to prevent the loss of precision. If high precision doesn't | - | ||||||||||||||||||
| 1208 | matter, use this method to increase execution speed by bypassing | - | ||||||||||||||||||
| 1209 | string conversions. | - | ||||||||||||||||||
| 1210 | - | |||||||||||||||||||
| 1211 | Note: Drivers that don't support fetching numerical values with low | - | ||||||||||||||||||
| 1212 | precision will ignore the precision policy. You can use | - | ||||||||||||||||||
| 1213 | QSqlDriver::hasFeature() to find out whether a driver supports this | - | ||||||||||||||||||
| 1214 | feature. | - | ||||||||||||||||||
| 1215 | - | |||||||||||||||||||
| 1216 | Note: Setting the precision policy doesn't affect the currently | - | ||||||||||||||||||
| 1217 | active query. Call \l{exec()}{exec(QString)} or prepare() in order | - | ||||||||||||||||||
| 1218 | to activate the policy. | - | ||||||||||||||||||
| 1219 | - | |||||||||||||||||||
| 1220 | \sa QSql::NumericalPrecisionPolicy, numericalPrecisionPolicy() | - | ||||||||||||||||||
| 1221 | */ | - | ||||||||||||||||||
| 1222 | void QSqlQuery::setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy) | - | ||||||||||||||||||
| 1223 | { | - | ||||||||||||||||||
| 1224 | d->sqlResult->setNumericalPrecisionPolicy(precisionPolicy); | - | ||||||||||||||||||
| 1225 | } executed 6 times by 1 test: end of blockExecuted by:
| 6 | ||||||||||||||||||
| 1226 | - | |||||||||||||||||||
| 1227 | /*! | - | ||||||||||||||||||
| 1228 | Returns the current precision policy. | - | ||||||||||||||||||
| 1229 | - | |||||||||||||||||||
| 1230 | \sa QSql::NumericalPrecisionPolicy, setNumericalPrecisionPolicy() | - | ||||||||||||||||||
| 1231 | */ | - | ||||||||||||||||||
| 1232 | QSql::NumericalPrecisionPolicy QSqlQuery::numericalPrecisionPolicy() const | - | ||||||||||||||||||
| 1233 | { | - | ||||||||||||||||||
| 1234 | return d->sqlResult->numericalPrecisionPolicy(); never executed: return d->sqlResult->numericalPrecisionPolicy(); | 0 | ||||||||||||||||||
| 1235 | } | - | ||||||||||||||||||
| 1236 | - | |||||||||||||||||||
| 1237 | /*! | - | ||||||||||||||||||
| 1238 | \since 4.3.2 | - | ||||||||||||||||||
| 1239 | - | |||||||||||||||||||
| 1240 | Instruct the database driver that no more data will be fetched from | - | ||||||||||||||||||
| 1241 | this query until it is re-executed. There is normally no need to | - | ||||||||||||||||||
| 1242 | call this function, but it may be helpful in order to free resources | - | ||||||||||||||||||
| 1243 | such as locks or cursors if you intend to re-use the query at a | - | ||||||||||||||||||
| 1244 | later time. | - | ||||||||||||||||||
| 1245 | - | |||||||||||||||||||
| 1246 | Sets the query to inactive. Bound values retain their values. | - | ||||||||||||||||||
| 1247 | - | |||||||||||||||||||
| 1248 | \sa prepare(), exec(), isActive() | - | ||||||||||||||||||
| 1249 | */ | - | ||||||||||||||||||
| 1250 | void QSqlQuery::finish() | - | ||||||||||||||||||
| 1251 | { | - | ||||||||||||||||||
| 1252 | if (isActive()) {
| 0-3 | ||||||||||||||||||
| 1253 | d->sqlResult->setLastError(QSqlError()); | - | ||||||||||||||||||
| 1254 | d->sqlResult->setAt(QSql::BeforeFirstRow); | - | ||||||||||||||||||
| 1255 | d->sqlResult->detachFromResultSet(); | - | ||||||||||||||||||
| 1256 | d->sqlResult->setActive(false); | - | ||||||||||||||||||
| 1257 | } executed 3 times by 1 test: end of blockExecuted by:
| 3 | ||||||||||||||||||
| 1258 | } executed 3 times by 1 test: end of blockExecuted by:
| 3 | ||||||||||||||||||
| 1259 | - | |||||||||||||||||||
| 1260 | /*! | - | ||||||||||||||||||
| 1261 | \since 4.4 | - | ||||||||||||||||||
| 1262 | - | |||||||||||||||||||
| 1263 | Discards the current result set and navigates to the next if available. | - | ||||||||||||||||||
| 1264 | - | |||||||||||||||||||
| 1265 | Some databases are capable of returning multiple result sets for | - | ||||||||||||||||||
| 1266 | stored procedures or SQL batches (a query strings that contains | - | ||||||||||||||||||
| 1267 | multiple statements). If multiple result sets are available after | - | ||||||||||||||||||
| 1268 | executing a query this function can be used to navigate to the next | - | ||||||||||||||||||
| 1269 | result set(s). | - | ||||||||||||||||||
| 1270 | - | |||||||||||||||||||
| 1271 | If a new result set is available this function will return true. | - | ||||||||||||||||||
| 1272 | The query will be repositioned on an \e invalid record in the new | - | ||||||||||||||||||
| 1273 | result set and must be navigated to a valid record before data | - | ||||||||||||||||||
| 1274 | values can be retrieved. If a new result set isn't available the | - | ||||||||||||||||||
| 1275 | function returns \c false and the query is set to inactive. In any | - | ||||||||||||||||||
| 1276 | case the old result set will be discarded. | - | ||||||||||||||||||
| 1277 | - | |||||||||||||||||||
| 1278 | When one of the statements is a non-select statement a count of | - | ||||||||||||||||||
| 1279 | affected rows may be available instead of a result set. | - | ||||||||||||||||||
| 1280 | - | |||||||||||||||||||
| 1281 | Note that some databases, i.e. Microsoft SQL Server, requires | - | ||||||||||||||||||
| 1282 | non-scrollable cursors when working with multiple result sets. Some | - | ||||||||||||||||||
| 1283 | databases may execute all statements at once while others may delay | - | ||||||||||||||||||
| 1284 | the execution until the result set is actually accessed, and some | - | ||||||||||||||||||
| 1285 | databases may have restrictions on which statements are allowed to | - | ||||||||||||||||||
| 1286 | be used in a SQL batch. | - | ||||||||||||||||||
| 1287 | - | |||||||||||||||||||
| 1288 | \sa QSqlDriver::hasFeature(), setForwardOnly(), next(), isSelect(), | - | ||||||||||||||||||
| 1289 | numRowsAffected(), isActive(), lastError() | - | ||||||||||||||||||
| 1290 | */ | - | ||||||||||||||||||
| 1291 | bool QSqlQuery::nextResult() | - | ||||||||||||||||||
| 1292 | { | - | ||||||||||||||||||
| 1293 | if (isActive())
| 0 | ||||||||||||||||||
| 1294 | return d->sqlResult->nextResult(); never executed: return d->sqlResult->nextResult(); | 0 | ||||||||||||||||||
| 1295 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 1296 | } | - | ||||||||||||||||||
| 1297 | - | |||||||||||||||||||
| 1298 | QT_END_NAMESPACE | - | ||||||||||||||||||
| Source code | Switch to Preprocessed file |