author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Thu, 08 Apr 2010 14:19:33 +0300 | |
branch | RCL_3 |
changeset 7 | 3f74d0d4af4c |
parent 4 | 3b1da2848fc7 |
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 |
||
42 |
||
43 |
#include <QtTest/QtTest> |
|
44 |
#include <q3sqlcursor.h> |
|
45 |
#include <qsqlfield.h> |
|
46 |
#include <qsqldriver.h> |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
47 |
#include <QSet> |
0 | 48 |
|
49 |
#include "../qsqldatabase/tst_databases.h" |
|
50 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
51 |
const QString qtest(qTableName( "qtest", __FILE__ )); |
0 | 52 |
|
53 |
//TESTED_FILES= |
|
54 |
||
55 |
QT_FORWARD_DECLARE_CLASS(QSqlDatabase) |
|
56 |
||
57 |
class tst_Q3SqlCursor : public QObject |
|
58 |
{ |
|
59 |
Q_OBJECT |
|
60 |
||
61 |
public: |
|
62 |
tst_Q3SqlCursor(); |
|
63 |
virtual ~tst_Q3SqlCursor(); |
|
64 |
||
65 |
||
66 |
public slots: |
|
67 |
void initTestCase(); |
|
68 |
void cleanupTestCase(); |
|
69 |
void init(); |
|
70 |
void cleanup(); |
|
71 |
private slots: |
|
72 |
void copyConstructor_data() { generic_data(); } |
|
73 |
void copyConstructor(); |
|
74 |
||
75 |
void value_data() { generic_data(); } |
|
76 |
void value(); |
|
77 |
void primaryIndex_data() { generic_data(); } |
|
78 |
void primaryIndex(); |
|
79 |
void insert_data() { generic_data(); } |
|
80 |
void insert(); |
|
81 |
void select_data() { generic_data(); } |
|
82 |
void select(); |
|
83 |
void setFilter_data() { generic_data(); } |
|
84 |
void setFilter(); |
|
85 |
void setName_data() { generic_data(); } |
|
86 |
void setName(); |
|
87 |
||
88 |
// problem specific tests |
|
89 |
void unicode_data() { generic_data(); } |
|
90 |
void unicode(); |
|
91 |
void precision_data() { generic_data(); } |
|
92 |
void precision(); |
|
93 |
void insertORA_data() { generic_data("QOCI"); } |
|
94 |
void insertORA(); |
|
95 |
void batchInsert_data() { generic_data(); } |
|
96 |
void batchInsert(); |
|
97 |
void insertSpecial_data() { generic_data(); } |
|
98 |
void insertSpecial(); |
|
99 |
void updateNoPK_data() { generic_data(); } |
|
100 |
void updateNoPK(); |
|
101 |
void insertFieldNameContainsWS_data() { generic_data(); } |
|
102 |
void insertFieldNameContainsWS(); // For task 117996 |
|
103 |
||
104 |
private: |
|
105 |
void generic_data(const QString &engine=QString()); |
|
106 |
void createTestTables( QSqlDatabase db ); |
|
107 |
void dropTestTables( QSqlDatabase db ); |
|
108 |
void populateTestTables( QSqlDatabase db ); |
|
109 |
||
110 |
tst_Databases dbs; |
|
111 |
}; |
|
112 |
||
113 |
tst_Q3SqlCursor::tst_Q3SqlCursor() |
|
114 |
{ |
|
115 |
} |
|
116 |
||
117 |
tst_Q3SqlCursor::~tst_Q3SqlCursor() |
|
118 |
{ |
|
119 |
} |
|
120 |
||
121 |
void tst_Q3SqlCursor::generic_data(const QString &engine) |
|
122 |
{ |
|
123 |
if ( dbs.fillTestTable(engine) == 0 ) { |
|
124 |
if(engine.isEmpty()) |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
125 |
QSKIP( "No database drivers are available in this Qt configuration", SkipAll ); |
0 | 126 |
else |
127 |
QSKIP( (QString("No database drivers of type %1 are available in this Qt configuration").arg(engine)).toLocal8Bit(), SkipAll ); |
|
128 |
} |
|
129 |
} |
|
130 |
||
131 |
void tst_Q3SqlCursor::createTestTables( QSqlDatabase db ) |
|
132 |
{ |
|
133 |
if ( !db.isValid() ) |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
134 |
return; |
0 | 135 |
QSqlQuery q( db ); |
136 |
||
137 |
if (tst_Databases::isSqlServer(db)) { |
|
138 |
QVERIFY_SQL(q, exec("SET ANSI_DEFAULTS ON")); |
|
139 |
QVERIFY_SQL(q, exec("SET IMPLICIT_TRANSACTIONS OFF")); |
|
140 |
} |
|
141 |
else if(tst_Databases::isPostgreSQL(db)) |
|
142 |
QVERIFY_SQL( q, exec("set client_min_messages='warning'")); |
|
143 |
||
144 |
// please never ever change this table; otherwise fix all tests ;) |
|
145 |
if ( tst_Databases::isMSAccess( db ) ) { |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
146 |
QVERIFY_SQL(q, exec( "create table " + qtest + " ( id int not null, t_varchar varchar(40) not null," |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
147 |
"t_char char(40), t_numeric number, primary key (id, t_varchar) )" )); |
0 | 148 |
} else { |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
149 |
QVERIFY_SQL(q, exec( "create table " + qtest + " ( id int not null, t_varchar varchar(40) not null," |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
150 |
"t_char char(40), t_numeric numeric(6, 3), primary key (id, t_varchar) )" )); |
0 | 151 |
} |
152 |
||
153 |
if ( tst_Databases::isSqlServer( db ) ) { |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
154 |
//workaround for SQL SERVER since he can store unicode only in nvarchar fields |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
155 |
QVERIFY_SQL(q, exec("create table " + qTableName("qtest_unicode", __FILE__) + " (id int not null, " |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
156 |
"t_varchar nvarchar(80) not null, t_char nchar(80) )" )); |
0 | 157 |
} else { |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
158 |
QVERIFY_SQL(q, exec("create table " + qTableName("qtest_unicode", __FILE__) + " (id int not null, " |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
159 |
"t_varchar varchar(100) not null," "t_char char(100))" )); |
0 | 160 |
} |
161 |
||
162 |
if (tst_Databases::isMSAccess(db)) { |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
163 |
QVERIFY_SQL(q, exec("create table " + qTableName("qtest_precision", __FILE__) + " (col1 number)")); |
0 | 164 |
} else { |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
165 |
QVERIFY_SQL(q, exec("create table " + qTableName("qtest_precision", __FILE__) + " (col1 numeric(15, 14))")); |
0 | 166 |
} |
167 |
} |
|
168 |
||
169 |
void tst_Q3SqlCursor::dropTestTables( QSqlDatabase db ) |
|
170 |
{ |
|
171 |
if ( !db.isValid() ) |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
172 |
return; |
0 | 173 |
QStringList tableNames; |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
174 |
tableNames << qtest |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
175 |
<< qTableName( "qtest_unicode", __FILE__ ) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
176 |
<< qTableName( "qtest_precision", __FILE__ ) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
177 |
<< qTableName( "qtest_ovchar", __FILE__ ) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
178 |
<< qTableName( "qtest_onvchar", __FILE__ ) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
179 |
<< qTableName( "qtestPK", __FILE__ ); |
0 | 180 |
tst_Databases::safeDropTables( db, tableNames ); |
181 |
} |
|
182 |
||
183 |
void tst_Q3SqlCursor::populateTestTables( QSqlDatabase db ) |
|
184 |
{ |
|
185 |
if (!db.isValid()) |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
186 |
return; |
0 | 187 |
QSqlQuery q( db ); |
188 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
189 |
q.exec( "delete from " + qtest ); //not fatal |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
190 |
QVERIFY_SQL(q, prepare("insert into " + qtest + " (id, t_varchar, t_char, t_numeric) values (?, ?, ?, ?)")); |
0 | 191 |
q.addBindValue(QVariantList() << 0 << 1 << 2 << 3); |
192 |
q.addBindValue(QVariantList() << "VarChar0" << "VarChar1" << "VarChar2" << "VarChar3"); |
|
193 |
q.addBindValue(QVariantList() << "Char0" << "Char1" << "Char2" << "Char3"); |
|
194 |
q.addBindValue(QVariantList() << 1.1 << 2.2 << 3.3 << 4.4); |
|
195 |
QVERIFY_SQL(q, execBatch()); |
|
196 |
} |
|
197 |
||
198 |
void tst_Q3SqlCursor::initTestCase() |
|
199 |
{ |
|
200 |
dbs.open(); |
|
201 |
||
202 |
for ( QStringList::ConstIterator it = dbs.dbNames.begin(); it != dbs.dbNames.end(); ++it ) { |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
203 |
QSqlDatabase db = QSqlDatabase::database( (*it) ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
204 |
CHECK_DATABASE( db ); |
0 | 205 |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
206 |
dropTestTables( db ); //in case of leftovers |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
207 |
createTestTables( db ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
208 |
populateTestTables( db ); |
0 | 209 |
} |
210 |
} |
|
211 |
||
212 |
void tst_Q3SqlCursor::cleanupTestCase() |
|
213 |
{ |
|
214 |
for ( QStringList::ConstIterator it = dbs.dbNames.begin(); it != dbs.dbNames.end(); ++it ) { |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
215 |
QSqlDatabase db = QSqlDatabase::database( (*it) ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
216 |
CHECK_DATABASE( db ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
217 |
dropTestTables( db ); |
0 | 218 |
} |
219 |
||
220 |
dbs.close(); |
|
221 |
} |
|
222 |
||
223 |
void tst_Q3SqlCursor::init() |
|
224 |
{ |
|
225 |
} |
|
226 |
||
227 |
void tst_Q3SqlCursor::cleanup() |
|
228 |
{ |
|
229 |
QFETCH( QString, dbName ); |
|
230 |
QSqlDatabase db = QSqlDatabase::database( dbName ); |
|
231 |
CHECK_DATABASE( db ); |
|
232 |
if ( QTest::currentTestFailed() ) { |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
233 |
//since Oracle ODBC totally craps out on error, we init again |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
234 |
db.close(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
235 |
db.open(); |
0 | 236 |
} |
237 |
} |
|
238 |
||
239 |
void tst_Q3SqlCursor::copyConstructor() |
|
240 |
{ |
|
241 |
QFETCH( QString, dbName ); |
|
242 |
QSqlDatabase db = QSqlDatabase::database( dbName ); |
|
243 |
CHECK_DATABASE( db ); |
|
244 |
||
245 |
Q3SqlCursor cur2; |
|
246 |
{ |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
247 |
Q3SqlCursor cur( qtest, true, db ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
248 |
QVERIFY_SQL(cur, select( cur.index( QString("id") ) )); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
249 |
cur2 = Q3SqlCursor( cur ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
250 |
// let "cur" run out of scope... |
0 | 251 |
} |
252 |
||
253 |
QSqlRecord* rec = cur2.primeUpdate(); |
|
254 |
Q_ASSERT( rec ); |
|
255 |
QCOMPARE( (int)rec->count(), 4 ); |
|
256 |
||
257 |
int i = 0; |
|
258 |
while ( cur2.next() ) { |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
259 |
QVERIFY( cur2.value("id").toInt() == i ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
260 |
i++; |
0 | 261 |
} |
262 |
} |
|
263 |
||
264 |
void tst_Q3SqlCursor::value() |
|
265 |
{ |
|
266 |
QFETCH( QString, dbName ); |
|
267 |
QSqlDatabase db = QSqlDatabase::database( dbName ); |
|
268 |
CHECK_DATABASE( db ); |
|
269 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
270 |
Q3SqlCursor cur( qtest, true, db ); |
0 | 271 |
QVERIFY_SQL(cur, select( cur.index( QString("id") ) )); |
272 |
int i = 0; |
|
273 |
while ( cur.next() ) { |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
274 |
QCOMPARE(cur.value("id").toInt(), i); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
275 |
i++; |
0 | 276 |
} |
277 |
} |
|
278 |
||
279 |
void tst_Q3SqlCursor::primaryIndex() |
|
280 |
{ |
|
281 |
QFETCH( QString, dbName ); |
|
282 |
QSqlDatabase db = QSqlDatabase::database( dbName ); |
|
283 |
CHECK_DATABASE( db ); |
|
284 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
285 |
Q3SqlCursor cur( qtest, true, db ); |
0 | 286 |
QSqlIndex index = cur.primaryIndex(); |
287 |
if ( tst_Databases::isMSAccess( db ) ) { |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
288 |
QCOMPARE( index.fieldName(1).upper(), QString( "ID" ) ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
289 |
QCOMPARE( index.fieldName(0).upper(), QString( "T_VARCHAR" ) ); |
0 | 290 |
} else { |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
291 |
QCOMPARE( index.fieldName(0).upper(), QString( "ID" ) ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
292 |
QCOMPARE( index.fieldName(1).upper(), QString( "T_VARCHAR" ) ); |
0 | 293 |
} |
294 |
QVERIFY(!index.isDescending(0)); |
|
295 |
QVERIFY(!index.isDescending(1)); |
|
296 |
} |
|
297 |
||
298 |
void tst_Q3SqlCursor::insert() |
|
299 |
{ |
|
300 |
QFETCH( QString, dbName ); |
|
301 |
QSqlDatabase db = QSqlDatabase::database( dbName ); |
|
302 |
CHECK_DATABASE( db ); |
|
303 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
304 |
Q3SqlCursor cur( qtest, true, db ); |
0 | 305 |
QSqlRecord* irec = cur.primeInsert(); |
306 |
QVERIFY( irec != 0 ); |
|
307 |
||
308 |
// check that primeInsert returns a valid QSqlRecord |
|
309 |
QCOMPARE( (int)irec->count(), 4 ); |
|
310 |
if ( ( irec->field( 0 ).type() != QVariant::Int ) && |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
311 |
( irec->field( 0 ).type() != QVariant::String ) && |
0 | 312 |
( irec->field( 0 ).type() != QVariant::Double ) ) { |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
313 |
QFAIL( QString( "Wrong datatype %1 for field 'ID'" |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
314 |
" (expected Int or String)" ).arg( QVariant::typeToName( irec->field( 0 ).type() ) ) ); |
0 | 315 |
} |
316 |
QCOMPARE( QVariant::typeToName( irec->field( 1 ).type() ), QVariant::typeToName( QVariant::String ) ); |
|
317 |
QCOMPARE( QVariant::typeToName( irec->field( 2 ).type() ), QVariant::typeToName( QVariant::String ) ); |
|
318 |
QVERIFY((QVariant::typeToName(irec->field(3).type()) == QVariant::typeToName(QVariant::Double)) || |
|
319 |
(QVariant::typeToName(irec->field(3).type()) == QVariant::typeToName(QVariant::String))); |
|
320 |
QCOMPARE( irec->field( 0 ).name().upper(), QString( "ID" ) ); |
|
321 |
QCOMPARE( irec->field( 1 ).name().upper(), QString( "T_VARCHAR" ) ); |
|
322 |
QCOMPARE( irec->field( 2 ).name().upper(), QString( "T_CHAR" ) ); |
|
323 |
QCOMPARE( irec->field( 3 ).name().upper(), QString( "T_NUMERIC" ) ); |
|
324 |
||
325 |
irec->setValue( "id", 400 ); |
|
326 |
irec->setValue( "t_varchar", "SomeVarChar" ); |
|
327 |
irec->setValue( "t_char", "SomeChar" ); |
|
328 |
irec->setValue( "t_numeric", 400.400 ); |
|
329 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
330 |
QSet<int> validReturns(QSet<int>() << -1 << 1); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
331 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
332 |
QVERIFY( validReturns.contains(cur.insert()) ); |
0 | 333 |
|
334 |
// restore old test-tables |
|
335 |
populateTestTables( db ); |
|
336 |
} |
|
337 |
||
338 |
void tst_Q3SqlCursor::insertSpecial() |
|
339 |
{ |
|
340 |
QFETCH( QString, dbName ); |
|
341 |
QSqlDatabase db = QSqlDatabase::database( dbName ); |
|
342 |
CHECK_DATABASE( db ); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
343 |
QSet<int> validReturns(QSet<int>() << -1 << 1); |
0 | 344 |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
345 |
Q3SqlCursor cur( qtest, true, db ); |
0 | 346 |
QSqlRecord* irec = cur.primeInsert(); |
347 |
QVERIFY( irec != 0 ); |
|
348 |
||
349 |
QStringList strings; |
|
350 |
strings << "StringWith'ATick" << "StringWith\"Doublequote" << "StringWith\\Backslash" << "StringWith~Tilde"; |
|
351 |
strings << "StringWith%Percent" << "StringWith_Underscore" << "StringWith[SquareBracket" << "StringWith{Brace"; |
|
352 |
strings << "StringWith''DoubleTick" << "StringWith\\Lot\\of\\Backslash" << "StringWith\"lot\"of\"quotes\""; |
|
353 |
strings << "'StartsAndEndsWithTick'" << "\"StartsAndEndsWithQuote\""; |
|
354 |
strings << "StringWith\nCR" << "StringWith\n\rCRLF"; |
|
355 |
||
356 |
int i = 800; |
|
357 |
||
358 |
// INSERT the strings |
|
359 |
QStringList::Iterator it; |
|
360 |
for ( it = strings.begin(); it != strings.end(); ++it ) { |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
361 |
QSqlRecord* irec = cur.primeInsert(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
362 |
QVERIFY( irec != 0 ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
363 |
irec->setValue( "id", i ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
364 |
irec->setValue( "t_varchar", (*it) ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
365 |
irec->setValue( "t_char", (*it) ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
366 |
irec->setValue( "t_numeric", (double)i ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
367 |
++i; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
368 |
QVERIFY( validReturns.contains(cur.insert()) ); |
0 | 369 |
} |
370 |
||
371 |
QVERIFY( cur.select( "id >= 800 and id < 900" ) ); |
|
372 |
||
373 |
int i2 = 800; |
|
374 |
while( cur.next() ) { |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
375 |
QCOMPARE( cur.value( "id" ).toInt(), i2 ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
376 |
QCOMPARE( cur.value( "t_varchar" ).toString().stripWhiteSpace(), strings.at( i2 - 800 ) ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
377 |
QCOMPARE( cur.value( "t_char" ).toString().stripWhiteSpace(), strings.at( i2 - 800 ) ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
378 |
QCOMPARE( cur.value( "t_numeric" ).toDouble(), (double)i2 ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
379 |
++i2; |
0 | 380 |
} |
381 |
QCOMPARE( i, i2 ); |
|
382 |
||
383 |
populateTestTables( db ); |
|
384 |
} |
|
385 |
||
386 |
void tst_Q3SqlCursor::batchInsert() |
|
387 |
{ |
|
388 |
QFETCH( QString, dbName ); |
|
389 |
QSqlDatabase db = QSqlDatabase::database( dbName ); |
|
390 |
CHECK_DATABASE( db ); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
391 |
QSet<int> validReturns(QSet<int>() << -1 << 1); |
0 | 392 |
|
393 |
QSqlQuery q( db ); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
394 |
q.exec( "delete from " + qtest ); |
0 | 395 |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
396 |
Q3SqlCursor cur( qtest, true, db ); |
0 | 397 |
|
398 |
int i = 0; |
|
399 |
for ( ; i < 100; ++i ) { |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
400 |
QSqlRecord* irec = cur.primeInsert(); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
401 |
Q_ASSERT( irec ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
402 |
irec->setValue( "id", i ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
403 |
irec->setValue( "t_varchar", "blah" ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
404 |
irec->setValue( "t_char", "blah" ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
405 |
irec->setValue( "t_numeric", 1.1 ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
406 |
if ( db.driverName().startsWith( "QSQLITE" ) ) { |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
407 |
QVERIFY( cur.insert( true ) ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
408 |
} else { |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
409 |
QVERIFY( validReturns.contains(cur.insert( true )) ); |
0 | 410 |
} |
411 |
} |
|
412 |
||
413 |
for ( ; i < 200; ++i ) { |
|
414 |
QSqlRecord* irec = cur.primeInsert(); |
|
415 |
Q_ASSERT( irec ); |
|
416 |
irec->setValue( "id", i ); |
|
417 |
irec->setValue( "t_varchar", "blah" ); |
|
418 |
irec->setValue( "t_char", "blah" ); |
|
419 |
irec->setValue( "t_numeric", 1.1 ); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
420 |
if ( db.driverName().startsWith( "QSQLITE" ) ) { |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
421 |
QVERIFY( cur.insert( false ) ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
422 |
} else { |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
423 |
QVERIFY( validReturns.contains(cur.insert( false )) ); |
0 | 424 |
} |
425 |
} |
|
426 |
||
427 |
i = 0; |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
428 |
QVERIFY_SQL(q, exec( "select * from " + qtest + " order by id" )); |
0 | 429 |
while ( q.next() ) { |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
430 |
QCOMPARE( q.value( 0 ).toInt(), i ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
431 |
i++; |
0 | 432 |
} |
433 |
||
434 |
QCOMPARE( i, 200 ); |
|
435 |
||
436 |
populateTestTables( db ); |
|
437 |
} |
|
438 |
||
439 |
static QString dumpUtf8( const QString& str ) |
|
440 |
{ |
|
441 |
QString res; |
|
442 |
for ( int i = 0; i < (int)str.length(); ++i ) { |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
443 |
res += "0x" + QString::number( str[ i ].unicode(), 16 ) + ' '; |
0 | 444 |
} |
445 |
return res; |
|
446 |
} |
|
447 |
||
448 |
void tst_Q3SqlCursor::insertORA() |
|
449 |
{ |
|
450 |
QFETCH( QString, dbName ); |
|
451 |
QSqlDatabase db = QSqlDatabase::database( dbName ); |
|
452 |
CHECK_DATABASE( db ); |
|
453 |
||
454 |
if (tst_Databases::getOraVersion(db) < 9) |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
455 |
QSKIP("Need Oracle >= 9", SkipSingle); |
0 | 456 |
|
457 |
/****** CHARSET TEST ******/ |
|
458 |
||
459 |
QSqlQuery q( db ); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
460 |
QVERIFY_SQL(q, exec( "create table " + qTableName( "qtest_ovchar", __FILE__ ) + " ( id int primary key, t_char varchar(40) )" )); |
0 | 461 |
|
462 |
static const QString val1( "blah1" ); |
|
463 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
464 |
Q3SqlCursor cur ( qTableName( "qtest_ovchar", __FILE__ ), true, db ); |
0 | 465 |
QSqlRecord* irec = cur.primeInsert(); |
466 |
irec->setValue( "id", 1 ); |
|
467 |
irec->setValue( "t_char", val1 ); |
|
468 |
QVERIFY( cur.insert() ); |
|
469 |
||
470 |
QVERIFY_SQL(cur, select()); |
|
471 |
QVERIFY( cur.next() ); |
|
472 |
if ( cur.value( "t_char" ).toString() != val1 ) |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
473 |
qDebug( QString( "Wrong value for t_char: expected '%1', got '%2'" ).arg( val1 ).arg( |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
474 |
cur.value( "t_char" ).toString() ) ); |
0 | 475 |
|
476 |
static const unsigned short utf8arr[] = { 0xd792,0xd79c,0xd792,0xd79c,0xd799,0x00 }; |
|
477 |
static const QString utf8str = QString::fromUcs2( utf8arr ); |
|
478 |
||
479 |
irec = cur.primeInsert(); |
|
480 |
irec->setValue( "id", 2 ); |
|
481 |
irec->setValue( "t_char", utf8str ); |
|
482 |
QVERIFY( cur.insert() ); |
|
483 |
||
484 |
QVERIFY_SQL(cur, select( "id=2" )); |
|
485 |
QVERIFY( cur.next() ); |
|
486 |
||
487 |
// until qtest knows non-fatal errors we use qDebug instead |
|
488 |
if ( cur.value( "t_char" ).toString() != utf8str ) |
|
489 |
qDebug( QString( "Wrong value for t_char: expected '%1', got '%2'" ).arg( dumpUtf8 ( utf8str ) ).arg( |
|
490 |
dumpUtf8( cur.value( "t_char" ).toString() ) ) ); |
|
491 |
||
492 |
/****** NCHARSET TEST ********/ |
|
493 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
494 |
QVERIFY_SQL(q, exec( "create table " + qTableName( "qtest_onvchar", __FILE__ ) + " ( id int primary key, t_nchar nvarchar2(40) )" )); |
0 | 495 |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
496 |
Q3SqlCursor cur2 ( qTableName( "qtest_onvchar", __FILE__ ), true, db ); |
0 | 497 |
irec = cur2.primeInsert(); |
498 |
irec->setValue( "id", 1 ); |
|
499 |
irec->setValue( "t_nchar", val1 ); |
|
500 |
QVERIFY( cur2.insert() ); |
|
501 |
||
502 |
QVERIFY_SQL(cur2, select()); |
|
503 |
QVERIFY( cur2.next() ); |
|
504 |
if ( cur2.value( "t_nchar" ).toString() != val1 ) |
|
505 |
qDebug( QString( "Wrong value for t_nchar: expected '%1', got '%2'" ).arg( val1 ).arg( |
|
506 |
cur2.value( "t_nchar" ).toString() ) ); |
|
507 |
||
508 |
irec = cur2.primeInsert(); |
|
509 |
irec->setValue( "id", 2 ); |
|
510 |
irec->setValue( "t_nchar", utf8str ); |
|
511 |
QVERIFY( cur2.insert() ); |
|
512 |
||
513 |
QVERIFY_SQL(cur2, select( "id=2" )); |
|
514 |
QVERIFY( cur2.next() ); |
|
515 |
||
516 |
// until qtest knows non-fatal errors we use qDebug instead |
|
517 |
if ( cur2.value( "t_nchar" ).toString() != utf8str ) |
|
518 |
qDebug( QString( "Wrong value for t_nchar: expected '%1', got '%2'" ).arg( dumpUtf8( utf8str ) ).arg( |
|
519 |
dumpUtf8( cur2.value( "t_nchar" ).toString() ) ) ); |
|
520 |
||
521 |
} |
|
522 |
||
523 |
void tst_Q3SqlCursor::unicode() |
|
524 |
{ |
|
525 |
QFETCH( QString, dbName ); |
|
526 |
QSqlDatabase db = QSqlDatabase::database( dbName ); |
|
527 |
CHECK_DATABASE( db ); |
|
528 |
||
529 |
static const QString utf8str = QString::fromUtf8( "ὕαλον ϕαγεῖν δύναμαι· τοῦτο οὔ με βλάπτει." ); |
|
530 |
if ( !db.driver()->hasFeature( QSqlDriver::Unicode ) ) { |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
531 |
QSKIP( "DBMS not Unicode capable", SkipSingle ); |
0 | 532 |
} |
533 |
// ascii in the data storage, can't transliterate properly. invalid test. |
|
534 |
if(db.driverName().startsWith("QIBASE") && (db.databaseName() == "silence.nokia.troll.no:c:\\ibase\\testdb_ascii" || db.databaseName() == "/opt/interbase/qttest.gdb")) |
|
535 |
QSKIP("Can't transliterate extended unicode to ascii", SkipSingle); |
|
536 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
537 |
Q3SqlCursor cur( qTableName( "qtest_unicode", __FILE__ ), true, db ); |
0 | 538 |
QSqlRecord* irec = cur.primeInsert(); |
539 |
irec->setValue( 0, 500 ); |
|
540 |
irec->setValue( 1, utf8str ); |
|
541 |
irec->setValue( 2, utf8str ); |
|
542 |
QVERIFY_SQL(cur, insert()); |
|
543 |
QVERIFY_SQL(cur, select( "id=500" )); |
|
544 |
QVERIFY_SQL(cur, next()); |
|
545 |
QString res = cur.value( 1 ).asString(); |
|
546 |
cur.primeDelete(); |
|
547 |
cur.del(); |
|
548 |
||
549 |
if ( res != utf8str ) { |
|
550 |
int i; |
|
551 |
for ( i = 0; i < (int)res.length(); ++i ) { |
|
552 |
if ( res[ i ] != utf8str[ i ] ) |
|
553 |
break; |
|
554 |
} |
|
555 |
if(db.driverName().startsWith("QMYSQL") || db.driverName().startsWith("QDB2")) |
|
556 |
qWarning() << "Needs someone with more Unicode knowledge than I have to fix:" << QString( "Strings differ at position %1: orig: %2, db: %3" ).arg( i ).arg( utf8str[ i ].unicode(), 0, 16 ).arg( res[ i ].unicode(), 0, 16 ); |
|
557 |
else |
|
558 |
QFAIL( QString( "Strings differ at position %1: orig: %2, db: %3" ).arg( i ).arg( utf8str[ i ].unicode(), 0, 16 ).arg( res[ i ].unicode(), 0, 16 ) ); |
|
559 |
} |
|
560 |
if((db.driverName().startsWith("QMYSQL") || db.driverName().startsWith("QDB2")) && res != utf8str) |
|
561 |
QEXPECT_FAIL("", "See above message", Continue); |
|
562 |
QVERIFY( res == utf8str ); |
|
563 |
} |
|
564 |
||
565 |
void tst_Q3SqlCursor::precision() |
|
566 |
{ |
|
567 |
static const QString precStr = QLatin1String("1.23456789012345"); |
|
568 |
static const double precDbl = 2.23456789012345; |
|
569 |
||
570 |
QFETCH( QString, dbName ); |
|
571 |
QSqlDatabase db = QSqlDatabase::database( dbName ); |
|
572 |
CHECK_DATABASE( db ); |
|
573 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
574 |
Q3SqlCursor cur( qTableName( "qtest_precision", __FILE__ ), true, db ); |
0 | 575 |
cur.setTrimmed( "col1", true ); |
576 |
QSqlRecord* irec = cur.primeInsert(); |
|
577 |
irec->setValue( 0, precStr ); |
|
578 |
QVERIFY( cur.insert() ); |
|
579 |
||
580 |
irec = cur.primeInsert(); |
|
581 |
irec->setValue( 0, precDbl ); |
|
582 |
QVERIFY( cur.insert() ); |
|
583 |
||
584 |
QVERIFY_SQL(cur, select()); |
|
585 |
QVERIFY( cur.next() ); |
|
586 |
QCOMPARE( cur.value( 0 ).asString(), precStr ); |
|
587 |
QVERIFY( cur.next() ); |
|
588 |
QCOMPARE( cur.value( 0 ).asDouble(), precDbl ); |
|
589 |
} |
|
590 |
||
591 |
void tst_Q3SqlCursor::setFilter() |
|
592 |
{ |
|
593 |
QFETCH( QString, dbName ); |
|
594 |
QSqlDatabase db = QSqlDatabase::database( dbName ); |
|
595 |
CHECK_DATABASE( db ); |
|
596 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
597 |
Q3SqlCursor cur( qtest, true, db ); |
0 | 598 |
cur.setFilter( "id = 2" ); |
599 |
||
600 |
QVERIFY_SQL(cur, select()); |
|
601 |
QVERIFY( cur.next() ); |
|
602 |
QCOMPARE( cur.value( "id" ).toInt(), 2 ); |
|
603 |
QVERIFY( !cur.next() ); |
|
604 |
||
605 |
QVERIFY_SQL(cur, select()); |
|
606 |
QVERIFY( cur.next() ); |
|
607 |
QCOMPARE( cur.value( "id" ).toInt(), 2 ); |
|
608 |
QVERIFY( !cur.next() ); |
|
609 |
||
610 |
QVERIFY_SQL(cur, select( "id = 3" )); |
|
611 |
QVERIFY( cur.next() ); |
|
612 |
QCOMPARE( cur.value( "id" ).toInt(), 3 ); |
|
613 |
QVERIFY( !cur.next() ); |
|
614 |
||
615 |
QVERIFY_SQL(cur, select()); |
|
616 |
QVERIFY( cur.next() ); |
|
617 |
QCOMPARE( cur.value( "id" ).toInt(), 3 ); |
|
618 |
QVERIFY( !cur.next() ); |
|
619 |
} |
|
620 |
||
621 |
void tst_Q3SqlCursor::select() |
|
622 |
{ |
|
623 |
QFETCH( QString, dbName ); |
|
624 |
QSqlDatabase db = QSqlDatabase::database( dbName ); |
|
625 |
CHECK_DATABASE( db ); |
|
626 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
627 |
Q3SqlCursor cur( qtest, true, db ); |
0 | 628 |
QVERIFY_SQL(cur, select()); |
629 |
QVERIFY( cur.next() ); |
|
630 |
QVERIFY( cur.next() ); |
|
631 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
632 |
Q3SqlCursor cur2( qtest, true, db ); |
0 | 633 |
QVERIFY_SQL(cur2, select( "id = 1" )); |
634 |
QVERIFY( cur2.next() ); |
|
635 |
QCOMPARE( cur2.value( 0 ).toInt(), 1 ); |
|
636 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
637 |
Q3SqlCursor cur3( qtest, true, db ); |
0 | 638 |
QVERIFY_SQL(cur3, select( cur3.primaryIndex( false ) )); |
639 |
QVERIFY( cur3.next() ); |
|
640 |
QVERIFY( cur3.next() ); |
|
641 |
QCOMPARE( cur3.value( 0 ).toInt(), 1 ); |
|
642 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
643 |
Q3SqlCursor cur4( qtest, true, db ); |
0 | 644 |
QSqlIndex idx = cur4.primaryIndex( false ); |
645 |
QCOMPARE( (int)idx.count(), 2 ); |
|
646 |
if ( tst_Databases::isMSAccess( db ) ) { |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
647 |
QCOMPARE( idx.field( 1 ).name().upper(), QString("ID") ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
648 |
QCOMPARE( idx.field( 0 ).name().upper(), QString("T_VARCHAR") ); |
0 | 649 |
} else { |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
650 |
QCOMPARE( idx.field( 0 ).name().upper(), QString("ID") ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
651 |
QCOMPARE( idx.field( 1 ).name().upper(), QString("T_VARCHAR") ); |
0 | 652 |
} |
653 |
||
654 |
#ifdef QT_DEBUG |
|
655 |
// Ignore debugging message advising users of a potential pitfall. |
|
656 |
QTest::ignoreMessage(QtDebugMsg, "Q3SqlCursor::setValue(): This will not affect actual database values. Use primeInsert(), primeUpdate() or primeDelete()."); |
|
657 |
#endif |
|
658 |
cur4.setValue( "id", 1 ); |
|
659 |
#ifdef QT_DEBUG |
|
660 |
QTest::ignoreMessage(QtDebugMsg, "Q3SqlCursor::setValue(): This will not affect actual database values. Use primeInsert(), primeUpdate() or primeDelete()."); |
|
661 |
#endif |
|
662 |
cur4.setValue( "t_varchar", "VarChar1" ); |
|
663 |
||
664 |
QVERIFY_SQL(cur4, select( idx, cur4.primaryIndex( false ) )); |
|
665 |
QVERIFY( cur4.next() ); |
|
666 |
QCOMPARE( cur4.value( 0 ).toInt(), 1 ); |
|
667 |
} |
|
668 |
||
669 |
void tst_Q3SqlCursor::setName() |
|
670 |
{ |
|
671 |
QFETCH( QString, dbName ); |
|
672 |
QSqlDatabase db = QSqlDatabase::database( dbName ); |
|
673 |
CHECK_DATABASE( db ); |
|
674 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
675 |
Q3SqlCursor c2( qtest, true, db ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
676 |
QCOMPARE( c2.name(), qtest ); |
0 | 677 |
QCOMPARE( c2.fieldName( 0 ).lower(), QString( "id" ) ); |
678 |
||
679 |
Q3SqlCursor c( QString(), true, db ); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
680 |
c.setName( qtest ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
681 |
QCOMPARE( c.name(), qtest ); |
0 | 682 |
QCOMPARE( c.fieldName( 0 ).lower(), QString( "id" ) ); |
683 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
684 |
c.setName( qTableName( "qtest_precision", __FILE__ ) ); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
685 |
QCOMPARE( c.name(), qTableName( "qtest_precision", __FILE__ ) ); |
0 | 686 |
QCOMPARE( c.fieldName( 0 ).lower(), QString( "col1" ) ); |
687 |
} |
|
688 |
||
689 |
/* Database independent test */ |
|
690 |
void tst_Q3SqlCursor::updateNoPK() |
|
691 |
{ |
|
692 |
QFETCH( QString, dbName ); |
|
693 |
QSqlDatabase db = QSqlDatabase::database( dbName ); |
|
694 |
CHECK_DATABASE( db ); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
695 |
QSet<int> validReturns(QSet<int>() << -1 << 1); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
696 |
|
0 | 697 |
QSqlQuery q(db); |
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
698 |
QVERIFY_SQL(q, exec("create table " + qTableName( "qtestPK", __FILE__ ) + " (id int, name varchar(20), num numeric)")); |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
699 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
700 |
Q3SqlCursor cur(qTableName("qtestPK", __FILE__), true, db); |
0 | 701 |
QSqlRecord* rec = cur.primeInsert(); |
702 |
Q_ASSERT(rec); |
|
703 |
rec->setNull(0); |
|
704 |
rec->setNull(1); |
|
705 |
rec->setNull(2); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
706 |
QVERIFY(validReturns.contains(cur.insert())); |
0 | 707 |
if (!db.driver()->hasFeature(QSqlDriver::PreparedQueries)) { |
708 |
||
709 |
// Only QPSQL, QMYSQL, QODBC and QOCI drivers currently use escape identifiers for column names |
|
710 |
if (db.driverName().startsWith("QPSQL") || |
|
711 |
db.driverName().startsWith("QMYSQL") || |
|
712 |
db.driverName().startsWith("QODBC") || |
|
713 |
db.driverName().startsWith("QOCI")) { |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
714 |
QString query = QString::fromLatin1("insert into " + qTableName("qtestPK", __FILE__) + |
0 | 715 |
" (" + db.driver()->escapeIdentifier("id", QSqlDriver::FieldName) + ',' |
716 |
+ db.driver()->escapeIdentifier("name", QSqlDriver::FieldName) + ',' |
|
717 |
+ db.driver()->escapeIdentifier("num", QSqlDriver::FieldName) + ')' |
|
718 |
+ " values (NULL,NULL,NULL)"); |
|
719 |
QCOMPARE(cur.lastQuery(), query); |
|
720 |
} else { |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
721 |
QCOMPARE(cur.lastQuery(), QString::fromLatin1("insert into " + qTableName("qtestPK", __FILE__) + |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
722 |
" (\"id\",\"name\",\"num\") values (NULL,NULL,NULL)")); |
0 | 723 |
} |
724 |
} |
|
725 |
||
726 |
rec = cur.primeUpdate(); |
|
727 |
Q_ASSERT(rec); |
|
728 |
rec->setValue(0, 1); |
|
729 |
rec->setNull(1); |
|
730 |
rec->setNull(2); |
|
731 |
// Sqlite returns 2, don't ask why. |
|
732 |
QVERIFY(cur.update() != 0); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
733 |
QString expect = "update " + qTableName("qtestPK", __FILE__) + |
0 | 734 |
" set "+db.driver()->escapeIdentifier("id", QSqlDriver::FieldName)+" = 1 , " |
735 |
+db.driver()->escapeIdentifier("name", QSqlDriver::FieldName)+" = NULL , " |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
736 |
+db.driver()->escapeIdentifier("num", QSqlDriver::FieldName)+" = NULL where " + qTableName("qtestPK", __FILE__) + ".id" |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
737 |
" IS NULL and " + qTableName("qtestPK", __FILE__) + ".name IS NULL and " + |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
738 |
qTableName("qtestPK", __FILE__) + ".num IS NULL"; |
0 | 739 |
if (!db.driver()->hasFeature(QSqlDriver::PreparedQueries)) { |
740 |
if (!db.driverName().startsWith("QSQLITE")) { |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
741 |
QCOMPARE(cur.lastQuery(), expect); |
0 | 742 |
} |
743 |
} |
|
744 |
QVERIFY(cur.select(cur.index(QString("id")))); |
|
745 |
QVERIFY(cur.next()); |
|
746 |
QCOMPARE(cur.value("id").toInt(), 1); |
|
747 |
QVERIFY(cur.isNull("name")); |
|
748 |
QVERIFY(cur.isNull("num")); |
|
749 |
} |
|
750 |
||
751 |
// For task 117996: Q3SqlCursor::insert() should not fail even if field names |
|
752 |
// contain white spaces. |
|
753 |
void tst_Q3SqlCursor::insertFieldNameContainsWS() { |
|
754 |
||
755 |
QFETCH( QString, dbName ); |
|
756 |
QSqlDatabase db = QSqlDatabase::database( dbName ); |
|
757 |
CHECK_DATABASE( db ); |
|
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
758 |
QSet<int> validReturns(QSet<int>() << -1 << 1); |
0 | 759 |
|
760 |
// The bugfix (and this test) depends on QSqlDriver::escapeIdentifier(...) |
|
761 |
// to be implemented, which is currently only the case for the |
|
762 |
// QPSQL, QODBC and QOCI drivers. |
|
763 |
if (!db.driverName().startsWith("QPSQL") && |
|
764 |
!db.driverName().startsWith("QODBC") && |
|
765 |
!db.driverName().startsWith("QOCI")) { |
|
766 |
QSKIP("PSQL, QODBC or QOCI specific test", SkipSingle); |
|
767 |
return; |
|
768 |
} |
|
769 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
770 |
QString tableName = qTableName("qtestws", __FILE__); |
0 | 771 |
|
772 |
QSqlQuery q(db); |
|
773 |
tst_Databases::safeDropTable(db, tableName); |
|
774 |
QString query = "CREATE TABLE %1 (id int, " |
|
775 |
+ db.driver()->escapeIdentifier("first Name", QSqlDriver::FieldName) |
|
776 |
+ " varchar(20), lastName varchar(20))"; |
|
777 |
QVERIFY_SQL(q, exec(query.arg(tableName))); |
|
778 |
||
779 |
Q3SqlCursor cur(tableName, true, db); |
|
780 |
cur.select(); |
|
781 |
||
782 |
QSqlRecord *r = cur.primeInsert(); |
|
783 |
r->setValue("id", 1); |
|
784 |
r->setValue("firsT NaMe", "Kong"); |
|
785 |
r->setValue("lastNaMe", "Harald"); |
|
786 |
||
7
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
787 |
QVERIFY(validReturns.contains(cur.insert())); |
0 | 788 |
|
789 |
cur.select(); |
|
790 |
cur.next(); |
|
791 |
||
792 |
QVERIFY(cur.value(0) == 1); |
|
793 |
QCOMPARE(cur.value(1).toString(), QString("Kong")); |
|
794 |
QCOMPARE(cur.value(2).toString(), QString("Harald")); |
|
795 |
||
796 |
tst_Databases::safeDropTable(db, tableName); |
|
797 |
||
798 |
} |
|
799 |
||
800 |
QTEST_MAIN(tst_Q3SqlCursor) |
|
801 |
#include "tst_q3sqlcursor.moc" |