51 #include "../drivers/psql/qsql_psql.h" 54 #include "../drivers/mysql/qsql_mysql.h" 57 #include "../drivers/odbc/qsql_odbc.h" 60 #include "../drivers/oci/qsql_oci.h" 64 #define RETCODE DBRETCODE 65 #include "../drivers/tds/qsql_tds.h" 69 #include "../drivers/db2/qsql_db2.h" 72 #include "../drivers/sqlite/qsql_sqlite.h" 75 #include "../drivers/sqlite2/qsql_sqlite2.h" 78 #undef SQL_FLOAT // avoid clash with ODBC 84 #define SCHAR IBASE_SCHAR // avoid clash with ODBC (older versions of ibase.h with Firebird) 85 #include "../drivers/ibase/qsql_ibase.h" 96 #include "private/qfactoryloader_p.h" 97 #include "private/qsqlnulldriver_p.h" 104 #ifndef QT_NO_LIBRARY 143 QSqlDatabasePrivate(
const QSqlDatabasePrivate &other);
144 ~QSqlDatabasePrivate();
146 void copy(
const QSqlDatabasePrivate *other);
162 static QSqlDatabasePrivate *shared_null();
167 static DriverDict &driverDict();
168 static void cleanConnections();
188 if (driver != shared_null()->driver)
199 while (it != dict->
end()) {
200 invalidateDb(it.value(), it.key(),
false);
212 qDriverDictInit =
false;
217 static DriverDict dict;
218 if (!qDriverDictInit) {
219 qDriverDictInit =
true;
234 if (db.
d->
ref != 1 && doWarn) {
235 qWarning(
"QSqlDatabasePrivate::removeDatabase: connection '%s' is still in use, " 251 invalidateDb(dict->
take(name),
name);
261 invalidateDb(dict->
take(name),
name);
262 qWarning(
"QSqlDatabasePrivate::addDatabase: duplicate connection name '%s', old " 298 uname = other->
uname;
299 pword = other->
pword;
300 hname = other->
hname;
309 if (driver != shared_null()->driver) {
311 driver = shared_null()->driver;
595 #ifdef QT_SQL_SQLITE2 602 #ifndef QT_NO_LIBRARY 613 for (DriverDict::const_iterator i = dict.constBegin(); i != dict.constEnd(); ++i) {
655 return dbDict()->contains_ts(connectionName);
670 return dbDict()->keys_ts();
795 #ifdef QT_SQL_SQLITE2 807 for (DriverDict::const_iterator
it = dict.constBegin();
808 it != dict.constEnd() && !driver; ++
it) {
809 if (type ==
it.key()) {
815 #ifndef QT_NO_LIBRARY 816 if (!driver && loader()) {
818 driver = factory->create(type);
820 #endif // QT_NO_LIBRARY 824 qWarning(
"QSqlDatabase: available drivers: %s",
827 qWarning(
"QSqlDatabase: an instance of QCoreApplication is required for loading driver plugins");
828 driver = shared_null()->driver;
860 d->driver->setLastError(r.lastError());
876 return d->driver->open(
d->dbname,
d->uname,
d->pword,
d->hname,
877 d->port,
d->connOptions);
900 return d->driver->open(
d->dbname, user, password,
d->hname,
901 d->port,
d->connOptions);
926 return d->driver->isOpen();
937 return d->driver->isOpenError();
951 return d->driver->beginTransaction();
971 return d->driver->commitTransaction();
992 return d->driver->rollbackTransaction();
1064 d->pword = password;
1187 return d->driver->lastError();
1200 return d->driver->tables(type);
1212 return d->driver->primaryIndex(tablename);
1225 return d->driver->record(tablename);
1318 d->connOptions = options;
1329 return d->connOptions;
1341 return drivers().contains(name);
1464 {
return query.
record(); }
1470 {
return query.
record(); }
1542 driver()->setNumericalPrecisionPolicy(precisionPolicy);
1543 d->precisionPolicy = precisionPolicy;
1560 return driver()->numericalPrecisionPolicy();
1562 return d->precisionPolicy;
1566 #ifndef QT_NO_DEBUG_STREAM 1570 dbg.
nospace() <<
"QSqlDatabase(invalid)";
1574 dbg.
nospace() <<
"QSqlDatabase(driver=\"" << d.
driverName() <<
"\", database=\"" 1576 <<
", user=\"" << d.
userName() <<
"\", open=" << d.
isOpen() <<
")";
The QSqlError class provides SQL database error information.
The QSqlIndex class provides functions to manipulate and describe database indexes.
QString userName() const
Returns the connection's user name; it may be empty.
The QDebug class provides an output stream for debugging information.
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...
bool isOpen() const
Returns true if the database connection is currently open; otherwise returns false.
static void removeDatabase(const QString &name)
#define QT_END_NAMESPACE
This macro expands to.
QString connectionName() const
Returns the connection name, which may be empty.
void clear()
Removes all items from the hash.
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.
bool transaction()
Begins a transaction on the database if the driver supports transactions.
void unlock()
Unlocks the lock.
The QAtomicInt class provides platform-independent atomic operations on integers. ...
#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's internal flag that records whether the last character was a space and returns a r...
void init(const QString &type)
Create the actual driver instance type.
QSqlRecord record() const
Returns a QSqlRecord containing the field information for the current query.
bool isValid() const
Returns true if the QSqlDatabase has a valid driver.
The QSqlQuery class provides a means of executing and manipulating SQL statements.
QSqlDatabase()
Creates an empty, invalid QSqlDatabase object.
The QSqlDatabase class represents a connection to a database.
const_iterator constBegin() const
Returns a const STL-style iterator pointing to the first item in the list.
#define QT_STATIC_CONST_IMPL
QSql::NumericalPrecisionPolicy precisionPolicy
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
void setPort(int p)
Sets the connection'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
The QSqlRecord class encapsulates a database record.
The QSqlDriver class is an abstract base class for accessing specific SQL databases.
The QString class provides a Unicode character string.
T take(const Key &key)
Removes the item with the key from the hash and returns the value associated with it...
The QHash class is a template class that provides a hash-table-based dictionary.
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.
static QSqlDatabasePrivate * shared_null()
bool contains(const QString &key) const
Returns true if the hash contains an item with the key; otherwise returns false.
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.
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.
static bool isDriverAvailable(const QString &name)
Returns true if a driver called name is available; otherwise returns false.
int port() const
Returns the connection'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
#define QT_BEGIN_NAMESPACE
This macro expands to.
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.
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.
void setConnectOptions(const QString &options=QString())
Sets database-specific options.
The QStringList class provides a list of strings.
void setPassword(const QString &password)
Sets the connection'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)
QBool contains(const QString &str, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the list contains the string str; otherwise returns false.
QByteArray toLatin1() const Q_REQUIRED_RESULT
Returns a Latin-1 representation of the string as a QByteArray.
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
QByteArray toLocal8Bit() const Q_REQUIRED_RESULT
Returns the local 8-bit representation of the string as a QByteArray.
QString databaseName() const
Returns the connection'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'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.
void setUserName(const QString &name)
Sets the connection's user name to name.
QT_STATIC_CONST char * defaultConnection
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...
static QCoreApplication * instance()
Returns a pointer to the application's QCoreApplication (or QApplication) instance.
void clear()
Clears the contents of the string and makes it empty.
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.
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.
void qAtomicAssign(T *&d, T *x)
This is a helper for the assignment operators of implicitly shared classes.
QStringList keys_ts() const
QString driverName() const
Returns the connection's driver name.
The QSqlDriverCreatorBase class is the base class for SQL driver factories.
void setHostName(const QString &host)
Sets the connection'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's database name to name.
bool exec(const QString &query)
Executes the SQL in query.
bool open()
Opens the database connection using the current connection values.
QString hostName() const
Returns the connection's host name; it may be empty.
QDebug & space()
Writes a space character to the debug stream and returns a reference to the stream.
Q_OUTOFLINE_TEMPLATE void qDeleteAll(ForwardIterator begin, ForwardIterator end)
QStringList tables(QSql::TableType type=QSql::Tables) const
Returns a list of the database'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.
#define Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS)
Declares a global static variable with the specified type and name.
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...
static void cleanConnections()
void setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy)
Sets the default numerical precision policy used by queries created on this database connection to pr...