|
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 |
|
42 |
|
43 #include <QtTest/QtTest> |
|
44 #include <QtSql/QtSql> |
|
45 |
|
46 #include "../qsqldatabase/tst_databases.h" |
|
47 |
|
48 |
|
49 |
|
50 //TESTED_CLASS= |
|
51 //TESTED_FILES= |
|
52 |
|
53 class tst_QSqlDriver : public QObject |
|
54 { |
|
55 Q_OBJECT |
|
56 |
|
57 public: |
|
58 void recreateTestTables(QSqlDatabase); |
|
59 |
|
60 tst_Databases dbs; |
|
61 |
|
62 public slots: |
|
63 void initTestCase_data(); |
|
64 void initTestCase(); |
|
65 void cleanupTestCase(); |
|
66 void init(); |
|
67 void cleanup(); |
|
68 |
|
69 private slots: |
|
70 void record(); |
|
71 void primaryIndex(); |
|
72 }; |
|
73 |
|
74 |
|
75 void tst_QSqlDriver::initTestCase_data() |
|
76 { |
|
77 dbs.open(); |
|
78 if (dbs.fillTestTable() == 0) { |
|
79 qWarning("NO DATABASES"); |
|
80 QSKIP("No database drivers are available in this Qt configuration", SkipAll); |
|
81 } |
|
82 } |
|
83 |
|
84 void tst_QSqlDriver::recreateTestTables(QSqlDatabase db) |
|
85 { |
|
86 QSqlQuery q(db); |
|
87 |
|
88 if(tst_Databases::isPostgreSQL(db)) |
|
89 QVERIFY_SQL( q, exec("set client_min_messages='warning'")); |
|
90 |
|
91 tst_Databases::safeDropTable( db, qTableName( "relTEST1" ) ); |
|
92 |
|
93 QVERIFY_SQL( q, exec("create table " + qTableName("relTEST1") + |
|
94 " (id int not null primary key, name varchar(20), title_key int, another_title_key int)")); |
|
95 QVERIFY_SQL( q, exec("insert into " + qTableName("relTEST1") + " values(1, 'harry', 1, 2)")); |
|
96 QVERIFY_SQL( q, exec("insert into " + qTableName("relTEST1") + " values(2, 'trond', 2, 1)")); |
|
97 QVERIFY_SQL( q, exec("insert into " + qTableName("relTEST1") + " values(3, 'vohi', 1, 2)")); |
|
98 QVERIFY_SQL( q, exec("insert into " + qTableName("relTEST1") + " values(4, 'boris', 2, 2)")); |
|
99 } |
|
100 |
|
101 void tst_QSqlDriver::initTestCase() |
|
102 { |
|
103 foreach (const QString &dbname, dbs.dbNames) |
|
104 recreateTestTables(QSqlDatabase::database(dbname)); |
|
105 } |
|
106 |
|
107 void tst_QSqlDriver::cleanupTestCase() |
|
108 { |
|
109 foreach (const QString &dbName, dbs.dbNames) { |
|
110 QSqlDatabase db = QSqlDatabase::database(dbName); |
|
111 tst_Databases::safeDropTable( db, qTableName( "relTEST1" ) ); |
|
112 } |
|
113 dbs.close(); |
|
114 } |
|
115 |
|
116 void tst_QSqlDriver::init() |
|
117 { |
|
118 } |
|
119 |
|
120 void tst_QSqlDriver::cleanup() |
|
121 { |
|
122 } |
|
123 |
|
124 void tst_QSqlDriver::record() |
|
125 { |
|
126 QFETCH_GLOBAL(QString, dbName); |
|
127 QSqlDatabase db = QSqlDatabase::database(dbName); |
|
128 CHECK_DATABASE(db); |
|
129 |
|
130 QString tablename = qTableName("relTEST1"); |
|
131 QStringList fields; |
|
132 fields << "id" << "name" << "title_key" << "another_title_key"; |
|
133 |
|
134 //check we can get records using an unquoted mixed case table name |
|
135 QSqlRecord rec = db.driver()->record(tablename); |
|
136 QCOMPARE(rec.count(), 4); |
|
137 |
|
138 if (db.driverName().startsWith("QIBASE")|| db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) |
|
139 for(int i = 0; i < fields.count(); ++i) |
|
140 fields[i] = fields[i].toUpper(); |
|
141 |
|
142 for (int i = 0; i < fields.count(); ++i) |
|
143 QCOMPARE(rec.fieldName(i), fields[i]); |
|
144 |
|
145 if (db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) |
|
146 tablename = tablename.toUpper(); |
|
147 else if (db.driverName().startsWith("QPSQL")) |
|
148 tablename = tablename.toLower(); |
|
149 |
|
150 if(!db.driverName().startsWith("QODBC") && !db.databaseName().contains("PostgreSql")) { |
|
151 //check we can get records using a properly quoted table name |
|
152 rec = db.driver()->record(db.driver()->escapeIdentifier(tablename,QSqlDriver::TableName)); |
|
153 QCOMPARE(rec.count(), 4); |
|
154 } |
|
155 |
|
156 for (int i = 0; i < fields.count(); ++i) |
|
157 QCOMPARE(rec.fieldName(i), fields[i]); |
|
158 |
|
159 if( db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) |
|
160 tablename = tablename.toLower(); |
|
161 else if (db.driverName().startsWith("QPSQL")) |
|
162 tablename = tablename.toUpper(); |
|
163 |
|
164 //check that we can't get records using incorrect tablename casing that's been quoted |
|
165 rec = db.driver()->record(db.driver()->escapeIdentifier(tablename,QSqlDriver::TableName)); |
|
166 if (tst_Databases::isMySQL(db) |
|
167 || db.driverName().startsWith("QSQLITE") |
|
168 || db.driverName().startsWith("QTDS") |
|
169 || tst_Databases::isSqlServer(db) |
|
170 || tst_Databases::isMSAccess(db)) |
|
171 QCOMPARE(rec.count(), 4); //mysql, sqlite and tds will match |
|
172 else |
|
173 QCOMPARE(rec.count(), 0); |
|
174 |
|
175 } |
|
176 |
|
177 void tst_QSqlDriver::primaryIndex() |
|
178 { |
|
179 QFETCH_GLOBAL(QString, dbName); |
|
180 QSqlDatabase db = QSqlDatabase::database(dbName); |
|
181 CHECK_DATABASE(db); |
|
182 |
|
183 QString tablename = qTableName("relTEST1"); |
|
184 //check that we can get primary index using unquoted mixed case table name |
|
185 QSqlIndex index = db.driver()->primaryIndex(tablename); |
|
186 QCOMPARE(index.count(), 1); |
|
187 |
|
188 if( db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) |
|
189 QCOMPARE(index.fieldName(0), QString::fromLatin1("ID")); |
|
190 else |
|
191 QCOMPARE(index.fieldName(0), QString::fromLatin1("id")); |
|
192 |
|
193 |
|
194 //check that we can get the primary index using a quoted tablename |
|
195 if( db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) |
|
196 tablename = tablename.toUpper(); |
|
197 else if (db.driverName().startsWith("QPSQL")) |
|
198 tablename = tablename.toLower(); |
|
199 |
|
200 if(!db.driverName().startsWith("QODBC") && !db.databaseName().contains("PostgreSql")) { |
|
201 index = db.driver()->primaryIndex(db.driver()->escapeIdentifier(tablename, QSqlDriver::TableName)); |
|
202 QCOMPARE(index.count(), 1); |
|
203 } |
|
204 if( db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) |
|
205 QCOMPARE(index.fieldName(0), QString::fromLatin1("ID")); |
|
206 else |
|
207 QCOMPARE(index.fieldName(0), QString::fromLatin1("id")); |
|
208 |
|
209 |
|
210 |
|
211 //check that we can not get the primary index using a quoted but incorrect table name casing |
|
212 if( db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) |
|
213 tablename = tablename.toLower(); |
|
214 else if (db.driverName().startsWith("QPSQL")) |
|
215 tablename = tablename.toUpper(); |
|
216 |
|
217 index = db.driver()->primaryIndex(db.driver()->escapeIdentifier(tablename, QSqlDriver::TableName)); |
|
218 if (tst_Databases::isMySQL(db) |
|
219 || db.driverName().startsWith("QSQLITE") |
|
220 || db.driverName().startsWith("QTDS") |
|
221 || tst_Databases::isSqlServer(db) |
|
222 || tst_Databases::isMSAccess(db)) |
|
223 QCOMPARE(index.count(), 1); //mysql will always find the table name regardless of casing |
|
224 else |
|
225 QCOMPARE(index.count(), 0); |
|
226 } |
|
227 |
|
228 QTEST_MAIN(tst_QSqlDriver) |
|
229 #include "tst_qsqldriver.moc" |