summaryrefslogtreecommitdiffstats
path: root/src/sql/kernel
diff options
context:
space:
mode:
authorMark Brand <mabrand@mabrand.nl>2015-11-26 16:54:34 +0100
committerMark Brand <mabrand@mabrand.nl>2015-12-05 00:19:27 +0000
commitd79ae904660af7a83dc42a389c2457a8c94020f7 (patch)
tree84fe3d87a24575d0ca8188553fa5dae9ab6e47e3 /src/sql/kernel
parentf5f47987ce369aa3f7553e6c0da509461a1ddf1a (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')
-rw-r--r--src/sql/kernel/qsqlcachedresult.cpp26
-rw-r--r--src/sql/kernel/qsqlcachedresult_p.h12
-rw-r--r--src/sql/kernel/qsqlresult.cpp14
-rw-r--r--src/sql/kernel/qsqlresult.h2
-rw-r--r--src/sql/kernel/qsqlresult_p.h16
5 files changed, 46 insertions, 24 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;
}
diff --git a/src/sql/kernel/qsqlcachedresult_p.h b/src/sql/kernel/qsqlcachedresult_p.h
index 7afa5d99c3..78ba92db43 100644
--- a/src/sql/kernel/qsqlcachedresult_p.h
+++ b/src/sql/kernel/qsqlcachedresult_p.h
@@ -46,6 +46,7 @@
//
#include "QtSql/qsqlresult.h"
+#include "QtSql/private/qsqlresult_p.h"
QT_BEGIN_NAMESPACE
@@ -56,13 +57,15 @@ class QSqlCachedResultPrivate;
class Q_SQL_EXPORT QSqlCachedResult: public QSqlResult
{
+ Q_DECLARE_PRIVATE(QSqlCachedResult)
+
public:
virtual ~QSqlCachedResult();
typedef QVector<QVariant> ValueCache;
protected:
- QSqlCachedResult(const QSqlDriver * db);
+ QSqlCachedResult(QSqlCachedResultPrivate &d);
void init(int colCount);
void cleanup();
@@ -86,13 +89,14 @@ protected:
void setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy policy) Q_DECL_OVERRIDE;
private:
bool cacheNext();
- QSqlCachedResultPrivate *d;
};
-class QSqlCachedResultPrivate
+class Q_SQL_EXPORT QSqlCachedResultPrivate: public QSqlResultPrivate
{
+ Q_DECLARE_PUBLIC(QSqlCachedResult)
+
public:
- QSqlCachedResultPrivate();
+ QSqlCachedResultPrivate(QSqlCachedResult *q, const QSqlDriver *drv);
bool canSeek(int i) const;
inline int cacheCount() const;
void init(int count, bool fo);
diff --git a/src/sql/kernel/qsqlresult.cpp b/src/sql/kernel/qsqlresult.cpp
index 051ecfca06..a701f9f669 100644
--- a/src/sql/kernel/qsqlresult.cpp
+++ b/src/sql/kernel/qsqlresult.cpp
@@ -87,7 +87,7 @@ QString QSqlResultPrivate::positionalToNamedBinding(const QString &query) const
result.reserve(n * 5 / 4);
QChar closingQuote;
int count = 0;
- bool ignoreBraces = (sqldriver->d_func()->dbmsType == QSqlDriver::PostgreSQL);
+ bool ignoreBraces = (sqldriver->dbmsType() == QSqlDriver::PostgreSQL);
for (int i = 0; i < n; ++i) {
QChar ch = query.at(i);
@@ -128,7 +128,7 @@ QString QSqlResultPrivate::namedToPositionalBinding(const QString &query)
QChar closingQuote;
int count = 0;
int i = 0;
- bool ignoreBraces = (sqldriver->d_func()->dbmsType == QSqlDriver::PostgreSQL);
+ bool ignoreBraces = (sqldriver->dbmsType() == QSqlDriver::PostgreSQL);
while (i < n) {
QChar ch = query.at(i);
@@ -218,22 +218,18 @@ QString QSqlResultPrivate::namedToPositionalBinding(const QString &query)
QSqlResult::QSqlResult(const QSqlDriver *db)
{
- d_ptr = new QSqlResultPrivate;
+ d_ptr = new QSqlResultPrivate(this, db);
Q_D(QSqlResult);
- d->q_ptr = this;
- d->sqldriver = db;
if (d->sqldriver)
setNumericalPrecisionPolicy(d->sqldriver->numericalPrecisionPolicy());
}
/*! \internal
*/
-QSqlResult::QSqlResult(QSqlResultPrivate &dd, const QSqlDriver *db)
+QSqlResult::QSqlResult(QSqlResultPrivate &dd)
+ : d_ptr(&dd)
{
- d_ptr = &dd;
Q_D(QSqlResult);
- d->q_ptr = this;
- d->sqldriver = db;
if (d->sqldriver)
setNumericalPrecisionPolicy(d->sqldriver->numericalPrecisionPolicy());
}
diff --git a/src/sql/kernel/qsqlresult.h b/src/sql/kernel/qsqlresult.h
index c86a8f858f..4a4b358a49 100644
--- a/src/sql/kernel/qsqlresult.h
+++ b/src/sql/kernel/qsqlresult.h
@@ -66,7 +66,7 @@ protected:
};
explicit QSqlResult(const QSqlDriver * db);
- QSqlResult(QSqlResultPrivate &dd, const QSqlDriver *db);
+ QSqlResult(QSqlResultPrivate &dd);
int at() const;
QString lastQuery() const;
QSqlError lastError() const;
diff --git a/src/sql/kernel/qsqlresult_p.h b/src/sql/kernel/qsqlresult_p.h
index 4fee77eecd..b158618b78 100644
--- a/src/sql/kernel/qsqlresult_p.h
+++ b/src/sql/kernel/qsqlresult_p.h
@@ -48,9 +48,15 @@
#include <QtCore/qpointer.h>
#include "qsqlerror.h"
#include "qsqlresult.h"
+#include "qsqldriver.h"
QT_BEGIN_NAMESPACE
+// convenience method Q*ResultPrivate::drv_d_func() returns pointer to private driver. Compare to Q_DECLARE_PRIVATE in qglobal.h.
+#define Q_DECLARE_SQLDRIVER_PRIVATE(Class) \
+ inline const Class##Private* drv_d_func() const { return !sqldriver ? nullptr : reinterpret_cast<const Class *>(static_cast<const QSqlDriver*>(sqldriver))->d_func(); } \
+ inline Class##Private* drv_d_func() { return !sqldriver ? nullptr : reinterpret_cast<Class *>(static_cast<QSqlDriver*>(sqldriver))->d_func(); }
+
struct QHolder {
QHolder(const QString &hldr = QString(), int index = -1): holderName(hldr), holderPos(index) { }
bool operator==(const QHolder &h) const { return h.holderPos == holderPos && h.holderName == holderName; }
@@ -59,14 +65,14 @@ struct QHolder {
int holderPos;
};
-class QSqlDriver;
-
class Q_SQL_EXPORT QSqlResultPrivate
{
+ Q_DECLARE_PUBLIC(QSqlResult)
public:
- QSqlResultPrivate()
- : q_ptr(0),
+ QSqlResultPrivate(QSqlResult *q, const QSqlDriver *drv)
+ : q_ptr(q),
+ sqldriver(const_cast<QSqlDriver*>(drv)),
idx(QSql::BeforeFirstRow),
active(false),
isSel(false),
@@ -107,7 +113,7 @@ public:
QString holderAt(int index) const;
QSqlResult *q_ptr;
- QPointer<const QSqlDriver> sqldriver;
+ QPointer<QSqlDriver> sqldriver;
int idx;
QString sql;
bool active;