kernel/qsqldatabase.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
2** -
3** Copyright (C) 2012 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 "qsqldatabase.h" -
43#include "qsqlquery.h" -
44 -
45#ifdef Q_OS_WIN32 -
46// Conflicting declarations of LPCBYTE in sqlfront.h and winscard.h -
47#define _WINSCARD_H_ -
48#endif -
49 -
50#ifdef QT_SQL_PSQL -
51#include "../drivers/psql/qsql_psql.h" -
52#endif -
53#ifdef QT_SQL_MYSQL -
54#include "../drivers/mysql/qsql_mysql.h" -
55#endif -
56#ifdef QT_SQL_ODBC -
57#include "../drivers/odbc/qsql_odbc.h" -
58#endif -
59#ifdef QT_SQL_OCI -
60#include "../drivers/oci/qsql_oci.h" -
61#endif -
62#ifdef QT_SQL_TDS -
63// conflicting RETCODE typedef between odbc and freetds -
64#define RETCODE DBRETCODE -
65#include "../drivers/tds/qsql_tds.h" -
66#undef RETCODE -
67#endif -
68#ifdef QT_SQL_DB2 -
69#include "../drivers/db2/qsql_db2.h" -
70#endif -
71#ifdef QT_SQL_SQLITE -
72#include "../drivers/sqlite/qsql_sqlite.h" -
73#endif -
74#ifdef QT_SQL_SQLITE2 -
75#include "../drivers/sqlite2/qsql_sqlite2.h" -
76#endif -
77#ifdef QT_SQL_IBASE -
78#undef SQL_FLOAT // avoid clash with ODBC -
79#undef SQL_DOUBLE -
80#undef SQL_TIMESTAMP -
81#undef SQL_TYPE_TIME -
82#undef SQL_TYPE_DATE -
83#undef SQL_DATE -
84#define SCHAR IBASE_SCHAR // avoid clash with ODBC (older versions of ibase.h with Firebird) -
85#include "../drivers/ibase/qsql_ibase.h" -
86#undef SCHAR -
87#endif -
88 -
89#include "qdebug.h" -
90#include "qcoreapplication.h" -
91#include "qreadwritelock.h" -
92#include "qsqlresult.h" -
93#include "qsqldriver.h" -
94#include "qsqldriverplugin.h" -
95#include "qsqlindex.h" -
96#include "private/qfactoryloader_p.h" -
97#include "private/qsqlnulldriver_p.h" -
98#include "qmutex.h" -
99#include "qhash.h" -
100#include <stdlib.h> -
101 -
102QT_BEGIN_NAMESPACE -
103 -
104#ifndef QT_NO_LIBRARY -
105Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,
never executed: delete x;
executed: return thisGlobalStatic.pointer.load();
Execution Count:97
partially evaluated: !thisGlobalStatic.pointer.testAndSetOrdered(0, x)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:9
evaluated: !thisGlobalStatic.pointer.load()
TRUEFALSE
yes
Evaluation Count:9
yes
Evaluation Count:88
partially evaluated: !thisGlobalStatic.destroyed
TRUEFALSE
yes
Evaluation Count:9
no
Evaluation Count:0
0-97
106 (QSqlDriverFactoryInterface_iid, -
107 QLatin1String("/sqldrivers"))) -
108#endif -
109 -
110QT_STATIC_CONST_IMPL char *QSqlDatabase::defaultConnection = "qt_sql_default_connection"; -
111 -
112typedef QHash<QString, QSqlDriverCreatorBase*> DriverDict; -
113 -
114class QConnectionDict: public QHash<QString, QSqlDatabase> -
115{ -
116public: -
117 inline bool contains_ts(const QString &key) -
118 { -
119 QReadLocker locker(&lock);
executed (the execution status of this line is deduced): QReadLocker locker(&lock);
-
120 return contains(key);
executed: return contains(key);
Execution Count:2
2
121 } -
122 inline QStringList keys_ts() const -
123 { -
124 QReadLocker locker(&lock);
never executed (the execution status of this line is deduced): QReadLocker locker(&lock);
-
125 return keys();
never executed: return keys();
0
126 } -
127 -
128 mutable QReadWriteLock lock; -
129}; -
130Q_GLOBAL_STATIC(QConnectionDict, dbDict)
never executed: delete x;
executed: return thisGlobalStatic.pointer.load();
Execution Count:2278
partially evaluated: !thisGlobalStatic.pointer.testAndSetOrdered(0, x)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:9
evaluated: !thisGlobalStatic.pointer.load()
TRUEFALSE
yes
Evaluation Count:9
yes
Evaluation Count:2269
partially evaluated: !thisGlobalStatic.destroyed
TRUEFALSE
yes
Evaluation Count:9
no
Evaluation Count:0
0-2278
131 -
132class QSqlDatabasePrivate -
133{ -
134public: -
135 QSqlDatabasePrivate(QSqlDatabase *d, QSqlDriver *dr = 0): -
136 ref(1), -
137 q(d), -
138 driver(dr), -
139 port(-1) -
140 { -
141 precisionPolicy = QSql::LowPrecisionDouble;
executed (the execution status of this line is deduced): precisionPolicy = QSql::LowPrecisionDouble;
-
142 }
executed: }
Execution Count:45
45
143 QSqlDatabasePrivate(const QSqlDatabasePrivate &other); -
144 ~QSqlDatabasePrivate(); -
145 void init(const QString& type); -
146 void copy(const QSqlDatabasePrivate *other); -
147 void disable(); -
148 -
149 QAtomicInt ref; -
150 QSqlDatabase *q; -
151 QSqlDriver* driver; -
152 QString dbname; -
153 QString uname; -
154 QString pword; -
155 QString hname; -
156 QString drvName; -
157 int port; -
158 QString connOptions; -
159 QString connName; -
160 QSql::NumericalPrecisionPolicy precisionPolicy; -
161 -
162 static QSqlDatabasePrivate *shared_null(); -
163 static QSqlDatabase database(const QString& name, bool open); -
164 static void addDatabase(const QSqlDatabase &db, const QString & name); -
165 static void removeDatabase(const QString& name); -
166 static void invalidateDb(const QSqlDatabase &db, const QString &name, bool doWarn = true); -
167 static DriverDict &driverDict(); -
168 static void cleanConnections(); -
169}; -
170 -
171QSqlDatabasePrivate::QSqlDatabasePrivate(const QSqlDatabasePrivate &other) : ref(1) -
172{ -
173 q = other.q;
never executed (the execution status of this line is deduced): q = other.q;
-
174 dbname = other.dbname;
never executed (the execution status of this line is deduced): dbname = other.dbname;
-
175 uname = other.uname;
never executed (the execution status of this line is deduced): uname = other.uname;
-
176 pword = other.pword;
never executed (the execution status of this line is deduced): pword = other.pword;
-
177 hname = other.hname;
never executed (the execution status of this line is deduced): hname = other.hname;
-
178 drvName = other.drvName;
never executed (the execution status of this line is deduced): drvName = other.drvName;
-
179 port = other.port;
never executed (the execution status of this line is deduced): port = other.port;
-
180 connOptions = other.connOptions;
never executed (the execution status of this line is deduced): connOptions = other.connOptions;
-
181 driver = other.driver;
never executed (the execution status of this line is deduced): driver = other.driver;
-
182 precisionPolicy = other.precisionPolicy;
never executed (the execution status of this line is deduced): precisionPolicy = other.precisionPolicy;
-
183}
never executed: }
0
184 -
185QSqlDatabasePrivate::~QSqlDatabasePrivate() -
186{ -
187 if (driver != shared_null()->driver)
evaluated: driver != shared_null()->driver
TRUEFALSE
yes
Evaluation Count:32
yes
Evaluation Count:13
13-32
188 delete driver;
executed: delete driver;
Execution Count:32
32
189}
executed: }
Execution Count:45
45
190 -
191void QSqlDatabasePrivate::cleanConnections() -
192{ -
193 QConnectionDict *dict = dbDict();
executed (the execution status of this line is deduced): QConnectionDict *dict = dbDict();
-
194 Q_ASSERT(dict);
executed (the execution status of this line is deduced): qt_noop();
-
195 QWriteLocker locker(&dict->lock);
executed (the execution status of this line is deduced): QWriteLocker locker(&dict->lock);
-
196 -
197 QConnectionDict::iterator it = dict->begin();
executed (the execution status of this line is deduced): QConnectionDict::iterator it = dict->begin();
-
198 while (it != dict->end()) {
evaluated: it != dict->end()
TRUEFALSE
yes
Evaluation Count:6
yes
Evaluation Count:22
6-22
199 invalidateDb(it.value(), it.key(), false);
executed (the execution status of this line is deduced): invalidateDb(it.value(), it.key(), false);
-
200 ++it;
executed (the execution status of this line is deduced): ++it;
-
201 }
executed: }
Execution Count:6
6
202 dict->clear();
executed (the execution status of this line is deduced): dict->clear();
-
203}
executed: }
Execution Count:22
22
204 -
205static bool qDriverDictInit = false; -
206static void cleanDriverDict() -
207{ -
208 qDeleteAll(QSqlDatabasePrivate::driverDict());
executed (the execution status of this line is deduced): qDeleteAll(QSqlDatabasePrivate::driverDict());
-
209 QSqlDatabasePrivate::driverDict().clear();
executed (the execution status of this line is deduced): QSqlDatabasePrivate::driverDict().clear();
-
210 QSqlDatabasePrivate::cleanConnections();
executed (the execution status of this line is deduced): QSqlDatabasePrivate::cleanConnections();
-
211 qDriverDictInit = false;
executed (the execution status of this line is deduced): qDriverDictInit = false;
-
212}
executed: }
Execution Count:22
22
213 -
214DriverDict &QSqlDatabasePrivate::driverDict() -
215{ -
216 static DriverDict dict; -
217 if (!qDriverDictInit) {
evaluated: !qDriverDictInit
TRUEFALSE
yes
Evaluation Count:22
yes
Evaluation Count:87
22-87
218 qDriverDictInit = true;
executed (the execution status of this line is deduced): qDriverDictInit = true;
-
219 qAddPostRoutine(cleanDriverDict);
executed (the execution status of this line is deduced): qAddPostRoutine(cleanDriverDict);
-
220 }
executed: }
Execution Count:22
22
221 return dict;
executed: return dict;
Execution Count:109
109
222} -
223 -
224QSqlDatabasePrivate *QSqlDatabasePrivate::shared_null() -
225{ -
226 static QSqlNullDriver dr; -
227 static QSqlDatabasePrivate n(NULL, &dr); -
228 return &n;
executed: return &n;
Execution Count:7890
7890
229} -
230 -
231void QSqlDatabasePrivate::invalidateDb(const QSqlDatabase &db, const QString &name, bool doWarn) -
232{ -
233 if (db.d->ref.load() != 1 && doWarn) {
evaluated: db.d->ref.load() != 1
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:35
partially evaluated: doWarn
TRUEFALSE
yes
Evaluation Count:1
no
Evaluation Count:0
0-35
234 qWarning("QSqlDatabasePrivate::removeDatabase: connection '%s' is still in use, "
executed (the execution status of this line is deduced): QMessageLogger("kernel/qsqldatabase.cpp", 234, __PRETTY_FUNCTION__).warning("QSqlDatabasePrivate::removeDatabase: connection '%s' is still in use, "
-
235 "all queries will cease to work.", name.toLocal8Bit().constData());
executed (the execution status of this line is deduced): "all queries will cease to work.", name.toLocal8Bit().constData());
-
236 db.d->disable();
executed (the execution status of this line is deduced): db.d->disable();
-
237 db.d->connName.clear();
executed (the execution status of this line is deduced): db.d->connName.clear();
-
238 }
executed: }
Execution Count:1
1
239}
executed: }
Execution Count:36
36
240 -
241void QSqlDatabasePrivate::removeDatabase(const QString &name) -
242{ -
243 QConnectionDict *dict = dbDict();
executed (the execution status of this line is deduced): QConnectionDict *dict = dbDict();
-
244 Q_ASSERT(dict);
executed (the execution status of this line is deduced): qt_noop();
-
245 QWriteLocker locker(&dict->lock);
executed (the execution status of this line is deduced): QWriteLocker locker(&dict->lock);
-
246 -
247 if (!dict->contains(name))
partially evaluated: !dict->contains(name)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:30
0-30
248 return;
never executed: return;
0
249 -
250 invalidateDb(dict->take(name), name);
executed (the execution status of this line is deduced): invalidateDb(dict->take(name), name);
-
251}
executed: }
Execution Count:30
30
252 -
253void QSqlDatabasePrivate::addDatabase(const QSqlDatabase &db, const QString &name) -
254{ -
255 QConnectionDict *dict = dbDict();
executed (the execution status of this line is deduced): QConnectionDict *dict = dbDict();
-
256 Q_ASSERT(dict);
executed (the execution status of this line is deduced): qt_noop();
-
257 QWriteLocker locker(&dict->lock);
executed (the execution status of this line is deduced): QWriteLocker locker(&dict->lock);
-
258 -
259 if (dict->contains(name)) {
partially evaluated: dict->contains(name)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:36
0-36
260 invalidateDb(dict->take(name), name);
never executed (the execution status of this line is deduced): invalidateDb(dict->take(name), name);
-
261 qWarning("QSqlDatabasePrivate::addDatabase: duplicate connection name '%s', old "
never executed (the execution status of this line is deduced): QMessageLogger("kernel/qsqldatabase.cpp", 261, __PRETTY_FUNCTION__).warning("QSqlDatabasePrivate::addDatabase: duplicate connection name '%s', old "
-
262 "connection removed.", name.toLocal8Bit().data());
never executed (the execution status of this line is deduced): "connection removed.", name.toLocal8Bit().data());
-
263 }
never executed: }
0
264 dict->insert(name, db);
executed (the execution status of this line is deduced): dict->insert(name, db);
-
265 db.d->connName = name;
executed (the execution status of this line is deduced): db.d->connName = name;
-
266}
executed: }
Execution Count:36
36
267 -
268/*! \internal -
269*/ -
270QSqlDatabase QSqlDatabasePrivate::database(const QString& name, bool open) -
271{ -
272 const QConnectionDict *dict = dbDict();
executed (the execution status of this line is deduced): const QConnectionDict *dict = dbDict();
-
273 Q_ASSERT(dict);
executed (the execution status of this line is deduced): qt_noop();
-
274 -
275 dict->lock.lockForRead();
executed (the execution status of this line is deduced): dict->lock.lockForRead();
-
276 QSqlDatabase db = dict->value(name);
executed (the execution status of this line is deduced): QSqlDatabase db = dict->value(name);
-
277 dict->lock.unlock();
executed (the execution status of this line is deduced): dict->lock.unlock();
-
278 if (db.isValid() && !db.isOpen() && open) {
evaluated: db.isValid()
TRUEFALSE
yes
Evaluation Count:1940
yes
Evaluation Count:248
evaluated: !db.isOpen()
TRUEFALSE
yes
Evaluation Count:25
yes
Evaluation Count:1915
evaluated: open
TRUEFALSE
yes
Evaluation Count:5
yes
Evaluation Count:20
5-1940
279 if (!db.open())
partially evaluated: !db.open()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:5
0-5
280 qWarning() << "QSqlDatabasePrivate::database: unable to open database:" << db.lastError().text();
never executed: QMessageLogger("kernel/qsqldatabase.cpp", 280, __PRETTY_FUNCTION__).warning() << "QSqlDatabasePrivate::database: unable to open database:" << db.lastError().text();
0
281 -
282 }
executed: }
Execution Count:5
5
283 return db;
executed: return db;
Execution Count:2188
2188
284} -
285 -
286 -
287/*! \internal -
288 Copies the connection data from \a other. -
289*/ -
290void QSqlDatabasePrivate::copy(const QSqlDatabasePrivate *other) -
291{ -
292 q = other->q;
executed (the execution status of this line is deduced): q = other->q;
-
293 dbname = other->dbname;
executed (the execution status of this line is deduced): dbname = other->dbname;
-
294 uname = other->uname;
executed (the execution status of this line is deduced): uname = other->uname;
-
295 pword = other->pword;
executed (the execution status of this line is deduced): pword = other->pword;
-
296 hname = other->hname;
executed (the execution status of this line is deduced): hname = other->hname;
-
297 drvName = other->drvName;
executed (the execution status of this line is deduced): drvName = other->drvName;
-
298 port = other->port;
executed (the execution status of this line is deduced): port = other->port;
-
299 connOptions = other->connOptions;
executed (the execution status of this line is deduced): connOptions = other->connOptions;
-
300 precisionPolicy = other->precisionPolicy;
executed (the execution status of this line is deduced): precisionPolicy = other->precisionPolicy;
-
301}
executed: }
Execution Count:7
7
302 -
303void QSqlDatabasePrivate::disable() -
304{ -
305 if (driver != shared_null()->driver) {
partially evaluated: driver != shared_null()->driver
TRUEFALSE
yes
Evaluation Count:1
no
Evaluation Count:0
0-1
306 delete driver;
executed (the execution status of this line is deduced): delete driver;
-
307 driver = shared_null()->driver;
executed (the execution status of this line is deduced): driver = shared_null()->driver;
-
308 }
executed: }
Execution Count:1
1
309}
executed: }
Execution Count:1
1
310 -
311/*! -
312 \class QSqlDriverCreatorBase -
313 \brief The QSqlDriverCreatorBase class is the base class for -
314 SQL driver factories. -
315 -
316 \ingroup database -
317 \inmodule QtSql -
318 -
319 Reimplement createObject() to return an instance of the specific -
320 QSqlDriver subclass that you want to provide. -
321 -
322 See QSqlDatabase::registerSqlDriver() for details. -
323 -
324 \sa QSqlDriverCreator -
325*/ -
326 -
327/*! -
328 \fn QSqlDriverCreatorBase::~QSqlDriverCreatorBase() -
329 -
330 Destroys the SQL driver creator object. -
331*/ -
332 -
333/*! -
334 \fn QSqlDriver *QSqlDriverCreatorBase::createObject() const -
335 -
336 Reimplement this function to returns a new instance of a -
337 QSqlDriver subclass. -
338*/ -
339 -
340/*! -
341 \class QSqlDriverCreator -
342 \brief The QSqlDriverCreator class is a template class that -
343 provides a SQL driver factory for a specific driver type. -
344 -
345 \ingroup database -
346 \inmodule QtSql -
347 -
348 QSqlDriverCreator<T> instantiates objects of type T, where T is a -
349 QSqlDriver subclass. -
350 -
351 See QSqlDatabase::registerSqlDriver() for details. -
352*/ -
353 -
354/*! -
355 \fn QSqlDriver *QSqlDriverCreator::createObject() const -
356 \reimp -
357*/ -
358 -
359/*! -
360 \class QSqlDatabase -
361 \brief The QSqlDatabase class represents a connection to -
362 a database. -
363 -
364 \ingroup database -
365 -
366 \inmodule QtSql -
367 -
368 The QSqlDatabase class provides an interface for accessing a -
369 database through a connection. An instance of QSqlDatabase -
370 represents the connection. The connection provides access to the -
371 database via one of the \l{SQL Database Drivers#Supported -
372 Databases} {supported database drivers}, which are derived from -
373 QSqlDriver. Alternatively, you can subclass your own database -
374 driver from QSqlDriver. See \l{How to Write Your Own Database -
375 Driver} for more information. -
376 -
377 Create a connection (i.e., an instance of QSqlDatabase) by calling -
378 one of the static addDatabase() functions, where you specify -
379 \l{SQL Database Drivers#Supported Databases} {the driver or type -
380 of driver} to use (i.e., what kind of database will you access?) -
381 and a connection name. A connection is known by its own name, -
382 \e{not} by the name of the database it connects to. You can have -
383 multiple connections to one database. QSqlDatabase also supports -
384 the concept of a \e{default} connection, which is the unnamed -
385 connection. To create the default connection, don't pass the -
386 connection name argument when you call addDatabase(). -
387 Subsequently, when you call any static member function that takes -
388 the connection name argument, if you don't pass the connection -
389 name argument, the default connection is assumed. The following -
390 snippet shows how to create and open a default connection to a -
391 PostgreSQL database: -
392 -
393 \snippet sqldatabase/sqldatabase.cpp 0 -
394 -
395 Once the QSqlDatabase object has been created, set the connection -
396 parameters with setDatabaseName(), setUserName(), setPassword(), -
397 setHostName(), setPort(), and setConnectOptions(). Then call -
398 open() to activate the physical connection to the database. The -
399 connection is not usable until you open it. -
400 -
401 The connection defined above will be the \e{default} connection, -
402 because we didn't give a connection name to \l{QSqlDatabase::} -
403 {addDatabase()}. Subsequently, you can get the default connection -
404 by calling database() without the connection name argument: -
405 -
406 \snippet sqldatabase/sqldatabase.cpp 1 -
407 -
408 QSqlDatabase is a value class. Changes made to a database -
409 connection via one instance of QSqlDatabase will affect other -
410 instances of QSqlDatabase that represent the same connection. Use -
411 cloneDatabase() to create an independent database connection based -
412 on an existing one. -
413 -
414 If you create multiple database connections, specify a unique -
415 connection name for each one, when you call addDatabase(). Use -
416 database() with a connection name to get that connection. Use -
417 removeDatabase() with a connection name to remove a connection. -
418 QSqlDatabase outputs a warning if you try to remove a connection -
419 referenced by other QSqlDatabase objects. Use contains() to see if -
420 a given connection name is in the list of connections. -
421 -
422 Once a connection is established, you can call tables() to get the -
423 list of tables in the database, call primaryIndex() to get a -
424 table's primary index, and call record() to get meta-information -
425 about a table's fields (e.g., field names). -
426 -
427 \note QSqlDatabase::exec() is deprecated. Use QSqlQuery::exec() -
428 instead. -
429 -
430 If the driver supports transactions, use transaction() to start a -
431 transaction, and commit() or rollback() to complete it. Use -
432 \l{QSqlDriver::} {hasFeature()} to ask if the driver supports -
433 transactions. \note When using transactions, you must start the -
434 transaction before you create your query. -
435 -
436 If an error occurs, lastError() will return information about it. -
437 -
438 Get the names of the available SQL drivers with drivers(). Check -
439 for the presence of a particular driver with isDriverAvailable(). -
440 If you have created your own custom driver, you must register it -
441 with registerSqlDriver(). -
442 -
443 \sa QSqlDriver, QSqlQuery, {Qt SQL}, {Threads and the SQL Module} -
444*/ -
445 -
446/*! \fn QSqlDatabase QSqlDatabase::addDatabase(const QString &type, const QString &connectionName) -
447 \threadsafe -
448 -
449 Adds a database to the list of database connections using the -
450 driver \a type and the connection name \a connectionName. If -
451 there already exists a database connection called \a -
452 connectionName, that connection is removed. -
453 -
454 The database connection is referred to by \a connectionName. The -
455 newly added database connection is returned. -
456 -
457 If \a type is not available or could not be loaded, isValid() returns false. -
458 -
459 If \a connectionName is not specified, the new connection becomes -
460 the default connection for the application, and subsequent calls -
461 to database() without the connection name argument will return the -
462 default connection. If a \a connectionName is provided here, use -
463 database(\a connectionName) to retrieve the connection. -
464 -
465 \warning If you add a connection with the same name as an existing -
466 connection, the new connection replaces the old one. If you call -
467 this function more than once without specifying \a connectionName, -
468 the default connection will be the one replaced. -
469 -
470 Before using the connection, it must be initialized. e.g., call -
471 some or all of setDatabaseName(), setUserName(), setPassword(), -
472 setHostName(), setPort(), and setConnectOptions(), and, finally, -
473 open(). -
474 -
475 \sa database(), removeDatabase(), {Threads and the SQL Module} -
476*/ -
477QSqlDatabase QSqlDatabase::addDatabase(const QString &type, const QString &connectionName) -
478{ -
479 QSqlDatabase db(type);
executed (the execution status of this line is deduced): QSqlDatabase db(type);
-
480 QSqlDatabasePrivate::addDatabase(db, connectionName);
executed (the execution status of this line is deduced): QSqlDatabasePrivate::addDatabase(db, connectionName);
-
481 return db;
executed: return db;
Execution Count:29
29
482} -
483 -
484/*! -
485 \threadsafe -
486 -
487 Returns the database connection called \a connectionName. The -
488 database connection must have been previously added with -
489 addDatabase(). If \a open is true (the default) and the database -
490 connection is not already open it is opened now. If no \a -
491 connectionName is specified the default connection is used. If \a -
492 connectionName does not exist in the list of databases, an invalid -
493 connection is returned. -
494 -
495 \sa isOpen(), {Threads and the SQL Module} -
496*/ -
497 -
498QSqlDatabase QSqlDatabase::database(const QString& connectionName, bool open) -
499{ -
500 return QSqlDatabasePrivate::database(connectionName, open);
executed: return QSqlDatabasePrivate::database(connectionName, open);
Execution Count:2188
2188
501} -
502 -
503/*! -
504 \threadsafe -
505 -
506 Removes the database connection \a connectionName from the list of -
507 database connections. -
508 -
509 \warning There should be no open queries on the database -
510 connection when this function is called, otherwise a resource leak -
511 will occur. -
512 -
513 Example: -
514 -
515 \snippet code/src_sql_kernel_qsqldatabase.cpp 0 -
516 -
517 The correct way to do it: -
518 -
519 \snippet code/src_sql_kernel_qsqldatabase.cpp 1 -
520 -
521 To remove the default connection, which may have been created with a -
522 call to addDatabase() not specifying a connection name, you can -
523 retrieve the default connection name by calling connectionName() on -
524 the database returned by database(). Note that if a default database -
525 hasn't been created an invalid database will be returned. -
526 -
527 \sa database(), connectionName(), {Threads and the SQL Module} -
528*/ -
529 -
530void QSqlDatabase::removeDatabase(const QString& connectionName) -
531{ -
532 QSqlDatabasePrivate::removeDatabase(connectionName);
executed (the execution status of this line is deduced): QSqlDatabasePrivate::removeDatabase(connectionName);
-
533}
executed: }
Execution Count:30
30
534 -
535/*! -
536 Returns a list of all the available database drivers. -
537 -
538 \sa registerSqlDriver() -
539*/ -
540 -
541QStringList QSqlDatabase::drivers() -
542{ -
543 QStringList list;
executed (the execution status of this line is deduced): QStringList list;
-
544 -
545#ifdef QT_SQL_PSQL -
546 list << QLatin1String("QPSQL7"); -
547 list << QLatin1String("QPSQL"); -
548#endif -
549#ifdef QT_SQL_MYSQL -
550 list << QLatin1String("QMYSQL3"); -
551 list << QLatin1String("QMYSQL"); -
552#endif -
553#ifdef QT_SQL_ODBC -
554 list << QLatin1String("QODBC3"); -
555 list << QLatin1String("QODBC"); -
556#endif -
557#ifdef QT_SQL_OCI -
558 list << QLatin1String("QOCI8"); -
559 list << QLatin1String("QOCI"); -
560#endif -
561#ifdef QT_SQL_TDS -
562 list << QLatin1String("QTDS7"); -
563 list << QLatin1String("QTDS"); -
564#endif -
565#ifdef QT_SQL_DB2 -
566 list << QLatin1String("QDB2"); -
567#endif -
568#ifdef QT_SQL_SQLITE -
569 list << QLatin1String("QSQLITE"); -
570#endif -
571#ifdef QT_SQL_SQLITE2 -
572 list << QLatin1String("QSQLITE2"); -
573#endif -
574#ifdef QT_SQL_IBASE -
575 list << QLatin1String("QIBASE"); -
576#endif -
577 -
578#ifndef QT_NO_LIBRARY -
579 if (QFactoryLoader *fl = loader()) {
partially evaluated: QFactoryLoader *fl = loader()
TRUEFALSE
yes
Evaluation Count:27
no
Evaluation Count:0
0-27
580 typedef QMultiMap<int, QString> PluginKeyMap;
executed (the execution status of this line is deduced): typedef QMultiMap<int, QString> PluginKeyMap;
-
581 typedef PluginKeyMap::const_iterator PluginKeyMapConstIterator;
executed (the execution status of this line is deduced): typedef PluginKeyMap::const_iterator PluginKeyMapConstIterator;
-
582 -
583 const PluginKeyMap keyMap = fl->keyMap();
executed (the execution status of this line is deduced): const PluginKeyMap keyMap = fl->keyMap();
-
584 const PluginKeyMapConstIterator cend = keyMap.constEnd();
executed (the execution status of this line is deduced): const PluginKeyMapConstIterator cend = keyMap.constEnd();
-
585 for (PluginKeyMapConstIterator it = keyMap.constBegin(); it != cend; ++it)
evaluated: it != cend
TRUEFALSE
yes
Evaluation Count:270
yes
Evaluation Count:27
27-270
586 if (!list.contains(it.value()))
partially evaluated: !list.contains(it.value())
TRUEFALSE
yes
Evaluation Count:270
no
Evaluation Count:0
0-270
587 list << it.value();
executed: list << it.value();
Execution Count:270
270
588 }
executed: }
Execution Count:27
27
589#endif -
590 -
591 DriverDict dict = QSqlDatabasePrivate::driverDict();
executed (the execution status of this line is deduced): DriverDict dict = QSqlDatabasePrivate::driverDict();
-
592 for (DriverDict::const_iterator i = dict.constBegin(); i != dict.constEnd(); ++i) {
evaluated: i != dict.constEnd()
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:27
1-27
593 if (!list.contains(i.key()))
partially evaluated: !list.contains(i.key())
TRUEFALSE
yes
Evaluation Count:1
no
Evaluation Count:0
0-1
594 list << i.key();
executed: list << i.key();
Execution Count:1
1
595 }
executed: }
Execution Count:1
1
596 -
597 return list;
executed: return list;
Execution Count:27
27
598} -
599 -
600/*! -
601 This function registers a new SQL driver called \a name, within -
602 the SQL framework. This is useful if you have a custom SQL driver -
603 and don't want to compile it as a plugin. -
604 -
605 Example: -
606 \snippet code/src_sql_kernel_qsqldatabase.cpp 2 -
607 -
608 QSqlDatabase takes ownership of the \a creator pointer, so you -
609 mustn't delete it yourself. -
610 -
611 \sa drivers() -
612*/ -
613void QSqlDatabase::registerSqlDriver(const QString& name, QSqlDriverCreatorBase *creator) -
614{ -
615 delete QSqlDatabasePrivate::driverDict().take(name);
executed (the execution status of this line is deduced): delete QSqlDatabasePrivate::driverDict().take(name);
-
616 if (creator)
partially evaluated: creator
TRUEFALSE
yes
Evaluation Count:1
no
Evaluation Count:0
0-1
617 QSqlDatabasePrivate::driverDict().insert(name, creator);
executed: QSqlDatabasePrivate::driverDict().insert(name, creator);
Execution Count:1
1
618}
executed: }
Execution Count:1
1
619 -
620/*! -
621 \threadsafe -
622 -
623 Returns true if the list of database connections contains \a -
624 connectionName; otherwise returns false. -
625 -
626 \sa connectionNames(), database(), {Threads and the SQL Module} -
627*/ -
628 -
629bool QSqlDatabase::contains(const QString& connectionName) -
630{ -
631 return dbDict()->contains_ts(connectionName);
executed: return dbDict()->contains_ts(connectionName);
Execution Count:2
2
632} -
633 -
634/*! -
635 \threadsafe -
636 -
637 Returns a list containing the names of all connections. -
638 -
639 \sa contains(), database(), {Threads and the SQL Module} -
640*/ -
641QStringList QSqlDatabase::connectionNames() -
642{ -
643 return dbDict()->keys_ts();
never executed: return dbDict()->keys_ts();
0
644} -
645 -
646/*! -
647 \overload -
648 -
649 Creates a QSqlDatabase connection that uses the driver referred -
650 to by \a type. If the \a type is not recognized, the database -
651 connection will have no functionality. -
652 -
653 The currently available driver types are: -
654 -
655 \table -
656 \header \li Driver Type \li Description -
657 \row \li QDB2 \li IBM DB2 -
658 \row \li QIBASE \li Borland InterBase Driver -
659 \row \li QMYSQL \li MySQL Driver -
660 \row \li QOCI \li Oracle Call Interface Driver -
661 \row \li QODBC \li ODBC Driver (includes Microsoft SQL Server) -
662 \row \li QPSQL \li PostgreSQL Driver -
663 \row \li QSQLITE \li SQLite version 3 or above -
664 \row \li QSQLITE2 \li SQLite version 2 -
665 \row \li QTDS \li Sybase Adaptive Server -
666 \endtable -
667 -
668 Additional third party drivers, including your own custom -
669 drivers, can be loaded dynamically. -
670 -
671 \sa {SQL Database Drivers}, registerSqlDriver(), drivers() -
672*/ -
673 -
674QSqlDatabase::QSqlDatabase(const QString &type) -
675{ -
676 d = new QSqlDatabasePrivate(this);
executed (the execution status of this line is deduced): d = new QSqlDatabasePrivate(this);
-
677 d->init(type);
executed (the execution status of this line is deduced): d->init(type);
-
678}
executed: }
Execution Count:36
36
679 -
680/*! -
681 \overload -
682 -
683 Creates a database connection using the given \a driver. -
684*/ -
685 -
686QSqlDatabase::QSqlDatabase(QSqlDriver *driver) -
687{ -
688 d = new QSqlDatabasePrivate(this, driver);
never executed (the execution status of this line is deduced): d = new QSqlDatabasePrivate(this, driver);
-
689}
never executed: }
0
690 -
691/*! -
692 Creates an empty, invalid QSqlDatabase object. Use addDatabase(), -
693 removeDatabase(), and database() to get valid QSqlDatabase -
694 objects. -
695*/ -
696QSqlDatabase::QSqlDatabase() -
697{ -
698 d = QSqlDatabasePrivate::shared_null();
executed (the execution status of this line is deduced): d = QSqlDatabasePrivate::shared_null();
-
699 d->ref.ref();
executed (the execution status of this line is deduced): d->ref.ref();
-
700}
executed: }
Execution Count:1246
1246
701 -
702/*! -
703 Creates a copy of \a other. -
704*/ -
705QSqlDatabase::QSqlDatabase(const QSqlDatabase &other) -
706{ -
707 d = other.d;
executed (the execution status of this line is deduced): d = other.d;
-
708 d->ref.ref();
executed (the execution status of this line is deduced): d->ref.ref();
-
709}
executed: }
Execution Count:17866
17866
710 -
711/*! -
712 Assigns \a other to this object. -
713*/ -
714QSqlDatabase &QSqlDatabase::operator=(const QSqlDatabase &other) -
715{ -
716 qAtomicAssign(d, other.d);
executed (the execution status of this line is deduced): qAtomicAssign(d, other.d);
-
717 return *this;
executed: return *this;
Execution Count:948
948
718} -
719 -
720/*! -
721 \internal -
722 -
723 Create the actual driver instance \a type. -
724*/ -
725 -
726void QSqlDatabasePrivate::init(const QString &type) -
727{ -
728 drvName = type;
executed (the execution status of this line is deduced): drvName = type;
-
729 -
730 if (!driver) {
partially evaluated: !driver
TRUEFALSE
yes
Evaluation Count:36
no
Evaluation Count:0
0-36
731#ifdef QT_SQL_PSQL -
732 if (type == QLatin1String("QPSQL") || type == QLatin1String("QPSQL7")) -
733 driver = new QPSQLDriver(); -
734#endif -
735#ifdef QT_SQL_MYSQL -
736 if (type == QLatin1String("QMYSQL") || type == QLatin1String("QMYSQL3")) -
737 driver = new QMYSQLDriver(); -
738#endif -
739#ifdef QT_SQL_ODBC -
740 if (type == QLatin1String("QODBC") || type == QLatin1String("QODBC3")) -
741 driver = new QODBCDriver(); -
742#endif -
743#ifdef QT_SQL_OCI -
744 if (type == QLatin1String("QOCI") || type == QLatin1String("QOCI8")) -
745 driver = new QOCIDriver(); -
746#endif -
747#ifdef QT_SQL_TDS -
748 if (type == QLatin1String("QTDS") || type == QLatin1String("QTDS7")) -
749 driver = new QTDSDriver(); -
750#endif -
751#ifdef QT_SQL_DB2 -
752 if (type == QLatin1String("QDB2")) -
753 driver = new QDB2Driver(); -
754#endif -
755#ifdef QT_SQL_SQLITE -
756 if (type == QLatin1String("QSQLITE")) -
757 driver = new QSQLiteDriver(); -
758#endif -
759#ifdef QT_SQL_SQLITE2 -
760 if (type == QLatin1String("QSQLITE2")) -
761 driver = new QSQLite2Driver(); -
762#endif -
763#ifdef QT_SQL_IBASE -
764 if (type == QLatin1String("QIBASE")) -
765 driver = new QIBaseDriver(); -
766#endif -
767 }
executed: }
Execution Count:36
36
768 -
769 if (!driver) {
partially evaluated: !driver
TRUEFALSE
yes
Evaluation Count:36
no
Evaluation Count:0
0-36
770 DriverDict dict = QSqlDatabasePrivate::driverDict();
executed (the execution status of this line is deduced): DriverDict dict = QSqlDatabasePrivate::driverDict();
-
771 for (DriverDict::const_iterator it = dict.constBegin();
executed (the execution status of this line is deduced): for (DriverDict::const_iterator it = dict.constBegin();
-
772 it != dict.constEnd() && !driver; ++it) {
evaluated: it != dict.constEnd()
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:36
partially evaluated: !driver
TRUEFALSE
yes
Evaluation Count:1
no
Evaluation Count:0
0-36
773 if (type == it.key()) {
partially evaluated: type == it.key()
TRUEFALSE
yes
Evaluation Count:1
no
Evaluation Count:0
0-1
774 driver = ((QSqlDriverCreatorBase*)(*it))->createObject();
executed (the execution status of this line is deduced): driver = ((QSqlDriverCreatorBase*)(*it))->createObject();
-
775 }
executed: }
Execution Count:1
1
776 }
executed: }
Execution Count:1
1
777 }
executed: }
Execution Count:36
36
778 -
779#ifndef QT_NO_LIBRARY -
780 if (!driver && loader())
evaluated: !driver
TRUEFALSE
yes
Evaluation Count:35
yes
Evaluation Count:1
partially evaluated: loader()
TRUEFALSE
yes
Evaluation Count:35
no
Evaluation Count:0
0-35
781 driver = qLoadPlugin<QSqlDriver, QSqlDriverPlugin>(loader(), type);
executed: driver = qLoadPlugin<QSqlDriver, QSqlDriverPlugin>(loader(), type);
Execution Count:35
35
782#endif // QT_NO_LIBRARY -
783 -
784 if (!driver) {
evaluated: !driver
TRUEFALSE
yes
Evaluation Count:3
yes
Evaluation Count:33
3-33
785 qWarning("QSqlDatabase: %s driver not loaded", type.toLatin1().data());
executed (the execution status of this line is deduced): QMessageLogger("kernel/qsqldatabase.cpp", 785, __PRETTY_FUNCTION__).warning("QSqlDatabase: %s driver not loaded", type.toLatin1().data());
-
786 qWarning("QSqlDatabase: available drivers: %s",
executed (the execution status of this line is deduced): QMessageLogger("kernel/qsqldatabase.cpp", 786, __PRETTY_FUNCTION__).warning("QSqlDatabase: available drivers: %s",
-
787 QSqlDatabase::drivers().join(QLatin1Char(' ')).toLatin1().data());
executed (the execution status of this line is deduced): QSqlDatabase::drivers().join(QLatin1Char(' ')).toLatin1().data());
-
788 if (QCoreApplication::instance() == 0)
evaluated: QCoreApplication::instance() == 0
TRUEFALSE
yes
Evaluation Count:1
yes
Evaluation Count:2
1-2
789 qWarning("QSqlDatabase: an instance of QCoreApplication is required for loading driver plugins");
executed: QMessageLogger("kernel/qsqldatabase.cpp", 789, __PRETTY_FUNCTION__).warning("QSqlDatabase: an instance of QCoreApplication is required for loading driver plugins");
Execution Count:1
1
790 driver = shared_null()->driver;
executed (the execution status of this line is deduced): driver = shared_null()->driver;
-
791 }
executed: }
Execution Count:3
3
792}
executed: }
Execution Count:36
36
793 -
794/*! -
795 Destroys the object and frees any allocated resources. -
796 -
797 If this is the last QSqlDatabase object that uses a certain -
798 database connection, the database connection is automatically closed. -
799 -
800 \sa close() -
801*/ -
802 -
803QSqlDatabase::~QSqlDatabase() -
804{ -
805 if (!d->ref.deref()) {
evaluated: !d->ref.deref()
TRUEFALSE
yes
Evaluation Count:36
yes
Evaluation Count:19112
36-19112
806 close();
executed (the execution status of this line is deduced): close();
-
807 delete d;
executed (the execution status of this line is deduced): delete d;
-
808 }
executed: }
Execution Count:36
36
809}
executed: }
Execution Count:19148
19148
810 -
811/*! -
812 Executes a SQL statement on the database and returns a QSqlQuery -
813 object. Use lastError() to retrieve error information. If \a -
814 query is empty, an empty, invalid query is returned and -
815 lastError() is not affected. -
816 -
817 \sa QSqlQuery, lastError() -
818*/ -
819 -
820QSqlQuery QSqlDatabase::exec(const QString & query) const -
821{ -
822 QSqlQuery r(d->driver->createResult());
executed (the execution status of this line is deduced): QSqlQuery r(d->driver->createResult());
-
823 if (!query.isEmpty()) {
partially evaluated: !query.isEmpty()
TRUEFALSE
yes
Evaluation Count:16
no
Evaluation Count:0
0-16
824 r.exec(query);
executed (the execution status of this line is deduced): r.exec(query);
-
825 d->driver->setLastError(r.lastError());
executed (the execution status of this line is deduced): d->driver->setLastError(r.lastError());
-
826 }
executed: }
Execution Count:16
16
827 return r;
executed: return r;
Execution Count:16
16
828} -
829 -
830/*! -
831 Opens the database connection using the current connection -
832 values. Returns true on success; otherwise returns false. Error -
833 information can be retrieved using lastError(). -
834 -
835 \sa lastError(), setDatabaseName(), setUserName(), setPassword(), -
836 setHostName(), setPort(), setConnectOptions() -
837*/ -
838 -
839bool QSqlDatabase::open() -
840{ -
841 return d->driver->open(d->dbname, d->uname, d->pword, d->hname,
executed: return d->driver->open(d->dbname, d->uname, d->pword, d->hname, d->port, d->connOptions);
Execution Count:48
48
842 d->port, d->connOptions);
executed: return d->driver->open(d->dbname, d->uname, d->pword, d->hname, d->port, d->connOptions);
Execution Count:48
48
843} -
844 -
845/*! -
846 \overload -
847 -
848 Opens the database connection using the given \a user name and \a -
849 password. Returns true on success; otherwise returns false. Error -
850 information can be retrieved using the lastError() function. -
851 -
852 This function does not store the password it is given. Instead, -
853 the password is passed directly to the driver for opening the -
854 connection and it is then discarded. -
855 -
856 \sa lastError() -
857*/ -
858 -
859bool QSqlDatabase::open(const QString& user, const QString& password) -
860{ -
861 setUserName(user);
executed (the execution status of this line is deduced): setUserName(user);
-
862 return d->driver->open(d->dbname, user, password, d->hname,
executed: return d->driver->open(d->dbname, user, password, d->hname, d->port, d->connOptions);
Execution Count:1
1
863 d->port, d->connOptions);
executed: return d->driver->open(d->dbname, user, password, d->hname, d->port, d->connOptions);
Execution Count:1
1
864} -
865 -
866/*! -
867 Closes the database connection, freeing any resources acquired, and -
868 invalidating any existing QSqlQuery objects that are used with the -
869 database. -
870 -
871 This will also affect copies of this QSqlDatabase object. -
872 -
873 \sa removeDatabase() -
874*/ -
875 -
876void QSqlDatabase::close() -
877{ -
878 d->driver->close();
executed (the execution status of this line is deduced): d->driver->close();
-
879}
executed: }
Execution Count:76
76
880 -
881/*! -
882 Returns true if the database connection is currently open; -
883 otherwise returns false. -
884*/ -
885 -
886bool QSqlDatabase::isOpen() const -
887{ -
888 return d->driver->isOpen();
executed: return d->driver->isOpen();
Execution Count:1999
1999
889} -
890 -
891/*! -
892 Returns true if there was an error opening the database -
893 connection; otherwise returns false. Error information can be -
894 retrieved using the lastError() function. -
895*/ -
896 -
897bool QSqlDatabase::isOpenError() const -
898{ -
899 return d->driver->isOpenError();
executed: return d->driver->isOpenError();
Execution Count:10
10
900} -
901 -
902/*! -
903 Begins a transaction on the database if the driver supports -
904 transactions. Returns \c{true} if the operation succeeded. -
905 Otherwise it returns \c{false}. -
906 -
907 \sa QSqlDriver::hasFeature(), commit(), rollback() -
908*/ -
909bool QSqlDatabase::transaction() -
910{ -
911 if (!d->driver->hasFeature(QSqlDriver::Transactions))
partially evaluated: !d->driver->hasFeature(QSqlDriver::Transactions)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:5
0-5
912 return false;
never executed: return false;
0
913 return d->driver->beginTransaction();
executed: return d->driver->beginTransaction();
Execution Count:5
5
914} -
915 -
916/*! -
917 Commits a transaction to the database if the driver supports -
918 transactions and a transaction() has been started. Returns \c{true} -
919 if the operation succeeded. Otherwise it returns \c{false}. -
920 -
921 \note For some databases, the commit will fail and return \c{false} -
922 if there is an \l{QSqlQuery::isActive()} {active query} using the -
923 database for a \c{SELECT}. Make the query \l{QSqlQuery::isActive()} -
924 {inactive} before doing the commit. -
925 -
926 Call lastError() to get information about errors. -
927 -
928 \sa QSqlQuery::isActive(), QSqlDriver::hasFeature(), rollback() -
929*/ -
930bool QSqlDatabase::commit() -
931{ -
932 if (!d->driver->hasFeature(QSqlDriver::Transactions))
partially evaluated: !d->driver->hasFeature(QSqlDriver::Transactions)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:4
0-4
933 return false;
never executed: return false;
0
934 return d->driver->commitTransaction();
executed: return d->driver->commitTransaction();
Execution Count:4
4
935} -
936 -
937/*! -
938 Rolls back a transaction on the database, if the driver supports -
939 transactions and a transaction() has been started. Returns \c{true} -
940 if the operation succeeded. Otherwise it returns \c{false}. -
941 -
942 \note For some databases, the rollback will fail and return -
943 \c{false} if there is an \l{QSqlQuery::isActive()} {active query} -
944 using the database for a \c{SELECT}. Make the query -
945 \l{QSqlQuery::isActive()} {inactive} before doing the rollback. -
946 -
947 Call lastError() to get information about errors. -
948 -
949 \sa QSqlQuery::isActive(), QSqlDriver::hasFeature(), commit() -
950*/ -
951bool QSqlDatabase::rollback() -
952{ -
953 if (!d->driver->hasFeature(QSqlDriver::Transactions))
partially evaluated: !d->driver->hasFeature(QSqlDriver::Transactions)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1
0-1
954 return false;
never executed: return false;
0
955 return d->driver->rollbackTransaction();
executed: return d->driver->rollbackTransaction();
Execution Count:1
1
956} -
957 -
958/*! -
959 Sets the connection's database name to \a name. To have effect, -
960 the database name must be set \e{before} the connection is -
961 \l{open()} {opened}. Alternatively, you can close() the -
962 connection, set the database name, and call open() again. \note -
963 The \e{database name} is not the \e{connection name}. The -
964 connection name must be passed to addDatabase() at connection -
965 object create time. -
966 -
967 For the QOCI (Oracle) driver, the database name is the TNS -
968 Service Name. -
969 -
970 For the QODBC driver, the \a name can either be a DSN, a DSN -
971 filename (in which case the file must have a \c .dsn extension), -
972 or a connection string. -
973 -
974 For example, Microsoft Access users can use the following -
975 connection string to open an \c .mdb file directly, instead of -
976 having to create a DSN entry in the ODBC manager: -
977 -
978 \snippet code/src_sql_kernel_qsqldatabase.cpp 3 -
979 -
980 There is no default value. -
981 -
982 \sa databaseName(), setUserName(), setPassword(), setHostName(), -
983 setPort(), setConnectOptions(), open() -
984*/ -
985 -
986void QSqlDatabase::setDatabaseName(const QString& name) -
987{ -
988 if (isValid())
partially evaluated: isValid()
TRUEFALSE
yes
Evaluation Count:26
no
Evaluation Count:0
0-26
989 d->dbname = name;
executed: d->dbname = name;
Execution Count:26
26
990}
executed: }
Execution Count:26
26
991 -
992/*! -
993 Sets the connection's user name to \a name. To have effect, the -
994 user name must be set \e{before} the connection is \l{open()} -
995 {opened}. Alternatively, you can close() the connection, set the -
996 user name, and call open() again. -
997 -
998 There is no default value. -
999 -
1000 \sa userName(), setDatabaseName(), setPassword(), setHostName(), -
1001 setPort(), setConnectOptions(), open() -
1002*/ -
1003 -
1004void QSqlDatabase::setUserName(const QString& name) -
1005{ -
1006 if (isValid())
partially evaluated: isValid()
TRUEFALSE
yes
Evaluation Count:24
no
Evaluation Count:0
0-24
1007 d->uname = name;
executed: d->uname = name;
Execution Count:24
24
1008}
executed: }
Execution Count:24
24
1009 -
1010/*! -
1011 Sets the connection's password to \a password. To have effect, the -
1012 password must be set \e{before} the connection is \l{open()} -
1013 {opened}. Alternatively, you can close() the connection, set the -
1014 password, and call open() again. -
1015 -
1016 There is no default value. -
1017 -
1018 \warning This function stores the password in plain text within -
1019 Qt. Use the open() call that takes a password as parameter to -
1020 avoid this behavior. -
1021 -
1022 \sa password(), setUserName(), setDatabaseName(), setHostName(), -
1023 setPort(), setConnectOptions(), open() -
1024*/ -
1025 -
1026void QSqlDatabase::setPassword(const QString& password) -
1027{ -
1028 if (isValid())
partially evaluated: isValid()
TRUEFALSE
yes
Evaluation Count:23
no
Evaluation Count:0
0-23
1029 d->pword = password;
executed: d->pword = password;
Execution Count:23
23
1030}
executed: }
Execution Count:23
23
1031 -
1032/*! -
1033 Sets the connection's host name to \a host. To have effect, the -
1034 host name must be set \e{before} the connection is \l{open()} -
1035 {opened}. Alternatively, you can close() the connection, set the -
1036 host name, and call open() again. -
1037 -
1038 There is no default value. -
1039 -
1040 \sa hostName(), setUserName(), setPassword(), setDatabaseName(), -
1041 setPort(), setConnectOptions(), open() -
1042*/ -
1043 -
1044void QSqlDatabase::setHostName(const QString& host) -
1045{ -
1046 if (isValid())
partially evaluated: isValid()
TRUEFALSE
yes
Evaluation Count:23
no
Evaluation Count:0
0-23
1047 d->hname = host;
executed: d->hname = host;
Execution Count:23
23
1048}
executed: }
Execution Count:23
23
1049 -
1050/*! -
1051 Sets the connection's port number to \a port. To have effect, the -
1052 port number must be set \e{before} the connection is \l{open()} -
1053 {opened}. Alternatively, you can close() the connection, set the -
1054 port number, and call open() again.. -
1055 -
1056 There is no default value. -
1057 -
1058 \sa port(), setUserName(), setPassword(), setHostName(), -
1059 setDatabaseName(), setConnectOptions(), open() -
1060*/ -
1061 -
1062void QSqlDatabase::setPort(int port) -
1063{ -
1064 if (isValid())
partially evaluated: isValid()
TRUEFALSE
yes
Evaluation Count:21
no
Evaluation Count:0
0-21
1065 d->port = port;
executed: d->port = port;
Execution Count:21
21
1066}
executed: }
Execution Count:21
21
1067 -
1068/*! -
1069 Returns the connection's database name, which may be empty. -
1070 \note The database name is not the connection name. -
1071 -
1072 \sa setDatabaseName() -
1073*/ -
1074QString QSqlDatabase::databaseName() const -
1075{ -
1076 return d->dbname;
executed: return d->dbname;
Execution Count:127
127
1077} -
1078 -
1079/*! -
1080 Returns the connection's user name; it may be empty. -
1081 -
1082 \sa setUserName() -
1083*/ -
1084QString QSqlDatabase::userName() const -
1085{ -
1086 return d->uname;
executed: return d->uname;
Execution Count:2
2
1087} -
1088 -
1089/*! -
1090 Returns the connection's password. If the password was not set -
1091 with setPassword(), and if the password was given in the open() -
1092 call, or if no password was used, an empty string is returned. -
1093*/ -
1094QString QSqlDatabase::password() const -
1095{ -
1096 return d->pword;
executed: return d->pword;
Execution Count:2
2
1097} -
1098 -
1099/*! -
1100 Returns the connection's host name; it may be empty. -
1101 -
1102 \sa setHostName() -
1103*/ -
1104QString QSqlDatabase::hostName() const -
1105{ -
1106 return d->hname;
executed: return d->hname;
Execution Count:11429
11429
1107} -
1108 -
1109/*! -
1110 Returns the connection's driver name. -
1111 -
1112 \sa addDatabase(), driver() -
1113*/ -
1114QString QSqlDatabase::driverName() const -
1115{ -
1116 return d->drvName;
executed: return d->drvName;
Execution Count:34794
34794
1117} -
1118 -
1119/*! -
1120 Returns the connection's port number. The value is undefined if -
1121 the port number has not been set. -
1122 -
1123 \sa setPort() -
1124*/ -
1125int QSqlDatabase::port() const -
1126{ -
1127 return d->port;
executed: return d->port;
Execution Count:11403
11403
1128} -
1129 -
1130/*! -
1131 Returns the database driver used to access the database -
1132 connection. -
1133 -
1134 \sa addDatabase(), drivers() -
1135*/ -
1136 -
1137QSqlDriver* QSqlDatabase::driver() const -
1138{ -
1139 return d->driver;
executed: return d->driver;
Execution Count:3533
3533
1140} -
1141 -
1142/*! -
1143 Returns information about the last error that occurred on the -
1144 database. -
1145 -
1146 Failures that occur in conjunction with an individual query are -
1147 reported by QSqlQuery::lastError(). -
1148 -
1149 \sa QSqlError, QSqlQuery::lastError() -
1150*/ -
1151 -
1152QSqlError QSqlDatabase::lastError() const -
1153{ -
1154 return d->driver->lastError();
executed: return d->driver->lastError();
Execution Count:29
29
1155} -
1156 -
1157 -
1158/*! -
1159 Returns a list of the database's tables, system tables and views, -
1160 as specified by the parameter \a type. -
1161 -
1162 \sa primaryIndex(), record() -
1163*/ -
1164 -
1165QStringList QSqlDatabase::tables(QSql::TableType type) const -
1166{ -
1167 return d->driver->tables(type);
executed: return d->driver->tables(type);
Execution Count:50
50
1168} -
1169 -
1170/*! -
1171 Returns the primary index for table \a tablename. If no primary -
1172 index exists an empty QSqlIndex is returned. -
1173 -
1174 \sa tables(), record() -
1175*/ -
1176 -
1177QSqlIndex QSqlDatabase::primaryIndex(const QString& tablename) const -
1178{ -
1179 return d->driver->primaryIndex(tablename);
executed: return d->driver->primaryIndex(tablename);
Execution Count:180
180
1180} -
1181 -
1182 -
1183/*! -
1184 Returns a QSqlRecord populated with the names of all the fields in -
1185 the table (or view) called \a tablename. The order in which the -
1186 fields appear in the record is undefined. If no such table (or -
1187 view) exists, an empty record is returned. -
1188*/ -
1189 -
1190QSqlRecord QSqlDatabase::record(const QString& tablename) const -
1191{ -
1192 return d->driver->record(tablename);
executed: return d->driver->record(tablename);
Execution Count:318
318
1193} -
1194 -
1195 -
1196/*! -
1197 Sets database-specific \a options. This must be done before the -
1198 connection is opened or it has no effect (or you can close() the -
1199 connection, call this function and open() the connection again). -
1200 -
1201 The format of the \a options string is a semicolon separated list -
1202 of option names or option=value pairs. The options depend on the -
1203 database client used: -
1204 -
1205 \table -
1206 \header \li ODBC \li MySQL \li PostgreSQL -
1207 \row -
1208 -
1209 \li -
1210 \list -
1211 \li SQL_ATTR_ACCESS_MODE -
1212 \li SQL_ATTR_LOGIN_TIMEOUT -
1213 \li SQL_ATTR_CONNECTION_TIMEOUT -
1214 \li SQL_ATTR_CURRENT_CATALOG -
1215 \li SQL_ATTR_METADATA_ID -
1216 \li SQL_ATTR_PACKET_SIZE -
1217 \li SQL_ATTR_TRACEFILE -
1218 \li SQL_ATTR_TRACE -
1219 \li SQL_ATTR_CONNECTION_POOLING -
1220 \li SQL_ATTR_ODBC_VERSION -
1221 \endlist -
1222 -
1223 \li -
1224 \list -
1225 \li CLIENT_COMPRESS -
1226 \li CLIENT_FOUND_ROWS -
1227 \li CLIENT_IGNORE_SPACE -
1228 \li CLIENT_SSL -
1229 \li CLIENT_ODBC -
1230 \li CLIENT_NO_SCHEMA -
1231 \li CLIENT_INTERACTIVE -
1232 \li UNIX_SOCKET -
1233 \li MYSQL_OPT_RECONNECT -
1234 \endlist -
1235 -
1236 \li -
1237 \list -
1238 \li connect_timeout -
1239 \li options -
1240 \li tty -
1241 \li requiressl -
1242 \li service -
1243 \endlist -
1244 -
1245 \header \li DB2 \li OCI \li TDS -
1246 \row -
1247 -
1248 \li -
1249 \list -
1250 \li SQL_ATTR_ACCESS_MODE -
1251 \li SQL_ATTR_LOGIN_TIMEOUT -
1252 \endlist -
1253 -
1254 \li -
1255 \list -
1256 \li OCI_ATTR_PREFETCH_ROWS -
1257 \li OCI_ATTR_PREFETCH_MEMORY -
1258 \endlist -
1259 -
1260 \li -
1261 \e none -
1262 -
1263 \header \li SQLite \li Interbase -
1264 \row -
1265 -
1266 \li -
1267 \list -
1268 \li QSQLITE_BUSY_TIMEOUT -
1269 \li QSQLITE_OPEN_READONLY -
1270 \li QSQLITE_ENABLE_SHARED_CACHE -
1271 \endlist -
1272 -
1273 \li -
1274 \list -
1275 \li ISC_DPB_LC_CTYPE -
1276 \li ISC_DPB_SQL_ROLE_NAME -
1277 \endlist -
1278 -
1279 \endtable -
1280 -
1281 Examples: -
1282 \snippet code/src_sql_kernel_qsqldatabase.cpp 4 -
1283 -
1284 Refer to the client library documentation for more information -
1285 about the different options. -
1286 -
1287 \sa connectOptions() -
1288*/ -
1289 -
1290void QSqlDatabase::setConnectOptions(const QString &options) -
1291{ -
1292 if (isValid())
partially evaluated: isValid()
TRUEFALSE
yes
Evaluation Count:22
no
Evaluation Count:0
0-22
1293 d->connOptions = options;
executed: d->connOptions = options;
Execution Count:22
22
1294}
executed: }
Execution Count:22
22
1295 -
1296/*! -
1297 Returns the connection options string used for this connection. -
1298 The string may be empty. -
1299 -
1300 \sa setConnectOptions() -
1301 */ -
1302QString QSqlDatabase::connectOptions() const -
1303{ -
1304 return d->connOptions;
never executed: return d->connOptions;
0
1305} -
1306 -
1307/*! -
1308 Returns true if a driver called \a name is available; otherwise -
1309 returns false. -
1310 -
1311 \sa drivers() -
1312*/ -
1313 -
1314bool QSqlDatabase::isDriverAvailable(const QString& name) -
1315{ -
1316 return drivers().contains(name);
never executed: return drivers().contains(name);
0
1317} -
1318 -
1319/*! \fn QSqlDatabase QSqlDatabase::addDatabase(QSqlDriver* driver, const QString& connectionName) -
1320 -
1321 This overload is useful when you want to create a database -
1322 connection with a \l{QSqlDriver} {driver} you instantiated -
1323 yourself. It might be your own database driver, or you might just -
1324 need to instantiate one of the Qt drivers yourself. If you do -
1325 this, it is recommended that you include the driver code in your -
1326 application. For example, you can create a PostgreSQL connection -
1327 with your own QPSQL driver like this: -
1328 -
1329 \snippet code/src_sql_kernel_qsqldatabase.cpp 5 -
1330 \codeline -
1331 \snippet code/src_sql_kernel_qsqldatabase.cpp 6 -
1332 -
1333 The above code sets up a PostgreSQL connection and instantiates a -
1334 QPSQLDriver object. Next, addDatabase() is called to add the -
1335 connection to the known connections so that it can be used by the -
1336 Qt SQL classes. When a driver is instantiated with a connection -
1337 handle (or set of handles), Qt assumes that you have already -
1338 opened the database connection. -
1339 -
1340 \note We assume that \c qtdir is the directory where Qt is -
1341 installed. This will pull in the code that is needed to use the -
1342 PostgreSQL client library and to instantiate a QPSQLDriver object, -
1343 assuming that you have the PostgreSQL headers somewhere in your -
1344 include search path. -
1345 -
1346 Remember that you must link your application against the database -
1347 client library. Make sure the client library is in your linker's -
1348 search path, and add lines like these to your \c{.pro} file: -
1349 -
1350 \snippet code/src_sql_kernel_qsqldatabase.cpp 7 -
1351 -
1352 The method described works for all the supplied drivers. The only -
1353 difference will be in the driver constructor arguments. Here is a -
1354 table of the drivers included with Qt, their source code files, -
1355 and their constructor arguments: -
1356 -
1357 \table -
1358 \header \li Driver \li Class name \li Constructor arguments \li File to include -
1359 \row -
1360 \li QPSQL -
1361 \li QPSQLDriver -
1362 \li PGconn *connection -
1363 \li \c qsql_psql.cpp -
1364 \row -
1365 \li QMYSQL -
1366 \li QMYSQLDriver -
1367 \li MYSQL *connection -
1368 \li \c qsql_mysql.cpp -
1369 \row -
1370 \li QOCI -
1371 \li QOCIDriver -
1372 \li OCIEnv *environment, OCISvcCtx *serviceContext -
1373 \li \c qsql_oci.cpp -
1374 \row -
1375 \li QODBC -
1376 \li QODBCDriver -
1377 \li SQLHANDLE environment, SQLHANDLE connection -
1378 \li \c qsql_odbc.cpp -
1379 \row -
1380 \li QDB2 -
1381 \li QDB2 -
1382 \li SQLHANDLE environment, SQLHANDLE connection -
1383 \li \c qsql_db2.cpp -
1384 \row -
1385 \li QTDS -
1386 \li QTDSDriver -
1387 \li LOGINREC *loginRecord, DBPROCESS *dbProcess, const QString &hostName -
1388 \li \c qsql_tds.cpp -
1389 \row -
1390 \li QSQLITE -
1391 \li QSQLiteDriver -
1392 \li sqlite *connection -
1393 \li \c qsql_sqlite.cpp -
1394 \row -
1395 \li QIBASE -
1396 \li QIBaseDriver -
1397 \li isc_db_handle connection -
1398 \li \c qsql_ibase.cpp -
1399 \endtable -
1400 -
1401 The host name (or service name) is needed when constructing the -
1402 QTDSDriver for creating new connections for internal queries. This -
1403 is to prevent blocking when several QSqlQuery objects are used -
1404 simultaneously. -
1405 -
1406 \warning Adding a database connection with the same connection -
1407 name as an existing connection, causes the existing connection to -
1408 be replaced by the new one. -
1409 -
1410 \warning The SQL framework takes ownership of the \a driver. It -
1411 must not be deleted. To remove the connection, use -
1412 removeDatabase(). -
1413 -
1414 \sa drivers() -
1415*/ -
1416QSqlDatabase QSqlDatabase::addDatabase(QSqlDriver* driver, const QString& connectionName) -
1417{ -
1418 QSqlDatabase db(driver);
never executed (the execution status of this line is deduced): QSqlDatabase db(driver);
-
1419 QSqlDatabasePrivate::addDatabase(db, connectionName);
never executed (the execution status of this line is deduced): QSqlDatabasePrivate::addDatabase(db, connectionName);
-
1420 return db;
never executed: return db;
0
1421} -
1422 -
1423/*! -
1424 Returns true if the QSqlDatabase has a valid driver. -
1425 -
1426 Example: -
1427 \snippet code/src_sql_kernel_qsqldatabase.cpp 8 -
1428*/ -
1429bool QSqlDatabase::isValid() const -
1430{ -
1431 return d->driver && d->driver != d->shared_null()->driver;
executed: return d->driver && d->driver != d->shared_null()->driver;
Execution Count:6594
6594
1432} -
1433 -
1434/*! -
1435 Clones the database connection \a other and stores it as \a -
1436 connectionName. All the settings from the original database, e.g. -
1437 databaseName(), hostName(), etc., are copied across. Does nothing -
1438 if \a other is an invalid database. Returns the newly created -
1439 database connection. -
1440 -
1441 \note The new connection has not been opened. Before using the new -
1442 connection, you must call open(). -
1443*/ -
1444QSqlDatabase QSqlDatabase::cloneDatabase(const QSqlDatabase &other, const QString &connectionName) -
1445{ -
1446 if (!other.isValid())
partially evaluated: !other.isValid()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:7
0-7
1447 return QSqlDatabase();
never executed: return QSqlDatabase();
0
1448 -
1449 QSqlDatabase db(other.driverName());
executed (the execution status of this line is deduced): QSqlDatabase db(other.driverName());
-
1450 db.d->copy(other.d);
executed (the execution status of this line is deduced): db.d->copy(other.d);
-
1451 QSqlDatabasePrivate::addDatabase(db, connectionName);
executed (the execution status of this line is deduced): QSqlDatabasePrivate::addDatabase(db, connectionName);
-
1452 return db;
executed: return db;
Execution Count:7
7
1453} -
1454 -
1455/*! -
1456 \since 4.4 -
1457 -
1458 Returns the connection name, which may be empty. \note The -
1459 connection name is not the \l{databaseName()} {database name}. -
1460 -
1461 \sa addDatabase() -
1462*/ -
1463QString QSqlDatabase::connectionName() const -
1464{ -
1465 return d->connName;
executed: return d->connName;
Execution Count:4
4
1466} -
1467 -
1468/*! -
1469 \since 4.6 -
1470 -
1471 Sets the default numerical precision policy used by queries created -
1472 on this database connection to \a precisionPolicy. -
1473 -
1474 Note: Drivers that don't support fetching numerical values with low -
1475 precision will ignore the precision policy. You can use -
1476 QSqlDriver::hasFeature() to find out whether a driver supports this -
1477 feature. -
1478 -
1479 Note: Setting the default precision policy to \a precisionPolicy -
1480 doesn't affect any currently active queries. -
1481 -
1482 \sa QSql::NumericalPrecisionPolicy, numericalPrecisionPolicy(), -
1483 QSqlQuery::setNumericalPrecisionPolicy(), QSqlQuery::numericalPrecisionPolicy() -
1484*/ -
1485void QSqlDatabase::setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy) -
1486{ -
1487 if(driver())
partially evaluated: driver()
TRUEFALSE
yes
Evaluation Count:2
no
Evaluation Count:0
0-2
1488 driver()->setNumericalPrecisionPolicy(precisionPolicy);
executed: driver()->setNumericalPrecisionPolicy(precisionPolicy);
Execution Count:2
2
1489 d->precisionPolicy = precisionPolicy;
executed (the execution status of this line is deduced): d->precisionPolicy = precisionPolicy;
-
1490}
executed: }
Execution Count:2
2
1491 -
1492/*! -
1493 \since 4.6 -
1494 -
1495 Returns the current default precision policy for the database connection. -
1496 -
1497 \sa QSql::NumericalPrecisionPolicy, setNumericalPrecisionPolicy(), -
1498 QSqlQuery::numericalPrecisionPolicy(), QSqlQuery::setNumericalPrecisionPolicy() -
1499*/ -
1500QSql::NumericalPrecisionPolicy QSqlDatabase::numericalPrecisionPolicy() const -
1501{ -
1502 if(driver())
partially evaluated: driver()
TRUEFALSE
yes
Evaluation Count:1
no
Evaluation Count:0
0-1
1503 return driver()->numericalPrecisionPolicy();
executed: return driver()->numericalPrecisionPolicy();
Execution Count:1
1
1504 else -
1505 return d->precisionPolicy;
never executed: return d->precisionPolicy;
0
1506} -
1507 -
1508 -
1509#ifndef QT_NO_DEBUG_STREAM -
1510QDebug operator<<(QDebug dbg, const QSqlDatabase &d) -
1511{ -
1512 if (!d.isValid()) {
never evaluated: !d.isValid()
0
1513 dbg.nospace() << "QSqlDatabase(invalid)";
never executed (the execution status of this line is deduced): dbg.nospace() << "QSqlDatabase(invalid)";
-
1514 return dbg.space();
never executed: return dbg.space();
0
1515 } -
1516 -
1517 dbg.nospace() << "QSqlDatabase(driver=\"" << d.driverName() << "\", database=\""
never executed (the execution status of this line is deduced): dbg.nospace() << "QSqlDatabase(driver=\"" << d.driverName() << "\", database=\""
-
1518 << d.databaseName() << "\", host=\"" << d.hostName() << "\", port=" << d.port()
never executed (the execution status of this line is deduced): << d.databaseName() << "\", host=\"" << d.hostName() << "\", port=" << d.port()
-
1519 << ", user=\"" << d.userName() << "\", open=" << d.isOpen() << ")";
never executed (the execution status of this line is deduced): << ", user=\"" << d.userName() << "\", open=" << d.isOpen() << ")";
-
1520 return dbg.space();
never executed: return dbg.space();
0
1521} -
1522#endif -
1523 -
1524QT_END_NAMESPACE -
1525 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial