13#include "QtCore/qapplicationstatic.h"
14#include "private/qfactoryloader_p.h"
15#include "private/qsqlnulldriver_p.h"
23using namespace
Qt::StringLiterals;
25#define CHECK_QCOREAPPLICATION \
26 if (Q_UNLIKELY(!QCoreApplication::instance())) { \
27 qCWarning(lcSqlDb, "QSqlDatabase requires a QCoreApplication"); \
30#define CHECK_QCOREAPPLICATION_RETVAL \
31 if (Q_UNLIKELY(!QCoreApplication::instance())) { \
32 qCWarning(lcSqlDb, "QSqlDatabase requires a QCoreApplication"); \
48 return connections.value(
key);
53 return connections.contains(
key);
58 return connections.keys();
123QtSqlGlobals::~QtSqlGlobals()
126 for (
const auto &[k,
v] : std::as_const(connections).asKeyValueRange())
139 if (
db.d->ref.loadRelaxed() != 1 && doWarn) {
140 qCWarning(lcSqlDb,
"QSqlDatabasePrivate::removeDatabase: connection '%ls' is still in use, "
143 db.d->connName.clear();
150 QtSqlGlobals *sqlGlobals = s_sqlGlobals();
153 if (!sqlGlobals->connections.contains(
name))
162 QtSqlGlobals *sqlGlobals = s_sqlGlobals();
165 if (sqlGlobals->connections.contains(
name)) {
167 qCWarning(lcSqlDb,
"QSqlDatabasePrivate::addDatabase: duplicate connection name '%ls', old "
170 sqlGlobals->connections.insert(
name,
db);
183 qCWarning(lcSqlDb,
"QSqlDatabasePrivate::database: requested database does not belong to the calling thread.");
187 if (
open && !
db.isOpen()) {
189 qCWarning(lcSqlDb) <<
"QSqlDatabasePrivate::database: unable to open database:" <<
db.lastError().text();
488 typedef QMultiMap<int, QString> PluginKeyMap;
490 const PluginKeyMap
keyMap = fl->keyMap();
497 QtSqlGlobals *sqlGlobals = s_sqlGlobals();
499 const auto &dict = sqlGlobals->registeredDrivers;
500 for (
const auto &[k, _] : dict.asKeyValueRange()) {
524 QtSqlGlobals *sqlGlobals = s_sqlGlobals();
526 delete sqlGlobals->registeredDrivers.take(
name);
528 sqlGlobals->registeredDrivers.insert(
name,
creator);
556 return s_sqlGlobals()->connectionNames();
644 QtSqlGlobals *sqlGlobals = s_sqlGlobals();
646 const auto &dict = sqlGlobals->registeredDrivers;
648 if (
it != dict.
end())
649 driver =
it.value()->createObject();
653 driver = qLoadPlugin<QSqlDriver, QSqlDriverPlugin>(loader(),
type);
657 qCWarning(lcSqlDb,
"QSqlDatabase: available drivers: %ls",
660 qCWarning(lcSqlDb,
"QSqlDatabase: an instance of QCoreApplication is required for loading driver plugins");
691#if QT_DEPRECATED_SINCE(6, 6)
695 if (!
query.isEmpty()) {
1259 if (!
other.isValid())
1363 qWarning(
"QSqlDatabasePrivate::moveToThread: connection '%ls' is still in use "
1386#ifndef QT_NO_DEBUG_STREAM
1393 dbg <<
"QSqlDatabase(invalid)";
1397 dbg <<
"QSqlDatabase(driver=\"" <<
d.driverName() <<
"\", database=\""
1398 <<
d.databaseName() <<
"\", host=\"" <<
d.hostName() <<
"\", port=" <<
d.port()
1399 <<
", user=\"" <<
d.userName() <<
"\", open=" <<
d.isOpen() <<
')';
1406#include "moc_qsqldatabase.cpp"
T loadRelaxed() const noexcept
static QCoreApplication * instance() noexcept
Returns a pointer to the application's QCoreApplication (or QGuiApplication/QApplication) instance.
QThread * thread() const
Returns the thread in which the object lives.
bool moveToThread(QThread *thread QT6_DECL_NEW_OVERLOAD_TAIL)
Changes the thread affinity for this object and its children and returns true on success.
iterator find(const T &value)
static QSqlDatabasePrivate * shared_null()
void init(const QString &type)
QSqlDatabasePrivate(QSqlDriver *dr)
static QSqlDatabase database(const QString &name, bool open)
static void removeDatabase(const QString &name)
static void invalidateDb(const QSqlDatabase &db, const QString &name, bool doWarn=true)
QSql::NumericalPrecisionPolicy precisionPolicy
static void addDatabase(const QSqlDatabase &db, const QString &name)
void copy(const QSqlDatabasePrivate *other)
The QSqlDatabase class handles a connection to a database.
bool isValid() const
Returns true if the QSqlDatabase has a valid driver.
void close()
Closes the database connection, freeing any resources acquired, and invalidating any existing QSqlQue...
static bool contains(const QString &connectionName=QLatin1StringView(defaultConnection))
\threadsafe
QString driverName() const
Returns the connection's driver name.
int port() const
Returns the connection's port number.
bool open()
Executes a SQL statement on the database and returns a QSqlQuery object.
QSqlIndex primaryIndex(const QString &tablename) const
Returns the primary index for table tablename.
void setDatabaseName(const QString &name)
Sets the connection's database name to name.
QString connectionName() const
Returns the connection name, which may be empty.
static QStringList connectionNames()
\threadsafe
QSqlDriver * driver() const
Returns the database driver used to access the database connection.
QString hostName() const
Returns the connection's host name; it may be empty.
bool isOpen() const
Returns true if the database connection is currently open; otherwise returns false.
QString connectOptions() const
Returns the connection options string used for this connection.
bool transaction()
Begins a transaction on the database if the driver supports transactions.
static const char * defaultConnection
void setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy)
Sets \l numericalPrecisionPolicy to precisionPolicy.
QString password() const
Returns the connection's password.
bool moveToThread(QThread *targetThread)
QSqlRecord record(const QString &tablename) const
Returns a QSqlRecord populated with the names of all the fields in the table (or view) called tablena...
static void removeDatabase(const QString &connectionName)
\threadsafe
QSqlDatabase & operator=(const QSqlDatabase &other)
Assigns other to this object.
static QSqlDatabase addDatabase(const QString &type, const QString &connectionName=QLatin1StringView(defaultConnection))
\threadsafe
QSql::NumericalPrecisionPolicy numericalPrecisionPolicy
void setPassword(const QString &password)
Sets the connection's password to password.
bool rollback()
Rolls back a transaction on the database, if the driver supports transactions and a transaction() has...
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.
bool isOpenError() const
Returns true if there was an error opening the database connection; otherwise returns false.
QSqlDatabase()
Creates an empty, invalid QSqlDatabase object.
static QSqlDatabase database(const QString &connectionName=QLatin1StringView(defaultConnection), bool open=true)
\threadsafe
bool commit()
Commits a transaction to the database if the driver supports transactions and a transaction() has bee...
static QSqlDatabase cloneDatabase(const QSqlDatabase &other, const QString &connectionName)
Clones the database connection other and stores it as connectionName.
void setUserName(const QString &name)
Sets the connection's user name to name.
QString userName() const
Returns the connection's user name; it may be empty.
static bool isDriverAvailable(const QString &name)
Returns true if a driver called name is available; otherwise returns false.
static void registerSqlDriver(const QString &name, QSqlDriverCreatorBase *creator)
[2]
void setConnectOptions(const QString &options=QString())
Sets database-specific options.
QString databaseName() const
Returns the connection's database name, which may be empty.
QThread * currentThread() const
QSqlError lastError() const
Returns information about the last error that occurred on the database.
void setHostName(const QString &host)
Sets the connection's host name to host.
void setPort(int p)
Sets the connection's port number to port.
static QStringList drivers()
Returns a list of all the available database drivers.
~QSqlDatabase()
Destroys the object and frees any allocated resources.
The QSqlDriverCreatorBase class is the base class for SQL driver factories.
The QSqlDriver class is an abstract base class for accessing specific SQL databases.
virtual bool commitTransaction()
This function is called to commit a transaction.
virtual QSqlResult * createResult() const =0
Creates an empty SQL result on the database.
void setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy)
Sets \l numericalPrecisionPolicy to precisionPolicy.
virtual QSqlIndex primaryIndex(const QString &tableName) const
Returns the primary index for table tableName.
virtual bool beginTransaction()
This function is called to begin a transaction.
QSqlError lastError() const
Returns a QSqlError object which contains information about the last error that occurred on the datab...
virtual QSqlRecord record(const QString &tableName) const
Returns a QSqlRecord populated with the names of the fields in table tableName.
virtual bool rollbackTransaction()
This function is called to rollback a transaction.
virtual void close()=0
Derived classes must reimplement this pure virtual function in order to close the database connection...
bool isOpenError() const
Returns true if the there was an error opening the database connection; otherwise returns false.
virtual void setLastError(const QSqlError &e)
This function is used to set the value of the last error, error, that occurred on the database.
virtual bool open(const QString &db, const QString &user=QString(), const QString &password=QString(), const QString &host=QString(), int port=-1, const QString &connOpts=QString())=0
Derived classes must reimplement this pure virtual function to open a database connection on database...
virtual bool isOpen() const
Returns true if the database connection is open; otherwise returns false.
QSql::NumericalPrecisionPolicy numericalPrecisionPolicy
virtual bool hasFeature(DriverFeature f) const =0
Returns true if the driver supports feature feature; otherwise returns false.
virtual QStringList tables(QSql::TableType tableType) const
Returns a list of the names of the tables in the database.
The QSqlError class provides SQL database error information.
The QSqlIndex class provides functions to manipulate and describe database indexes.
The QSqlQuery class provides a means of executing and manipulating SQL statements.
The QSqlRecord class encapsulates a database record.
\macro QT_RESTRICTED_CAST_FROM_ASCII
static QThread * currentThread()
qDeleteAll(list.begin(), list.end())
QSet< QString >::iterator it
Combined button and popup list for selecting options.
static jboolean copy(JNIEnv *, jobject)
#define Q_APPLICATION_STATIC(TYPE, NAME,...)
QT_WARNING_POP void qAtomicAssign(T *&d, T *x)
This is a helper for the assignment operators of implicitly shared classes.
DBusConnection * connection
#define Q_GLOBAL_STATIC_WITH_ARGS(TYPE, NAME, ARGS)
#define Q_LOGGING_CATEGORY(name,...)
#define qCWarning(category,...)
GLsizei const GLfloat * v
[13]
QDebug operator<<(QDebug dbg, const QSqlDatabase &d)
#define CHECK_QCOREAPPLICATION
#define CHECK_QCOREAPPLICATION_RETVAL
#define QSqlDriverFactoryInterface_iid
#define qUtf16Printable(string)
static QT_BEGIN_NAMESPACE void init(QTextBoundaryFinder::BoundaryType type, QStringView str, QCharAttributes *attributes)
static const struct @450 keyMap[]
file open(QIODevice::ReadOnly)
QItemEditorCreatorBase * creator
bool contains(const AT &t) const noexcept
QHash< QString, QSqlDriverCreatorBase * > registeredDrivers
bool connectionExists(const QString &key) const
QHash< QString, QSqlDatabase > connections
QStringList connectionNames() const
QSqlDatabase connection(const QString &key) const