Skip to content

Commit

Permalink
QSqlDriver: return the connection name of the assoicated QSqlDatabase
Browse files Browse the repository at this point in the history
A QSqlDriver instance is directly bound to a QSqlDatabase object. But
there was no way to get the QSqlDatabase out of a QSqlQuery/QSqlDriver.
Fix it by storing the connection name also in the driver during
creation and add a getter for it.

[ChangeLog][QtSql][QSqlDriver] Added connectionName() which returns the
connection name of the associated QSqlDatabase instance.

Pick-to: 6.9
Task-number: QTBUG-123603
Change-Id: If78b85413cf6ca965ff6bf9f3600cb54169b5569
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
  • Loading branch information
chehrlic committed Dec 20, 2024
1 parent ec3a5f4 commit 5b07e3d
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/sql/kernel/qsqldatabase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,11 @@ QSqlDatabasePrivate::QSqlDatabasePrivate(const QSqlDatabasePrivate &other) : ref
connOptions = other.connOptions;
driver = other.driver;
precisionPolicy = other.precisionPolicy;
if (driver)
if (driver) {
driver->setNumericalPrecisionPolicy(other.driver->numericalPrecisionPolicy());
auto drvPriv = static_cast<QSqlDriverPrivate *>(QObjectPrivate::get(driver));
drvPriv->connectionName = connName;
}
}

QSqlDatabasePrivate::~QSqlDatabasePrivate()
Expand Down Expand Up @@ -169,6 +172,8 @@ void QSqlDatabasePrivate::addDatabase(const QSqlDatabase &db, const QString &nam
}
sqlGlobals->connections.insert(name, db);
db.d->connName = name;
auto drvPriv = static_cast<QSqlDriverPrivate *>(QObjectPrivate::get(db.d->driver));
drvPriv->connectionName = name;
}

/*! \internal
Expand Down
12 changes: 12 additions & 0 deletions src/sql/kernel/qsqldriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,18 @@ int QSqlDriver::maximumIdentifierLength(QSqlDriver::IdentifierType type) const
return INT_MAX;
}

/*!
\since 6.9
Returns the database connection name the driver was created by with
QSqlDatabase::addDatabase()
*/
QString QSqlDriver::connectionName() const
{
Q_D(const QSqlDriver);
return d->connectionName;
}

QT_END_NAMESPACE

#include "moc_qsqldriver.cpp"
4 changes: 4 additions & 0 deletions src/sql/kernel/qsqldriver.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class QVariant;
class Q_SQL_EXPORT QSqlDriver : public QObject
{
friend class QSqlDatabase;
friend class QSqlDatabasePrivate;
friend class QSqlResultPrivate;
Q_OBJECT
Q_PROPERTY(QSql::NumericalPrecisionPolicy numericalPrecisionPolicy READ numericalPrecisionPolicy WRITE setNumericalPrecisionPolicy)
Expand Down Expand Up @@ -98,6 +99,9 @@ class Q_SQL_EXPORT QSqlDriver : public QObject

DbmsType dbmsType() const;
virtual int maximumIdentifierLength(IdentifierType type) const;

QString connectionName() const;

public Q_SLOTS:
virtual bool cancelQuery();

Expand Down
1 change: 1 addition & 0 deletions src/sql/kernel/qsqldriver_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class QSqlDriverPrivate : public QObjectPrivate
dbmsType(type)
{ }

QString connectionName;
QSqlError error;
QSql::NumericalPrecisionPolicy precisionPolicy = QSql::LowPrecisionDouble;
QSqlDriver::DbmsType dbmsType;
Expand Down

0 comments on commit 5b07e3d

Please sign in to comment.