branch | RCL_3 |
changeset 7 | 3f74d0d4af4c |
parent 4 | 3b1da2848fc7 |
6:dee5afe5301f | 7:3f74d0d4af4c |
---|---|
200 void sqlite_bindAndFetchUInt(); |
200 void sqlite_bindAndFetchUInt(); |
201 |
201 |
202 void sqlStatementUseIsNull_189093_data() { generic_data(); } |
202 void sqlStatementUseIsNull_189093_data() { generic_data(); } |
203 void sqlStatementUseIsNull_189093(); |
203 void sqlStatementUseIsNull_189093(); |
204 |
204 |
205 void sqlite_enable_cache_mode_data() { generic_data("QSQLITE"); } |
|
206 void sqlite_enable_cache_mode(); |
|
205 |
207 |
206 private: |
208 private: |
207 void createTestTables(QSqlDatabase db); |
209 void createTestTables(QSqlDatabase db); |
208 void dropTestTables(QSqlDatabase db); |
210 void dropTestTables(QSqlDatabase db); |
209 void populateTestTables(QSqlDatabase db); |
211 void populateTestTables(QSqlDatabase db); |
255 |
257 |
256 // creates a table out of the FieldDefs and returns the number of fields |
258 // creates a table out of the FieldDefs and returns the number of fields |
257 // excluding the primary key field |
259 // excluding the primary key field |
258 static int createFieldTable(const FieldDef fieldDefs[], QSqlDatabase db) |
260 static int createFieldTable(const FieldDef fieldDefs[], QSqlDatabase db) |
259 { |
261 { |
260 tst_Databases::safeDropTable(db, qTableName("qtestfields")); |
262 tst_Databases::safeDropTable(db, qTableName("qtestfields", __FILE__)); |
261 QSqlQuery q(db); |
263 QSqlQuery q(db); |
262 // construct a create table statement consisting of all fieldtypes |
264 // construct a create table statement consisting of all fieldtypes |
263 QString qs = "create table " + qTableName("qtestfields"); |
265 QString qs = "create table " + qTableName("qtestfields", __FILE__); |
264 QString autoName = tst_Databases::autoFieldName(db); |
266 QString autoName = tst_Databases::autoFieldName(db); |
265 if (tst_Databases::isMSAccess(db)) |
267 if (tst_Databases::isMSAccess(db)) |
266 qs.append(" (id int not null"); |
268 qs.append(" (id int not null"); |
267 else if (tst_Databases::isPostgreSQL(db)) |
269 else if (tst_Databases::isPostgreSQL(db)) |
268 qs.append(" (id serial not null"); |
270 qs.append(" (id serial not null"); |
312 } else if(tst_Databases::isPostgreSQL(db)) |
314 } else if(tst_Databases::isPostgreSQL(db)) |
313 QVERIFY_SQL( q, exec("set client_min_messages='warning'")); |
315 QVERIFY_SQL( q, exec("set client_min_messages='warning'")); |
314 |
316 |
315 // please never ever change this table; otherwise fix all tests ;) |
317 // please never ever change this table; otherwise fix all tests ;) |
316 if (tst_Databases::isMSAccess(db)) { |
318 if (tst_Databases::isMSAccess(db)) { |
317 QVERIFY_SQL(q, exec("create table " + qTableName("qtest") + |
319 QVERIFY_SQL(q, exec("create table " + qTableName("qtest", __FILE__) + |
318 " (id int not null, t_varchar varchar(40) not null, t_char char(40), " |
320 " (id int not null, t_varchar varchar(40) not null, t_char char(40), " |
319 "t_numeric number, primary key (id, t_varchar))")); |
321 "t_numeric number, primary key (id, t_varchar))")); |
320 } else { |
322 } else { |
321 QVERIFY_SQL(q, exec("create table " + qTableName("qtest") + |
323 QVERIFY_SQL(q, exec("create table " + qTableName("qtest", __FILE__) + |
322 " (id integer not null, t_varchar varchar(40) not null, " |
324 " (id integer not null, t_varchar varchar(40) not null, " |
323 "t_char char(40), t_numeric numeric(6, 3), primary key (id, t_varchar))")); |
325 "t_char char(40), t_numeric numeric(6, 3), primary key (id, t_varchar))")); |
324 } |
326 } |
325 |
327 |
326 if (testWhiteSpaceNames(db.driverName())) { |
328 if (testWhiteSpaceNames(db.driverName())) { |
327 QString qry = "create table " |
329 QString qry = "create table " |
328 + db.driver()->escapeIdentifier(qTableName("qtest") + " test", QSqlDriver::TableName) |
330 + db.driver()->escapeIdentifier(qTableName("qtest", __FILE__) + " test", QSqlDriver::TableName) |
329 + '(' |
331 + '(' |
330 + db.driver()->escapeIdentifier(QLatin1String("test test"), QSqlDriver::FieldName) |
332 + db.driver()->escapeIdentifier(QLatin1String("test test"), QSqlDriver::FieldName) |
331 + " int not null primary key)"; |
333 + " int not null primary key)"; |
332 QVERIFY_SQL(q, exec(qry)); |
334 QVERIFY_SQL(q, exec(qry)); |
333 } |
335 } |
342 QSqlQuery q(db); |
344 QSqlQuery q(db); |
343 QVERIFY_SQL( q, exec("set client_min_messages='warning'")); |
345 QVERIFY_SQL( q, exec("set client_min_messages='warning'")); |
344 } |
346 } |
345 |
347 |
346 // drop the view first, otherwise we'll get dependency problems |
348 // drop the view first, otherwise we'll get dependency problems |
347 tst_Databases::safeDropViews(db, QStringList() << qTableName("qtest_view") << qTableName("qtest_view2")); |
349 tst_Databases::safeDropViews(db, QStringList() << qTableName("qtest_view", __FILE__) << qTableName("qtest_view2", __FILE__)); |
348 |
350 |
349 QStringList tableNames; |
351 QStringList tableNames; |
350 tableNames << qTableName("qtest") |
352 tableNames << qTableName("qtest", __FILE__) |
351 << qTableName("qtestfields") |
353 << qTableName("qtestfields", __FILE__) |
352 << qTableName("qtestalter") |
354 << qTableName("qtestalter", __FILE__) |
353 << qTableName("qtest_temp") |
355 << qTableName("qtest_temp", __FILE__) |
354 << qTableName("qtest_bigint") |
356 << qTableName("qtest_bigint", __FILE__) |
355 << qTableName("qtest_xmltype") |
357 << qTableName("qtest_xmltype", __FILE__) |
356 << qTableName("latin1table") |
358 << qTableName("latin1table", __FILE__) |
357 << qTableName("qtest_sqlguid") |
359 << qTableName("qtest_sqlguid", __FILE__) |
358 << qTableName("batable") |
360 << qTableName("batable", __FILE__) |
359 << qTableName("qtest_prec") |
361 << qTableName("qtest_prec", __FILE__) |
360 << qTableName("uint") |
362 << qTableName("uint", __FILE__) |
361 << qTableName("strings") |
363 << qTableName("strings", __FILE__) |
362 << qTableName("numericfields") |
364 << qTableName("numericfields", __FILE__) |
363 << qTableName("qtest_ibaseblobs") |
365 << qTableName("qtest_ibaseblobs", __FILE__) |
364 << qTableName("qtestBindBool") |
366 << qTableName("qtestBindBool", __FILE__) |
365 << qTableName("testqGetString") |
367 << qTableName("testqGetString", __FILE__) |
366 << qTableName("qtest_sqlguid") |
368 << qTableName("qtest_sqlguid", __FILE__) |
367 << qTableName("uint_table") |
369 << qTableName("uint_table", __FILE__) |
368 << qTableName("uint_test") |
370 << qTableName("uint_test", __FILE__) |
369 << qTableName("bug_249059"); |
371 << qTableName("bug_249059", __FILE__); |
370 |
372 |
371 QSqlQuery q(0, db); |
373 QSqlQuery q(0, db); |
372 if (db.driverName().startsWith("QPSQL")) { |
374 if (db.driverName().startsWith("QPSQL")) { |
373 q.exec("drop schema " + qTableName("qtestschema") + " cascade"); |
375 q.exec("drop schema " + qTableName("qtestschema", __FILE__) + " cascade"); |
374 q.exec("drop schema " + qTableName("qtestScHeMa") + " cascade"); |
376 q.exec("drop schema " + qTableName("qtestScHeMa", __FILE__) + " cascade"); |
375 } |
377 } |
376 |
378 |
377 if (testWhiteSpaceNames(db.driverName())) |
379 if (testWhiteSpaceNames(db.driverName())) |
378 tableNames << db.driver()->escapeIdentifier(qTableName("qtest") + " test", QSqlDriver::TableName); |
380 tableNames << db.driver()->escapeIdentifier(qTableName("qtest", __FILE__) + " test", QSqlDriver::TableName); |
379 |
381 |
380 tst_Databases::safeDropTables(db, tableNames); |
382 tst_Databases::safeDropTables(db, tableNames); |
381 |
383 |
382 if (db.driverName().startsWith("QOCI")) { |
384 if (db.driverName().startsWith("QOCI")) { |
383 q.exec("drop user "+qTableName("CREATOR")+" cascade"); |
385 q.exec("drop user "+qTableName("CREATOR", __FILE__)+" cascade"); |
384 q.exec("drop user "+qTableName("APPUSER")+" cascade"); |
386 q.exec("drop user "+qTableName("APPUSER", __FILE__)+" cascade"); |
385 q.exec("DROP TABLE system."+qTableName("mypassword")); |
387 q.exec("DROP TABLE system."+qTableName("mypassword", __FILE__)); |
386 |
388 |
387 } |
389 } |
388 } |
390 } |
389 |
391 |
390 void tst_QSqlDatabase::populateTestTables(QSqlDatabase db) |
392 void tst_QSqlDatabase::populateTestTables(QSqlDatabase db) |
391 { |
393 { |
392 if (!db.isValid()) |
394 if (!db.isValid()) |
393 return; |
395 return; |
394 QSqlQuery q(db); |
396 QSqlQuery q(db); |
395 |
397 const QString qtest(qTableName("qtest", __FILE__)); |
396 q.exec("delete from " + qTableName("qtest")); //non-fatal |
398 |
397 QVERIFY_SQL(q, exec("insert into " + qTableName("qtest") + " (id, t_varchar, t_char, t_numeric) values (0, 'VarChar0', 'Char0', 1.1)")); |
399 q.exec("delete from " + qtest); //non-fatal |
398 QVERIFY_SQL(q, exec("insert into " + qTableName("qtest") + " (id, t_varchar, t_char, t_numeric) values (1, 'VarChar1', 'Char1', 2.2)")); |
400 QVERIFY_SQL(q, exec("insert into " + qtest + " (id, t_varchar, t_char, t_numeric) values (0, 'VarChar0', 'Char0', 1.1)")); |
399 QVERIFY_SQL(q, exec("insert into " + qTableName("qtest") + " (id, t_varchar, t_char, t_numeric) values (2, 'VarChar2', 'Char2', 3.3)")); |
401 QVERIFY_SQL(q, exec("insert into " + qtest + " (id, t_varchar, t_char, t_numeric) values (1, 'VarChar1', 'Char1', 2.2)")); |
400 QVERIFY_SQL(q, exec("insert into " + qTableName("qtest") + " (id, t_varchar, t_char, t_numeric) values (3, 'VarChar3', 'Char3', 4.4)")); |
402 QVERIFY_SQL(q, exec("insert into " + qtest + " (id, t_varchar, t_char, t_numeric) values (2, 'VarChar2', 'Char2', 3.3)")); |
401 QVERIFY_SQL(q, exec("insert into " + qTableName("qtest") + " (id, t_varchar, t_char, t_numeric) values (4, 'VarChar4', NULL, NULL)")); |
403 QVERIFY_SQL(q, exec("insert into " + qtest + " (id, t_varchar, t_char, t_numeric) values (3, 'VarChar3', 'Char3', 4.4)")); |
404 QVERIFY_SQL(q, exec("insert into " + qtest + " (id, t_varchar, t_char, t_numeric) values (4, 'VarChar4', NULL, NULL)")); |
|
402 } |
405 } |
403 |
406 |
404 void tst_QSqlDatabase::initTestCase() |
407 void tst_QSqlDatabase::initTestCase() |
405 { |
408 { |
406 dbs.open(); |
409 dbs.open(); |
492 QSqlRecord rec = db.record(q); |
495 QSqlRecord rec = db.record(q); |
493 QVERIFY(rec.isEmpty()); |
496 QVERIFY(rec.isEmpty()); |
494 Q3SqlRecordInfo rInf = db.recordInfo(q); |
497 Q3SqlRecordInfo rInf = db.recordInfo(q); |
495 QVERIFY(rInf.isEmpty()); |
498 QVERIFY(rInf.isEmpty()); |
496 |
499 |
497 QVERIFY_SQL(q, exec("create table " + qTableName("qtest_temp") + " (id int)")); |
500 QVERIFY_SQL(q, exec("create table " + qTableName("qtest_temp", __FILE__) + " (id int)")); |
498 |
501 |
499 // query without result set should return empty record |
502 // query without result set should return empty record |
500 rec = db.record(q); |
503 rec = db.record(q); |
501 QVERIFY(rec.isEmpty()); |
504 QVERIFY(rec.isEmpty()); |
502 rInf = db.recordInfo(q); |
505 rInf = db.recordInfo(q); |
508 { |
511 { |
509 QFETCH(QString, dbName); |
512 QFETCH(QString, dbName); |
510 QSqlDatabase db = QSqlDatabase::database(dbName); |
513 QSqlDatabase db = QSqlDatabase::database(dbName); |
511 CHECK_DATABASE(db); |
514 CHECK_DATABASE(db); |
512 |
515 |
516 const QString qtest(qTableName("qtest", __FILE__)), qtest_view(qTableName("qtest_view", __FILE__)), temp_tab(qTableName("test_tab", __FILE__)); |
|
517 |
|
513 bool views = true; |
518 bool views = true; |
514 bool tempTables = false; |
519 bool tempTables = false; |
515 |
520 |
516 QSqlQuery q(db); |
521 QSqlQuery q(db); |
517 if ( db.driverName().startsWith( "QMYSQL" ) && tst_Databases::getMySqlVersion( db ).section( QChar('.'), 0, 0 ).toInt()<5 ) |
522 if ( db.driverName().startsWith( "QMYSQL" ) && tst_Databases::getMySqlVersion( db ).section( QChar('.'), 0, 0 ).toInt()<5 ) |
518 QSKIP( "Test requires MySQL >= 5.0", SkipSingle ); |
523 QSKIP( "Test requires MySQL >= 5.0", SkipSingle ); |
519 |
524 |
520 |
525 |
521 if (!q.exec("CREATE VIEW " + qTableName("qtest_view") + " as select * from " + qTableName("qtest"))) { |
526 if (!q.exec("CREATE VIEW " + qtest_view + " as select * from " + qtest)) { |
522 qDebug(QString("DBMS '%1' cannot handle VIEWs: %2").arg( |
527 qDebug(QString("DBMS '%1' cannot handle VIEWs: %2").arg( |
523 tst_Databases::dbToString(db)).arg(QString(tst_Databases::printError(q.lastError()))).toLatin1()); |
528 tst_Databases::dbToString(db)).arg(QString(tst_Databases::printError(q.lastError()))).toLatin1()); |
524 views = false; |
529 views = false; |
525 } |
530 } |
526 |
531 |
527 if (db.driverName().startsWith("QSQLITE3")) { |
532 if (db.driverName().startsWith("QSQLITE3")) { |
528 QVERIFY_SQL(q, exec("CREATE TEMPORARY TABLE " + qTableName("temp_tab") + " (id int)")); |
533 QVERIFY_SQL(q, exec("CREATE TEMPORARY TABLE " + temp_tab + " (id int)")); |
529 tempTables = true; |
534 tempTables = true; |
530 } |
535 } |
531 |
536 |
532 QStringList tables = db.tables(QSql::Tables); |
537 QStringList tables = db.tables(QSql::Tables); |
533 QVERIFY(tables.contains(qTableName("qtest"), Qt::CaseInsensitive)); |
538 QVERIFY(tables.contains(qtest, Qt::CaseInsensitive)); |
534 QVERIFY(!tables.contains("sql_features", Qt::CaseInsensitive)); //check for postgres 7.4 internal tables |
539 QVERIFY(!tables.contains("sql_features", Qt::CaseInsensitive)); //check for postgres 7.4 internal tables |
535 if (views) { |
540 if (views) { |
536 QVERIFY(!tables.contains(qTableName("qtest_view"), Qt::CaseInsensitive)); |
541 QVERIFY(!tables.contains(qtest_view, Qt::CaseInsensitive)); |
537 } |
542 } |
538 if (tempTables) |
543 if (tempTables) |
539 QVERIFY(tables.contains(qTableName("temp_tab"), Qt::CaseInsensitive)); |
544 QVERIFY(tables.contains(temp_tab, Qt::CaseInsensitive)); |
540 |
545 |
541 tables = db.tables(QSql::Views); |
546 tables = db.tables(QSql::Views); |
542 if (views) { |
547 if (views) { |
543 if(!tables.contains(qTableName("qtest_view"), Qt::CaseInsensitive)) |
548 if(!tables.contains(qtest_view, Qt::CaseInsensitive)) |
544 qDebug() << "failed to find" << qTableName("qtest_view") << "in" << tables; |
549 qDebug() << "failed to find" << qtest_view << "in" << tables; |
545 QVERIFY(tables.contains(qTableName("qtest_view"), Qt::CaseInsensitive)); |
550 QVERIFY(tables.contains(qtest_view, Qt::CaseInsensitive)); |
546 } |
551 } |
547 if (tempTables) |
552 if (tempTables) |
548 QVERIFY(!tables.contains(qTableName("temp_tab"), Qt::CaseInsensitive)); |
553 QVERIFY(!tables.contains(temp_tab, Qt::CaseInsensitive)); |
549 QVERIFY(!tables.contains(qTableName("qtest"), Qt::CaseInsensitive)); |
554 QVERIFY(!tables.contains(qtest, Qt::CaseInsensitive)); |
550 |
555 |
551 tables = db.tables(QSql::SystemTables); |
556 tables = db.tables(QSql::SystemTables); |
552 QVERIFY(!tables.contains(qTableName("qtest"), Qt::CaseInsensitive)); |
557 QVERIFY(!tables.contains(qtest, Qt::CaseInsensitive)); |
553 QVERIFY(!tables.contains(qTableName("qtest_view"), Qt::CaseInsensitive)); |
558 QVERIFY(!tables.contains(qtest_view, Qt::CaseInsensitive)); |
554 QVERIFY(!tables.contains(qTableName("temp_tab"), Qt::CaseInsensitive)); |
559 QVERIFY(!tables.contains(temp_tab, Qt::CaseInsensitive)); |
555 |
560 |
556 tables = db.tables(QSql::AllTables); |
561 tables = db.tables(QSql::AllTables); |
557 if (views) |
562 if (views) |
558 QVERIFY(tables.contains(qTableName("qtest_view"), Qt::CaseInsensitive)); |
563 QVERIFY(tables.contains(qtest_view, Qt::CaseInsensitive)); |
559 if (tempTables) |
564 if (tempTables) |
560 QVERIFY(tables.contains(qTableName("temp_tab"), Qt::CaseInsensitive)); |
565 QVERIFY(tables.contains(temp_tab, Qt::CaseInsensitive)); |
561 QVERIFY(tables.contains(qTableName("qtest"), Qt::CaseInsensitive)); |
566 QVERIFY(tables.contains(qtest, Qt::CaseInsensitive)); |
562 |
567 |
563 if (db.driverName().startsWith("QPSQL")) { |
568 if (db.driverName().startsWith("QPSQL")) { |
564 QVERIFY(tables.contains(qTableName("qtest") + " test")); |
569 QVERIFY(tables.contains(qtest + " test")); |
565 } |
570 } |
566 } |
571 } |
567 |
572 |
568 void tst_QSqlDatabase::whitespaceInIdentifiers() |
573 void tst_QSqlDatabase::whitespaceInIdentifiers() |
569 { |
574 { |
570 QFETCH(QString, dbName); |
575 QFETCH(QString, dbName); |
571 QSqlDatabase db = QSqlDatabase::database(dbName); |
576 QSqlDatabase db = QSqlDatabase::database(dbName); |
572 CHECK_DATABASE(db); |
577 CHECK_DATABASE(db); |
573 |
578 |
574 if (testWhiteSpaceNames(db.driverName())) { |
579 if (testWhiteSpaceNames(db.driverName())) { |
575 QString tableName = qTableName("qtest") + " test"; |
580 const QString tableName(qTableName("qtest", __FILE__) + " test"); |
576 QVERIFY(db.tables().contains(tableName, Qt::CaseInsensitive)); |
581 QVERIFY(db.tables().contains(tableName, Qt::CaseInsensitive)); |
577 |
582 |
578 QSqlRecord rec = db.record(db.driver()->escapeIdentifier(tableName, QSqlDriver::TableName)); |
583 QSqlRecord rec = db.record(db.driver()->escapeIdentifier(tableName, QSqlDriver::TableName)); |
579 QCOMPARE(rec.count(), 1); |
584 QCOMPARE(rec.count(), 1); |
580 QCOMPARE(rec.fieldName(0), QString("test test")); |
585 QCOMPARE(rec.fieldName(0), QString("test test")); |
598 void tst_QSqlDatabase::alterTable() |
603 void tst_QSqlDatabase::alterTable() |
599 { |
604 { |
600 QFETCH(QString, dbName); |
605 QFETCH(QString, dbName); |
601 QSqlDatabase db = QSqlDatabase::database(dbName); |
606 QSqlDatabase db = QSqlDatabase::database(dbName); |
602 CHECK_DATABASE(db); |
607 CHECK_DATABASE(db); |
603 |
608 const QString qtestalter(qTableName("qtestalter", __FILE__)); |
604 QSqlQuery q(db); |
609 |
605 |
610 QSqlQuery q(db); |
606 QVERIFY_SQL(q, exec("create table " + qTableName("qtestalter") + " (F1 char(20), F2 char(20), F3 char(20))")); |
611 |
607 QSqlRecord rec = db.record(qTableName("qtestalter")); |
612 QVERIFY_SQL(q, exec("create table " + qtestalter + " (F1 char(20), F2 char(20), F3 char(20))")); |
613 QSqlRecord rec = db.record(qtestalter); |
|
608 QCOMPARE((int)rec.count(), 3); |
614 QCOMPARE((int)rec.count(), 3); |
609 #ifdef QT3_SUPPORT |
615 #ifdef QT3_SUPPORT |
610 Q3SqlRecordInfo rinf = db.recordInfo(qTableName("qtestalter")); |
616 Q3SqlRecordInfo rinf = db.recordInfo(qtestalter); |
611 QCOMPARE((int)rinf.count(), 3); |
617 QCOMPARE((int)rinf.count(), 3); |
612 #endif |
618 #endif |
613 |
619 |
614 |
620 |
615 int i; |
621 int i; |
618 #ifdef QT3_SUPPORT |
624 #ifdef QT3_SUPPORT |
619 QCOMPARE(rinf[ i ].name().upper(), QString("F%1").arg(i + 1)); |
625 QCOMPARE(rinf[ i ].name().upper(), QString("F%1").arg(i + 1)); |
620 #endif |
626 #endif |
621 } |
627 } |
622 |
628 |
623 if (!q.exec("alter table " + qTableName("qtestalter") + " drop column F2")) { |
629 if (!q.exec("alter table " + qtestalter + " drop column F2")) { |
624 QSKIP("DBMS doesn't support dropping columns in ALTER TABLE statement", SkipSingle); |
630 QSKIP("DBMS doesn't support dropping columns in ALTER TABLE statement", SkipSingle); |
625 } |
631 } |
626 |
632 |
627 rec = db.record(qTableName("qtestalter")); |
633 rec = db.record(qtestalter); |
628 #ifdef QT3_SUPPORT |
634 #ifdef QT3_SUPPORT |
629 rinf = db.recordInfo(qTableName("qtestalter")); |
635 rinf = db.recordInfo(qtestalter); |
630 #endif |
636 #endif |
631 |
637 |
632 QCOMPARE((int)rec.count(), 2); |
638 QCOMPARE((int)rec.count(), 2); |
633 #ifdef QT3_SUPPORT |
639 #ifdef QT3_SUPPORT |
634 QCOMPARE((int)rinf.count(), 2); |
640 QCOMPARE((int)rinf.count(), 2); |
639 #ifdef QT3_SUPPORT |
645 #ifdef QT3_SUPPORT |
640 QCOMPARE(rinf[ 0 ].name().upper(), QString("F1")); |
646 QCOMPARE(rinf[ 0 ].name().upper(), QString("F1")); |
641 QCOMPARE(rinf[ 1 ].name().upper(), QString("F3")); |
647 QCOMPARE(rinf[ 1 ].name().upper(), QString("F3")); |
642 #endif |
648 #endif |
643 |
649 |
644 q.exec("select * from " + qTableName("qtestalter")); |
650 q.exec("select * from " + qtestalter); |
645 |
651 |
646 #ifdef QT3_SUPPORT |
652 #ifdef QT3_SUPPORT |
647 rec = db.record(q); |
653 rec = db.record(q); |
648 rinf = db.recordInfo(q); |
654 rinf = db.recordInfo(q); |
649 |
655 |
726 { |
732 { |
727 CHECK_DATABASE(db); |
733 CHECK_DATABASE(db); |
728 |
734 |
729 // check whether recordInfo returns the right types |
735 // check whether recordInfo returns the right types |
730 #ifdef QT3_SUPPORT |
736 #ifdef QT3_SUPPORT |
731 Q3SqlRecordInfo inf = db.recordInfo(qTableName("qtestfields")); |
737 Q3SqlRecordInfo inf = db.recordInfo(qTableName("qtestfields", __FILE__)); |
732 QCOMPARE((int)inf.count(), fieldCount+1); |
738 QCOMPARE((int)inf.count(), fieldCount+1); |
733 testRecordInfo(fieldDefs, inf); |
739 testRecordInfo(fieldDefs, inf); |
734 #endif |
740 #endif |
735 |
741 |
736 QSqlRecord rec = db.record(qTableName("qtestfields")); |
742 QSqlRecord rec = db.record(qTableName("qtestfields", __FILE__)); |
737 QCOMPARE((int)rec.count(), fieldCount+1); |
743 QCOMPARE((int)rec.count(), fieldCount+1); |
738 testRecord(fieldDefs, rec, db); |
744 testRecord(fieldDefs, rec, db); |
739 |
745 |
740 QSqlQuery q(db); |
746 QSqlQuery q(db); |
741 QVERIFY_SQL(q, exec("select * from " + qTableName("qtestfields"))); |
747 QVERIFY_SQL(q, exec("select * from " + qTableName("qtestfields", __FILE__))); |
742 |
748 |
743 #ifdef QT3_SUPPORT |
749 #ifdef QT3_SUPPORT |
744 inf = db.recordInfo(q); |
750 inf = db.recordInfo(q); |
745 QCOMPARE((int)inf.count(), fieldCount+1); |
751 QCOMPARE((int)inf.count(), fieldCount+1); |
746 testRecordInfo(fieldDefs, inf); |
752 testRecordInfo(fieldDefs, inf); |
756 { |
762 { |
757 Q_UNUSED(fieldDefs); |
763 Q_UNUSED(fieldDefs); |
758 #ifdef QT3_SUPPORT |
764 #ifdef QT3_SUPPORT |
759 CHECK_DATABASE(db); |
765 CHECK_DATABASE(db); |
760 |
766 |
761 Q3SqlCursor cur(qTableName("qtestfields"), true, db); |
767 Q3SqlCursor cur(qTableName("qtestfields", __FILE__), true, db); |
762 QVERIFY_SQL(cur, select()); |
768 QVERIFY_SQL(cur, select()); |
763 QSqlRecord* rec = cur.primeInsert(); |
769 QSqlRecord* rec = cur.primeInsert(); |
764 Q_ASSERT(rec); |
770 Q_ASSERT(rec); |
765 rec->setValue("id", pkey++); |
771 rec->setValue("id", pkey++); |
766 int i = 0; |
772 int i = 0; |
817 { |
823 { |
818 Q_UNUSED(fieldDefs); |
824 Q_UNUSED(fieldDefs); |
819 #ifdef QT3_SUPPORT |
825 #ifdef QT3_SUPPORT |
820 CHECK_DATABASE(db); |
826 CHECK_DATABASE(db); |
821 |
827 |
822 Q3SqlCursor cur(qTableName("qtestfields"), true, db); |
828 Q3SqlCursor cur(qTableName("qtestfields", __FILE__), true, db); |
823 QVERIFY_SQL(cur, select()); |
829 QVERIFY_SQL(cur, select()); |
824 QSqlRecord* rec = cur.primeInsert(); |
830 QSqlRecord* rec = cur.primeInsert(); |
825 Q_ASSERT(rec); |
831 Q_ASSERT(rec); |
826 rec->setValue("id", pkey++); |
832 rec->setValue("id", pkey++); |
827 int i = 0; |
833 int i = 0; |
951 checkNullValues(fieldDefs, db); |
957 checkNullValues(fieldDefs, db); |
952 for (int i = 0; i < ITERATION_COUNT; ++i) |
958 for (int i = 0; i < ITERATION_COUNT; ++i) |
953 checkValues(fieldDefs, db); |
959 checkValues(fieldDefs, db); |
954 |
960 |
955 // some additional tests |
961 // some additional tests |
956 QSqlRecord rec = db.record(qTableName("qtestfields")); |
962 QSqlRecord rec = db.record(qTableName("qtestfields", __FILE__)); |
957 QCOMPARE(rec.field("T_NUMBER").length(), 10); |
963 QCOMPARE(rec.field("T_NUMBER").length(), 10); |
958 QCOMPARE(rec.field("T_NUMBER").precision(), 5); |
964 QCOMPARE(rec.field("T_NUMBER").precision(), 5); |
959 |
965 |
960 QSqlQuery q(db); |
966 QSqlQuery q(db); |
961 QVERIFY_SQL(q, exec("SELECT * FROM " + qTableName("qtestfields"))); |
967 QVERIFY_SQL(q, exec("SELECT * FROM " + qTableName("qtestfields", __FILE__))); |
962 rec = q.record(); |
968 rec = q.record(); |
963 QCOMPARE(rec.field("T_NUMBER").length(), 10); |
969 QCOMPARE(rec.field("T_NUMBER").length(), 10); |
964 QCOMPARE(rec.field("T_NUMBER").precision(), 5); |
970 QCOMPARE(rec.field("T_NUMBER").precision(), 5); |
965 } |
971 } |
966 |
972 |
1020 QSqlQuery q(db); |
1026 QSqlQuery q(db); |
1021 |
1027 |
1022 if(tst_Databases::isPostgreSQL(db)) |
1028 if(tst_Databases::isPostgreSQL(db)) |
1023 QVERIFY_SQL( q, exec("set client_min_messages='warning'")); |
1029 QVERIFY_SQL( q, exec("set client_min_messages='warning'")); |
1024 |
1030 |
1025 q.exec("drop sequence " + qTableName("qtestfields") + "_t_bigserial_seq"); |
1031 q.exec("drop sequence " + qTableName("qtestfields", __FILE__) + "_t_bigserial_seq"); |
1026 q.exec("drop sequence " + qTableName("qtestfields") + "_t_serial_seq"); |
1032 q.exec("drop sequence " + qTableName("qtestfields", __FILE__) + "_t_serial_seq"); |
1027 // older psql cut off the table name |
1033 // older psql cut off the table name |
1028 q.exec("drop sequence " + qTableName("qtestfields").left(15) + "_t_bigserial_seq"); |
1034 q.exec("drop sequence " + qTableName("qtestfields", __FILE__).left(15) + "_t_bigserial_seq"); |
1029 q.exec("drop sequence " + qTableName("qtestfields").left(18) + "_t_serial_seq"); |
1035 q.exec("drop sequence " + qTableName("qtestfields", __FILE__).left(18) + "_t_serial_seq"); |
1030 |
1036 |
1031 const int fieldCount = createFieldTable(fieldDefs, db); |
1037 const int fieldCount = createFieldTable(fieldDefs, db); |
1032 QVERIFY(fieldCount > 0); |
1038 QVERIFY(fieldCount > 0); |
1033 |
1039 |
1034 commonFieldTest(fieldDefs, db, fieldCount); |
1040 commonFieldTest(fieldDefs, db, fieldCount); |
1321 void tst_QSqlDatabase::transaction() |
1327 void tst_QSqlDatabase::transaction() |
1322 { |
1328 { |
1323 QFETCH(QString, dbName); |
1329 QFETCH(QString, dbName); |
1324 QSqlDatabase db = QSqlDatabase::database(dbName); |
1330 QSqlDatabase db = QSqlDatabase::database(dbName); |
1325 CHECK_DATABASE(db); |
1331 CHECK_DATABASE(db); |
1332 const QString qtest(qTableName("qtest", __FILE__)); |
|
1326 |
1333 |
1327 if (!db.driver()->hasFeature(QSqlDriver::Transactions)) { |
1334 if (!db.driver()->hasFeature(QSqlDriver::Transactions)) { |
1328 QSKIP("DBMS not transaction capable", SkipSingle); |
1335 QSKIP("DBMS not transaction capable", SkipSingle); |
1329 } |
1336 } |
1330 |
1337 |
1331 QVERIFY(db.transaction()); |
1338 QVERIFY(db.transaction()); |
1332 |
1339 |
1333 QSqlQuery q(db); |
1340 QSqlQuery q(db); |
1334 QVERIFY_SQL(q, exec("insert into " + qTableName("qtest") + " values (40, 'VarChar40', 'Char40', 40.40)")); |
1341 QVERIFY_SQL(q, exec("insert into " + qtest + " values (40, 'VarChar40', 'Char40', 40.40)")); |
1335 QVERIFY_SQL(q, exec("select * from " + qTableName("qtest") + " where id = 40")); |
1342 QVERIFY_SQL(q, exec("select * from " + qtest + " where id = 40")); |
1336 QVERIFY(q.next()); |
1343 QVERIFY(q.next()); |
1337 QCOMPARE(q.value(0).toInt(), 40); |
1344 QCOMPARE(q.value(0).toInt(), 40); |
1338 q.clear(); |
1345 q.clear(); |
1339 |
1346 |
1340 QVERIFY(db.commit()); |
1347 QVERIFY(db.commit()); |
1341 |
1348 |
1342 QVERIFY(db.transaction()); |
1349 QVERIFY(db.transaction()); |
1343 QVERIFY_SQL(q, exec("select * from " + qTableName("qtest") + " where id = 40")); |
1350 QVERIFY_SQL(q, exec("select * from " + qtest + " where id = 40")); |
1344 QVERIFY(q.next()); |
1351 QVERIFY(q.next()); |
1345 QCOMPARE(q.value(0).toInt(), 40); |
1352 QCOMPARE(q.value(0).toInt(), 40); |
1346 q.clear(); |
1353 q.clear(); |
1347 QVERIFY(db.commit()); |
1354 QVERIFY(db.commit()); |
1348 |
1355 |
1349 QVERIFY(db.transaction()); |
1356 QVERIFY(db.transaction()); |
1350 QVERIFY_SQL(q, exec("insert into " + qTableName("qtest") + " values (41, 'VarChar41', 'Char41', 41.41)")); |
1357 QVERIFY_SQL(q, exec("insert into " + qtest + " values (41, 'VarChar41', 'Char41', 41.41)")); |
1351 QVERIFY_SQL(q, exec("select * from " + qTableName("qtest") + " where id = 41")); |
1358 QVERIFY_SQL(q, exec("select * from " + qtest + " where id = 41")); |
1352 QVERIFY(q.next()); |
1359 QVERIFY(q.next()); |
1353 QCOMPARE(q.value(0).toInt(), 41); |
1360 QCOMPARE(q.value(0).toInt(), 41); |
1354 q.clear(); // for SQLite which does not allow any references on rows that shall be rolled back |
1361 q.clear(); // for SQLite which does not allow any references on rows that shall be rolled back |
1355 if (!db.rollback()) { |
1362 if (!db.rollback()) { |
1356 if (db.driverName().startsWith("QMYSQL")) { |
1363 if (db.driverName().startsWith("QMYSQL")) { |
1359 } else { |
1366 } else { |
1360 QFAIL("Could not rollback transaction: " + tst_Databases::printError(db.lastError())); |
1367 QFAIL("Could not rollback transaction: " + tst_Databases::printError(db.lastError())); |
1361 } |
1368 } |
1362 } |
1369 } |
1363 |
1370 |
1364 QVERIFY_SQL(q, exec("select * from " + qTableName("qtest") + " where id = 41")); |
1371 QVERIFY_SQL(q, exec("select * from " + qtest + " where id = 41")); |
1365 if(db.driverName().startsWith("QODBC") && dbName.contains("MySQL")) |
1372 if(db.driverName().startsWith("QODBC") && dbName.contains("MySQL")) |
1366 QEXPECT_FAIL("", "Some odbc drivers don't actually roll back despite telling us they do, especially the mysql driver", Continue); |
1373 QEXPECT_FAIL("", "Some odbc drivers don't actually roll back despite telling us they do, especially the mysql driver", Continue); |
1367 QVERIFY(!q.next()); |
1374 QVERIFY(!q.next()); |
1368 |
1375 |
1369 populateTestTables(db); |
1376 populateTestTables(db); |
1373 { |
1380 { |
1374 QFETCH(QString, dbName); |
1381 QFETCH(QString, dbName); |
1375 QSqlDatabase db = QSqlDatabase::database(dbName); |
1382 QSqlDatabase db = QSqlDatabase::database(dbName); |
1376 CHECK_DATABASE(db); |
1383 CHECK_DATABASE(db); |
1377 QString drvName = db.driverName(); |
1384 QString drvName = db.driverName(); |
1385 const QString qtest_bigint(qTableName("qtest_bigint", __FILE__)); |
|
1378 |
1386 |
1379 QSqlQuery q(db); |
1387 QSqlQuery q(db); |
1380 q.setForwardOnly(true); |
1388 q.setForwardOnly(true); |
1381 if (drvName.startsWith("QOCI")) |
1389 if (drvName.startsWith("QOCI")) |
1382 q.setNumericalPrecisionPolicy(QSql::LowPrecisionInt64); |
1390 q.setNumericalPrecisionPolicy(QSql::LowPrecisionInt64); |
1383 |
1391 |
1384 if (drvName.startsWith("QMYSQL")) { |
1392 if (drvName.startsWith("QMYSQL")) { |
1385 QVERIFY_SQL(q, exec("create table " + qTableName("qtest_bigint") + " (id int, t_s64bit bigint, t_u64bit bigint unsigned)")); |
1393 QVERIFY_SQL(q, exec("create table " + qtest_bigint + " (id int, t_s64bit bigint, t_u64bit bigint unsigned)")); |
1386 } else if (drvName.startsWith("QPSQL") |
1394 } else if (drvName.startsWith("QPSQL") |
1387 || drvName.startsWith("QDB2") |
1395 || drvName.startsWith("QDB2") |
1388 || tst_Databases::isSqlServer(db)) { |
1396 || tst_Databases::isSqlServer(db)) { |
1389 QVERIFY_SQL(q, exec("create table " + qTableName("qtest_bigint") + "(id int, t_s64bit bigint, t_u64bit bigint)")); |
1397 QVERIFY_SQL(q, exec("create table " + qtest_bigint + "(id int, t_s64bit bigint, t_u64bit bigint)")); |
1390 } else if (drvName.startsWith("QOCI")) { |
1398 } else if (drvName.startsWith("QOCI")) { |
1391 QVERIFY_SQL(q, exec("create table " + qTableName("qtest_bigint") + " (id int, t_s64bit int, t_u64bit int)")); |
1399 QVERIFY_SQL(q, exec("create table " + qtest_bigint + " (id int, t_s64bit int, t_u64bit int)")); |
1392 //} else if (drvName.startsWith("QIBASE")) { |
1400 //} else if (drvName.startsWith("QIBASE")) { |
1393 // QVERIFY_SQL(q, exec("create table " + qTableName("qtest_bigint") + " (id int, t_s64bit int64, t_u64bit int64)")); |
1401 // QVERIFY_SQL(q, exec("create table " + qtest_bigint + " (id int, t_s64bit int64, t_u64bit int64)")); |
1394 } else { |
1402 } else { |
1395 QSKIP("no 64 bit integer support", SkipAll); |
1403 QSKIP("no 64 bit integer support", SkipAll); |
1396 } |
1404 } |
1397 QVERIFY(q.prepare("insert into " + qTableName("qtest_bigint") + " values (?, ?, ?)")); |
1405 QVERIFY(q.prepare("insert into " + qtest_bigint + " values (?, ?, ?)")); |
1398 qlonglong ll = Q_INT64_C(9223372036854775807); |
1406 qlonglong ll = Q_INT64_C(9223372036854775807); |
1399 qulonglong ull = Q_UINT64_C(18446744073709551615); |
1407 qulonglong ull = Q_UINT64_C(18446744073709551615); |
1400 |
1408 |
1401 if (drvName.startsWith("QMYSQL") || drvName.startsWith("QOCI")) { |
1409 if (drvName.startsWith("QMYSQL") || drvName.startsWith("QOCI")) { |
1402 q.bindValue(0, 0); |
1410 q.bindValue(0, 0); |
1416 q.bindValue(0, 1); |
1424 q.bindValue(0, 1); |
1417 q.bindValue(1, -ll); |
1425 q.bindValue(1, -ll); |
1418 q.bindValue(2, (qlonglong) ull); |
1426 q.bindValue(2, (qlonglong) ull); |
1419 QVERIFY_SQL(q, exec()); |
1427 QVERIFY_SQL(q, exec()); |
1420 } |
1428 } |
1421 QVERIFY(q.exec("select * from " + qTableName("qtest_bigint") + " order by id")); |
1429 QVERIFY(q.exec("select * from " + qtest_bigint + " order by id")); |
1422 QVERIFY(q.next()); |
1430 QVERIFY(q.next()); |
1423 QCOMPARE(q.value(1).toDouble(), (double)ll); |
1431 QCOMPARE(q.value(1).toDouble(), (double)ll); |
1424 QCOMPARE(q.value(1).toLongLong(), ll); |
1432 QCOMPARE(q.value(1).toLongLong(), ll); |
1425 if(drvName.startsWith("QOCI")) |
1433 if(drvName.startsWith("QOCI")) |
1426 QEXPECT_FAIL("", "Oracle driver lacks support for unsigned int64 types", Continue); |
1434 QEXPECT_FAIL("", "Oracle driver lacks support for unsigned int64 types", Continue); |
1443 || db.driverName().startsWith("QSQLITE") |
1451 || db.driverName().startsWith("QSQLITE") |
1444 || db.driverName().startsWith("QTDS") |
1452 || db.driverName().startsWith("QTDS") |
1445 || db.driverName().startsWith("QODBC")) |
1453 || db.driverName().startsWith("QODBC")) |
1446 cs = true; |
1454 cs = true; |
1447 |
1455 |
1448 QSqlRecord rec = db.record(qTableName("qtest")); |
1456 QSqlRecord rec = db.record(qTableName("qtest", __FILE__)); |
1449 QVERIFY((int)rec.count() > 0); |
1457 QVERIFY((int)rec.count() > 0); |
1450 if (!cs) { |
1458 if (!cs) { |
1451 rec = db.record(qTableName("QTEST").toUpper()); |
1459 rec = db.record(qTableName("QTEST", __FILE__).toUpper()); |
1452 QVERIFY((int)rec.count() > 0); |
1460 QVERIFY((int)rec.count() > 0); |
1453 rec = db.record(qTableName("qTesT")); |
1461 rec = db.record(qTableName("qTesT", __FILE__)); |
1454 QVERIFY((int)rec.count() > 0); |
1462 QVERIFY((int)rec.count() > 0); |
1455 } |
1463 } |
1456 |
1464 |
1457 #ifdef QT3_SUPPORT |
1465 #ifdef QT3_SUPPORT |
1458 Q3SqlRecordInfo rInf = db.recordInfo(qTableName("qtest")); |
1466 Q3SqlRecordInfo rInf = db.recordInfo(qTableName("qtest", __FILE__)); |
1459 QVERIFY((int)rInf.count() > 0); |
1467 QVERIFY((int)rInf.count() > 0); |
1460 if (!cs) { |
1468 if (!cs) { |
1461 rInf = db.recordInfo(qTableName("QTEST").upper()); |
1469 rInf = db.recordInfo(qTableName("QTEST", __FILE__).upper()); |
1462 QVERIFY((int)rInf.count() > 0); |
1470 QVERIFY((int)rInf.count() > 0); |
1463 rInf = db.recordInfo(qTableName("qTesT")); |
1471 rInf = db.recordInfo(qTableName("qTesT", __FILE__)); |
1464 QVERIFY((int)rInf.count() > 0); |
1472 QVERIFY((int)rInf.count() > 0); |
1465 } |
1473 } |
1466 #endif |
1474 #endif |
1467 |
1475 |
1468 rec = db.primaryIndex(qTableName("qtest")); |
1476 rec = db.primaryIndex(qTableName("qtest", __FILE__)); |
1469 QVERIFY((int)rec.count() > 0); |
1477 QVERIFY((int)rec.count() > 0); |
1470 if (!cs) { |
1478 if (!cs) { |
1471 rec = db.primaryIndex(qTableName("QTEST").toUpper()); |
1479 rec = db.primaryIndex(qTableName("QTEST", __FILE__).toUpper()); |
1472 QVERIFY((int)rec.count() > 0); |
1480 QVERIFY((int)rec.count() > 0); |
1473 rec = db.primaryIndex(qTableName("qTesT")); |
1481 rec = db.primaryIndex(qTableName("qTesT", __FILE__)); |
1474 QVERIFY((int)rec.count() > 0); |
1482 QVERIFY((int)rec.count() > 0); |
1475 } |
1483 } |
1476 } |
1484 } |
1477 |
1485 |
1478 void tst_QSqlDatabase::noEscapedFieldNamesInRecord() |
1486 void tst_QSqlDatabase::noEscapedFieldNamesInRecord() |
1484 QString fieldname("t_varchar"); |
1492 QString fieldname("t_varchar"); |
1485 if (db.driverName().startsWith("QOCI") || db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QDB2")) |
1493 if (db.driverName().startsWith("QOCI") || db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QDB2")) |
1486 fieldname = fieldname.toUpper(); |
1494 fieldname = fieldname.toUpper(); |
1487 |
1495 |
1488 QSqlQuery q(db); |
1496 QSqlQuery q(db); |
1489 QString query = "SELECT " + db.driver()->escapeIdentifier(fieldname, QSqlDriver::FieldName) + " FROM " + qTableName("qtest"); |
1497 QString query = "SELECT " + db.driver()->escapeIdentifier(fieldname, QSqlDriver::FieldName) + " FROM " + qTableName("qtest", __FILE__); |
1490 QVERIFY_SQL(q, exec(query)); |
1498 QVERIFY_SQL(q, exec(query)); |
1491 QCOMPARE(q.record().fieldName(0), fieldname); |
1499 QCOMPARE(q.record().fieldName(0), fieldname); |
1492 } |
1500 } |
1493 |
1501 |
1494 void tst_QSqlDatabase::psql_schemas() |
1502 void tst_QSqlDatabase::psql_schemas() |
1504 |
1512 |
1505 if(tst_Databases::isPostgreSQL(db)) { |
1513 if(tst_Databases::isPostgreSQL(db)) { |
1506 QVERIFY_SQL( q, exec("set client_min_messages='warning'")); |
1514 QVERIFY_SQL( q, exec("set client_min_messages='warning'")); |
1507 } |
1515 } |
1508 |
1516 |
1509 QVERIFY_SQL(q, exec("CREATE SCHEMA " + qTableName("qtestschema"))); |
1517 QVERIFY_SQL(q, exec("CREATE SCHEMA " + qTableName("qtestschema", __FILE__))); |
1510 |
1518 |
1511 QString table = qTableName("qtestschema") + '.' + qTableName("qtesttable"); |
1519 QString table = qTableName("qtestschema", __FILE__) + '.' + qTableName("qtesttable", __FILE__); |
1512 QVERIFY_SQL(q, exec("CREATE TABLE " + table + " (id int primary key, name varchar(20))")); |
1520 QVERIFY_SQL(q, exec("CREATE TABLE " + table + " (id int primary key, name varchar(20))")); |
1513 |
1521 |
1514 QVERIFY(db.tables().contains(table)); |
1522 QVERIFY(db.tables().contains(table)); |
1515 |
1523 |
1516 QSqlRecord rec = db.record(table); |
1524 QSqlRecord rec = db.record(table); |
1542 QSqlQuery q(db); |
1550 QSqlQuery q(db); |
1543 |
1551 |
1544 if(tst_Databases::isPostgreSQL(db)) |
1552 if(tst_Databases::isPostgreSQL(db)) |
1545 QVERIFY_SQL( q, exec("set client_min_messages='warning'")); |
1553 QVERIFY_SQL( q, exec("set client_min_messages='warning'")); |
1546 |
1554 |
1547 QString schemaName = qTableName("qtestScHeMa"); |
1555 const QString schemaName(qTableName("qtestScHeMa", __FILE__)), |
1548 QString tableName = qTableName("qtest"); |
1556 tableName(qTableName("qtest", __FILE__)), |
1549 QString field1Name = QString("fIeLdNaMe"); |
1557 field1Name(QLatin1String("fIeLdNaMe")), |
1550 QString field2Name = QString("ZuLu"); |
1558 field2Name(QLatin1String("ZuLu")); |
1551 |
1559 |
1552 q.exec(QString("DROP SCHEMA \"%1\" CASCADE").arg(schemaName)); |
1560 q.exec(QString("DROP SCHEMA \"%1\" CASCADE").arg(schemaName)); |
1553 QString createSchema = QString("CREATE SCHEMA \"%1\"").arg(schemaName); |
1561 QString createSchema = QString("CREATE SCHEMA \"%1\"").arg(schemaName); |
1554 QVERIFY_SQL(q, exec(createSchema)); |
1562 QVERIFY_SQL(q, exec(createSchema)); |
1555 QString createTable = QString("CREATE TABLE \"%1\".\"%2\" (\"%3\" int PRIMARY KEY, \"%4\" varchar(20))").arg(schemaName).arg(tableName).arg(field1Name).arg(field2Name); |
1563 QString createTable = QString("CREATE TABLE \"%1\".\"%2\" (\"%3\" int PRIMARY KEY, \"%4\" varchar(20))").arg(schemaName).arg(tableName).arg(field1Name).arg(field2Name); |
1572 QCOMPARE(rec.field(0).type(), QVariant::Int); |
1580 QCOMPARE(rec.field(0).type(), QVariant::Int); |
1573 |
1581 |
1574 q.exec(QString("DROP SCHEMA \"%1\" CASCADE").arg(schemaName)); |
1582 q.exec(QString("DROP SCHEMA \"%1\" CASCADE").arg(schemaName)); |
1575 } |
1583 } |
1576 |
1584 |
1577 |
|
1578 void tst_QSqlDatabase::psql_escapeBytea() |
1585 void tst_QSqlDatabase::psql_escapeBytea() |
1579 { |
1586 { |
1580 QFETCH(QString, dbName); |
1587 QFETCH(QString, dbName); |
1581 QSqlDatabase db = QSqlDatabase::database(dbName); |
1588 QSqlDatabase db = QSqlDatabase::database(dbName); |
1582 CHECK_DATABASE(db); |
1589 CHECK_DATABASE(db); |
1583 |
1590 |
1584 const char dta[4] = {'\x71', '\x14', '\x32', '\x81'}; |
1591 const char dta[4] = {'\x71', '\x14', '\x32', '\x81'}; |
1585 QByteArray ba(dta, 4); |
1592 QByteArray ba(dta, 4); |
1586 |
1593 |
1587 QSqlQuery q(db); |
1594 QSqlQuery q(db); |
1588 QString tableName = qTableName("batable"); |
1595 const QString tableName(qTableName("batable", __FILE__)); |
1589 QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (ba bytea)").arg(tableName))); |
1596 QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (ba bytea)").arg(tableName))); |
1590 |
1597 |
1591 QSqlQuery iq(db); |
1598 QSqlQuery iq(db); |
1592 QVERIFY_SQL(iq, prepare(QString("INSERT INTO %1 VALUES (?)").arg(tableName))); |
1599 QVERIFY_SQL(iq, prepare(QString("INSERT INTO %1 VALUES (?)").arg(tableName))); |
1593 iq.bindValue(0, QVariant(ba)); |
1600 iq.bindValue(0, QVariant(ba)); |
1616 double ver=version.section(QChar::fromLatin1('.'),0,1).toDouble(); |
1623 double ver=version.section(QChar::fromLatin1('.'),0,1).toDouble(); |
1617 if (ver < 7.3) |
1624 if (ver < 7.3) |
1618 QSKIP("Test requires PostgreSQL >= 7.3", SkipSingle); |
1625 QSKIP("Test requires PostgreSQL >= 7.3", SkipSingle); |
1619 |
1626 |
1620 QSqlQuery q(db); |
1627 QSqlQuery q(db); |
1621 QString tableName = qTableName("bug_249059"); |
1628 const QString tableName(qTableName("bug_249059", __FILE__)); |
1622 QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (dt timestamp, t time)").arg(tableName))); |
1629 QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (dt timestamp, t time)").arg(tableName))); |
1623 |
1630 |
1624 QSqlQuery iq(db); |
1631 QSqlQuery iq(db); |
1625 QVERIFY_SQL(iq, prepare(QString("INSERT INTO %1 VALUES (?, ?)").arg(tableName))); |
1632 QVERIFY_SQL(iq, prepare(QString("INSERT INTO %1 VALUES (?, ?)").arg(tableName))); |
1626 iq.bindValue(0, QVariant(QString("2001-09-09 04:05:06.789 -5:00"))); |
1633 iq.bindValue(0, QVariant(QString("2001-09-09 04:05:06.789 -5:00"))); |
1651 QSqlDatabase db = QSqlDatabase::database(dbName); |
1658 QSqlDatabase db = QSqlDatabase::database(dbName); |
1652 CHECK_DATABASE(db); |
1659 CHECK_DATABASE(db); |
1653 // DBMS_SPECIFIC(db, "QPSQL"); |
1660 // DBMS_SPECIFIC(db, "QPSQL"); |
1654 |
1661 |
1655 QSqlQuery q(db); |
1662 QSqlQuery q(db); |
1656 QString tableName = qTableName("qtest_prec"); |
1663 const QString tableName(qTableName("qtest_prec", __FILE__)); |
1657 if(!db.driver()->hasFeature(QSqlDriver::LowPrecisionNumbers)) |
1664 if(!db.driver()->hasFeature(QSqlDriver::LowPrecisionNumbers)) |
1658 QSKIP("Driver or database doesn't support setting precision policy", SkipSingle); |
1665 QSKIP("Driver or database doesn't support setting precision policy", SkipSingle); |
1659 |
1666 |
1660 // Create a test table with some data |
1667 // Create a test table with some data |
1661 if(tst_Databases::isMSAccess(db)) |
1668 if(tst_Databases::isMSAccess(db)) |
1748 QSKIP("MySQL through ODBC-driver specific test", SkipSingle); |
1755 QSKIP("MySQL through ODBC-driver specific test", SkipSingle); |
1749 return; |
1756 return; |
1750 } |
1757 } |
1751 |
1758 |
1752 QSqlQuery q(db); |
1759 QSqlQuery q(db); |
1753 QString tableName = qTableName("uint"); |
1760 const QString tableName(qTableName("uint", __FILE__)); |
1754 QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (foo integer(10) unsigned, bar integer(10))").arg(tableName))); |
1761 QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (foo integer(10) unsigned, bar integer(10))").arg(tableName))); |
1755 QVERIFY_SQL(q, exec(QString("INSERT INTO %1 VALUES (-4000000000, -4000000000)").arg(tableName))); |
1762 QVERIFY_SQL(q, exec(QString("INSERT INTO %1 VALUES (-4000000000, -4000000000)").arg(tableName))); |
1756 QVERIFY_SQL(q, exec(QString("INSERT INTO %1 VALUES (4000000000, 4000000000)").arg(tableName))); |
1763 QVERIFY_SQL(q, exec(QString("INSERT INTO %1 VALUES (4000000000, 4000000000)").arg(tableName))); |
1757 |
1764 |
1758 QVERIFY_SQL(q, exec(QString("SELECT foo, bar FROM %1").arg(tableName))); |
1765 QVERIFY_SQL(q, exec(QString("SELECT foo, bar FROM %1").arg(tableName))); |
1774 QSKIP("MS Access specific test", SkipSingle); |
1781 QSKIP("MS Access specific test", SkipSingle); |
1775 return; |
1782 return; |
1776 } |
1783 } |
1777 |
1784 |
1778 QSqlQuery q(db); |
1785 QSqlQuery q(db); |
1779 QString tableName = qTableName("strings"); |
1786 const QString tableName(qTableName("strings", __FILE__)); |
1780 QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (aStr memo, bStr memo, cStr memo, dStr memo" |
1787 QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (aStr memo, bStr memo, cStr memo, dStr memo" |
1781 ", eStr memo, fStr memo, gStr memo, hStr memo)").arg(tableName))); |
1788 ", eStr memo, fStr memo, gStr memo, hStr memo)").arg(tableName))); |
1782 |
1789 |
1783 QVERIFY_SQL(q, prepare(QString("INSERT INTO %1 VALUES (?, ?, ?, ?, ?, ?, ?, ?)").arg(tableName))); |
1790 QVERIFY_SQL(q, prepare(QString("INSERT INTO %1 VALUES (?, ?, ?, ?, ?, ?, ?, ?)").arg(tableName))); |
1784 QString aStr, bStr, cStr, dStr, eStr, fStr, gStr, hStr; |
1791 QString aStr, bStr, cStr, dStr, eStr, fStr, gStr, hStr; |
1812 QFETCH(QString, dbName); |
1819 QFETCH(QString, dbName); |
1813 QSqlDatabase db = QSqlDatabase::database(dbName); |
1820 QSqlDatabase db = QSqlDatabase::database(dbName); |
1814 CHECK_DATABASE(db); |
1821 CHECK_DATABASE(db); |
1815 |
1822 |
1816 QSqlQuery q(db); |
1823 QSqlQuery q(db); |
1817 QString tableName = qTableName("numericfields"); |
1824 const QString tableName(qTableName("numericfields", __FILE__)); |
1818 QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (id int not null, num1 NUMERIC(2,1), " |
1825 QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (id int not null, num1 NUMERIC(2,1), " |
1819 "num2 NUMERIC(5,2), num3 NUMERIC(10,3), " |
1826 "num2 NUMERIC(5,2), num3 NUMERIC(10,3), " |
1820 "num4 NUMERIC(18,4))").arg(tableName))); |
1827 "num4 NUMERIC(18,4))").arg(tableName))); |
1821 |
1828 |
1822 QVERIFY_SQL(q, exec(QString("INSERT INTO %1 VALUES (1, 1.1, 123.45, 1234567.123, 10203040506070.8090)").arg(tableName))); |
1829 QVERIFY_SQL(q, exec(QString("INSERT INTO %1 VALUES (1, 1.1, 123.45, 1234567.123, 10203040506070.8090)").arg(tableName))); |
1884 { |
1891 { |
1885 QFETCH(QString, dbName); |
1892 QFETCH(QString, dbName); |
1886 QSqlDatabase db = QSqlDatabase::database(dbName); |
1893 QSqlDatabase db = QSqlDatabase::database(dbName); |
1887 CHECK_DATABASE(db); |
1894 CHECK_DATABASE(db); |
1888 |
1895 |
1889 QString tableName = qTableName("qtest_ibaseblobs"); |
1896 const QString tableName(qTableName("qtest_ibaseblobs", __FILE__)); |
1890 QSqlQuery q(db); |
1897 QSqlQuery q(db); |
1891 QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (blob1 BLOB segment size 256)").arg(tableName))); |
1898 QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (blob1 BLOB segment size 256)").arg(tableName))); |
1892 |
1899 |
1893 QVERIFY_SQL(q, prepare(QString("INSERT INTO %1 VALUES (?)").arg(tableName))); |
1900 QVERIFY_SQL(q, prepare(QString("INSERT INTO %1 VALUES (?)").arg(tableName))); |
1894 q.bindValue(0, QByteArray().fill('x', 1024)); |
1901 q.bindValue(0, QByteArray().fill('x', 1024)); |
1917 QFETCH(QString, dbName); |
1924 QFETCH(QString, dbName); |
1918 QSqlDatabase db = QSqlDatabase::database(dbName); |
1925 QSqlDatabase db = QSqlDatabase::database(dbName); |
1919 CHECK_DATABASE(db); |
1926 CHECK_DATABASE(db); |
1920 |
1927 |
1921 QSqlQuery q(db); |
1928 QSqlQuery q(db); |
1922 QString procName = qTableName("qtest_proc1"); |
1929 const QString procName(qTableName("qtest_proc1", __FILE__)); |
1923 q.exec(QString("drop procedure %1").arg(procName)); |
1930 q.exec(QString("drop procedure %1").arg(procName)); |
1924 QVERIFY_SQL(q, exec("CREATE PROCEDURE " + procName + " (str VARCHAR(10))\nAS BEGIN\nstr='test';\nEND;")); |
1931 QVERIFY_SQL(q, exec("CREATE PROCEDURE " + procName + " (str VARCHAR(10))\nAS BEGIN\nstr='test';\nEND;")); |
1925 QVERIFY_SQL(q, exec(QString("execute procedure %1('qtest')").arg(procName))); |
1932 QVERIFY_SQL(q, exec(QString("execute procedure %1('qtest')").arg(procName))); |
1926 q.exec(QString("drop procedure %1").arg(procName)); |
1933 q.exec(QString("drop procedure %1").arg(procName)); |
1927 } |
1934 } |
1935 if (!db.driverName().startsWith("QIBASE")) { |
1942 if (!db.driverName().startsWith("QIBASE")) { |
1936 QSKIP("InterBase specific test", SkipSingle); |
1943 QSKIP("InterBase specific test", SkipSingle); |
1937 return; |
1944 return; |
1938 } |
1945 } |
1939 |
1946 |
1940 QString procName = qTableName("qtest_proc2"); |
1947 const QString procName(qTableName("qtest_proc2", __FILE__)); |
1941 |
1948 |
1942 QSqlQuery q(db); |
1949 QSqlQuery q(db); |
1943 q.exec(QString("drop procedure %1").arg(procName)); |
1950 q.exec(QString("drop procedure %1").arg(procName)); |
1944 QVERIFY_SQL(q, exec("CREATE PROCEDURE " + procName + " (" |
1951 QVERIFY_SQL(q, exec("CREATE PROCEDURE " + procName + " (" |
1945 "\nABC INTEGER)" |
1952 "\nABC INTEGER)" |
1977 QSqlDatabase db = QSqlDatabase::database(dbName); |
1984 QSqlDatabase db = QSqlDatabase::database(dbName); |
1978 CHECK_DATABASE(db); |
1985 CHECK_DATABASE(db); |
1979 |
1986 |
1980 QSqlQuery q(db); |
1987 QSqlQuery q(db); |
1981 |
1988 |
1982 QVERIFY_SQL(q, exec(QString("INSERT INTO %1 (id, t_varchar, t_char) values (50, 'Trim Test ', 'Trim Test 2 ')").arg(qTableName("qtest")))); |
1989 QVERIFY_SQL(q, exec(QString("INSERT INTO %1 (id, t_varchar, t_char) values (50, 'Trim Test ', 'Trim Test 2 ')").arg(qTableName("qtest", __FILE__)))); |
1983 QVERIFY_SQL(q, exec(QString("INSERT INTO %1 (id, t_varchar, t_char) values (51, 'TrimTest', 'Trim Test 2')").arg(qTableName("qtest")))); |
1990 QVERIFY_SQL(q, exec(QString("INSERT INTO %1 (id, t_varchar, t_char) values (51, 'TrimTest', 'Trim Test 2')").arg(qTableName("qtest", __FILE__)))); |
1984 QVERIFY_SQL(q, exec(QString("INSERT INTO %1 (id, t_varchar, t_char) values (52, ' ', ' ')").arg(qTableName("qtest")))); |
1991 QVERIFY_SQL(q, exec(QString("INSERT INTO %1 (id, t_varchar, t_char) values (52, ' ', ' ')").arg(qTableName("qtest", __FILE__)))); |
1985 |
1992 |
1986 QVERIFY_SQL(q, exec(QString("SELECT t_varchar, t_char FROM %1 WHERE id >= 50 AND id <= 52 ORDER BY id").arg(qTableName("qtest")))); |
1993 QVERIFY_SQL(q, exec(QString("SELECT t_varchar, t_char FROM %1 WHERE id >= 50 AND id <= 52 ORDER BY id").arg(qTableName("qtest", __FILE__)))); |
1987 |
1994 |
1988 QVERIFY_SQL(q, next()); |
1995 QVERIFY_SQL(q, next()); |
1989 |
1996 |
1990 QCOMPARE(db.driver()->formatValue(q.record().field(0), true), QString("'Trim Test'")); |
1997 QCOMPARE(db.driver()->formatValue(q.record().field(0), true), QString("'Trim Test'")); |
1991 QCOMPARE(db.driver()->formatValue(q.record().field(1), true), QString("'Trim Test 2'")); |
1998 QCOMPARE(db.driver()->formatValue(q.record().field(1), true), QString("'Trim Test 2'")); |
2005 QFETCH(QString, dbName); |
2012 QFETCH(QString, dbName); |
2006 QSqlDatabase db = QSqlDatabase::database(dbName); |
2013 QSqlDatabase db = QSqlDatabase::database(dbName); |
2007 CHECK_DATABASE(db); |
2014 CHECK_DATABASE(db); |
2008 |
2015 |
2009 QSqlQuery q(db); |
2016 QSqlQuery q(db); |
2010 QVERIFY_SQL(q, exec("SELECT * from " + qTableName("qtest"))); |
2017 QVERIFY_SQL(q, exec("SELECT * from " + qTableName("qtest", __FILE__))); |
2011 QVERIFY_SQL(q, next()); |
2018 QVERIFY_SQL(q, next()); |
2012 db.open(); |
2019 db.open(); |
2013 QVERIFY_SQL(q, exec("SELECT * from " + qTableName("qtest"))); |
2020 QVERIFY_SQL(q, exec("SELECT * from " + qTableName("qtest", __FILE__))); |
2014 QVERIFY_SQL(q, next()); |
2021 QVERIFY_SQL(q, next()); |
2015 db.open(); |
2022 db.open(); |
2016 } |
2023 } |
2017 |
2024 |
2018 void tst_QSqlDatabase::odbc_bindBoolean() |
2025 void tst_QSqlDatabase::odbc_bindBoolean() |
2025 QSKIP("MySql has inconsistent behaviour of bit field type across versions.", SkipSingle); |
2032 QSKIP("MySql has inconsistent behaviour of bit field type across versions.", SkipSingle); |
2026 return; |
2033 return; |
2027 } |
2034 } |
2028 |
2035 |
2029 QSqlQuery q(db); |
2036 QSqlQuery q(db); |
2030 QVERIFY_SQL(q, exec("CREATE TABLE " + qTableName("qtestBindBool") + "(id int, boolvalue bit)")); |
2037 QVERIFY_SQL(q, exec("CREATE TABLE " + qTableName("qtestBindBool", __FILE__) + "(id int, boolvalue bit)")); |
2031 |
2038 |
2032 // Bind and insert |
2039 // Bind and insert |
2033 QVERIFY_SQL(q, prepare("INSERT INTO " + qTableName("qtestBindBool") + " VALUES(?, ?)")); |
2040 QVERIFY_SQL(q, prepare("INSERT INTO " + qTableName("qtestBindBool", __FILE__) + " VALUES(?, ?)")); |
2034 q.bindValue(0, 1); |
2041 q.bindValue(0, 1); |
2035 q.bindValue(1, true); |
2042 q.bindValue(1, true); |
2036 QVERIFY_SQL(q, exec()); |
2043 QVERIFY_SQL(q, exec()); |
2037 q.bindValue(0, 2); |
2044 q.bindValue(0, 2); |
2038 q.bindValue(1, false); |
2045 q.bindValue(1, false); |
2039 QVERIFY_SQL(q, exec()); |
2046 QVERIFY_SQL(q, exec()); |
2040 |
2047 |
2041 // Retrive |
2048 // Retrive |
2042 QVERIFY_SQL(q, exec("SELECT id, boolvalue FROM " + qTableName("qtestBindBool") + " ORDER BY id")); |
2049 QVERIFY_SQL(q, exec("SELECT id, boolvalue FROM " + qTableName("qtestBindBool", __FILE__) + " ORDER BY id")); |
2043 QVERIFY_SQL(q, next()); |
2050 QVERIFY_SQL(q, next()); |
2044 QCOMPARE(q.value(0).toInt(), 1); |
2051 QCOMPARE(q.value(0).toInt(), 1); |
2045 QCOMPARE(q.value(1).toBool(), true); |
2052 QCOMPARE(q.value(1).toBool(), true); |
2046 QVERIFY_SQL(q, next()); |
2053 QVERIFY_SQL(q, next()); |
2047 QCOMPARE(q.value(0).toInt(), 2); |
2054 QCOMPARE(q.value(0).toInt(), 2); |
2051 void tst_QSqlDatabase::odbc_testqGetString() |
2058 void tst_QSqlDatabase::odbc_testqGetString() |
2052 { |
2059 { |
2053 QFETCH(QString, dbName); |
2060 QFETCH(QString, dbName); |
2054 QSqlDatabase db = QSqlDatabase::database(dbName); |
2061 QSqlDatabase db = QSqlDatabase::database(dbName); |
2055 CHECK_DATABASE(db); |
2062 CHECK_DATABASE(db); |
2063 const QString testqGetString(qTableName("testqGetString", __FILE__)); |
|
2056 |
2064 |
2057 QSqlQuery q(db); |
2065 QSqlQuery q(db); |
2058 if (tst_Databases::isSqlServer(db)) |
2066 if (tst_Databases::isSqlServer(db)) |
2059 QVERIFY_SQL(q, exec("CREATE TABLE " + qTableName("testqGetString") + "(id int, vcvalue varchar(MAX))")); |
2067 QVERIFY_SQL(q, exec("CREATE TABLE " + testqGetString + "(id int, vcvalue varchar(MAX))")); |
2060 else if(tst_Databases::isMSAccess(db)) |
2068 else if(tst_Databases::isMSAccess(db)) |
2061 QVERIFY_SQL(q, exec("CREATE TABLE " + qTableName("testqGetString") + "(id int, vcvalue memo)")); |
2069 QVERIFY_SQL(q, exec("CREATE TABLE " + testqGetString + "(id int, vcvalue memo)")); |
2062 else |
2070 else |
2063 QVERIFY_SQL(q, exec("CREATE TABLE " + qTableName("testqGetString") + "(id int, vcvalue varchar(65538))")); |
2071 QVERIFY_SQL(q, exec("CREATE TABLE " + testqGetString + "(id int, vcvalue varchar(65538))")); |
2064 |
2072 |
2065 QString largeString; |
2073 QString largeString; |
2066 largeString.fill('A', 65536); |
2074 largeString.fill('A', 65536); |
2067 |
2075 |
2068 // Bind and insert |
2076 // Bind and insert |
2069 QVERIFY_SQL(q, prepare("INSERT INTO " + qTableName("testqGetString") + " VALUES(?, ?)")); |
2077 QVERIFY_SQL(q, prepare("INSERT INTO " + testqGetString + " VALUES(?, ?)")); |
2070 q.bindValue(0, 1); |
2078 q.bindValue(0, 1); |
2071 q.bindValue(1, largeString); |
2079 q.bindValue(1, largeString); |
2072 QVERIFY_SQL(q, exec()); |
2080 QVERIFY_SQL(q, exec()); |
2073 q.bindValue(0, 2); |
2081 q.bindValue(0, 2); |
2074 q.bindValue(1, largeString+QLatin1Char('B')); |
2082 q.bindValue(1, largeString+QLatin1Char('B')); |
2076 q.bindValue(0, 3); |
2084 q.bindValue(0, 3); |
2077 q.bindValue(1, largeString+QLatin1Char('B')+QLatin1Char('C')); |
2085 q.bindValue(1, largeString+QLatin1Char('B')+QLatin1Char('C')); |
2078 QVERIFY_SQL(q, exec()); |
2086 QVERIFY_SQL(q, exec()); |
2079 |
2087 |
2080 // Retrive |
2088 // Retrive |
2081 QVERIFY_SQL(q, exec("SELECT id, vcvalue FROM " + qTableName("testqGetString") + " ORDER BY id")); |
2089 QVERIFY_SQL(q, exec("SELECT id, vcvalue FROM " + testqGetString + " ORDER BY id")); |
2082 QVERIFY_SQL(q, next()); |
2090 QVERIFY_SQL(q, next()); |
2083 QCOMPARE(q.value(0).toInt(), 1); |
2091 QCOMPARE(q.value(0).toInt(), 1); |
2084 QCOMPARE(q.value(1).toString().length(), 65536); |
2092 QCOMPARE(q.value(1).toString().length(), 65536); |
2085 QVERIFY_SQL(q, next()); |
2093 QVERIFY_SQL(q, next()); |
2086 QCOMPARE(q.value(0).toInt(), 2); |
2094 QCOMPARE(q.value(0).toInt(), 2); |
2094 void tst_QSqlDatabase::mysql_multiselect() |
2102 void tst_QSqlDatabase::mysql_multiselect() |
2095 { |
2103 { |
2096 QFETCH(QString, dbName); |
2104 QFETCH(QString, dbName); |
2097 QSqlDatabase db = QSqlDatabase::database(dbName); |
2105 QSqlDatabase db = QSqlDatabase::database(dbName); |
2098 CHECK_DATABASE(db); |
2106 CHECK_DATABASE(db); |
2107 const QString qtest(qTableName("qtest", __FILE__)); |
|
2099 |
2108 |
2100 QSqlQuery q(db); |
2109 QSqlQuery q(db); |
2101 QString version=tst_Databases::getMySqlVersion( db ); |
2110 QString version=tst_Databases::getMySqlVersion( db ); |
2102 double ver=version.section(QChar::fromLatin1('.'),0,1).toDouble(); |
2111 double ver=version.section(QChar::fromLatin1('.'),0,1).toDouble(); |
2103 if (ver < 4.1) |
2112 if (ver < 4.1) |
2104 QSKIP("Test requires MySQL >= 4.1", SkipSingle); |
2113 QSKIP("Test requires MySQL >= 4.1", SkipSingle); |
2105 |
2114 |
2106 QVERIFY_SQL(q, exec("SELECT * FROM " + qTableName("qtest") + "; SELECT * FROM " + qTableName("qtest"))); |
2115 QVERIFY_SQL(q, exec("SELECT * FROM " + qtest + "; SELECT * FROM " + qtest)); |
2107 QVERIFY_SQL(q, next()); |
2116 QVERIFY_SQL(q, next()); |
2108 QVERIFY_SQL(q, exec("SELECT * FROM " + qTableName("qtest") + "; SELECT * FROM " + qTableName("qtest"))); |
2117 QVERIFY_SQL(q, exec("SELECT * FROM " + qtest + "; SELECT * FROM " + qtest)); |
2109 QVERIFY_SQL(q, next()); |
2118 QVERIFY_SQL(q, next()); |
2110 QVERIFY_SQL(q, exec("SELECT * FROM " + qTableName("qtest"))); |
2119 QVERIFY_SQL(q, exec("SELECT * FROM " + qtest)); |
2111 } |
2120 } |
2112 |
2121 |
2113 void tst_QSqlDatabase::ibase_useCustomCharset() |
2122 void tst_QSqlDatabase::ibase_useCustomCharset() |
2114 { |
2123 { |
2115 QFETCH(QString, dbName); |
2124 QFETCH(QString, dbName); |
2119 |
2128 |
2120 db.close(); |
2129 db.close(); |
2121 db.setConnectOptions("ISC_DPB_LC_CTYPE=Latin1"); |
2130 db.setConnectOptions("ISC_DPB_LC_CTYPE=Latin1"); |
2122 db.open(); |
2131 db.open(); |
2123 |
2132 |
2124 QString tableName = qTableName("latin1table"); |
2133 const QString tableName(qTableName("latin1table", __FILE__)); |
2125 |
2134 |
2126 QSqlQuery q(db); |
2135 QSqlQuery q(db); |
2127 QVERIFY_SQL(q, exec(QString("CREATE TABLE %1(text VARCHAR(6) CHARACTER SET Latin1)").arg(tableName))); |
2136 QVERIFY_SQL(q, exec(QString("CREATE TABLE %1(text VARCHAR(6) CHARACTER SET Latin1)").arg(tableName))); |
2128 QVERIFY_SQL(q, prepare(QString("INSERT INTO %1 VALUES(?)").arg(tableName))); |
2137 QVERIFY_SQL(q, prepare(QString("INSERT INTO %1 VALUES(?)").arg(tableName))); |
2129 q.addBindValue(nonlatin1string); |
2138 q.addBindValue(nonlatin1string); |
2157 { |
2166 { |
2158 QFETCH(QString, dbName); |
2167 QFETCH(QString, dbName); |
2159 QSqlDatabase db = QSqlDatabase::database(dbName); |
2168 QSqlDatabase db = QSqlDatabase::database(dbName); |
2160 CHECK_DATABASE(db); |
2169 CHECK_DATABASE(db); |
2161 |
2170 |
2162 QString tableName = qTableName("qtest_xmltype"); |
2171 const QString tableName(qTableName("qtest_xmltype", __FILE__)); |
2163 QString xml("<?xml version=\"1.0\"?><TABLE_NAME>MY_TABLE</TABLE_NAME>"); |
2172 QString xml("<?xml version=\"1.0\"?><TABLE_NAME>MY_TABLE</TABLE_NAME>"); |
2164 QSqlQuery q(db); |
2173 QSqlQuery q(db); |
2165 |
2174 |
2166 // Embedding the XML in the statement |
2175 // Embedding the XML in the statement |
2167 if(!q.exec(QString("CREATE TABLE %1(xmldata xmltype)").arg(tableName))) |
2176 if(!q.exec(QString("CREATE TABLE %1(xmldata xmltype)").arg(tableName))) |
2185 { |
2194 { |
2186 QFETCH(QString, dbName); |
2195 QFETCH(QString, dbName); |
2187 QSqlDatabase db = QSqlDatabase::database(dbName); |
2196 QSqlDatabase db = QSqlDatabase::database(dbName); |
2188 CHECK_DATABASE(db); |
2197 CHECK_DATABASE(db); |
2189 |
2198 |
2190 QString tableName = qTableName("qtest"); |
2199 const QString tableName(qTableName("qtest", __FILE__)); |
2191 QSqlQuery q(db); |
2200 QSqlQuery q(db); |
2192 |
2201 |
2193 QVERIFY_SQL(q, exec(QString("SELECT t_varchar, t_char FROM %1").arg(tableName))); |
2202 QVERIFY_SQL(q, exec(QString("SELECT t_varchar, t_char FROM %1").arg(tableName))); |
2194 QVERIFY_SQL(q, next()); |
2203 QVERIFY_SQL(q, next()); |
2195 QCOMPARE(q.record().field(0).length(), 40); |
2204 QCOMPARE(q.record().field(0).length(), 40); |
2201 QFETCH(QString, dbName); |
2210 QFETCH(QString, dbName); |
2202 QSqlDatabase db = QSqlDatabase::database(dbName); |
2211 QSqlDatabase db = QSqlDatabase::database(dbName); |
2203 CHECK_DATABASE(db); |
2212 CHECK_DATABASE(db); |
2204 |
2213 |
2205 QSqlQuery q(db); |
2214 QSqlQuery q(db); |
2206 QString creator(qTableName("CREATOR")), appuser(qTableName("APPUSER")), table1(qTableName("TABLE1")); |
2215 const QString creator(qTableName("CREATOR", __FILE__)), appuser(qTableName("APPUSER", __FILE__)), table1(qTableName("TABLE1", __FILE__)); |
2207 // QVERIFY_SQL(q, exec("drop public synonym "+table1)); |
2216 // QVERIFY_SQL(q, exec("drop public synonym "+table1)); |
2208 QVERIFY_SQL(q, exec(QString("create user %1 identified by %2 default tablespace users temporary tablespace temp").arg(creator).arg(creator))); |
2217 QVERIFY_SQL(q, exec(QString("create user %1 identified by %2 default tablespace users temporary tablespace temp").arg(creator).arg(creator))); |
2209 QVERIFY_SQL(q, exec(QString("grant CONNECT to %1").arg(creator))); |
2218 QVERIFY_SQL(q, exec(QString("grant CONNECT to %1").arg(creator))); |
2210 QVERIFY_SQL(q, exec(QString("grant RESOURCE to %1").arg(creator))); |
2219 QVERIFY_SQL(q, exec(QString("grant RESOURCE to %1").arg(creator))); |
2211 QSqlDatabase db2=db.cloneDatabase(db, QLatin1String("oci_synonymstest")); |
2220 QSqlDatabase db2=db.cloneDatabase(db, QLatin1String("oci_synonymstest")); |
2219 QVERIFY_SQL(q2, exec(QString("grant select, insert, update, delete on %1 to %2").arg(table1).arg(appuser))); |
2228 QVERIFY_SQL(q2, exec(QString("grant select, insert, update, delete on %1 to %2").arg(table1).arg(appuser))); |
2220 QSqlDatabase db3=db.cloneDatabase(db, QLatin1String("oci_synonymstest2")); |
2229 QSqlDatabase db3=db.cloneDatabase(db, QLatin1String("oci_synonymstest2")); |
2221 db3.close(); |
2230 db3.close(); |
2222 QVERIFY_SQL(db3, open(appuser,appuser)); |
2231 QVERIFY_SQL(db3, open(appuser,appuser)); |
2223 QSqlQuery q3(db3); |
2232 QSqlQuery q3(db3); |
2224 QVERIFY_SQL(q3, exec("create synonym "+appuser+'.'+qTableName("synonyms")+" for "+creator+'.'+table1)); |
2233 QVERIFY_SQL(q3, exec("create synonym "+appuser+'.'+qTableName("synonyms", __FILE__)+" for "+creator+'.'+table1)); |
2225 QVERIFY_SQL(db3, tables().filter(qTableName("synonyms"), Qt::CaseInsensitive).count() >= 1); |
2234 QVERIFY_SQL(db3, tables().filter(qTableName("synonyms", __FILE__), Qt::CaseInsensitive).count() >= 1); |
2226 } |
2235 } |
2227 |
2236 |
2228 |
2237 |
2229 // This test isn't really necessary as SQL_GUID / uniqueidentifier is |
2238 // This test isn't really necessary as SQL_GUID / uniqueidentifier is |
2230 // already tested in recordSQLServer(). |
2239 // already tested in recordSQLServer(). |
2236 if (!tst_Databases::isSqlServer(db)) { |
2245 if (!tst_Databases::isSqlServer(db)) { |
2237 QSKIP("SQL Server (ODBC) specific test", SkipSingle); |
2246 QSKIP("SQL Server (ODBC) specific test", SkipSingle); |
2238 return; |
2247 return; |
2239 } |
2248 } |
2240 |
2249 |
2241 QString tableName = qTableName("qtest_sqlguid"); |
2250 const QString tableName(qTableName("qtest_sqlguid", __FILE__)); |
2242 QString guid = QString("AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE"); |
2251 QString guid = QString("AAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE"); |
2243 QString invalidGuid = QString("GAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE"); |
2252 QString invalidGuid = QString("GAAAAAAA-BBBB-CCCC-DDDD-EEEEEEEEEEEE"); |
2244 |
2253 |
2245 QSqlQuery q(db); |
2254 QSqlQuery q(db); |
2246 QVERIFY_SQL(q, exec(QString("CREATE TABLE %1(id uniqueidentifier)").arg(tableName))); |
2255 QVERIFY_SQL(q, exec(QString("CREATE TABLE %1(id uniqueidentifier)").arg(tableName))); |
2279 { |
2288 { |
2280 QFETCH(QString, dbName); |
2289 QFETCH(QString, dbName); |
2281 QSqlDatabase db = QSqlDatabase::database(dbName); |
2290 QSqlDatabase db = QSqlDatabase::database(dbName); |
2282 CHECK_DATABASE(db); |
2291 CHECK_DATABASE(db); |
2283 |
2292 |
2284 QString tableName = qTableName("uint_table"); |
2293 const QString tableName(qTableName("uint_table", __FILE__)); |
2285 unsigned int val = 4294967295U; |
2294 unsigned int val = 4294967295U; |
2286 |
2295 |
2287 QSqlQuery q(db); |
2296 QSqlQuery q(db); |
2288 if ( tst_Databases::isMSAccess( db ) ) |
2297 if ( tst_Databases::isMSAccess( db ) ) |
2289 QVERIFY_SQL(q, exec(QString("CREATE TABLE %1(num number)").arg(tableName))); |
2298 QVERIFY_SQL(q, exec(QString("CREATE TABLE %1(num number)").arg(tableName))); |
2343 { |
2352 { |
2344 QFETCH(QString, dbName); |
2353 QFETCH(QString, dbName); |
2345 QSqlDatabase db = QSqlDatabase::database(dbName); |
2354 QSqlDatabase db = QSqlDatabase::database(dbName); |
2346 CHECK_DATABASE(db); |
2355 CHECK_DATABASE(db); |
2347 |
2356 |
2348 QString procedureName = qTableName("posteventProc"); |
2357 const QString procedureName(qTableName("posteventProc", __FILE__)); |
2349 QSqlDriver *driver=db.driver(); |
2358 QSqlDriver *driver=db.driver(); |
2350 QVERIFY_SQL(*driver, subscribeToNotification(procedureName)); |
2359 QVERIFY_SQL(*driver, subscribeToNotification(procedureName)); |
2351 QTest::qWait(300); // Interbase needs some time to call the driver callback. |
2360 QTest::qWait(300); // Interbase needs some time to call the driver callback. |
2352 |
2361 |
2353 db.transaction(); // InterBase events are posted from within transactions. |
2362 db.transaction(); // InterBase events are posted from within transactions. |
2377 #if defined(Q_OS_LINUX) |
2386 #if defined(Q_OS_LINUX) |
2378 QSKIP( "Event support doesn't work on linux", SkipAll ); |
2387 QSKIP( "Event support doesn't work on linux", SkipAll ); |
2379 #endif |
2388 #endif |
2380 |
2389 |
2381 QSqlQuery query(db); |
2390 QSqlQuery query(db); |
2382 QString procedureName = qTableName("posteventProc"); |
2391 QString procedureName = qTableName("posteventProc", __FILE__); |
2383 |
2392 |
2384 QSqlDriver &driver=*(db.driver()); |
2393 QSqlDriver &driver=*(db.driver()); |
2385 QVERIFY_SQL(driver, subscribeToNotification(procedureName)); |
2394 QVERIFY_SQL(driver, subscribeToNotification(procedureName)); |
2386 QSignalSpy spy(db.driver(), SIGNAL(notification(const QString&))); |
2395 QSignalSpy spy(db.driver(), SIGNAL(notification(const QString&))); |
2387 query.exec(QString("NOTIFY \"%1\"").arg(procedureName)); |
2396 query.exec(QString("NOTIFY \"%1\"").arg(procedureName)); |
2401 QSKIP("SQLite3 specific test", SkipSingle); |
2410 QSKIP("SQLite3 specific test", SkipSingle); |
2402 return; |
2411 return; |
2403 } |
2412 } |
2404 |
2413 |
2405 QSqlQuery q(db); |
2414 QSqlQuery q(db); |
2406 QString tableName = qTableName("uint_test"); |
2415 const QString tableName(qTableName("uint_test", __FILE__)); |
2407 QVERIFY_SQL(q, exec(QString("CREATE TABLE %1(uint_field UNSIGNED INTEGER)").arg(tableName))); |
2416 QVERIFY_SQL(q, exec(QString("CREATE TABLE %1(uint_field UNSIGNED INTEGER)").arg(tableName))); |
2408 QVERIFY_SQL(q, prepare(QString("INSERT INTO %1 VALUES(?)").arg(tableName))); |
2417 QVERIFY_SQL(q, prepare(QString("INSERT INTO %1 VALUES(?)").arg(tableName))); |
2409 q.addBindValue(4000000000U); |
2418 q.addBindValue(4000000000U); |
2410 QVERIFY_SQL(q, exec()); |
2419 QVERIFY_SQL(q, exec()); |
2411 QVERIFY_SQL(q, exec(QString("SELECT uint_field FROM %1").arg(tableName))); |
2420 QVERIFY_SQL(q, exec(QString("SELECT uint_field FROM %1").arg(tableName))); |
2421 { |
2430 { |
2422 QFETCH(QString, dbName); |
2431 QFETCH(QString, dbName); |
2423 QSqlDatabase db = QSqlDatabase::database(dbName); |
2432 QSqlDatabase db = QSqlDatabase::database(dbName); |
2424 CHECK_DATABASE(db); |
2433 CHECK_DATABASE(db); |
2425 |
2434 |
2426 QString tableName = qTableName("qtest"); |
2435 const QString tableName(qTableName("qtest", __FILE__)); |
2427 QSqlQuery q(db); |
2436 QSqlQuery q(db); |
2428 q.exec(QString("SELECT id, t_varchar, t_char, t_numeric FROM %1").arg(tableName)); |
2437 q.exec(QString("SELECT id, t_varchar, t_char, t_numeric FROM %1").arg(tableName)); |
2429 q.next(); |
2438 q.next(); |
2430 QVariant c4 = q.value(3); |
2439 QVariant c4 = q.value(3); |
2431 QVariant c3 = q.value(2); |
2440 QVariant c3 = q.value(2); |
2444 QSqlDatabase db = QSqlDatabase::database(dbName); |
2453 QSqlDatabase db = QSqlDatabase::database(dbName); |
2445 CHECK_DATABASE(db); |
2454 CHECK_DATABASE(db); |
2446 |
2455 |
2447 // select a record with NULL value |
2456 // select a record with NULL value |
2448 QSqlQuery q(QString::null, db); |
2457 QSqlQuery q(QString::null, db); |
2449 QVERIFY_SQL(q, exec("select * from " + qTableName("qtest") + " where id = 4")); |
2458 QVERIFY_SQL(q, exec("select * from " + qTableName("qtest", __FILE__) + " where id = 4")); |
2450 QVERIFY_SQL(q, next()); |
2459 QVERIFY_SQL(q, next()); |
2451 |
2460 |
2452 QSqlDriver *driver = db.driver(); |
2461 QSqlDriver *driver = db.driver(); |
2453 QVERIFY(driver); |
2462 QVERIFY(driver); |
2454 |
2463 |
2467 if ( db.driverName().startsWith( "QMYSQL" ) && tst_Databases::getMySqlVersion( db ).section( QChar('.'), 0, 1 ).toDouble()<4.1 ) |
2476 if ( db.driverName().startsWith( "QMYSQL" ) && tst_Databases::getMySqlVersion( db ).section( QChar('.'), 0, 1 ).toDouble()<4.1 ) |
2468 QSKIP( "Test requires MySQL >= 4.1", SkipSingle ); |
2477 QSKIP( "Test requires MySQL >= 4.1", SkipSingle ); |
2469 |
2478 |
2470 QSqlQuery q(db); |
2479 QSqlQuery q(db); |
2471 QVERIFY_SQL(q, exec("begin")); |
2480 QVERIFY_SQL(q, exec("begin")); |
2472 QVERIFY_SQL(q, exec("insert into "+qTableName("qtest")+" VALUES (54, 'foo', 'foo', 54.54)")); |
2481 QVERIFY_SQL(q, exec("insert into "+qTableName("qtest", __FILE__)+" VALUES (54, 'foo', 'foo', 54.54)")); |
2473 QVERIFY_SQL(q, exec("savepoint foo")); |
2482 QVERIFY_SQL(q, exec("savepoint foo")); |
2474 } |
2483 } |
2475 |
2484 |
2476 void tst_QSqlDatabase::oci_tables() |
2485 void tst_QSqlDatabase::oci_tables() |
2477 { |
2486 { |
2478 QFETCH(QString, dbName); |
2487 QFETCH(QString, dbName); |
2479 QSqlDatabase db = QSqlDatabase::database(dbName); |
2488 QSqlDatabase db = QSqlDatabase::database(dbName); |
2480 CHECK_DATABASE(db); |
2489 CHECK_DATABASE(db); |
2481 QSqlQuery q(db); |
2490 QSqlQuery q(db); |
2482 QString systemTableName("system."+qTableName("mypassword")); |
2491 const QString systemTableName("system."+qTableName("mypassword", __FILE__)); |
2483 QVERIFY_SQL(q, exec("CREATE TABLE "+systemTableName+"(name VARCHAR(20))")); |
2492 QVERIFY_SQL(q, exec("CREATE TABLE "+systemTableName+"(name VARCHAR(20))")); |
2484 QVERIFY(!db.tables().contains(systemTableName.toUpper())); |
2493 QVERIFY(!db.tables().contains(systemTableName.toUpper())); |
2485 QVERIFY(db.tables(QSql::SystemTables).contains(systemTableName.toUpper())); |
2494 QVERIFY(db.tables(QSql::SystemTables).contains(systemTableName.toUpper())); |
2486 } |
2495 } |
2487 |
2496 |
2497 void tst_QSqlDatabase::sqlite_enable_cache_mode() |
|
2498 { |
|
2499 QFETCH(QString, dbName); |
|
2500 if(dbName.endsWith(":memory:")) |
|
2501 QSKIP( "cache mode is meaningless for :memory: databases", SkipSingle ); |
|
2502 QSqlDatabase db = QSqlDatabase::database(dbName); |
|
2503 CHECK_DATABASE(db); |
|
2504 db.close(); |
|
2505 db.setConnectOptions("QSQLITE_ENABLE_SHARED_CACHE"); |
|
2506 QVERIFY_SQL(db, open()); |
|
2507 QSqlDatabase db2 = QSqlDatabase::cloneDatabase(db, dbName+":cachemodeconn2"); |
|
2508 db2.setConnectOptions("QSQLITE_ENABLE_SHARED_CACHE"); |
|
2509 QVERIFY_SQL(db2, open()); |
|
2510 QSqlQuery q(db), q2(db2); |
|
2511 QVERIFY_SQL(q, exec("select * from "+qTableName("qtest", __FILE__))); |
|
2512 QVERIFY_SQL(q2, exec("select * from "+qTableName("qtest", __FILE__))); |
|
2513 } |
|
2514 |
|
2488 QTEST_MAIN(tst_QSqlDatabase) |
2515 QTEST_MAIN(tst_QSqlDatabase) |
2489 #include "tst_qsqldatabase.moc" |
2516 #include "tst_qsqldatabase.moc" |