diff options
author | Christian Ehrlicher <ch.ehrlicher@gmx.de> | 2018-02-07 20:12:33 +0100 |
---|---|---|
committer | Christian Ehrlicher <ch.ehrlicher@gmx.de> | 2018-03-07 18:35:57 +0000 |
commit | 3185b40d5de1092ed2bdd83f72478a344c5fc9e9 (patch) | |
tree | 044e6b8d3182b13b94529f2a62dabdb6f98a93d5 /src/plugins/sqldrivers/psql | |
parent | 70cfe551b285b4a3e770aabbca8f8bdcfadffb09 (diff) |
SQL/Postgres: Fix support for nan, inf and -inf
Postgresql needs a special value for nan and +/- inf. This was
considered during insert but not during select.
Also remove some pre-c++11 inf/nan - handling and replace it with
Qt equivalents.
Change-Id: I044ca58e9cf673f4b100b05a0d8e25c8a9c29ec5
Reviewed-by: Robert Szefner <robertsz27@interia.pl>
Reviewed-by: Andy Shaw <andy.shaw@qt.io>
Diffstat (limited to 'src/plugins/sqldrivers/psql')
-rw-r--r-- | src/plugins/sqldrivers/psql/qsql_psql.cpp | 51 |
1 files changed, 13 insertions, 38 deletions
diff --git a/src/plugins/sqldrivers/psql/qsql_psql.cpp b/src/plugins/sqldrivers/psql/qsql_psql.cpp index f67c78b2bb..b4eb69e6cf 100644 --- a/src/plugins/sqldrivers/psql/qsql_psql.cpp +++ b/src/plugins/sqldrivers/psql/qsql_psql.cpp @@ -57,29 +57,7 @@ #include <libpq-fe.h> #include <pg_config.h> -#include <stdlib.h> -#include <math.h> -// below code taken from an example at http://www.gnu.org/software/hello/manual/autoconf/Function-Portability.html -#ifndef isnan - # define isnan(x) \ - (sizeof (x) == sizeof (long double) ? isnan_ld (x) \ - : sizeof (x) == sizeof (double) ? isnan_d (x) \ - : isnan_f (x)) - static inline int isnan_f (float x) { return x != x; } - static inline int isnan_d (double x) { return x != x; } - static inline int isnan_ld (long double x) { return x != x; } -#endif - -#ifndef isinf - # define isinf(x) \ - (sizeof (x) == sizeof (long double) ? isinf_ld (x) \ - : sizeof (x) == sizeof (double) ? isinf_d (x) \ - : isinf_f (x)) - static inline int isinf_f (float x) { return isnan (x - x); } - static inline int isinf_d (double x) { return isnan (x - x); } - static inline int isinf_ld (long double x) { return isnan (x - x); } -#endif - +#include <cmath> // workaround for postgres defining their OIDs in a private header file #define QBOOLOID 16 @@ -135,7 +113,7 @@ static const StatementId InvalidStatementId = 0; class QPSQLResultPrivate; -class QPSQLResult: public QSqlResult +class QPSQLResult final : public QSqlResult { Q_DECLARE_PRIVATE(QPSQLResult) @@ -164,7 +142,7 @@ protected: bool exec() override; }; -class QPSQLDriverPrivate : public QSqlDriverPrivate +class QPSQLDriverPrivate final : public QSqlDriverPrivate { Q_DECLARE_PUBLIC(QPSQLDriver) public: @@ -671,7 +649,7 @@ QVariant QPSQLResult::data(int i) return QString::fromLatin1(val).toULongLong(); case QVariant::Int: return atoi(val); - case QVariant::Double: + case QVariant::Double: { if (ptype == QNUMERICOID) { if (numericalPrecisionPolicy() != QSql::HighPrecision) { QVariant retval; @@ -689,7 +667,12 @@ QVariant QPSQLResult::data(int i) } return QString::fromLatin1(val); } + if (qstricmp(val, "Infinity") == 0) + return qInf(); + if (qstricmp(val, "-Infinity") == 0) + return -qInf(); return QString::fromLatin1(val).toDouble(); + } case QVariant::Date: if (val[0] == '\0') { return QVariant(QDate()); @@ -1497,18 +1480,10 @@ QSqlRecord QPSQLDriver::record(const QString& tablename) const template <class FloatType> inline void assignSpecialPsqlFloatValue(FloatType val, QString *target) { - if (isnan(val)) { - *target = QLatin1String("'NaN'"); - } else { - switch (isinf(val)) { - case 1: - *target = QLatin1String("'Infinity'"); - break; - case -1: - *target = QLatin1String("'-Infinity'"); - break; - } - } + if (qIsNaN(val)) + *target = QStringLiteral("'NaN'"); + else if (qIsInf(val)) + *target = (val < 0) ? QStringLiteral("'-Infinity'") : QStringLiteral("'Infinity'"); } QString QPSQLDriver::formatValue(const QSqlField &field, bool trimStrings) const |