diff options
Diffstat (limited to 'src/plugins/sqldrivers')
-rw-r--r-- | src/plugins/sqldrivers/configure.json | 33 | ||||
-rw-r--r-- | src/plugins/sqldrivers/configure.pri | 54 | ||||
-rw-r--r-- | src/plugins/sqldrivers/mysql/qsql_mysql.cpp | 32 | ||||
-rw-r--r-- | src/plugins/sqldrivers/odbc/qsql_odbc.cpp | 2 | ||||
-rw-r--r-- | src/plugins/sqldrivers/odbc/qsql_odbc_p.h | 4 | ||||
-rw-r--r-- | src/plugins/sqldrivers/psql/main.cpp | 8 | ||||
-rw-r--r-- | src/plugins/sqldrivers/psql/qsql_psql.cpp | 300 | ||||
-rw-r--r-- | src/plugins/sqldrivers/psql/qsql_psql_p.h | 19 | ||||
-rw-r--r-- | src/plugins/sqldrivers/sqlite/qsql_sqlite_p.h | 4 |
9 files changed, 224 insertions, 232 deletions
diff --git a/src/plugins/sqldrivers/configure.json b/src/plugins/sqldrivers/configure.json index 417d894978..441cdc4885 100644 --- a/src/plugins/sqldrivers/configure.json +++ b/src/plugins/sqldrivers/configure.json @@ -39,9 +39,8 @@ "libraries": { "db2": { "label": "DB2 (IBM)", - "test": { - "include": [ "sqlcli.h", "sqlcli1.h" ] - }, + "test": {}, + "headers": [ "sqlcli.h", "sqlcli1.h" ], "sources": [ { "libs": "-ldb2cli", "condition": "config.win32" }, { "libs": "-ldb2", "condition": "!config.win32" } @@ -49,9 +48,8 @@ }, "ibase": { "label": "InterBase", - "test": { - "include": "ibase.h" - }, + "test": {}, + "headers": "ibase.h", "sources": [ { "libs": "-lgds32_ms", "condition": "config.win32" }, { "libs": "-lgds", "condition": "!config.win32" } @@ -65,9 +63,9 @@ "# include <windows.h>", "#endif" ], - "include": "mysql.h", "main": "mysql_get_client_version();" }, + "headers": "mysql.h", "sources": [ { "type": "mysqlConfig", "query": "--libs_r", "cleanlibs": true }, { "type": "mysqlConfig", "query": "--libs", "cleanlibs": true }, @@ -81,12 +79,12 @@ "psql": { "label": "PostgreSQL", "test": { - "include": "libpq-fe.h", "main": [ "PQescapeBytea(0, 0, 0);", "PQunescapeBytea(0, 0);" ] }, + "headers": "libpq-fe.h", "sources": [ { "type": "pkgConfig", "args": "libpq" }, { "type": "psqlConfig" }, @@ -96,9 +94,8 @@ }, "tds": { "label": "TDS (Sybase)", - "test": { - "include": [ "sybfront.h", "sybdb.h" ] - }, + "test": {}, + "headers": [ "sybfront.h", "sybdb.h" ], "sources": [ { "type": "sybaseEnv", "libs": "-lNTWDBLIB", "condition": "config.win32" }, { "type": "sybaseEnv", "libs": "-lsybdb", "condition": "!config.win32" } @@ -106,9 +103,8 @@ }, "oci": { "label": "OCI (Oracle)", - "test": { - "include": "oci.h" - }, + "test": {}, + "headers": "oci.h", "sources": [ { "libs": "-loci", "condition": "config.win32" }, { "libs": "-lclntsh", "condition": "!config.win32" } @@ -122,12 +118,12 @@ "# include <windows.h>", "#endif" ], - "include": [ "sql.h", "sqlext.h" ], "main": [ "SQLHANDLE env;", "SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &env);" ] }, + "headers": [ "sql.h", "sqlext.h" ], "sources": [ { "libs": "-lodbc32", "condition": "config.win32" }, { "libs": "-liodbc", "condition": "config.darwin" }, @@ -136,9 +132,8 @@ }, "sqlite2": { "label": "SQLite (version 2)", - "test": { - "include": "sqlite.h" - }, + "test": {}, + "headers": "sqlite.h", "sources": [ "-lsqlite" ] @@ -147,9 +142,9 @@ "label": "SQLite (version 3)", "export": "sqlite", "test": { - "include": "sqlite3.h", "main": "sqlite3_open_v2(0, 0, 0, 0);" }, + "headers": "sqlite3.h", "sources": [ { "type": "pkgConfig", "args": "sqlite3" }, "-lsqlite3" diff --git a/src/plugins/sqldrivers/configure.pri b/src/plugins/sqldrivers/configure.pri index 24954e9514..be6a930e52 100644 --- a/src/plugins/sqldrivers/configure.pri +++ b/src/plugins/sqldrivers/configure.pri @@ -1,29 +1,16 @@ # custom tests -defineReplace(filterLibraryPath) { - str = $${1} - for (l, QMAKE_DEFAULT_LIBDIRS): \ - str -= "-L$$l" - - return($$str) -} - defineTest(qtConfLibrary_psqlConfig) { pg_config = $$config.input.psql_config - isEmpty(pg_config): \ + isEmpty(pg_config):!cross_compile: \ pg_config = $$qtConfFindInPath("pg_config") !win32:!isEmpty(pg_config) { qtRunLoggedCommand("$$pg_config --libdir", libdir)|return(false) + !qtConfResolvePathLibs($${1}.libs, $$libdir, -lpq): \ + return(false) qtRunLoggedCommand("$$pg_config --includedir", includedir)|return(false) - libdir -= $$QMAKE_DEFAULT_LIBDIRS - libs = - !isEmpty(libdir): libs += "-L$$libdir" - libs += "-lpq" - $${1}.libs = $$libs - includedir -= $$QMAKE_DEFAULT_INCDIRS - $${1}.includedir = $$includedir - export($${1}.libs) - export($${1}.includedir) + !qtConfResolvePathIncs($${1}.includedir, $$includedir, $$2): \ + return(false) return(true) } qtLog("pg_config not found.") @@ -34,8 +21,9 @@ defineTest(qtConfLibrary_psqlEnv) { # Respect PSQL_LIBS if set PSQL_LIBS = $$getenv(PSQL_LIBS) !isEmpty(PSQL_LIBS) { - eval($${1}.libs = $$PSQL_LIBS) - export($${1}.libs) + eval(libs = $$PSQL_LIBS) + !qtConfResolveLibs($${1}.libs, $$libs): \ + return(false) } else { !qtConfLibrary_inline($$1, $$2): \ return(false) @@ -45,7 +33,7 @@ defineTest(qtConfLibrary_psqlEnv) { defineTest(qtConfLibrary_mysqlConfig) { mysql_config = $$config.input.mysql_config - isEmpty(mysql_config): \ + isEmpty(mysql_config):!cross_compile: \ mysql_config = $$qtConfFindInPath("mysql_config") !isEmpty(mysql_config) { qtRunLoggedCommand("$$mysql_config --version", version)|return(false) @@ -58,7 +46,6 @@ defineTest(qtConfLibrary_mysqlConfig) { qtRunLoggedCommand("$$mysql_config $$query", libs)|return(false) qtRunLoggedCommand("$$mysql_config --include", includedir)|return(false) eval(libs = $$libs) - libs = $$filterLibraryPath($$libs) # -rdynamic should not be returned by mysql_config, but is on RHEL 6.6 libs -= -rdynamic equals($${1}.cleanlibs, true) { @@ -69,16 +56,15 @@ defineTest(qtConfLibrary_mysqlConfig) { } libs = $$cleanlibs } - $${1}.libs = $$libs + !qtConfResolveLibs($${1}.libs, $$libs): \ + return(false) eval(rawincludedir = $$includedir) rawincludedir ~= s/^-I//g includedir = for (id, rawincludedir): \ includedir += $$clean_path($$id) - includedir -= $$QMAKE_DEFAULT_INCDIRS - $${1}.includedir = $$includedir - export($${1}.libs) - export($${1}.includedir) + !qtConfResolvePathIncs($${1}.includedir, $$includedir, $$2): \ + return(false) return(true) } qtLog("mysql_config not found.") @@ -86,14 +72,14 @@ defineTest(qtConfLibrary_mysqlConfig) { } defineTest(qtConfLibrary_sybaseEnv) { - libs = + libdir = sybase = $$getenv(SYBASE) !isEmpty(sybase): \ - libs += "-L$${sybase}/lib" - eval(libs += $$getenv(SYBASE_LIBS)) - !isEmpty(libs) { - $${1}.libs = $$libs - export($${1}.libs) - } + libdir += $${sybase}/lib + eval(libs = $$getenv(SYBASE_LIBS)) + isEmpty(libs): \ + libs = $$eval($${1}.libs) + !qtConfResolvePathLibs($${1}.libs, $$libdir, $$libs): \ + return(false) return(true) } diff --git a/src/plugins/sqldrivers/mysql/qsql_mysql.cpp b/src/plugins/sqldrivers/mysql/qsql_mysql.cpp index 58da2a3c51..febbe58506 100644 --- a/src/plugins/sqldrivers/mysql/qsql_mysql.cpp +++ b/src/plugins/sqldrivers/mysql/qsql_mysql.cpp @@ -48,7 +48,9 @@ #include <qsqlquery.h> #include <qsqlrecord.h> #include <qstringlist.h> +#if QT_CONFIG(textcodec) #include <qtextcodec.h> +#endif #include <qvector.h> #include <qfile.h> #include <qdebug.h> @@ -86,7 +88,7 @@ class QMYSQLDriverPrivate : public QSqlDriverPrivate public: QMYSQLDriverPrivate() : QSqlDriverPrivate(), mysql(0), -#ifndef QT_NO_TEXTCODEC +#if QT_CONFIG(textcodec) tc(QTextCodec::codecForLocale()), #else tc(0), @@ -100,7 +102,7 @@ public: static inline QString toUnicode(QTextCodec *tc, const char *str) { -#ifdef QT_NO_TEXTCODEC +#if !QT_CONFIG(textcodec) Q_UNUSED(tc); return QString::fromLatin1(str); #else @@ -110,7 +112,7 @@ static inline QString toUnicode(QTextCodec *tc, const char *str) static inline QString toUnicode(QTextCodec *tc, const char *str, int length) { -#ifdef QT_NO_TEXTCODEC +#if !QT_CONFIG(textcodec) Q_UNUSED(tc); return QString::fromLatin1(str, length); #else @@ -120,7 +122,7 @@ static inline QString toUnicode(QTextCodec *tc, const char *str, int length) static inline QByteArray fromUnicode(QTextCodec *tc, const QString &str) { -#ifdef QT_NO_TEXTCODEC +#if !QT_CONFIG(textcodec) Q_UNUSED(tc); return str.toLatin1(); #else @@ -195,6 +197,7 @@ protected: QSqlRecord record() const override; void virtual_hook(int id, void *data) override; bool nextResult() override; + void detachFromResultSet() override; #if MYSQL_VERSION_ID >= 40108 bool prepare(const QString &stmt) override; @@ -255,7 +258,7 @@ public: bool preparedQuery; }; -#ifndef QT_NO_TEXTCODEC +#if QT_CONFIG(textcodec) static QTextCodec* codec(MYSQL* mysql) { #if MYSQL_VERSION_ID >= 32321 @@ -265,7 +268,7 @@ static QTextCodec* codec(MYSQL* mysql) #endif return QTextCodec::codecForLocale(); } -#endif // QT_NO_TEXTCODEC +#endif // textcodec static QSqlError qMakeError(const QString& err, QSqlError::ErrorType type, const QMYSQLDriverPrivate* p) @@ -802,6 +805,15 @@ int QMYSQLResult::numRowsAffected() return d->rowsAffected; } +void QMYSQLResult::detachFromResultSet() +{ + Q_D(QMYSQLResult); + + if (d->preparedQuery) { + mysql_stmt_free_result(d->stmt); + } +} + QVariant QMYSQLResult::lastInsertId() const { Q_D(const QMYSQLResult); @@ -1205,7 +1217,7 @@ QMYSQLDriver::QMYSQLDriver(MYSQL * con, QObject * parent) init(); if (con) { d->mysql = (MYSQL *) con; -#ifndef QT_NO_TEXTCODEC +#if QT_CONFIG(textcodec) d->tc = codec(con); #endif setOpen(true); @@ -1434,14 +1446,14 @@ bool QMYSQLDriver::open(const QString& db, if (mysql_get_client_version() >= 50503 && mysql_get_server_version(d->mysql) >= 50503) { // force the communication to be utf8mb4 (only utf8mb4 supports 4-byte characters) mysql_set_character_set(d->mysql, "utf8mb4"); -#ifndef QT_NO_TEXTCODEC +#if QT_CONFIG(textcodec) d->tc = QTextCodec::codecForName("UTF-8"); #endif } else { // force the communication to be utf8 mysql_set_character_set(d->mysql, "utf8"); -#ifndef QT_NO_TEXTCODEC +#if QT_CONFIG(textcodec) d->tc = codec(d->mysql); #endif } @@ -1539,7 +1551,7 @@ QSqlIndex QMYSQLDriver::primaryIndex(const QString& tablename) const QSqlQuery i(createResult()); QString stmt(QLatin1String("show index from %1;")); QSqlRecord fil = record(tablename); - i.exec(stmt.arg(tablename)); + i.exec(stmt.arg(escapeIdentifier(tablename, QSqlDriver::TableName))); while (i.isActive() && i.next()) { if (i.value(2).toString() == QLatin1String("PRIMARY")) { idx.append(fil.field(i.value(4).toString())); diff --git a/src/plugins/sqldrivers/odbc/qsql_odbc.cpp b/src/plugins/sqldrivers/odbc/qsql_odbc.cpp index 74abed56a9..5bf23fdaed 100644 --- a/src/plugins/sqldrivers/odbc/qsql_odbc.cpp +++ b/src/plugins/sqldrivers/odbc/qsql_odbc.cpp @@ -345,7 +345,7 @@ static QSqlError qMakeError(const QString& err, QSqlError::ErrorType type, { int nativeCode = -1; QString message = qODBCWarn(p, &nativeCode); - return QSqlError(QLatin1String("QODBC3: ") + err, qODBCWarn(p), type, + return QSqlError(QLatin1String("QODBC3: ") + err, message, type, nativeCode != -1 ? QString::number(nativeCode) : QString()); } diff --git a/src/plugins/sqldrivers/odbc/qsql_odbc_p.h b/src/plugins/sqldrivers/odbc/qsql_odbc_p.h index ea0aa6fc8b..ccd0206f38 100644 --- a/src/plugins/sqldrivers/odbc/qsql_odbc_p.h +++ b/src/plugins/sqldrivers/odbc/qsql_odbc_p.h @@ -89,8 +89,8 @@ class Q_EXPORT_SQLDRIVER_ODBC QODBCDriver : public QSqlDriver friend class QODBCResultPrivate; public: - explicit QODBCDriver(QObject *parent=0); - QODBCDriver(SQLHANDLE env, SQLHANDLE con, QObject * parent=0); + explicit QODBCDriver(QObject *parent=nullptr); + QODBCDriver(SQLHANDLE env, SQLHANDLE con, QObject * parent=nullptr); virtual ~QODBCDriver(); bool hasFeature(DriverFeature f) const override; void close() override; diff --git a/src/plugins/sqldrivers/psql/main.cpp b/src/plugins/sqldrivers/psql/main.cpp index c5d546f6ff..a0862a238a 100644 --- a/src/plugins/sqldrivers/psql/main.cpp +++ b/src/plugins/sqldrivers/psql/main.cpp @@ -61,11 +61,9 @@ QPSQLDriverPlugin::QPSQLDriverPlugin() QSqlDriver* QPSQLDriverPlugin::create(const QString &name) { - if (name == QLatin1String("QPSQL") || name == QLatin1String("QPSQL7")) { - QPSQLDriver* driver = new QPSQLDriver(); - return driver; - } - return 0; + if (name == QLatin1String("QPSQL") || name == QLatin1String("QPSQL7")) + return new QPSQLDriver; + return nullptr; } QT_END_NAMESPACE diff --git a/src/plugins/sqldrivers/psql/qsql_psql.cpp b/src/plugins/sqldrivers/psql/qsql_psql.cpp index bf0493b0c3..c1be91cb22 100644 --- a/src/plugins/sqldrivers/psql/qsql_psql.cpp +++ b/src/plugins/sqldrivers/psql/qsql_psql.cpp @@ -42,7 +42,7 @@ #include <qcoreapplication.h> #include <qvariant.h> #include <qdatetime.h> -#include <qregexp.h> +#include <qregularexpression.h> #include <qsqlerror.h> #include <qsqlfield.h> #include <qsqlindex.h> @@ -53,6 +53,9 @@ #include <qlocale.h> #include <QtSql/private/qsqlresult_p.h> #include <QtSql/private/qsqldriver_p.h> +#include <QtCore/private/qlocale_tools_p.h> + +#include <queue> #include <libpq-fe.h> #include <pg_config.h> @@ -133,7 +136,7 @@ protected: bool nextResult() override; QVariant data(int i) override; bool isNull(int field) override; - bool reset (const QString &query) override; + bool reset(const QString &query) override; int size() override; int numRowsAffected() override; QSqlRecord record() const override; @@ -147,10 +150,10 @@ class QPSQLDriverPrivate final : public QSqlDriverPrivate Q_DECLARE_PUBLIC(QPSQLDriver) public: QPSQLDriverPrivate() : QSqlDriverPrivate(), - connection(0), + connection(nullptr), isUtf8(false), pro(QPSQLDriver::Version6), - sn(0), + sn(nullptr), pendingNotifyCheck(false), hasBackslashEscape(false), stmtCount(0), @@ -187,11 +190,13 @@ public: void QPSQLDriverPrivate::appendTables(QStringList &tl, QSqlQuery &t, QChar type) { - QString query = QString::fromLatin1("select pg_class.relname, pg_namespace.nspname from pg_class " - "left join pg_namespace on (pg_class.relnamespace = pg_namespace.oid) " - "where (pg_class.relkind = '%1') and (pg_class.relname !~ '^Inv') " - "and (pg_class.relname !~ '^pg_') " - "and (pg_namespace.nspname != 'information_schema')").arg(type); + const QString query = + QStringLiteral("SELECT pg_class.relname, pg_namespace.nspname FROM pg_class " + "LEFT JOIN pg_namespace ON (pg_class.relnamespace = pg_namespace.oid) " + "WHERE (pg_class.relkind = '") + type + + QStringLiteral("') AND (pg_class.relname !~ '^Inv') " + "AND (pg_class.relname !~ '^pg_') " + "AND (pg_namespace.nspname != 'information_schema')"); t.exec(query); while (t.next()) { QString schema = t.value(1).toString(); @@ -294,10 +299,10 @@ public: Q_DECLARE_SQLDRIVER_PRIVATE(QPSQLDriver) QPSQLResultPrivate(QPSQLResult *q, const QPSQLDriver *drv) : QSqlResultPrivate(q, drv), - result(0), + result(nullptr), + stmtId(InvalidStatementId), currentSize(-1), canFetchMoreRows(false), - stmtId(InvalidStatementId), preparedQueriesEnabled(false) { } @@ -305,25 +310,25 @@ public: void deallocatePreparedStmt(); PGresult *result; - QList<PGresult*> nextResultSets; + std::queue<PGresult*> nextResultSets; + QString preparedStmtId; + StatementId stmtId; int currentSize; bool canFetchMoreRows; - StatementId stmtId; bool preparedQueriesEnabled; - QString preparedStmtId; bool processResults(); }; -static QSqlError qMakeError(const QString& err, QSqlError::ErrorType type, - const QPSQLDriverPrivate *p, PGresult* result = 0) +static QSqlError qMakeError(const QString &err, QSqlError::ErrorType type, + const QPSQLDriverPrivate *p, PGresult *result = nullptr) { const char *s = PQerrorMessage(p->connection); QString msg = p->isUtf8 ? QString::fromUtf8(s) : QString::fromLocal8Bit(s); QString errorCode; if (result) { - errorCode = QString::fromLatin1(PQresultErrorField(result, PG_DIAG_SQLSTATE)); - msg += QString::fromLatin1("(%1)").arg(errorCode); + errorCode = QString::fromLatin1(PQresultErrorField(result, PG_DIAG_SQLSTATE)); + msg += QString::fromLatin1("(%1)").arg(errorCode); } return QSqlError(QLatin1String("QPSQL: ") + err, msg, type, errorCode); } @@ -423,7 +428,7 @@ static QVariant::Type qDecodePSQLType(int t) void QPSQLResultPrivate::deallocatePreparedStmt() { - const QString stmt = QLatin1String("DEALLOCATE ") + preparedStmtId; + const QString stmt = QStringLiteral("DEALLOCATE ") + preparedStmtId; PGresult *result = drv_d_func()->exec(stmt); if (PQresultStatus(result) != PGRES_COMMAND_OK) @@ -432,7 +437,7 @@ void QPSQLResultPrivate::deallocatePreparedStmt() preparedStmtId.clear(); } -QPSQLResult::QPSQLResult(const QPSQLDriver* db) +QPSQLResult::QPSQLResult(const QPSQLDriver *db) : QSqlResult(*new QPSQLResultPrivate(this, db)) { Q_D(QPSQLResult); @@ -460,8 +465,10 @@ void QPSQLResult::cleanup() if (d->result) PQclear(d->result); d->result = nullptr; - while (!d->nextResultSets.isEmpty()) - PQclear(d->nextResultSets.takeFirst()); + while (!d->nextResultSets.empty()) { + PQclear(d->nextResultSets.front()); + d->nextResultSets.pop(); + } if (d->stmtId != InvalidStatementId) { if (d->drv_d_func()) d->drv_d_func()->finishQuery(d->stmtId); @@ -622,7 +629,11 @@ bool QPSQLResult::nextResult() if (d->result) PQclear(d->result); - d->result = d->nextResultSets.isEmpty() ? nullptr : d->nextResultSets.takeFirst(); + d->result = nullptr; + if (!d->nextResultSets.empty()) { + d->result = d->nextResultSets.front(); + d->nextResultSets.pop(); + } return d->processResults(); } @@ -646,34 +657,37 @@ QVariant QPSQLResult::data(int i) return d->drv_d_func()->isUtf8 ? QString::fromUtf8(val) : QString::fromLatin1(val); case QVariant::LongLong: if (val[0] == '-') - return QString::fromLatin1(val).toLongLong(); + return QByteArray::fromRawData(val, qstrlen(val)).toLongLong(); else - return QString::fromLatin1(val).toULongLong(); + return QByteArray::fromRawData(val, qstrlen(val)).toULongLong(); case QVariant::Int: return atoi(val); case QVariant::Double: { if (ptype == QNUMERICOID) { - if (numericalPrecisionPolicy() != QSql::HighPrecision) { - QVariant retval; - bool convert; - double dbl=QString::fromLatin1(val).toDouble(&convert); - if (numericalPrecisionPolicy() == QSql::LowPrecisionInt64) - retval = (qlonglong)dbl; - else if (numericalPrecisionPolicy() == QSql::LowPrecisionInt32) - retval = (int)dbl; - else if (numericalPrecisionPolicy() == QSql::LowPrecisionDouble) - retval = dbl; - if (!convert) - return QVariant(); - return retval; - } - return QString::fromLatin1(val); + if (numericalPrecisionPolicy() == QSql::HighPrecision) + return QString::fromLatin1(val); + } + bool ok; + double dbl = qstrtod(val, nullptr, &ok); + if (!ok) { + if (qstricmp(val, "NaN") == 0) + dbl = qQNaN(); + else if (qstricmp(val, "Infinity") == 0) + dbl = qInf(); + else if (qstricmp(val, "-Infinity") == 0) + dbl = -qInf(); + else + return QVariant(); } - if (qstricmp(val, "Infinity") == 0) - return qInf(); - if (qstricmp(val, "-Infinity") == 0) - return -qInf(); - return QString::fromLatin1(val).toDouble(); + if (ptype == QNUMERICOID) { + if (numericalPrecisionPolicy() == QSql::LowPrecisionInt64) + return QVariant((qlonglong)dbl); + else if (numericalPrecisionPolicy() == QSql::LowPrecisionInt32) + return QVariant((int)dbl); + else if (numericalPrecisionPolicy() == QSql::LowPrecisionDouble) + return QVariant(dbl); + } + return dbl; } case QVariant::Date: if (val[0] == '\0') { @@ -701,7 +715,7 @@ QVariant QPSQLResult::data(int i) #if QT_CONFIG(datestring) if (dtval.length() < 10) { return QVariant(QDateTime()); - } else { + } else { QChar sign = dtval[dtval.size() - 3]; if (sign == QLatin1Char('-') || sign == QLatin1Char('+')) dtval += QLatin1String(":00"); return QVariant(QDateTime::fromString(dtval, Qt::ISODate).toLocalTime()); @@ -731,7 +745,7 @@ bool QPSQLResult::isNull(int field) return PQgetisnull(d->result, currentRow, field); } -bool QPSQLResult::reset (const QString& query) +bool QPSQLResult::reset(const QString &query) { Q_D(QPSQLResult); cleanup(); @@ -754,7 +768,7 @@ bool QPSQLResult::reset (const QString& query) if (!isForwardOnly()) { // Fetch all result sets right away while (PGresult *nextResultSet = d->drv_d_func()->getResult(d->stmtId)) - d->nextResultSets.append(nextResultSet); + d->nextResultSets.push(nextResultSet); } return d->processResults(); } @@ -768,7 +782,8 @@ int QPSQLResult::size() int QPSQLResult::numRowsAffected() { Q_D(const QPSQLResult); - return QString::fromLatin1(PQcmdTuples(d->result)).toInt(); + const char *tuples = PQcmdTuples(d->result); + return QByteArray::fromRawData(tuples, qstrlen(tuples)).toInt(); } QVariant QPSQLResult::lastInsertId() const @@ -777,7 +792,7 @@ QVariant QPSQLResult::lastInsertId() const if (d->drv_d_func()->pro >= QPSQLDriver::Version8_1) { QSqlQuery qry(driver()->createResult()); // Most recent sequence value obtained from nextval - if (qry.exec(QLatin1String("SELECT lastval();")) && qry.next()) + if (qry.exec(QStringLiteral("SELECT lastval();")) && qry.next()) return qry.value(0); } else if (isActive()) { Oid id = PQoidValue(d->result); @@ -857,7 +872,6 @@ QSqlRecord QPSQLResult::record() const void QPSQLResult::virtual_hook(int id, void *data) { Q_ASSERT(data); - QSqlResult::virtual_hook(id, data); } @@ -868,15 +882,13 @@ static QString qCreateParamString(const QVector<QVariant> &boundValues, const QS QString params; QSqlField f; - for (int i = 0; i < boundValues.count(); ++i) { - const QVariant &val = boundValues.at(i); - + for (const QVariant &val : boundValues) { f.setType(val.type()); if (val.isNull()) f.clear(); else f.setValue(val); - if(!params.isNull()) + if (!params.isNull()) params.append(QLatin1String(", ")); params.append(driver->formatValue(f)); } @@ -886,7 +898,7 @@ static QString qCreateParamString(const QVector<QVariant> &boundValues, const QS QString qMakePreparedStmtId() { static QBasicAtomicInt qPreparedStmtCount = Q_BASIC_ATOMIC_INITIALIZER(0); - QString id = QLatin1String("qpsqlpstmt_") + QString::number(qPreparedStmtCount.fetchAndAddRelaxed(1) + 1, 16); + QString id = QStringLiteral("qpsqlpstmt_") + QString::number(qPreparedStmtCount.fetchAndAddRelaxed(1) + 1, 16); return id; } @@ -902,7 +914,7 @@ bool QPSQLResult::prepare(const QString &query) d->deallocatePreparedStmt(); const QString stmtId = qMakePreparedStmtId(); - const QString stmt = QString::fromLatin1("PREPARE %1 AS ").arg(stmtId).append(d->positionalToNamedBinding(query)); + const QString stmt = QStringLiteral("PREPARE %1 AS ").arg(stmtId).append(d->positionalToNamedBinding(query)); PGresult *result = d->drv_d_func()->exec(stmt); @@ -930,9 +942,9 @@ bool QPSQLResult::exec() QString stmt; const QString params = qCreateParamString(boundValues(), driver()); if (params.isEmpty()) - stmt = QString::fromLatin1("EXECUTE %1").arg(d->preparedStmtId); + stmt = QStringLiteral("EXECUTE %1").arg(d->preparedStmtId); else - stmt = QString::fromLatin1("EXECUTE %1 (%2)").arg(d->preparedStmtId, params); + stmt = QStringLiteral("EXECUTE %1 (%2)").arg(d->preparedStmtId, params); d->stmtId = d->drv_d_func()->sendQuery(stmt); if (d->stmtId == InvalidStatementId) { @@ -948,7 +960,7 @@ bool QPSQLResult::exec() if (!isForwardOnly()) { // Fetch all result sets right away while (PGresult *nextResultSet = d->drv_d_func()->getResult(d->stmtId)) - d->nextResultSets.append(nextResultSet); + d->nextResultSets.push(nextResultSet); } return d->processResults(); } @@ -957,7 +969,7 @@ bool QPSQLResult::exec() bool QPSQLDriverPrivate::setEncodingUtf8() { - PGresult* result = exec("SET CLIENT_ENCODING TO 'UNICODE'"); + PGresult *result = exec("SET CLIENT_ENCODING TO 'UNICODE'"); int status = PQresultStatus(result); PQclear(result); return status == PGRES_COMMAND_OK; @@ -965,7 +977,7 @@ bool QPSQLDriverPrivate::setEncodingUtf8() void QPSQLDriverPrivate::setDatestyle() { - PGresult* result = exec("SET DATESTYLE TO 'ISO'"); + PGresult *result = exec("SET DATESTYLE TO 'ISO'"); int status = PQresultStatus(result); if (status != PGRES_COMMAND_OK) qWarning("%s", PQerrorMessage(connection)); @@ -994,7 +1006,7 @@ void QPSQLDriverPrivate::detectBackslashEscape() hasBackslashEscape = true; } else { hasBackslashEscape = false; - PGresult* result = exec(QLatin1String("SELECT '\\\\' x")); + PGresult *result = exec(QStringLiteral("SELECT '\\\\' x")); int status = PQresultStatus(result); if (status == PGRES_COMMAND_OK || status == PGRES_TUPLES_OK) if (QString::fromLatin1(PQgetvalue(result, 0, 0)) == QLatin1String("\\")) @@ -1060,8 +1072,10 @@ static QPSQLDriver::Protocol qMakePSQLVersion(int vMaj, int vMin) } case 10: return QPSQLDriver::Version10; + case 11: + return QPSQLDriver::Version11; default: - if (vMaj > 10) + if (vMaj > 11) return QPSQLDriver::UnknownLaterVersion; break; } @@ -1070,20 +1084,21 @@ static QPSQLDriver::Protocol qMakePSQLVersion(int vMaj, int vMin) static QPSQLDriver::Protocol qFindPSQLVersion(const QString &versionString) { - const QRegExp rx(QStringLiteral("(\\d+)(?:\\.(\\d+))?")); - if (rx.indexIn(versionString) != -1) { + const QRegularExpression rx(QStringLiteral("(\\d+)(?:\\.(\\d+))?")); + const QRegularExpressionMatch match = rx.match(versionString); + if (match.hasMatch()) { // Beginning with PostgreSQL version 10, a major release is indicated by // increasing the first part of the version, e.g. 10 to 11. // Before version 10, a major release was indicated by increasing either // the first or second part of the version number, e.g. 9.5 to 9.6. - int vMaj = rx.cap(1).toInt(); + int vMaj = match.capturedRef(1).toInt(); int vMin; if (vMaj >= 10) { vMin = 0; } else { - if (rx.cap(2).isEmpty()) + if (match.capturedRef(2).isEmpty()) return QPSQLDriver::VersionUnknown; - vMin = rx.cap(2).toInt(); + vMin = match.capturedRef(2).toInt(); } return qMakePSQLVersion(vMaj, vMin); } @@ -1094,7 +1109,7 @@ static QPSQLDriver::Protocol qFindPSQLVersion(const QString &versionString) QPSQLDriver::Protocol QPSQLDriverPrivate::getPSQLVersion() { QPSQLDriver::Protocol serverVersion = QPSQLDriver::Version6; - PGresult* result = exec("select version()"); + PGresult *result = exec("SELECT version()"); int status = PQresultStatus(result); if (status == PGRES_COMMAND_OK || status == PGRES_TUPLES_OK) { serverVersion = qFindPSQLVersion( @@ -1200,12 +1215,12 @@ static QString qQuote(QString s) return s; } -bool QPSQLDriver::open(const QString & db, - const QString & user, - const QString & password, - const QString & host, - int port, - const QString& connOpts) +bool QPSQLDriver::open(const QString &db, + const QString &user, + const QString &password, + const QString &host, + int port, + const QString &connOpts) { Q_D(QPSQLDriver); if (isOpen()) @@ -1234,7 +1249,7 @@ bool QPSQLDriver::open(const QString & db, setLastError(qMakeError(tr("Unable to connect"), QSqlError::ConnectionError, d)); setOpenError(true); PQfinish(d->connection); - d->connection = 0; + d->connection = nullptr; return false; } @@ -1258,12 +1273,12 @@ void QPSQLDriver::close() if (d->sn) { disconnect(d->sn, SIGNAL(activated(int)), this, SLOT(_q_handleNotification(int))); delete d->sn; - d->sn = 0; + d->sn = nullptr; } if (d->connection) PQfinish(d->connection); - d->connection = 0; + d->connection = nullptr; setOpen(false); setOpenError(false); } @@ -1281,7 +1296,7 @@ bool QPSQLDriver::beginTransaction() qWarning("QPSQLDriver::beginTransaction: Database not open"); return false; } - PGresult* res = d->exec("BEGIN"); + PGresult *res = d->exec("BEGIN"); if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) { setLastError(qMakeError(tr("Could not begin transaction"), QSqlError::TransactionError, d, res)); @@ -1299,7 +1314,7 @@ bool QPSQLDriver::commitTransaction() qWarning("QPSQLDriver::commitTransaction: Database not open"); return false; } - PGresult* res = d->exec("COMMIT"); + PGresult *res = d->exec("COMMIT"); bool transaction_failed = false; @@ -1328,7 +1343,7 @@ bool QPSQLDriver::rollbackTransaction() qWarning("QPSQLDriver::rollbackTransaction: Database not open"); return false; } - PGresult* res = d->exec("ROLLBACK"); + PGresult *res = d->exec("ROLLBACK"); if (!res || PQresultStatus(res) != PGRES_COMMAND_OK) { setLastError(qMakeError(tr("Could not rollback transaction"), QSqlError::TransactionError, d, res)); @@ -1353,8 +1368,8 @@ QStringList QPSQLDriver::tables(QSql::TableType type) const if (type & QSql::Views) const_cast<QPSQLDriverPrivate*>(d)->appendTables(tl, t, QLatin1Char('v')); if (type & QSql::SystemTables) { - t.exec(QLatin1String("select relname from pg_class where (relkind = 'r') " - "and (relname like 'pg_%') ")); + t.exec(QStringLiteral("SELECT relname FROM pg_class WHERE (relkind = 'r') " + "AND (relname LIKE 'pg_%') ")); while (t.next()) tl.append(t.value(0).toString()); } @@ -1371,7 +1386,7 @@ static void qSplitTableName(QString &tablename, QString &schema) tablename = tablename.mid(dot + 1); } -QSqlIndex QPSQLDriver::primaryIndex(const QString& tablename) const +QSqlIndex QPSQLDriver::primaryIndex(const QString &tablename) const { QSqlIndex idx(tablename); if (!isOpen()) @@ -1381,31 +1396,23 @@ QSqlIndex QPSQLDriver::primaryIndex(const QString& tablename) const QString tbl = tablename; QString schema; qSplitTableName(tbl, schema); - - if (isIdentifierEscaped(tbl, QSqlDriver::TableName)) - tbl = stripDelimiters(tbl, QSqlDriver::TableName); - else - tbl = std::move(tbl).toLower(); - - if (isIdentifierEscaped(schema, QSqlDriver::TableName)) - schema = stripDelimiters(schema, QSqlDriver::TableName); - else - schema = std::move(schema).toLower(); - - QString stmt = QLatin1String("SELECT pg_attribute.attname, pg_attribute.atttypid::int, " - "pg_class.relname " - "FROM pg_attribute, pg_class " - "WHERE %1 pg_class.oid IN " - "(SELECT indexrelid FROM pg_index WHERE indisprimary = true AND indrelid IN " - "(SELECT oid FROM pg_class WHERE relname = '%2')) " - "AND pg_attribute.attrelid = pg_class.oid " - "AND pg_attribute.attisdropped = false " - "ORDER BY pg_attribute.attnum"); + schema = stripDelimiters(schema, QSqlDriver::TableName); + tbl = stripDelimiters(tbl, QSqlDriver::TableName); + + QString stmt = QStringLiteral("SELECT pg_attribute.attname, pg_attribute.atttypid::int, " + "pg_class.relname " + "FROM pg_attribute, pg_class " + "WHERE %1 pg_class.oid IN " + "(SELECT indexrelid FROM pg_index WHERE indisprimary = true AND indrelid IN " + "(SELECT oid FROM pg_class WHERE relname = '%2')) " + "AND pg_attribute.attrelid = pg_class.oid " + "AND pg_attribute.attisdropped = false " + "ORDER BY pg_attribute.attnum"); if (schema.isEmpty()) - stmt = stmt.arg(QLatin1String("pg_table_is_visible(pg_class.oid) AND")); + stmt = stmt.arg(QStringLiteral("pg_table_is_visible(pg_class.oid) AND")); else - stmt = stmt.arg(QString::fromLatin1("pg_class.relnamespace = (select oid from " - "pg_namespace where pg_namespace.nspname = '%1') AND").arg(schema)); + stmt = stmt.arg(QStringLiteral("pg_class.relnamespace = (SELECT oid FROM " + "pg_namespace WHERE pg_namespace.nspname = '%1') AND").arg(schema)); i.exec(stmt.arg(tbl)); while (i.isActive() && i.next()) { @@ -1416,7 +1423,7 @@ QSqlIndex QPSQLDriver::primaryIndex(const QString& tablename) const return idx; } -QSqlRecord QPSQLDriver::record(const QString& tablename) const +QSqlRecord QPSQLDriver::record(const QString &tablename) const { QSqlRecord info; if (!isOpen()) @@ -1425,34 +1432,26 @@ QSqlRecord QPSQLDriver::record(const QString& tablename) const QString tbl = tablename; QString schema; qSplitTableName(tbl, schema); - - if (isIdentifierEscaped(tbl, QSqlDriver::TableName)) - tbl = stripDelimiters(tbl, QSqlDriver::TableName); - else - tbl = std::move(tbl).toLower(); - - if (isIdentifierEscaped(schema, QSqlDriver::TableName)) - schema = stripDelimiters(schema, QSqlDriver::TableName); - else - schema = std::move(schema).toLower(); - - QString stmt = QLatin1String("select pg_attribute.attname, pg_attribute.atttypid::int, " - "pg_attribute.attnotnull, pg_attribute.attlen, pg_attribute.atttypmod, " - "pg_attrdef.adsrc " - "from pg_class, pg_attribute " - "left join pg_attrdef on (pg_attrdef.adrelid = " - "pg_attribute.attrelid and pg_attrdef.adnum = pg_attribute.attnum) " - "where %1 " - "and pg_class.relname = '%2' " - "and pg_attribute.attnum > 0 " - "and pg_attribute.attrelid = pg_class.oid " - "and pg_attribute.attisdropped = false " - "order by pg_attribute.attnum"); + schema = stripDelimiters(schema, QSqlDriver::TableName); + tbl = stripDelimiters(tbl, QSqlDriver::TableName); + + QString stmt = QStringLiteral("SELECT pg_attribute.attname, pg_attribute.atttypid::int, " + "pg_attribute.attnotnull, pg_attribute.attlen, pg_attribute.atttypmod, " + "pg_attrdef.adsrc " + "FROM pg_class, pg_attribute " + "LEFT JOIN pg_attrdef ON (pg_attrdef.adrelid = " + "pg_attribute.attrelid AND pg_attrdef.adnum = pg_attribute.attnum) " + "WHERE %1 " + "AND pg_class.relname = '%2' " + "AND pg_attribute.attnum > 0 " + "AND pg_attribute.attrelid = pg_class.oid " + "AND pg_attribute.attisdropped = false " + "ORDER BY pg_attribute.attnum"); if (schema.isEmpty()) - stmt = stmt.arg(QLatin1String("pg_table_is_visible(pg_class.oid)")); + stmt = stmt.arg(QStringLiteral("pg_table_is_visible(pg_class.oid)")); else - stmt = stmt.arg(QString::fromLatin1("pg_class.relnamespace = (select oid from " - "pg_namespace where pg_namespace.nspname = '%1')").arg(schema)); + stmt = stmt.arg(QStringLiteral("pg_class.relnamespace = (SELECT oid FROM " + "pg_namespace WHERE pg_namespace.nspname = '%1')").arg(schema)); QSqlQuery query(createResult()); query.exec(stmt.arg(tbl)); @@ -1494,9 +1493,10 @@ inline void assignSpecialPsqlFloatValue(FloatType val, QString *target) QString QPSQLDriver::formatValue(const QSqlField &field, bool trimStrings) const { Q_D(const QPSQLDriver); + const auto nullStr = [](){ return QStringLiteral("NULL"); }; QString r; if (field.isNull()) { - r = QLatin1String("NULL"); + r = nullStr(); } else { switch (int(field.type())) { case QVariant::DateTime: @@ -1505,14 +1505,14 @@ QString QPSQLDriver::formatValue(const QSqlField &field, bool trimStrings) const // we force the value to be considered with a timezone information, and we force it to be UTC // this is safe since postgresql stores only the UTC value and not the timezone offset (only used // while parsing), so we have correct behavior in both case of with timezone and without tz - r = QLatin1String("TIMESTAMP WITH TIME ZONE ") + QLatin1Char('\'') + - QLocale::c().toString(field.value().toDateTime().toUTC(), QLatin1String("yyyy-MM-ddThh:mm:ss.zzz")) + + r = QStringLiteral("TIMESTAMP WITH TIME ZONE ") + QLatin1Char('\'') + + QLocale::c().toString(field.value().toDateTime().toUTC(), QStringLiteral("yyyy-MM-ddThh:mm:ss.zzz")) + QLatin1Char('Z') + QLatin1Char('\''); } else { - r = QLatin1String("NULL"); + r = nullStr(); } #else - r = QLatin1String("NULL"); + r = nullStr(); #endif // datestring break; case QVariant::Time: @@ -1522,19 +1522,19 @@ QString QPSQLDriver::formatValue(const QSqlField &field, bool trimStrings) const } else #endif { - r = QLatin1String("NULL"); + r = nullStr(); } break; case QVariant::String: r = QSqlDriver::formatValue(field, trimStrings); if (d->hasBackslashEscape) - r.replace(QLatin1String("\\"), QLatin1String("\\\\")); + r.replace(QLatin1Char('\\'), QLatin1String("\\\\")); break; case QVariant::Bool: if (field.value().toBool()) - r = QLatin1String("TRUE"); + r = QStringLiteral("TRUE"); else - r = QLatin1String("FALSE"); + r = QStringLiteral("FALSE"); break; case QVariant::ByteArray: { QByteArray ba(field.value().toByteArray()); @@ -1574,7 +1574,7 @@ QString QPSQLDriver::formatValue(const QSqlField &field, bool trimStrings) const QString QPSQLDriver::escapeIdentifier(const QString &identifier, IdentifierType) const { QString res = identifier; - if(!identifier.isEmpty() && !identifier.startsWith(QLatin1Char('"')) && !identifier.endsWith(QLatin1Char('"')) ) { + if (!identifier.isEmpty() && !identifier.startsWith(QLatin1Char('"')) && !identifier.endsWith(QLatin1Char('"')) ) { res.replace(QLatin1Char('"'), QLatin1String("\"\"")); res.prepend(QLatin1Char('"')).append(QLatin1Char('"')); res.replace(QLatin1Char('.'), QLatin1String("\".\"")); @@ -1613,7 +1613,7 @@ bool QPSQLDriver::subscribeToNotification(const QString &name) // Add the name to the list of subscriptions here so that QSQLDriverPrivate::exec knows // to check for notifications immediately after executing the LISTEN d->seid << name; - QString query = QLatin1String("LISTEN ") + escapeIdentifier(name, QSqlDriver::TableName); + QString query = QStringLiteral("LISTEN ") + escapeIdentifier(name, QSqlDriver::TableName); PGresult *result = d->exec(query); if (PQresultStatus(result) != PGRES_COMMAND_OK) { d->seid.removeLast(); @@ -1649,7 +1649,7 @@ bool QPSQLDriver::unsubscribeFromNotification(const QString &name) return false; } - QString query = QLatin1String("UNLISTEN ") + escapeIdentifier(name, QSqlDriver::TableName); + QString query = QStringLiteral("UNLISTEN ") + escapeIdentifier(name, QSqlDriver::TableName); PGresult *result = d->exec(query); if (PQresultStatus(result) != PGRES_COMMAND_OK) { setLastError(qMakeError(tr("Unable to unsubscribe"), QSqlError::StatementError, d, result)); @@ -1663,7 +1663,7 @@ bool QPSQLDriver::unsubscribeFromNotification(const QString &name) if (d->seid.isEmpty()) { disconnect(d->sn, SIGNAL(activated(int)), this, SLOT(_q_handleNotification(int))); delete d->sn; - d->sn = 0; + d->sn = nullptr; } return true; @@ -1681,8 +1681,8 @@ void QPSQLDriver::_q_handleNotification(int) d->pendingNotifyCheck = false; PQconsumeInput(d->connection); - PGnotify *notify = 0; - while((notify = PQnotifies(d->connection)) != 0) { + PGnotify *notify = nullptr; + while ((notify = PQnotifies(d->connection)) != nullptr) { QString name(QLatin1String(notify->relname)); if (d->seid.contains(name)) { QString payload; diff --git a/src/plugins/sqldrivers/psql/qsql_psql_p.h b/src/plugins/sqldrivers/psql/qsql_psql_p.h index 2873a9f851..99e0b5f60f 100644 --- a/src/plugins/sqldrivers/psql/qsql_psql_p.h +++ b/src/plugins/sqldrivers/psql/qsql_psql_p.h @@ -92,25 +92,26 @@ public: Version9_5 = 21, Version9_6 = 22, Version10 = 23, + Version11 = 24, UnknownLaterVersion = 100000 }; - explicit QPSQLDriver(QObject *parent=0); - explicit QPSQLDriver(PGconn *conn, QObject *parent=0); + explicit QPSQLDriver(QObject *parent = nullptr); + explicit QPSQLDriver(PGconn *conn, QObject *parent = nullptr); ~QPSQLDriver(); bool hasFeature(DriverFeature f) const override; - bool open(const QString & db, - const QString & user, - const QString & password, - const QString & host, + bool open(const QString &db, + const QString &user, + const QString &password, + const QString &host, int port, - const QString& connOpts) override; + const QString &connOpts) override; bool isOpen() const override; void close() override; QSqlResult *createResult() const override; QStringList tables(QSql::TableType) const override; - QSqlIndex primaryIndex(const QString& tablename) const override; - QSqlRecord record(const QString& tablename) const override; + QSqlIndex primaryIndex(const QString &tablename) const override; + QSqlRecord record(const QString &tablename) const override; Protocol protocol() const; QVariant handle() const override; diff --git a/src/plugins/sqldrivers/sqlite/qsql_sqlite_p.h b/src/plugins/sqldrivers/sqlite/qsql_sqlite_p.h index 61be4c937f..c7952bca9a 100644 --- a/src/plugins/sqldrivers/sqlite/qsql_sqlite_p.h +++ b/src/plugins/sqldrivers/sqlite/qsql_sqlite_p.h @@ -72,8 +72,8 @@ class Q_EXPORT_SQLDRIVER_SQLITE QSQLiteDriver : public QSqlDriver Q_OBJECT friend class QSQLiteResultPrivate; public: - explicit QSQLiteDriver(QObject *parent = 0); - explicit QSQLiteDriver(sqlite3 *connection, QObject *parent = 0); + explicit QSQLiteDriver(QObject *parent = nullptr); + explicit QSQLiteDriver(sqlite3 *connection, QObject *parent = nullptr); ~QSQLiteDriver(); bool hasFeature(DriverFeature f) const override; bool open(const QString & db, |