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