author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Fri, 19 Feb 2010 23:40:16 +0200 | |
branch | RCL_3 |
changeset 4 | 3b1da2848fc7 |
parent 0 | 1918ee327afb |
child 7 | 3f74d0d4af4c |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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 |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
224 |
// addDb( "QMYSQL3", "testdb", "testuser", "Ee4Gabf6_", "bq-mysql50.apac.nokia.com" ); // MySQL 5.0.45-7.el5 linux |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
225 |
// addDb( "QMYSQL3", "testdb", "testuser", "Ee4Gabf6_", "bq-mysql51.apac.nokia.com" ); // MySQL 5.1.36-6.7.2.i586 linux |
0 | 226 |
|
227 |
// addDb( "QPSQL7", "testdb", "troll", "trond", "horsehead.nokia.troll.no" ); // V7.2 NOT SUPPORTED! |
|
228 |
// addDb( "QPSQL7", "testdb", "troll", "trond", "horsehead.nokia.troll.no", 5434 ); // V7.2 NOT SUPPORTED! Multi-byte |
|
229 |
// addDb( "QPSQL7", "testdb", "troll", "trond", "horsehead.nokia.troll.no", 5435 ); // V7.3 |
|
230 |
// addDb( "QPSQL7", "testdb", "troll", "trond", "horsehead.nokia.troll.no", 5436 ); // V7.4 |
|
231 |
// addDb( "QPSQL7", "testdb", "troll", "trond", "horsehead.nokia.troll.no", 5437 ); // V8.0.3 |
|
232 |
// addDb( "QPSQL7", "testdb", "troll", "trond", "silence.nokia.troll.no" ); // V8.2.1, UTF-8 |
|
233 |
// addDb( "QPSQL7", "testdb", "testuser", "Ee4Gabf6_", "postgres74-nokia.trolltech.com.au" ); // Version 7.4.19-1.el4_6.1 |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
234 |
// addDb( "QPSQL7", "testdb", "testuser", "Ee4Gabf6_", "bq-pgsql81.apac.nokia.com" ); // Version 8.1.11-1.el5_1.1 |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
235 |
// addDb( "QPSQL7", "testdb", "testuser", "Ee4Gabf6_", "bq-pgsql84.apac.nokia.com" ); // Version 8.4.1-2.1.i586 |
0 | 236 |
|
237 |
// addDb( "QDB2", "testdb", "troll", "trond", "silence.nokia.troll.no" ); // DB2 v9.1 on silence |
|
238 |
||
239 |
// yes - interbase really wants the physical path on the host machine. |
|
240 |
// addDb( "QIBASE", "/opt/interbase/qttest.gdb", "SYSDBA", "masterkey", "horsehead.nokia.troll.no" ); |
|
241 |
// addDb( "QIBASE", "silence.troll.no:c:\\ibase\\testdb", "SYSDBA", "masterkey", "" ); // InterBase 7.5 on silence |
|
242 |
// addDb( "QIBASE", "silence.troll.no:c:\\ibase\\testdb_ascii", "SYSDBA", "masterkey", "" ); // InterBase 7.5 on silence |
|
243 |
// addDb( "QIBASE", "/opt/firebird/databases/testdb.fdb", "testuser", "Ee4Gabf6_", "firebird1-nokia.trolltech.com.au" ); // Firebird 1.5.5 |
|
244 |
// addDb( "QIBASE", "/opt/firebird/databases/testdb.fdb", "testuser", "Ee4Gabf6_", "firebird2-nokia.trolltech.com.au" ); // Firebird 2.1.1 |
|
245 |
||
246 |
// use in-memory database to prevent local files |
|
247 |
// addDb("QSQLITE", ":memory:"); |
|
248 |
addDb( "QSQLITE", QDir::toNativeSeparators(QDir::tempPath()+"/foo.db") ); |
|
249 |
// addDb( "QSQLITE2", QDir::toNativeSeparators(QDir::tempPath()+"/foo2.db") ); |
|
250 |
// addDb( "QODBC3", "DRIVER={SQL SERVER};SERVER=iceblink.nokia.troll.no\\ICEBLINK", "troll", "trond", "" ); |
|
251 |
// addDb( "QODBC3", "DRIVER={SQL Native Client};SERVER=silence.nokia.troll.no\\SQLEXPRESS", "troll", "trond", "" ); |
|
252 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
253 |
// addDb( "QODBC", "DRIVER={MySQL ODBC 5.1 Driver};SERVER=mysql5-nokia.trolltech.com.au;DATABASE=testdb", "testuser", "Ee4Gabf6_", "" ); |
0 | 254 |
// addDb( "QODBC", "DRIVER={MySQL ODBC 5.1 Driver};SERVER=mysql4-nokia.trolltech.com.au;DATABASE=testdb", "testuser", "Ee4Gabf6_", "" ); |
255 |
// addDb( "QODBC", "DRIVER={FreeTDS};SERVER=horsehead.nokia.troll.no;DATABASE=testdb;PORT=4101;UID=troll;PWD=trondk", "troll", "trondk", "" ); |
|
256 |
// addDb( "QODBC", "DRIVER={FreeTDS};SERVER=silence.nokia.troll.no;DATABASE=testdb;PORT=2392;UID=troll;PWD=trond", "troll", "trond", "" ); |
|
257 |
// addDb( "QODBC", "DRIVER={FreeTDS};SERVER=bq-winserv2003-x86-01.apac.nokia.com;DATABASE=testdb;PORT=1433;UID=testuser;PWD=Ee4Gabf6_;TDS_Version=8.0", "", "", "" ); |
|
258 |
// addDb( "QODBC", "DRIVER={FreeTDS};SERVER=bq-winserv2008-x86-01.apac.nokia.com;DATABASE=testdb;PORT=1433;UID=testuser;PWD=Ee4Gabf6_;TDS_Version=8.0", "", "", "" ); |
|
259 |
// addDb( "QTDS7", "testdb", "testuser", "Ee4Gabf6_", "bq-winserv2003" ); |
|
260 |
// addDb( "QTDS7", "testdb", "testuser", "Ee4Gabf6_", "bq-winserv2008" ); |
|
261 |
// addDb( "QODBC3", "DRIVER={SQL SERVER};SERVER=bq-winserv2003-x86-01.apac.nokia.com;DATABASE=testdb;PORT=1433", "testuser", "Ee4Gabf6_", "" ); |
|
262 |
// addDb( "QODBC3", "DRIVER={SQL SERVER};SERVER=bq-winserv2008-x86-01.apac.nokia.com;DATABASE=testdb;PORT=1433", "testuser", "Ee4Gabf6_", "" ); |
|
263 |
// addDb( "QODBC", "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:\\dbs\\access\\testdb.mdb", "", "", "" ); |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
264 |
// addDb( "QODBC", "DRIVER={Postgresql};SERVER=postgres81-nokia.trolltech.com.au;DATABASE=testdb", "testuser", "Ee4Gabf6_", "" ); |
0 | 265 |
} |
266 |
||
267 |
void open() |
|
268 |
{ |
|
269 |
addDbs(); |
|
270 |
||
271 |
QStringList::Iterator it = dbNames.begin(); |
|
272 |
||
273 |
while ( it != dbNames.end() ) { |
|
274 |
QSqlDatabase db = QSqlDatabase::database(( *it ), false ); |
|
275 |
qDebug() << "Opening:" << (*it); |
|
276 |
||
277 |
if ( db.isValid() && !db.isOpen() ) { |
|
278 |
if ( !db.open() ) { |
|
279 |
qWarning( "tst_Databases: Unable to open %s on %s:\n%s", qPrintable( db.driverName() ), qPrintable( *it ), qPrintable( db.lastError().databaseText() ) ); |
|
280 |
// well... opening failed, so we just ignore the server, maybe it is not running |
|
281 |
it = dbNames.erase( it ); |
|
282 |
} else { |
|
283 |
++it; |
|
284 |
} |
|
285 |
} |
|
286 |
} |
|
287 |
} |
|
288 |
||
289 |
void close() |
|
290 |
{ |
|
291 |
for ( QStringList::Iterator it = dbNames.begin(); it != dbNames.end(); ++it ) { |
|
292 |
{ |
|
293 |
QSqlDatabase db = QSqlDatabase::database(( *it ), false ); |
|
294 |
||
295 |
if ( db.isValid() && db.isOpen() ) |
|
296 |
db.close(); |
|
297 |
} |
|
298 |
||
299 |
QSqlDatabase::removeDatabase(( *it ) ); |
|
300 |
} |
|
301 |
||
302 |
dbNames.clear(); |
|
303 |
} |
|
304 |
||
305 |
// for debugging only: outputs the connection as string |
|
306 |
static QString dbToString( const QSqlDatabase db ) |
|
307 |
{ |
|
308 |
QString res = db.driverName() + "@"; |
|
309 |
||
310 |
if ( db.driverName().startsWith( "QODBC" ) || db.driverName().startsWith( "QOCI" ) ) { |
|
311 |
res += db.databaseName(); |
|
312 |
} else { |
|
313 |
res += db.hostName(); |
|
314 |
} |
|
315 |
||
316 |
if ( db.port() > 0 ) { |
|
317 |
res += ":" + QString::number( db.port() ); |
|
318 |
} |
|
319 |
||
320 |
return res; |
|
321 |
} |
|
322 |
||
323 |
// drop a table only if it exists to prevent warnings |
|
324 |
static void safeDropTables( QSqlDatabase db, const QStringList& tableNames ) |
|
325 |
{ |
|
326 |
bool wasDropped; |
|
327 |
QSqlQuery q( db ); |
|
328 |
QStringList dbtables=db.tables(); |
|
329 |
||
330 |
foreach(const QString &tableName, tableNames) |
|
331 |
{ |
|
332 |
wasDropped = true; |
|
333 |
QString table=tableName; |
|
334 |
if ( db.driver()->isIdentifierEscaped(table, QSqlDriver::TableName)) |
|
335 |
table = db.driver()->stripDelimiters(table, QSqlDriver::TableName); |
|
336 |
||
337 |
if ( dbtables.contains( table, Qt::CaseInsensitive ) ) { |
|
338 |
foreach(const QString &table2, dbtables.filter(table, Qt::CaseInsensitive)) { |
|
339 |
if(table2.compare(table.section('.', -1, -1), Qt::CaseInsensitive) == 0) { |
|
340 |
table=db.driver()->escapeIdentifier(table2, QSqlDriver::TableName); |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
341 |
if(db.driverName().startsWith( "QPSQL" )) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
342 |
wasDropped = q.exec( "drop table " + table + " cascade"); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
343 |
else |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
344 |
wasDropped = q.exec( "drop table " + table); |
0 | 345 |
dbtables.removeAll(table2); |
346 |
} |
|
347 |
} |
|
348 |
} |
|
349 |
if ( !wasDropped ) { |
|
350 |
qWarning() << dbToString(db) << "unable to drop table" << tableName << ':' << q.lastError(); |
|
351 |
// qWarning() << "last query:" << q.lastQuery(); |
|
352 |
// qWarning() << "dbtables:" << dbtables; |
|
353 |
// qWarning() << "db.tables():" << db.tables(); |
|
354 |
} |
|
355 |
} |
|
356 |
} |
|
357 |
||
358 |
static void safeDropTable( QSqlDatabase db, const QString& tableName ) |
|
359 |
{ |
|
360 |
safeDropTables(db, QStringList() << tableName); |
|
361 |
} |
|
362 |
||
363 |
static void safeDropViews( QSqlDatabase db, const QStringList &viewNames ) |
|
364 |
{ |
|
365 |
if ( isMSAccess( db ) ) // Access is sooo stupid. |
|
366 |
safeDropTables( db, viewNames ); |
|
367 |
||
368 |
bool wasDropped; |
|
369 |
QSqlQuery q( db ); |
|
370 |
QStringList dbtables=db.tables(QSql::Views); |
|
371 |
||
372 |
foreach(QString viewName, viewNames) |
|
373 |
{ |
|
374 |
wasDropped = true; |
|
375 |
QString view=viewName; |
|
376 |
if ( db.driver()->isIdentifierEscaped(view, QSqlDriver::TableName)) |
|
377 |
view = db.driver()->stripDelimiters(view, QSqlDriver::TableName); |
|
378 |
||
379 |
if ( dbtables.contains( view, Qt::CaseInsensitive ) ) { |
|
380 |
foreach(const QString &view2, dbtables.filter(view, Qt::CaseInsensitive)) { |
|
381 |
if(view2.compare(view.section('.', -1, -1), Qt::CaseInsensitive) == 0) { |
|
382 |
view=db.driver()->escapeIdentifier(view2, QSqlDriver::TableName); |
|
383 |
wasDropped = q.exec( "drop view " + view); |
|
384 |
dbtables.removeAll(view); |
|
385 |
} |
|
386 |
} |
|
387 |
} |
|
388 |
||
389 |
if ( !wasDropped ) |
|
390 |
qWarning() << dbToString(db) << "unable to drop view" << viewName << ':' << q.lastError(); |
|
391 |
// << "\nlast query:" << q.lastQuery() |
|
392 |
// << "\ndbtables:" << dbtables |
|
393 |
// << "\ndb.tables(QSql::Views):" << db.tables(QSql::Views); |
|
394 |
} |
|
395 |
} |
|
396 |
||
397 |
static void safeDropView( QSqlDatabase db, const QString& tableName ) |
|
398 |
{ |
|
399 |
safeDropViews(db, QStringList() << tableName); |
|
400 |
} |
|
401 |
||
402 |
// returns the type name of the blob datatype for the database db. |
|
403 |
// blobSize is only used if the db doesn't have a generic blob type |
|
404 |
static QString blobTypeName( QSqlDatabase db, int blobSize = 10000 ) |
|
405 |
{ |
|
406 |
if ( db.driverName().startsWith( "QMYSQL" ) ) |
|
407 |
return "longblob"; |
|
408 |
||
409 |
if ( db.driverName().startsWith( "QPSQL" ) ) |
|
410 |
return "bytea"; |
|
411 |
||
412 |
if ( db.driverName().startsWith( "QTDS" ) |
|
413 |
|| isSqlServer( db ) |
|
414 |
|| isMSAccess( db ) ) |
|
415 |
return "image"; |
|
416 |
||
417 |
if ( db.driverName().startsWith( "QDB2" ) ) |
|
418 |
return QString( "blob(%1)" ).arg( blobSize ); |
|
419 |
||
420 |
if ( db.driverName().startsWith( "QIBASE" ) ) |
|
421 |
return QString( "blob sub_type 0 segment size 4096" ); |
|
422 |
||
423 |
if ( db.driverName().startsWith( "QOCI" ) |
|
424 |
|| db.driverName().startsWith( "QSQLITE" ) ) |
|
425 |
return "blob"; |
|
426 |
||
427 |
qDebug() << "tst_Databases::blobTypeName: Don't know the blob type for" << dbToString( db ); |
|
428 |
||
429 |
return "blob"; |
|
430 |
} |
|
431 |
||
432 |
static QString autoFieldName( QSqlDatabase db ) |
|
433 |
{ |
|
434 |
if ( db.driverName().startsWith( "QMYSQL" ) ) |
|
435 |
return "AUTO_INCREMENT"; |
|
436 |
if ( db.driverName().startsWith( "QTDS" ) ) |
|
437 |
return "IDENTITY"; |
|
438 |
/* if ( db.driverName().startsWith( "QPSQL" ) ) |
|
439 |
return "SERIAL";*/ |
|
440 |
// if ( db.driverName().startsWith( "QDB2" ) ) |
|
441 |
// return "GENERATED BY DEFAULT AS IDENTITY"; |
|
442 |
||
443 |
return QString(); |
|
444 |
} |
|
445 |
||
446 |
static QByteArray printError( const QSqlError& err ) |
|
447 |
{ |
|
448 |
QString result; |
|
449 |
if(err.number() > 0) |
|
450 |
result += '(' + QString::number(err.number()) + ") "; |
|
451 |
result += '\''; |
|
452 |
if(!err.driverText().isEmpty()) |
|
453 |
result += err.driverText() + "' || '"; |
|
454 |
result += err.databaseText() + "'"; |
|
455 |
return result.toLocal8Bit(); |
|
456 |
} |
|
457 |
||
458 |
static QByteArray printError( const QSqlError& err, const QSqlDatabase& db ) |
|
459 |
{ |
|
460 |
QString result(dbToString(db) + ": "); |
|
461 |
if(err.number() > 0) |
|
462 |
result += '(' + QString::number(err.number()) + ") "; |
|
463 |
result += '\''; |
|
464 |
if(!err.driverText().isEmpty()) |
|
465 |
result += err.driverText() + "' || '"; |
|
466 |
result += err.databaseText() + "'"; |
|
467 |
return result.toLocal8Bit(); |
|
468 |
} |
|
469 |
||
470 |
static bool isSqlServer( QSqlDatabase db ) |
|
471 |
{ |
|
472 |
return db.databaseName().contains( "sql server", Qt::CaseInsensitive ) |
|
473 |
|| db.databaseName().contains( "sqlserver", Qt::CaseInsensitive ) |
|
474 |
|| db.databaseName().contains( "sql native client", Qt::CaseInsensitive ) |
|
475 |
|| db.databaseName().contains( "bq-winserv", Qt::CaseInsensitive ) |
|
476 |
|| db.hostName().contains( "bq-winserv", Qt::CaseInsensitive ); |
|
477 |
} |
|
478 |
||
479 |
static bool isMSAccess( QSqlDatabase db ) |
|
480 |
{ |
|
481 |
return db.databaseName().contains( "Access Driver", Qt::CaseInsensitive ); |
|
482 |
} |
|
483 |
||
484 |
static bool isPostgreSQL( QSqlDatabase db ) |
|
485 |
{ |
|
486 |
return db.driverName().startsWith("QPSQL") || (db.driverName().startsWith("QODBC") && db.databaseName().contains("PostgreSQL") ); |
|
487 |
} |
|
488 |
||
489 |
static bool isMySQL( QSqlDatabase db ) |
|
490 |
{ |
|
491 |
return db.driverName().startsWith("QMYSQL") || (db.driverName().startsWith("QODBC") && db.databaseName().contains("MySQL") ); |
|
492 |
} |
|
493 |
static bool isDB2( QSqlDatabase db ) |
|
494 |
{ |
|
495 |
return db.driverName().startsWith("QDB2") || (db.driverName().startsWith("QODBC") && db.databaseName().contains("db2") ); |
|
496 |
} |
|
497 |
||
498 |
// -1 on fail, else Oracle version |
|
499 |
static int getOraVersion( QSqlDatabase db ) |
|
500 |
{ |
|
501 |
int ver = -1; |
|
502 |
QSqlQuery q( "SELECT banner FROM v$version", db ); |
|
503 |
q.next(); |
|
504 |
||
505 |
QRegExp vers( "([0-9]+)\\.[0-9\\.]+[0-9]" ); |
|
506 |
||
507 |
if ( vers.indexIn( q.value( 0 ).toString() ) ) { |
|
508 |
bool ok; |
|
509 |
ver = vers.cap( 1 ).toInt( &ok ); |
|
510 |
||
511 |
if ( !ok ) |
|
512 |
ver = -1; |
|
513 |
} |
|
514 |
||
515 |
return ver; |
|
516 |
} |
|
517 |
||
518 |
static QString getMySqlVersion( const QSqlDatabase &db ) |
|
519 |
{ |
|
520 |
QSqlQuery q(db); |
|
521 |
q.exec( "select version()" ); |
|
522 |
if(q.next()) |
|
523 |
return q.value( 0 ).toString(); |
|
524 |
else |
|
525 |
return QString(); |
|
526 |
} |
|
527 |
||
528 |
static QString getPSQLVersion( const QSqlDatabase &db ) |
|
529 |
{ |
|
530 |
QSqlQuery q(db); |
|
531 |
q.exec( "select version()" ); |
|
532 |
if(q.next()) |
|
533 |
return q.value( 0 ).toString(); |
|
534 |
else |
|
535 |
return QString(); |
|
536 |
} |
|
537 |
||
538 |
QStringList dbNames; |
|
539 |
int counter; |
|
540 |
}; |
|
541 |
||
542 |
#endif |
|
543 |