0
|
1 |
/****************************************************************************
|
|
2 |
**
|
|
3 |
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|
4 |
** All rights reserved.
|
|
5 |
** Contact: Nokia Corporation (qt-info@nokia.com)
|
|
6 |
**
|
|
7 |
** This file is part of the test suite of the Qt Toolkit.
|
|
8 |
**
|
|
9 |
** $QT_BEGIN_LICENSE:LGPL$
|
|
10 |
** No Commercial Usage
|
|
11 |
** This file contains pre-release code and may not be distributed.
|
|
12 |
** You may use this file in accordance with the terms and conditions
|
|
13 |
** contained in the Technology Preview License Agreement accompanying
|
|
14 |
** this package.
|
|
15 |
**
|
|
16 |
** GNU Lesser General Public License Usage
|
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser
|
|
18 |
** General Public License version 2.1 as published by the Free Software
|
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the
|
|
20 |
** packaging of this file. Please review the following information to
|
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements
|
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|
23 |
**
|
|
24 |
** In addition, as a special exception, Nokia gives you certain additional
|
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception
|
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
|
27 |
**
|
|
28 |
** If you have questions regarding the use of this file, please contact
|
|
29 |
** Nokia at qt-info@nokia.com.
|
|
30 |
**
|
|
31 |
**
|
|
32 |
**
|
|
33 |
**
|
|
34 |
**
|
|
35 |
**
|
|
36 |
**
|
|
37 |
**
|
|
38 |
** $QT_END_LICENSE$
|
|
39 |
**
|
|
40 |
****************************************************************************/
|
|
41 |
/* possible connection parameters */
|
|
42 |
|
|
43 |
#ifndef TST_DATABASES_H
|
|
44 |
#define TST_DATABASES_H
|
|
45 |
|
|
46 |
#include <QSqlDatabase>
|
|
47 |
#include <QSqlDriver>
|
|
48 |
#include <QSqlError>
|
|
49 |
#include <QSqlQuery>
|
|
50 |
#include <QRegExp>
|
|
51 |
#include <QDir>
|
|
52 |
#include <QVariant>
|
|
53 |
#include <QDebug>
|
|
54 |
|
|
55 |
#include <QtTest/QtTest>
|
|
56 |
|
|
57 |
#if defined (Q_OS_WIN) || defined (Q_OS_WIN32)
|
|
58 |
# include <qt_windows.h>
|
|
59 |
# if defined (Q_OS_WINCE)
|
|
60 |
# include <winsock2.h>
|
|
61 |
# endif
|
|
62 |
#else
|
|
63 |
#include <unistd.h>
|
|
64 |
#endif
|
|
65 |
|
|
66 |
#define CHECK_DATABASE( db ) \
|
|
67 |
if ( !db.isValid() ) { qFatal( "db is Invalid" ); }
|
|
68 |
|
|
69 |
#define QVERIFY_SQL(q, stmt) QVERIFY2((q).stmt, tst_Databases::printError((q).lastError(), db))
|
|
70 |
#define QFAIL_SQL(q, stmt) QVERIFY2(!(q).stmt, tst_Databases::printError((q).lastError(), db))
|
|
71 |
|
|
72 |
#define DBMS_SPECIFIC(db, driver) \
|
|
73 |
if (!db.driverName().startsWith(driver)) { QSKIP(driver " specific test", SkipSingle); return; }
|
|
74 |
|
|
75 |
// ### use QSystem::hostName if it is integrated in qtest/main
|
|
76 |
static QString qGetHostName()
|
|
77 |
{
|
|
78 |
static QString hostname;
|
|
79 |
|
|
80 |
if ( !hostname.isEmpty() )
|
|
81 |
return hostname;
|
|
82 |
|
|
83 |
char hn[257];
|
|
84 |
|
|
85 |
if ( gethostname( hn, 255 ) == 0 ) {
|
|
86 |
hn[256] = '\0';
|
|
87 |
hostname = QString::fromLatin1( hn );
|
|
88 |
hostname.replace( QLatin1Char( '.' ), QLatin1Char( '_' ) );
|
|
89 |
hostname.replace( QLatin1Char( '-' ), QLatin1Char( '_' ) );
|
|
90 |
}
|
|
91 |
|
|
92 |
return hostname;
|
|
93 |
}
|
|
94 |
|
|
95 |
// to prevent nameclashes on our database server, each machine
|
|
96 |
// will use its own set of table names. Call this function to get
|
|
97 |
// "tablename_hostname"
|
|
98 |
inline static QString qTableName( const QString& prefix, QSqlDriver* driver = 0 )
|
|
99 |
{
|
|
100 |
if ( !driver )
|
|
101 |
return prefix + "_" + qGetHostName().replace( "-", "_" );
|
|
102 |
else
|
|
103 |
return driver->escapeIdentifier( prefix + "_" + qGetHostName(), QSqlDriver::TableName );
|
|
104 |
}
|
|
105 |
|
|
106 |
inline static bool testWhiteSpaceNames( const QString &name )
|
|
107 |
{
|
|
108 |
/* return name.startsWith( "QPSQL" )
|
|
109 |
|| name.startsWith( "QODBC" )
|
|
110 |
|| name.startsWith( "QSQLITE" )
|
|
111 |
|| name.startsWith( "QMYSQL" );*/
|
|
112 |
return name != QLatin1String("QSQLITE2");
|
|
113 |
}
|
|
114 |
|
|
115 |
inline static QString toHex( const QString& binary )
|
|
116 |
{
|
|
117 |
QString str;
|
|
118 |
static char const hexchars[] = "0123456789ABCDEF";
|
|
119 |
|
|
120 |
for ( int i = 0; i < binary.size(); i++ ) {
|
|
121 |
ushort code = binary.at(i).unicode();
|
|
122 |
str += (QChar)(hexchars[ (code >> 12) & 0x0F ]);
|
|
123 |
str += (QChar)(hexchars[ (code >> 8) & 0x0F ]);
|
|
124 |
str += (QChar)(hexchars[ (code >> 4) & 0x0F ]);
|
|
125 |
str += (QChar)(hexchars[ code & 0x0F ]);
|
|
126 |
}
|
|
127 |
|
|
128 |
return str;
|
|
129 |
}
|
|
130 |
|
|
131 |
|
|
132 |
class tst_Databases
|
|
133 |
{
|
|
134 |
|
|
135 |
public:
|
|
136 |
tst_Databases(): counter( 0 )
|
|
137 |
{
|
|
138 |
}
|
|
139 |
|
|
140 |
~tst_Databases()
|
|
141 |
{
|
|
142 |
close();
|
|
143 |
}
|
|
144 |
|
|
145 |
// returns a testtable consisting of the names of all database connections if
|
|
146 |
// driverPrefix is empty, otherwise only those that start with driverPrefix.
|
|
147 |
int fillTestTable( const QString& driverPrefix = QString() ) const
|
|
148 |
{
|
|
149 |
QTest::addColumn<QString>( "dbName" );
|
|
150 |
int count = 0;
|
|
151 |
|
|
152 |
for ( int i = 0; i < dbNames.count(); ++i ) {
|
|
153 |
QSqlDatabase db = QSqlDatabase::database( dbNames.at( i ) );
|
|
154 |
|
|
155 |
if ( !db.isValid() )
|
|
156 |
continue;
|
|
157 |
|
|
158 |
if ( driverPrefix.isEmpty() || db.driverName().startsWith( driverPrefix ) ) {
|
|
159 |
QTest::newRow( dbNames.at( i ).toLatin1() ) << dbNames.at( i );
|
|
160 |
++count;
|
|
161 |
}
|
|
162 |
}
|
|
163 |
|
|
164 |
return count;
|
|
165 |
}
|
|
166 |
|
|
167 |
void addDb( const QString& driver, const QString& dbName,
|
|
168 |
const QString& user = QString(), const QString& passwd = QString(),
|
|
169 |
const QString& host = QString(), int port = -1, const QString params = QString() )
|
|
170 |
{
|
|
171 |
QSqlDatabase db;
|
|
172 |
|
|
173 |
if ( !QSqlDatabase::drivers().contains( driver ) ) {
|
|
174 |
qWarning() << "Driver" << driver << "is not installed";
|
|
175 |
return;
|
|
176 |
}
|
|
177 |
|
|
178 |
// construct a stupid unique name
|
|
179 |
QString cName = QString::number( counter++ ) + "_" + driver + "@";
|
|
180 |
|
|
181 |
cName += host.isEmpty() ? dbName : host;
|
|
182 |
|
|
183 |
if ( port > 0 )
|
|
184 |
cName += ":" + QString::number( port );
|
|
185 |
|
|
186 |
db = QSqlDatabase::addDatabase( driver, cName );
|
|
187 |
|
|
188 |
if ( !db.isValid() ) {
|
|
189 |
qWarning( "Could not create database object" );
|
|
190 |
return;
|
|
191 |
}
|
|
192 |
|
|
193 |
db.setDatabaseName( dbName );
|
|
194 |
|
|
195 |
db.setUserName( user );
|
|
196 |
db.setPassword( passwd );
|
|
197 |
db.setHostName( host );
|
|
198 |
db.setPort( port );
|
|
199 |
db.setConnectOptions( params );
|
|
200 |
dbNames.append( cName );
|
|
201 |
}
|
|
202 |
|
|
203 |
void addDbs()
|
|
204 |
{
|
|
205 |
// addDb( "QOCI8", "//horsehead.nokia.troll.no:1521/pony.troll.no", "scott", "tiger" ); // Oracle 9i on horsehead
|
|
206 |
// addDb( "QOCI8", "//horsehead.nokia.troll.no:1521/ustest.troll.no", "scott", "tiger", "" ); // Oracle 9i on horsehead
|
|
207 |
// addDb( "QOCI8", "//iceblink.nokia.troll.no:1521/ice.troll.no", "scott", "tiger", "" ); // Oracle 8 on iceblink (not currently working)
|
|
208 |
// addDb( "QOCI", "//silence.nokia.troll.no:1521/testdb", "scott", "tiger" ); // Oracle 10g on silence
|
|
209 |
// addDb( "QOCI", "//oracle10g-nokia.trolltech.com.au:1521/XE", "scott", "tiger" ); // Oracle 10gexpress on xen
|
|
210 |
|
|
211 |
// This requires a local ODBC data source to be configured( pointing to a MySql database )
|
|
212 |
// addDb( "QODBC", "mysqlodbc", "troll", "trond" );
|
|
213 |
// addDb( "QODBC", "SqlServer", "troll", "trond" );
|
|
214 |
// addDb( "QTDS7", "testdb", "troll", "trondk", "horsehead" );
|
|
215 |
// addDb( "QODBC", "silencetestdb", "troll", "trond", "silence" );
|
|
216 |
// addDb( "QODBC", "horseheadtestdb", "troll", "trondk", "horsehead" );
|
|
217 |
|
|
218 |
// addDb( "QMYSQL3", "testdb", "troll", "trond", "horsehead.nokia.troll.no" );
|
|
219 |
// addDb( "QMYSQL3", "testdb", "troll", "trond", "horsehead.nokia.troll.no", 3307 );
|
|
220 |
// addDb( "QMYSQL3", "testdb", "troll", "trond", "horsehead.nokia.troll.no", 3308, "CLIENT_COMPRESS=1;CLIENT_SSL=1" ); // MySQL 4.1.1
|
|
221 |
// addDb( "QMYSQL3", "testdb", "troll", "trond", "horsehead.nokia.troll.no", 3309, "CLIENT_COMPRESS=1;CLIENT_SSL=1" ); // MySQL 5.0.18 Linux
|
|
222 |
// addDb( "QMYSQL3", "testdb", "troll", "trond", "silence.nokia.troll.no" ); // MySQL 5.1.36 Windows
|
|
223 |
// addDb( "QMYSQL3", "testdb", "testuser", "Ee4Gabf6_", "mysql4-nokia.trolltech.com.au" ); // MySQL 4.1.22-2.el4 linux
|
|
224 |
// addDb( "QMYSQL3", "testdb", "testuser", "Ee4Gabf6_", "mysql5-nokia.trolltech.com.au" ); // MySQL 5.0.45-7.el5 linux
|
|
225 |
|
|
226 |
// addDb( "QPSQL7", "testdb", "troll", "trond", "horsehead.nokia.troll.no" ); // V7.2 NOT SUPPORTED!
|
|
227 |
// addDb( "QPSQL7", "testdb", "troll", "trond", "horsehead.nokia.troll.no", 5434 ); // V7.2 NOT SUPPORTED! Multi-byte
|
|
228 |
// addDb( "QPSQL7", "testdb", "troll", "trond", "horsehead.nokia.troll.no", 5435 ); // V7.3
|
|
229 |
// addDb( "QPSQL7", "testdb", "troll", "trond", "horsehead.nokia.troll.no", 5436 ); // V7.4
|
|
230 |
// addDb( "QPSQL7", "testdb", "troll", "trond", "horsehead.nokia.troll.no", 5437 ); // V8.0.3
|
|
231 |
// addDb( "QPSQL7", "testdb", "troll", "trond", "silence.nokia.troll.no" ); // V8.2.1, UTF-8
|
|
232 |
// addDb( "QPSQL7", "testdb", "testuser", "Ee4Gabf6_", "postgres74-nokia.trolltech.com.au" ); // Version 7.4.19-1.el4_6.1
|
|
233 |
// addDb( "QPSQL7", "testdb", "testuser", "Ee4Gabf6_", "postgres81-nokia.trolltech.com.au" ); // Version 8.1.11-1.el5_1.1
|
|
234 |
|
|
235 |
// addDb( "QDB2", "testdb", "troll", "trond", "silence.nokia.troll.no" ); // DB2 v9.1 on silence
|
|
236 |
|
|
237 |
// yes - interbase really wants the physical path on the host machine.
|
|
238 |
// addDb( "QIBASE", "/opt/interbase/qttest.gdb", "SYSDBA", "masterkey", "horsehead.nokia.troll.no" );
|
|
239 |
// addDb( "QIBASE", "silence.troll.no:c:\\ibase\\testdb", "SYSDBA", "masterkey", "" ); // InterBase 7.5 on silence
|
|
240 |
// addDb( "QIBASE", "silence.troll.no:c:\\ibase\\testdb_ascii", "SYSDBA", "masterkey", "" ); // InterBase 7.5 on silence
|
|
241 |
// addDb( "QIBASE", "/opt/firebird/databases/testdb.fdb", "testuser", "Ee4Gabf6_", "firebird1-nokia.trolltech.com.au" ); // Firebird 1.5.5
|
|
242 |
// addDb( "QIBASE", "/opt/firebird/databases/testdb.fdb", "testuser", "Ee4Gabf6_", "firebird2-nokia.trolltech.com.au" ); // Firebird 2.1.1
|
|
243 |
|
|
244 |
// use in-memory database to prevent local files
|
|
245 |
// addDb("QSQLITE", ":memory:");
|
|
246 |
addDb( "QSQLITE", QDir::toNativeSeparators(QDir::tempPath()+"/foo.db") );
|
|
247 |
// addDb( "QSQLITE2", QDir::toNativeSeparators(QDir::tempPath()+"/foo2.db") );
|
|
248 |
// addDb( "QODBC3", "DRIVER={SQL SERVER};SERVER=iceblink.nokia.troll.no\\ICEBLINK", "troll", "trond", "" );
|
|
249 |
// addDb( "QODBC3", "DRIVER={SQL Native Client};SERVER=silence.nokia.troll.no\\SQLEXPRESS", "troll", "trond", "" );
|
|
250 |
|
|
251 |
// addDb( "QODBC", "DRIVER={MySQL ODBC 3.51 Driver};SERVER=mysql5-nokia.trolltech.com.au;DATABASE=testdb", "testuser", "Ee4Gabf6_", "" );
|
|
252 |
// addDb( "QODBC", "DRIVER={MySQL ODBC 5.1 Driver};SERVER=mysql4-nokia.trolltech.com.au;DATABASE=testdb", "testuser", "Ee4Gabf6_", "" );
|
|
253 |
// addDb( "QODBC", "DRIVER={FreeTDS};SERVER=horsehead.nokia.troll.no;DATABASE=testdb;PORT=4101;UID=troll;PWD=trondk", "troll", "trondk", "" );
|
|
254 |
// addDb( "QODBC", "DRIVER={FreeTDS};SERVER=silence.nokia.troll.no;DATABASE=testdb;PORT=2392;UID=troll;PWD=trond", "troll", "trond", "" );
|
|
255 |
// addDb( "QODBC", "DRIVER={FreeTDS};SERVER=bq-winserv2003-x86-01.apac.nokia.com;DATABASE=testdb;PORT=1433;UID=testuser;PWD=Ee4Gabf6_;TDS_Version=8.0", "", "", "" );
|
|
256 |
// addDb( "QODBC", "DRIVER={FreeTDS};SERVER=bq-winserv2008-x86-01.apac.nokia.com;DATABASE=testdb;PORT=1433;UID=testuser;PWD=Ee4Gabf6_;TDS_Version=8.0", "", "", "" );
|
|
257 |
// addDb( "QTDS7", "testdb", "testuser", "Ee4Gabf6_", "bq-winserv2003" );
|
|
258 |
// addDb( "QTDS7", "testdb", "testuser", "Ee4Gabf6_", "bq-winserv2008" );
|
|
259 |
// addDb( "QODBC3", "DRIVER={SQL SERVER};SERVER=bq-winserv2003-x86-01.apac.nokia.com;DATABASE=testdb;PORT=1433", "testuser", "Ee4Gabf6_", "" );
|
|
260 |
// addDb( "QODBC3", "DRIVER={SQL SERVER};SERVER=bq-winserv2008-x86-01.apac.nokia.com;DATABASE=testdb;PORT=1433", "testuser", "Ee4Gabf6_", "" );
|
|
261 |
// addDb( "QODBC", "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:\\dbs\\access\\testdb.mdb", "", "", "" );
|
|
262 |
}
|
|
263 |
|
|
264 |
void open()
|
|
265 |
{
|
|
266 |
addDbs();
|
|
267 |
|
|
268 |
QStringList::Iterator it = dbNames.begin();
|
|
269 |
|
|
270 |
while ( it != dbNames.end() ) {
|
|
271 |
QSqlDatabase db = QSqlDatabase::database(( *it ), false );
|
|
272 |
qDebug() << "Opening:" << (*it);
|
|
273 |
|
|
274 |
if ( db.isValid() && !db.isOpen() ) {
|
|
275 |
if ( !db.open() ) {
|
|
276 |
qWarning( "tst_Databases: Unable to open %s on %s:\n%s", qPrintable( db.driverName() ), qPrintable( *it ), qPrintable( db.lastError().databaseText() ) );
|
|
277 |
// well... opening failed, so we just ignore the server, maybe it is not running
|
|
278 |
it = dbNames.erase( it );
|
|
279 |
} else {
|
|
280 |
++it;
|
|
281 |
}
|
|
282 |
}
|
|
283 |
}
|
|
284 |
}
|
|
285 |
|
|
286 |
void close()
|
|
287 |
{
|
|
288 |
for ( QStringList::Iterator it = dbNames.begin(); it != dbNames.end(); ++it ) {
|
|
289 |
{
|
|
290 |
QSqlDatabase db = QSqlDatabase::database(( *it ), false );
|
|
291 |
|
|
292 |
if ( db.isValid() && db.isOpen() )
|
|
293 |
db.close();
|
|
294 |
}
|
|
295 |
|
|
296 |
QSqlDatabase::removeDatabase(( *it ) );
|
|
297 |
}
|
|
298 |
|
|
299 |
dbNames.clear();
|
|
300 |
}
|
|
301 |
|
|
302 |
// for debugging only: outputs the connection as string
|
|
303 |
static QString dbToString( const QSqlDatabase db )
|
|
304 |
{
|
|
305 |
QString res = db.driverName() + "@";
|
|
306 |
|
|
307 |
if ( db.driverName().startsWith( "QODBC" ) || db.driverName().startsWith( "QOCI" ) ) {
|
|
308 |
res += db.databaseName();
|
|
309 |
} else {
|
|
310 |
res += db.hostName();
|
|
311 |
}
|
|
312 |
|
|
313 |
if ( db.port() > 0 ) {
|
|
314 |
res += ":" + QString::number( db.port() );
|
|
315 |
}
|
|
316 |
|
|
317 |
return res;
|
|
318 |
}
|
|
319 |
|
|
320 |
// drop a table only if it exists to prevent warnings
|
|
321 |
static void safeDropTables( QSqlDatabase db, const QStringList& tableNames )
|
|
322 |
{
|
|
323 |
bool wasDropped;
|
|
324 |
QSqlQuery q( db );
|
|
325 |
QStringList dbtables=db.tables();
|
|
326 |
|
|
327 |
foreach(const QString &tableName, tableNames)
|
|
328 |
{
|
|
329 |
wasDropped = true;
|
|
330 |
QString table=tableName;
|
|
331 |
if ( db.driver()->isIdentifierEscaped(table, QSqlDriver::TableName))
|
|
332 |
table = db.driver()->stripDelimiters(table, QSqlDriver::TableName);
|
|
333 |
|
|
334 |
if ( dbtables.contains( table, Qt::CaseInsensitive ) ) {
|
|
335 |
foreach(const QString &table2, dbtables.filter(table, Qt::CaseInsensitive)) {
|
|
336 |
if(table2.compare(table.section('.', -1, -1), Qt::CaseInsensitive) == 0) {
|
|
337 |
table=db.driver()->escapeIdentifier(table2, QSqlDriver::TableName);
|
|
338 |
wasDropped = q.exec( "drop table " + table);
|
|
339 |
dbtables.removeAll(table2);
|
|
340 |
}
|
|
341 |
}
|
|
342 |
}
|
|
343 |
if ( !wasDropped ) {
|
|
344 |
qWarning() << dbToString(db) << "unable to drop table" << tableName << ':' << q.lastError();
|
|
345 |
// qWarning() << "last query:" << q.lastQuery();
|
|
346 |
// qWarning() << "dbtables:" << dbtables;
|
|
347 |
// qWarning() << "db.tables():" << db.tables();
|
|
348 |
}
|
|
349 |
}
|
|
350 |
}
|
|
351 |
|
|
352 |
static void safeDropTable( QSqlDatabase db, const QString& tableName )
|
|
353 |
{
|
|
354 |
safeDropTables(db, QStringList() << tableName);
|
|
355 |
}
|
|
356 |
|
|
357 |
static void safeDropViews( QSqlDatabase db, const QStringList &viewNames )
|
|
358 |
{
|
|
359 |
if ( isMSAccess( db ) ) // Access is sooo stupid.
|
|
360 |
safeDropTables( db, viewNames );
|
|
361 |
|
|
362 |
bool wasDropped;
|
|
363 |
QSqlQuery q( db );
|
|
364 |
QStringList dbtables=db.tables(QSql::Views);
|
|
365 |
|
|
366 |
foreach(QString viewName, viewNames)
|
|
367 |
{
|
|
368 |
wasDropped = true;
|
|
369 |
QString view=viewName;
|
|
370 |
if ( db.driver()->isIdentifierEscaped(view, QSqlDriver::TableName))
|
|
371 |
view = db.driver()->stripDelimiters(view, QSqlDriver::TableName);
|
|
372 |
|
|
373 |
if ( dbtables.contains( view, Qt::CaseInsensitive ) ) {
|
|
374 |
foreach(const QString &view2, dbtables.filter(view, Qt::CaseInsensitive)) {
|
|
375 |
if(view2.compare(view.section('.', -1, -1), Qt::CaseInsensitive) == 0) {
|
|
376 |
view=db.driver()->escapeIdentifier(view2, QSqlDriver::TableName);
|
|
377 |
wasDropped = q.exec( "drop view " + view);
|
|
378 |
dbtables.removeAll(view);
|
|
379 |
}
|
|
380 |
}
|
|
381 |
}
|
|
382 |
|
|
383 |
if ( !wasDropped )
|
|
384 |
qWarning() << dbToString(db) << "unable to drop view" << viewName << ':' << q.lastError();
|
|
385 |
// << "\nlast query:" << q.lastQuery()
|
|
386 |
// << "\ndbtables:" << dbtables
|
|
387 |
// << "\ndb.tables(QSql::Views):" << db.tables(QSql::Views);
|
|
388 |
}
|
|
389 |
}
|
|
390 |
|
|
391 |
static void safeDropView( QSqlDatabase db, const QString& tableName )
|
|
392 |
{
|
|
393 |
safeDropViews(db, QStringList() << tableName);
|
|
394 |
}
|
|
395 |
|
|
396 |
// returns the type name of the blob datatype for the database db.
|
|
397 |
// blobSize is only used if the db doesn't have a generic blob type
|
|
398 |
static QString blobTypeName( QSqlDatabase db, int blobSize = 10000 )
|
|
399 |
{
|
|
400 |
if ( db.driverName().startsWith( "QMYSQL" ) )
|
|
401 |
return "longblob";
|
|
402 |
|
|
403 |
if ( db.driverName().startsWith( "QPSQL" ) )
|
|
404 |
return "bytea";
|
|
405 |
|
|
406 |
if ( db.driverName().startsWith( "QTDS" )
|
|
407 |
|| isSqlServer( db )
|
|
408 |
|| isMSAccess( db ) )
|
|
409 |
return "image";
|
|
410 |
|
|
411 |
if ( db.driverName().startsWith( "QDB2" ) )
|
|
412 |
return QString( "blob(%1)" ).arg( blobSize );
|
|
413 |
|
|
414 |
if ( db.driverName().startsWith( "QIBASE" ) )
|
|
415 |
return QString( "blob sub_type 0 segment size 4096" );
|
|
416 |
|
|
417 |
if ( db.driverName().startsWith( "QOCI" )
|
|
418 |
|| db.driverName().startsWith( "QSQLITE" ) )
|
|
419 |
return "blob";
|
|
420 |
|
|
421 |
qDebug() << "tst_Databases::blobTypeName: Don't know the blob type for" << dbToString( db );
|
|
422 |
|
|
423 |
return "blob";
|
|
424 |
}
|
|
425 |
|
|
426 |
static QString autoFieldName( QSqlDatabase db )
|
|
427 |
{
|
|
428 |
if ( db.driverName().startsWith( "QMYSQL" ) )
|
|
429 |
return "AUTO_INCREMENT";
|
|
430 |
if ( db.driverName().startsWith( "QTDS" ) )
|
|
431 |
return "IDENTITY";
|
|
432 |
/* if ( db.driverName().startsWith( "QPSQL" ) )
|
|
433 |
return "SERIAL";*/
|
|
434 |
// if ( db.driverName().startsWith( "QDB2" ) )
|
|
435 |
// return "GENERATED BY DEFAULT AS IDENTITY";
|
|
436 |
|
|
437 |
return QString();
|
|
438 |
}
|
|
439 |
|
|
440 |
static QByteArray printError( const QSqlError& err )
|
|
441 |
{
|
|
442 |
QString result;
|
|
443 |
if(err.number() > 0)
|
|
444 |
result += '(' + QString::number(err.number()) + ") ";
|
|
445 |
result += '\'';
|
|
446 |
if(!err.driverText().isEmpty())
|
|
447 |
result += err.driverText() + "' || '";
|
|
448 |
result += err.databaseText() + "'";
|
|
449 |
return result.toLocal8Bit();
|
|
450 |
}
|
|
451 |
|
|
452 |
static QByteArray printError( const QSqlError& err, const QSqlDatabase& db )
|
|
453 |
{
|
|
454 |
QString result(dbToString(db) + ": ");
|
|
455 |
if(err.number() > 0)
|
|
456 |
result += '(' + QString::number(err.number()) + ") ";
|
|
457 |
result += '\'';
|
|
458 |
if(!err.driverText().isEmpty())
|
|
459 |
result += err.driverText() + "' || '";
|
|
460 |
result += err.databaseText() + "'";
|
|
461 |
return result.toLocal8Bit();
|
|
462 |
}
|
|
463 |
|
|
464 |
static bool isSqlServer( QSqlDatabase db )
|
|
465 |
{
|
|
466 |
return db.databaseName().contains( "sql server", Qt::CaseInsensitive )
|
|
467 |
|| db.databaseName().contains( "sqlserver", Qt::CaseInsensitive )
|
|
468 |
|| db.databaseName().contains( "sql native client", Qt::CaseInsensitive )
|
|
469 |
|| db.databaseName().contains( "bq-winserv", Qt::CaseInsensitive )
|
|
470 |
|| db.hostName().contains( "bq-winserv", Qt::CaseInsensitive );
|
|
471 |
}
|
|
472 |
|
|
473 |
static bool isMSAccess( QSqlDatabase db )
|
|
474 |
{
|
|
475 |
return db.databaseName().contains( "Access Driver", Qt::CaseInsensitive );
|
|
476 |
}
|
|
477 |
|
|
478 |
static bool isPostgreSQL( QSqlDatabase db )
|
|
479 |
{
|
|
480 |
return db.driverName().startsWith("QPSQL") || (db.driverName().startsWith("QODBC") && db.databaseName().contains("PostgreSQL") );
|
|
481 |
}
|
|
482 |
|
|
483 |
static bool isMySQL( QSqlDatabase db )
|
|
484 |
{
|
|
485 |
return db.driverName().startsWith("QMYSQL") || (db.driverName().startsWith("QODBC") && db.databaseName().contains("MySQL") );
|
|
486 |
}
|
|
487 |
static bool isDB2( QSqlDatabase db )
|
|
488 |
{
|
|
489 |
return db.driverName().startsWith("QDB2") || (db.driverName().startsWith("QODBC") && db.databaseName().contains("db2") );
|
|
490 |
}
|
|
491 |
|
|
492 |
// -1 on fail, else Oracle version
|
|
493 |
static int getOraVersion( QSqlDatabase db )
|
|
494 |
{
|
|
495 |
int ver = -1;
|
|
496 |
QSqlQuery q( "SELECT banner FROM v$version", db );
|
|
497 |
q.next();
|
|
498 |
|
|
499 |
QRegExp vers( "([0-9]+)\\.[0-9\\.]+[0-9]" );
|
|
500 |
|
|
501 |
if ( vers.indexIn( q.value( 0 ).toString() ) ) {
|
|
502 |
bool ok;
|
|
503 |
ver = vers.cap( 1 ).toInt( &ok );
|
|
504 |
|
|
505 |
if ( !ok )
|
|
506 |
ver = -1;
|
|
507 |
}
|
|
508 |
|
|
509 |
return ver;
|
|
510 |
}
|
|
511 |
|
|
512 |
static QString getMySqlVersion( const QSqlDatabase &db )
|
|
513 |
{
|
|
514 |
QSqlQuery q(db);
|
|
515 |
q.exec( "select version()" );
|
|
516 |
if(q.next())
|
|
517 |
return q.value( 0 ).toString();
|
|
518 |
else
|
|
519 |
return QString();
|
|
520 |
}
|
|
521 |
|
|
522 |
static QString getPSQLVersion( const QSqlDatabase &db )
|
|
523 |
{
|
|
524 |
QSqlQuery q(db);
|
|
525 |
q.exec( "select version()" );
|
|
526 |
if(q.next())
|
|
527 |
return q.value( 0 ).toString();
|
|
528 |
else
|
|
529 |
return QString();
|
|
530 |
}
|
|
531 |
|
|
532 |
QStringList dbNames;
|
|
533 |
int counter;
|
|
534 |
};
|
|
535 |
|
|
536 |
#endif
|
|
537 |
|