summaryrefslogtreecommitdiffstats
path: root/src/sql/kernel
diff options
context:
space:
mode:
Diffstat (limited to 'src/sql/kernel')
-rw-r--r--src/sql/kernel/qsqldatabase.cpp55
-rw-r--r--src/sql/kernel/qsqldatabase.h2
-rw-r--r--src/sql/kernel/qsqldriver.cpp28
-rw-r--r--src/sql/kernel/qsqldriver.h3
-rw-r--r--src/sql/kernel/qsqlquery.cpp23
-rw-r--r--src/sql/kernel/qsqlresult.cpp17
-rw-r--r--src/sql/kernel/qsqlresult.h1
7 files changed, 109 insertions, 20 deletions
diff --git a/src/sql/kernel/qsqldatabase.cpp b/src/sql/kernel/qsqldatabase.cpp
index a270c0e..990ad9a 100644
--- a/src/sql/kernel/qsqldatabase.cpp
+++ b/src/sql/kernel/qsqldatabase.cpp
@@ -129,11 +129,16 @@ Q_GLOBAL_STATIC(QConnectionDict, dbDict)
class QSqlDatabasePrivate
{
public:
- QSqlDatabasePrivate(QSqlDriver *dr = 0):
+ QSqlDatabasePrivate(QSqlDatabase *d, QSqlDriver *dr = 0):
+ q(d),
driver(dr),
port(-1)
{
ref = 1;
+ if(driver)
+ precisionPolicy = driver->numericalPrecisionPolicy();
+ else
+ precisionPolicy= QSql::LowPrecisionDouble;
}
QSqlDatabasePrivate(const QSqlDatabasePrivate &other);
~QSqlDatabasePrivate();
@@ -142,6 +147,7 @@ public:
void disable();
QAtomicInt ref;
+ QSqlDatabase *q;
QSqlDriver* driver;
QString dbname;
QString uname;
@@ -151,6 +157,7 @@ public:
int port;
QString connOptions;
QString connName;
+ QSql::NumericalPrecisionPolicy precisionPolicy;
static QSqlDatabasePrivate *shared_null();
static QSqlDatabase database(const QString& name, bool open);
@@ -164,6 +171,7 @@ public:
QSqlDatabasePrivate::QSqlDatabasePrivate(const QSqlDatabasePrivate &other)
{
ref = 1;
+ q = other.q;
dbname = other.dbname;
uname = other.uname;
pword = other.pword;
@@ -172,6 +180,7 @@ QSqlDatabasePrivate::QSqlDatabasePrivate(const QSqlDatabasePrivate &other)
port = other.port;
connOptions = other.connOptions;
driver = other.driver;
+ precisionPolicy = other.precisionPolicy;
}
QSqlDatabasePrivate::~QSqlDatabasePrivate()
@@ -216,7 +225,7 @@ DriverDict &QSqlDatabasePrivate::driverDict()
QSqlDatabasePrivate *QSqlDatabasePrivate::shared_null()
{
static QSqlNullDriver dr;
- static QSqlDatabasePrivate n(&dr);
+ static QSqlDatabasePrivate n(NULL, &dr);
return &n;
}
@@ -281,6 +290,7 @@ QSqlDatabase QSqlDatabasePrivate::database(const QString& name, bool open)
*/
void QSqlDatabasePrivate::copy(const QSqlDatabasePrivate *other)
{
+ q = other->q;
dbname = other->dbname;
uname = other->uname;
pword = other->pword;
@@ -288,6 +298,7 @@ void QSqlDatabasePrivate::copy(const QSqlDatabasePrivate *other)
drvName = other->drvName;
port = other->port;
connOptions = other->connOptions;
+ precisionPolicy = other->precisionPolicy;
}
void QSqlDatabasePrivate::disable()
@@ -658,7 +669,7 @@ QStringList QSqlDatabase::connectionNames()
QSqlDatabase::QSqlDatabase(const QString &type)
{
- d = new QSqlDatabasePrivate();
+ d = new QSqlDatabasePrivate(this);
d->init(type);
}
@@ -670,7 +681,7 @@ QSqlDatabase::QSqlDatabase(const QString &type)
QSqlDatabase::QSqlDatabase(QSqlDriver *driver)
{
- d = new QSqlDatabasePrivate(driver);
+ d = new QSqlDatabasePrivate(this, driver);
}
/*!
@@ -1469,6 +1480,42 @@ QString QSqlDatabase::connectionName() const
return d->connName;
}
+/*!
+
+ Sets the default numerical precision policy that queries use when created
+ on this database connection.
+
+ Note: Drivers that don't support fetching numerical values with low
+ precision will ignore the precision policy. You can use
+ QSqlDriver::hasFeature() to find out whether a driver supports this
+ feature.
+
+ Note: Setting the default precision policy doesn't affect any currently
+ active queries.
+
+ \sa QSql::NumericalPrecisionPolicy, numericalPrecisionPolicy(), QSqlQuery::setNumericalPrecisionPolicy(), QSqlQuery::numericalPrecisionPolicy()
+*/
+void QSqlDatabase::setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy)
+{
+ if(driver())
+ driver()->setNumericalPrecisionPolicy(precisionPolicy);
+ d->precisionPolicy = precisionPolicy;
+}
+
+/*!
+ Returns the current default precision policy for the database connection.
+
+ \sa QSql::NumericalPrecisionPolicy, setNumericalPrecisionPolicy(), QSqlQuery::numericalPrecisionPolicy(), QSqlQuery::setNumericalPrecisionPolicy()
+*/
+QSql::NumericalPrecisionPolicy QSqlDatabase::numericalPrecisionPolicy() const
+{
+ if(driver())
+ return driver()->numericalPrecisionPolicy();
+ else
+ return d->precisionPolicy;
+}
+
+
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, const QSqlDatabase &d)
{
diff --git a/src/sql/kernel/qsqldatabase.h b/src/sql/kernel/qsqldatabase.h
index ca6f0b0..638a8a5 100644
--- a/src/sql/kernel/qsqldatabase.h
+++ b/src/sql/kernel/qsqldatabase.h
@@ -120,6 +120,8 @@ public:
int port() const;
QString connectOptions() const;
QString connectionName() const;
+ void setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy);
+ QSql::NumericalPrecisionPolicy numericalPrecisionPolicy() const;
QSqlDriver* driver() const;
diff --git a/src/sql/kernel/qsqldriver.cpp b/src/sql/kernel/qsqldriver.cpp
index 477d531..f7492c3 100644
--- a/src/sql/kernel/qsqldriver.cpp
+++ b/src/sql/kernel/qsqldriver.cpp
@@ -72,10 +72,11 @@ public:
uint isOpen : 1;
uint isOpenError : 1;
QSqlError error;
+ QSql::NumericalPrecisionPolicy precisionPolicy;
};
inline QSqlDriverPrivate::QSqlDriverPrivate()
- : QObjectPrivate(), isOpen(false), isOpenError(false)
+ : QObjectPrivate(), isOpen(false), isOpenError(false), precisionPolicy(QSql::LowPrecisionDouble)
{
}
@@ -912,4 +913,29 @@ QString QSqlDriver::stripDelimitersImplementation(const QString &identifier, Ide
return ret;
}
+/*!
+
+ Sets the default numerical precision policy that queries use when created
+ by this driver.
+
+ Note: Setting the default precision policy doesn't affect any currently
+ active queries.
+
+ \sa QSql::NumericalPrecisionPolicy, numericalPrecisionPolicy(), QSqlQuery::setNumericalPrecisionPolicy(), QSqlQuery::numericalPrecisionPolicy()
+*/
+void QSqlDriver::setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy)
+{
+ d_func()->precisionPolicy = precisionPolicy;
+}
+
+/*!
+ Returns the current default precision policy for the database connection.
+
+ \sa QSql::NumericalPrecisionPolicy, setNumericalPrecisionPolicy(), QSqlQuery::numericalPrecisionPolicy(), QSqlQuery::setNumericalPrecisionPolicy()
+*/
+QSql::NumericalPrecisionPolicy QSqlDriver::numericalPrecisionPolicy() const
+{
+ return d_func()->precisionPolicy;
+}
+
QT_END_NAMESPACE
diff --git a/src/sql/kernel/qsqldriver.h b/src/sql/kernel/qsqldriver.h
index 8ac1471..1b52437 100644
--- a/src/sql/kernel/qsqldriver.h
+++ b/src/sql/kernel/qsqldriver.h
@@ -130,6 +130,9 @@ public:
bool isIdentifierEscaped(const QString &identifier, IdentifierType type) const; // ### Qt 5: make virtual
QString stripDelimiters(const QString &identifier, IdentifierType type) const; // ### Qt 5: make virtual
+ void setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy);
+ QSql::NumericalPrecisionPolicy numericalPrecisionPolicy() const;
+
Q_SIGNALS:
void notification(const QString &name);
diff --git a/src/sql/kernel/qsqlquery.cpp b/src/sql/kernel/qsqlquery.cpp
index 2a07e28..2e531b4 100644
--- a/src/sql/kernel/qsqlquery.cpp
+++ b/src/sql/kernel/qsqlquery.cpp
@@ -61,7 +61,6 @@ public:
~QSqlQueryPrivate();
QAtomicInt ref;
QSqlResult* sqlResult;
- QSql::NumericalPrecisionPolicy precisionPolicy;
static QSqlQueryPrivate* shared_null();
};
@@ -81,7 +80,7 @@ QSqlQueryPrivate* QSqlQueryPrivate::shared_null()
\internal
*/
QSqlQueryPrivate::QSqlQueryPrivate(QSqlResult* result)
- : ref(1), sqlResult(result), precisionPolicy(QSql::HighPrecision)
+ : ref(1), sqlResult(result)
{
if (!sqlResult)
sqlResult = nullResult();
@@ -351,14 +350,14 @@ bool QSqlQuery::exec(const QString& query)
if (d->ref != 1) {
bool fo = isForwardOnly();
*this = QSqlQuery(driver()->createResult());
- d->sqlResult->setNumericalPrecisionPolicy(d->precisionPolicy);
+ d->sqlResult->setNumericalPrecisionPolicy(d->sqlResult->numericalPrecisionPolicy());
setForwardOnly(fo);
} else {
d->sqlResult->clear();
d->sqlResult->setActive(false);
d->sqlResult->setLastError(QSqlError());
d->sqlResult->setAt(QSql::BeforeFirstRow);
- d->sqlResult->setNumericalPrecisionPolicy(d->precisionPolicy);
+ d->sqlResult->setNumericalPrecisionPolicy(d->sqlResult->numericalPrecisionPolicy());
}
d->sqlResult->setQuery(query.trimmed());
if (!driver()->isOpen() || driver()->isOpenError()) {
@@ -891,12 +890,12 @@ bool QSqlQuery::prepare(const QString& query)
bool fo = isForwardOnly();
*this = QSqlQuery(driver()->createResult());
setForwardOnly(fo);
- d->sqlResult->setNumericalPrecisionPolicy(d->precisionPolicy);
+ d->sqlResult->setNumericalPrecisionPolicy(d->sqlResult->numericalPrecisionPolicy());
} else {
d->sqlResult->setActive(false);
d->sqlResult->setLastError(QSqlError());
d->sqlResult->setAt(QSql::BeforeFirstRow);
- d->sqlResult->setNumericalPrecisionPolicy(d->precisionPolicy);
+ d->sqlResult->setNumericalPrecisionPolicy(d->sqlResult->numericalPrecisionPolicy());
}
if (!driver()) {
qWarning("QSqlQuery::prepare: no driver");
@@ -1126,10 +1125,10 @@ QVariant QSqlQuery::lastInsertId() const
Instruct the database driver to return numerical values with a
precision specified by \a precisionPolicy.
- The Oracle driver, for example, retrieves numerical values as
- strings by default to prevent the loss of precision. If the high
- precision doesn't matter, use this method to increase execution
- speed by bypassing string conversions.
+ The Oracle driver, for example, can retrieve numerical values as
+ strings to prevent the loss of precision. If high precision doesn't
+ matter, use this method to increase execution speed by bypassing
+ string conversions.
Note: Drivers that don't support fetching numerical values with low
precision will ignore the precision policy. You can use
@@ -1144,7 +1143,7 @@ QVariant QSqlQuery::lastInsertId() const
*/
void QSqlQuery::setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy)
{
- d->precisionPolicy = precisionPolicy;
+ d->sqlResult->setNumericalPrecisionPolicy(precisionPolicy);
}
/*!
@@ -1154,7 +1153,7 @@ void QSqlQuery::setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy preci
*/
QSql::NumericalPrecisionPolicy QSqlQuery::numericalPrecisionPolicy() const
{
- return d->precisionPolicy;
+ return d->sqlResult->numericalPrecisionPolicy();
}
/*!
diff --git a/src/sql/kernel/qsqlresult.cpp b/src/sql/kernel/qsqlresult.cpp
index 180bfc7..a621b24 100644
--- a/src/sql/kernel/qsqlresult.cpp
+++ b/src/sql/kernel/qsqlresult.cpp
@@ -48,6 +48,7 @@
#include "qsqlresult.h"
#include "qvector.h"
#include "qsqldriver.h"
+#include <QDebug>
QT_BEGIN_NAMESPACE
@@ -64,7 +65,7 @@ class QSqlResultPrivate
public:
QSqlResultPrivate(QSqlResult* d)
: q(d), sqldriver(0), idx(QSql::BeforeFirstRow), active(false),
- isSel(false), forwardOnly(false), bindCount(0), binds(QSqlResult::PositionalBinding)
+ isSel(false), forwardOnly(false), precisionPolicy(QSql::LowPrecisionDouble), bindCount(0), binds(QSqlResult::PositionalBinding)
{}
void clearValues()
@@ -104,6 +105,7 @@ public:
bool isSel;
QSqlError error;
bool forwardOnly;
+ QSql::NumericalPrecisionPolicy precisionPolicy;
int bindCount;
QSqlResult::BindingSyntax binds;
@@ -249,6 +251,9 @@ QSqlResult::QSqlResult(const QSqlDriver *db)
{
d = new QSqlResultPrivate(this);
d->sqldriver = db;
+ if(db) {
+ setNumericalPrecisionPolicy(db->numericalPrecisionPolicy());
+ }
}
/*!
@@ -965,8 +970,14 @@ void QSqlResult::detachFromResultSet()
*/
void QSqlResult::setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy policy)
{
- if (driver()->hasFeature(QSqlDriver::LowPrecisionNumbers))
- virtual_hook(SetNumericalPrecision, &policy);
+ d->precisionPolicy = policy;
+}
+
+/*! \internal
+ */
+QSql::NumericalPrecisionPolicy QSqlResult::numericalPrecisionPolicy() const
+{
+ return d->precisionPolicy;
}
/*! \internal
diff --git a/src/sql/kernel/qsqlresult.h b/src/sql/kernel/qsqlresult.h
index 0a3d8b9..30c933b 100644
--- a/src/sql/kernel/qsqlresult.h
+++ b/src/sql/kernel/qsqlresult.h
@@ -135,6 +135,7 @@ protected:
bool execBatch(bool arrayBind = false);
void detachFromResultSet();
void setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy policy);
+ QSql::NumericalPrecisionPolicy numericalPrecisionPolicy() const;
bool nextResult();
private: