| 1 |
/**************************************************************************** |
| 2 |
** |
| 3 |
** Copyright (C) 2011 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 |
** GNU Lesser General Public License Usage |
| 11 |
** This file may be used under the terms of the GNU Lesser General Public |
| 12 |
** License version 2.1 as published by the Free Software Foundation and |
| 13 |
** appearing in the file LICENSE.LGPL included in the packaging of this |
| 14 |
** file. Please review the following information to ensure the GNU Lesser |
| 15 |
** General Public License version 2.1 requirements will be met: |
| 16 |
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
| 17 |
** |
| 18 |
** In addition, as a special exception, Nokia gives you certain additional |
| 19 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
| 20 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
| 21 |
** |
| 22 |
** GNU General Public License Usage |
| 23 |
** Alternatively, this file may be used under the terms of the GNU General |
| 24 |
** Public License version 3.0 as published by the Free Software Foundation |
| 25 |
** and appearing in the file LICENSE.GPL included in the packaging of this |
| 26 |
** file. Please review the following information to ensure the GNU General |
| 27 |
** Public License version 3.0 requirements will be met: |
| 28 |
** http://www.gnu.org/copyleft/gpl.html. |
| 29 |
** |
| 30 |
** Other Usage |
| 31 |
** Alternatively, this file may be used in accordance with the terms and |
| 32 |
** conditions contained in a signed written agreement between you and Nokia. |
| 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> |
| 47 |
#include <QSet> |
| 48 |
|
| 49 |
#include "../qsqldatabase/tst_databases.h" |
| 50 |
|
| 51 |
const QString qtest(qTableName( "qtest", __FILE__ )); |
| 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()) |
| 125 |
QSKIP( "No database drivers are available in this Qt configuration", SkipAll ); |
| 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() ) |
| 134 |
return; |
| 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 ) ) { |
| 146 |
QVERIFY_SQL(q, exec( "create table " + qtest + " ( id int not null, t_varchar varchar(40) not null," |
| 147 |
"t_char char(40), t_numeric number, primary key (id, t_varchar) )" )); |
| 148 |
} else { |
| 149 |
QVERIFY_SQL(q, exec( "create table " + qtest + " ( id int not null, t_varchar varchar(40) not null," |
| 150 |
"t_char char(40), t_numeric numeric(6, 3), primary key (id, t_varchar) )" )); |
| 151 |
} |
| 152 |
|
| 153 |
if ( tst_Databases::isSqlServer( db ) ) { |
| 154 |
//workaround for SQL SERVER since he can store unicode only in nvarchar fields |
| 155 |
QVERIFY_SQL(q, exec("create table " + qTableName("qtest_unicode", __FILE__) + " (id int not null, " |
| 156 |
"t_varchar nvarchar(80) not null, t_char nchar(80) )" )); |
| 157 |
} else { |
| 158 |
QVERIFY_SQL(q, exec("create table " + qTableName("qtest_unicode", __FILE__) + " (id int not null, " |
| 159 |
"t_varchar varchar(100) not null," "t_char char(100))" )); |
| 160 |
} |
| 161 |
|
| 162 |
if (tst_Databases::isMSAccess(db)) { |
| 163 |
QVERIFY_SQL(q, exec("create table " + qTableName("qtest_precision", __FILE__) + " (col1 number)")); |
| 164 |
} else { |
| 165 |
QVERIFY_SQL(q, exec("create table " + qTableName("qtest_precision", __FILE__) + " (col1 numeric(15, 14))")); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
void tst_Q3SqlCursor::dropTestTables( QSqlDatabase db ) |
| 170 |
{ |
| 171 |
if ( !db.isValid() ) |
| 172 |
return; |
| 173 |
QStringList tableNames; |
| 174 |
tableNames << qtest |
| 175 |
<< qTableName( "qtest_unicode", __FILE__ ) |
| 176 |
<< qTableName( "qtest_precision", __FILE__ ) |
| 177 |
<< qTableName( "qtest_ovchar", __FILE__ ) |
| 178 |
<< qTableName( "qtest_onvchar", __FILE__ ) |
| 179 |
<< qTableName( "qtestPK", __FILE__ ); |
| 180 |
tst_Databases::safeDropTables( db, tableNames ); |
| 181 |
} |
| 182 |
|
| 183 |
void tst_Q3SqlCursor::populateTestTables( QSqlDatabase db ) |
| 184 |
{ |
| 185 |
if (!db.isValid()) |
| 186 |
return; |
| 187 |
QSqlQuery q( db ); |
| 188 |
|
| 189 |
q.exec( "delete from " + qtest ); //not fatal |
| 190 |
QVERIFY_SQL(q, prepare("insert into " + qtest + " (id, t_varchar, t_char, t_numeric) values (?, ?, ?, ?)")); |
| 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 ) { |
| 203 |
QSqlDatabase db = QSqlDatabase::database( (*it) ); |
| 204 |
CHECK_DATABASE( db ); |
| 205 |
|
| 206 |
dropTestTables( db ); //in case of leftovers |
| 207 |
createTestTables( db ); |
| 208 |
populateTestTables( db ); |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
void tst_Q3SqlCursor::cleanupTestCase() |
| 213 |
{ |
| 214 |
for ( QStringList::ConstIterator it = dbs.dbNames.begin(); it != dbs.dbNames.end(); ++it ) { |
| 215 |
QSqlDatabase db = QSqlDatabase::database( (*it) ); |
| 216 |
CHECK_DATABASE( db ); |
| 217 |
dropTestTables( db ); |
| 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() ) { |
| 233 |
//since Oracle ODBC totally craps out on error, we init again |
| 234 |
db.close(); |
| 235 |
db.open(); |
| 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 |
{ |
| 247 |
Q3SqlCursor cur( qtest, true, db ); |
| 248 |
QVERIFY_SQL(cur, select( cur.index( QString("id") ) )); |
| 249 |
cur2 = Q3SqlCursor( cur ); |
| 250 |
// let "cur" run out of scope... |
| 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() ) { |
| 259 |
QVERIFY( cur2.value("id").toInt() == i ); |
| 260 |
i++; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
void tst_Q3SqlCursor::value() |
| 265 |
{ |
| 266 |
QFETCH( QString, dbName ); |
| 267 |
QSqlDatabase db = QSqlDatabase::database( dbName ); |
| 268 |
CHECK_DATABASE( db ); |
| 269 |
|
| 270 |
Q3SqlCursor cur( qtest, true, db ); |
| 271 |
QVERIFY_SQL(cur, select( cur.index( QString("id") ) )); |
| 272 |
int i = 0; |
| 273 |
while ( cur.next() ) { |
| 274 |
QCOMPARE(cur.value("id").toInt(), i); |
| 275 |
i++; |
| 276 |
} |
| 277 |
} |
| 278 |
|
| 279 |
void tst_Q3SqlCursor::primaryIndex() |
| 280 |
{ |
| 281 |
QFETCH( QString, dbName ); |
| 282 |
QSqlDatabase db = QSqlDatabase::database( dbName ); |
| 283 |
CHECK_DATABASE( db ); |
| 284 |
|
| 285 |
Q3SqlCursor cur( qtest, true, db ); |
| 286 |
QSqlIndex index = cur.primaryIndex(); |
| 287 |
if ( tst_Databases::isMSAccess( db ) ) { |
| 288 |
QCOMPARE( index.fieldName(1).upper(), QString( "ID" ) ); |
| 289 |
QCOMPARE( index.fieldName(0).upper(), QString( "T_VARCHAR" ) ); |
| 290 |
} else { |
| 291 |
QCOMPARE( index.fieldName(0).upper(), QString( "ID" ) ); |
| 292 |
QCOMPARE( index.fieldName(1).upper(), QString( "T_VARCHAR" ) ); |
| 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 |
|
| 304 |
Q3SqlCursor cur( qtest, true, db ); |
| 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 ) && |
| 311 |
( irec->field( 0 ).type() != QVariant::String ) && |
| 312 |
( irec->field( 0 ).type() != QVariant::Double ) ) { |
| 313 |
QFAIL( QString( "Wrong datatype %1 for field 'ID'" |
| 314 |
" (expected Int or String)" ).arg( QVariant::typeToName( irec->field( 0 ).type() ) ) ); |
| 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 |
|
| 330 |
QSet<int> validReturns(QSet<int>() << -1 << 1); |
| 331 |
|
| 332 |
QVERIFY( validReturns.contains(cur.insert()) ); |
| 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 ); |
| 343 |
QSet<int> validReturns(QSet<int>() << -1 << 1); |
| 344 |
|
| 345 |
Q3SqlCursor cur( qtest, true, db ); |
| 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 ) { |
| 361 |
QSqlRecord* irec = cur.primeInsert(); |
| 362 |
QVERIFY( irec != 0 ); |
| 363 |
irec->setValue( "id", i ); |
| 364 |
irec->setValue( "t_varchar", (*it) ); |
| 365 |
irec->setValue( "t_char", (*it) ); |
| 366 |
irec->setValue( "t_numeric", (double)i ); |
| 367 |
++i; |
| 368 |
QVERIFY( validReturns.contains(cur.insert()) ); |
| 369 |
} |
| 370 |
|
| 371 |
QVERIFY( cur.select( "id >= 800 and id < 900" ) ); |
| 372 |
|
| 373 |
int i2 = 800; |
| 374 |
while( cur.next() ) { |
| 375 |
QCOMPARE( cur.value( "id" ).toInt(), i2 ); |
| 376 |
QCOMPARE( cur.value( "t_varchar" ).toString().stripWhiteSpace(), strings.at( i2 - 800 ) ); |
| 377 |
QCOMPARE( cur.value( "t_char" ).toString().stripWhiteSpace(), strings.at( i2 - 800 ) ); |
| 378 |
QCOMPARE( cur.value( "t_numeric" ).toDouble(), (double)i2 ); |
| 379 |
++i2; |
| 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 ); |
| 391 |
QSet<int> validReturns(QSet<int>() << -1 << 1); |
| 392 |
|
| 393 |
QSqlQuery q( db ); |
| 394 |
q.exec( "delete from " + qtest ); |
| 395 |
|
| 396 |
Q3SqlCursor cur( qtest, true, db ); |
| 397 |
|
| 398 |
int i = 0; |
| 399 |
for ( ; i < 100; ++i ) { |
| 400 |
QSqlRecord* irec = cur.primeInsert(); |
| 401 |
Q_ASSERT( irec ); |
| 402 |
irec->setValue( "id", i ); |
| 403 |
irec->setValue( "t_varchar", "blah" ); |
| 404 |
irec->setValue( "t_char", "blah" ); |
| 405 |
irec->setValue( "t_numeric", 1.1 ); |
| 406 |
if ( db.driverName().startsWith( "QSQLITE" ) ) { |
| 407 |
QVERIFY( cur.insert( true ) ); |
| 408 |
} else { |
| 409 |
QVERIFY( validReturns.contains(cur.insert( true )) ); |
| 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 ); |
| 420 |
if ( db.driverName().startsWith( "QSQLITE" ) ) { |
| 421 |
QVERIFY( cur.insert( false ) ); |
| 422 |
} else { |
| 423 |
QVERIFY( validReturns.contains(cur.insert( false )) ); |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
i = 0; |
| 428 |
QVERIFY_SQL(q, exec( "select * from " + qtest + " order by id" )); |
| 429 |
while ( q.next() ) { |
| 430 |
QCOMPARE( q.value( 0 ).toInt(), i ); |
| 431 |
i++; |
| 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 ) { |
| 443 |
res += "0x" + QString::number( str[ i ].unicode(), 16 ) + ' '; |
| 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) |
| 455 |
QSKIP("Need Oracle >= 9", SkipSingle); |
| 456 |
|
| 457 |
/****** CHARSET TEST ******/ |
| 458 |
|
| 459 |
QSqlQuery q( db ); |
| 460 |
QVERIFY_SQL(q, exec( "create table " + qTableName( "qtest_ovchar", __FILE__ ) + " ( id int primary key, t_char varchar(40) )" )); |
| 461 |
|
| 462 |
static const QString val1( "blah1" ); |
| 463 |
|
| 464 |
Q3SqlCursor cur ( qTableName( "qtest_ovchar", __FILE__ ), true, db ); |
| 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 ) |
| 473 |
qDebug( QString( "Wrong value for t_char: expected '%1', got '%2'" ).arg( val1 ).arg( |
| 474 |
cur.value( "t_char" ).toString() ) ); |
| 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 |
|
| 494 |
QVERIFY_SQL(q, exec( "create table " + qTableName( "qtest_onvchar", __FILE__ ) + " ( id int primary key, t_nchar nvarchar2(40) )" )); |
| 495 |
|
| 496 |
Q3SqlCursor cur2 ( qTableName( "qtest_onvchar", __FILE__ ), true, db ); |
| 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 ) ) { |
| 531 |
QSKIP( "DBMS not Unicode capable", SkipSingle ); |
| 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 |
|
| 537 |
Q3SqlCursor cur( qTableName( "qtest_unicode", __FILE__ ), true, db ); |
| 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 |
|
| 574 |
Q3SqlCursor cur( qTableName( "qtest_precision", __FILE__ ), true, db ); |
| 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 |
|
| 597 |
Q3SqlCursor cur( qtest, true, db ); |
| 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 |
|
| 627 |
Q3SqlCursor cur( qtest, true, db ); |
| 628 |
QVERIFY_SQL(cur, select()); |
| 629 |
QVERIFY( cur.next() ); |
| 630 |
QVERIFY( cur.next() ); |
| 631 |
|
| 632 |
Q3SqlCursor cur2( qtest, true, db ); |
| 633 |
QVERIFY_SQL(cur2, select( "id = 1" )); |
| 634 |
QVERIFY( cur2.next() ); |
| 635 |
QCOMPARE( cur2.value( 0 ).toInt(), 1 ); |
| 636 |
|
| 637 |
Q3SqlCursor cur3( qtest, true, db ); |
| 638 |
QVERIFY_SQL(cur3, select( cur3.primaryIndex( false ) )); |
| 639 |
QVERIFY( cur3.next() ); |
| 640 |
QVERIFY( cur3.next() ); |
| 641 |
QCOMPARE( cur3.value( 0 ).toInt(), 1 ); |
| 642 |
|
| 643 |
Q3SqlCursor cur4( qtest, true, db ); |
| 644 |
QSqlIndex idx = cur4.primaryIndex( false ); |
| 645 |
QCOMPARE( (int)idx.count(), 2 ); |
| 646 |
if ( tst_Databases::isMSAccess( db ) ) { |
| 647 |
QCOMPARE( idx.field( 1 ).name().upper(), QString("ID") ); |
| 648 |
QCOMPARE( idx.field( 0 ).name().upper(), QString("T_VARCHAR") ); |
| 649 |
} else { |
| 650 |
QCOMPARE( idx.field( 0 ).name().upper(), QString("ID") ); |
| 651 |
QCOMPARE( idx.field( 1 ).name().upper(), QString("T_VARCHAR") ); |
| 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 |
|
| 675 |
Q3SqlCursor c2( qtest, true, db ); |
| 676 |
QCOMPARE( c2.name(), qtest ); |
| 677 |
QCOMPARE( c2.fieldName( 0 ).lower(), QString( "id" ) ); |
| 678 |
|
| 679 |
Q3SqlCursor c( QString(), true, db ); |
| 680 |
c.setName( qtest ); |
| 681 |
QCOMPARE( c.name(), qtest ); |
| 682 |
QCOMPARE( c.fieldName( 0 ).lower(), QString( "id" ) ); |
| 683 |
|
| 684 |
c.setName( qTableName( "qtest_precision", __FILE__ ) ); |
| 685 |
QCOMPARE( c.name(), qTableName( "qtest_precision", __FILE__ ) ); |
| 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 ); |
| 695 |
QSet<int> validReturns(QSet<int>() << -1 << 1); |
| 696 |
|
| 697 |
QSqlQuery q(db); |
| 698 |
QVERIFY_SQL(q, exec("create table " + qTableName( "qtestPK", __FILE__ ) + " (id int, name varchar(20), num numeric)")); |
| 699 |
|
| 700 |
Q3SqlCursor cur(qTableName("qtestPK", __FILE__), true, db); |
| 701 |
QSqlRecord* rec = cur.primeInsert(); |
| 702 |
Q_ASSERT(rec); |
| 703 |
rec->setNull(0); |
| 704 |
rec->setNull(1); |
| 705 |
rec->setNull(2); |
| 706 |
QVERIFY(validReturns.contains(cur.insert())); |
| 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")) { |
| 714 |
QString query = QString::fromLatin1("insert into " + qTableName("qtestPK", __FILE__) + |
| 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 { |
| 721 |
QCOMPARE(cur.lastQuery(), QString::fromLatin1("insert into " + qTableName("qtestPK", __FILE__) + |
| 722 |
" (\"id\",\"name\",\"num\") values (NULL,NULL,NULL)")); |
| 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); |
| 733 |
QString expect = "update " + qTableName("qtestPK", __FILE__) + |
| 734 |
" set "+db.driver()->escapeIdentifier("id", QSqlDriver::FieldName)+" = 1 , " |
| 735 |
+db.driver()->escapeIdentifier("name", QSqlDriver::FieldName)+" = NULL , " |
| 736 |
+db.driver()->escapeIdentifier("num", QSqlDriver::FieldName)+" = NULL where " + qTableName("qtestPK", __FILE__) + ".id" |
| 737 |
" IS NULL and " + qTableName("qtestPK", __FILE__) + ".name IS NULL and " + |
| 738 |
qTableName("qtestPK", __FILE__) + ".num IS NULL"; |
| 739 |
if (!db.driver()->hasFeature(QSqlDriver::PreparedQueries)) { |
| 740 |
if (!db.driverName().startsWith("QSQLITE")) { |
| 741 |
QCOMPARE(cur.lastQuery(), expect); |
| 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 ); |
| 758 |
QSet<int> validReturns(QSet<int>() << -1 << 1); |
| 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 |
|
| 770 |
QString tableName = qTableName("qtestws", __FILE__); |
| 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 |
|
| 787 |
QVERIFY(validReturns.contains(cur.insert())); |
| 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" |