2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the test suite of the Qt Toolkit.
|
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
|
2012-09-19 12:28:29 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-28 08:44:43 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-15 12:36:27 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2016-01-15 12:36:27 +00:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
/* possible connection parameters */
|
|
|
|
|
|
|
|
#ifndef TST_DATABASES_H
|
|
|
|
#define TST_DATABASES_H
|
|
|
|
|
|
|
|
#include <QSqlDatabase>
|
|
|
|
#include <QSqlDriver>
|
|
|
|
#include <QSqlError>
|
|
|
|
#include <QSqlQuery>
|
|
|
|
#include <QRegExp>
|
|
|
|
#include <QDir>
|
2014-04-02 12:08:29 +00:00
|
|
|
#include <QScopedPointer>
|
2011-04-27 10:05:43 +00:00
|
|
|
#include <QVariant>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QSqlTableModel>
|
2013-03-30 18:55:25 +00:00
|
|
|
#include <QtSql/private/qsqldriver_p.h>
|
2011-04-27 10:05:43 +00:00
|
|
|
#include <QtTest/QtTest>
|
|
|
|
|
|
|
|
#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) \
|
2011-10-19 02:53:13 +00:00
|
|
|
if (!db.driverName().startsWith(driver)) { QSKIP(driver " specific test"); }
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
// ### use QSystem::hostName if it is integrated in qtest/main
|
|
|
|
static QString qGetHostName()
|
|
|
|
{
|
|
|
|
static QString hostname;
|
|
|
|
|
2016-03-10 13:06:06 +00:00
|
|
|
if (hostname.isEmpty()) {
|
|
|
|
hostname = QSysInfo::machineHostName();
|
|
|
|
hostname.replace(QLatin1Char( '.' ), QLatin1Char( '_' ));
|
|
|
|
hostname.replace(QLatin1Char( '-' ), QLatin1Char( '_' ));
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return hostname;
|
|
|
|
}
|
|
|
|
|
|
|
|
// to prevent nameclashes on our database server, each machine
|
|
|
|
// will use its own set of table names. Call this function to get
|
|
|
|
// "tablename_hostname"
|
2013-03-21 10:23:31 +00:00
|
|
|
inline QString fixupTableName(const QString &tableName, QSqlDatabase db)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2013-03-21 10:23:31 +00:00
|
|
|
QString tbName = tableName;
|
|
|
|
// On Oracle we are limited to 30 character tablenames
|
|
|
|
QSqlDriverPrivate *d = static_cast<QSqlDriverPrivate *>(QObjectPrivate::get(db.driver()));
|
2014-09-16 14:03:14 +00:00
|
|
|
if (d && d->dbmsType == QSqlDriver::Oracle)
|
2013-03-21 10:23:31 +00:00
|
|
|
tbName.truncate(30);
|
|
|
|
return tbName;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2013-03-21 10:23:31 +00:00
|
|
|
inline static QString qTableName(const QString& prefix, const char *sourceFileName, QSqlDatabase db)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2013-04-04 06:45:50 +00:00
|
|
|
QString tableStr = QLatin1String("dbtst");
|
|
|
|
if (db.driverName().toLower().contains("ODBC"))
|
|
|
|
tableStr += QLatin1String("_odbc");
|
|
|
|
return fixupTableName(QString(QLatin1String("dbtst") + db.driverName() +
|
|
|
|
QString::number(qHash(QLatin1String(sourceFileName) +
|
2013-03-21 10:23:31 +00:00
|
|
|
"_" + qGetHostName().replace( "-", "_" )), 16) + "_" + prefix), db);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline static QString qTableName(const QString& prefix, QSqlDatabase db)
|
|
|
|
{
|
2013-04-04 06:45:50 +00:00
|
|
|
QString tableStr;
|
|
|
|
if (db.driverName().toLower().contains("ODBC"))
|
|
|
|
tableStr += QLatin1String("_odbc");
|
2015-10-13 07:46:56 +00:00
|
|
|
return fixupTableName(QString(db.driver()->escapeIdentifier(prefix + tableStr + QLatin1Char('_') +
|
2013-04-04 06:45:50 +00:00
|
|
|
qGetHostName(), QSqlDriver::TableName)),db);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline static bool testWhiteSpaceNames( const QString &name )
|
|
|
|
{
|
|
|
|
/* return name.startsWith( "QPSQL" )
|
|
|
|
|| name.startsWith( "QODBC" )
|
|
|
|
|| name.startsWith( "QSQLITE" )
|
|
|
|
|| name.startsWith( "QMYSQL" );*/
|
|
|
|
return name != QLatin1String("QSQLITE2");
|
|
|
|
}
|
|
|
|
|
|
|
|
inline static QString toHex( const QString& binary )
|
|
|
|
{
|
|
|
|
QString str;
|
|
|
|
static char const hexchars[] = "0123456789ABCDEF";
|
|
|
|
|
|
|
|
for ( int i = 0; i < binary.size(); i++ ) {
|
|
|
|
ushort code = binary.at(i).unicode();
|
|
|
|
str += (QChar)(hexchars[ (code >> 12) & 0x0F ]);
|
|
|
|
str += (QChar)(hexchars[ (code >> 8) & 0x0F ]);
|
|
|
|
str += (QChar)(hexchars[ (code >> 4) & 0x0F ]);
|
|
|
|
str += (QChar)(hexchars[ code & 0x0F ]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class tst_Databases
|
|
|
|
{
|
|
|
|
|
|
|
|
public:
|
|
|
|
tst_Databases(): counter( 0 )
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~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.
|
|
|
|
int fillTestTable( const QString& driverPrefix = QString() ) const
|
|
|
|
{
|
|
|
|
QTest::addColumn<QString>( "dbName" );
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
for ( int i = 0; i < dbNames.count(); ++i ) {
|
|
|
|
QSqlDatabase db = QSqlDatabase::database( dbNames.at( i ) );
|
|
|
|
|
|
|
|
if ( !db.isValid() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ( driverPrefix.isEmpty() || db.driverName().startsWith( driverPrefix ) ) {
|
|
|
|
QTest::newRow( dbNames.at( i ).toLatin1() ) << dbNames.at( i );
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fillTestTableWithStrategies( const QString& driverPrefix = QString() ) const
|
|
|
|
{
|
|
|
|
QTest::addColumn<QString>( "dbName" );
|
|
|
|
QTest::addColumn<int>("submitpolicy_i");
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
for ( int i = 0; i < dbNames.count(); ++i ) {
|
|
|
|
QSqlDatabase db = QSqlDatabase::database( dbNames.at( i ) );
|
|
|
|
|
|
|
|
if ( !db.isValid() )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if ( driverPrefix.isEmpty() || db.driverName().startsWith( driverPrefix ) ) {
|
|
|
|
QTest::newRow( QString("%1 [field]").arg(dbNames.at( i )).toLatin1() ) << dbNames.at( i ) << (int)QSqlTableModel::OnFieldChange;
|
|
|
|
QTest::newRow( QString("%1 [row]").arg(dbNames.at( i )).toLatin1() ) << dbNames.at( i ) << (int)QSqlTableModel::OnRowChange;
|
|
|
|
QTest::newRow( QString("%1 [manual]").arg(dbNames.at( i )).toLatin1() ) << dbNames.at( i ) << (int)QSqlTableModel::OnManualSubmit;
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 params = QString() )
|
|
|
|
{
|
|
|
|
QSqlDatabase db;
|
|
|
|
|
|
|
|
if ( !QSqlDatabase::drivers().contains( driver ) ) {
|
|
|
|
qWarning() << "Driver" << driver << "is not installed";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// construct a stupid unique name
|
2015-10-13 07:46:56 +00:00
|
|
|
QString cName = QString::number( counter++ ) + QLatin1Char('_') + driver + QLatin1Char('@');
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
cName += host.isEmpty() ? dbName : host;
|
|
|
|
|
|
|
|
if ( port > 0 )
|
2015-10-13 07:46:56 +00:00
|
|
|
cName += QLatin1Char(':') + QString::number( port );
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
db = QSqlDatabase::addDatabase( driver, cName );
|
|
|
|
|
|
|
|
if ( !db.isValid() ) {
|
|
|
|
qWarning( "Could not create database object" );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
db.setDatabaseName( dbName );
|
|
|
|
|
|
|
|
db.setUserName( user );
|
|
|
|
db.setPassword( passwd );
|
|
|
|
db.setHostName( host );
|
|
|
|
db.setPort( port );
|
|
|
|
db.setConnectOptions( params );
|
|
|
|
dbNames.append( cName );
|
|
|
|
}
|
|
|
|
|
2014-04-02 12:08:29 +00:00
|
|
|
bool addDbs()
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2018-01-25 08:14:03 +00:00
|
|
|
// 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();
|
|
|
|
QJsonArray::const_iterator it = entriesA.constBegin();
|
|
|
|
while (it != entriesA.constEnd()) {
|
|
|
|
if ((*it).isObject()) {
|
|
|
|
const QJsonObject object = (*it).toObject();
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-02 12:08:29 +00:00
|
|
|
QTemporaryDir *sqLiteDir = dbDir();
|
2018-01-25 08:14:03 +00:00
|
|
|
if (sqLiteDir) {
|
|
|
|
addDb(QStringLiteral("QSQLITE"), QDir::toNativeSeparators(sqLiteDir->path() + QStringLiteral("/foo.db")));
|
|
|
|
added = true;
|
|
|
|
}
|
|
|
|
return added;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
2014-04-02 12:08:29 +00:00
|
|
|
// 'false' return indicates a system error, for example failure to create a temporary directory.
|
|
|
|
bool open()
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2014-04-02 12:08:29 +00:00
|
|
|
if (!addDbs())
|
|
|
|
return false;
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
QStringList::Iterator it = dbNames.begin();
|
|
|
|
|
|
|
|
while ( it != dbNames.end() ) {
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(( *it ), false );
|
|
|
|
qDebug() << "Opening:" << (*it);
|
|
|
|
|
|
|
|
if ( db.isValid() && !db.isOpen() ) {
|
|
|
|
if ( !db.open() ) {
|
|
|
|
qWarning( "tst_Databases: Unable to open %s on %s:\n%s", qPrintable( db.driverName() ), qPrintable( *it ), qPrintable( db.lastError().databaseText() ) );
|
|
|
|
// well... opening failed, so we just ignore the server, maybe it is not running
|
|
|
|
it = dbNames.erase( it );
|
|
|
|
} else {
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-04-02 12:08:29 +00:00
|
|
|
return true;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void close()
|
|
|
|
{
|
|
|
|
for ( QStringList::Iterator it = dbNames.begin(); it != dbNames.end(); ++it ) {
|
|
|
|
{
|
|
|
|
QSqlDatabase db = QSqlDatabase::database(( *it ), false );
|
|
|
|
|
|
|
|
if ( db.isValid() && db.isOpen() )
|
|
|
|
db.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
QSqlDatabase::removeDatabase(( *it ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
dbNames.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
// for debugging only: outputs the connection as string
|
|
|
|
static QString dbToString( const QSqlDatabase db )
|
|
|
|
{
|
2015-10-13 07:46:56 +00:00
|
|
|
QString res = db.driverName() + QLatin1Char('@');
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
if ( db.driverName().startsWith( "QODBC" ) || db.driverName().startsWith( "QOCI" ) ) {
|
|
|
|
res += db.databaseName();
|
|
|
|
} else {
|
|
|
|
res += db.hostName();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( db.port() > 0 ) {
|
2015-10-13 07:46:56 +00:00
|
|
|
res += QLatin1Char(':') + QString::number( db.port() );
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// drop a table only if it exists to prevent warnings
|
|
|
|
static void safeDropTables( QSqlDatabase db, const QStringList& tableNames )
|
|
|
|
{
|
|
|
|
bool wasDropped;
|
|
|
|
QSqlQuery q( db );
|
|
|
|
QStringList dbtables=db.tables();
|
2014-11-20 11:55:56 +00:00
|
|
|
QSqlDriver::DbmsType dbType = getDatabaseType(db);
|
2011-04-27 10:05:43 +00:00
|
|
|
foreach(const QString &tableName, tableNames)
|
|
|
|
{
|
|
|
|
wasDropped = true;
|
|
|
|
QString table=tableName;
|
|
|
|
if ( db.driver()->isIdentifierEscaped(table, QSqlDriver::TableName))
|
|
|
|
table = db.driver()->stripDelimiters(table, QSqlDriver::TableName);
|
|
|
|
|
|
|
|
if ( dbtables.contains( table, Qt::CaseInsensitive ) ) {
|
|
|
|
foreach(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);
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType == QSqlDriver::PostgreSQL)
|
2011-04-27 10:05:43 +00:00
|
|
|
wasDropped = q.exec( "drop table " + table + " cascade");
|
|
|
|
else
|
|
|
|
wasDropped = q.exec( "drop table " + table);
|
|
|
|
dbtables.removeAll(table2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( !wasDropped ) {
|
|
|
|
qWarning() << dbToString(db) << "unable to drop table" << tableName << ':' << q.lastError();
|
|
|
|
// qWarning() << "last query:" << q.lastQuery();
|
|
|
|
// qWarning() << "dbtables:" << dbtables;
|
|
|
|
// qWarning() << "db.tables():" << db.tables();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void safeDropTable( QSqlDatabase db, const QString& tableName )
|
|
|
|
{
|
|
|
|
safeDropTables(db, QStringList() << tableName);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void safeDropViews( QSqlDatabase db, const QStringList &viewNames )
|
|
|
|
{
|
|
|
|
if ( isMSAccess( db ) ) // Access is sooo stupid.
|
|
|
|
safeDropTables( db, viewNames );
|
|
|
|
|
|
|
|
bool wasDropped;
|
|
|
|
QSqlQuery q( db );
|
|
|
|
QStringList dbtables=db.tables(QSql::Views);
|
|
|
|
|
|
|
|
foreach(QString viewName, viewNames)
|
|
|
|
{
|
|
|
|
wasDropped = true;
|
|
|
|
QString view=viewName;
|
|
|
|
if ( db.driver()->isIdentifierEscaped(view, QSqlDriver::TableName))
|
|
|
|
view = db.driver()->stripDelimiters(view, QSqlDriver::TableName);
|
|
|
|
|
|
|
|
if ( dbtables.contains( view, Qt::CaseInsensitive ) ) {
|
|
|
|
foreach(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);
|
|
|
|
dbtables.removeAll(view);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( !wasDropped )
|
|
|
|
qWarning() << dbToString(db) << "unable to drop view" << viewName << ':' << q.lastError();
|
|
|
|
// << "\nlast query:" << q.lastQuery()
|
|
|
|
// << "\ndbtables:" << dbtables
|
|
|
|
// << "\ndb.tables(QSql::Views):" << db.tables(QSql::Views);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void safeDropView( QSqlDatabase db, const QString& tableName )
|
|
|
|
{
|
|
|
|
safeDropViews(db, QStringList() << tableName);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
static QString blobTypeName( QSqlDatabase db, int blobSize = 10000 )
|
|
|
|
{
|
2014-11-20 11:55:56 +00:00
|
|
|
const QSqlDriver::DbmsType dbType = getDatabaseType(db);
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType == QSqlDriver::MySqlServer)
|
2011-04-27 10:05:43 +00:00
|
|
|
return "longblob";
|
|
|
|
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType == QSqlDriver::PostgreSQL)
|
2011-04-27 10:05:43 +00:00
|
|
|
return "bytea";
|
|
|
|
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType == QSqlDriver::Sybase
|
|
|
|
|| dbType == QSqlDriver::MSSqlServer
|
2011-04-27 10:05:43 +00:00
|
|
|
|| isMSAccess( db ) )
|
|
|
|
return "image";
|
|
|
|
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType == QSqlDriver::DB2)
|
2011-04-27 10:05:43 +00:00
|
|
|
return QString( "blob(%1)" ).arg( blobSize );
|
|
|
|
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType == QSqlDriver::Interbase)
|
2011-04-27 10:05:43 +00:00
|
|
|
return QString( "blob sub_type 0 segment size 4096" );
|
|
|
|
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType == QSqlDriver::Oracle
|
|
|
|
|| dbType == QSqlDriver::SQLite)
|
2011-04-27 10:05:43 +00:00
|
|
|
return "blob";
|
|
|
|
|
|
|
|
qDebug() << "tst_Databases::blobTypeName: Don't know the blob type for" << dbToString( db );
|
|
|
|
|
|
|
|
return "blob";
|
|
|
|
}
|
|
|
|
|
2012-12-20 18:20:27 +00:00
|
|
|
static QString dateTimeTypeName(QSqlDatabase db)
|
|
|
|
{
|
2014-11-20 11:55:56 +00:00
|
|
|
const QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType == QSqlDriver::PostgreSQL)
|
Added timezone support for datetime fields in PSQL
This patch adds correct timezone support in PSQL plugin. Prior to this
patch, no timezone support was provided, so only the following case
worked :
* using local time in both client application and postgresql server
* datetime were using second precision
This patch tries to take care that postgresql has two different
datatypes for date time, respectively :
* timestamp with time zone
* timestamp without time zone
Both are internally stored as UTC values, but are not parsed the same.
* timestamp with time zone assumes that there is a time zone
information and will parse date time accordingly, and then, convert
into UTC before storing them
* timestamp without time zone assumes that there is no time zone
information and will silently ignore any, unless the datetime is
explicitly specified as having a time zone, in case it will convert
it into UTC before storing it
Both are retrieved as local time values, with the following difference
* timestamp with time zone includes the timezone information
(2014-02-12 10:20:12+0100 for example)
* timestamp without time zone does not include it
The patch does the following :
* parse the date retrieved by postgresql server using QDateTime
functions, which work correctly
* always convert the date to UTC before giving it to postgresql
* force time zone so that timezone information is taken into account
by postgresql
* also adds the milliseconds when storing QDateTime values
The following configurations are tested to work :
* client and server using same timezone, timestamp with or without tz
* client and server using different timezone, timestamp with tz
The following configuration will *not* work :
* client and server using different timezones, timestamp without tz
Because data will be converted to local time by the postgresql server,
so when returned it will be different from what had been serialized.
Prior to this patch, it gave the illusion to work because since TZ
information was lost, time was stored as local time from postgresql.
Lots of inconsistencies occurred, though, in case client tz changes...
I don't expect this to be an issue since having different TZ in server
and client and *not* handling this is a broken setup anyway.
Almost based on changes proposed by julien.blanc@nmc-company.fr
[ChangeLog][QtSql] Added timezone support for datetime fields in PSQL
Task-number: QTBUG-36211
Change-Id: I5650a5ef60cb3f14f0ab619825612831c7e90c12
Reviewed-by: Mark Brand <mabrand@mabrand.nl>
2014-03-18 10:58:49 +00:00
|
|
|
return QLatin1String("timestamptz");
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType == QSqlDriver::Oracle && getOraVersion(db) >= 9)
|
2013-03-21 10:23:31 +00:00
|
|
|
return QLatin1String("timestamp(0)");
|
2012-12-20 18:20:27 +00:00
|
|
|
return QLatin1String("datetime");
|
|
|
|
}
|
|
|
|
|
2011-04-27 10:05:43 +00:00
|
|
|
static QString autoFieldName( QSqlDatabase db )
|
|
|
|
{
|
2014-11-20 11:55:56 +00:00
|
|
|
const QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType == QSqlDriver::MySqlServer)
|
2011-04-27 10:05:43 +00:00
|
|
|
return "AUTO_INCREMENT";
|
2014-09-16 14:03:14 +00:00
|
|
|
if (dbType == QSqlDriver::Sybase || dbType == QSqlDriver::MSSqlServer)
|
2011-04-27 10:05:43 +00:00
|
|
|
return "IDENTITY";
|
2014-09-16 14:03:14 +00:00
|
|
|
/* if (dbType == QSqlDriver::PostgreSQL)
|
2011-04-27 10:05:43 +00:00
|
|
|
return "SERIAL";*/
|
2014-09-16 14:03:14 +00:00
|
|
|
// if (dbType == QSqlDriver::DB2)
|
2011-04-27 10:05:43 +00:00
|
|
|
// return "GENERATED BY DEFAULT AS IDENTITY";
|
|
|
|
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
static QByteArray printError( const QSqlError& err )
|
|
|
|
{
|
|
|
|
QString result;
|
2013-12-16 22:26:44 +00:00
|
|
|
if (!err.nativeErrorCode().isEmpty())
|
|
|
|
result += '(' + err.nativeErrorCode() + ") ";
|
2011-04-27 10:05:43 +00:00
|
|
|
result += '\'';
|
|
|
|
if(!err.driverText().isEmpty())
|
|
|
|
result += err.driverText() + "' || '";
|
2015-10-13 07:46:56 +00:00
|
|
|
result += err.databaseText() + QLatin1Char('\'');
|
2011-04-27 10:05:43 +00:00
|
|
|
return result.toLocal8Bit();
|
|
|
|
}
|
|
|
|
|
|
|
|
static QByteArray printError( const QSqlError& err, const QSqlDatabase& db )
|
|
|
|
{
|
|
|
|
QString result(dbToString(db) + ": ");
|
2013-12-16 22:26:44 +00:00
|
|
|
if (!err.nativeErrorCode().isEmpty())
|
|
|
|
result += '(' + err.nativeErrorCode() + ") ";
|
2011-04-27 10:05:43 +00:00
|
|
|
result += '\'';
|
|
|
|
if(!err.driverText().isEmpty())
|
|
|
|
result += err.driverText() + "' || '";
|
2015-10-13 07:46:56 +00:00
|
|
|
result += err.databaseText() + QLatin1Char('\'');
|
2011-04-27 10:05:43 +00:00
|
|
|
return result.toLocal8Bit();
|
|
|
|
}
|
|
|
|
|
2014-11-20 11:55:56 +00:00
|
|
|
static QSqlDriver::DbmsType getDatabaseType(QSqlDatabase db)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
2013-03-30 18:55:25 +00:00
|
|
|
QSqlDriverPrivate *d = static_cast<QSqlDriverPrivate *>(QObjectPrivate::get(db.driver()));
|
2013-04-04 06:45:50 +00:00
|
|
|
return d->dbmsType;
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool isMSAccess( QSqlDatabase db )
|
|
|
|
{
|
|
|
|
return db.databaseName().contains( "Access Driver", Qt::CaseInsensitive );
|
|
|
|
}
|
|
|
|
|
|
|
|
// -1 on fail, else Oracle version
|
|
|
|
static int getOraVersion( QSqlDatabase db )
|
|
|
|
{
|
|
|
|
int ver = -1;
|
|
|
|
QSqlQuery q( "SELECT banner FROM v$version", db );
|
|
|
|
q.next();
|
|
|
|
|
|
|
|
QRegExp vers( "([0-9]+)\\.[0-9\\.]+[0-9]" );
|
|
|
|
|
|
|
|
if ( vers.indexIn( q.value( 0 ).toString() ) ) {
|
|
|
|
bool ok;
|
|
|
|
ver = vers.cap( 1 ).toInt( &ok );
|
|
|
|
|
|
|
|
if ( !ok )
|
|
|
|
ver = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ver;
|
|
|
|
}
|
|
|
|
|
|
|
|
static QString getMySqlVersion( const QSqlDatabase &db )
|
|
|
|
{
|
|
|
|
QSqlQuery q(db);
|
|
|
|
q.exec( "select version()" );
|
|
|
|
if(q.next())
|
|
|
|
return q.value( 0 ).toString();
|
|
|
|
else
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
static QString getPSQLVersion( const QSqlDatabase &db )
|
|
|
|
{
|
|
|
|
QSqlQuery q(db);
|
|
|
|
q.exec( "select version()" );
|
|
|
|
if(q.next())
|
|
|
|
return q.value( 0 ).toString();
|
|
|
|
else
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList dbNames;
|
|
|
|
int counter;
|
2014-04-02 12:08:29 +00: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;
|
2011-04-27 10:05:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|