2023-10-30 06:33:08 +08:00
|
|
|
// Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
|
|
|
|
/* possible connection parameters */
|
|
|
|
|
|
|
|
#ifndef TST_DATABASES_H
|
|
|
|
#define TST_DATABASES_H
|
|
|
|
|
|
|
|
#include <QSqlDatabase>
|
|
|
|
#include <QSqlDriver>
|
|
|
|
#include <QSqlError>
|
|
|
|
#include <QSqlQuery>
|
2023-11-02 05:23:55 +08:00
|
|
|
#include <QSqlRecord>
|
2023-10-30 06:33:08 +08:00
|
|
|
#include <QRegularExpression>
|
|
|
|
#include <QRegularExpressionMatch>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QScopedPointer>
|
|
|
|
#include <QVariant>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QSqlTableModel>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QJsonDocument>
|
2023-11-02 05:23:55 +08:00
|
|
|
#include <QSysInfo>
|
|
|
|
#include <QVersionNumber>
|
2023-10-30 06:33:08 +08:00
|
|
|
#include <QtSql/private/qsqldriver_p.h>
|
|
|
|
#include <QTest>
|
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
using namespace Qt::StringLiterals;
|
|
|
|
|
2023-10-30 06:33:08 +08:00
|
|
|
#define CHECK_DATABASE( db ) \
|
|
|
|
if ( !db.isValid() ) { qFatal( "db is Invalid" ); }
|
|
|
|
|
|
|
|
#define QVERIFY_SQL(q, stmt) QVERIFY2((q).stmt, tst_Databases::printError((q).lastError(), db))
|
|
|
|
#define QFAIL_SQL(q, stmt) QVERIFY2(!(q).stmt, tst_Databases::printError((q).lastError(), db))
|
|
|
|
|
|
|
|
#define DBMS_SPECIFIC(db, driver) \
|
|
|
|
if (!db.driverName().startsWith(driver)) { QSKIP(driver " specific test"); }
|
|
|
|
|
|
|
|
// to prevent nameclashes on our database server, each machine
|
|
|
|
// will use its own set of table names. Call this function to get
|
|
|
|
// "tablename_hostname"
|
|
|
|
inline static QString qTableName(const QString &prefix, const char *sourceFileName,
|
|
|
|
QSqlDatabase db, bool escape = true)
|
|
|
|
{
|
2023-11-02 05:23:55 +08:00
|
|
|
const auto hash = qHash(QLatin1String(sourceFileName) + '_' +
|
|
|
|
QSysInfo::machineHostName().replace('-', '_'));
|
|
|
|
auto tableStr = QLatin1String("dbtst") + db.driverName() + '_' + prefix +
|
|
|
|
QString::number(hash, 16);
|
|
|
|
// Oracle & Interbase/Firebird have a limit on the tablename length
|
|
|
|
QSqlDriver *drv = db.driver();
|
|
|
|
if (drv)
|
|
|
|
tableStr.truncate(drv->maximumIdentifierLength(QSqlDriver::TableName));
|
2023-10-30 06:33:08 +08:00
|
|
|
return escape ? db.driver()->escapeIdentifier(tableStr, QSqlDriver::TableName) : tableStr;
|
|
|
|
}
|
|
|
|
|
|
|
|
class tst_Databases
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
~tst_Databases()
|
|
|
|
{
|
|
|
|
close();
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns a testtable consisting of the names of all database connections if
|
|
|
|
// driverPrefix is empty, otherwise only those that start with driverPrefix.
|
2023-11-02 05:23:55 +08:00
|
|
|
int fillTestTable(const QString &driverPrefix = QString()) const
|
2023-10-30 06:33:08 +08:00
|
|
|
{
|
2023-11-02 05:23:55 +08:00
|
|
|
QTest::addColumn<QString>("dbName");
|
2023-10-30 06:33:08 +08:00
|
|
|
int count = 0;
|
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
for (const auto &dbName : std::as_const(dbNames)) {
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
if (!db.isValid())
|
2023-10-30 06:33:08 +08:00
|
|
|
continue;
|
2023-11-02 05:23:55 +08:00
|
|
|
if (driverPrefix.isEmpty() || db.driverName().startsWith(driverPrefix)) {
|
|
|
|
QTest::newRow(dbName.toLatin1()) << dbName;
|
2023-10-30 06:33:08 +08:00
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
int fillTestTableWithStrategies(const QString &driverPrefix = QString()) const
|
2023-10-30 06:33:08 +08:00
|
|
|
{
|
2023-11-02 05:23:55 +08:00
|
|
|
QTest::addColumn<QString>("dbName");
|
|
|
|
QTest::addColumn<QSqlTableModel::EditStrategy>("submitpolicy");
|
2023-10-30 06:33:08 +08:00
|
|
|
int count = 0;
|
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
for (const auto &dbName : std::as_const(dbNames)) {
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName);
|
|
|
|
if (!db.isValid())
|
2023-10-30 06:33:08 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if ( driverPrefix.isEmpty() || db.driverName().startsWith( driverPrefix ) ) {
|
2023-11-02 05:23:55 +08:00
|
|
|
QTest::newRow(QString("%1 [field]").arg(dbName).toLatin1() ) << dbName << QSqlTableModel::OnFieldChange;
|
|
|
|
QTest::newRow(QString("%1 [row]").arg(dbName).toLatin1() ) << dbName << QSqlTableModel::OnRowChange;
|
|
|
|
QTest::newRow(QString("%1 [manual]").arg(dbName).toLatin1() ) << dbName << QSqlTableModel::OnManualSubmit;
|
2023-10-30 06:33:08 +08:00
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
void addDb(const QString &driver, const QString &dbName,
|
|
|
|
const QString &user = QString(), const QString &passwd = QString(),
|
|
|
|
const QString &host = QString(), int port = -1, const QString ¶ms = QString())
|
2023-10-30 06:33:08 +08:00
|
|
|
{
|
2023-11-02 05:23:55 +08:00
|
|
|
if (!QSqlDatabase::drivers().contains(driver)) {
|
2023-10-30 06:33:08 +08:00
|
|
|
qWarning() << "Driver" << driver << "is not installed";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// construct a stupid unique name
|
2023-11-02 05:23:55 +08:00
|
|
|
QString cName = QString::number(counter++) + QLatin1Char('_') + driver + QLatin1Char('@');
|
2023-10-30 06:33:08 +08:00
|
|
|
|
|
|
|
cName += host.isEmpty() ? dbName : host;
|
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
if (port > 0)
|
|
|
|
cName += QLatin1Char(':') + QString::number(port);
|
2023-10-30 06:33:08 +08:00
|
|
|
|
|
|
|
if (driver == "QSQLITE") {
|
|
|
|
// Since the database for sqlite is generated at runtime it's always
|
|
|
|
// available, but we use QTempDir so it's always in a different
|
|
|
|
// location. Thus, let's ignore the path completely.
|
|
|
|
cName = "SQLite";
|
|
|
|
qInfo("SQLite will use the database located at %ls", qUtf16Printable(dbName));
|
|
|
|
}
|
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
auto db = QSqlDatabase::addDatabase(driver, cName);
|
|
|
|
if (!db.isValid()) {
|
2023-10-30 06:33:08 +08:00
|
|
|
qWarning( "Could not create database object" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
db.setDatabaseName(dbName);
|
|
|
|
db.setUserName(user);
|
|
|
|
db.setPassword(passwd);
|
|
|
|
db.setHostName(host);
|
|
|
|
db.setPort(port);
|
|
|
|
db.setConnectOptions(params);
|
|
|
|
dbNames.append(cName);
|
2023-10-30 06:33:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool addDbs()
|
|
|
|
{
|
|
|
|
// Test databases can be defined in a file using the following format:
|
|
|
|
//
|
|
|
|
// {
|
|
|
|
// "entries": [
|
|
|
|
// {
|
|
|
|
// "driver": "QPSQL",
|
|
|
|
// "name": "testdb",
|
|
|
|
// "username": "postgres",
|
|
|
|
// "password": "password",
|
|
|
|
// "hostname": "localhost",
|
|
|
|
// "port": 5432,
|
|
|
|
// "parameters": "extraoptions"
|
|
|
|
// },
|
|
|
|
// {
|
|
|
|
// ....
|
|
|
|
// }
|
|
|
|
// ]
|
|
|
|
// }
|
|
|
|
|
|
|
|
bool added = false;
|
|
|
|
const QString databasesFile(qgetenv("QT_TEST_DATABASES_FILE"));
|
|
|
|
QFile f(databasesFile.isEmpty() ? "testdbs.json" : databasesFile);
|
|
|
|
if (f.exists() && f.open(QIODevice::ReadOnly)) {
|
|
|
|
const QJsonDocument doc = QJsonDocument::fromJson(f.readAll());
|
|
|
|
f.close();
|
|
|
|
const QJsonValue entriesV = doc.object().value(QLatin1String("entries"));
|
|
|
|
if (!entriesV.isArray()) {
|
|
|
|
qWarning() << "No entries in " + f.fileName();
|
|
|
|
} else {
|
|
|
|
const QJsonArray entriesA = entriesV.toArray();
|
2023-11-02 05:23:55 +08:00
|
|
|
for (const auto &elem : entriesA) {
|
|
|
|
if (elem.isObject()) {
|
|
|
|
const QJsonObject object = elem.toObject();
|
2023-10-30 06:33:08 +08:00
|
|
|
addDb(object.value(QStringLiteral("driver")).toString(),
|
|
|
|
object.value(QStringLiteral("name")).toString(),
|
|
|
|
object.value(QStringLiteral("username")).toString(),
|
|
|
|
object.value(QStringLiteral("password")).toString(),
|
|
|
|
object.value(QStringLiteral("hostname")).toString(),
|
|
|
|
object.value(QStringLiteral("port")).toInt(),
|
|
|
|
object.value(QStringLiteral("parameters")).toString());
|
|
|
|
added = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QTemporaryDir *sqLiteDir = dbDir();
|
|
|
|
if (sqLiteDir) {
|
|
|
|
addDb(QStringLiteral("QSQLITE"), QDir::toNativeSeparators(sqLiteDir->path() + QStringLiteral("/sqlite.db")));
|
|
|
|
added = true;
|
|
|
|
}
|
|
|
|
return added;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 'false' return indicates a system error, for example failure to create a temporary directory.
|
|
|
|
bool open()
|
|
|
|
{
|
|
|
|
if (!addDbs())
|
|
|
|
return false;
|
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
auto it = dbNames.begin();
|
|
|
|
while (it != dbNames.end()) {
|
|
|
|
const auto &dbName = *it;
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName, false );
|
|
|
|
qDebug() << "Opening:" << dbName;
|
2023-10-30 06:33:08 +08:00
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
if (db.isValid() && !db.isOpen()) {
|
|
|
|
if (!db.open()) {
|
|
|
|
qWarning("tst_Databases: Unable to open %s on %s:\n%s", qPrintable(db.driverName()),
|
|
|
|
qPrintable(dbName), qPrintable(db.lastError().databaseText()));
|
2023-10-30 06:33:08 +08:00
|
|
|
// well... opening failed, so we just ignore the server, maybe it is not running
|
2023-11-02 05:23:55 +08:00
|
|
|
it = dbNames.erase(it);
|
2023-10-30 06:33:08 +08:00
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void close()
|
|
|
|
{
|
2023-11-02 05:23:55 +08:00
|
|
|
for (const auto &dbName : std::as_const(dbNames)) {
|
2023-10-30 06:33:08 +08:00
|
|
|
{
|
2023-11-02 05:23:55 +08:00
|
|
|
QSqlDatabase db = QSqlDatabase::database(dbName, false);
|
|
|
|
if (db.isValid() && db.isOpen())
|
2023-10-30 06:33:08 +08:00
|
|
|
db.close();
|
|
|
|
}
|
2023-11-02 05:23:55 +08:00
|
|
|
QSqlDatabase::removeDatabase(dbName);
|
2023-10-30 06:33:08 +08:00
|
|
|
}
|
|
|
|
dbNames.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// for debugging only: outputs the connection as string
|
2023-11-02 05:23:55 +08:00
|
|
|
static QString dbToString(const QSqlDatabase &db)
|
2023-10-30 06:33:08 +08:00
|
|
|
{
|
|
|
|
QString res = db.driverName() + QLatin1Char('@');
|
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
if (db.driverName().startsWith("QODBC") || db.driverName().startsWith("QOCI"))
|
2023-10-30 06:33:08 +08:00
|
|
|
res += db.databaseName();
|
2023-11-02 05:23:55 +08:00
|
|
|
else
|
2023-10-30 06:33:08 +08:00
|
|
|
res += db.hostName();
|
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
if (db.port() > 0)
|
|
|
|
res += QLatin1Char(':') + QString::number(db.port());
|
2023-10-30 06:33:08 +08:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// drop a table only if it exists to prevent warnings
|
2023-11-02 05:23:55 +08:00
|
|
|
static void safeDropTables(const QSqlDatabase &db, const QStringList &tableNames)
|
2023-10-30 06:33:08 +08:00
|
|
|
{
|
2023-11-02 05:23:55 +08:00
|
|
|
QSqlQuery q(db);
|
|
|
|
QStringList dbtables = db.tables();
|
2023-10-30 06:33:08 +08:00
|
|
|
QSqlDriver::DbmsType dbType = getDatabaseType(db);
|
2023-11-02 05:23:55 +08:00
|
|
|
for (const QString &tableName : tableNames)
|
2023-10-30 06:33:08 +08:00
|
|
|
{
|
2023-11-02 05:23:55 +08:00
|
|
|
bool wasDropped = true;
|
|
|
|
QString table = tableName;
|
|
|
|
if (db.driver()->isIdentifierEscaped(table, QSqlDriver::TableName))
|
2023-10-30 06:33:08 +08:00
|
|
|
table = db.driver()->stripDelimiters(table, QSqlDriver::TableName);
|
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
if (dbtables.contains(table, Qt::CaseInsensitive)) {
|
|
|
|
for (const QString &table2 : dbtables.filter(table, Qt::CaseInsensitive)) {
|
|
|
|
if (table2.compare(table.section('.', -1, -1), Qt::CaseInsensitive) == 0) {
|
|
|
|
table = db.driver()->escapeIdentifier(table2, QSqlDriver::TableName);
|
|
|
|
if (dbType == QSqlDriver::PostgreSQL || dbType == QSqlDriver::MimerSQL)
|
2023-10-30 06:33:08 +08:00
|
|
|
wasDropped = q.exec( "drop table " + table + " cascade");
|
|
|
|
else
|
|
|
|
wasDropped = q.exec( "drop table " + table);
|
|
|
|
dbtables.removeAll(table2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-11-02 05:23:55 +08:00
|
|
|
if (!wasDropped) {
|
2023-10-30 06:33:08 +08:00
|
|
|
qWarning() << dbToString(db) << "unable to drop table" << tableName << ':' << q.lastError();
|
|
|
|
// qWarning() << "last query:" << q.lastQuery();
|
|
|
|
// qWarning() << "dbtables:" << dbtables;
|
|
|
|
// qWarning() << "db.tables():" << db.tables();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
static void safeDropViews(const QSqlDatabase &db, const QStringList &viewNames)
|
2023-10-30 06:33:08 +08:00
|
|
|
{
|
2023-11-02 05:23:55 +08:00
|
|
|
if (isMSAccess(db)) // Access is sooo stupid.
|
|
|
|
safeDropTables(db, viewNames);
|
2023-10-30 06:33:08 +08:00
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
QSqlQuery q(db);
|
|
|
|
QStringList dbtables = db.tables(QSql::Views);
|
|
|
|
for (const QString &viewName : viewNames)
|
2023-10-30 06:33:08 +08:00
|
|
|
{
|
2023-11-02 05:23:55 +08:00
|
|
|
bool wasDropped = true;
|
|
|
|
QString view = viewName;
|
|
|
|
if (db.driver()->isIdentifierEscaped(view, QSqlDriver::TableName))
|
2023-10-30 06:33:08 +08:00
|
|
|
view = db.driver()->stripDelimiters(view, QSqlDriver::TableName);
|
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
if (dbtables.contains(view, Qt::CaseInsensitive)) {
|
|
|
|
for (const QString &view2 : dbtables.filter(view, Qt::CaseInsensitive)) {
|
|
|
|
if (view2.compare(view.section('.', -1, -1), Qt::CaseInsensitive) == 0) {
|
|
|
|
view = db.driver()->escapeIdentifier(view2, QSqlDriver::TableName);
|
|
|
|
wasDropped = q.exec("drop view " + view);
|
2023-10-30 06:33:08 +08:00
|
|
|
dbtables.removeAll(view);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
if (!wasDropped)
|
2023-10-30 06:33:08 +08:00
|
|
|
qWarning() << dbToString(db) << "unable to drop view" << viewName << ':' << q.lastError();
|
|
|
|
// << "\nlast query:" << q.lastQuery()
|
|
|
|
// << "\ndbtables:" << dbtables
|
|
|
|
// << "\ndb.tables(QSql::Views):" << db.tables(QSql::Views);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns the type name of the blob datatype for the database db.
|
|
|
|
// blobSize is only used if the db doesn't have a generic blob type
|
2023-11-02 05:23:55 +08:00
|
|
|
static QString blobTypeName(const QSqlDatabase &db, int blobSize = 10000)
|
2023-10-30 06:33:08 +08:00
|
|
|
{
|
|
|
|
const QSqlDriver::DbmsType dbType = getDatabaseType(db);
|
|
|
|
if (dbType == QSqlDriver::MySqlServer)
|
|
|
|
return "longblob";
|
|
|
|
|
|
|
|
if (dbType == QSqlDriver::PostgreSQL)
|
|
|
|
return "bytea";
|
|
|
|
|
|
|
|
if (dbType == QSqlDriver::Sybase
|
|
|
|
|| dbType == QSqlDriver::MSSqlServer
|
|
|
|
|| isMSAccess( db ) )
|
|
|
|
return "image";
|
|
|
|
|
|
|
|
if (dbType == QSqlDriver::DB2)
|
|
|
|
return QString( "blob(%1)" ).arg( blobSize );
|
|
|
|
|
|
|
|
if (dbType == QSqlDriver::Interbase)
|
|
|
|
return QString( "blob sub_type 0 segment size 4096" );
|
|
|
|
|
|
|
|
if (dbType == QSqlDriver::Oracle
|
|
|
|
|| dbType == QSqlDriver::SQLite)
|
|
|
|
return "blob";
|
|
|
|
|
|
|
|
qDebug() << "tst_Databases::blobTypeName: Don't know the blob type for" << dbToString( db );
|
|
|
|
|
|
|
|
return "blob";
|
|
|
|
}
|
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
static QString dateTimeTypeName(const QSqlDatabase &db)
|
2023-10-30 06:33:08 +08:00
|
|
|
{
|
|
|
|
const QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
|
|
|
|
if (dbType == QSqlDriver::PostgreSQL)
|
|
|
|
return QLatin1String("timestamptz");
|
|
|
|
if (dbType == QSqlDriver::Oracle && getOraVersion(db) >= 9)
|
|
|
|
return QLatin1String("timestamp(0)");
|
2023-11-02 05:23:55 +08:00
|
|
|
if (dbType == QSqlDriver::Interbase || dbType == QSqlDriver::MimerSQL)
|
2023-10-30 06:33:08 +08:00
|
|
|
return QLatin1String("timestamp");
|
|
|
|
return QLatin1String("datetime");
|
|
|
|
}
|
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
static QString timeTypeName(const QSqlDatabase &db)
|
2023-10-30 06:33:08 +08:00
|
|
|
{
|
|
|
|
const QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
|
|
|
|
if (dbType == QSqlDriver::Oracle && getOraVersion(db) >= 9)
|
|
|
|
return QLatin1String("timestamp(0)");
|
|
|
|
return QLatin1String("time");
|
|
|
|
}
|
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
static QString dateTypeName(const QSqlDatabase &db)
|
2023-10-30 06:33:08 +08:00
|
|
|
{
|
|
|
|
const QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
|
|
|
|
if (dbType == QSqlDriver::Oracle && getOraVersion(db) >= 9)
|
|
|
|
return QLatin1String("timestamp(0)");
|
|
|
|
return QLatin1String("date");
|
|
|
|
}
|
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
static QString autoFieldName(const QSqlDatabase &db)
|
2023-10-30 06:33:08 +08:00
|
|
|
{
|
|
|
|
const QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
|
|
|
|
if (dbType == QSqlDriver::MySqlServer)
|
|
|
|
return "AUTO_INCREMENT";
|
|
|
|
if (dbType == QSqlDriver::Sybase || dbType == QSqlDriver::MSSqlServer)
|
|
|
|
return "IDENTITY";
|
|
|
|
/* if (dbType == QSqlDriver::PostgreSQL)
|
|
|
|
return "SERIAL";*/
|
|
|
|
// if (dbType == QSqlDriver::DB2)
|
|
|
|
// return "GENERATED BY DEFAULT AS IDENTITY";
|
|
|
|
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
static QByteArray printError(const QSqlError &err)
|
2023-10-30 06:33:08 +08:00
|
|
|
{
|
|
|
|
QString result;
|
|
|
|
if (!err.nativeErrorCode().isEmpty())
|
2023-11-02 05:23:55 +08:00
|
|
|
result += u'(' + err.nativeErrorCode() + ") ";
|
|
|
|
result += u'\'';
|
|
|
|
if (!err.driverText().isEmpty())
|
2023-10-30 06:33:08 +08:00
|
|
|
result += err.driverText() + "' || '";
|
2023-11-02 05:23:55 +08:00
|
|
|
result += err.databaseText() + u'\'';
|
2023-10-30 06:33:08 +08:00
|
|
|
return result.toLocal8Bit();
|
|
|
|
}
|
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
static QByteArray printError(const QSqlError &err, const QSqlDatabase &db)
|
2023-10-30 06:33:08 +08:00
|
|
|
{
|
2023-11-02 05:23:55 +08:00
|
|
|
return dbToString(db).toLocal8Bit() + ": " + printError(err);
|
2023-10-30 06:33:08 +08:00
|
|
|
}
|
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
static QSqlDriver::DbmsType getDatabaseType(const QSqlDatabase &db)
|
2023-10-30 06:33:08 +08:00
|
|
|
{
|
2023-11-02 05:23:55 +08:00
|
|
|
return db.driver()->dbmsType();
|
2023-10-30 06:33:08 +08:00
|
|
|
}
|
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
static bool isMSAccess(const QSqlDatabase &db)
|
2023-10-30 06:33:08 +08:00
|
|
|
{
|
|
|
|
return db.databaseName().contains( "Access Driver", Qt::CaseInsensitive );
|
|
|
|
}
|
|
|
|
|
|
|
|
// -1 on fail, else Oracle version
|
2023-11-02 05:23:55 +08:00
|
|
|
static int getOraVersion(const QSqlDatabase &db)
|
2023-10-30 06:33:08 +08:00
|
|
|
{
|
|
|
|
int ver = -1;
|
|
|
|
QSqlQuery q( "SELECT banner FROM v$version", db );
|
|
|
|
q.next();
|
|
|
|
|
|
|
|
QRegularExpression vers("([0-9]+)\\.[0-9\\.]+[0-9]");
|
|
|
|
QRegularExpressionMatch match = vers.match(q.value(0).toString());
|
|
|
|
if (match.hasMatch()) {
|
|
|
|
bool ok;
|
|
|
|
ver = match.captured(1).toInt(&ok);
|
|
|
|
|
|
|
|
if (!ok)
|
|
|
|
ver = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ver;
|
|
|
|
}
|
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
static QVersionNumber getIbaseEngineVersion(const QSqlDatabase &db)
|
2023-10-30 06:33:08 +08:00
|
|
|
{
|
|
|
|
QSqlQuery q(db);
|
2023-11-02 05:23:55 +08:00
|
|
|
q.exec("SELECT rdb$get_context('SYSTEM', 'ENGINE_VERSION') as version from rdb$database;"_L1);
|
|
|
|
q.next();
|
|
|
|
auto record = q.record();
|
|
|
|
auto version = QVersionNumber::fromString(record.value(0).toString());
|
|
|
|
return version;
|
2023-10-30 06:33:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
QStringList dbNames;
|
2023-11-02 05:23:55 +08:00
|
|
|
int counter = 0;
|
2023-10-30 06:33:08 +08:00
|
|
|
|
|
|
|
private:
|
|
|
|
QTemporaryDir *dbDir()
|
|
|
|
{
|
|
|
|
if (m_dbDir.isNull()) {
|
|
|
|
m_dbDir.reset(new QTemporaryDir);
|
|
|
|
if (!m_dbDir->isValid()) {
|
|
|
|
qWarning() << Q_FUNC_INFO << "Unable to create a temporary directory: " << QDir::toNativeSeparators(m_dbDir->path());
|
|
|
|
m_dbDir.reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m_dbDir.data();
|
|
|
|
}
|
|
|
|
|
|
|
|
QScopedPointer<QTemporaryDir> m_dbDir;
|
|
|
|
};
|
|
|
|
|
2023-11-02 05:23:55 +08:00
|
|
|
class TableScope
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
TableScope(const QSqlDatabase &db, const QString &fullTableName)
|
|
|
|
: m_db(db)
|
|
|
|
, m_tableName(fullTableName)
|
|
|
|
{
|
|
|
|
tst_Databases::safeDropTables(m_db, {m_tableName});
|
|
|
|
}
|
|
|
|
TableScope(const QSqlDatabase &db, const char *tableName, const char *file, bool escape = true)
|
|
|
|
: TableScope(db, qTableName(tableName, file, db, escape))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~TableScope()
|
|
|
|
{
|
|
|
|
tst_Databases::safeDropTables(m_db, {m_tableName});
|
|
|
|
}
|
|
|
|
|
|
|
|
QString tableName() const
|
|
|
|
{
|
|
|
|
return m_tableName;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
QSqlDatabase m_db;
|
|
|
|
QString m_tableName;
|
|
|
|
};
|
|
|
|
|
|
|
|
class ProcScope
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ProcScope(const QSqlDatabase &db, const char *procName, const char *file)
|
|
|
|
: m_db(db),
|
|
|
|
m_procName(qTableName(procName, file, db))
|
|
|
|
{
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
~ProcScope()
|
|
|
|
{
|
|
|
|
cleanup();
|
|
|
|
}
|
|
|
|
QString name() const
|
|
|
|
{
|
|
|
|
return m_procName;
|
|
|
|
}
|
|
|
|
protected:
|
|
|
|
void cleanup()
|
|
|
|
{
|
|
|
|
QSqlQuery q(m_db);
|
|
|
|
q.exec("DROP PROCEDURE IF EXISTS " + m_procName);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
QSqlDatabase m_db;
|
|
|
|
const QString m_procName;
|
|
|
|
};
|
|
|
|
|
2023-10-30 06:33:08 +08:00
|
|
|
#endif
|
|
|
|
|