diff options
author | Mark Brand <mabrand@mabrand.nl> | 2015-11-26 16:54:34 +0100 |
---|---|---|
committer | Mark Brand <mabrand@mabrand.nl> | 2015-12-05 00:19:27 +0000 |
commit | d79ae904660af7a83dc42a389c2457a8c94020f7 (patch) | |
tree | 84fe3d87a24575d0ca8188553fa5dae9ab6e47e3 /src/sql/drivers/ibase | |
parent | f5f47987ce369aa3f7553e6c0da509461a1ddf1a (diff) |
qsql: apply Qt's PIMPL idiom to Q*ResultPrivate
QResult and QResultPrivate are not derived from QObject and
QObjectPrivate respectively, but can still benefit from Qt's
PIMPL idiom. There are several interrelated aspects to this:
- Base all driver ResultPrivate classes on QResultPrivate.
Previously, each level in the Result hierarchy tended to keep
its own private data class.
- The ResultPrivate class initializes its own Result (q_ptr)
and Driver members. This ensures that these pointers are
correctly set in time for the ResultPrivate constructors
and Result constructors. This is more efficient and makes
it a lot easier to follow what's being allocated, initialized,
and cleaned-up.
- Use macros Q_DECLARE_PRIVATE, Q_DECLARE_PUBLIC, Q_D, and
Q_Q for access to and from ResultPrivate objects.
- ResultPrivate classes refer frequently to their counterpart
DriverPrivate. Various patterns were used to do this. Now
Q_DECLARE_SQLDRIVER_PRIVATE arranges this uniformly while
hiding ugly casting. It creates a public method in the
ResultPrivate returning the correctly typed pointer to the
corresponding DriverPrivate object. Since the method is public,
the Result class and helper classes and functions can also use it.
- The explicit const is removed from QResultPrivate::sqldriver,
even though it is treated (mostly) like a const within the
context of Result and ResultPrivate. This is the same pattern
seen in Qt's PIMPL idiom. The macro created getter methods
take care of const.
- qsql_mysql was using a signal/slot connection to zero its own
copy of the driver pointer when the driver was destroyed. This is
no longer necessary.
Change-Id: Ida4933bc92fb3e9a05ea4b53b48085894734e36e
Reviewed-by: Israel Lins Albuquerque <israelins85@yahoo.com.br>
Reviewed-by: Mark Brand <mabrand@mabrand.nl>
Diffstat (limited to 'src/sql/drivers/ibase')
-rw-r--r-- | src/sql/drivers/ibase/qsql_ibase.cpp | 47 |
1 files changed, 30 insertions, 17 deletions
diff --git a/src/sql/drivers/ibase/qsql_ibase.cpp b/src/sql/drivers/ibase/qsql_ibase.cpp index 844950dcc7..29f9d3aeb1 100644 --- a/src/sql/drivers/ibase/qsql_ibase.cpp +++ b/src/sql/drivers/ibase/qsql_ibase.cpp @@ -345,7 +345,7 @@ class QIBaseResultPrivate; class QIBaseResult : public QSqlCachedResult { - friend class QIBaseResultPrivate; + Q_DECLARE_PRIVATE(QIBaseResult) public: explicit QIBaseResult(const QIBaseDriver* db); @@ -361,20 +361,22 @@ protected: int size() Q_DECL_OVERRIDE; int numRowsAffected() Q_DECL_OVERRIDE; QSqlRecord record() const Q_DECL_OVERRIDE; - -private: - QIBaseResultPrivate* d; }; -class QIBaseResultPrivate +class QIBaseResultPrivate: public QSqlCachedResultPrivate { + Q_DECLARE_PUBLIC(QIBaseResult) + public: - QIBaseResultPrivate(QIBaseResult *d, const QIBaseDriver *ddb); + Q_DECLARE_SQLDRIVER_PRIVATE(QIBaseDriver) + + QIBaseResultPrivate(QIBaseResult *q, const QIBaseDriver *drv); ~QIBaseResultPrivate() { cleanup(); } void cleanup(); bool isError(const char *msg, QSqlError::ErrorType typ = QSqlError::UnknownError) { + Q_Q(QIBaseResult); QString imsg; ISC_LONG sqlcode; if (!getIBaseError(imsg, status, sqlcode, tc)) @@ -395,8 +397,6 @@ public: bool writeArray(int i, const QList<QVariant> &list); public: - QIBaseResult *q; - const QIBaseDriver *db; ISC_STATUS status[20]; isc_tr_handle trans; //indicator whether we have a local transaction or a transaction on driver level @@ -410,14 +410,22 @@ public: }; -QIBaseResultPrivate::QIBaseResultPrivate(QIBaseResult *d, const QIBaseDriver *ddb): - q(d), db(ddb), trans(0), stmt(0), ibase(ddb->d_func()->ibase), sqlda(0), inda(0), queryType(-1), tc(ddb->d_func()->tc) +QIBaseResultPrivate::QIBaseResultPrivate(QIBaseResult *q, const QIBaseDriver *drv) + : QSqlCachedResultPrivate(q, drv), + trans(0), + localTransaction(!drv_d_func()->ibase), + stmt(0), + ibase(drv_d_func()->ibase), + sqlda(0), + inda(0), + queryType(-1), + tc(drv_d_func()->tc) { - localTransaction = (ddb->d_func()->ibase == 0); } void QIBaseResultPrivate::cleanup() { + Q_Q(QIBaseResult); commit(); if (!localTransaction) trans = 0; @@ -779,6 +787,7 @@ static char* createArrayBuffer(char *buffer, const QList<QVariant> &list, bool QIBaseResultPrivate::writeArray(int column, const QList<QVariant> &list) { + Q_Q(QIBaseResult); QString error; ISC_QUAD *arrayId = (ISC_QUAD*) inda->sqlvar[column].sqldata; ISC_ARRAY_DESC desc; @@ -854,9 +863,9 @@ bool QIBaseResultPrivate::transaction() { if (trans) return true; - if (db->d_func()->trans) { + if (drv_d_func()->trans) { localTransaction = false; - trans = db->d_func()->trans; + trans = drv_d_func()->trans; return true; } localTransaction = true; @@ -887,19 +896,18 @@ bool QIBaseResultPrivate::commit() ////////// -QIBaseResult::QIBaseResult(const QIBaseDriver* db): - QSqlCachedResult(db) +QIBaseResult::QIBaseResult(const QIBaseDriver *db) + : QSqlCachedResult(*new QIBaseResultPrivate(this, db)) { - d = new QIBaseResultPrivate(this, db); } QIBaseResult::~QIBaseResult() { - delete d; } bool QIBaseResult::prepare(const QString& query) { + Q_D(QIBaseResult); // qDebug("prepare: %s", qPrintable(query)); if (!driver() || !driver()->isOpen() || driver()->isOpenError()) return false; @@ -976,6 +984,7 @@ bool QIBaseResult::prepare(const QString& query) bool QIBaseResult::exec() { + Q_D(QIBaseResult); bool ok = true; if (!d->trans) @@ -1113,6 +1122,7 @@ bool QIBaseResult::reset (const QString& query) bool QIBaseResult::gotoNext(QSqlCachedResult::ValueCache& row, int rowIdx) { + Q_D(QIBaseResult); ISC_STATUS stat = 0; // Stored Procedures are special - they populate our d->sqlda when executing, @@ -1307,6 +1317,7 @@ int QIBaseResult::size() int QIBaseResult::numRowsAffected() { + Q_D(QIBaseResult); static char acCountInfo[] = {isc_info_sql_records}; char cCountType; bool bIsProcedure = false; @@ -1361,6 +1372,7 @@ int QIBaseResult::numRowsAffected() QSqlRecord QIBaseResult::record() const { + Q_D(const QIBaseResult); QSqlRecord rec; if (!isActive() || !d->sqlda) return rec; @@ -1400,6 +1412,7 @@ QSqlRecord QIBaseResult::record() const QVariant QIBaseResult::handle() const { + Q_D(const QIBaseResult); return QVariant(qRegisterMetaType<isc_stmt_handle>("isc_stmt_handle"), &d->stmt); } |