diff options
Diffstat (limited to 'src/plugins/sqldrivers')
-rw-r--r-- | src/plugins/sqldrivers/db2/qsql_db2.cpp | 14 | ||||
-rw-r--r-- | src/plugins/sqldrivers/ibase/qsql_ibase.cpp | 9 | ||||
-rw-r--r-- | src/plugins/sqldrivers/mysql/qsql_mysql.cpp | 3 | ||||
-rw-r--r-- | src/plugins/sqldrivers/odbc/qsql_odbc.cpp | 7 | ||||
-rw-r--r-- | src/plugins/sqldrivers/psql/qsql_psql.cpp | 12 | ||||
-rw-r--r-- | src/plugins/sqldrivers/sqlite/qsql_sqlite.cpp | 126 | ||||
-rw-r--r-- | src/plugins/sqldrivers/sqlite2/qsql_sqlite2.cpp | 2 | ||||
-rw-r--r-- | src/plugins/sqldrivers/tds/qsql_tds.cpp | 4 |
8 files changed, 157 insertions, 20 deletions
diff --git a/src/plugins/sqldrivers/db2/qsql_db2.cpp b/src/plugins/sqldrivers/db2/qsql_db2.cpp index 839d80466f..2bfd99cfa9 100644 --- a/src/plugins/sqldrivers/db2/qsql_db2.cpp +++ b/src/plugins/sqldrivers/db2/qsql_db2.cpp @@ -66,6 +66,10 @@ QT_BEGIN_NAMESPACE static const int COLNAMESIZE = 255; +// Based on what is mentioned in the documentation here: +// https://www.ibm.com/support/knowledgecenter/en/SSEPEK_10.0.0/sqlref/src/tpc/db2z_limits.html +// The limit is 128 bytes for table names +static const SQLSMALLINT TABLENAMESIZE = 128; static const SQLSMALLINT qParamType[4] = { SQL_PARAM_INPUT, SQL_PARAM_INPUT, SQL_PARAM_OUTPUT, SQL_PARAM_INPUT_OUTPUT }; class QDB2DriverPrivate : public QSqlDriverPrivate @@ -297,6 +301,12 @@ static QSqlField qMakeFieldInfo(const QDB2ResultPrivate* d, int i) f.setLength(colSize == 0 ? -1 : int(colSize)); f.setPrecision(colScale == 0 ? -1 : int(colScale)); f.setSqlType(int(colType)); + SQLTCHAR tableName[TABLENAMESIZE]; + SQLSMALLINT tableNameLen; + r = SQLColAttribute(d->hStmt, i + 1, SQL_DESC_BASE_TABLE_NAME, tableName, + TABLENAMESIZE, &tableNameLen, 0); + if (r == SQL_SUCCESS) + f.setTableName(qFromTChar(tableName)); return f; } @@ -1394,7 +1404,9 @@ QSqlRecord QDB2Driver::record(const QString& tableName) const SQL_FETCH_NEXT, 0); while (r == SQL_SUCCESS) { - fil.append(qMakeFieldInfo(hStmt)); + QSqlField fld = qMakeFieldInfo(hStmt); + fld.setTableName(tableName); + fil.append(fld); r = SQLFetchScroll(hStmt, SQL_FETCH_NEXT, 0); diff --git a/src/plugins/sqldrivers/ibase/qsql_ibase.cpp b/src/plugins/sqldrivers/ibase/qsql_ibase.cpp index 2b75f3c964..c50fc7916a 100644 --- a/src/plugins/sqldrivers/ibase/qsql_ibase.cpp +++ b/src/plugins/sqldrivers/ibase/qsql_ibase.cpp @@ -1383,7 +1383,8 @@ QSqlRecord QIBaseResult::record() const for (int i = 0; i < d->sqlda->sqld; ++i) { v = d->sqlda->sqlvar[i]; QSqlField f(QString::fromLatin1(v.aliasname, v.aliasname_length).simplified(), - qIBaseTypeName2(v.sqltype, v.sqlscale < 0)); + qIBaseTypeName2(v.sqltype, v.sqlscale < 0), + QString::fromLatin1(v.relname, v.relname_length)); f.setLength(v.sqllen); f.setPrecision(qAbs(v.sqlscale)); f.setRequiredStatus((v.sqltype & 1) == 0 ? QSqlField::Required : QSqlField::Optional); @@ -1686,7 +1687,7 @@ QSqlRecord QIBaseDriver::record(const QString& tablename) const while (q.next()) { int type = q.value(1).toInt(); bool hasScale = q.value(3).toInt() < 0; - QSqlField f(q.value(0).toString().simplified(), qIBaseTypeName(type, hasScale)); + QSqlField f(q.value(0).toString().simplified(), qIBaseTypeName(type, hasScale), tablename); if(hasScale) { f.setLength(q.value(4).toInt()); f.setPrecision(qAbs(q.value(3).toInt())); @@ -1727,7 +1728,9 @@ QSqlIndex QIBaseDriver::primaryIndex(const QString &table) const "ORDER BY b.RDB$FIELD_POSITION")); while (q.next()) { - QSqlField field(q.value(1).toString().simplified(), qIBaseTypeName(q.value(2).toInt(), q.value(3).toInt() < 0)); + QSqlField field(q.value(1).toString().simplified(), + qIBaseTypeName(q.value(2).toInt(), q.value(3).toInt() < 0), + tablename); index.append(field); //TODO: asc? desc? index.setName(q.value(0).toString()); } diff --git a/src/plugins/sqldrivers/mysql/qsql_mysql.cpp b/src/plugins/sqldrivers/mysql/qsql_mysql.cpp index 28d5ccb4a2..365f89957a 100644 --- a/src/plugins/sqldrivers/mysql/qsql_mysql.cpp +++ b/src/plugins/sqldrivers/mysql/qsql_mysql.cpp @@ -331,7 +331,8 @@ static QVariant::Type qDecodeMYSQLType(int mysqltype, uint flags) static QSqlField qToField(MYSQL_FIELD *field, QTextCodec *tc) { QSqlField f(toUnicode(tc, field->name), - qDecodeMYSQLType(int(field->type), field->flags)); + qDecodeMYSQLType(int(field->type), field->flags), + toUnicode(tc, field->table)); f.setRequired(IS_NOT_NULL(field->flags)); f.setLength(field->length); f.setPrecision(field->decimals); diff --git a/src/plugins/sqldrivers/odbc/qsql_odbc.cpp b/src/plugins/sqldrivers/odbc/qsql_odbc.cpp index 59ef42d609..c32a29c5e7 100644 --- a/src/plugins/sqldrivers/odbc/qsql_odbc.cpp +++ b/src/plugins/sqldrivers/odbc/qsql_odbc.cpp @@ -64,6 +64,7 @@ QT_BEGIN_NAMESPACE #define ODBC_CHECK_DRIVER static const int COLNAMESIZE = 256; +static const SQLSMALLINT TABLENAMESIZE = 128; //Map Qt parameter types to ODBC types static const SQLSMALLINT qParamType[4] = { SQL_PARAM_INPUT, SQL_PARAM_INPUT, SQL_PARAM_OUTPUT, SQL_PARAM_INPUT_OUTPUT }; @@ -730,6 +731,12 @@ static QSqlField qMakeFieldInfo(const SQLHANDLE hStmt, int i, QString *errorMess f.setRequired(false); // else we don't know f.setAutoValue(isAutoValue(hStmt, i)); + QVarLengthArray<SQLTCHAR> tableName(TABLENAMESIZE); + SQLSMALLINT tableNameLen; + r = SQLColAttribute(hStmt, i + 1, SQL_DESC_BASE_TABLE_NAME, tableName.data(), + TABLENAMESIZE, &tableNameLen, 0); + if (r == SQL_SUCCESS) + f.setTableName(fromSQLTCHAR(tableName, tableNameLen)); return f; } diff --git a/src/plugins/sqldrivers/psql/qsql_psql.cpp b/src/plugins/sqldrivers/psql/qsql_psql.cpp index 6ba32a0c71..35b0f9a3e3 100644 --- a/src/plugins/sqldrivers/psql/qsql_psql.cpp +++ b/src/plugins/sqldrivers/psql/qsql_psql.cpp @@ -553,6 +553,11 @@ QSqlRecord QPSQLResult::record() const f.setName(QString::fromUtf8(PQfname(d->result, i))); else f.setName(QString::fromLocal8Bit(PQfname(d->result, i))); + QSqlQuery qry(driver()->createResult()); + if (qry.exec(QStringLiteral("SELECT relname FROM pg_class WHERE pg_class.oid = %1") + .arg(PQftable(d->result, i))) && qry.next()) { + f.setTableName(qry.value(0).toString()); + } int ptype = PQftype(d->result, i); f.setType(qDecodePSQLType(ptype)); int len = PQfsize(d->result, i); @@ -1156,7 +1161,7 @@ QSqlIndex QPSQLDriver::primaryIndex(const QString& tablename) const i.exec(stmt.arg(tbl)); while (i.isActive() && i.next()) { - QSqlField f(i.value(0).toString(), qDecodePSQLType(i.value(1).toInt())); + QSqlField f(i.value(0).toString(), qDecodePSQLType(i.value(1).toInt()), tablename); idx.append(f); idx.setName(i.value(2).toString()); } @@ -1248,7 +1253,7 @@ QSqlRecord QPSQLDriver::record(const QString& tablename) const QString defVal = query.value(5).toString(); if (!defVal.isEmpty() && defVal.at(0) == QLatin1Char('\'')) defVal = defVal.mid(1, defVal.length() - 2); - QSqlField f(query.value(0).toString(), qDecodePSQLType(query.value(1).toInt())); + QSqlField f(query.value(0).toString(), qDecodePSQLType(query.value(1).toInt()), tablename); f.setRequired(query.value(2).toBool()); f.setLength(len); f.setPrecision(precision); @@ -1275,7 +1280,7 @@ QSqlRecord QPSQLDriver::record(const QString& tablename) const len = precision - 4; precision = -1; } - QSqlField f(query.value(0).toString(), qDecodePSQLType(query.value(1).toInt())); + QSqlField f(query.value(0).toString(), qDecodePSQLType(query.value(1).toInt()), tablename); f.setRequired(query.value(2).toBool()); f.setLength(len); f.setPrecision(precision); @@ -1430,6 +1435,7 @@ bool QPSQLDriver::subscribeToNotification(const QString &name) QString query = QLatin1String("LISTEN ") + escapeIdentifier(name, QSqlDriver::TableName); PGresult *result = d->exec(query); if (PQresultStatus(result) != PGRES_COMMAND_OK) { + d->seid.removeLast(); setLastError(qMakeError(tr("Unable to subscribe"), QSqlError::StatementError, d, result)); PQclear(result); return false; diff --git a/src/plugins/sqldrivers/sqlite/qsql_sqlite.cpp b/src/plugins/sqldrivers/sqlite/qsql_sqlite.cpp index 13ec024924..444b18686a 100644 --- a/src/plugins/sqldrivers/sqlite/qsql_sqlite.cpp +++ b/src/plugins/sqldrivers/sqlite/qsql_sqlite.cpp @@ -51,6 +51,10 @@ #include <qstringlist.h> #include <qvector.h> #include <qdebug.h> +#ifndef QT_NO_REGULAREXPRESSION +#include <qcache.h> +#include <qregularexpression.h> +#endif #include <QTimeZone> #if defined Q_OS_WIN @@ -209,7 +213,9 @@ void QSQLiteResultPrivate::initColumns(bool emptyResultset) QString colName = QString(reinterpret_cast<const QChar *>( sqlite3_column_name16(stmt, i)) ).remove(QLatin1Char('"')); - + const QString tableName = QString(reinterpret_cast<const QChar *>( + sqlite3_column_table_name16(stmt, i)) + ).remove(QLatin1Char('"')); // must use typeName for resolving the type to match QSqliteDriver::record QString typeName = QString(reinterpret_cast<const QChar *>( sqlite3_column_decltype16(stmt, i))); @@ -242,7 +248,7 @@ void QSQLiteResultPrivate::initColumns(bool emptyResultset) } } - QSqlField fld(colName, fieldType); + QSqlField fld(colName, fieldType, tableName); fld.setSqlType(stp); rInf.append(fld); } @@ -440,7 +446,7 @@ static QString timespecToString(const QDateTime &dateTime) bool QSQLiteResult::exec() { Q_D(QSQLiteResult); - const QVector<QVariant> values = boundValues(); + QVector<QVariant> values = boundValues(); d->skippedStatus = false; d->skipRow = false; @@ -455,8 +461,41 @@ bool QSQLiteResult::exec() d->finalize(); return false; } + int paramCount = sqlite3_bind_parameter_count(d->stmt); - if (paramCount == values.count()) { + bool paramCountIsValid = paramCount == values.count(); + +#if (SQLITE_VERSION_NUMBER >= 3003011) + // In the case of the reuse of a named placeholder + if (paramCount < values.count()) { + const auto countIndexes = [](int counter, const QList<int>& indexList) { + return counter + indexList.length(); + }; + + const int bindParamCount = std::accumulate(d->indexes.cbegin(), + d->indexes.cend(), + 0, + countIndexes); + + paramCountIsValid = bindParamCount == values.count(); + // When using named placeholders, it will reuse the index for duplicated + // placeholders. So we need to ensure the QVector has only one instance of + // each value as SQLite will do the rest for us. + QVector<QVariant> prunedValues; + QList<int> handledIndexes; + for (int i = 0, currentIndex = 0; i < values.size(); ++i) { + if (handledIndexes.contains(i)) + continue; + const auto placeHolder = QString::fromUtf8(sqlite3_bind_parameter_name(d->stmt, currentIndex + 1)); + handledIndexes << d->indexes[placeHolder]; + prunedValues << values.at(d->indexes[placeHolder].first()); + ++currentIndex; + } + values = prunedValues; + } +#endif + + if (paramCountIsValid) { for (int i = 0; i < paramCount; ++i) { res = SQLITE_OK; const QVariant value = values.at(i); @@ -483,14 +522,14 @@ bool QSQLiteResult::exec() break; case QVariant::DateTime: { const QDateTime dateTime = value.toDateTime(); - const QString str = dateTime.toString(QStringLiteral("yyyy-MM-ddThh:mm:ss.zzz") + timespecToString(dateTime)); + const QString str = dateTime.toString(QLatin1String("yyyy-MM-ddThh:mm:ss.zzz") + timespecToString(dateTime)); res = sqlite3_bind_text16(d->stmt, i + 1, str.utf16(), str.size() * sizeof(ushort), SQLITE_TRANSIENT); break; } case QVariant::Time: { const QTime time = value.toTime(); - const QString str = time.toString(QStringLiteral("hh:mm:ss.zzz")); + const QString str = time.toString(QStringViewLiteral("hh:mm:ss.zzz")); res = sqlite3_bind_text16(d->stmt, i + 1, str.utf16(), str.size() * sizeof(ushort), SQLITE_TRANSIENT); break; @@ -583,6 +622,40 @@ QVariant QSQLiteResult::handle() const ///////////////////////////////////////////////////////// +#ifndef QT_NO_REGULAREXPRESSION +static void _q_regexp(sqlite3_context* context, int argc, sqlite3_value** argv) +{ + if (Q_UNLIKELY(argc != 2)) { + sqlite3_result_int(context, 0); + return; + } + + const QString pattern = QString::fromUtf8( + reinterpret_cast<const char*>(sqlite3_value_text(argv[0]))); + const QString subject = QString::fromUtf8( + reinterpret_cast<const char*>(sqlite3_value_text(argv[1]))); + + auto cache = static_cast<QCache<QString, QRegularExpression>*>(sqlite3_user_data(context)); + auto regexp = cache->object(pattern); + const bool wasCached = regexp; + + if (!wasCached) + regexp = new QRegularExpression(pattern, QRegularExpression::DontCaptureOption | QRegularExpression::OptimizeOnFirstUsageOption); + + const bool found = subject.contains(*regexp); + + if (!wasCached) + cache->insert(pattern, regexp); + + sqlite3_result_int(context, int(found)); +} + +static void _q_regexp_cleanup(void *cache) +{ + delete static_cast<QCache<QString, QRegularExpression>*>(cache); +} +#endif + QSQLiteDriver::QSQLiteDriver(QObject * parent) : QSqlDriver(*new QSQLiteDriverPrivate, parent) { @@ -618,11 +691,17 @@ bool QSQLiteDriver::hasFeature(DriverFeature f) const case EventNotifications: return true; case QuerySize: - case NamedPlaceholders: case BatchOperations: case MultipleResultSets: case CancelQuery: return false; + case NamedPlaceholders: +#if (SQLITE_VERSION_NUMBER < 3003011) + return false; +#else + return true; +#endif + } return false; } @@ -642,6 +721,11 @@ bool QSQLiteDriver::open(const QString & db, const QString &, const QString &, c bool sharedCache = false; bool openReadOnlyOption = false; bool openUriOption = false; +#ifndef QT_NO_REGULAREXPRESSION + static const QLatin1String regexpConnectOption = QLatin1String("QSQLITE_ENABLE_REGEXP"); + bool defineRegexp = false; + int regexpCacheSize = 25; +#endif const auto opts = conOpts.splitRef(QLatin1Char(';')); for (auto option : opts) { @@ -661,18 +745,42 @@ bool QSQLiteDriver::open(const QString & db, const QString &, const QString &, c } else if (option == QLatin1String("QSQLITE_ENABLE_SHARED_CACHE")) { sharedCache = true; } +#ifndef QT_NO_REGULAREXPRESSION + else if (option.startsWith(regexpConnectOption)) { + option = option.mid(regexpConnectOption.size()).trimmed(); + if (option.isEmpty()) { + defineRegexp = true; + } else if (option.startsWith(QLatin1Char('='))) { + bool ok = false; + const int cacheSize = option.mid(1).trimmed().toInt(&ok); + if (ok) { + defineRegexp = true; + if (cacheSize > 0) + regexpCacheSize = cacheSize; + } + } + } +#endif } int openMode = (openReadOnlyOption ? SQLITE_OPEN_READONLY : (SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE)); + openMode |= (sharedCache ? SQLITE_OPEN_SHAREDCACHE : SQLITE_OPEN_PRIVATECACHE); if (openUriOption) openMode |= SQLITE_OPEN_URI; - sqlite3_enable_shared_cache(sharedCache); + openMode |= SQLITE_OPEN_NOMUTEX; if (sqlite3_open_v2(db.toUtf8().constData(), &d->access, openMode, NULL) == SQLITE_OK) { sqlite3_busy_timeout(d->access, timeOut); setOpen(true); setOpenError(false); +#ifndef QT_NO_REGULAREXPRESSION + if (defineRegexp) { + auto cache = new QCache<QString, QRegularExpression>(regexpCacheSize); + sqlite3_create_function_v2(d->access, "regexp", 2, SQLITE_UTF8, cache, &_q_regexp, NULL, + NULL, &_q_regexp_cleanup); + } +#endif return true; } else { if (d->access) { @@ -807,7 +915,7 @@ static QSqlIndex qGetTableInfo(QSqlQuery &q, const QString &tableName, bool only if (onlyPIndex && !isPk) continue; QString typeName = q.value(2).toString().toLower(); - QSqlField fld(q.value(1).toString(), qGetColumnType(typeName)); + QSqlField fld(q.value(1).toString(), qGetColumnType(typeName), tableName); if (isPk && (typeName == QLatin1String("integer"))) // INTEGER PRIMARY KEY fields are auto-generated in sqlite // INT PRIMARY KEY is not the same as INTEGER PRIMARY KEY! diff --git a/src/plugins/sqldrivers/sqlite2/qsql_sqlite2.cpp b/src/plugins/sqldrivers/sqlite2/qsql_sqlite2.cpp index 67c24e4168..93f47e3f13 100644 --- a/src/plugins/sqldrivers/sqlite2/qsql_sqlite2.cpp +++ b/src/plugins/sqldrivers/sqlite2/qsql_sqlite2.cpp @@ -576,7 +576,7 @@ QSqlIndex QSQLite2Driver::primaryIndex(const QString &tblname) const QVariant::Type type = QVariant::Invalid; if (rec.contains(name)) type = rec.field(name).type(); - index.append(QSqlField(name, type)); + index.append(QSqlField(name, type, tblname)); } return index; } diff --git a/src/plugins/sqldrivers/tds/qsql_tds.cpp b/src/plugins/sqldrivers/tds/qsql_tds.cpp index 6ebd09a572..670198af81 100644 --- a/src/plugins/sqldrivers/tds/qsql_tds.cpp +++ b/src/plugins/sqldrivers/tds/qsql_tds.cpp @@ -767,7 +767,7 @@ QSqlRecord QTDSDriver::record(const QString& tablename) const "where id = (select id from sysobjects where name = '%1')")); t.exec(stmt.arg(table)); while (t.next()) { - QSqlField f(t.value(0).toString().simplified(), qDecodeTDSType(t.value(1).toInt())); + QSqlField f(t.value(0).toString().simplified(), qDecodeTDSType(t.value(1).toInt()), tablename); f.setLength(t.value(2).toInt()); f.setPrecision(t.value(3).toInt()); f.setSqlType(t.value(1).toInt()); @@ -853,7 +853,7 @@ QSqlIndex QTDSDriver::primaryIndex(const QString& tablename) const QRegExp regx(QLatin1String("\\s*(\\S+)(?:\\s+(DESC|desc))?\\s*")); for(QStringList::Iterator it = fNames.begin(); it != fNames.end(); ++it) { regx.indexIn(*it); - QSqlField f(regx.cap(1), rec.field(regx.cap(1)).type()); + QSqlField f(regx.cap(1), rec.field(regx.cap(1)).type(), tablename); if (regx.cap(2).toLower() == QLatin1String("desc")) { idx.append(f, true); } else { |