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/tds/qsql_tds.cpp | |
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/tds/qsql_tds.cpp')
-rw-r--r-- | src/sql/drivers/tds/qsql_tds.cpp | 35 |
1 files changed, 24 insertions, 11 deletions
diff --git a/src/sql/drivers/tds/qsql_tds.cpp b/src/sql/drivers/tds/qsql_tds.cpp index a8104d938e..75d6cc2cd6 100644 --- a/src/sql/drivers/tds/qsql_tds.cpp +++ b/src/sql/drivers/tds/qsql_tds.cpp @@ -131,6 +131,8 @@ QSqlError qMakeError(const QString& err, QSqlError::ErrorType type, int errNo = class QTDSDriverPrivate : public QSqlDriverPrivate { + Q_DECLARE_PUBLIC(QTDSDriver) + public: QTDSDriverPrivate() : QSqlDriverPrivate(), login(0), initialized(false) { dbmsType = QSqlDriver::Sybase; } LOGINREC* login; // login information @@ -150,6 +152,8 @@ class QTDSResultPrivate; class QTDSResult : public QSqlCachedResult { + Q_DECLARE_PRIVATE(QTDSResult) + public: explicit QTDSResult(const QTDSDriver* db); ~QTDSResult(); @@ -162,15 +166,18 @@ protected: int numRowsAffected() Q_DECL_OVERRIDE; bool gotoNext(QSqlCachedResult::ValueCache &values, int index) Q_DECL_OVERRIDE; QSqlRecord record() const Q_DECL_OVERRIDE; - -private: - QTDSResultPrivate* d; }; -class QTDSResultPrivate +class QTDSResultPrivate: public QSqlCachedResultPrivate { + Q_DECLARE_PUBLIC(QTDSResult) + public: - QTDSResultPrivate():login(0), dbproc(0) {} + Q_DECLARE_SQLDRIVER_PRIVATE(QTDSDriver) + QTDSResultPrivate(QTDSResult *q, const QTDSDriver *drv) + : QSqlCachedResultPrivate(q, drv), + login(0), + dbproc(0) {} LOGINREC* login; // login information DBPROCESS* dbproc; // connection from app to server QSqlError lastError; @@ -316,15 +323,15 @@ QVariant::Type qFieldType(QTDSResultPrivate* d, int i) QTDSResult::QTDSResult(const QTDSDriver* db) - : QSqlCachedResult(db) + : QSqlCachedResult(*new QTDSResultPrivate(this, db)) { - d = new QTDSResultPrivate(); - d->login = db->d_func()->login; + Q_D(QTDSResult); + d->login = d->drv_d_func()->login; - d->dbproc = dbopen(d->login, const_cast<char*>(db->d_func()->hostName.toLatin1().constData())); + d->dbproc = dbopen(d->login, const_cast<char*>(d->drv_d_func()->hostName.toLatin1().constData())); if (!d->dbproc) return; - if (dbuse(d->dbproc, const_cast<char*>(db->d_func()->db.toLatin1().constData())) == FAIL) + if (dbuse(d->dbproc, const_cast<char*>(d->drv_d_func()->db.toLatin1().constData())) == FAIL) return; // insert d in error handler dict @@ -335,15 +342,16 @@ QTDSResult::QTDSResult(const QTDSDriver* db) QTDSResult::~QTDSResult() { + Q_D(QTDSResult); cleanup(); if (d->dbproc) dbclose(d->dbproc); errs()->remove(d->dbproc); - delete d; } void QTDSResult::cleanup() { + Q_D(QTDSResult); d->clearErrorMsgs(); d->rec.clear(); for (int i = 0; i < d->buffer.size(); ++i) @@ -358,6 +366,7 @@ void QTDSResult::cleanup() QVariant QTDSResult::handle() const { + Q_D(const QTDSResult); return QVariant(qRegisterMetaType<DBPROCESS *>("DBPROCESS*"), &d->dbproc); } @@ -368,6 +377,7 @@ static inline bool qIsNull(const QTDSColumnData &p) bool QTDSResult::gotoNext(QSqlCachedResult::ValueCache &values, int index) { + Q_D(QTDSResult); STATUS stat = dbnextrow(d->dbproc); if (stat == NO_MORE_ROWS) { setAt(QSql::AfterLastRow); @@ -427,6 +437,7 @@ bool QTDSResult::gotoNext(QSqlCachedResult::ValueCache &values, int index) bool QTDSResult::reset (const QString& query) { + Q_D(QTDSResult); cleanup(); if (!driver() || !driver()-> isOpen() || driver()->isOpenError()) return false; @@ -515,6 +526,7 @@ int QTDSResult::size() int QTDSResult::numRowsAffected() { + Q_D(const QTDSResult); #ifdef DBNTWIN32 if (dbiscount(d->dbproc)) { return DBCOUNT(d->dbproc); @@ -527,6 +539,7 @@ int QTDSResult::numRowsAffected() QSqlRecord QTDSResult::record() const { + Q_D(const QTDSResult); return d->rec; } |