1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the test suite of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:GPL-EXCEPT$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU
19** General Public License version 3 as published by the Free Software
20** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
21** included in the packaging of this file. Please review the following
22** information to ensure the GNU General Public License requirements will
23** be met: https://www.gnu.org/licenses/gpl-3.0.html.
24**
25** $QT_END_LICENSE$
26**
27****************************************************************************/
28
29
30#include <QtTest/QtTest>
31#include <QtSql/QtSql>
32
33#include "../qsqldatabase/tst_databases.h"
34
35class tst_QSqlDriver : public QObject
36{
37 Q_OBJECT
38
39public:
40 void recreateTestTables(QSqlDatabase);
41
42 tst_Databases dbs;
43
44public slots:
45 void initTestCase_data();
46 void initTestCase();
47 void cleanupTestCase();
48 void init();
49 void cleanup();
50
51private slots:
52 void record();
53 void primaryIndex();
54 void formatValue();
55};
56
57static bool driverSupportsDefaultValues(QSqlDriver::DbmsType dbType)
58{
59 switch (dbType) {
60 case QSqlDriver::SQLite:
61 case QSqlDriver::PostgreSQL:
62 case QSqlDriver::Oracle:
63 return true;
64 default:
65 break;
66 }
67 return false;
68}
69
70void tst_QSqlDriver::initTestCase_data()
71{
72 QVERIFY(dbs.open());
73 if (dbs.fillTestTable() == 0)
74 QSKIP("No database drivers are available in this Qt configuration");
75}
76
77void tst_QSqlDriver::recreateTestTables(QSqlDatabase db)
78{
79 QSqlQuery q(db);
80 const QString relTEST1(qTableName(prefix: "relTEST1", __FILE__, db));
81
82 QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
83 if (dbType == QSqlDriver::PostgreSQL)
84 QVERIFY_SQL( q, exec("set client_min_messages='warning'"));
85
86 tst_Databases::safeDropTable( db, tableName: relTEST1 );
87 QString doubleField;
88 if (dbType == QSqlDriver::SQLite)
89 doubleField = "more_data double";
90 else if (dbType == QSqlDriver::Oracle)
91 doubleField = "more_data number(8,7)";
92 else if (dbType == QSqlDriver::PostgreSQL)
93 doubleField = "more_data double precision";
94 else
95 doubleField = "more_data double(8,7)";
96 const QString defValue(driverSupportsDefaultValues(dbType) ? QStringLiteral("DEFAULT 'defaultVal'") : QString());
97 QVERIFY_SQL( q, exec("create table " + relTEST1 +
98 " (id int not null primary key, name varchar(20) " + defValue + ", title_key int, another_title_key int, " + doubleField + QLatin1Char(')')));
99 QVERIFY_SQL( q, exec("insert into " + relTEST1 + " values(1, 'harry', 1, 2, 1.234567)"));
100 QVERIFY_SQL( q, exec("insert into " + relTEST1 + " values(2, 'trond', 2, 1, 8.901234)"));
101 QVERIFY_SQL( q, exec("insert into " + relTEST1 + " values(3, 'vohi', 1, 2, 5.678901)"));
102 QVERIFY_SQL( q, exec("insert into " + relTEST1 + " values(4, 'boris', 2, 2, 2.345678)"));
103}
104
105void tst_QSqlDriver::initTestCase()
106{
107 foreach (const QString &dbname, dbs.dbNames)
108 recreateTestTables(db: QSqlDatabase::database(connectionName: dbname));
109}
110
111void tst_QSqlDriver::cleanupTestCase()
112{
113 foreach (const QString &dbName, dbs.dbNames) {
114 QSqlDatabase db = QSqlDatabase::database(connectionName: dbName);
115 tst_Databases::safeDropTable(db, tableName: qTableName(prefix: "relTEST1", __FILE__, db));
116 const QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
117 if (dbType == QSqlDriver::Oracle)
118 tst_Databases::safeDropTable(db, tableName: qTableName(prefix: "clobTable", __FILE__, db));
119 }
120 dbs.close();
121}
122
123void tst_QSqlDriver::init()
124{
125}
126
127void tst_QSqlDriver::cleanup()
128{
129}
130
131void tst_QSqlDriver::record()
132{
133 QFETCH_GLOBAL(QString, dbName);
134 QSqlDatabase db = QSqlDatabase::database(connectionName: dbName);
135 CHECK_DATABASE(db);
136
137 QString tablename(qTableName(prefix: "relTEST1", __FILE__, db));
138 QStringList fields;
139 fields << "id" << "name" << "title_key" << "another_title_key" << "more_data";
140
141 //check we can get records using an unquoted mixed case table name
142 QSqlRecord rec = db.driver()->record(tableName: tablename);
143 QCOMPARE(rec.count(), fields.size());
144
145 QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
146 // QTBUG-1363: QSqlField::length() always return -1 when using QODBC3 driver and QSqlDatabase::record()
147 if (dbType == QSqlDriver::MSSqlServer && db.driverName().startsWith(s: "QODBC"))
148 QCOMPARE(rec.field(1).length(), 20);
149
150 if (dbType == QSqlDriver::Interbase || dbType == QSqlDriver::Oracle || dbType == QSqlDriver::DB2)
151 for(int i = 0; i < fields.count(); ++i)
152 fields[i] = fields[i].toUpper();
153
154 for (int i = 0; i < fields.count(); ++i)
155 QCOMPARE(rec.fieldName(i), fields[i]);
156
157 if (driverSupportsDefaultValues(dbType))
158 QCOMPARE(rec.field(QStringLiteral("name")).defaultValue().toString(), QStringLiteral("defaultVal"));
159
160 if (dbType == QSqlDriver::Interbase || dbType == QSqlDriver::Oracle || dbType == QSqlDriver::DB2)
161 tablename = tablename.toUpper();
162 else if (dbType == QSqlDriver::PostgreSQL)
163 tablename = tablename.toLower();
164
165 if (dbType != QSqlDriver::PostgreSQL && !db.driverName().startsWith(s: "QODBC")) {
166 //check we can get records using a properly quoted table name
167 rec = db.driver()->record(tableName: db.driver()->escapeIdentifier(identifier: tablename,type: QSqlDriver::TableName));
168 QCOMPARE(rec.count(), 5);
169 }
170
171 for (int i = 0; i < fields.count(); ++i)
172 QCOMPARE(rec.fieldName(i), fields[i]);
173
174 if (dbType == QSqlDriver::Interbase || dbType == QSqlDriver::Oracle || dbType == QSqlDriver::DB2)
175 tablename = tablename.toLower();
176 else if (dbType == QSqlDriver::PostgreSQL)
177 tablename = tablename.toUpper();
178
179 //check that we can't get records using incorrect tablename casing that's been quoted
180 rec = db.driver()->record(tableName: db.driver()->escapeIdentifier(identifier: tablename,type: QSqlDriver::TableName));
181 if (dbType == QSqlDriver::MySqlServer || dbType == QSqlDriver::SQLite || dbType == QSqlDriver::Sybase
182 || dbType == QSqlDriver::MSSqlServer || tst_Databases::isMSAccess(db))
183 QCOMPARE(rec.count(), 5); //mysql, sqlite and tds will match
184 else
185 QCOMPARE(rec.count(), 0);
186
187}
188
189void tst_QSqlDriver::primaryIndex()
190{
191 QFETCH_GLOBAL(QString, dbName);
192 QSqlDatabase db = QSqlDatabase::database(connectionName: dbName);
193 CHECK_DATABASE(db);
194
195 QString tablename(qTableName(prefix: "relTEST1", __FILE__, db));
196 //check that we can get primary index using unquoted mixed case table name
197 QSqlIndex index = db.driver()->primaryIndex(tableName: tablename);
198 QCOMPARE(index.count(), 1);
199
200 QSqlDriver::DbmsType dbType = tst_Databases::getDatabaseType(db);
201 if (dbType == QSqlDriver::Interbase || dbType == QSqlDriver::Oracle || dbType == QSqlDriver::DB2)
202 QCOMPARE(index.fieldName(0), QString::fromLatin1("ID"));
203 else
204 QCOMPARE(index.fieldName(0), QString::fromLatin1("id"));
205
206
207 //check that we can get the primary index using a quoted tablename
208 if (dbType == QSqlDriver::Interbase || dbType == QSqlDriver::Oracle || dbType == QSqlDriver::DB2)
209 tablename = tablename.toUpper();
210 else if (dbType == QSqlDriver::PostgreSQL)
211 tablename = tablename.toLower();
212
213 if (dbType != QSqlDriver::PostgreSQL && !db.driverName().startsWith(s: "QODBC")) {
214 index = db.driver()->primaryIndex(tableName: db.driver()->escapeIdentifier(identifier: tablename, type: QSqlDriver::TableName));
215 QCOMPARE(index.count(), 1);
216 }
217 if (dbType == QSqlDriver::Interbase || dbType == QSqlDriver::Oracle || dbType == QSqlDriver::DB2)
218 QCOMPARE(index.fieldName(0), QString::fromLatin1("ID"));
219 else
220 QCOMPARE(index.fieldName(0), QString::fromLatin1("id"));
221
222
223
224 //check that we can not get the primary index using a quoted but incorrect table name casing
225 if (dbType == QSqlDriver::Interbase || dbType == QSqlDriver::Oracle || dbType == QSqlDriver::DB2)
226 tablename = tablename.toLower();
227 else if (dbType == QSqlDriver::PostgreSQL)
228 tablename = tablename.toUpper();
229
230 index = db.driver()->primaryIndex(tableName: db.driver()->escapeIdentifier(identifier: tablename, type: QSqlDriver::TableName));
231 if (dbType == QSqlDriver::MySqlServer || dbType == QSqlDriver::SQLite || dbType == QSqlDriver::Sybase
232 || dbType == QSqlDriver::MSSqlServer || tst_Databases::isMSAccess(db))
233 QCOMPARE(index.count(), 1); //mysql will always find the table name regardless of casing
234 else
235 QCOMPARE(index.count(), 0);
236
237 // Test getting a primary index for a table with a clob in it - QTBUG-64427
238 if (dbType == QSqlDriver::Oracle) {
239 const QString clobTable(qTableName(prefix: "clobTable", __FILE__, db));
240 QSqlQuery qry(db);
241 QVERIFY_SQL(qry, exec("CREATE TABLE " + clobTable + " (id INTEGER, clobField CLOB)"));
242 QVERIFY_SQL(qry, exec("CREATE UNIQUE INDEX " + clobTable + "IDX ON " + clobTable + " (id)"));
243 QVERIFY_SQL(qry, exec("ALTER TABLE " + clobTable + " ADD CONSTRAINT " + clobTable +
244 "PK PRIMARY KEY(id)"));
245 QVERIFY_SQL(qry, exec("ALTER TABLE " + clobTable + " MODIFY (id NOT NULL ENABLE)"));
246 const QSqlIndex primaryIndex = db.driver()->primaryIndex(tableName: clobTable);
247 QCOMPARE(primaryIndex.count(), 1);
248 QCOMPARE(primaryIndex.fieldName(0), QStringLiteral("ID"));
249 }
250}
251
252void tst_QSqlDriver::formatValue()
253{
254 QFETCH_GLOBAL(QString, dbName);
255 QSqlDatabase db = QSqlDatabase::database(connectionName: dbName);
256 CHECK_DATABASE(db);
257 QString tablename(qTableName(prefix: "relTEST1", __FILE__, db));
258 QSqlQuery qry(db);
259 QVERIFY_SQL(qry, exec("SELECT * FROM " + tablename));
260 qry.next();
261 QSqlRecord rec = qry.record();
262 QCOMPARE(db.driver()->formatValue(rec.field("id")), QString("1"));
263 QCOMPARE(db.driver()->formatValue(rec.field("name")), QString("'harry'"));
264 QCOMPARE(db.driver()->formatValue(rec.field("more_data")), QString("1.234567"));
265}
266
267QTEST_MAIN(tst_QSqlDriver)
268#include "tst_qsqldriver.moc"
269

source code of qtbase/tests/auto/sql/kernel/qsqldriver/tst_qsqldriver.cpp