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/kernel/qsqlcachedresult.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/kernel/qsqlcachedresult.cpp')
-rw-r--r-- | src/sql/kernel/qsqlcachedresult.cpp | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/src/sql/kernel/qsqlcachedresult.cpp b/src/sql/kernel/qsqlcachedresult.cpp index babe38bf73..6c810a7e9a 100644 --- a/src/sql/kernel/qsqlcachedresult.cpp +++ b/src/sql/kernel/qsqlcachedresult.cpp @@ -36,6 +36,7 @@ #include <qvariant.h> #include <qdatetime.h> #include <qvector.h> +#include <QtSql/private/qsqldriver_p.h> QT_BEGIN_NAMESPACE @@ -53,8 +54,12 @@ QT_BEGIN_NAMESPACE static const uint initial_cache_size = 128; -QSqlCachedResultPrivate::QSqlCachedResultPrivate(): - rowCacheEnd(0), colCount(0), forwardOnly(false), atEnd(false) +QSqlCachedResultPrivate::QSqlCachedResultPrivate(QSqlCachedResult *q, const QSqlDriver *drv) + : QSqlResultPrivate(q, drv), + rowCacheEnd(0), + colCount(0), + forwardOnly(false), + atEnd(false) { } @@ -116,23 +121,24 @@ inline int QSqlCachedResultPrivate::cacheCount() const ////////////// -QSqlCachedResult::QSqlCachedResult(const QSqlDriver * db): QSqlResult (db) +QSqlCachedResult::QSqlCachedResult(QSqlCachedResultPrivate &d) + : QSqlResult(d) { - d = new QSqlCachedResultPrivate(); } QSqlCachedResult::~QSqlCachedResult() { - delete d; } void QSqlCachedResult::init(int colCount) { + Q_D(QSqlCachedResult); d->init(colCount, isForwardOnly()); } bool QSqlCachedResult::fetch(int i) { + Q_D(QSqlCachedResult); if ((!isActive()) || (i < 0)) return false; if (at() == i) @@ -171,6 +177,7 @@ bool QSqlCachedResult::fetch(int i) bool QSqlCachedResult::fetchNext() { + Q_D(QSqlCachedResult); if (d->canSeek(at() + 1)) { setAt(at() + 1); return true; @@ -185,6 +192,7 @@ bool QSqlCachedResult::fetchPrevious() bool QSqlCachedResult::fetchFirst() { + Q_D(QSqlCachedResult); if (d->forwardOnly && at() != QSql::BeforeFirstRow) { return false; } @@ -197,6 +205,7 @@ bool QSqlCachedResult::fetchFirst() bool QSqlCachedResult::fetchLast() { + Q_D(QSqlCachedResult); if (d->atEnd) { if (d->forwardOnly) return false; @@ -217,6 +226,7 @@ bool QSqlCachedResult::fetchLast() QVariant QSqlCachedResult::data(int i) { + Q_D(const QSqlCachedResult); int idx = d->forwardOnly ? i : at() * d->colCount + i; if (i >= d->colCount || i < 0 || at() < 0 || idx >= d->rowCacheEnd) return QVariant(); @@ -226,6 +236,7 @@ QVariant QSqlCachedResult::data(int i) bool QSqlCachedResult::isNull(int i) { + Q_D(const QSqlCachedResult); int idx = d->forwardOnly ? i : at() * d->colCount + i; if (i >= d->colCount || i < 0 || at() < 0 || idx >= d->rowCacheEnd) return true; @@ -235,6 +246,7 @@ bool QSqlCachedResult::isNull(int i) void QSqlCachedResult::cleanup() { + Q_D(QSqlCachedResult); setAt(QSql::BeforeFirstRow); setActive(false); d->cleanup(); @@ -242,6 +254,7 @@ void QSqlCachedResult::cleanup() void QSqlCachedResult::clearValues() { + Q_D(QSqlCachedResult); setAt(QSql::BeforeFirstRow); d->rowCacheEnd = 0; d->atEnd = false; @@ -249,6 +262,7 @@ void QSqlCachedResult::clearValues() bool QSqlCachedResult::cacheNext() { + Q_D(QSqlCachedResult); if (d->atEnd) return false; @@ -268,11 +282,13 @@ bool QSqlCachedResult::cacheNext() int QSqlCachedResult::colCount() const { + Q_D(const QSqlCachedResult); return d->colCount; } QSqlCachedResult::ValueCache &QSqlCachedResult::cache() { + Q_D(QSqlCachedResult); return d->cache; } |