kernel/qsqldatabase.cpp

Source codeSwitch to Preprocessed file
LineSource CodeCoverage
1/**************************************************************************** -
2** -
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies). -
4** Contact: http://www.qt-project.org/legal -
5** -
6** This file is part of the QtSql module of the Qt Toolkit. -
7** -
8** $QT_BEGIN_LICENSE:LGPL$ -
9** Commercial License Usage -
10** Licensees holding valid commercial Qt licenses may use this file in -
11** accordance with the commercial license agreement provided with the -
12** Software or, alternatively, in accordance with the terms contained in -
13** a written agreement between you and Digia. For licensing terms and -
14** conditions see http://qt.digia.com/licensing. For further information -
15** use the contact form at http://qt.digia.com/contact-us. -
16** -
17** GNU Lesser General Public License Usage -
18** Alternatively, this file may be used under the terms of the GNU Lesser -
19** General Public License version 2.1 as published by the Free Software -
20** Foundation and appearing in the file LICENSE.LGPL included in the -
21** packaging of this file. Please review the following information to -
22** ensure the GNU Lesser General Public License version 2.1 requirements -
23** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. -
24** -
25** In addition, as a special exception, Digia gives you certain additional -
26** rights. These rights are described in the Digia Qt LGPL Exception -
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. -
28** -
29** GNU General Public License Usage -
30** Alternatively, this file may be used under the terms of the GNU -
31** General Public License version 3.0 as published by the Free Software -
32** Foundation and appearing in the file LICENSE.GPL included in the -
33** packaging of this file. Please review the following information to -
34** ensure the GNU General Public License version 3.0 requirements will be -
35** met: http://www.gnu.org/copyleft/gpl.html. -
36** -
37** -
38** $QT_END_LICENSE$ -
39** -
40****************************************************************************/ -
41 -
42#include "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:2309
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:2300
partially evaluated: !thisGlobalStatic.destroyed
TRUEFALSE
yes
Evaluation Count:9
no
Evaluation Count:0
0-2309
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:8087
8087
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:1957
yes
Evaluation Count:262
evaluated: !db.isOpen()
TRUEFALSE
yes
Evaluation Count:25
yes
Evaluation Count:1932
evaluated: open
TRUEFALSE
yes
Evaluation Count:5
yes
Evaluation Count:20
5-1957
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:2219
2219
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:2219
2219
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:1281
1281
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:18205
18205
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:969
969
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 \sa close() -
798*/ -
799 -
800QSqlDatabase::~QSqlDatabase() -
801{ -
802 if (!d->ref.deref()) {
evaluated: !d->ref.deref()
TRUEFALSE
yes
Evaluation Count:36
yes
Evaluation Count:19486
36-19486
803 close();
executed (the execution status of this line is deduced): close();
-
804 delete d;
executed (the execution status of this line is deduced): delete d;
-
805 }
executed: }
Execution Count:36
36
806}
executed: }
Execution Count:19522
19522
807 -
808/*! -
809 Executes a SQL statement on the database and returns a QSqlQuery -
810 object. Use lastError() to retrieve error information. If \a -
811 query is empty, an empty, invalid query is returned and -
812 lastError() is not affected. -
813 -
814 \sa QSqlQuery, lastError() -
815*/ -
816 -
817QSqlQuery QSqlDatabase::exec(const QString & query) const -
818{ -
819 QSqlQuery r(d->driver->createResult());
executed (the execution status of this line is deduced): QSqlQuery r(d->driver->createResult());
-
820 if (!query.isEmpty()) {
partially evaluated: !query.isEmpty()
TRUEFALSE
yes
Evaluation Count:16
no
Evaluation Count:0
0-16
821 r.exec(query);
executed (the execution status of this line is deduced): r.exec(query);
-
822 d->driver->setLastError(r.lastError());
executed (the execution status of this line is deduced): d->driver->setLastError(r.lastError());
-
823 }
executed: }
Execution Count:16
16
824 return r;
executed: return r;
Execution Count:16
16
825} -
826 -
827/*! -
828 Opens the database connection using the current connection -
829 values. Returns true on success; otherwise returns false. Error -
830 information can be retrieved using lastError(). -
831 -
832 \sa lastError(), setDatabaseName(), setUserName(), setPassword(), -
833 setHostName(), setPort(), setConnectOptions() -
834*/ -
835 -
836bool QSqlDatabase::open() -
837{ -
838 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
839 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
840} -
841 -
842/*! -
843 \overload -
844 -
845 Opens the database connection using the given \a user name and \a -
846 password. Returns true on success; otherwise returns false. Error -
847 information can be retrieved using the lastError() function. -
848 -
849 This function does not store the password it is given. Instead, -
850 the password is passed directly to the driver for opening the -
851 connection and it is then discarded. -
852 -
853 \sa lastError() -
854*/ -
855 -
856bool QSqlDatabase::open(const QString& user, const QString& password) -
857{ -
858 setUserName(user);
executed (the execution status of this line is deduced): setUserName(user);
-
859 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
860 d->port, d->connOptions);
executed: return d->driver->open(d->dbname, user, password, d->hname, d->port, d->connOptions);
Execution Count:1
1
861} -
862 -
863/*! -
864 Closes the database connection, freeing any resources acquired, and -
865 invalidating any existing QSqlQuery objects that are used with the -
866 database. -
867 -
868 This will also affect copies of this QSqlDatabase object. -
869 -
870 \sa removeDatabase() -
871*/ -
872 -
873void QSqlDatabase::close() -
874{ -
875 d->driver->close();
executed (the execution status of this line is deduced): d->driver->close();
-
876}
executed: }
Execution Count:76
76
877 -
878/*! -
879 Returns true if the database connection is currently open; -
880 otherwise returns false. -
881*/ -
882 -
883bool QSqlDatabase::isOpen() const -
884{ -
885 return d->driver->isOpen();
executed: return d->driver->isOpen();
Execution Count:2016
2016
886} -
887 -
888/*! -
889 Returns true if there was an error opening the database -
890 connection; otherwise returns false. Error information can be -
891 retrieved using the lastError() function. -
892*/ -
893 -
894bool QSqlDatabase::isOpenError() const -
895{ -
896 return d->driver->isOpenError();
executed: return d->driver->isOpenError();
Execution Count:10
10
897} -
898 -
899/*! -
900 Begins a transaction on the database if the driver supports -
901 transactions. Returns \c{true} if the operation succeeded. -
902 Otherwise it returns \c{false}. -
903 -
904 \sa QSqlDriver::hasFeature(), commit(), rollback() -
905*/ -
906bool QSqlDatabase::transaction() -
907{ -
908 if (!d->driver->hasFeature(QSqlDriver::Transactions))
partially evaluated: !d->driver->hasFeature(QSqlDriver::Transactions)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:5
0-5
909 return false;
never executed: return false;
0
910 return d->driver->beginTransaction();
executed: return d->driver->beginTransaction();
Execution Count:5
5
911} -
912 -
913/*! -
914 Commits a transaction to the database if the driver supports -
915 transactions and a transaction() has been started. Returns \c{true} -
916 if the operation succeeded. Otherwise it returns \c{false}. -
917 -
918 \note For some databases, the commit will fail and return \c{false} -
919 if there is an \l{QSqlQuery::isActive()} {active query} using the -
920 database for a \c{SELECT}. Make the query \l{QSqlQuery::isActive()} -
921 {inactive} before doing the commit. -
922 -
923 Call lastError() to get information about errors. -
924 -
925 \sa QSqlQuery::isActive(), QSqlDriver::hasFeature(), rollback() -
926*/ -
927bool QSqlDatabase::commit() -
928{ -
929 if (!d->driver->hasFeature(QSqlDriver::Transactions))
partially evaluated: !d->driver->hasFeature(QSqlDriver::Transactions)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:4
0-4
930 return false;
never executed: return false;
0
931 return d->driver->commitTransaction();
executed: return d->driver->commitTransaction();
Execution Count:4
4
932} -
933 -
934/*! -
935 Rolls back a transaction on the database, if the driver supports -
936 transactions and a transaction() has been started. Returns \c{true} -
937 if the operation succeeded. Otherwise it returns \c{false}. -
938 -
939 \note For some databases, the rollback will fail and return -
940 \c{false} if there is an \l{QSqlQuery::isActive()} {active query} -
941 using the database for a \c{SELECT}. Make the query -
942 \l{QSqlQuery::isActive()} {inactive} before doing the rollback. -
943 -
944 Call lastError() to get information about errors. -
945 -
946 \sa QSqlQuery::isActive(), QSqlDriver::hasFeature(), commit() -
947*/ -
948bool QSqlDatabase::rollback() -
949{ -
950 if (!d->driver->hasFeature(QSqlDriver::Transactions))
partially evaluated: !d->driver->hasFeature(QSqlDriver::Transactions)
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:1
0-1
951 return false;
never executed: return false;
0
952 return d->driver->rollbackTransaction();
executed: return d->driver->rollbackTransaction();
Execution Count:1
1
953} -
954 -
955/*! -
956 Sets the connection's database name to \a name. To have effect, -
957 the database name must be set \e{before} the connection is -
958 \l{open()} {opened}. Alternatively, you can close() the -
959 connection, set the database name, and call open() again. \note -
960 The \e{database name} is not the \e{connection name}. The -
961 connection name must be passed to addDatabase() at connection -
962 object create time. -
963 -
964 For the QOCI (Oracle) driver, the database name is the TNS -
965 Service Name. -
966 -
967 For the QODBC driver, the \a name can either be a DSN, a DSN -
968 filename (in which case the file must have a \c .dsn extension), -
969 or a connection string. -
970 -
971 For example, Microsoft Access users can use the following -
972 connection string to open an \c .mdb file directly, instead of -
973 having to create a DSN entry in the ODBC manager: -
974 -
975 \snippet code/src_sql_kernel_qsqldatabase.cpp 3 -
976 -
977 There is no default value. -
978 -
979 \sa databaseName(), setUserName(), setPassword(), setHostName(), -
980 setPort(), setConnectOptions(), open() -
981*/ -
982 -
983void QSqlDatabase::setDatabaseName(const QString& name) -
984{ -
985 if (isValid())
partially evaluated: isValid()
TRUEFALSE
yes
Evaluation Count:26
no
Evaluation Count:0
0-26
986 d->dbname = name;
executed: d->dbname = name;
Execution Count:26
26
987}
executed: }
Execution Count:26
26
988 -
989/*! -
990 Sets the connection's user name to \a name. To have effect, the -
991 user name must be set \e{before} the connection is \l{open()} -
992 {opened}. Alternatively, you can close() the connection, set the -
993 user name, and call open() again. -
994 -
995 There is no default value. -
996 -
997 \sa userName(), setDatabaseName(), setPassword(), setHostName(), -
998 setPort(), setConnectOptions(), open() -
999*/ -
1000 -
1001void QSqlDatabase::setUserName(const QString& name) -
1002{ -
1003 if (isValid())
partially evaluated: isValid()
TRUEFALSE
yes
Evaluation Count:24
no
Evaluation Count:0
0-24
1004 d->uname = name;
executed: d->uname = name;
Execution Count:24
24
1005}
executed: }
Execution Count:24
24
1006 -
1007/*! -
1008 Sets the connection's password to \a password. To have effect, the -
1009 password must be set \e{before} the connection is \l{open()} -
1010 {opened}. Alternatively, you can close() the connection, set the -
1011 password, and call open() again. -
1012 -
1013 There is no default value. -
1014 -
1015 \warning This function stores the password in plain text within -
1016 Qt. Use the open() call that takes a password as parameter to -
1017 avoid this behavior. -
1018 -
1019 \sa password(), setUserName(), setDatabaseName(), setHostName(), -
1020 setPort(), setConnectOptions(), open() -
1021*/ -
1022 -
1023void QSqlDatabase::setPassword(const QString& password) -
1024{ -
1025 if (isValid())
partially evaluated: isValid()
TRUEFALSE
yes
Evaluation Count:23
no
Evaluation Count:0
0-23
1026 d->pword = password;
executed: d->pword = password;
Execution Count:23
23
1027}
executed: }
Execution Count:23
23
1028 -
1029/*! -
1030 Sets the connection's host name to \a host. To have effect, the -
1031 host name must be set \e{before} the connection is \l{open()} -
1032 {opened}. Alternatively, you can close() the connection, set the -
1033 host name, and call open() again. -
1034 -
1035 There is no default value. -
1036 -
1037 \sa hostName(), setUserName(), setPassword(), setDatabaseName(), -
1038 setPort(), setConnectOptions(), open() -
1039*/ -
1040 -
1041void QSqlDatabase::setHostName(const QString& host) -
1042{ -
1043 if (isValid())
partially evaluated: isValid()
TRUEFALSE
yes
Evaluation Count:23
no
Evaluation Count:0
0-23
1044 d->hname = host;
executed: d->hname = host;
Execution Count:23
23
1045}
executed: }
Execution Count:23
23
1046 -
1047/*! -
1048 Sets the connection's port number to \a port. To have effect, the -
1049 port number must be set \e{before} the connection is \l{open()} -
1050 {opened}. Alternatively, you can close() the connection, set the -
1051 port number, and call open() again.. -
1052 -
1053 There is no default value. -
1054 -
1055 \sa port(), setUserName(), setPassword(), setHostName(), -
1056 setDatabaseName(), setConnectOptions(), open() -
1057*/ -
1058 -
1059void QSqlDatabase::setPort(int port) -
1060{ -
1061 if (isValid())
partially evaluated: isValid()
TRUEFALSE
yes
Evaluation Count:21
no
Evaluation Count:0
0-21
1062 d->port = port;
executed: d->port = port;
Execution Count:21
21
1063}
executed: }
Execution Count:21
21
1064 -
1065/*! -
1066 Returns the connection's database name, which may be empty. -
1067 \note The database name is not the connection name. -
1068 -
1069 \sa setDatabaseName() -
1070*/ -
1071QString QSqlDatabase::databaseName() const -
1072{ -
1073 return d->dbname;
executed: return d->dbname;
Execution Count:127
127
1074} -
1075 -
1076/*! -
1077 Returns the connection's user name; it may be empty. -
1078 -
1079 \sa setUserName() -
1080*/ -
1081QString QSqlDatabase::userName() const -
1082{ -
1083 return d->uname;
executed: return d->uname;
Execution Count:2
2
1084} -
1085 -
1086/*! -
1087 Returns the connection's password. If the password was not set -
1088 with setPassword(), and if the password was given in the open() -
1089 call, or if no password was used, an empty string is returned. -
1090*/ -
1091QString QSqlDatabase::password() const -
1092{ -
1093 return d->pword;
executed: return d->pword;
Execution Count:2
2
1094} -
1095 -
1096/*! -
1097 Returns the connection's host name; it may be empty. -
1098 -
1099 \sa setHostName() -
1100*/ -
1101QString QSqlDatabase::hostName() const -
1102{ -
1103 return d->hname;
executed: return d->hname;
Execution Count:11573
11573
1104} -
1105 -
1106/*! -
1107 Returns the connection's driver name. -
1108 -
1109 \sa addDatabase(), driver() -
1110*/ -
1111QString QSqlDatabase::driverName() const -
1112{ -
1113 return d->drvName;
executed: return d->drvName;
Execution Count:35246
35246
1114} -
1115 -
1116/*! -
1117 Returns the connection's port number. The value is undefined if -
1118 the port number has not been set. -
1119 -
1120 \sa setPort() -
1121*/ -
1122int QSqlDatabase::port() const -
1123{ -
1124 return d->port;
executed: return d->port;
Execution Count:11547
11547
1125} -
1126 -
1127/*! -
1128 Returns the database driver used to access the database -
1129 connection. -
1130 -
1131 \sa addDatabase(), drivers() -
1132*/ -
1133 -
1134QSqlDriver* QSqlDatabase::driver() const -
1135{ -
1136 return d->driver;
executed: return d->driver;
Execution Count:3707
3707
1137} -
1138 -
1139/*! -
1140 Returns information about the last error that occurred on the -
1141 database. -
1142 -
1143 Failures that occur in conjunction with an individual query are -
1144 reported by QSqlQuery::lastError(). -
1145 -
1146 \sa QSqlError, QSqlQuery::lastError() -
1147*/ -
1148 -
1149QSqlError QSqlDatabase::lastError() const -
1150{ -
1151 return d->driver->lastError();
executed: return d->driver->lastError();
Execution Count:29
29
1152} -
1153 -
1154 -
1155/*! -
1156 Returns a list of the database's tables, system tables and views, -
1157 as specified by the parameter \a type. -
1158 -
1159 \sa primaryIndex(), record() -
1160*/ -
1161 -
1162QStringList QSqlDatabase::tables(QSql::TableType type) const -
1163{ -
1164 return d->driver->tables(type);
executed: return d->driver->tables(type);
Execution Count:50
50
1165} -
1166 -
1167/*! -
1168 Returns the primary index for table \a tablename. If no primary -
1169 index exists an empty QSqlIndex is returned. -
1170 -
1171 \sa tables(), record() -
1172*/ -
1173 -
1174QSqlIndex QSqlDatabase::primaryIndex(const QString& tablename) const -
1175{ -
1176 return d->driver->primaryIndex(tablename);
executed: return d->driver->primaryIndex(tablename);
Execution Count:187
187
1177} -
1178 -
1179 -
1180/*! -
1181 Returns a QSqlRecord populated with the names of all the fields in -
1182 the table (or view) called \a tablename. The order in which the -
1183 fields appear in the record is undefined. If no such table (or -
1184 view) exists, an empty record is returned. -
1185*/ -
1186 -
1187QSqlRecord QSqlDatabase::record(const QString& tablename) const -
1188{ -
1189 return d->driver->record(tablename);
executed: return d->driver->record(tablename);
Execution Count:328
328
1190} -
1191 -
1192 -
1193/*! -
1194 Sets database-specific \a options. This must be done before the -
1195 connection is opened or it has no effect (or you can close() the -
1196 connection, call this function and open() the connection again). -
1197 -
1198 The format of the \a options string is a semicolon separated list -
1199 of option names or option=value pairs. The options depend on the -
1200 database client used: -
1201 -
1202 \table -
1203 \header \li ODBC \li MySQL \li PostgreSQL -
1204 \row -
1205 -
1206 \li -
1207 \list -
1208 \li SQL_ATTR_ACCESS_MODE -
1209 \li SQL_ATTR_LOGIN_TIMEOUT -
1210 \li SQL_ATTR_CONNECTION_TIMEOUT -
1211 \li SQL_ATTR_CURRENT_CATALOG -
1212 \li SQL_ATTR_METADATA_ID -
1213 \li SQL_ATTR_PACKET_SIZE -
1214 \li SQL_ATTR_TRACEFILE -
1215 \li SQL_ATTR_TRACE -
1216 \li SQL_ATTR_CONNECTION_POOLING -
1217 \li SQL_ATTR_ODBC_VERSION -
1218 \endlist -
1219 -
1220 \li -
1221 \list -
1222 \li CLIENT_COMPRESS -
1223 \li CLIENT_FOUND_ROWS -
1224 \li CLIENT_IGNORE_SPACE -
1225 \li CLIENT_SSL -
1226 \li CLIENT_ODBC -
1227 \li CLIENT_NO_SCHEMA -
1228 \li CLIENT_INTERACTIVE -
1229 \li UNIX_SOCKET -
1230 \li MYSQL_OPT_RECONNECT -
1231 \endlist -
1232 -
1233 \li -
1234 \list -
1235 \li connect_timeout -
1236 \li options -
1237 \li tty -
1238 \li requiressl -
1239 \li service -
1240 \endlist -
1241 -
1242 \header \li DB2 \li OCI \li TDS -
1243 \row -
1244 -
1245 \li -
1246 \list -
1247 \li SQL_ATTR_ACCESS_MODE -
1248 \li SQL_ATTR_LOGIN_TIMEOUT -
1249 \endlist -
1250 -
1251 \li -
1252 \list -
1253 \li OCI_ATTR_PREFETCH_ROWS -
1254 \li OCI_ATTR_PREFETCH_MEMORY -
1255 \endlist -
1256 -
1257 \li -
1258 \e none -
1259 -
1260 \header \li SQLite \li Interbase -
1261 \row -
1262 -
1263 \li -
1264 \list -
1265 \li QSQLITE_BUSY_TIMEOUT -
1266 \li QSQLITE_OPEN_READONLY -
1267 \li QSQLITE_ENABLE_SHARED_CACHE -
1268 \endlist -
1269 -
1270 \li -
1271 \list -
1272 \li ISC_DPB_LC_CTYPE -
1273 \li ISC_DPB_SQL_ROLE_NAME -
1274 \endlist -
1275 -
1276 \endtable -
1277 -
1278 Examples: -
1279 \snippet code/src_sql_kernel_qsqldatabase.cpp 4 -
1280 -
1281 Refer to the client library documentation for more information -
1282 about the different options. -
1283 -
1284 \sa connectOptions() -
1285*/ -
1286 -
1287void QSqlDatabase::setConnectOptions(const QString &options) -
1288{ -
1289 if (isValid())
partially evaluated: isValid()
TRUEFALSE
yes
Evaluation Count:22
no
Evaluation Count:0
0-22
1290 d->connOptions = options;
executed: d->connOptions = options;
Execution Count:22
22
1291}
executed: }
Execution Count:22
22
1292 -
1293/*! -
1294 Returns the connection options string used for this connection. -
1295 The string may be empty. -
1296 -
1297 \sa setConnectOptions() -
1298 */ -
1299QString QSqlDatabase::connectOptions() const -
1300{ -
1301 return d->connOptions;
never executed: return d->connOptions;
0
1302} -
1303 -
1304/*! -
1305 Returns true if a driver called \a name is available; otherwise -
1306 returns false. -
1307 -
1308 \sa drivers() -
1309*/ -
1310 -
1311bool QSqlDatabase::isDriverAvailable(const QString& name) -
1312{ -
1313 return drivers().contains(name);
never executed: return drivers().contains(name);
0
1314} -
1315 -
1316/*! \fn QSqlDatabase QSqlDatabase::addDatabase(QSqlDriver* driver, const QString& connectionName) -
1317 -
1318 This overload is useful when you want to create a database -
1319 connection with a \l{QSqlDriver} {driver} you instantiated -
1320 yourself. It might be your own database driver, or you might just -
1321 need to instantiate one of the Qt drivers yourself. If you do -
1322 this, it is recommended that you include the driver code in your -
1323 application. For example, you can create a PostgreSQL connection -
1324 with your own QPSQL driver like this: -
1325 -
1326 \snippet code/src_sql_kernel_qsqldatabase.cpp 5 -
1327 \codeline -
1328 \snippet code/src_sql_kernel_qsqldatabase.cpp 6 -
1329 -
1330 The above code sets up a PostgreSQL connection and instantiates a -
1331 QPSQLDriver object. Next, addDatabase() is called to add the -
1332 connection to the known connections so that it can be used by the -
1333 Qt SQL classes. When a driver is instantiated with a connection -
1334 handle (or set of handles), Qt assumes that you have already -
1335 opened the database connection. -
1336 -
1337 \note We assume that \c qtdir is the directory where Qt is -
1338 installed. This will pull in the code that is needed to use the -
1339 PostgreSQL client library and to instantiate a QPSQLDriver object, -
1340 assuming that you have the PostgreSQL headers somewhere in your -
1341 include search path. -
1342 -
1343 Remember that you must link your application against the database -
1344 client library. Make sure the client library is in your linker's -
1345 search path, and add lines like these to your \c{.pro} file: -
1346 -
1347 \snippet code/src_sql_kernel_qsqldatabase.cpp 7 -
1348 -
1349 The method described works for all the supplied drivers. The only -
1350 difference will be in the driver constructor arguments. Here is a -
1351 table of the drivers included with Qt, their source code files, -
1352 and their constructor arguments: -
1353 -
1354 \table -
1355 \header \li Driver \li Class name \li Constructor arguments \li File to include -
1356 \row -
1357 \li QPSQL -
1358 \li QPSQLDriver -
1359 \li PGconn *connection -
1360 \li \c qsql_psql.cpp -
1361 \row -
1362 \li QMYSQL -
1363 \li QMYSQLDriver -
1364 \li MYSQL *connection -
1365 \li \c qsql_mysql.cpp -
1366 \row -
1367 \li QOCI -
1368 \li QOCIDriver -
1369 \li OCIEnv *environment, OCISvcCtx *serviceContext -
1370 \li \c qsql_oci.cpp -
1371 \row -
1372 \li QODBC -
1373 \li QODBCDriver -
1374 \li SQLHANDLE environment, SQLHANDLE connection -
1375 \li \c qsql_odbc.cpp -
1376 \row -
1377 \li QDB2 -
1378 \li QDB2 -
1379 \li SQLHANDLE environment, SQLHANDLE connection -
1380 \li \c qsql_db2.cpp -
1381 \row -
1382 \li QTDS -
1383 \li QTDSDriver -
1384 \li LOGINREC *loginRecord, DBPROCESS *dbProcess, const QString &hostName -
1385 \li \c qsql_tds.cpp -
1386 \row -
1387 \li QSQLITE -
1388 \li QSQLiteDriver -
1389 \li sqlite *connection -
1390 \li \c qsql_sqlite.cpp -
1391 \row -
1392 \li QIBASE -
1393 \li QIBaseDriver -
1394 \li isc_db_handle connection -
1395 \li \c qsql_ibase.cpp -
1396 \endtable -
1397 -
1398 The host name (or service name) is needed when constructing the -
1399 QTDSDriver for creating new connections for internal queries. This -
1400 is to prevent blocking when several QSqlQuery objects are used -
1401 simultaneously. -
1402 -
1403 \warning Adding a database connection with the same connection -
1404 name as an existing connection, causes the existing connection to -
1405 be replaced by the new one. -
1406 -
1407 \warning The SQL framework takes ownership of the \a driver. It -
1408 must not be deleted. To remove the connection, use -
1409 removeDatabase(). -
1410 -
1411 \sa drivers() -
1412*/ -
1413QSqlDatabase QSqlDatabase::addDatabase(QSqlDriver* driver, const QString& connectionName) -
1414{ -
1415 QSqlDatabase db(driver);
never executed (the execution status of this line is deduced): QSqlDatabase db(driver);
-
1416 QSqlDatabasePrivate::addDatabase(db, connectionName);
never executed (the execution status of this line is deduced): QSqlDatabasePrivate::addDatabase(db, connectionName);
-
1417 return db;
never executed: return db;
0
1418} -
1419 -
1420/*! -
1421 Returns true if the QSqlDatabase has a valid driver. -
1422 -
1423 Example: -
1424 \snippet code/src_sql_kernel_qsqldatabase.cpp 8 -
1425*/ -
1426bool QSqlDatabase::isValid() const -
1427{ -
1428 return d->driver && d->driver != d->shared_null()->driver;
executed: return d->driver && d->driver != d->shared_null()->driver;
Execution Count:6756
6756
1429} -
1430 -
1431/*! -
1432 Clones the database connection \a other and stores it as \a -
1433 connectionName. All the settings from the original database, e.g. -
1434 databaseName(), hostName(), etc., are copied across. Does nothing -
1435 if \a other is an invalid database. Returns the newly created -
1436 database connection. -
1437 -
1438 \note The new connection has not been opened. Before using the new -
1439 connection, you must call open(). -
1440*/ -
1441QSqlDatabase QSqlDatabase::cloneDatabase(const QSqlDatabase &other, const QString &connectionName) -
1442{ -
1443 if (!other.isValid())
partially evaluated: !other.isValid()
TRUEFALSE
no
Evaluation Count:0
yes
Evaluation Count:7
0-7
1444 return QSqlDatabase();
never executed: return QSqlDatabase();
0
1445 -
1446 QSqlDatabase db(other.driverName());
executed (the execution status of this line is deduced): QSqlDatabase db(other.driverName());
-
1447 db.d->copy(other.d);
executed (the execution status of this line is deduced): db.d->copy(other.d);
-
1448 QSqlDatabasePrivate::addDatabase(db, connectionName);
executed (the execution status of this line is deduced): QSqlDatabasePrivate::addDatabase(db, connectionName);
-
1449 return db;
executed: return db;
Execution Count:7
7
1450} -
1451 -
1452/*! -
1453 \since 4.4 -
1454 -
1455 Returns the connection name, which may be empty. \note The -
1456 connection name is not the \l{databaseName()} {database name}. -
1457 -
1458 \sa addDatabase() -
1459*/ -
1460QString QSqlDatabase::connectionName() const -
1461{ -
1462 return d->connName;
executed: return d->connName;
Execution Count:4
4
1463} -
1464 -
1465/*! -
1466 \since 4.6 -
1467 -
1468 Sets the default numerical precision policy used by queries created -
1469 on this database connection to \a precisionPolicy. -
1470 -
1471 Note: Drivers that don't support fetching numerical values with low -
1472 precision will ignore the precision policy. You can use -
1473 QSqlDriver::hasFeature() to find out whether a driver supports this -
1474 feature. -
1475 -
1476 Note: Setting the default precision policy to \a precisionPolicy -
1477 doesn't affect any currently active queries. -
1478 -
1479 \sa QSql::NumericalPrecisionPolicy, numericalPrecisionPolicy(), -
1480 QSqlQuery::setNumericalPrecisionPolicy(), QSqlQuery::numericalPrecisionPolicy() -
1481*/ -
1482void QSqlDatabase::setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy) -
1483{ -
1484 if(driver())
partially evaluated: driver()
TRUEFALSE
yes
Evaluation Count:2
no
Evaluation Count:0
0-2
1485 driver()->setNumericalPrecisionPolicy(precisionPolicy);
executed: driver()->setNumericalPrecisionPolicy(precisionPolicy);
Execution Count:2
2
1486 d->precisionPolicy = precisionPolicy;
executed (the execution status of this line is deduced): d->precisionPolicy = precisionPolicy;
-
1487}
executed: }
Execution Count:2
2
1488 -
1489/*! -
1490 \since 4.6 -
1491 -
1492 Returns the current default precision policy for the database connection. -
1493 -
1494 \sa QSql::NumericalPrecisionPolicy, setNumericalPrecisionPolicy(), -
1495 QSqlQuery::numericalPrecisionPolicy(), QSqlQuery::setNumericalPrecisionPolicy() -
1496*/ -
1497QSql::NumericalPrecisionPolicy QSqlDatabase::numericalPrecisionPolicy() const -
1498{ -
1499 if(driver())
partially evaluated: driver()
TRUEFALSE
yes
Evaluation Count:1
no
Evaluation Count:0
0-1
1500 return driver()->numericalPrecisionPolicy();
executed: return driver()->numericalPrecisionPolicy();
Execution Count:1
1
1501 else -
1502 return d->precisionPolicy;
never executed: return d->precisionPolicy;
0
1503} -
1504 -
1505 -
1506#ifndef QT_NO_DEBUG_STREAM -
1507QDebug operator<<(QDebug dbg, const QSqlDatabase &d) -
1508{ -
1509 if (!d.isValid()) {
never evaluated: !d.isValid()
0
1510 dbg.nospace() << "QSqlDatabase(invalid)";
never executed (the execution status of this line is deduced): dbg.nospace() << "QSqlDatabase(invalid)";
-
1511 return dbg.space();
never executed: return dbg.space();
0
1512 } -
1513 -
1514 dbg.nospace() << "QSqlDatabase(driver=\"" << d.driverName() << "\", database=\""
never executed (the execution status of this line is deduced): dbg.nospace() << "QSqlDatabase(driver=\"" << d.driverName() << "\", database=\""
-
1515 << 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()
-
1516 << ", user=\"" << d.userName() << "\", open=" << d.isOpen() << ")";
never executed (the execution status of this line is deduced): << ", user=\"" << d.userName() << "\", open=" << d.isOpen() << ")";
-
1517 return dbg.space();
never executed: return dbg.space();
0
1518} -
1519#endif -
1520 -
1521QT_END_NAMESPACE -
1522 -
Source codeSwitch to Preprocessed file

Generated by Squish Coco Non-Commercial