Line | Source Code | Coverage |
---|
1 | /**************************************************************************** | - |
2 | ** | - |
3 | ** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). | - |
4 | ** Contact: http://www.qt-project.org/legal | - |
5 | ** | - |
6 | ** This file is part of the QtSql module of the Qt Toolkit. | - |
7 | ** | - |
8 | ** $QT_BEGIN_LICENSE:LGPL$ | - |
9 | ** Commercial License Usage | - |
10 | ** Licensees holding valid commercial Qt licenses may use this file in | - |
11 | ** accordance with the commercial license agreement provided with the | - |
12 | ** Software or, alternatively, in accordance with the terms contained in | - |
13 | ** a written agreement between you and Digia. For licensing terms and | - |
14 | ** conditions see http://qt.digia.com/licensing. For further information | - |
15 | ** use the contact form at http://qt.digia.com/contact-us. | - |
16 | ** | - |
17 | ** GNU Lesser General Public License Usage | - |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - |
19 | ** General Public License version 2.1 as published by the Free Software | - |
20 | ** Foundation and appearing in the file LICENSE.LGPL included in the | - |
21 | ** packaging of this file. Please review the following information to | - |
22 | ** ensure the GNU Lesser General Public License version 2.1 requirements | - |
23 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | - |
24 | ** | - |
25 | ** In addition, as a special exception, Digia gives you certain additional | - |
26 | ** rights. These rights are described in the Digia Qt LGPL Exception | - |
27 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | - |
28 | ** | - |
29 | ** GNU General Public License Usage | - |
30 | ** Alternatively, this file may be used under the terms of the GNU | - |
31 | ** General Public License version 3.0 as published by the Free Software | - |
32 | ** Foundation and appearing in the file LICENSE.GPL included in the | - |
33 | ** packaging of this file. Please review the following information to | - |
34 | ** ensure the GNU General Public License version 3.0 requirements will be | - |
35 | ** met: http://www.gnu.org/copyleft/gpl.html. | - |
36 | ** | - |
37 | ** | - |
38 | ** $QT_END_LICENSE$ | - |
39 | ** | - |
40 | ****************************************************************************/ | - |
41 | | - |
42 | #include "qsqlquery.h" | - |
43 | | - |
44 | //#define QT_DEBUG_SQL | - |
45 | | - |
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)) never executed: delete x; executed: return thisGlobalStatic.pointer.load(); Execution Count:1435 partially evaluated: !thisGlobalStatic.pointer.testAndSetOrdered(0, x) no Evaluation Count:0 | yes Evaluation Count:8 |
evaluated: !thisGlobalStatic.pointer.load() yes Evaluation Count:8 | yes Evaluation Count:1427 |
partially evaluated: !thisGlobalStatic.destroyed yes Evaluation Count:8 | no Evaluation Count:0 |
| 0-1435 |
69 | Q_GLOBAL_STATIC(QSqlNullDriver, nullDriver) never executed: delete x; executed: return thisGlobalStatic.pointer.load(); Execution Count:9 partially evaluated: !thisGlobalStatic.pointer.testAndSetOrdered(0, x) no Evaluation Count:0 | yes Evaluation Count:9 |
partially evaluated: !thisGlobalStatic.pointer.load() yes Evaluation Count:9 | no Evaluation Count:0 |
partially evaluated: !thisGlobalStatic.destroyed yes Evaluation Count:9 | no Evaluation Count:0 |
| 0-9 |
70 | Q_GLOBAL_STATIC_WITH_ARGS(QSqlNullResult, nullResult, (nullDriver())) never executed: delete x; executed: return thisGlobalStatic.pointer.load(); Execution Count:2191 partially evaluated: !thisGlobalStatic.pointer.testAndSetOrdered(0, x) no Evaluation Count:0 | yes Evaluation Count:9 |
evaluated: !thisGlobalStatic.pointer.load() yes Evaluation Count:9 | yes Evaluation Count:2182 |
partially evaluated: !thisGlobalStatic.destroyed yes Evaluation Count:9 | no Evaluation Count:0 |
| 0-2191 |
71 | | - |
72 | QSqlQueryPrivate* QSqlQueryPrivate::shared_null() | - |
73 | { | - |
74 | QSqlQueryPrivate *null = nullQueryPrivate(); executed (the execution status of this line is deduced): QSqlQueryPrivate *null = nullQueryPrivate(); | - |
75 | null->ref.ref(); executed (the execution status of this line is deduced): null->ref.ref(); | - |
76 | return null; executed: return null; Execution Count:1435 | 1435 |
77 | } | - |
78 | | - |
79 | /*! | - |
80 | \internal | - |
81 | */ | - |
82 | QSqlQueryPrivate::QSqlQueryPrivate(QSqlResult* result) | - |
83 | : ref(1), sqlResult(result) | - |
84 | { | - |
85 | if (!sqlResult) evaluated: !sqlResult yes Evaluation Count:8 | yes Evaluation Count:2175 |
| 8-2175 |
86 | sqlResult = nullResult(); executed: sqlResult = nullResult(); Execution Count:8 | 8 |
87 | } executed: } Execution Count:2183 | 2183 |
88 | | - |
89 | QSqlQueryPrivate::~QSqlQueryPrivate() | - |
90 | { | - |
91 | QSqlResult *nr = nullResult(); executed (the execution status of this line is deduced): QSqlResult *nr = nullResult(); | - |
92 | if (!nr || sqlResult == nr) partially evaluated: !nr no Evaluation Count:0 | yes Evaluation Count:2183 |
evaluated: sqlResult == nr yes Evaluation Count:8 | yes Evaluation Count:2175 |
| 0-2183 |
93 | return; executed: return; Execution Count:8 | 8 |
94 | delete sqlResult; executed (the execution status of this line is deduced): delete sqlResult; | - |
95 | } executed: } Execution Count:2175 | 2175 |
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 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 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); executed (the execution status of this line is deduced): d = new QSqlQueryPrivate(result); | - |
236 | } executed: } Execution Count:2175 | 2175 |
237 | | - |
238 | /*! | - |
239 | Destroys the object and frees any allocated resources. | - |
240 | */ | - |
241 | | - |
242 | QSqlQuery::~QSqlQuery() | - |
243 | { | - |
244 | if (!d->ref.deref()) evaluated: !d->ref.deref() yes Evaluation Count:1597 | yes Evaluation Count:2014 |
| 1597-2014 |
245 | delete d; executed: delete d; Execution Count:1597 | 1597 |
246 | } executed: } Execution Count:3611 | 3611 |
247 | | - |
248 | /*! | - |
249 | Constructs a copy of \a other. | - |
250 | */ | - |
251 | | - |
252 | QSqlQuery::QSqlQuery(const QSqlQuery& other) | - |
253 | { | - |
254 | d = other.d; executed (the execution status of this line is deduced): d = other.d; | - |
255 | d->ref.ref(); executed (the execution status of this line is deduced): d->ref.ref(); | - |
256 | } executed: } Execution Count:1 | 1 |
257 | | - |
258 | /*! | - |
259 | \internal | - |
260 | */ | - |
261 | static void qInit(QSqlQuery *q, const QString& query, QSqlDatabase db) | - |
262 | { | - |
263 | QSqlDatabase database = db; executed (the execution status of this line is deduced): QSqlDatabase database = db; | - |
264 | if (!database.isValid()) evaluated: !database.isValid() yes Evaluation Count:773 | yes Evaluation Count:662 |
| 662-773 |
265 | database = QSqlDatabase::database(QLatin1String(QSqlDatabase::defaultConnection), false); executed: database = QSqlDatabase::database(QLatin1String(QSqlDatabase::defaultConnection), false); Execution Count:773 | 773 |
266 | if (database.isValid()) { evaluated: database.isValid() yes Evaluation Count:1175 | yes Evaluation Count:260 |
| 260-1175 |
267 | *q = QSqlQuery(database.driver()->createResult()); executed (the execution status of this line is deduced): *q = QSqlQuery(database.driver()->createResult()); | - |
268 | } executed: } Execution Count:1175 | 1175 |
269 | if (!query.isEmpty()) evaluated: !query.isEmpty() yes Evaluation Count:540 | yes Evaluation Count:895 |
| 540-895 |
270 | q->exec(query); executed: q->exec(query); Execution Count:540 | 540 |
271 | } executed: } Execution Count:1435 | 1435 |
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(); executed (the execution status of this line is deduced): d = QSqlQueryPrivate::shared_null(); | - |
284 | qInit(this, query, db); executed (the execution status of this line is deduced): qInit(this, query, db); | - |
285 | } executed: } Execution Count:1109 | 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(); executed (the execution status of this line is deduced): d = QSqlQueryPrivate::shared_null(); | - |
297 | qInit(this, QString(), db); executed (the execution status of this line is deduced): qInit(this, QString(), db); | - |
298 | } executed: } Execution Count:326 | 326 |
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); executed (the execution status of this line is deduced): qAtomicAssign(d, other.d); | - |
308 | return *this; executed: return *this; Execution Count:2016 | 2016 |
309 | } | - |
310 | | - |
311 | /*! | - |
312 | Returns true if the query is \l{isActive()}{active} and positioned | - |
313 | on a valid record and the \a field is NULL; otherwise returns | - |
314 | false. Note that for some drivers, isNull() will not return accurate | - |
315 | information until after an attempt is made to retrieve data. | - |
316 | | - |
317 | \sa isActive(), isValid(), value() | - |
318 | */ | - |
319 | | - |
320 | bool QSqlQuery::isNull(int field) const | - |
321 | { | - |
322 | if (d->sqlResult->isActive() && d->sqlResult->isValid()) partially evaluated: d->sqlResult->isActive() yes Evaluation Count:5 | no Evaluation Count:0 |
partially evaluated: d->sqlResult->isValid() yes Evaluation Count:5 | no Evaluation Count:0 |
| 0-5 |
323 | return d->sqlResult->isNull(field); executed: return d->sqlResult->isNull(field); Execution Count:5 | 5 |
324 | return true; never executed: return true; | 0 |
325 | } | - |
326 | | - |
327 | /*! | - |
328 | | - |
329 | Executes the SQL in \a query. Returns true and sets the query state | - |
330 | to \l{isActive()}{active} if the query was successful; otherwise | - |
331 | returns false. The \a query string must use syntax appropriate for | - |
332 | the SQL database being queried (for example, standard SQL). | - |
333 | | - |
334 | After the query is executed, the query is positioned on an \e | - |
335 | invalid record and must be navigated to a valid record before data | - |
336 | values can be retrieved (for example, using next()). | - |
337 | | - |
338 | Note that the last error for this query is reset when exec() is | - |
339 | called. | - |
340 | | - |
341 | For SQLite, the query string can contain only one statement at a time. | - |
342 | If more than one statement is given, the function returns false. | - |
343 | | - |
344 | Example: | - |
345 | | - |
346 | \snippet sqldatabase/sqldatabase.cpp 34 | - |
347 | | - |
348 | \sa isActive(), isValid(), next(), previous(), first(), last(), | - |
349 | seek() | - |
350 | */ | - |
351 | | - |
352 | bool QSqlQuery::exec(const QString& query) | - |
353 | { | - |
354 | if (d->ref.load() != 1) { evaluated: d->ref.load() != 1 yes Evaluation Count:5 | yes Evaluation Count:2552 |
| 5-2552 |
355 | bool fo = isForwardOnly(); executed (the execution status of this line is deduced): bool fo = isForwardOnly(); | - |
356 | *this = QSqlQuery(driver()->createResult()); executed (the execution status of this line is deduced): *this = QSqlQuery(driver()->createResult()); | - |
357 | d->sqlResult->setNumericalPrecisionPolicy(d->sqlResult->numericalPrecisionPolicy()); executed (the execution status of this line is deduced): d->sqlResult->setNumericalPrecisionPolicy(d->sqlResult->numericalPrecisionPolicy()); | - |
358 | setForwardOnly(fo); executed (the execution status of this line is deduced): setForwardOnly(fo); | - |
359 | } else { executed: } Execution Count:5 | 5 |
360 | d->sqlResult->clear(); executed (the execution status of this line is deduced): d->sqlResult->clear(); | - |
361 | d->sqlResult->setActive(false); executed (the execution status of this line is deduced): d->sqlResult->setActive(false); | - |
362 | d->sqlResult->setLastError(QSqlError()); executed (the execution status of this line is deduced): d->sqlResult->setLastError(QSqlError()); | - |
363 | d->sqlResult->setAt(QSql::BeforeFirstRow); executed (the execution status of this line is deduced): d->sqlResult->setAt(QSql::BeforeFirstRow); | - |
364 | d->sqlResult->setNumericalPrecisionPolicy(d->sqlResult->numericalPrecisionPolicy()); executed (the execution status of this line is deduced): d->sqlResult->setNumericalPrecisionPolicy(d->sqlResult->numericalPrecisionPolicy()); | - |
365 | } executed: } Execution Count:2552 | 2552 |
366 | d->sqlResult->setQuery(query.trimmed()); executed (the execution status of this line is deduced): d->sqlResult->setQuery(query.trimmed()); | - |
367 | if (!driver()->isOpen() || driver()->isOpenError()) { evaluated: !driver()->isOpen() yes Evaluation Count:2 | yes Evaluation Count:2555 |
partially evaluated: driver()->isOpenError() no Evaluation Count:0 | yes Evaluation Count:2555 |
| 0-2555 |
368 | qWarning("QSqlQuery::exec: database not open"); executed (the execution status of this line is deduced): QMessageLogger("kernel/qsqlquery.cpp", 368, __PRETTY_FUNCTION__).warning("QSqlQuery::exec: database not open"); | - |
369 | return false; executed: return false; Execution Count:2 | 2 |
370 | } | - |
371 | if (query.isEmpty()) { partially evaluated: query.isEmpty() no Evaluation Count:0 | yes Evaluation Count:2555 |
| 0-2555 |
372 | qWarning("QSqlQuery::exec: empty query"); never executed (the execution status of this line is deduced): QMessageLogger("kernel/qsqlquery.cpp", 372, __PRETTY_FUNCTION__).warning("QSqlQuery::exec: empty query"); | - |
373 | return false; never executed: return false; | 0 |
374 | } | - |
375 | #ifdef QT_DEBUG_SQL | - |
376 | qDebug("\n QSqlQuery: %s", query.toLocal8Bit().constData()); | - |
377 | #endif | - |
378 | return d->sqlResult->reset(query); executed: return d->sqlResult->reset(query); Execution Count:2555 | 2555 |
379 | } | - |
380 | | - |
381 | /*! | - |
382 | Returns the value of field \a index in the current record. | - |
383 | | - |
384 | The fields are numbered from left to right using the text of the | - |
385 | \c SELECT statement, e.g. in | - |
386 | | - |
387 | \snippet code/src_sql_kernel_qsqlquery.cpp 0 | - |
388 | | - |
389 | field 0 is \c forename and field 1 is \c | - |
390 | surname. Using \c{SELECT *} is not recommended because the order | - |
391 | of the fields in the query is undefined. | - |
392 | | - |
393 | An invalid QVariant is returned if field \a index does not | - |
394 | exist, if the query is inactive, or if the query is positioned on | - |
395 | an invalid record. | - |
396 | | - |
397 | \sa previous(), next(), first(), last(), seek(), isActive(), isValid() | - |
398 | */ | - |
399 | | - |
400 | QVariant QSqlQuery::value(int index) const | - |
401 | { | - |
402 | if (isActive() && isValid() && (index > -1)) partially evaluated: isActive() yes Evaluation Count:9361 | no Evaluation Count:0 |
partially evaluated: isValid() yes Evaluation Count:9361 | no Evaluation Count:0 |
partially evaluated: (index > -1) yes Evaluation Count:9361 | no Evaluation Count:0 |
| 0-9361 |
403 | return d->sqlResult->data(index); executed: return d->sqlResult->data(index); Execution Count:9361 | 9361 |
404 | qWarning("QSqlQuery::value: not positioned on a valid record"); never executed (the execution status of this line is deduced): QMessageLogger("kernel/qsqlquery.cpp", 404, __PRETTY_FUNCTION__).warning("QSqlQuery::value: not positioned on a valid record"); | - |
405 | return QVariant(); never executed: return QVariant(); | 0 |
406 | } | - |
407 | | - |
408 | /*! | - |
409 | \overload | - |
410 | | - |
411 | Returns the value of the field called \a name in the current record. | - |
412 | If field \a name does not exist an invalid variant is returned. | - |
413 | | - |
414 | This overload is less efficient than \l{QSqlQuery::}{value()} | - |
415 | */ | - |
416 | | - |
417 | QVariant QSqlQuery::value(const QString& name) const | - |
418 | { | - |
419 | int index = d->sqlResult->record().indexOf(name); executed (the execution status of this line is deduced): int index = d->sqlResult->record().indexOf(name); | - |
420 | if (index > -1) partially evaluated: index > -1 yes Evaluation Count:5 | no Evaluation Count:0 |
| 0-5 |
421 | return value(index); executed: return value(index); Execution Count:5 | 5 |
422 | qWarning("QSqlQuery::value: unknown field name '%s'", qPrintable(name)); never executed (the execution status of this line is deduced): QMessageLogger("kernel/qsqlquery.cpp", 422, __PRETTY_FUNCTION__).warning("QSqlQuery::value: unknown field name '%s'", QString(name).toLocal8Bit().constData()); | - |
423 | return QVariant(); never executed: return QVariant(); | 0 |
424 | } | - |
425 | | - |
426 | /*! | - |
427 | Returns the current internal position of the query. The first | - |
428 | record is at position zero. If the position is invalid, the | - |
429 | function returns QSql::BeforeFirstRow or | - |
430 | QSql::AfterLastRow, which are special negative values. | - |
431 | | - |
432 | \sa previous(), next(), first(), last(), seek(), isActive(), isValid() | - |
433 | */ | - |
434 | | - |
435 | int QSqlQuery::at() const | - |
436 | { | - |
437 | return d->sqlResult->at(); executed: return d->sqlResult->at(); Execution Count:11209 | 11209 |
438 | } | - |
439 | | - |
440 | /*! | - |
441 | Returns the text of the current query being used, or an empty | - |
442 | string if there is no current query text. | - |
443 | | - |
444 | \sa executedQuery() | - |
445 | */ | - |
446 | | - |
447 | QString QSqlQuery::lastQuery() const | - |
448 | { | - |
449 | return d->sqlResult->lastQuery(); executed: return d->sqlResult->lastQuery(); Execution Count:124 | 124 |
450 | } | - |
451 | | - |
452 | /*! | - |
453 | Returns the database driver associated with the query. | - |
454 | */ | - |
455 | | - |
456 | const QSqlDriver *QSqlQuery::driver() const | - |
457 | { | - |
458 | return d->sqlResult->driver(); executed: return d->sqlResult->driver(); Execution Count:6693 | 6693 |
459 | } | - |
460 | | - |
461 | /*! | - |
462 | Returns the result associated with the query. | - |
463 | */ | - |
464 | | - |
465 | const QSqlResult* QSqlQuery::result() const | - |
466 | { | - |
467 | return d->sqlResult; executed: return d->sqlResult; Execution Count:155 | 155 |
468 | } | - |
469 | | - |
470 | /*! | - |
471 | Retrieves the record at position \a index, if available, and | - |
472 | positions the query on the retrieved record. The first record is at | - |
473 | position 0. Note that the query must be in an \l{isActive()} | - |
474 | {active} state and isSelect() must return true before calling this | - |
475 | function. | - |
476 | | - |
477 | If \a relative is false (the default), the following rules apply: | - |
478 | | - |
479 | \list | - |
480 | | - |
481 | \li If \a index is negative, the result is positioned before the | - |
482 | first record and false is returned. | - |
483 | | - |
484 | \li Otherwise, an attempt is made to move to the record at position | - |
485 | \a index. If the record at position \a index could not be retrieved, | - |
486 | the result is positioned after the last record and false is | - |
487 | returned. If the record is successfully retrieved, true is returned. | - |
488 | | - |
489 | \endlist | - |
490 | | - |
491 | If \a relative is true, the following rules apply: | - |
492 | | - |
493 | \list | - |
494 | | - |
495 | \li If the result is currently positioned before the first record or | - |
496 | on the first record, and \a index is negative, there is no change, | - |
497 | and false is returned. | - |
498 | | - |
499 | \li If the result is currently located after the last record, and \a | - |
500 | index is positive, there is no change, and false is returned. | - |
501 | | - |
502 | \li If the result is currently located somewhere in the middle, and | - |
503 | the relative offset \a index moves the result below zero, the result | - |
504 | is positioned before the first record and false is returned. | - |
505 | | - |
506 | \li Otherwise, an attempt is made to move to the record \a index | - |
507 | records ahead of the current record (or \a index records behind the | - |
508 | current record if \a index is negative). If the record at offset \a | - |
509 | index could not be retrieved, the result is positioned after the | - |
510 | last record if \a index >= 0, (or before the first record if \a | - |
511 | index is negative), and false is returned. If the record is | - |
512 | successfully retrieved, true is returned. | - |
513 | | - |
514 | \endlist | - |
515 | | - |
516 | \sa next(), previous(), first(), last(), at(), isActive(), isValid() | - |
517 | */ | - |
518 | bool QSqlQuery::seek(int index, bool relative) | - |
519 | { | - |
520 | if (!isSelect() || !isActive()) partially evaluated: !isSelect() no Evaluation Count:0 | yes Evaluation Count:3874 |
partially evaluated: !isActive() no Evaluation Count:0 | yes Evaluation Count:3874 |
| 0-3874 |
521 | return false; never executed: return false; | 0 |
522 | int actualIdx; executed (the execution status of this line is deduced): int actualIdx; | - |
523 | if (!relative) { // arbitrary seek evaluated: !relative yes Evaluation Count:3873 | yes Evaluation Count:1 |
| 1-3873 |
524 | if (index < 0) { evaluated: index < 0 yes Evaluation Count:1 | yes Evaluation Count:3872 |
| 1-3872 |
525 | d->sqlResult->setAt(QSql::BeforeFirstRow); executed (the execution status of this line is deduced): d->sqlResult->setAt(QSql::BeforeFirstRow); | - |
526 | return false; executed: return false; Execution Count:1 | 1 |
527 | } | - |
528 | actualIdx = index; executed (the execution status of this line is deduced): actualIdx = index; | - |
529 | } else { executed: } Execution Count:3872 | 3872 |
530 | switch (at()) { // relative seek | - |
531 | case QSql::BeforeFirstRow: | - |
532 | if (index > 0) never evaluated: index > 0 | 0 |
533 | actualIdx = index; never executed: actualIdx = index; | 0 |
534 | else { | - |
535 | return false; never executed: return false; | 0 |
536 | } | - |
537 | break; | 0 |
538 | case QSql::AfterLastRow: | - |
539 | if (index < 0) { never evaluated: index < 0 | 0 |
540 | d->sqlResult->fetchLast(); never executed (the execution status of this line is deduced): d->sqlResult->fetchLast(); | - |
541 | actualIdx = at() + index; never executed (the execution status of this line is deduced): actualIdx = at() + index; | - |
542 | } else { | 0 |
543 | return false; never executed: return false; | 0 |
544 | } | - |
545 | break; | 0 |
546 | default: | - |
547 | if ((at() + index) < 0) { partially evaluated: (at() + index) < 0 no Evaluation Count:0 | yes Evaluation Count:1 |
| 0-1 |
548 | d->sqlResult->setAt(QSql::BeforeFirstRow); never executed (the execution status of this line is deduced): d->sqlResult->setAt(QSql::BeforeFirstRow); | - |
549 | return false; never executed: return false; | 0 |
550 | } | - |
551 | actualIdx = at() + index; executed (the execution status of this line is deduced): actualIdx = at() + index; | - |
552 | break; executed: break; Execution Count:1 | 1 |
553 | } | - |
554 | } executed: } Execution Count:1 | 1 |
555 | // let drivers optimize | - |
556 | if (isForwardOnly() && actualIdx < at()) { evaluated: isForwardOnly() yes Evaluation Count:2 | yes Evaluation Count:3871 |
evaluated: actualIdx < at() yes Evaluation Count:1 | yes Evaluation Count:1 |
| 1-3871 |
557 | qWarning("QSqlQuery::seek: cannot seek backwards in a forward only query"); executed (the execution status of this line is deduced): QMessageLogger("kernel/qsqlquery.cpp", 557, __PRETTY_FUNCTION__).warning("QSqlQuery::seek: cannot seek backwards in a forward only query"); | - |
558 | return false; executed: return false; Execution Count:1 | 1 |
559 | } | - |
560 | if (actualIdx == (at() + 1) && at() != QSql::BeforeFirstRow) { evaluated: actualIdx == (at() + 1) yes Evaluation Count:1223 | yes Evaluation Count:2649 |
evaluated: at() != QSql::BeforeFirstRow yes Evaluation Count:1221 | yes Evaluation Count:2 |
| 2-2649 |
561 | if (!d->sqlResult->fetchNext()) { partially evaluated: !d->sqlResult->fetchNext() no Evaluation Count:0 | yes Evaluation Count:1221 |
| 0-1221 |
562 | d->sqlResult->setAt(QSql::AfterLastRow); never executed (the execution status of this line is deduced): d->sqlResult->setAt(QSql::AfterLastRow); | - |
563 | return false; never executed: return false; | 0 |
564 | } | - |
565 | return true; executed: return true; Execution Count:1221 | 1221 |
566 | } | - |
567 | if (actualIdx == (at() - 1)) { evaluated: actualIdx == (at() - 1) yes Evaluation Count:39 | yes Evaluation Count:2612 |
| 39-2612 |
568 | if (!d->sqlResult->fetchPrevious()) { partially evaluated: !d->sqlResult->fetchPrevious() no Evaluation Count:0 | yes Evaluation Count:39 |
| 0-39 |
569 | d->sqlResult->setAt(QSql::BeforeFirstRow); never executed (the execution status of this line is deduced): d->sqlResult->setAt(QSql::BeforeFirstRow); | - |
570 | return false; never executed: return false; | 0 |
571 | } | - |
572 | return true; executed: return true; Execution Count:39 | 39 |
573 | } | - |
574 | if (!d->sqlResult->fetch(actualIdx)) { evaluated: !d->sqlResult->fetch(actualIdx) yes Evaluation Count:270 | yes Evaluation Count:2342 |
| 270-2342 |
575 | d->sqlResult->setAt(QSql::AfterLastRow); executed (the execution status of this line is deduced): d->sqlResult->setAt(QSql::AfterLastRow); | - |
576 | return false; executed: return false; Execution Count:270 | 270 |
577 | } | - |
578 | return true; executed: return true; Execution Count:2342 | 2342 |
579 | } | - |
580 | | - |
581 | /*! | - |
582 | | - |
583 | Retrieves the next record in the result, if available, and positions | - |
584 | the query on the retrieved record. Note that the result must be in | - |
585 | the \l{isActive()}{active} state and isSelect() must return true | - |
586 | before calling this function or it will do nothing and return false. | - |
587 | | - |
588 | The following rules apply: | - |
589 | | - |
590 | \list | - |
591 | | - |
592 | \li If the result is currently located before the first record, | - |
593 | e.g. immediately after a query is executed, an attempt is made to | - |
594 | retrieve the first record. | - |
595 | | - |
596 | \li If the result is currently located after the last record, there | - |
597 | is no change and false is returned. | - |
598 | | - |
599 | \li If the result is located somewhere in the middle, an attempt is | - |
600 | made to retrieve the next record. | - |
601 | | - |
602 | \endlist | - |
603 | | - |
604 | If the record could not be retrieved, the result is positioned after | - |
605 | the last record and false is returned. If the record is successfully | - |
606 | retrieved, true is returned. | - |
607 | | - |
608 | \sa previous(), first(), last(), seek(), at(), isActive(), isValid() | - |
609 | */ | - |
610 | bool QSqlQuery::next() | - |
611 | { | - |
612 | if (!isSelect() || !isActive()) evaluated: !isSelect() yes Evaluation Count:4 | yes Evaluation Count:3412 |
partially evaluated: !isActive() no Evaluation Count:0 | yes Evaluation Count:3412 |
| 0-3412 |
613 | return false; executed: return false; Execution Count:4 | 4 |
614 | bool b = false; executed (the execution status of this line is deduced): bool b = false; | - |
615 | switch (at()) { | - |
616 | case QSql::BeforeFirstRow: | - |
617 | b = d->sqlResult->fetchFirst(); executed (the execution status of this line is deduced): b = d->sqlResult->fetchFirst(); | - |
618 | return b; executed: return b; Execution Count:694 | 694 |
619 | case QSql::AfterLastRow: | - |
620 | return false; executed: return false; Execution Count:13 | 13 |
621 | default: | - |
622 | if (!d->sqlResult->fetchNext()) { evaluated: !d->sqlResult->fetchNext() yes Evaluation Count:831 | yes Evaluation Count:1874 |
| 831-1874 |
623 | d->sqlResult->setAt(QSql::AfterLastRow); executed (the execution status of this line is deduced): d->sqlResult->setAt(QSql::AfterLastRow); | - |
624 | return false; executed: return false; Execution Count:831 | 831 |
625 | } | - |
626 | return true; executed: return true; Execution Count:1874 | 1874 |
627 | } | - |
628 | } | 0 |
629 | | - |
630 | /*! | - |
631 | | - |
632 | Retrieves the previous record in the result, if available, and | - |
633 | positions the query on the retrieved record. Note that the result | - |
634 | must be in the \l{isActive()}{active} state and isSelect() must | - |
635 | return true before calling this function or it will do nothing and | - |
636 | return false. | - |
637 | | - |
638 | The following rules apply: | - |
639 | | - |
640 | \list | - |
641 | | - |
642 | \li If the result is currently located before the first record, there | - |
643 | is no change and false is returned. | - |
644 | | - |
645 | \li If the result is currently located after the last record, an | - |
646 | attempt is made to retrieve the last record. | - |
647 | | - |
648 | \li If the result is somewhere in the middle, an attempt is made to | - |
649 | retrieve the previous record. | - |
650 | | - |
651 | \endlist | - |
652 | | - |
653 | If the record could not be retrieved, the result is positioned | - |
654 | before the first record and false is returned. If the record is | - |
655 | successfully retrieved, true is returned. | - |
656 | | - |
657 | \sa next(), first(), last(), seek(), at(), isActive(), isValid() | - |
658 | */ | - |
659 | bool QSqlQuery::previous() | - |
660 | { | - |
661 | if (!isSelect() || !isActive()) partially evaluated: !isSelect() no Evaluation Count:0 | yes Evaluation Count:3 |
partially evaluated: !isActive() no Evaluation Count:0 | yes Evaluation Count:3 |
| 0-3 |
662 | return false; never executed: return false; | 0 |
663 | if (isForwardOnly()) { evaluated: isForwardOnly() yes Evaluation Count:1 | yes Evaluation Count:2 |
| 1-2 |
664 | qWarning("QSqlQuery::seek: cannot seek backwards in a forward only query"); executed (the execution status of this line is deduced): QMessageLogger("kernel/qsqlquery.cpp", 664, __PRETTY_FUNCTION__).warning("QSqlQuery::seek: cannot seek backwards in a forward only query"); | - |
665 | return false; executed: return false; Execution Count:1 | 1 |
666 | } | - |
667 | | - |
668 | bool b = false; executed (the execution status of this line is deduced): bool b = false; | - |
669 | switch (at()) { | - |
670 | case QSql::BeforeFirstRow: | - |
671 | return false; never executed: return false; | 0 |
672 | case QSql::AfterLastRow: | - |
673 | b = d->sqlResult->fetchLast(); executed (the execution status of this line is deduced): b = d->sqlResult->fetchLast(); | - |
674 | return b; executed: return b; Execution Count:1 | 1 |
675 | default: | - |
676 | if (!d->sqlResult->fetchPrevious()) { partially evaluated: !d->sqlResult->fetchPrevious() no Evaluation Count:0 | yes Evaluation Count:1 |
| 0-1 |
677 | d->sqlResult->setAt(QSql::BeforeFirstRow); never executed (the execution status of this line is deduced): d->sqlResult->setAt(QSql::BeforeFirstRow); | - |
678 | return false; never executed: return false; | 0 |
679 | } | - |
680 | return true; executed: return true; Execution Count:1 | 1 |
681 | } | - |
682 | } | 0 |
683 | | - |
684 | /*! | - |
685 | Retrieves the first record in the result, if available, and | - |
686 | positions the query on the retrieved record. Note that the result | - |
687 | must be in the \l{isActive()}{active} state and isSelect() must | - |
688 | return true before calling this function or it will do nothing and | - |
689 | return false. Returns true if successful. If unsuccessful the query | - |
690 | position is set to an invalid position and false is returned. | - |
691 | | - |
692 | \sa next(), previous(), last(), seek(), at(), isActive(), isValid() | - |
693 | */ | - |
694 | bool QSqlQuery::first() | - |
695 | { | - |
696 | if (!isSelect() || !isActive()) evaluated: !isSelect() yes Evaluation Count:1 | yes Evaluation Count:18 |
partially evaluated: !isActive() no Evaluation Count:0 | yes Evaluation Count:18 |
| 0-18 |
697 | return false; executed: return false; Execution Count:1 | 1 |
698 | if (isForwardOnly() && at() > QSql::BeforeFirstRow) { evaluated: isForwardOnly() yes Evaluation Count:3 | yes Evaluation Count:15 |
evaluated: at() > QSql::BeforeFirstRow yes Evaluation Count:2 | yes Evaluation Count:1 |
| 1-15 |
699 | qWarning("QSqlQuery::seek: cannot seek backwards in a forward only query"); executed (the execution status of this line is deduced): QMessageLogger("kernel/qsqlquery.cpp", 699, __PRETTY_FUNCTION__).warning("QSqlQuery::seek: cannot seek backwards in a forward only query"); | - |
700 | return false; executed: return false; Execution Count:2 | 2 |
701 | } | - |
702 | bool b = false; executed (the execution status of this line is deduced): bool b = false; | - |
703 | b = d->sqlResult->fetchFirst(); executed (the execution status of this line is deduced): b = d->sqlResult->fetchFirst(); | - |
704 | return b; executed: return b; Execution Count:16 | 16 |
705 | } | - |
706 | | - |
707 | /*! | - |
708 | | - |
709 | Retrieves the last record in the result, if available, and positions | - |
710 | the query on the retrieved record. Note that the result must be in | - |
711 | the \l{isActive()}{active} state and isSelect() must return true | - |
712 | before calling this function or it will do nothing and return false. | - |
713 | Returns true if successful. If unsuccessful the query position is | - |
714 | set to an invalid position and false is returned. | - |
715 | | - |
716 | \sa next(), previous(), first(), seek(), at(), isActive(), isValid() | - |
717 | */ | - |
718 | | - |
719 | bool QSqlQuery::last() | - |
720 | { | - |
721 | if (!isSelect() || !isActive()) partially evaluated: !isSelect() no Evaluation Count:0 | yes Evaluation Count:8 |
partially evaluated: !isActive() no Evaluation Count:0 | yes Evaluation Count:8 |
| 0-8 |
722 | return false; never executed: return false; | 0 |
723 | bool b = false; executed (the execution status of this line is deduced): bool b = false; | - |
724 | b = d->sqlResult->fetchLast(); executed (the execution status of this line is deduced): b = d->sqlResult->fetchLast(); | - |
725 | return b; executed: return b; Execution Count:8 | 8 |
726 | } | - |
727 | | - |
728 | /*! | - |
729 | Returns the size of the result (number of rows returned), or -1 if | - |
730 | the size cannot be determined or if the database does not support | - |
731 | reporting information about query sizes. Note that for non-\c SELECT | - |
732 | statements (isSelect() returns false), size() will return -1. If the | - |
733 | query is not active (isActive() returns false), -1 is returned. | - |
734 | | - |
735 | To determine the number of rows affected by a non-\c SELECT | - |
736 | statement, use numRowsAffected(). | - |
737 | | - |
738 | \sa isActive(), numRowsAffected(), QSqlDriver::hasFeature() | - |
739 | */ | - |
740 | int QSqlQuery::size() const | - |
741 | { | - |
742 | if (isActive() && d->sqlResult->driver()->hasFeature(QSqlDriver::QuerySize)) evaluated: isActive() yes Evaluation Count:5 | yes Evaluation Count:1 |
partially evaluated: d->sqlResult->driver()->hasFeature(QSqlDriver::QuerySize) no Evaluation Count:0 | yes Evaluation Count:5 |
| 0-5 |
743 | return d->sqlResult->size(); never executed: return d->sqlResult->size(); | 0 |
744 | return -1; executed: return -1; Execution Count:6 | 6 |
745 | } | - |
746 | | - |
747 | /*! | - |
748 | Returns the number of rows affected by the result's SQL statement, | - |
749 | or -1 if it cannot be determined. Note that for \c SELECT | - |
750 | statements, the value is undefined; use size() instead. If the query | - |
751 | is not \l{isActive()}{active}, -1 is returned. | - |
752 | | - |
753 | \sa size(), QSqlDriver::hasFeature() | - |
754 | */ | - |
755 | | - |
756 | int QSqlQuery::numRowsAffected() const | - |
757 | { | - |
758 | if (isActive()) evaluated: isActive() yes Evaluation Count:15 | yes Evaluation Count:2 |
| 2-15 |
759 | return d->sqlResult->numRowsAffected(); executed: return d->sqlResult->numRowsAffected(); Execution Count:15 | 15 |
760 | return -1; executed: return -1; Execution Count:2 | 2 |
761 | } | - |
762 | | - |
763 | /*! | - |
764 | Returns error information about the last error (if any) that | - |
765 | occurred with this query. | - |
766 | | - |
767 | \sa QSqlError, QSqlDatabase::lastError() | - |
768 | */ | - |
769 | | - |
770 | QSqlError QSqlQuery::lastError() const | - |
771 | { | - |
772 | return d->sqlResult->lastError(); executed: return d->sqlResult->lastError(); Execution Count:11076 | 11076 |
773 | } | - |
774 | | - |
775 | /*! | - |
776 | Returns true if the query is currently positioned on a valid | - |
777 | record; otherwise returns false. | - |
778 | */ | - |
779 | | - |
780 | bool QSqlQuery::isValid() const | - |
781 | { | - |
782 | return d->sqlResult->isValid(); executed: return d->sqlResult->isValid(); Execution Count:9863 | 9863 |
783 | } | - |
784 | | - |
785 | /*! | - |
786 | | - |
787 | Returns true if the query is \e{active}. An active QSqlQuery is one | - |
788 | that has been \l{QSqlQuery::exec()} {exec()'d} successfully but not | - |
789 | yet finished with. When you are finished with an active query, you | - |
790 | can make the query inactive by calling finish() or clear(), or | - |
791 | you can delete the QSqlQuery instance. | - |
792 | | - |
793 | \note Of particular interest is an active query that is a \c{SELECT} | - |
794 | statement. For some databases that support transactions, an active | - |
795 | query that is a \c{SELECT} statement can cause a \l{QSqlDatabase::} | - |
796 | {commit()} or a \l{QSqlDatabase::} {rollback()} to fail, so before | - |
797 | committing or rolling back, you should make your active \c{SELECT} | - |
798 | statement query inactive using one of the ways listed above. | - |
799 | | - |
800 | \sa isSelect() | - |
801 | */ | - |
802 | bool QSqlQuery::isActive() const | - |
803 | { | - |
804 | return d->sqlResult->isActive(); executed: return d->sqlResult->isActive(); Execution Count:17429 | 17429 |
805 | } | - |
806 | | - |
807 | /*! | - |
808 | Returns true if the current query is a \c SELECT statement; | - |
809 | otherwise returns false. | - |
810 | */ | - |
811 | | - |
812 | bool QSqlQuery::isSelect() const | - |
813 | { | - |
814 | return d->sqlResult->isSelect(); executed: return d->sqlResult->isSelect(); Execution Count:7322 | 7322 |
815 | } | - |
816 | | - |
817 | /*! | - |
818 | Returns true if you can only scroll forward through a result set; | - |
819 | otherwise returns false. | - |
820 | | - |
821 | \sa setForwardOnly(), next() | - |
822 | */ | - |
823 | bool QSqlQuery::isForwardOnly() const | - |
824 | { | - |
825 | return d->sqlResult->isForwardOnly(); executed: return d->sqlResult->isForwardOnly(); Execution Count:4288 | 4288 |
826 | } | - |
827 | | - |
828 | /*! | - |
829 | Sets forward only mode to \a forward. If \a forward is true, only | - |
830 | next() and seek() with positive values, are allowed for navigating | - |
831 | the results. | - |
832 | | - |
833 | Forward only mode can be (depending on the driver) more memory | - |
834 | efficient since results do not need to be cached. It will also | - |
835 | improve performance on some databases. For this to be true, you must | - |
836 | call \c setForwardOnly() before the query is prepared or executed. | - |
837 | Note that the constructor that takes a query and a database may | - |
838 | execute the query. | - |
839 | | - |
840 | Forward only mode is off by default. | - |
841 | | - |
842 | Setting forward only to false is a suggestion to the database engine, | - |
843 | which has the final say on whether a result set is forward only or | - |
844 | scrollable. isForwardOnly() will always return the correct status of | - |
845 | the result set. | - |
846 | | - |
847 | \note Calling setForwardOnly after execution of the query will result | - |
848 | in unexpected results at best, and crashes at worst. | - |
849 | | - |
850 | \sa isForwardOnly(), next(), seek(), QSqlResult::setForwardOnly() | - |
851 | */ | - |
852 | void QSqlQuery::setForwardOnly(bool forward) | - |
853 | { | - |
854 | d->sqlResult->setForwardOnly(forward); executed (the execution status of this line is deduced): d->sqlResult->setForwardOnly(forward); | - |
855 | } executed: } Execution Count:646 | 646 |
856 | | - |
857 | /*! | - |
858 | Returns a QSqlRecord containing the field information for the | - |
859 | current query. If the query points to a valid row (isValid() returns | - |
860 | true), the record is populated with the row's values. An empty | - |
861 | record is returned when there is no active query (isActive() returns | - |
862 | false). | - |
863 | | - |
864 | To retrieve values from a query, value() should be used since | - |
865 | its index-based lookup is faster. | - |
866 | | - |
867 | In the following example, a \c{SELECT * FROM} query is executed. | - |
868 | Since the order of the columns is not defined, QSqlRecord::indexOf() | - |
869 | is used to obtain the index of a column. | - |
870 | | - |
871 | \snippet code/src_sql_kernel_qsqlquery.cpp 1 | - |
872 | | - |
873 | \sa value() | - |
874 | */ | - |
875 | QSqlRecord QSqlQuery::record() const | - |
876 | { | - |
877 | QSqlRecord rec = d->sqlResult->record(); executed (the execution status of this line is deduced): QSqlRecord rec = d->sqlResult->record(); | - |
878 | | - |
879 | if (isValid()) { evaluated: isValid() yes Evaluation Count:94 | yes Evaluation Count:403 |
| 94-403 |
880 | for (int i = 0; i < rec.count(); ++i) evaluated: i < rec.count() yes Evaluation Count:231 | yes Evaluation Count:94 |
| 94-231 |
881 | rec.setValue(i, value(i)); executed: rec.setValue(i, value(i)); Execution Count:231 | 231 |
882 | } executed: } Execution Count:94 | 94 |
883 | return rec; executed: return rec; Execution Count:497 | 497 |
884 | } | - |
885 | | - |
886 | /*! | - |
887 | Clears the result set and releases any resources held by the | - |
888 | query. Sets the query state to inactive. You should rarely if ever | - |
889 | need to call this function. | - |
890 | */ | - |
891 | void QSqlQuery::clear() | - |
892 | { | - |
893 | *this = QSqlQuery(driver()->createResult()); executed (the execution status of this line is deduced): *this = QSqlQuery(driver()->createResult()); | - |
894 | } executed: } Execution Count:397 | 397 |
895 | | - |
896 | /*! | - |
897 | Prepares the SQL query \a query for execution. Returns true if the | - |
898 | query is prepared successfully; otherwise returns false. | - |
899 | | - |
900 | The query may contain placeholders for binding values. Both Oracle | - |
901 | style colon-name (e.g., \c{:surname}), and ODBC style (\c{?}) | - |
902 | placeholders are supported; but they cannot be mixed in the same | - |
903 | query. See the \l{QSqlQuery examples}{Detailed Description} for | - |
904 | examples. | - |
905 | | - |
906 | Portability note: Some databases choose to delay preparing a query | - |
907 | until it is executed the first time. In this case, preparing a | - |
908 | syntactically wrong query succeeds, but every consecutive exec() | - |
909 | will fail. | - |
910 | | - |
911 | For SQLite, the query string can contain only one statement at a time. | - |
912 | If more than one statement is given, the function returns false. | - |
913 | | - |
914 | Example: | - |
915 | | - |
916 | \snippet sqldatabase/sqldatabase.cpp 9 | - |
917 | | - |
918 | \sa exec(), bindValue(), addBindValue() | - |
919 | */ | - |
920 | bool QSqlQuery::prepare(const QString& query) | - |
921 | { | - |
922 | if (d->ref.load() != 1) { evaluated: d->ref.load() != 1 yes Evaluation Count:2 | yes Evaluation Count:224 |
| 2-224 |
923 | bool fo = isForwardOnly(); executed (the execution status of this line is deduced): bool fo = isForwardOnly(); | - |
924 | *this = QSqlQuery(driver()->createResult()); executed (the execution status of this line is deduced): *this = QSqlQuery(driver()->createResult()); | - |
925 | setForwardOnly(fo); executed (the execution status of this line is deduced): setForwardOnly(fo); | - |
926 | d->sqlResult->setNumericalPrecisionPolicy(d->sqlResult->numericalPrecisionPolicy()); executed (the execution status of this line is deduced): d->sqlResult->setNumericalPrecisionPolicy(d->sqlResult->numericalPrecisionPolicy()); | - |
927 | } else { executed: } Execution Count:2 | 2 |
928 | d->sqlResult->setActive(false); executed (the execution status of this line is deduced): d->sqlResult->setActive(false); | - |
929 | d->sqlResult->setLastError(QSqlError()); executed (the execution status of this line is deduced): d->sqlResult->setLastError(QSqlError()); | - |
930 | d->sqlResult->setAt(QSql::BeforeFirstRow); executed (the execution status of this line is deduced): d->sqlResult->setAt(QSql::BeforeFirstRow); | - |
931 | d->sqlResult->setNumericalPrecisionPolicy(d->sqlResult->numericalPrecisionPolicy()); executed (the execution status of this line is deduced): d->sqlResult->setNumericalPrecisionPolicy(d->sqlResult->numericalPrecisionPolicy()); | - |
932 | } executed: } Execution Count:224 | 224 |
933 | if (!driver()) { partially evaluated: !driver() no Evaluation Count:0 | yes Evaluation Count:226 |
| 0-226 |
934 | qWarning("QSqlQuery::prepare: no driver"); never executed (the execution status of this line is deduced): QMessageLogger("kernel/qsqlquery.cpp", 934, __PRETTY_FUNCTION__).warning("QSqlQuery::prepare: no driver"); | - |
935 | return false; never executed: return false; | 0 |
936 | } | - |
937 | if (!driver()->isOpen() || driver()->isOpenError()) { partially evaluated: !driver()->isOpen() no Evaluation Count:0 | yes Evaluation Count:226 |
partially evaluated: driver()->isOpenError() no Evaluation Count:0 | yes Evaluation Count:226 |
| 0-226 |
938 | qWarning("QSqlQuery::prepare: database not open"); never executed (the execution status of this line is deduced): QMessageLogger("kernel/qsqlquery.cpp", 938, __PRETTY_FUNCTION__).warning("QSqlQuery::prepare: database not open"); | - |
939 | return false; never executed: return false; | 0 |
940 | } | - |
941 | if (query.isEmpty()) { partially evaluated: query.isEmpty() no Evaluation Count:0 | yes Evaluation Count:226 |
| 0-226 |
942 | qWarning("QSqlQuery::prepare: empty query"); never executed (the execution status of this line is deduced): QMessageLogger("kernel/qsqlquery.cpp", 942, __PRETTY_FUNCTION__).warning("QSqlQuery::prepare: empty query"); | - |
943 | return false; never executed: return false; | 0 |
944 | } | - |
945 | #ifdef QT_DEBUG_SQL | - |
946 | qDebug("\n QSqlQuery::prepare: %s", query.toLocal8Bit().constData()); | - |
947 | #endif | - |
948 | return d->sqlResult->savePrepare(query); executed: return d->sqlResult->savePrepare(query); Execution Count:226 | 226 |
949 | } | - |
950 | | - |
951 | /*! | - |
952 | Executes a previously prepared SQL query. Returns true if the query | - |
953 | executed successfully; otherwise returns false. | - |
954 | | - |
955 | Note that the last error for this query is reset when exec() is | - |
956 | called. | - |
957 | | - |
958 | \sa prepare(), bindValue(), addBindValue(), boundValue(), boundValues() | - |
959 | */ | - |
960 | bool QSqlQuery::exec() | - |
961 | { | - |
962 | d->sqlResult->resetBindCount(); executed (the execution status of this line is deduced): d->sqlResult->resetBindCount(); | - |
963 | | - |
964 | if (d->sqlResult->lastError().isValid()) evaluated: d->sqlResult->lastError().isValid() yes Evaluation Count:92073 | yes Evaluation Count:24651 |
| 24651-92073 |
965 | d->sqlResult->setLastError(QSqlError()); executed: d->sqlResult->setLastError(QSqlError()); Execution Count:92073 | 92073 |
966 | | - |
967 | return d->sqlResult->exec(); executed: return d->sqlResult->exec(); Execution Count:116724 | 116724 |
968 | } | - |
969 | | - |
970 | /*! \enum QSqlQuery::BatchExecutionMode | - |
971 | | - |
972 | \value ValuesAsRows - Updates multiple rows. Treats every entry in a QVariantList as a value for updating the next row. | - |
973 | \value ValuesAsColumns - Updates a single row. Treats every entry in a QVariantList as a single value of an array type. | - |
974 | */ | - |
975 | | - |
976 | /*! | - |
977 | \since 4.2 | - |
978 | | - |
979 | Executes a previously prepared SQL query in a batch. All the bound | - |
980 | parameters have to be lists of variants. If the database doesn't | - |
981 | support batch executions, the driver will simulate it using | - |
982 | conventional exec() calls. | - |
983 | | - |
984 | Returns true if the query is executed successfully; otherwise | - |
985 | returns false. | - |
986 | | - |
987 | Example: | - |
988 | | - |
989 | \snippet code/src_sql_kernel_qsqlquery.cpp 2 | - |
990 | | - |
991 | The example above inserts four new rows into \c myTable: | - |
992 | | - |
993 | \snippet code/src_sql_kernel_qsqlquery.cpp 3 | - |
994 | | - |
995 | To bind NULL values, a null QVariant of the relevant type has to be | - |
996 | added to the bound QVariantList; for example, \c | - |
997 | {QVariant(QVariant::String)} should be used if you are using | - |
998 | strings. | - |
999 | | - |
1000 | \note Every bound QVariantList must contain the same amount of | - |
1001 | variants. | - |
1002 | | - |
1003 | \note The type of the QVariants in a list must not change. For | - |
1004 | example, you cannot mix integer and string variants within a | - |
1005 | QVariantList. | - |
1006 | | - |
1007 | The \a mode parameter indicates how the bound QVariantList will be | - |
1008 | interpreted. If \a mode is \c ValuesAsRows, every variant within | - |
1009 | the QVariantList will be interpreted as a value for a new row. \c | - |
1010 | ValuesAsColumns is a special case for the Oracle driver. In this | - |
1011 | mode, every entry within a QVariantList will be interpreted as | - |
1012 | array-value for an IN or OUT value within a stored procedure. Note | - |
1013 | that this will only work if the IN or OUT value is a table-type | - |
1014 | consisting of only one column of a basic type, for example \c{TYPE | - |
1015 | myType IS TABLE OF VARCHAR(64) INDEX BY BINARY_INTEGER;} | - |
1016 | | - |
1017 | \sa prepare(), bindValue(), addBindValue() | - |
1018 | */ | - |
1019 | bool QSqlQuery::execBatch(BatchExecutionMode mode) | - |
1020 | { | - |
1021 | return d->sqlResult->execBatch(mode == ValuesAsColumns); never executed: return d->sqlResult->execBatch(mode == ValuesAsColumns); | 0 |
1022 | } | - |
1023 | | - |
1024 | /*! | - |
1025 | Set the placeholder \a placeholder to be bound to value \a val in | - |
1026 | the prepared statement. Note that the placeholder mark (e.g \c{:}) | - |
1027 | must be included when specifying the placeholder name. If \a | - |
1028 | paramType is QSql::Out or QSql::InOut, the placeholder will be | - |
1029 | overwritten with data from the database after the exec() call. | - |
1030 | In this case, sufficient space must be pre-allocated to store | - |
1031 | the result into. | - |
1032 | | - |
1033 | To bind a NULL value, use a null QVariant; for example, use | - |
1034 | \c {QVariant(QVariant::String)} if you are binding a string. | - |
1035 | | - |
1036 | Values cannot be bound to multiple locations in the query, eg: | - |
1037 | \code | - |
1038 | INSERT INTO testtable (id, name, samename) VALUES (:id, :name, :name) | - |
1039 | \endcode | - |
1040 | Binding to name will bind to the first :name, but not the second. | - |
1041 | | - |
1042 | \sa addBindValue(), prepare(), exec(), boundValue(), boundValues() | - |
1043 | */ | - |
1044 | void QSqlQuery::bindValue(const QString& placeholder, const QVariant& val, | - |
1045 | QSql::ParamType paramType | - |
1046 | ) | - |
1047 | { | - |
1048 | d->sqlResult->bindValue(placeholder, val, paramType); executed (the execution status of this line is deduced): d->sqlResult->bindValue(placeholder, val, paramType); | - |
1049 | } executed: } Execution Count:46 | 46 |
1050 | | - |
1051 | /*! | - |
1052 | Set the placeholder in position \a pos to be bound to value \a val | - |
1053 | in the prepared statement. Field numbering starts at 0. If \a | - |
1054 | paramType is QSql::Out or QSql::InOut, the placeholder will be | - |
1055 | overwritten with data from the database after the exec() call. | - |
1056 | */ | - |
1057 | void QSqlQuery::bindValue(int pos, const QVariant& val, QSql::ParamType paramType) | - |
1058 | { | - |
1059 | d->sqlResult->bindValue(pos, val, paramType); executed (the execution status of this line is deduced): d->sqlResult->bindValue(pos, val, paramType); | - |
1060 | } executed: } Execution Count:71 | 71 |
1061 | | - |
1062 | /*! | - |
1063 | Adds the value \a val to the list of values when using positional | - |
1064 | value binding. The order of the addBindValue() calls determines | - |
1065 | which placeholder a value will be bound to in the prepared query. | - |
1066 | If \a paramType is QSql::Out or QSql::InOut, the placeholder will be | - |
1067 | overwritten with data from the database after the exec() call. | - |
1068 | | - |
1069 | To bind a NULL value, use a null QVariant; for example, use \c | - |
1070 | {QVariant(QVariant::String)} if you are binding a string. | - |
1071 | | - |
1072 | \sa bindValue(), prepare(), exec(), boundValue(), boundValues() | - |
1073 | */ | - |
1074 | void QSqlQuery::addBindValue(const QVariant& val, QSql::ParamType paramType) | - |
1075 | { | - |
1076 | d->sqlResult->addBindValue(val, paramType); executed (the execution status of this line is deduced): d->sqlResult->addBindValue(val, paramType); | - |
1077 | } executed: } Execution Count:233361 | 233361 |
1078 | | - |
1079 | /*! | - |
1080 | Returns the value for the \a placeholder. | - |
1081 | | - |
1082 | \sa boundValues(), bindValue(), addBindValue() | - |
1083 | */ | - |
1084 | QVariant QSqlQuery::boundValue(const QString& placeholder) const | - |
1085 | { | - |
1086 | return d->sqlResult->boundValue(placeholder); never executed: return d->sqlResult->boundValue(placeholder); | 0 |
1087 | } | - |
1088 | | - |
1089 | /*! | - |
1090 | Returns the value for the placeholder at position \a pos. | - |
1091 | */ | - |
1092 | QVariant QSqlQuery::boundValue(int pos) const | - |
1093 | { | - |
1094 | return d->sqlResult->boundValue(pos); executed: return d->sqlResult->boundValue(pos); Execution Count:1 | 1 |
1095 | } | - |
1096 | | - |
1097 | /*! | - |
1098 | Returns a map of the bound values. | - |
1099 | | - |
1100 | With named binding, the bound values can be examined in the | - |
1101 | following ways: | - |
1102 | | - |
1103 | \snippet sqldatabase/sqldatabase.cpp 14 | - |
1104 | | - |
1105 | With positional binding, the code becomes: | - |
1106 | | - |
1107 | \snippet sqldatabase/sqldatabase.cpp 15 | - |
1108 | | - |
1109 | \sa boundValue(), bindValue(), addBindValue() | - |
1110 | */ | - |
1111 | QMap<QString,QVariant> QSqlQuery::boundValues() const | - |
1112 | { | - |
1113 | QMap<QString,QVariant> map; executed (the execution status of this line is deduced): QMap<QString,QVariant> map; | - |
1114 | | - |
1115 | const QVector<QVariant> values(d->sqlResult->boundValues()); executed (the execution status of this line is deduced): const QVector<QVariant> values(d->sqlResult->boundValues()); | - |
1116 | for (int i = 0; i < values.count(); ++i) evaluated: i < values.count() yes Evaluation Count:104 | yes Evaluation Count:52 |
| 52-104 |
1117 | map[d->sqlResult->boundValueName(i)] = values.at(i); executed: map[d->sqlResult->boundValueName(i)] = values.at(i); Execution Count:104 | 104 |
1118 | return map; executed: return map; Execution Count:52 | 52 |
1119 | } | - |
1120 | | - |
1121 | /*! | - |
1122 | Returns the last query that was successfully executed. | - |
1123 | | - |
1124 | In most cases this function returns the same string as lastQuery(). | - |
1125 | If a prepared query with placeholders is executed on a DBMS that | - |
1126 | does not support it, the preparation of this query is emulated. The | - |
1127 | placeholders in the original query are replaced with their bound | - |
1128 | values to form a new query. This function returns the modified | - |
1129 | query. It is mostly useful for debugging purposes. | - |
1130 | | - |
1131 | \sa lastQuery() | - |
1132 | */ | - |
1133 | QString QSqlQuery::executedQuery() const | - |
1134 | { | - |
1135 | return d->sqlResult->executedQuery(); executed: return d->sqlResult->executedQuery(); Execution Count:1 | 1 |
1136 | } | - |
1137 | | - |
1138 | /*! | - |
1139 | Returns the object ID of the most recent inserted row if the | - |
1140 | database supports it. An invalid QVariant will be returned if the | - |
1141 | query did not insert any value or if the database does not report | - |
1142 | the id back. If more than one row was touched by the insert, the | - |
1143 | behavior is undefined. | - |
1144 | | - |
1145 | For MySQL databases the row's auto-increment field will be returned. | - |
1146 | | - |
1147 | \note For this function to work in PSQL, the table table must | - |
1148 | contain OIDs, which may not have been created by default. Check the | - |
1149 | \c default_with_oids configuration variable to be sure. | - |
1150 | | - |
1151 | \sa QSqlDriver::hasFeature() | - |
1152 | */ | - |
1153 | QVariant QSqlQuery::lastInsertId() const | - |
1154 | { | - |
1155 | return d->sqlResult->lastInsertId(); executed: return d->sqlResult->lastInsertId(); Execution Count:7 | 7 |
1156 | } | - |
1157 | | - |
1158 | /*! | - |
1159 | | - |
1160 | Instruct the database driver to return numerical values with a | - |
1161 | precision specified by \a precisionPolicy. | - |
1162 | | - |
1163 | The Oracle driver, for example, can retrieve numerical values as | - |
1164 | strings to prevent the loss of precision. If high precision doesn't | - |
1165 | matter, use this method to increase execution speed by bypassing | - |
1166 | string conversions. | - |
1167 | | - |
1168 | Note: Drivers that don't support fetching numerical values with low | - |
1169 | precision will ignore the precision policy. You can use | - |
1170 | QSqlDriver::hasFeature() to find out whether a driver supports this | - |
1171 | feature. | - |
1172 | | - |
1173 | Note: Setting the precision policy doesn't affect the currently | - |
1174 | active query. Call \l{exec()}{exec(QString)} or prepare() in order | - |
1175 | to activate the policy. | - |
1176 | | - |
1177 | \sa QSql::NumericalPrecisionPolicy, numericalPrecisionPolicy() | - |
1178 | */ | - |
1179 | void QSqlQuery::setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy) | - |
1180 | { | - |
1181 | d->sqlResult->setNumericalPrecisionPolicy(precisionPolicy); executed (the execution status of this line is deduced): d->sqlResult->setNumericalPrecisionPolicy(precisionPolicy); | - |
1182 | } executed: } Execution Count:6 | 6 |
1183 | | - |
1184 | /*! | - |
1185 | Returns the current precision policy. | - |
1186 | | - |
1187 | \sa QSql::NumericalPrecisionPolicy, setNumericalPrecisionPolicy() | - |
1188 | */ | - |
1189 | QSql::NumericalPrecisionPolicy QSqlQuery::numericalPrecisionPolicy() const | - |
1190 | { | - |
1191 | return d->sqlResult->numericalPrecisionPolicy(); never executed: return d->sqlResult->numericalPrecisionPolicy(); | 0 |
1192 | } | - |
1193 | | - |
1194 | /*! | - |
1195 | \since 4.3.2 | - |
1196 | | - |
1197 | Instruct the database driver that no more data will be fetched from | - |
1198 | this query until it is re-executed. There is normally no need to | - |
1199 | call this function, but it may be helpful in order to free resources | - |
1200 | such as locks or cursors if you intend to re-use the query at a | - |
1201 | later time. | - |
1202 | | - |
1203 | Sets the query to inactive. Bound values retain their values. | - |
1204 | | - |
1205 | \sa prepare(), exec(), isActive() | - |
1206 | */ | - |
1207 | void QSqlQuery::finish() | - |
1208 | { | - |
1209 | if (isActive()) { partially evaluated: isActive() yes Evaluation Count:3 | no Evaluation Count:0 |
| 0-3 |
1210 | d->sqlResult->setLastError(QSqlError()); executed (the execution status of this line is deduced): d->sqlResult->setLastError(QSqlError()); | - |
1211 | d->sqlResult->setAt(QSql::BeforeFirstRow); executed (the execution status of this line is deduced): d->sqlResult->setAt(QSql::BeforeFirstRow); | - |
1212 | d->sqlResult->detachFromResultSet(); executed (the execution status of this line is deduced): d->sqlResult->detachFromResultSet(); | - |
1213 | d->sqlResult->setActive(false); executed (the execution status of this line is deduced): d->sqlResult->setActive(false); | - |
1214 | } executed: } Execution Count:3 | 3 |
1215 | } executed: } Execution Count:3 | 3 |
1216 | | - |
1217 | /*! | - |
1218 | \since 4.4 | - |
1219 | | - |
1220 | Discards the current result set and navigates to the next if available. | - |
1221 | | - |
1222 | Some databases are capable of returning multiple result sets for | - |
1223 | stored procedures or SQL batches (a query strings that contains | - |
1224 | multiple statements). If multiple result sets are available after | - |
1225 | executing a query this function can be used to navigate to the next | - |
1226 | result set(s). | - |
1227 | | - |
1228 | If a new result set is available this function will return true. | - |
1229 | The query will be repositioned on an \e invalid record in the new | - |
1230 | result set and must be navigated to a valid record before data | - |
1231 | values can be retrieved. If a new result set isn't available the | - |
1232 | function returns false and the query is set to inactive. In any | - |
1233 | case the old result set will be discarded. | - |
1234 | | - |
1235 | When one of the statements is a non-select statement a count of | - |
1236 | affected rows may be available instead of a result set. | - |
1237 | | - |
1238 | Note that some databases, i.e. Microsoft SQL Server, requires | - |
1239 | non-scrollable cursors when working with multiple result sets. Some | - |
1240 | databases may execute all statements at once while others may delay | - |
1241 | the execution until the result set is actually accessed, and some | - |
1242 | databases may have restrictions on which statements are allowed to | - |
1243 | be used in a SQL batch. | - |
1244 | | - |
1245 | \sa QSqlDriver::hasFeature(), setForwardOnly(), next(), isSelect(), | - |
1246 | numRowsAffected(), isActive(), lastError() | - |
1247 | */ | - |
1248 | bool QSqlQuery::nextResult() | - |
1249 | { | - |
1250 | if (isActive()) never evaluated: isActive() | 0 |
1251 | return d->sqlResult->nextResult(); never executed: return d->sqlResult->nextResult(); | 0 |
1252 | return false; never executed: return false; | 0 |
1253 | } | - |
1254 | | - |
1255 | QT_END_NAMESPACE | - |
1256 | | - |
| | |