| Absolute File Name: | /home/qt/qt5_coco/qt5/qtbase/src/sql/kernel/qsqldatabase.cpp |
| Source code | Switch to Preprocessed file |
| Line | Source | Count | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | /**************************************************************************** | - | ||||||||||||||||||
| 2 | ** | - | ||||||||||||||||||
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. | - | ||||||||||||||||||
| 4 | ** Contact: https://www.qt.io/licensing/ | - | ||||||||||||||||||
| 5 | ** | - | ||||||||||||||||||
| 6 | ** This file is part of the QtSql module of the Qt Toolkit. | - | ||||||||||||||||||
| 7 | ** | - | ||||||||||||||||||
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ | - | ||||||||||||||||||
| 9 | ** Commercial License Usage | - | ||||||||||||||||||
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in | - | ||||||||||||||||||
| 11 | ** accordance with the commercial license agreement provided with the | - | ||||||||||||||||||
| 12 | ** Software or, alternatively, in accordance with the terms contained in | - | ||||||||||||||||||
| 13 | ** a written agreement between you and The Qt Company. For licensing terms | - | ||||||||||||||||||
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further | - | ||||||||||||||||||
| 15 | ** information use the contact form at https://www.qt.io/contact-us. | - | ||||||||||||||||||
| 16 | ** | - | ||||||||||||||||||
| 17 | ** GNU Lesser General Public License Usage | - | ||||||||||||||||||
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser | - | ||||||||||||||||||
| 19 | ** General Public License version 3 as published by the Free Software | - | ||||||||||||||||||
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the | - | ||||||||||||||||||
| 21 | ** packaging of this file. Please review the following information to | - | ||||||||||||||||||
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements | - | ||||||||||||||||||
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. | - | ||||||||||||||||||
| 24 | ** | - | ||||||||||||||||||
| 25 | ** GNU General Public License Usage | - | ||||||||||||||||||
| 26 | ** Alternatively, this file may be used under the terms of the GNU | - | ||||||||||||||||||
| 27 | ** General Public License version 2.0 or (at your option) the GNU General | - | ||||||||||||||||||
| 28 | ** Public license version 3 or any later version approved by the KDE Free | - | ||||||||||||||||||
| 29 | ** Qt Foundation. The licenses are as published by the Free Software | - | ||||||||||||||||||
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 | - | ||||||||||||||||||
| 31 | ** included in the packaging of this file. Please review the following | - | ||||||||||||||||||
| 32 | ** information to ensure the GNU General Public License requirements will | - | ||||||||||||||||||
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and | - | ||||||||||||||||||
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. | - | ||||||||||||||||||
| 35 | ** | - | ||||||||||||||||||
| 36 | ** $QT_END_LICENSE$ | - | ||||||||||||||||||
| 37 | ** | - | ||||||||||||||||||
| 38 | ****************************************************************************/ | - | ||||||||||||||||||
| 39 | - | |||||||||||||||||||
| 40 | #include "qsqldatabase.h" | - | ||||||||||||||||||
| 41 | #include "qsqlquery.h" | - | ||||||||||||||||||
| 42 | - | |||||||||||||||||||
| 43 | #ifdef Q_OS_WIN32 | - | ||||||||||||||||||
| 44 | // Conflicting declarations of LPCBYTE in sqlfront.h and winscard.h | - | ||||||||||||||||||
| 45 | #define _WINSCARD_H_ | - | ||||||||||||||||||
| 46 | #endif | - | ||||||||||||||||||
| 47 | - | |||||||||||||||||||
| 48 | #ifdef QT_SQL_PSQL | - | ||||||||||||||||||
| 49 | #include "../drivers/psql/qsql_psql_p.h" | - | ||||||||||||||||||
| 50 | #endif | - | ||||||||||||||||||
| 51 | #ifdef QT_SQL_MYSQL | - | ||||||||||||||||||
| 52 | #include "../drivers/mysql/qsql_mysql_p.h" | - | ||||||||||||||||||
| 53 | #endif | - | ||||||||||||||||||
| 54 | #ifdef QT_SQL_ODBC | - | ||||||||||||||||||
| 55 | #include "../drivers/odbc/qsql_odbc_p.h" | - | ||||||||||||||||||
| 56 | #endif | - | ||||||||||||||||||
| 57 | #ifdef QT_SQL_OCI | - | ||||||||||||||||||
| 58 | #include "../drivers/oci/qsql_oci_p.h" | - | ||||||||||||||||||
| 59 | #endif | - | ||||||||||||||||||
| 60 | #ifdef QT_SQL_TDS | - | ||||||||||||||||||
| 61 | // conflicting RETCODE typedef between odbc and freetds | - | ||||||||||||||||||
| 62 | #define RETCODE DBRETCODE | - | ||||||||||||||||||
| 63 | #include "../drivers/tds/qsql_tds_p.h" | - | ||||||||||||||||||
| 64 | #undef RETCODE | - | ||||||||||||||||||
| 65 | #endif | - | ||||||||||||||||||
| 66 | #ifdef QT_SQL_DB2 | - | ||||||||||||||||||
| 67 | #include "../drivers/db2/qsql_db2_p.h" | - | ||||||||||||||||||
| 68 | #endif | - | ||||||||||||||||||
| 69 | #ifdef QT_SQL_SQLITE | - | ||||||||||||||||||
| 70 | #include "../drivers/sqlite/qsql_sqlite_p.h" | - | ||||||||||||||||||
| 71 | #endif | - | ||||||||||||||||||
| 72 | #ifdef QT_SQL_SQLITE2 | - | ||||||||||||||||||
| 73 | #include "../drivers/sqlite2/qsql_sqlite2_p.h" | - | ||||||||||||||||||
| 74 | #endif | - | ||||||||||||||||||
| 75 | #ifdef QT_SQL_IBASE | - | ||||||||||||||||||
| 76 | #undef SQL_FLOAT // avoid clash with ODBC | - | ||||||||||||||||||
| 77 | #undef SQL_DOUBLE | - | ||||||||||||||||||
| 78 | #undef SQL_TIMESTAMP | - | ||||||||||||||||||
| 79 | #undef SQL_TYPE_TIME | - | ||||||||||||||||||
| 80 | #undef SQL_TYPE_DATE | - | ||||||||||||||||||
| 81 | #undef SQL_DATE | - | ||||||||||||||||||
| 82 | #define SCHAR IBASE_SCHAR // avoid clash with ODBC (older versions of ibase.h with Firebird) | - | ||||||||||||||||||
| 83 | #include "../drivers/ibase/qsql_ibase_p.h" | - | ||||||||||||||||||
| 84 | #undef SCHAR | - | ||||||||||||||||||
| 85 | #endif | - | ||||||||||||||||||
| 86 | - | |||||||||||||||||||
| 87 | #include "qdebug.h" | - | ||||||||||||||||||
| 88 | #include "qcoreapplication.h" | - | ||||||||||||||||||
| 89 | #include "qreadwritelock.h" | - | ||||||||||||||||||
| 90 | #include "qsqlresult.h" | - | ||||||||||||||||||
| 91 | #include "qsqldriver.h" | - | ||||||||||||||||||
| 92 | #include "qsqldriverplugin.h" | - | ||||||||||||||||||
| 93 | #include "qsqlindex.h" | - | ||||||||||||||||||
| 94 | #include "private/qfactoryloader_p.h" | - | ||||||||||||||||||
| 95 | #include "private/qsqlnulldriver_p.h" | - | ||||||||||||||||||
| 96 | #include "qmutex.h" | - | ||||||||||||||||||
| 97 | #include "qhash.h" | - | ||||||||||||||||||
| 98 | #include <stdlib.h> | - | ||||||||||||||||||
| 99 | - | |||||||||||||||||||
| 100 | QT_BEGIN_NAMESPACE | - | ||||||||||||||||||
| 101 | - | |||||||||||||||||||
| 102 | Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader, executed 9 times by 9 tests: end of blockExecuted by:
executed 9 times by 9 tests: guard.store(QtGlobalStatic::Destroyed);Executed by:
executed 96 times by 9 tests: return &holder.value;Executed by:
| 0-96 | ||||||||||||||||||
| 103 | (QSqlDriverFactoryInterface_iid, | - | ||||||||||||||||||
| 104 | QLatin1String("/sqldrivers"))) | - | ||||||||||||||||||
| 105 | - | |||||||||||||||||||
| 106 | #if !defined(Q_CC_MSVC) || _MSC_VER >= 1900 | - | ||||||||||||||||||
| 107 | // ### Qt6: remove the #ifdef | - | ||||||||||||||||||
| 108 | const | - | ||||||||||||||||||
| 109 | #endif | - | ||||||||||||||||||
| 110 | char *QSqlDatabase::defaultConnection = const_cast<char *>("qt_sql_default_connection"); | - | ||||||||||||||||||
| 111 | - | |||||||||||||||||||
| 112 | typedef QHash<QString, QSqlDriverCreatorBase*> DriverDict; | - | ||||||||||||||||||
| 113 | - | |||||||||||||||||||
| 114 | class QConnectionDict: public QHash<QString, QSqlDatabase> | - | ||||||||||||||||||
| 115 | { | - | ||||||||||||||||||
| 116 | public: | - | ||||||||||||||||||
| 117 | inline bool contains_ts(const QString &key) | - | ||||||||||||||||||
| 118 | { | - | ||||||||||||||||||
| 119 | QReadLocker locker(&lock); | - | ||||||||||||||||||
| 120 | return contains(key); executed 2 times by 1 test: return contains(key);Executed by:
| 2 | ||||||||||||||||||
| 121 | } | - | ||||||||||||||||||
| 122 | inline QStringList keys_ts() const | - | ||||||||||||||||||
| 123 | { | - | ||||||||||||||||||
| 124 | QReadLocker locker(&lock); | - | ||||||||||||||||||
| 125 | return keys(); never executed: return keys(); | 0 | ||||||||||||||||||
| 126 | } | - | ||||||||||||||||||
| 127 | - | |||||||||||||||||||
| 128 | mutable QReadWriteLock lock; | - | ||||||||||||||||||
| 129 | }; | - | ||||||||||||||||||
| 130 | Q_GLOBAL_STATIC(QConnectionDict, dbDict) executed 9 times by 9 tests: end of blockExecuted by:
executed 9 times by 9 tests: guard.store(QtGlobalStatic::Destroyed);Executed by:
executed 2321 times by 17 tests: return &holder.value;Executed by:
| 0-2321 | ||||||||||||||||||
| 131 | - | |||||||||||||||||||
| 132 | class QSqlDatabasePrivate | - | ||||||||||||||||||
| 133 | { | - | ||||||||||||||||||
| 134 | public: | - | ||||||||||||||||||
| 135 | QSqlDatabasePrivate(QSqlDatabase *d, QSqlDriver *dr = 0): | - | ||||||||||||||||||
| 136 | ref(1), | - | ||||||||||||||||||
| 137 | q(d), | - | ||||||||||||||||||
| 138 | driver(dr), | - | ||||||||||||||||||
| 139 | port(-1) | - | ||||||||||||||||||
| 140 | { | - | ||||||||||||||||||
| 141 | precisionPolicy = QSql::LowPrecisionDouble; | - | ||||||||||||||||||
| 142 | } executed 44 times by 9 tests: end of blockExecuted by:
| 44 | ||||||||||||||||||
| 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 | - | |||||||||||||||||||
| 171 | QSqlDatabasePrivate::QSqlDatabasePrivate(const QSqlDatabasePrivate &other) : ref(1) | - | ||||||||||||||||||
| 172 | { | - | ||||||||||||||||||
| 173 | q = other.q; | - | ||||||||||||||||||
| 174 | dbname = other.dbname; | - | ||||||||||||||||||
| 175 | uname = other.uname; | - | ||||||||||||||||||
| 176 | pword = other.pword; | - | ||||||||||||||||||
| 177 | hname = other.hname; | - | ||||||||||||||||||
| 178 | drvName = other.drvName; | - | ||||||||||||||||||
| 179 | port = other.port; | - | ||||||||||||||||||
| 180 | connOptions = other.connOptions; | - | ||||||||||||||||||
| 181 | driver = other.driver; | - | ||||||||||||||||||
| 182 | precisionPolicy = other.precisionPolicy; | - | ||||||||||||||||||
| 183 | } never executed: end of block | 0 | ||||||||||||||||||
| 184 | - | |||||||||||||||||||
| 185 | QSqlDatabasePrivate::~QSqlDatabasePrivate() | - | ||||||||||||||||||
| 186 | { | - | ||||||||||||||||||
| 187 | if (driver != shared_null()->driver)
| 13-33 | ||||||||||||||||||
| 188 | delete driver; executed 33 times by 11 tests: delete driver;Executed by:
| 33 | ||||||||||||||||||
| 189 | } executed 46 times by 17 tests: end of blockExecuted by:
| 46 | ||||||||||||||||||
| 190 | - | |||||||||||||||||||
| 191 | void QSqlDatabasePrivate::cleanConnections() | - | ||||||||||||||||||
| 192 | { | - | ||||||||||||||||||
| 193 | QConnectionDict *dict = dbDict(); | - | ||||||||||||||||||
| 194 | Q_ASSERT(dict); | - | ||||||||||||||||||
| 195 | QWriteLocker locker(&dict->lock); | - | ||||||||||||||||||
| 196 | - | |||||||||||||||||||
| 197 | QConnectionDict::iterator it = dict->begin(); | - | ||||||||||||||||||
| 198 | while (it != dict->end()) {
| 7-22 | ||||||||||||||||||
| 199 | invalidateDb(it.value(), it.key(), false); | - | ||||||||||||||||||
| 200 | ++it; | - | ||||||||||||||||||
| 201 | } executed 7 times by 4 tests: end of blockExecuted by:
| 7 | ||||||||||||||||||
| 202 | dict->clear(); | - | ||||||||||||||||||
| 203 | } executed 22 times by 9 tests: end of blockExecuted by:
| 22 | ||||||||||||||||||
| 204 | - | |||||||||||||||||||
| 205 | static bool qDriverDictInit = false; | - | ||||||||||||||||||
| 206 | static void cleanDriverDict() | - | ||||||||||||||||||
| 207 | { | - | ||||||||||||||||||
| 208 | qDeleteAll(QSqlDatabasePrivate::driverDict()); | - | ||||||||||||||||||
| 209 | QSqlDatabasePrivate::driverDict().clear(); | - | ||||||||||||||||||
| 210 | QSqlDatabasePrivate::cleanConnections(); | - | ||||||||||||||||||
| 211 | qDriverDictInit = false; | - | ||||||||||||||||||
| 212 | } executed 22 times by 9 tests: end of blockExecuted by:
| 22 | ||||||||||||||||||
| 213 | - | |||||||||||||||||||
| 214 | DriverDict &QSqlDatabasePrivate::driverDict() | - | ||||||||||||||||||
| 215 | { | - | ||||||||||||||||||
| 216 | static DriverDict dict; | - | ||||||||||||||||||
| 217 | if (!qDriverDictInit) {
| 21-87 | ||||||||||||||||||
| 218 | qDriverDictInit = true; | - | ||||||||||||||||||
| 219 | qAddPostRoutine(cleanDriverDict); | - | ||||||||||||||||||
| 220 | } executed 21 times by 8 tests: end of blockExecuted by:
| 21 | ||||||||||||||||||
| 221 | return dict; executed 108 times by 17 tests: return dict;Executed by:
| 108 | ||||||||||||||||||
| 222 | } | - | ||||||||||||||||||
| 223 | - | |||||||||||||||||||
| 224 | QSqlDatabasePrivate *QSqlDatabasePrivate::shared_null() | - | ||||||||||||||||||
| 225 | { | - | ||||||||||||||||||
| 226 | static QSqlNullDriver dr; | - | ||||||||||||||||||
| 227 | static QSqlDatabasePrivate n(NULL, &dr); | - | ||||||||||||||||||
| 228 | return &n; executed 8121 times by 18 tests: return &n;Executed by:
| 8121 | ||||||||||||||||||
| 229 | } | - | ||||||||||||||||||
| 230 | - | |||||||||||||||||||
| 231 | void QSqlDatabasePrivate::invalidateDb(const QSqlDatabase &db, const QString &name, bool doWarn) | - | ||||||||||||||||||
| 232 | { | - | ||||||||||||||||||
| 233 | if (db.d->ref.load() != 1 && doWarn) {
| 0-36 | ||||||||||||||||||
| 234 | qWarning("QSqlDatabasePrivate::removeDatabase: connection '%s' is still in use, " | - | ||||||||||||||||||
| 235 | "all queries will cease to work.", name.toLocal8Bit().constData()); | - | ||||||||||||||||||
| 236 | db.d->disable(); | - | ||||||||||||||||||
| 237 | db.d->connName.clear(); | - | ||||||||||||||||||
| 238 | } executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||||||||
| 239 | } executed 37 times by 11 tests: end of blockExecuted by:
| 37 | ||||||||||||||||||
| 240 | - | |||||||||||||||||||
| 241 | void QSqlDatabasePrivate::removeDatabase(const QString &name) | - | ||||||||||||||||||
| 242 | { | - | ||||||||||||||||||
| 243 | QConnectionDict *dict = dbDict(); | - | ||||||||||||||||||
| 244 | Q_ASSERT(dict); | - | ||||||||||||||||||
| 245 | QWriteLocker locker(&dict->lock); | - | ||||||||||||||||||
| 246 | - | |||||||||||||||||||
| 247 | if (!dict->contains(name))
| 0-30 | ||||||||||||||||||
| 248 | return; never executed: return; | 0 | ||||||||||||||||||
| 249 | - | |||||||||||||||||||
| 250 | invalidateDb(dict->take(name), name); | - | ||||||||||||||||||
| 251 | } executed 30 times by 8 tests: end of blockExecuted by:
| 30 | ||||||||||||||||||
| 252 | - | |||||||||||||||||||
| 253 | void QSqlDatabasePrivate::addDatabase(const QSqlDatabase &db, const QString &name) | - | ||||||||||||||||||
| 254 | { | - | ||||||||||||||||||
| 255 | QConnectionDict *dict = dbDict(); | - | ||||||||||||||||||
| 256 | Q_ASSERT(dict); | - | ||||||||||||||||||
| 257 | QWriteLocker locker(&dict->lock); | - | ||||||||||||||||||
| 258 | - | |||||||||||||||||||
| 259 | if (dict->contains(name)) {
| 0-36 | ||||||||||||||||||
| 260 | invalidateDb(dict->take(name), name); | - | ||||||||||||||||||
| 261 | qWarning("QSqlDatabasePrivate::addDatabase: duplicate connection name '%s', old " | - | ||||||||||||||||||
| 262 | "connection removed.", name.toLocal8Bit().data()); | - | ||||||||||||||||||
| 263 | } never executed: end of block | 0 | ||||||||||||||||||
| 264 | dict->insert(name, db); | - | ||||||||||||||||||
| 265 | db.d->connName = name; | - | ||||||||||||||||||
| 266 | } executed 36 times by 9 tests: end of blockExecuted by:
| 36 | ||||||||||||||||||
| 267 | - | |||||||||||||||||||
| 268 | /*! \internal | - | ||||||||||||||||||
| 269 | */ | - | ||||||||||||||||||
| 270 | QSqlDatabase QSqlDatabasePrivate::database(const QString& name, bool open) | - | ||||||||||||||||||
| 271 | { | - | ||||||||||||||||||
| 272 | const QConnectionDict *dict = dbDict(); | - | ||||||||||||||||||
| 273 | Q_ASSERT(dict); | - | ||||||||||||||||||
| 274 | - | |||||||||||||||||||
| 275 | dict->lock.lockForRead(); | - | ||||||||||||||||||
| 276 | QSqlDatabase db = dict->value(name); | - | ||||||||||||||||||
| 277 | dict->lock.unlock(); | - | ||||||||||||||||||
| 278 | if (db.isValid() && !db.isOpen() && open) {
| 5-1969 | ||||||||||||||||||
| 279 | if (!db.open())
| 0-5 | ||||||||||||||||||
| 280 | qWarning() << "QSqlDatabasePrivate::database: unable to open database:" << db.lastError().text(); never executed: QMessageLogger(__FILE__, 280, __PRETTY_FUNCTION__).warning() << "QSqlDatabasePrivate::database: unable to open database:" << db.lastError().text(); | 0 | ||||||||||||||||||
| 281 | - | |||||||||||||||||||
| 282 | } executed 5 times by 1 test: end of blockExecuted by:
| 5 | ||||||||||||||||||
| 283 | return db; executed 2231 times by 9 tests: return db;Executed by:
| 2231 | ||||||||||||||||||
| 284 | } | - | ||||||||||||||||||
| 285 | - | |||||||||||||||||||
| 286 | - | |||||||||||||||||||
| 287 | /*! \internal | - | ||||||||||||||||||
| 288 | Copies the connection data from \a other. | - | ||||||||||||||||||
| 289 | */ | - | ||||||||||||||||||
| 290 | void QSqlDatabasePrivate::copy(const QSqlDatabasePrivate *other) | - | ||||||||||||||||||
| 291 | { | - | ||||||||||||||||||
| 292 | q = other->q; | - | ||||||||||||||||||
| 293 | dbname = other->dbname; | - | ||||||||||||||||||
| 294 | uname = other->uname; | - | ||||||||||||||||||
| 295 | pword = other->pword; | - | ||||||||||||||||||
| 296 | hname = other->hname; | - | ||||||||||||||||||
| 297 | drvName = other->drvName; | - | ||||||||||||||||||
| 298 | port = other->port; | - | ||||||||||||||||||
| 299 | connOptions = other->connOptions; | - | ||||||||||||||||||
| 300 | precisionPolicy = other->precisionPolicy; | - | ||||||||||||||||||
| 301 | } executed 7 times by 3 tests: end of blockExecuted by:
| 7 | ||||||||||||||||||
| 302 | - | |||||||||||||||||||
| 303 | void QSqlDatabasePrivate::disable() | - | ||||||||||||||||||
| 304 | { | - | ||||||||||||||||||
| 305 | if (driver != shared_null()->driver) {
| 0-1 | ||||||||||||||||||
| 306 | delete driver; | - | ||||||||||||||||||
| 307 | driver = shared_null()->driver; | - | ||||||||||||||||||
| 308 | } executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||||||||
| 309 | } executed 1 time by 1 test: end of blockExecuted by:
| 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 \c 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 | */ | - | ||||||||||||||||||
| 477 | QSqlDatabase QSqlDatabase::addDatabase(const QString &type, const QString &connectionName) | - | ||||||||||||||||||
| 478 | { | - | ||||||||||||||||||
| 479 | QSqlDatabase db(type); | - | ||||||||||||||||||
| 480 | QSqlDatabasePrivate::addDatabase(db, connectionName); | - | ||||||||||||||||||
| 481 | return db; executed 29 times by 8 tests: return db;Executed by:
| 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 | - | |||||||||||||||||||
| 498 | QSqlDatabase QSqlDatabase::database(const QString& connectionName, bool open) | - | ||||||||||||||||||
| 499 | { | - | ||||||||||||||||||
| 500 | return QSqlDatabasePrivate::database(connectionName, open); executed 2231 times by 9 tests: return QSqlDatabasePrivate::database(connectionName, open);Executed by:
| 2231 | ||||||||||||||||||
| 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 | - | |||||||||||||||||||
| 530 | void QSqlDatabase::removeDatabase(const QString& connectionName) | - | ||||||||||||||||||
| 531 | { | - | ||||||||||||||||||
| 532 | QSqlDatabasePrivate::removeDatabase(connectionName); | - | ||||||||||||||||||
| 533 | } executed 30 times by 8 tests: end of blockExecuted by:
| 30 | ||||||||||||||||||
| 534 | - | |||||||||||||||||||
| 535 | /*! | - | ||||||||||||||||||
| 536 | Returns a list of all the available database drivers. | - | ||||||||||||||||||
| 537 | - | |||||||||||||||||||
| 538 | \sa registerSqlDriver() | - | ||||||||||||||||||
| 539 | */ | - | ||||||||||||||||||
| 540 | - | |||||||||||||||||||
| 541 | QStringList QSqlDatabase::drivers() | - | ||||||||||||||||||
| 542 | { | - | ||||||||||||||||||
| 543 | 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 | if (QFactoryLoader *fl = loader()) {
| 0-26 | ||||||||||||||||||
| 579 | typedef QMultiMap<int, QString> PluginKeyMap; | - | ||||||||||||||||||
| 580 | typedef PluginKeyMap::const_iterator PluginKeyMapConstIterator; | - | ||||||||||||||||||
| 581 | - | |||||||||||||||||||
| 582 | const PluginKeyMap keyMap = fl->keyMap(); | - | ||||||||||||||||||
| 583 | const PluginKeyMapConstIterator cend = keyMap.constEnd(); | - | ||||||||||||||||||
| 584 | for (PluginKeyMapConstIterator it = keyMap.constBegin(); it != cend; ++it)
| 26-234 | ||||||||||||||||||
| 585 | if (!list.contains(it.value()))
| 0-234 | ||||||||||||||||||
| 586 | list << it.value(); executed 234 times by 7 tests: list << it.value();Executed by:
| 234 | ||||||||||||||||||
| 587 | } executed 26 times by 7 tests: end of blockExecuted by:
| 26 | ||||||||||||||||||
| 588 | - | |||||||||||||||||||
| 589 | DriverDict dict = QSqlDatabasePrivate::driverDict(); | - | ||||||||||||||||||
| 590 | for (DriverDict::const_iterator i = dict.constBegin(); i != dict.constEnd(); ++i) {
| 1-26 | ||||||||||||||||||
| 591 | if (!list.contains(i.key()))
| 0-1 | ||||||||||||||||||
| 592 | list << i.key(); executed 1 time by 1 test: list << i.key();Executed by:
| 1 | ||||||||||||||||||
| 593 | } executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||||||||
| 594 | - | |||||||||||||||||||
| 595 | return list; executed 26 times by 7 tests: return list;Executed by:
| 26 | ||||||||||||||||||
| 596 | } | - | ||||||||||||||||||
| 597 | - | |||||||||||||||||||
| 598 | /*! | - | ||||||||||||||||||
| 599 | This function registers a new SQL driver called \a name, within | - | ||||||||||||||||||
| 600 | the SQL framework. This is useful if you have a custom SQL driver | - | ||||||||||||||||||
| 601 | and don't want to compile it as a plugin. | - | ||||||||||||||||||
| 602 | - | |||||||||||||||||||
| 603 | Example: | - | ||||||||||||||||||
| 604 | \snippet code/src_sql_kernel_qsqldatabase.cpp 2 | - | ||||||||||||||||||
| 605 | - | |||||||||||||||||||
| 606 | QSqlDatabase takes ownership of the \a creator pointer, so you | - | ||||||||||||||||||
| 607 | mustn't delete it yourself. | - | ||||||||||||||||||
| 608 | - | |||||||||||||||||||
| 609 | \sa drivers() | - | ||||||||||||||||||
| 610 | */ | - | ||||||||||||||||||
| 611 | void QSqlDatabase::registerSqlDriver(const QString& name, QSqlDriverCreatorBase *creator) | - | ||||||||||||||||||
| 612 | { | - | ||||||||||||||||||
| 613 | delete QSqlDatabasePrivate::driverDict().take(name); | - | ||||||||||||||||||
| 614 | if (creator)
| 0-1 | ||||||||||||||||||
| 615 | QSqlDatabasePrivate::driverDict().insert(name, creator); executed 1 time by 1 test: QSqlDatabasePrivate::driverDict().insert(name, creator);Executed by:
| 1 | ||||||||||||||||||
| 616 | } executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||||||||
| 617 | - | |||||||||||||||||||
| 618 | /*! | - | ||||||||||||||||||
| 619 | \threadsafe | - | ||||||||||||||||||
| 620 | - | |||||||||||||||||||
| 621 | Returns \c true if the list of database connections contains \a | - | ||||||||||||||||||
| 622 | connectionName; otherwise returns \c false. | - | ||||||||||||||||||
| 623 | - | |||||||||||||||||||
| 624 | \sa connectionNames(), database(), {Threads and the SQL Module} | - | ||||||||||||||||||
| 625 | */ | - | ||||||||||||||||||
| 626 | - | |||||||||||||||||||
| 627 | bool QSqlDatabase::contains(const QString& connectionName) | - | ||||||||||||||||||
| 628 | { | - | ||||||||||||||||||
| 629 | return dbDict()->contains_ts(connectionName); executed 2 times by 1 test: return dbDict()->contains_ts(connectionName);Executed by:
| 2 | ||||||||||||||||||
| 630 | } | - | ||||||||||||||||||
| 631 | - | |||||||||||||||||||
| 632 | /*! | - | ||||||||||||||||||
| 633 | \threadsafe | - | ||||||||||||||||||
| 634 | - | |||||||||||||||||||
| 635 | Returns a list containing the names of all connections. | - | ||||||||||||||||||
| 636 | - | |||||||||||||||||||
| 637 | \sa contains(), database(), {Threads and the SQL Module} | - | ||||||||||||||||||
| 638 | */ | - | ||||||||||||||||||
| 639 | QStringList QSqlDatabase::connectionNames() | - | ||||||||||||||||||
| 640 | { | - | ||||||||||||||||||
| 641 | return dbDict()->keys_ts(); never executed: return dbDict()->keys_ts(); | 0 | ||||||||||||||||||
| 642 | } | - | ||||||||||||||||||
| 643 | - | |||||||||||||||||||
| 644 | /*! | - | ||||||||||||||||||
| 645 | \overload | - | ||||||||||||||||||
| 646 | - | |||||||||||||||||||
| 647 | Creates a QSqlDatabase connection that uses the driver referred | - | ||||||||||||||||||
| 648 | to by \a type. If the \a type is not recognized, the database | - | ||||||||||||||||||
| 649 | connection will have no functionality. | - | ||||||||||||||||||
| 650 | - | |||||||||||||||||||
| 651 | The currently available driver types are: | - | ||||||||||||||||||
| 652 | - | |||||||||||||||||||
| 653 | \table | - | ||||||||||||||||||
| 654 | \header \li Driver Type \li Description | - | ||||||||||||||||||
| 655 | \row \li QDB2 \li IBM DB2 | - | ||||||||||||||||||
| 656 | \row \li QIBASE \li Borland InterBase Driver | - | ||||||||||||||||||
| 657 | \row \li QMYSQL \li MySQL Driver | - | ||||||||||||||||||
| 658 | \row \li QOCI \li Oracle Call Interface Driver | - | ||||||||||||||||||
| 659 | \row \li QODBC \li ODBC Driver (includes Microsoft SQL Server) | - | ||||||||||||||||||
| 660 | \row \li QPSQL \li PostgreSQL Driver | - | ||||||||||||||||||
| 661 | \row \li QSQLITE \li SQLite version 3 or above | - | ||||||||||||||||||
| 662 | \row \li QSQLITE2 \li SQLite version 2 | - | ||||||||||||||||||
| 663 | \row \li QTDS \li Sybase Adaptive Server | - | ||||||||||||||||||
| 664 | \endtable | - | ||||||||||||||||||
| 665 | - | |||||||||||||||||||
| 666 | Additional third party drivers, including your own custom | - | ||||||||||||||||||
| 667 | drivers, can be loaded dynamically. | - | ||||||||||||||||||
| 668 | - | |||||||||||||||||||
| 669 | \sa {SQL Database Drivers}, registerSqlDriver(), drivers() | - | ||||||||||||||||||
| 670 | */ | - | ||||||||||||||||||
| 671 | - | |||||||||||||||||||
| 672 | QSqlDatabase::QSqlDatabase(const QString &type) | - | ||||||||||||||||||
| 673 | { | - | ||||||||||||||||||
| 674 | d = new QSqlDatabasePrivate(this); | - | ||||||||||||||||||
| 675 | d->init(type); | - | ||||||||||||||||||
| 676 | } executed 36 times by 9 tests: end of blockExecuted by:
| 36 | ||||||||||||||||||
| 677 | - | |||||||||||||||||||
| 678 | /*! | - | ||||||||||||||||||
| 679 | \overload | - | ||||||||||||||||||
| 680 | - | |||||||||||||||||||
| 681 | Creates a database connection using the given \a driver. | - | ||||||||||||||||||
| 682 | */ | - | ||||||||||||||||||
| 683 | - | |||||||||||||||||||
| 684 | QSqlDatabase::QSqlDatabase(QSqlDriver *driver) | - | ||||||||||||||||||
| 685 | { | - | ||||||||||||||||||
| 686 | d = new QSqlDatabasePrivate(this, driver); | - | ||||||||||||||||||
| 687 | } never executed: end of block | 0 | ||||||||||||||||||
| 688 | - | |||||||||||||||||||
| 689 | /*! | - | ||||||||||||||||||
| 690 | Creates an empty, invalid QSqlDatabase object. Use addDatabase(), | - | ||||||||||||||||||
| 691 | removeDatabase(), and database() to get valid QSqlDatabase | - | ||||||||||||||||||
| 692 | objects. | - | ||||||||||||||||||
| 693 | */ | - | ||||||||||||||||||
| 694 | QSqlDatabase::QSqlDatabase() | - | ||||||||||||||||||
| 695 | { | - | ||||||||||||||||||
| 696 | d = QSqlDatabasePrivate::shared_null(); | - | ||||||||||||||||||
| 697 | d->ref.ref(); | - | ||||||||||||||||||
| 698 | } executed 1287 times by 9 tests: end of blockExecuted by:
| 1287 | ||||||||||||||||||
| 699 | - | |||||||||||||||||||
| 700 | /*! | - | ||||||||||||||||||
| 701 | Creates a copy of \a other. | - | ||||||||||||||||||
| 702 | */ | - | ||||||||||||||||||
| 703 | QSqlDatabase::QSqlDatabase(const QSqlDatabase &other) | - | ||||||||||||||||||
| 704 | { | - | ||||||||||||||||||
| 705 | d = other.d; | - | ||||||||||||||||||
| 706 | d->ref.ref(); | - | ||||||||||||||||||
| 707 | } executed 19102 times by 9 tests: end of blockExecuted by:
| 19102 | ||||||||||||||||||
| 708 | - | |||||||||||||||||||
| 709 | /*! | - | ||||||||||||||||||
| 710 | Assigns \a other to this object. | - | ||||||||||||||||||
| 711 | */ | - | ||||||||||||||||||
| 712 | QSqlDatabase &QSqlDatabase::operator=(const QSqlDatabase &other) | - | ||||||||||||||||||
| 713 | { | - | ||||||||||||||||||
| 714 | qAtomicAssign(d, other.d); | - | ||||||||||||||||||
| 715 | return *this; executed 968 times by 9 tests: return *this;Executed by:
| 968 | ||||||||||||||||||
| 716 | } | - | ||||||||||||||||||
| 717 | - | |||||||||||||||||||
| 718 | /*! | - | ||||||||||||||||||
| 719 | \internal | - | ||||||||||||||||||
| 720 | - | |||||||||||||||||||
| 721 | Create the actual driver instance \a type. | - | ||||||||||||||||||
| 722 | */ | - | ||||||||||||||||||
| 723 | - | |||||||||||||||||||
| 724 | void QSqlDatabasePrivate::init(const QString &type) | - | ||||||||||||||||||
| 725 | { | - | ||||||||||||||||||
| 726 | drvName = type; | - | ||||||||||||||||||
| 727 | - | |||||||||||||||||||
| 728 | if (!driver) {
| 0-36 | ||||||||||||||||||
| 729 | #ifdef QT_SQL_PSQL | - | ||||||||||||||||||
| 730 | if (type == QLatin1String("QPSQL") || type == QLatin1String("QPSQL7")) | - | ||||||||||||||||||
| 731 | driver = new QPSQLDriver(); | - | ||||||||||||||||||
| 732 | #endif | - | ||||||||||||||||||
| 733 | #ifdef QT_SQL_MYSQL | - | ||||||||||||||||||
| 734 | if (type == QLatin1String("QMYSQL") || type == QLatin1String("QMYSQL3")) | - | ||||||||||||||||||
| 735 | driver = new QMYSQLDriver(); | - | ||||||||||||||||||
| 736 | #endif | - | ||||||||||||||||||
| 737 | #ifdef QT_SQL_ODBC | - | ||||||||||||||||||
| 738 | if (type == QLatin1String("QODBC") || type == QLatin1String("QODBC3")) | - | ||||||||||||||||||
| 739 | driver = new QODBCDriver(); | - | ||||||||||||||||||
| 740 | #endif | - | ||||||||||||||||||
| 741 | #ifdef QT_SQL_OCI | - | ||||||||||||||||||
| 742 | if (type == QLatin1String("QOCI") || type == QLatin1String("QOCI8")) | - | ||||||||||||||||||
| 743 | driver = new QOCIDriver(); | - | ||||||||||||||||||
| 744 | #endif | - | ||||||||||||||||||
| 745 | #ifdef QT_SQL_TDS | - | ||||||||||||||||||
| 746 | if (type == QLatin1String("QTDS") || type == QLatin1String("QTDS7")) | - | ||||||||||||||||||
| 747 | driver = new QTDSDriver(); | - | ||||||||||||||||||
| 748 | #endif | - | ||||||||||||||||||
| 749 | #ifdef QT_SQL_DB2 | - | ||||||||||||||||||
| 750 | if (type == QLatin1String("QDB2")) | - | ||||||||||||||||||
| 751 | driver = new QDB2Driver(); | - | ||||||||||||||||||
| 752 | #endif | - | ||||||||||||||||||
| 753 | #ifdef QT_SQL_SQLITE | - | ||||||||||||||||||
| 754 | if (type == QLatin1String("QSQLITE")) | - | ||||||||||||||||||
| 755 | driver = new QSQLiteDriver(); | - | ||||||||||||||||||
| 756 | #endif | - | ||||||||||||||||||
| 757 | #ifdef QT_SQL_SQLITE2 | - | ||||||||||||||||||
| 758 | if (type == QLatin1String("QSQLITE2")) | - | ||||||||||||||||||
| 759 | driver = new QSQLite2Driver(); | - | ||||||||||||||||||
| 760 | #endif | - | ||||||||||||||||||
| 761 | #ifdef QT_SQL_IBASE | - | ||||||||||||||||||
| 762 | if (type == QLatin1String("QIBASE")) | - | ||||||||||||||||||
| 763 | driver = new QIBaseDriver(); | - | ||||||||||||||||||
| 764 | #endif | - | ||||||||||||||||||
| 765 | } executed 36 times by 9 tests: end of blockExecuted by:
| 36 | ||||||||||||||||||
| 766 | - | |||||||||||||||||||
| 767 | if (!driver) {
| 0-36 | ||||||||||||||||||
| 768 | DriverDict dict = QSqlDatabasePrivate::driverDict(); | - | ||||||||||||||||||
| 769 | for (DriverDict::const_iterator it = dict.constBegin(); | - | ||||||||||||||||||
| 770 | it != dict.constEnd() && !driver; ++it) {
| 0-36 | ||||||||||||||||||
| 771 | if (type == it.key()) {
| 0-1 | ||||||||||||||||||
| 772 | driver = ((QSqlDriverCreatorBase*)(*it))->createObject(); | - | ||||||||||||||||||
| 773 | } executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||||||||
| 774 | } executed 1 time by 1 test: end of blockExecuted by:
| 1 | ||||||||||||||||||
| 775 | } executed 36 times by 9 tests: end of blockExecuted by:
| 36 | ||||||||||||||||||
| 776 | - | |||||||||||||||||||
| 777 | if (!driver && loader())
| 0-35 | ||||||||||||||||||
| 778 | driver = qLoadPlugin<QSqlDriver, QSqlDriverPlugin>(loader(), type); executed 35 times by 9 tests: driver = qLoadPlugin<QSqlDriver, QSqlDriverPlugin>(loader(), type);Executed by:
| 35 | ||||||||||||||||||
| 779 | - | |||||||||||||||||||
| 780 | if (!driver) {
| 3-33 | ||||||||||||||||||
| 781 | qWarning("QSqlDatabase: %s driver not loaded", type.toLatin1().data()); | - | ||||||||||||||||||
| 782 | qWarning("QSqlDatabase: available drivers: %s", | - | ||||||||||||||||||
| 783 | QSqlDatabase::drivers().join(QLatin1Char(' ')).toLatin1().data()); | - | ||||||||||||||||||
| 784 | if (QCoreApplication::instance() == 0)
| 1-2 | ||||||||||||||||||
| 785 | qWarning("QSqlDatabase: an instance of QCoreApplication is required for loading driver plugins"); executed 1 time by 1 test: QMessageLogger(__FILE__, 785, __PRETTY_FUNCTION__).warning("QSqlDatabase: an instance of QCoreApplication is required for loading driver plugins");Executed by:
| 1 | ||||||||||||||||||
| 786 | driver = shared_null()->driver; | - | ||||||||||||||||||
| 787 | } executed 3 times by 3 tests: end of blockExecuted by:
| 3 | ||||||||||||||||||
| 788 | } executed 36 times by 9 tests: end of blockExecuted by:
| 36 | ||||||||||||||||||
| 789 | - | |||||||||||||||||||
| 790 | /*! | - | ||||||||||||||||||
| 791 | Destroys the object and frees any allocated resources. | - | ||||||||||||||||||
| 792 | - | |||||||||||||||||||
| 793 | \sa close() | - | ||||||||||||||||||
| 794 | */ | - | ||||||||||||||||||
| 795 | - | |||||||||||||||||||
| 796 | QSqlDatabase::~QSqlDatabase() | - | ||||||||||||||||||
| 797 | { | - | ||||||||||||||||||
| 798 | if (!d->ref.deref()) {
| 37-20389 | ||||||||||||||||||
| 799 | close(); | - | ||||||||||||||||||
| 800 | delete d; | - | ||||||||||||||||||
| 801 | } executed 37 times by 11 tests: end of blockExecuted by:
| 37 | ||||||||||||||||||
| 802 | } executed 20426 times by 12 tests: end of blockExecuted by:
| 20426 | ||||||||||||||||||
| 803 | - | |||||||||||||||||||
| 804 | /*! | - | ||||||||||||||||||
| 805 | Executes a SQL statement on the database and returns a QSqlQuery | - | ||||||||||||||||||
| 806 | object. Use lastError() to retrieve error information. If \a | - | ||||||||||||||||||
| 807 | query is empty, an empty, invalid query is returned and | - | ||||||||||||||||||
| 808 | lastError() is not affected. | - | ||||||||||||||||||
| 809 | - | |||||||||||||||||||
| 810 | \sa QSqlQuery, lastError() | - | ||||||||||||||||||
| 811 | */ | - | ||||||||||||||||||
| 812 | - | |||||||||||||||||||
| 813 | QSqlQuery QSqlDatabase::exec(const QString & query) const | - | ||||||||||||||||||
| 814 | { | - | ||||||||||||||||||
| 815 | QSqlQuery r(d->driver->createResult()); | - | ||||||||||||||||||
| 816 | if (!query.isEmpty()) {
| 0-16 | ||||||||||||||||||
| 817 | r.exec(query); | - | ||||||||||||||||||
| 818 | d->driver->setLastError(r.lastError()); | - | ||||||||||||||||||
| 819 | } executed 16 times by 1 test: end of blockExecuted by:
| 16 | ||||||||||||||||||
| 820 | return r; executed 16 times by 1 test: return r;Executed by:
| 16 | ||||||||||||||||||
| 821 | } | - | ||||||||||||||||||
| 822 | - | |||||||||||||||||||
| 823 | /*! | - | ||||||||||||||||||
| 824 | Opens the database connection using the current connection | - | ||||||||||||||||||
| 825 | values. Returns \c true on success; otherwise returns \c false. Error | - | ||||||||||||||||||
| 826 | information can be retrieved using lastError(). | - | ||||||||||||||||||
| 827 | - | |||||||||||||||||||
| 828 | \sa lastError(), setDatabaseName(), setUserName(), setPassword(), | - | ||||||||||||||||||
| 829 | setHostName(), setPort(), setConnectOptions() | - | ||||||||||||||||||
| 830 | */ | - | ||||||||||||||||||
| 831 | - | |||||||||||||||||||
| 832 | bool QSqlDatabase::open() | - | ||||||||||||||||||
| 833 | { | - | ||||||||||||||||||
| 834 | return d->driver->open(d->dbname, d->uname, d->pword, d->hname, executed 48 times by 9 tests: return d->driver->open(d->dbname, d->uname, d->pword, d->hname, d->port, d->connOptions);Executed by:
| 48 | ||||||||||||||||||
| 835 | d->port, d->connOptions); executed 48 times by 9 tests: return d->driver->open(d->dbname, d->uname, d->pword, d->hname, d->port, d->connOptions);Executed by:
| 48 | ||||||||||||||||||
| 836 | } | - | ||||||||||||||||||
| 837 | - | |||||||||||||||||||
| 838 | /*! | - | ||||||||||||||||||
| 839 | \overload | - | ||||||||||||||||||
| 840 | - | |||||||||||||||||||
| 841 | Opens the database connection using the given \a user name and \a | - | ||||||||||||||||||
| 842 | password. Returns \c true on success; otherwise returns \c false. Error | - | ||||||||||||||||||
| 843 | information can be retrieved using the lastError() function. | - | ||||||||||||||||||
| 844 | - | |||||||||||||||||||
| 845 | This function does not store the password it is given. Instead, | - | ||||||||||||||||||
| 846 | the password is passed directly to the driver for opening the | - | ||||||||||||||||||
| 847 | connection and it is then discarded. | - | ||||||||||||||||||
| 848 | - | |||||||||||||||||||
| 849 | \sa lastError() | - | ||||||||||||||||||
| 850 | */ | - | ||||||||||||||||||
| 851 | - | |||||||||||||||||||
| 852 | bool QSqlDatabase::open(const QString& user, const QString& password) | - | ||||||||||||||||||
| 853 | { | - | ||||||||||||||||||
| 854 | setUserName(user); | - | ||||||||||||||||||
| 855 | return d->driver->open(d->dbname, user, password, d->hname, executed 1 time by 1 test: return d->driver->open(d->dbname, user, password, d->hname, d->port, d->connOptions);Executed by:
| 1 | ||||||||||||||||||
| 856 | d->port, d->connOptions); executed 1 time by 1 test: return d->driver->open(d->dbname, user, password, d->hname, d->port, d->connOptions);Executed by:
| 1 | ||||||||||||||||||
| 857 | } | - | ||||||||||||||||||
| 858 | - | |||||||||||||||||||
| 859 | /*! | - | ||||||||||||||||||
| 860 | Closes the database connection, freeing any resources acquired, and | - | ||||||||||||||||||
| 861 | invalidating any existing QSqlQuery objects that are used with the | - | ||||||||||||||||||
| 862 | database. | - | ||||||||||||||||||
| 863 | - | |||||||||||||||||||
| 864 | This will also affect copies of this QSqlDatabase object. | - | ||||||||||||||||||
| 865 | - | |||||||||||||||||||
| 866 | \sa removeDatabase() | - | ||||||||||||||||||
| 867 | */ | - | ||||||||||||||||||
| 868 | - | |||||||||||||||||||
| 869 | void QSqlDatabase::close() | - | ||||||||||||||||||
| 870 | { | - | ||||||||||||||||||
| 871 | d->driver->close(); | - | ||||||||||||||||||
| 872 | } executed 80 times by 11 tests: end of blockExecuted by:
| 80 | ||||||||||||||||||
| 873 | - | |||||||||||||||||||
| 874 | /*! | - | ||||||||||||||||||
| 875 | Returns \c true if the database connection is currently open; | - | ||||||||||||||||||
| 876 | otherwise returns \c false. | - | ||||||||||||||||||
| 877 | */ | - | ||||||||||||||||||
| 878 | - | |||||||||||||||||||
| 879 | bool QSqlDatabase::isOpen() const | - | ||||||||||||||||||
| 880 | { | - | ||||||||||||||||||
| 881 | return d->driver->isOpen(); executed 2027 times by 9 tests: return d->driver->isOpen();Executed by:
| 2027 | ||||||||||||||||||
| 882 | } | - | ||||||||||||||||||
| 883 | - | |||||||||||||||||||
| 884 | /*! | - | ||||||||||||||||||
| 885 | Returns \c true if there was an error opening the database | - | ||||||||||||||||||
| 886 | connection; otherwise returns \c false. Error information can be | - | ||||||||||||||||||
| 887 | retrieved using the lastError() function. | - | ||||||||||||||||||
| 888 | */ | - | ||||||||||||||||||
| 889 | - | |||||||||||||||||||
| 890 | bool QSqlDatabase::isOpenError() const | - | ||||||||||||||||||
| 891 | { | - | ||||||||||||||||||
| 892 | return d->driver->isOpenError(); executed 10 times by 1 test: return d->driver->isOpenError();Executed by:
| 10 | ||||||||||||||||||
| 893 | } | - | ||||||||||||||||||
| 894 | - | |||||||||||||||||||
| 895 | /*! | - | ||||||||||||||||||
| 896 | Begins a transaction on the database if the driver supports | - | ||||||||||||||||||
| 897 | transactions. Returns \c{true} if the operation succeeded. | - | ||||||||||||||||||
| 898 | Otherwise it returns \c{false}. | - | ||||||||||||||||||
| 899 | - | |||||||||||||||||||
| 900 | \sa QSqlDriver::hasFeature(), commit(), rollback() | - | ||||||||||||||||||
| 901 | */ | - | ||||||||||||||||||
| 902 | bool QSqlDatabase::transaction() | - | ||||||||||||||||||
| 903 | { | - | ||||||||||||||||||
| 904 | if (!d->driver->hasFeature(QSqlDriver::Transactions))
| 0-5 | ||||||||||||||||||
| 905 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 906 | return d->driver->beginTransaction(); executed 5 times by 3 tests: return d->driver->beginTransaction();Executed by:
| 5 | ||||||||||||||||||
| 907 | } | - | ||||||||||||||||||
| 908 | - | |||||||||||||||||||
| 909 | /*! | - | ||||||||||||||||||
| 910 | Commits a transaction to the database if the driver supports | - | ||||||||||||||||||
| 911 | transactions and a transaction() has been started. Returns \c{true} | - | ||||||||||||||||||
| 912 | if the operation succeeded. Otherwise it returns \c{false}. | - | ||||||||||||||||||
| 913 | - | |||||||||||||||||||
| 914 | \note For some databases, the commit will fail and return \c{false} | - | ||||||||||||||||||
| 915 | if there is an \l{QSqlQuery::isActive()} {active query} using the | - | ||||||||||||||||||
| 916 | database for a \c{SELECT}. Make the query \l{QSqlQuery::isActive()} | - | ||||||||||||||||||
| 917 | {inactive} before doing the commit. | - | ||||||||||||||||||
| 918 | - | |||||||||||||||||||
| 919 | Call lastError() to get information about errors. | - | ||||||||||||||||||
| 920 | - | |||||||||||||||||||
| 921 | \sa QSqlQuery::isActive(), QSqlDriver::hasFeature(), rollback() | - | ||||||||||||||||||
| 922 | */ | - | ||||||||||||||||||
| 923 | bool QSqlDatabase::commit() | - | ||||||||||||||||||
| 924 | { | - | ||||||||||||||||||
| 925 | if (!d->driver->hasFeature(QSqlDriver::Transactions))
| 0-4 | ||||||||||||||||||
| 926 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 927 | return d->driver->commitTransaction(); executed 4 times by 3 tests: return d->driver->commitTransaction();Executed by:
| 4 | ||||||||||||||||||
| 928 | } | - | ||||||||||||||||||
| 929 | - | |||||||||||||||||||
| 930 | /*! | - | ||||||||||||||||||
| 931 | Rolls back a transaction on the database, if the driver supports | - | ||||||||||||||||||
| 932 | transactions and a transaction() has been started. Returns \c{true} | - | ||||||||||||||||||
| 933 | if the operation succeeded. Otherwise it returns \c{false}. | - | ||||||||||||||||||
| 934 | - | |||||||||||||||||||
| 935 | \note For some databases, the rollback will fail and return | - | ||||||||||||||||||
| 936 | \c{false} if there is an \l{QSqlQuery::isActive()} {active query} | - | ||||||||||||||||||
| 937 | using the database for a \c{SELECT}. Make the query | - | ||||||||||||||||||
| 938 | \l{QSqlQuery::isActive()} {inactive} before doing the rollback. | - | ||||||||||||||||||
| 939 | - | |||||||||||||||||||
| 940 | Call lastError() to get information about errors. | - | ||||||||||||||||||
| 941 | - | |||||||||||||||||||
| 942 | \sa QSqlQuery::isActive(), QSqlDriver::hasFeature(), commit() | - | ||||||||||||||||||
| 943 | */ | - | ||||||||||||||||||
| 944 | bool QSqlDatabase::rollback() | - | ||||||||||||||||||
| 945 | { | - | ||||||||||||||||||
| 946 | if (!d->driver->hasFeature(QSqlDriver::Transactions))
| 0-1 | ||||||||||||||||||
| 947 | return false; never executed: return false; | 0 | ||||||||||||||||||
| 948 | return d->driver->rollbackTransaction(); executed 1 time by 1 test: return d->driver->rollbackTransaction();Executed by:
| 1 | ||||||||||||||||||
| 949 | } | - | ||||||||||||||||||
| 950 | - | |||||||||||||||||||
| 951 | /*! | - | ||||||||||||||||||
| 952 | Sets the connection's database name to \a name. To have effect, | - | ||||||||||||||||||
| 953 | the database name must be set \e{before} the connection is | - | ||||||||||||||||||
| 954 | \l{open()} {opened}. Alternatively, you can close() the | - | ||||||||||||||||||
| 955 | connection, set the database name, and call open() again. \note | - | ||||||||||||||||||
| 956 | The \e{database name} is not the \e{connection name}. The | - | ||||||||||||||||||
| 957 | connection name must be passed to addDatabase() at connection | - | ||||||||||||||||||
| 958 | object create time. | - | ||||||||||||||||||
| 959 | - | |||||||||||||||||||
| 960 | For the QOCI (Oracle) driver, the database name is the TNS | - | ||||||||||||||||||
| 961 | Service Name. | - | ||||||||||||||||||
| 962 | - | |||||||||||||||||||
| 963 | For the QODBC driver, the \a name can either be a DSN, a DSN | - | ||||||||||||||||||
| 964 | filename (in which case the file must have a \c .dsn extension), | - | ||||||||||||||||||
| 965 | or a connection string. | - | ||||||||||||||||||
| 966 | - | |||||||||||||||||||
| 967 | For example, Microsoft Access users can use the following | - | ||||||||||||||||||
| 968 | connection string to open an \c .mdb file directly, instead of | - | ||||||||||||||||||
| 969 | having to create a DSN entry in the ODBC manager: | - | ||||||||||||||||||
| 970 | - | |||||||||||||||||||
| 971 | \snippet code/src_sql_kernel_qsqldatabase.cpp 3 | - | ||||||||||||||||||
| 972 | - | |||||||||||||||||||
| 973 | There is no default value. | - | ||||||||||||||||||
| 974 | - | |||||||||||||||||||
| 975 | \sa databaseName(), setUserName(), setPassword(), setHostName(), | - | ||||||||||||||||||
| 976 | setPort(), setConnectOptions(), open() | - | ||||||||||||||||||
| 977 | */ | - | ||||||||||||||||||
| 978 | - | |||||||||||||||||||
| 979 | void QSqlDatabase::setDatabaseName(const QString& name) | - | ||||||||||||||||||
| 980 | { | - | ||||||||||||||||||
| 981 | if (isValid())
| 0-25 | ||||||||||||||||||
| 982 | d->dbname = name; executed 25 times by 9 tests: d->dbname = name;Executed by:
| 25 | ||||||||||||||||||
| 983 | } executed 25 times by 9 tests: end of blockExecuted by:
| 25 | ||||||||||||||||||
| 984 | - | |||||||||||||||||||
| 985 | /*! | - | ||||||||||||||||||
| 986 | Sets the connection's user name to \a name. To have effect, the | - | ||||||||||||||||||
| 987 | user name must be set \e{before} the connection is \l{open()} | - | ||||||||||||||||||
| 988 | {opened}. Alternatively, you can close() the connection, set the | - | ||||||||||||||||||
| 989 | user name, and call open() again. | - | ||||||||||||||||||
| 990 | - | |||||||||||||||||||
| 991 | There is no default value. | - | ||||||||||||||||||
| 992 | - | |||||||||||||||||||
| 993 | \sa userName(), setDatabaseName(), setPassword(), setHostName(), | - | ||||||||||||||||||
| 994 | setPort(), setConnectOptions(), open() | - | ||||||||||||||||||
| 995 | */ | - | ||||||||||||||||||
| 996 | - | |||||||||||||||||||
| 997 | void QSqlDatabase::setUserName(const QString& name) | - | ||||||||||||||||||
| 998 | { | - | ||||||||||||||||||
| 999 | if (isValid())
| 0-23 | ||||||||||||||||||
| 1000 | d->uname = name; executed 23 times by 7 tests: d->uname = name;Executed by:
| 23 | ||||||||||||||||||
| 1001 | } executed 23 times by 7 tests: end of blockExecuted by:
| 23 | ||||||||||||||||||
| 1002 | - | |||||||||||||||||||
| 1003 | /*! | - | ||||||||||||||||||
| 1004 | Sets the connection's password to \a password. To have effect, the | - | ||||||||||||||||||
| 1005 | password must be set \e{before} the connection is \l{open()} | - | ||||||||||||||||||
| 1006 | {opened}. Alternatively, you can close() the connection, set the | - | ||||||||||||||||||
| 1007 | password, and call open() again. | - | ||||||||||||||||||
| 1008 | - | |||||||||||||||||||
| 1009 | There is no default value. | - | ||||||||||||||||||
| 1010 | - | |||||||||||||||||||
| 1011 | \warning This function stores the password in plain text within | - | ||||||||||||||||||
| 1012 | Qt. Use the open() call that takes a password as parameter to | - | ||||||||||||||||||
| 1013 | avoid this behavior. | - | ||||||||||||||||||
| 1014 | - | |||||||||||||||||||
| 1015 | \sa password(), setUserName(), setDatabaseName(), setHostName(), | - | ||||||||||||||||||
| 1016 | setPort(), setConnectOptions(), open() | - | ||||||||||||||||||
| 1017 | */ | - | ||||||||||||||||||
| 1018 | - | |||||||||||||||||||
| 1019 | void QSqlDatabase::setPassword(const QString& password) | - | ||||||||||||||||||
| 1020 | { | - | ||||||||||||||||||
| 1021 | if (isValid())
| 0-22 | ||||||||||||||||||
| 1022 | d->pword = password; executed 22 times by 7 tests: d->pword = password;Executed by:
| 22 | ||||||||||||||||||
| 1023 | } executed 22 times by 7 tests: end of blockExecuted by:
| 22 | ||||||||||||||||||
| 1024 | - | |||||||||||||||||||
| 1025 | /*! | - | ||||||||||||||||||
| 1026 | Sets the connection's host name to \a host. To have effect, the | - | ||||||||||||||||||
| 1027 | host name must be set \e{before} the connection is \l{open()} | - | ||||||||||||||||||
| 1028 | {opened}. Alternatively, you can close() the connection, set the | - | ||||||||||||||||||
| 1029 | host name, and call open() again. | - | ||||||||||||||||||
| 1030 | - | |||||||||||||||||||
| 1031 | There is no default value. | - | ||||||||||||||||||
| 1032 | - | |||||||||||||||||||
| 1033 | \sa hostName(), setUserName(), setPassword(), setDatabaseName(), | - | ||||||||||||||||||
| 1034 | setPort(), setConnectOptions(), open() | - | ||||||||||||||||||
| 1035 | */ | - | ||||||||||||||||||
| 1036 | - | |||||||||||||||||||
| 1037 | void QSqlDatabase::setHostName(const QString& host) | - | ||||||||||||||||||
| 1038 | { | - | ||||||||||||||||||
| 1039 | if (isValid())
| 0-23 | ||||||||||||||||||
| 1040 | d->hname = host; executed 23 times by 7 tests: d->hname = host;Executed by:
| 23 | ||||||||||||||||||
| 1041 | } executed 23 times by 7 tests: end of blockExecuted by:
| 23 | ||||||||||||||||||
| 1042 | - | |||||||||||||||||||
| 1043 | /*! | - | ||||||||||||||||||
| 1044 | Sets the connection's port number to \a port. To have effect, the | - | ||||||||||||||||||
| 1045 | port number must be set \e{before} the connection is \l{open()} | - | ||||||||||||||||||
| 1046 | {opened}. Alternatively, you can close() the connection, set the | - | ||||||||||||||||||
| 1047 | port number, and call open() again.. | - | ||||||||||||||||||
| 1048 | - | |||||||||||||||||||
| 1049 | There is no default value. | - | ||||||||||||||||||
| 1050 | - | |||||||||||||||||||
| 1051 | \sa port(), setUserName(), setPassword(), setHostName(), | - | ||||||||||||||||||
| 1052 | setDatabaseName(), setConnectOptions(), open() | - | ||||||||||||||||||
| 1053 | */ | - | ||||||||||||||||||
| 1054 | - | |||||||||||||||||||
| 1055 | void QSqlDatabase::setPort(int port) | - | ||||||||||||||||||
| 1056 | { | - | ||||||||||||||||||
| 1057 | if (isValid())
| 0-20 | ||||||||||||||||||
| 1058 | d->port = port; executed 20 times by 7 tests: d->port = port;Executed by:
| 20 | ||||||||||||||||||
| 1059 | } executed 20 times by 7 tests: end of blockExecuted by:
| 20 | ||||||||||||||||||
| 1060 | - | |||||||||||||||||||
| 1061 | /*! | - | ||||||||||||||||||
| 1062 | Returns the connection's database name, which may be empty. | - | ||||||||||||||||||
| 1063 | \note The database name is not the connection name. | - | ||||||||||||||||||
| 1064 | - | |||||||||||||||||||
| 1065 | \sa setDatabaseName() | - | ||||||||||||||||||
| 1066 | */ | - | ||||||||||||||||||
| 1067 | QString QSqlDatabase::databaseName() const | - | ||||||||||||||||||
| 1068 | { | - | ||||||||||||||||||
| 1069 | return d->dbname; executed 21 times by 5 tests: return d->dbname;Executed by:
| 21 | ||||||||||||||||||
| 1070 | } | - | ||||||||||||||||||
| 1071 | - | |||||||||||||||||||
| 1072 | /*! | - | ||||||||||||||||||
| 1073 | Returns the connection's user name; it may be empty. | - | ||||||||||||||||||
| 1074 | - | |||||||||||||||||||
| 1075 | \sa setUserName() | - | ||||||||||||||||||
| 1076 | */ | - | ||||||||||||||||||
| 1077 | QString QSqlDatabase::userName() const | - | ||||||||||||||||||
| 1078 | { | - | ||||||||||||||||||
| 1079 | return d->uname; executed 2 times by 1 test: return d->uname;Executed by:
| 2 | ||||||||||||||||||
| 1080 | } | - | ||||||||||||||||||
| 1081 | - | |||||||||||||||||||
| 1082 | /*! | - | ||||||||||||||||||
| 1083 | Returns the connection's password. If the password was not set | - | ||||||||||||||||||
| 1084 | with setPassword(), and if the password was given in the open() | - | ||||||||||||||||||
| 1085 | call, or if no password was used, an empty string is returned. | - | ||||||||||||||||||
| 1086 | */ | - | ||||||||||||||||||
| 1087 | QString QSqlDatabase::password() const | - | ||||||||||||||||||
| 1088 | { | - | ||||||||||||||||||
| 1089 | return d->pword; executed 2 times by 1 test: return d->pword;Executed by:
| 2 | ||||||||||||||||||
| 1090 | } | - | ||||||||||||||||||
| 1091 | - | |||||||||||||||||||
| 1092 | /*! | - | ||||||||||||||||||
| 1093 | Returns the connection's host name; it may be empty. | - | ||||||||||||||||||
| 1094 | - | |||||||||||||||||||
| 1095 | \sa setHostName() | - | ||||||||||||||||||
| 1096 | */ | - | ||||||||||||||||||
| 1097 | QString QSqlDatabase::hostName() const | - | ||||||||||||||||||
| 1098 | { | - | ||||||||||||||||||
| 1099 | return d->hname; executed 11564 times by 8 tests: return d->hname;Executed by:
| 11564 | ||||||||||||||||||
| 1100 | } | - | ||||||||||||||||||
| 1101 | - | |||||||||||||||||||
| 1102 | /*! | - | ||||||||||||||||||
| 1103 | Returns the connection's driver name. | - | ||||||||||||||||||
| 1104 | - | |||||||||||||||||||
| 1105 | \sa addDatabase(), driver() | - | ||||||||||||||||||
| 1106 | */ | - | ||||||||||||||||||
| 1107 | QString QSqlDatabase::driverName() const | - | ||||||||||||||||||
| 1108 | { | - | ||||||||||||||||||
| 1109 | return d->drvName; executed 35660 times by 8 tests: return d->drvName;Executed by:
| 35660 | ||||||||||||||||||
| 1110 | } | - | ||||||||||||||||||
| 1111 | - | |||||||||||||||||||
| 1112 | /*! | - | ||||||||||||||||||
| 1113 | Returns the connection's port number. The value is undefined if | - | ||||||||||||||||||
| 1114 | the port number has not been set. | - | ||||||||||||||||||
| 1115 | - | |||||||||||||||||||
| 1116 | \sa setPort() | - | ||||||||||||||||||
| 1117 | */ | - | ||||||||||||||||||
| 1118 | int QSqlDatabase::port() const | - | ||||||||||||||||||
| 1119 | { | - | ||||||||||||||||||
| 1120 | return d->port; executed 11564 times by 8 tests: return d->port;Executed by:
| 11564 | ||||||||||||||||||
| 1121 | } | - | ||||||||||||||||||
| 1122 | - | |||||||||||||||||||
| 1123 | /*! | - | ||||||||||||||||||
| 1124 | Returns the database driver used to access the database | - | ||||||||||||||||||
| 1125 | connection. | - | ||||||||||||||||||
| 1126 | - | |||||||||||||||||||
| 1127 | \sa addDatabase(), drivers() | - | ||||||||||||||||||
| 1128 | */ | - | ||||||||||||||||||
| 1129 | - | |||||||||||||||||||
| 1130 | QSqlDriver* QSqlDatabase::driver() const | - | ||||||||||||||||||
| 1131 | { | - | ||||||||||||||||||
| 1132 | return d->driver; executed 4311 times by 8 tests: return d->driver;Executed by:
| 4311 | ||||||||||||||||||
| 1133 | } | - | ||||||||||||||||||
| 1134 | - | |||||||||||||||||||
| 1135 | /*! | - | ||||||||||||||||||
| 1136 | Returns information about the last error that occurred on the | - | ||||||||||||||||||
| 1137 | database. | - | ||||||||||||||||||
| 1138 | - | |||||||||||||||||||
| 1139 | Failures that occur in conjunction with an individual query are | - | ||||||||||||||||||
| 1140 | reported by QSqlQuery::lastError(). | - | ||||||||||||||||||
| 1141 | - | |||||||||||||||||||
| 1142 | \sa QSqlError, QSqlQuery::lastError() | - | ||||||||||||||||||
| 1143 | */ | - | ||||||||||||||||||
| 1144 | - | |||||||||||||||||||
| 1145 | QSqlError QSqlDatabase::lastError() const | - | ||||||||||||||||||
| 1146 | { | - | ||||||||||||||||||
| 1147 | return d->driver->lastError(); executed 29 times by 6 tests: return d->driver->lastError();Executed by:
| 29 | ||||||||||||||||||
| 1148 | } | - | ||||||||||||||||||
| 1149 | - | |||||||||||||||||||
| 1150 | - | |||||||||||||||||||
| 1151 | /*! | - | ||||||||||||||||||
| 1152 | Returns a list of the database's tables, system tables and views, | - | ||||||||||||||||||
| 1153 | as specified by the parameter \a type. | - | ||||||||||||||||||
| 1154 | - | |||||||||||||||||||
| 1155 | \sa primaryIndex(), record() | - | ||||||||||||||||||
| 1156 | */ | - | ||||||||||||||||||
| 1157 | - | |||||||||||||||||||
| 1158 | QStringList QSqlDatabase::tables(QSql::TableType type) const | - | ||||||||||||||||||
| 1159 | { | - | ||||||||||||||||||
| 1160 | return d->driver->tables(type); executed 51 times by 8 tests: return d->driver->tables(type);Executed by:
| 51 | ||||||||||||||||||
| 1161 | } | - | ||||||||||||||||||
| 1162 | - | |||||||||||||||||||
| 1163 | /*! | - | ||||||||||||||||||
| 1164 | Returns the primary index for table \a tablename. If no primary | - | ||||||||||||||||||
| 1165 | index exists an empty QSqlIndex is returned. | - | ||||||||||||||||||
| 1166 | - | |||||||||||||||||||
| 1167 | \sa tables(), record() | - | ||||||||||||||||||
| 1168 | */ | - | ||||||||||||||||||
| 1169 | - | |||||||||||||||||||
| 1170 | QSqlIndex QSqlDatabase::primaryIndex(const QString& tablename) const | - | ||||||||||||||||||
| 1171 | { | - | ||||||||||||||||||
| 1172 | return d->driver->primaryIndex(tablename); executed 187 times by 4 tests: return d->driver->primaryIndex(tablename);Executed by:
| 187 | ||||||||||||||||||
| 1173 | } | - | ||||||||||||||||||
| 1174 | - | |||||||||||||||||||
| 1175 | - | |||||||||||||||||||
| 1176 | /*! | - | ||||||||||||||||||
| 1177 | Returns a QSqlRecord populated with the names of all the fields in | - | ||||||||||||||||||
| 1178 | the table (or view) called \a tablename. The order in which the | - | ||||||||||||||||||
| 1179 | fields appear in the record is undefined. If no such table (or | - | ||||||||||||||||||
| 1180 | view) exists, an empty record is returned. | - | ||||||||||||||||||
| 1181 | */ | - | ||||||||||||||||||
| 1182 | - | |||||||||||||||||||
| 1183 | QSqlRecord QSqlDatabase::record(const QString& tablename) const | - | ||||||||||||||||||
| 1184 | { | - | ||||||||||||||||||
| 1185 | return d->driver->record(tablename); executed 328 times by 5 tests: return d->driver->record(tablename);Executed by:
| 328 | ||||||||||||||||||
| 1186 | } | - | ||||||||||||||||||
| 1187 | - | |||||||||||||||||||
| 1188 | - | |||||||||||||||||||
| 1189 | /*! | - | ||||||||||||||||||
| 1190 | Sets database-specific \a options. This must be done before the | - | ||||||||||||||||||
| 1191 | connection is opened or it has no effect (or you can close() the | - | ||||||||||||||||||
| 1192 | connection, call this function and open() the connection again). | - | ||||||||||||||||||
| 1193 | - | |||||||||||||||||||
| 1194 | The format of the \a options string is a semicolon separated list | - | ||||||||||||||||||
| 1195 | of option names or option=value pairs. The options depend on the | - | ||||||||||||||||||
| 1196 | database client used: | - | ||||||||||||||||||
| 1197 | - | |||||||||||||||||||
| 1198 | \table | - | ||||||||||||||||||
| 1199 | \header \li ODBC \li MySQL \li PostgreSQL | - | ||||||||||||||||||
| 1200 | \row | - | ||||||||||||||||||
| 1201 | - | |||||||||||||||||||
| 1202 | \li | - | ||||||||||||||||||
| 1203 | \list | - | ||||||||||||||||||
| 1204 | \li SQL_ATTR_ACCESS_MODE | - | ||||||||||||||||||
| 1205 | \li SQL_ATTR_LOGIN_TIMEOUT | - | ||||||||||||||||||
| 1206 | \li SQL_ATTR_CONNECTION_TIMEOUT | - | ||||||||||||||||||
| 1207 | \li SQL_ATTR_CURRENT_CATALOG | - | ||||||||||||||||||
| 1208 | \li SQL_ATTR_METADATA_ID | - | ||||||||||||||||||
| 1209 | \li SQL_ATTR_PACKET_SIZE | - | ||||||||||||||||||
| 1210 | \li SQL_ATTR_TRACEFILE | - | ||||||||||||||||||
| 1211 | \li SQL_ATTR_TRACE | - | ||||||||||||||||||
| 1212 | \li SQL_ATTR_CONNECTION_POOLING | - | ||||||||||||||||||
| 1213 | \li SQL_ATTR_ODBC_VERSION | - | ||||||||||||||||||
| 1214 | \endlist | - | ||||||||||||||||||
| 1215 | - | |||||||||||||||||||
| 1216 | \li | - | ||||||||||||||||||
| 1217 | \list | - | ||||||||||||||||||
| 1218 | \li CLIENT_COMPRESS | - | ||||||||||||||||||
| 1219 | \li CLIENT_FOUND_ROWS | - | ||||||||||||||||||
| 1220 | \li CLIENT_IGNORE_SPACE | - | ||||||||||||||||||
| 1221 | \li CLIENT_ODBC | - | ||||||||||||||||||
| 1222 | \li CLIENT_NO_SCHEMA | - | ||||||||||||||||||
| 1223 | \li CLIENT_INTERACTIVE | - | ||||||||||||||||||
| 1224 | \li UNIX_SOCKET | - | ||||||||||||||||||
| 1225 | \li MYSQL_OPT_RECONNECT | - | ||||||||||||||||||
| 1226 | \li MYSQL_OPT_CONNECT_TIMEOUT | - | ||||||||||||||||||
| 1227 | \li MYSQL_OPT_READ_TIMEOUT | - | ||||||||||||||||||
| 1228 | \li MYSQL_OPT_WRITE_TIMEOUT | - | ||||||||||||||||||
| 1229 | \li SSL_KEY | - | ||||||||||||||||||
| 1230 | \li SSL_CERT | - | ||||||||||||||||||
| 1231 | \li SSL_CA | - | ||||||||||||||||||
| 1232 | \li SSL_CAPATH | - | ||||||||||||||||||
| 1233 | \li SSL_CIPHER | - | ||||||||||||||||||
| 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_OPEN_URI | - | ||||||||||||||||||
| 1271 | \li QSQLITE_ENABLE_SHARED_CACHE | - | ||||||||||||||||||
| 1272 | \endlist | - | ||||||||||||||||||
| 1273 | - | |||||||||||||||||||
| 1274 | \li | - | ||||||||||||||||||
| 1275 | \list | - | ||||||||||||||||||
| 1276 | \li ISC_DPB_LC_CTYPE | - | ||||||||||||||||||
| 1277 | \li ISC_DPB_SQL_ROLE_NAME | - | ||||||||||||||||||
| 1278 | \endlist | - | ||||||||||||||||||
| 1279 | - | |||||||||||||||||||
| 1280 | \endtable | - | ||||||||||||||||||
| 1281 | - | |||||||||||||||||||
| 1282 | Examples: | - | ||||||||||||||||||
| 1283 | \snippet code/src_sql_kernel_qsqldatabase.cpp 4 | - | ||||||||||||||||||
| 1284 | - | |||||||||||||||||||
| 1285 | Refer to the client library documentation for more information | - | ||||||||||||||||||
| 1286 | about the different options. | - | ||||||||||||||||||
| 1287 | - | |||||||||||||||||||
| 1288 | \sa connectOptions() | - | ||||||||||||||||||
| 1289 | */ | - | ||||||||||||||||||
| 1290 | - | |||||||||||||||||||
| 1291 | void QSqlDatabase::setConnectOptions(const QString &options) | - | ||||||||||||||||||
| 1292 | { | - | ||||||||||||||||||
| 1293 | if (isValid())
| 0-21 | ||||||||||||||||||
| 1294 | d->connOptions = options; executed 21 times by 7 tests: d->connOptions = options;Executed by:
| 21 | ||||||||||||||||||
| 1295 | } executed 21 times by 7 tests: end of blockExecuted by:
| 21 | ||||||||||||||||||
| 1296 | - | |||||||||||||||||||
| 1297 | /*! | - | ||||||||||||||||||
| 1298 | Returns the connection options string used for this connection. | - | ||||||||||||||||||
| 1299 | The string may be empty. | - | ||||||||||||||||||
| 1300 | - | |||||||||||||||||||
| 1301 | \sa setConnectOptions() | - | ||||||||||||||||||
| 1302 | */ | - | ||||||||||||||||||
| 1303 | QString QSqlDatabase::connectOptions() const | - | ||||||||||||||||||
| 1304 | { | - | ||||||||||||||||||
| 1305 | return d->connOptions; never executed: return d->connOptions; | 0 | ||||||||||||||||||
| 1306 | } | - | ||||||||||||||||||
| 1307 | - | |||||||||||||||||||
| 1308 | /*! | - | ||||||||||||||||||
| 1309 | Returns \c true if a driver called \a name is available; otherwise | - | ||||||||||||||||||
| 1310 | returns \c false. | - | ||||||||||||||||||
| 1311 | - | |||||||||||||||||||
| 1312 | \sa drivers() | - | ||||||||||||||||||
| 1313 | */ | - | ||||||||||||||||||
| 1314 | - | |||||||||||||||||||
| 1315 | bool QSqlDatabase::isDriverAvailable(const QString& name) | - | ||||||||||||||||||
| 1316 | { | - | ||||||||||||||||||
| 1317 | return drivers().contains(name); never executed: return drivers().contains(name); | 0 | ||||||||||||||||||
| 1318 | } | - | ||||||||||||||||||
| 1319 | - | |||||||||||||||||||
| 1320 | /*! \fn QSqlDatabase QSqlDatabase::addDatabase(QSqlDriver* driver, const QString& connectionName) | - | ||||||||||||||||||
| 1321 | - | |||||||||||||||||||
| 1322 | This overload is useful when you want to create a database | - | ||||||||||||||||||
| 1323 | connection with a \l{QSqlDriver} {driver} you instantiated | - | ||||||||||||||||||
| 1324 | yourself. It might be your own database driver, or you might just | - | ||||||||||||||||||
| 1325 | need to instantiate one of the Qt drivers yourself. If you do | - | ||||||||||||||||||
| 1326 | this, it is recommended that you include the driver code in your | - | ||||||||||||||||||
| 1327 | application. For example, you can create a PostgreSQL connection | - | ||||||||||||||||||
| 1328 | with your own QPSQL driver like this: | - | ||||||||||||||||||
| 1329 | - | |||||||||||||||||||
| 1330 | \snippet code/src_sql_kernel_qsqldatabase.cpp 5 | - | ||||||||||||||||||
| 1331 | \codeline | - | ||||||||||||||||||
| 1332 | \snippet code/src_sql_kernel_qsqldatabase.cpp 6 | - | ||||||||||||||||||
| 1333 | - | |||||||||||||||||||
| 1334 | The above code sets up a PostgreSQL connection and instantiates a | - | ||||||||||||||||||
| 1335 | QPSQLDriver object. Next, addDatabase() is called to add the | - | ||||||||||||||||||
| 1336 | connection to the known connections so that it can be used by the | - | ||||||||||||||||||
| 1337 | Qt SQL classes. When a driver is instantiated with a connection | - | ||||||||||||||||||
| 1338 | handle (or set of handles), Qt assumes that you have already | - | ||||||||||||||||||
| 1339 | opened the database connection. | - | ||||||||||||||||||
| 1340 | - | |||||||||||||||||||
| 1341 | \note We assume that \c qtdir is the directory where Qt is | - | ||||||||||||||||||
| 1342 | installed. This will pull in the code that is needed to use the | - | ||||||||||||||||||
| 1343 | PostgreSQL client library and to instantiate a QPSQLDriver object, | - | ||||||||||||||||||
| 1344 | assuming that you have the PostgreSQL headers somewhere in your | - | ||||||||||||||||||
| 1345 | include search path. | - | ||||||||||||||||||
| 1346 | - | |||||||||||||||||||
| 1347 | Remember that you must link your application against the database | - | ||||||||||||||||||
| 1348 | client library. Make sure the client library is in your linker's | - | ||||||||||||||||||
| 1349 | search path, and add lines like these to your \c{.pro} file: | - | ||||||||||||||||||
| 1350 | - | |||||||||||||||||||
| 1351 | \snippet code/src_sql_kernel_qsqldatabase.cpp 7 | - | ||||||||||||||||||
| 1352 | - | |||||||||||||||||||
| 1353 | The method described works for all the supplied drivers. The only | - | ||||||||||||||||||
| 1354 | difference will be in the driver constructor arguments. Here is a | - | ||||||||||||||||||
| 1355 | table of the drivers included with Qt, their source code files, | - | ||||||||||||||||||
| 1356 | and their constructor arguments: | - | ||||||||||||||||||
| 1357 | - | |||||||||||||||||||
| 1358 | \table | - | ||||||||||||||||||
| 1359 | \header \li Driver \li Class name \li Constructor arguments \li File to include | - | ||||||||||||||||||
| 1360 | \row | - | ||||||||||||||||||
| 1361 | \li QPSQL | - | ||||||||||||||||||
| 1362 | \li QPSQLDriver | - | ||||||||||||||||||
| 1363 | \li PGconn *connection | - | ||||||||||||||||||
| 1364 | \li \c qsql_psql.cpp | - | ||||||||||||||||||
| 1365 | \row | - | ||||||||||||||||||
| 1366 | \li QMYSQL | - | ||||||||||||||||||
| 1367 | \li QMYSQLDriver | - | ||||||||||||||||||
| 1368 | \li MYSQL *connection | - | ||||||||||||||||||
| 1369 | \li \c qsql_mysql.cpp | - | ||||||||||||||||||
| 1370 | \row | - | ||||||||||||||||||
| 1371 | \li QOCI | - | ||||||||||||||||||
| 1372 | \li QOCIDriver | - | ||||||||||||||||||
| 1373 | \li OCIEnv *environment, OCISvcCtx *serviceContext | - | ||||||||||||||||||
| 1374 | \li \c qsql_oci.cpp | - | ||||||||||||||||||
| 1375 | \row | - | ||||||||||||||||||
| 1376 | \li QODBC | - | ||||||||||||||||||
| 1377 | \li QODBCDriver | - | ||||||||||||||||||
| 1378 | \li SQLHANDLE environment, SQLHANDLE connection | - | ||||||||||||||||||
| 1379 | \li \c qsql_odbc.cpp | - | ||||||||||||||||||
| 1380 | \row | - | ||||||||||||||||||
| 1381 | \li QDB2 | - | ||||||||||||||||||
| 1382 | \li QDB2 | - | ||||||||||||||||||
| 1383 | \li SQLHANDLE environment, SQLHANDLE connection | - | ||||||||||||||||||
| 1384 | \li \c qsql_db2.cpp | - | ||||||||||||||||||
| 1385 | \row | - | ||||||||||||||||||
| 1386 | \li QTDS | - | ||||||||||||||||||
| 1387 | \li QTDSDriver | - | ||||||||||||||||||
| 1388 | \li LOGINREC *loginRecord, DBPROCESS *dbProcess, const QString &hostName | - | ||||||||||||||||||
| 1389 | \li \c qsql_tds.cpp | - | ||||||||||||||||||
| 1390 | \row | - | ||||||||||||||||||
| 1391 | \li QSQLITE | - | ||||||||||||||||||
| 1392 | \li QSQLiteDriver | - | ||||||||||||||||||
| 1393 | \li sqlite *connection | - | ||||||||||||||||||
| 1394 | \li \c qsql_sqlite.cpp | - | ||||||||||||||||||
| 1395 | \row | - | ||||||||||||||||||
| 1396 | \li QIBASE | - | ||||||||||||||||||
| 1397 | \li QIBaseDriver | - | ||||||||||||||||||
| 1398 | \li isc_db_handle connection | - | ||||||||||||||||||
| 1399 | \li \c qsql_ibase.cpp | - | ||||||||||||||||||
| 1400 | \endtable | - | ||||||||||||||||||
| 1401 | - | |||||||||||||||||||
| 1402 | The host name (or service name) is needed when constructing the | - | ||||||||||||||||||
| 1403 | QTDSDriver for creating new connections for internal queries. This | - | ||||||||||||||||||
| 1404 | is to prevent blocking when several QSqlQuery objects are used | - | ||||||||||||||||||
| 1405 | simultaneously. | - | ||||||||||||||||||
| 1406 | - | |||||||||||||||||||
| 1407 | \warning Adding a database connection with the same connection | - | ||||||||||||||||||
| 1408 | name as an existing connection, causes the existing connection to | - | ||||||||||||||||||
| 1409 | be replaced by the new one. | - | ||||||||||||||||||
| 1410 | - | |||||||||||||||||||
| 1411 | \warning The SQL framework takes ownership of the \a driver. It | - | ||||||||||||||||||
| 1412 | must not be deleted. To remove the connection, use | - | ||||||||||||||||||
| 1413 | removeDatabase(). | - | ||||||||||||||||||
| 1414 | - | |||||||||||||||||||
| 1415 | \sa drivers() | - | ||||||||||||||||||
| 1416 | */ | - | ||||||||||||||||||
| 1417 | QSqlDatabase QSqlDatabase::addDatabase(QSqlDriver* driver, const QString& connectionName) | - | ||||||||||||||||||
| 1418 | { | - | ||||||||||||||||||
| 1419 | QSqlDatabase db(driver); | - | ||||||||||||||||||
| 1420 | QSqlDatabasePrivate::addDatabase(db, connectionName); | - | ||||||||||||||||||
| 1421 | return db; never executed: return db; | 0 | ||||||||||||||||||
| 1422 | } | - | ||||||||||||||||||
| 1423 | - | |||||||||||||||||||
| 1424 | /*! | - | ||||||||||||||||||
| 1425 | Returns \c true if the QSqlDatabase has a valid driver. | - | ||||||||||||||||||
| 1426 | - | |||||||||||||||||||
| 1427 | Example: | - | ||||||||||||||||||
| 1428 | \snippet code/src_sql_kernel_qsqldatabase.cpp 8 | - | ||||||||||||||||||
| 1429 | */ | - | ||||||||||||||||||
| 1430 | bool QSqlDatabase::isValid() const | - | ||||||||||||||||||
| 1431 | { | - | ||||||||||||||||||
| 1432 | return d->driver && d->driver != d->shared_null()->driver; executed 6783 times by 9 tests: return d->driver && d->driver != d->shared_null()->driver;Executed by:
| 6783 | ||||||||||||||||||
| 1433 | } | - | ||||||||||||||||||
| 1434 | - | |||||||||||||||||||
| 1435 | /*! | - | ||||||||||||||||||
| 1436 | Clones the database connection \a other and stores it as \a | - | ||||||||||||||||||
| 1437 | connectionName. All the settings from the original database, e.g. | - | ||||||||||||||||||
| 1438 | databaseName(), hostName(), etc., are copied across. Does nothing | - | ||||||||||||||||||
| 1439 | if \a other is an invalid database. Returns the newly created | - | ||||||||||||||||||
| 1440 | database connection. | - | ||||||||||||||||||
| 1441 | - | |||||||||||||||||||
| 1442 | \note The new connection has not been opened. Before using the new | - | ||||||||||||||||||
| 1443 | connection, you must call open(). | - | ||||||||||||||||||
| 1444 | */ | - | ||||||||||||||||||
| 1445 | QSqlDatabase QSqlDatabase::cloneDatabase(const QSqlDatabase &other, const QString &connectionName) | - | ||||||||||||||||||
| 1446 | { | - | ||||||||||||||||||
| 1447 | if (!other.isValid())
| 0-7 | ||||||||||||||||||
| 1448 | return QSqlDatabase(); never executed: return QSqlDatabase(); | 0 | ||||||||||||||||||
| 1449 | - | |||||||||||||||||||
| 1450 | QSqlDatabase db(other.driverName()); | - | ||||||||||||||||||
| 1451 | db.d->copy(other.d); | - | ||||||||||||||||||
| 1452 | QSqlDatabasePrivate::addDatabase(db, connectionName); | - | ||||||||||||||||||
| 1453 | return db; executed 7 times by 3 tests: return db;Executed by:
| 7 | ||||||||||||||||||
| 1454 | } | - | ||||||||||||||||||
| 1455 | - | |||||||||||||||||||
| 1456 | /*! | - | ||||||||||||||||||
| 1457 | \since 4.4 | - | ||||||||||||||||||
| 1458 | - | |||||||||||||||||||
| 1459 | Returns the connection name, which may be empty. \note The | - | ||||||||||||||||||
| 1460 | connection name is not the \l{databaseName()} {database name}. | - | ||||||||||||||||||
| 1461 | - | |||||||||||||||||||
| 1462 | \sa addDatabase() | - | ||||||||||||||||||
| 1463 | */ | - | ||||||||||||||||||
| 1464 | QString QSqlDatabase::connectionName() const | - | ||||||||||||||||||
| 1465 | { | - | ||||||||||||||||||
| 1466 | return d->connName; executed 4 times by 1 test: return d->connName;Executed by:
| 4 | ||||||||||||||||||
| 1467 | } | - | ||||||||||||||||||
| 1468 | - | |||||||||||||||||||
| 1469 | /*! | - | ||||||||||||||||||
| 1470 | \since 4.6 | - | ||||||||||||||||||
| 1471 | - | |||||||||||||||||||
| 1472 | Sets the default numerical precision policy used by queries created | - | ||||||||||||||||||
| 1473 | on this database connection to \a precisionPolicy. | - | ||||||||||||||||||
| 1474 | - | |||||||||||||||||||
| 1475 | Note: Drivers that don't support fetching numerical values with low | - | ||||||||||||||||||
| 1476 | precision will ignore the precision policy. You can use | - | ||||||||||||||||||
| 1477 | QSqlDriver::hasFeature() to find out whether a driver supports this | - | ||||||||||||||||||
| 1478 | feature. | - | ||||||||||||||||||
| 1479 | - | |||||||||||||||||||
| 1480 | Note: Setting the default precision policy to \a precisionPolicy | - | ||||||||||||||||||
| 1481 | doesn't affect any currently active queries. | - | ||||||||||||||||||
| 1482 | - | |||||||||||||||||||
| 1483 | \sa QSql::NumericalPrecisionPolicy, numericalPrecisionPolicy(), | - | ||||||||||||||||||
| 1484 | QSqlQuery::setNumericalPrecisionPolicy(), QSqlQuery::numericalPrecisionPolicy() | - | ||||||||||||||||||
| 1485 | */ | - | ||||||||||||||||||
| 1486 | void QSqlDatabase::setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy) | - | ||||||||||||||||||
| 1487 | { | - | ||||||||||||||||||
| 1488 | if(driver())
| 0-2 | ||||||||||||||||||
| 1489 | driver()->setNumericalPrecisionPolicy(precisionPolicy); executed 2 times by 1 test: driver()->setNumericalPrecisionPolicy(precisionPolicy);Executed by:
| 2 | ||||||||||||||||||
| 1490 | d->precisionPolicy = precisionPolicy; | - | ||||||||||||||||||
| 1491 | } executed 2 times by 1 test: end of blockExecuted by:
| 2 | ||||||||||||||||||
| 1492 | - | |||||||||||||||||||
| 1493 | /*! | - | ||||||||||||||||||
| 1494 | \since 4.6 | - | ||||||||||||||||||
| 1495 | - | |||||||||||||||||||
| 1496 | Returns the current default precision policy for the database connection. | - | ||||||||||||||||||
| 1497 | - | |||||||||||||||||||
| 1498 | \sa QSql::NumericalPrecisionPolicy, setNumericalPrecisionPolicy(), | - | ||||||||||||||||||
| 1499 | QSqlQuery::numericalPrecisionPolicy(), QSqlQuery::setNumericalPrecisionPolicy() | - | ||||||||||||||||||
| 1500 | */ | - | ||||||||||||||||||
| 1501 | QSql::NumericalPrecisionPolicy QSqlDatabase::numericalPrecisionPolicy() const | - | ||||||||||||||||||
| 1502 | { | - | ||||||||||||||||||
| 1503 | if(driver())
| 0-1 | ||||||||||||||||||
| 1504 | return driver()->numericalPrecisionPolicy(); executed 1 time by 1 test: return driver()->numericalPrecisionPolicy();Executed by:
| 1 | ||||||||||||||||||
| 1505 | else | - | ||||||||||||||||||
| 1506 | return d->precisionPolicy; never executed: return d->precisionPolicy; | 0 | ||||||||||||||||||
| 1507 | } | - | ||||||||||||||||||
| 1508 | - | |||||||||||||||||||
| 1509 | - | |||||||||||||||||||
| 1510 | #ifndef QT_NO_DEBUG_STREAM | - | ||||||||||||||||||
| 1511 | QDebug operator<<(QDebug dbg, const QSqlDatabase &d) | - | ||||||||||||||||||
| 1512 | { | - | ||||||||||||||||||
| 1513 | QDebugStateSaver saver(dbg); | - | ||||||||||||||||||
| 1514 | dbg.nospace(); | - | ||||||||||||||||||
| 1515 | dbg.noquote(); | - | ||||||||||||||||||
| 1516 | if (!d.isValid()) {
| 0 | ||||||||||||||||||
| 1517 | dbg << "QSqlDatabase(invalid)"; | - | ||||||||||||||||||
| 1518 | return dbg; never executed: return dbg; | 0 | ||||||||||||||||||
| 1519 | } | - | ||||||||||||||||||
| 1520 | - | |||||||||||||||||||
| 1521 | dbg << "QSqlDatabase(driver=\"" << d.driverName() << "\", database=\"" | - | ||||||||||||||||||
| 1522 | << d.databaseName() << "\", host=\"" << d.hostName() << "\", port=" << d.port() | - | ||||||||||||||||||
| 1523 | << ", user=\"" << d.userName() << "\", open=" << d.isOpen() << ')'; | - | ||||||||||||||||||
| 1524 | return dbg; never executed: return dbg; | 0 | ||||||||||||||||||
| 1525 | } | - | ||||||||||||||||||
| 1526 | #endif | - | ||||||||||||||||||
| 1527 | - | |||||||||||||||||||
| 1528 | QT_END_NAMESPACE | - | ||||||||||||||||||
| Source code | Switch to Preprocessed file |