Qt 4.8
qsqldatabase.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSql module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "qsqldatabase.h"
43 #include "qsqlquery.h"
44 
45 #ifdef Q_OS_WIN32
46 // Conflicting declarations of LPCBYTE in sqlfront.h and winscard.h
47 #define _WINSCARD_H_
48 #endif
49 
50 #ifdef QT_SQL_PSQL
51 #include "../drivers/psql/qsql_psql.h"
52 #endif
53 #ifdef QT_SQL_MYSQL
54 #include "../drivers/mysql/qsql_mysql.h"
55 #endif
56 #ifdef QT_SQL_ODBC
57 #include "../drivers/odbc/qsql_odbc.h"
58 #endif
59 #ifdef QT_SQL_OCI
60 #include "../drivers/oci/qsql_oci.h"
61 #endif
62 #ifdef QT_SQL_TDS
63 // conflicting RETCODE typedef between odbc and freetds
64 #define RETCODE DBRETCODE
65 #include "../drivers/tds/qsql_tds.h"
66 #undef RETCODE
67 #endif
68 #ifdef QT_SQL_DB2
69 #include "../drivers/db2/qsql_db2.h"
70 #endif
71 #ifdef QT_SQL_SQLITE
72 #include "../drivers/sqlite/qsql_sqlite.h"
73 #endif
74 #ifdef QT_SQL_SQLITE2
75 #include "../drivers/sqlite2/qsql_sqlite2.h"
76 #endif
77 #ifdef QT_SQL_IBASE
78 #undef SQL_FLOAT // avoid clash with ODBC
79 #undef SQL_DOUBLE
80 #undef SQL_TIMESTAMP
81 #undef SQL_TYPE_TIME
82 #undef SQL_TYPE_DATE
83 #undef SQL_DATE
84 #define SCHAR IBASE_SCHAR // avoid clash with ODBC (older versions of ibase.h with Firebird)
85 #include "../drivers/ibase/qsql_ibase.h"
86 #undef SCHAR
87 #endif
88 
89 #include "qdebug.h"
90 #include "qcoreapplication.h"
91 #include "qreadwritelock.h"
92 #include "qsqlresult.h"
93 #include "qsqldriver.h"
94 #include "qsqldriverplugin.h"
95 #include "qsqlindex.h"
96 #include "private/qfactoryloader_p.h"
97 #include "private/qsqlnulldriver_p.h"
98 #include "qmutex.h"
99 #include "qhash.h"
100 #include <stdlib.h>
101 
103 
104 #ifndef QT_NO_LIBRARY
107  QLatin1String("/sqldrivers")))
108 #endif
109 
110 QT_STATIC_CONST_IMPL char *QSqlDatabase::defaultConnection = "qt_sql_default_connection";
111 
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);
121  }
122  inline QStringList keys_ts() const
123  {
124  QReadLocker locker(&lock);
125  return keys();
126  }
127 
129 };
131 
133 {
134 public:
136  q(d),
137  driver(dr),
138  port(-1)
139  {
140  ref = 1;
141  precisionPolicy= QSql::LowPrecisionDouble;
142  }
143  QSqlDatabasePrivate(const QSqlDatabasePrivate &other);
144  ~QSqlDatabasePrivate();
145  void init(const QString& type);
146  void copy(const QSqlDatabasePrivate *other);
147  void disable();
148 
157  int port;
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 
172 {
173  ref = 1;
174  q = other.q;
175  dbname = other.dbname;
176  uname = other.uname;
177  pword = other.pword;
178  hname = other.hname;
179  drvName = other.drvName;
180  port = other.port;
181  connOptions = other.connOptions;
182  driver = other.driver;
183  precisionPolicy = other.precisionPolicy;
184 }
185 
187 {
188  if (driver != shared_null()->driver)
189  delete driver;
190 }
191 
193 {
194  QConnectionDict *dict = dbDict();
195  Q_ASSERT(dict);
196  QWriteLocker locker(&dict->lock);
197 
199  while (it != dict->end()) {
200  invalidateDb(it.value(), it.key(), false);
201  ++it;
202  }
203  dict->clear();
204 }
205 
206 static bool qDriverDictInit = false;
207 static void cleanDriverDict()
208 {
212  qDriverDictInit = false;
213 }
214 
216 {
217  static DriverDict dict;
218  if (!qDriverDictInit) {
219  qDriverDictInit = true;
221  }
222  return dict;
223 }
224 
226 {
227  static QSqlNullDriver dr;
228  static QSqlDatabasePrivate n(NULL, &dr);
229  return &n;
230 }
231 
232 void QSqlDatabasePrivate::invalidateDb(const QSqlDatabase &db, const QString &name, bool doWarn)
233 {
234  if (db.d->ref != 1 && doWarn) {
235  qWarning("QSqlDatabasePrivate::removeDatabase: connection '%s' is still in use, "
236  "all queries will cease to work.", name.toLocal8Bit().constData());
237  db.d->disable();
238  db.d->connName.clear();
239  }
240 }
241 
243 {
244  QConnectionDict *dict = dbDict();
245  Q_ASSERT(dict);
246  QWriteLocker locker(&dict->lock);
247 
248  if (!dict->contains(name))
249  return;
250 
251  invalidateDb(dict->take(name), name);
252 }
253 
255 {
256  QConnectionDict *dict = dbDict();
257  Q_ASSERT(dict);
258  QWriteLocker locker(&dict->lock);
259 
260  if (dict->contains(name)) {
261  invalidateDb(dict->take(name), name);
262  qWarning("QSqlDatabasePrivate::addDatabase: duplicate connection name '%s', old "
263  "connection removed.", name.toLocal8Bit().data());
264  }
265  dict->insert(name, db);
266  db.d->connName = name;
267 }
268 
272 {
273  const QConnectionDict *dict = dbDict();
274  Q_ASSERT(dict);
275 
276  dict->lock.lockForRead();
277  QSqlDatabase db = dict->value(name);
278  dict->lock.unlock();
279  if (db.isValid() && !db.isOpen() && open) {
280  if (!db.open())
281  qWarning() << "QSqlDatabasePrivate::database: unable to open database:" << db.lastError().text();
282 
283  }
284  return db;
285 }
286 
287 
295 {
296  q = other->q;
297  dbname = other->dbname;
298  uname = other->uname;
299  pword = other->pword;
300  hname = other->hname;
301  drvName = other->drvName;
302  port = other->port;
303  connOptions = other->connOptions;
304  precisionPolicy = other->precisionPolicy;
305 }
306 
308 {
309  if (driver != shared_null()->driver) {
310  delete driver;
311  driver = shared_null()->driver;
312  }
313 }
314 
496 {
497  QSqlDatabase db(type);
498  QSqlDatabasePrivate::addDatabase(db, connectionName);
499  return db;
500 }
501 
519 QSqlDatabase QSqlDatabase::database(const QString& connectionName, bool open)
520 {
521  return QSqlDatabasePrivate::database(connectionName, open);
522 }
523 
554 void QSqlDatabase::removeDatabase(const QString& connectionName)
555 {
556  QSqlDatabasePrivate::removeDatabase(connectionName);
557 }
558 
566 {
567  QStringList list;
568 
569 #ifdef QT_SQL_PSQL
570  list << QLatin1String("QPSQL7");
571  list << QLatin1String("QPSQL");
572 #endif
573 #ifdef QT_SQL_MYSQL
574  list << QLatin1String("QMYSQL3");
575  list << QLatin1String("QMYSQL");
576 #endif
577 #ifdef QT_SQL_ODBC
578  list << QLatin1String("QODBC3");
579  list << QLatin1String("QODBC");
580 #endif
581 #ifdef QT_SQL_OCI
582  list << QLatin1String("QOCI8");
583  list << QLatin1String("QOCI");
584 #endif
585 #ifdef QT_SQL_TDS
586  list << QLatin1String("QTDS7");
587  list << QLatin1String("QTDS");
588 #endif
589 #ifdef QT_SQL_DB2
590  list << QLatin1String("QDB2");
591 #endif
592 #ifdef QT_SQL_SQLITE
593  list << QLatin1String("QSQLITE");
594 #endif
595 #ifdef QT_SQL_SQLITE2
596  list << QLatin1String("QSQLITE2");
597 #endif
598 #ifdef QT_SQL_IBASE
599  list << QLatin1String("QIBASE");
600 #endif
601 
602 #ifndef QT_NO_LIBRARY
603  if (QFactoryLoader *fl = loader()) {
604  QStringList keys = fl->keys();
605  for (QStringList::const_iterator i = keys.constBegin(); i != keys.constEnd(); ++i) {
606  if (!list.contains(*i))
607  list << *i;
608  }
609  }
610 #endif
611 
612  DriverDict dict = QSqlDatabasePrivate::driverDict();
613  for (DriverDict::const_iterator i = dict.constBegin(); i != dict.constEnd(); ++i) {
614  if (!list.contains(i.key()))
615  list << i.key();
616  }
617 
618  return list;
619 }
620 
635 {
636  delete QSqlDatabasePrivate::driverDict().take(name);
637  if (creator)
638  QSqlDatabasePrivate::driverDict().insert(name, creator);
639 }
640 
653 bool QSqlDatabase::contains(const QString& connectionName)
654 {
655  return dbDict()->contains_ts(connectionName);
656 }
657 
669 {
670  return dbDict()->keys_ts();
671 }
672 
705 {
706  d = new QSqlDatabasePrivate(this);
707  d->init(type);
708 }
709 
720 {
721  d = new QSqlDatabasePrivate(this, driver);
722 }
723 
730 {
732  d->ref.ref();
733 }
734 
739 {
740  d = other.d;
741  d->ref.ref();
742 }
743 
748 {
749  qAtomicAssign(d, other.d);
750  return *this;
751 }
752 
763 {
764  drvName = type;
765 
766  if (!driver) {
767 #ifdef QT_SQL_PSQL
768  if (type == QLatin1String("QPSQL") || type == QLatin1String("QPSQL7"))
769  driver = new QPSQLDriver();
770 #endif
771 #ifdef QT_SQL_MYSQL
772  if (type == QLatin1String("QMYSQL") || type == QLatin1String("QMYSQL3"))
773  driver = new QMYSQLDriver();
774 #endif
775 #ifdef QT_SQL_ODBC
776  if (type == QLatin1String("QODBC") || type == QLatin1String("QODBC3"))
777  driver = new QODBCDriver();
778 #endif
779 #ifdef QT_SQL_OCI
780  if (type == QLatin1String("QOCI") || type == QLatin1String("QOCI8"))
781  driver = new QOCIDriver();
782 #endif
783 #ifdef QT_SQL_TDS
784  if (type == QLatin1String("QTDS") || type == QLatin1String("QTDS7"))
785  driver = new QTDSDriver();
786 #endif
787 #ifdef QT_SQL_DB2
788  if (type == QLatin1String("QDB2"))
789  driver = new QDB2Driver();
790 #endif
791 #ifdef QT_SQL_SQLITE
792  if (type == QLatin1String("QSQLITE"))
793  driver = new QSQLiteDriver();
794 #endif
795 #ifdef QT_SQL_SQLITE2
796  if (type == QLatin1String("QSQLITE2"))
797  driver = new QSQLite2Driver();
798 #endif
799 #ifdef QT_SQL_IBASE
800  if (type == QLatin1String("QIBASE"))
801  driver = new QIBaseDriver();
802 #endif
803  }
804 
805  if (!driver) {
806  DriverDict dict = QSqlDatabasePrivate::driverDict();
807  for (DriverDict::const_iterator it = dict.constBegin();
808  it != dict.constEnd() && !driver; ++it) {
809  if (type == it.key()) {
810  driver = ((QSqlDriverCreatorBase*)(*it))->createObject();
811  }
812  }
813  }
814 
815 #ifndef QT_NO_LIBRARY
816  if (!driver && loader()) {
817  if (QSqlDriverFactoryInterface *factory = qobject_cast<QSqlDriverFactoryInterface*>(loader()->instance(type)))
818  driver = factory->create(type);
819  }
820 #endif // QT_NO_LIBRARY
821 
822  if (!driver) {
823  qWarning("QSqlDatabase: %s driver not loaded", type.toLatin1().data());
824  qWarning("QSqlDatabase: available drivers: %s",
825  QSqlDatabase::drivers().join(QLatin1String(" ")).toLatin1().data());
826  if (QCoreApplication::instance() == 0)
827  qWarning("QSqlDatabase: an instance of QCoreApplication is required for loading driver plugins");
828  driver = shared_null()->driver;
829  }
830 }
831 
839 {
840  if (!d->ref.deref()) {
841  close();
842  delete d;
843  }
844 }
845 
856 {
857  QSqlQuery r(d->driver->createResult());
858  if (!query.isEmpty()) {
859  r.exec(query);
860  d->driver->setLastError(r.lastError());
861  }
862  return r;
863 }
864 
875 {
876  return d->driver->open(d->dbname, d->uname, d->pword, d->hname,
877  d->port, d->connOptions);
878 }
879 
897 bool QSqlDatabase::open(const QString& user, const QString& password)
898 {
899  setUserName(user);
900  return d->driver->open(d->dbname, user, password, d->hname,
901  d->port, d->connOptions);
902 }
903 
915 {
916  d->driver->close();
917 }
918 
925 {
926  return d->driver->isOpen();
927 }
928 
936 {
937  return d->driver->isOpenError();
938 }
939 
948 {
949  if (!d->driver->hasFeature(QSqlDriver::Transactions))
950  return false;
951  return d->driver->beginTransaction();
952 }
953 
968 {
969  if (!d->driver->hasFeature(QSqlDriver::Transactions))
970  return false;
971  return d->driver->commitTransaction();
972 }
973 
989 {
990  if (!d->driver->hasFeature(QSqlDriver::Transactions))
991  return false;
992  return d->driver->rollbackTransaction();
993 }
994 
1024 {
1025  if (isValid())
1026  d->dbname = name;
1027 }
1028 
1041 {
1042  if (isValid())
1043  d->uname = name;
1044 }
1045 
1061 void QSqlDatabase::setPassword(const QString& password)
1062 {
1063  if (isValid())
1064  d->pword = password;
1065 }
1066 
1079 {
1080  if (isValid())
1081  d->hname = host;
1082 }
1083 
1096 {
1097  if (isValid())
1098  d->port = port;
1099 }
1100 
1108 {
1109  return d->dbname;
1110 }
1111 
1118 {
1119  return d->uname;
1120 }
1121 
1128 {
1129  return d->pword;
1130 }
1131 
1138 {
1139  return d->hname;
1140 }
1141 
1148 {
1149  return d->drvName;
1150 }
1151 
1159 {
1160  return d->port;
1161 }
1162 
1171 {
1172  return d->driver;
1173 }
1174 
1186 {
1187  return d->driver->lastError();
1188 }
1189 
1190 
1199 {
1200  return d->driver->tables(type);
1201 }
1202 
1211 {
1212  return d->driver->primaryIndex(tablename);
1213 }
1214 
1215 
1223 QSqlRecord QSqlDatabase::record(const QString& tablename) const
1224 {
1225  return d->driver->record(tablename);
1226 }
1227 
1228 
1316 {
1317  if (isValid())
1318  d->connOptions = options;
1319 }
1320 
1328 {
1329  return d->connOptions;
1330 }
1331 
1340 {
1341  return drivers().contains(name);
1342 }
1343 
1442 {
1443  QSqlDatabase db(driver);
1444  QSqlDatabasePrivate::addDatabase(db, connectionName);
1445  return db;
1446 }
1447 
1455 {
1456  return d->driver && d->driver != d->shared_null()->driver;
1457 }
1458 
1459 #ifdef QT3_SUPPORT
1460 
1463 QSqlRecord QSqlDatabase::record(const QSqlQuery& query) const
1464 { return query.record(); }
1465 
1469 QSqlRecord QSqlDatabase::recordInfo(const QSqlQuery& query) const
1470 { return query.record(); }
1471 
1480 #endif
1481 
1492 QSqlDatabase QSqlDatabase::cloneDatabase(const QSqlDatabase &other, const QString &connectionName)
1493 {
1494  if (!other.isValid())
1495  return QSqlDatabase();
1496 
1497  QSqlDatabase db(other.driverName());
1498  db.d->copy(other.d);
1499  QSqlDatabasePrivate::addDatabase(db, connectionName);
1500  return db;
1501 }
1502 
1515 {
1516  return d->connName;
1517 }
1518 
1540 {
1541  if(driver())
1542  driver()->setNumericalPrecisionPolicy(precisionPolicy);
1543  d->precisionPolicy = precisionPolicy;
1544 }
1545 
1558 {
1559  if(driver())
1560  return driver()->numericalPrecisionPolicy();
1561  else
1562  return d->precisionPolicy;
1563 }
1564 
1565 
1566 #ifndef QT_NO_DEBUG_STREAM
1568 {
1569  if (!d.isValid()) {
1570  dbg.nospace() << "QSqlDatabase(invalid)";
1571  return dbg.space();
1572  }
1573 
1574  dbg.nospace() << "QSqlDatabase(driver=\"" << d.driverName() << "\", database=\""
1575  << d.databaseName() << "\", host=\"" << d.hostName() << "\", port=" << d.port()
1576  << ", user=\"" << d.userName() << "\", open=" << d.isOpen() << ")";
1577  return dbg.space();
1578 }
1579 #endif
1580 
The QSqlError class provides SQL database error information.
Definition: qsqlerror.h:53
The QSqlIndex class provides functions to manipulate and describe database indexes.
Definition: qsqlindex.h:55
QString userName() const
Returns the connection&#39;s user name; it may be empty.
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
void copy(const QSqlDatabasePrivate *other)
Copies the connection data from other.
void close()
Closes the database connection, freeing any resources acquired, and invalidating any existing QSqlQue...
QString text() const
This is a convenience function that returns databaseText() and driverText() concatenated into a singl...
Definition: qsqlerror.cpp:237
NumericalPrecisionPolicy
Definition: qsql.h:82
bool isOpen() const
Returns true if the database connection is currently open; otherwise returns false.
QBasicAtomicInt ref
Definition: qhash.h:121
int type
Definition: qmetatype.cpp:239
static void removeDatabase(const QString &name)
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
QString connectionName() const
Returns the connection name, which may be empty.
void clear()
Removes all items from the hash.
Definition: qhash.h:574
bool commit()
Commits a transaction to the database if the driver supports transactions and a transaction() has bee...
char * data()
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:429
bool transaction()
Begins a transaction on the database if the driver supports transactions.
QReadWriteLock lock
void unlock()
Unlocks the lock.
The QAtomicInt class provides platform-independent atomic operations on integers. ...
Definition: qatomic.h:55
#define it(className, varName)
void qAddPostRoutine(QtCleanUpFunction p)
static QStringList drivers()
Returns a list of all the available database drivers.
static QStringList connectionNames()
Returns a list containing the names of all connections.
QDebug & nospace()
Clears the stream&#39;s internal flag that records whether the last character was a space and returns a r...
Definition: qdebug.h:92
void init(const QString &type)
Create the actual driver instance type.
QSqlDatabasePrivate * d
Definition: qsqldatabase.h:150
QSqlRecord record() const
Returns a QSqlRecord containing the field information for the current query.
Definition: qsqlquery.cpp:858
bool isValid() const
Returns true if the QSqlDatabase has a valid driver.
The QSqlQuery class provides a means of executing and manipulating SQL statements.
Definition: qsqlquery.h:63
QSqlDatabase()
Creates an empty, invalid QSqlDatabase object.
The QSqlDatabase class represents a connection to a database.
Definition: qsqldatabase.h:78
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the list.
Definition: qlist.h:269
#define QT_STATIC_CONST_IMPL
Definition: qglobal.h:1041
QSql::NumericalPrecisionPolicy precisionPolicy
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
void setPort(int p)
Sets the connection&#39;s port number to port.
Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, loader,(QSqlDriverFactoryInterface_iid, QLatin1String("/sqldrivers"))) QT_STATIC_CONST_IMPL char *QSqlDatabase typedef QHash< QString, QSqlDriverCreatorBase * > DriverDict
QSqlDatabase * q
The QSqlRecord class encapsulates a database record.
Definition: qsqlrecord.h:58
The QSqlDriver class is an abstract base class for accessing specific SQL databases.
Definition: qsqldriver.h:68
The QString class provides a Unicode character string.
Definition: qstring.h:83
T take(const Key &key)
Removes the item with the key from the hash and returns the value associated with it...
Definition: qhash.h:807
The QHash class is a template class that provides a hash-table-based dictionary.
Definition: qdatastream.h:66
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
QString connectOptions() const
Returns the connection options string used for this connection.
~QSqlDatabase()
Destroys the object and frees any allocated resources.
bool contains_ts(const QString &key)
QDataStream & operator<<(QDataStream &out, const QHash< QString, QSqlDatabase > &hash)
Writes the hash hash to stream out.
Definition: qdatastream.h:376
static QSqlDatabasePrivate * shared_null()
bool contains(const QString &key) const
Returns true if the hash contains an item with the key; otherwise returns false.
Definition: qhash.h:872
QSqlDriver * driver() const
Returns the database driver used to access the database connection.
static bool contains(const QString &connectionName=QLatin1String(defaultConnection))
Returns true if the list of database connections contains connectionName; otherwise returns false...
const T value(const Key &key) const
Returns the value associated with the key.
Definition: qhash.h:606
static QSqlDatabase cloneDatabase(const QSqlDatabase &other, const QString &connectionName)
Clones the database connection other and and stores it as connectionName.
QSqlIndex primaryIndex(const QString &tablename) const
Returns the primary index for table tablename.
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition: qhash.h:753
static bool isDriverAvailable(const QString &name)
Returns true if a driver called name is available; otherwise returns false.
int port() const
Returns the connection&#39;s port number.
static void removeDatabase(const QString &connectionName)
Removes the database connection connectionName from the list of database connections.
static QSqlDatabase database(const QString &name, bool open)
friend class const_iterator
Definition: qlist.h:264
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
QSqlRecord record(const QString &tablename) const
Returns a QSqlRecord populated with the names of all the fields in the table (or view) called tablena...
#define QSqlDriverFactoryInterface_iid
QSql::NumericalPrecisionPolicy numericalPrecisionPolicy() const
Returns the current default precision policy for the database connection.
QSqlError lastError() const
Returns information about the last error that occurred on the database.
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static bool init
const char * name
static QSqlDatabase database(const QString &connectionName=QLatin1String(defaultConnection), bool open=true)
Returns the database connection called connectionName.
#define Q_GLOBAL_STATIC(TYPE, NAME)
Declares a global static variable with the given type and name.
Definition: qglobal.h:1968
void setConnectOptions(const QString &options=QString())
Sets database-specific options.
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
void setPassword(const QString &password)
Sets the connection&#39;s password to password.
Q_CORE_EXPORT void qWarning(const char *,...)
void lockForRead()
Locks the lock for reading.
static const char * data(const QByteArray &arr)
TableType
Definition: qsql.h:74
QBool contains(const QString &str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the list contains the string str; otherwise returns false.
Definition: qstringlist.h:172
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
Definition: qstring.cpp:3993
The QReadLocker class is a convenience class that simplifies locking and unlocking read-write locks f...
static void addDatabase(const QSqlDatabase &db, const QString &name)
static QHashData shared_null
Definition: qhash.h:151
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
Definition: qstring.cpp:4049
QString databaseName() const
Returns the connection&#39;s database name, which may be empty.
The QWriteLocker class is a convenience class that simplifies locking and unlocking read-write locks ...
static bool qDriverDictInit
QString password() const
Returns the connection&#39;s password.
bool rollback()
Rolls back a transaction on the database, if the driver supports transactions and a transaction() has...
QSqlDatabase & operator=(const QSqlDatabase &other)
Assigns other to this object.
static void invalidateDb(const QSqlDatabase &db, const QString &name, bool doWarn=true)
static void cleanDriverDict()
const char * constData() const
Returns a pointer to the data stored in the byte array.
Definition: qbytearray.h:433
void setUserName(const QString &name)
Sets the connection&#39;s user name to name.
QT_STATIC_CONST char * defaultConnection
Definition: qsqldatabase.h:128
static void registerSqlDriver(const QString &name, QSqlDriverCreatorBase *creator)
This function registers a new SQL driver called name, within the SQL framework.
iterator end()
Returns an STL-style iterator pointing to the imaginary item after the last item in the hash...
Definition: qhash.h:467
static QCoreApplication * instance()
Returns a pointer to the application&#39;s QCoreApplication (or QApplication) instance.
void clear()
Clears the contents of the string and makes it empty.
Definition: qstring.h:723
static QSqlDatabase addDatabase(const QString &type, const QString &connectionName=QLatin1String(defaultConnection))
Adds a database to the list of database connections using the driver type and the connection name con...
static DriverDict & driverDict()
The QReadWriteLock class provides read-write locking.
QSqlDatabasePrivate(QSqlDatabase *d, QSqlDriver *dr=0)
const QString key(const QSqlDatabase &value) const
Returns the first key mapped to value.
Definition: qhash.h:674
bool isOpenError() const
Returns true if there was an error opening the database connection; otherwise returns false...
iterator begin()
Returns an STL-style iterator pointing to the first item in the hash.
Definition: qhash.h:464
void qAtomicAssign(T *&d, T *x)
This is a helper for the assignment operators of implicitly shared classes.
Definition: qatomic.h:195
QStringList keys_ts() const
QString driverName() const
Returns the connection&#39;s driver name.
The QSqlDriverCreatorBase class is the base class for SQL driver factories.
Definition: qsqldatabase.h:64
void setHostName(const QString &host)
Sets the connection&#39;s host name to host.
QSqlQuery exec(const QString &query=QString()) const
Executes a SQL statement on the database and returns a QSqlQuery object.
void setDatabaseName(const QString &name)
Sets the connection&#39;s database name to name.
bool exec(const QString &query)
Executes the SQL in query.
Definition: qsqlquery.cpp:355
bool open()
Opens the database connection using the current connection values.
QString hostName() const
Returns the connection&#39;s host name; it may be empty.
#define class
QDebug & space()
Writes a space character to the debug stream and returns a reference to the stream.
Definition: qdebug.h:91
Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
Definition: qalgorithms.h:319
QStringList tables(QSql::TableType type=QSql::Tables) const
Returns a list of the database&#39;s tables, system tables and views, as specified by the parameter type...
QList< QString > keys() const
Returns a list containing all the keys in the hash, in an arbitrary order.
Definition: qhash.h:648
#define Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS)
Declares a global static variable with the specified type and name.
Definition: qglobal.h:1982
int open(const char *, int,...)
const_iterator constEnd() const
Returns a const STL-style iterator pointing to the imaginary item after the last item in the list...
Definition: qlist.h:272
static void cleanConnections()
void setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy)
Sets the default numerical precision policy used by queries created on this database connection to pr...