From bee70a74465ef837b446ef2d2309a002da5bfe30 Mon Sep 17 00:00:00 2001 From: Bill King Date: Tue, 13 Oct 2009 11:57:05 +1000 Subject: Fixes issue with multiple lookups to same table/field Previously the renaming scheme created a new table_field alias name. If multiple references referred to the same table/field lookup, then multiples of the same alias would be generated in the query, leading the query to fail. Reviewed-by: Justin McPherson --- src/sql/models/qsqlrelationaltablemodel.cpp | 3 ++- .../qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/sql/models/qsqlrelationaltablemodel.cpp b/src/sql/models/qsqlrelationaltablemodel.cpp index aebecf1..5f0a35d 100644 --- a/src/sql/models/qsqlrelationaltablemodel.cpp +++ b/src/sql/models/qsqlrelationaltablemodel.cpp @@ -569,7 +569,8 @@ QString QSqlRelationalTableModel::selectStatement() const QString displayColumn = relation.displayColumn(); if (d->db.driver()->isIdentifierEscaped(displayColumn, QSqlDriver::FieldName)) displayColumn = d->db.driver()->stripDelimiters(displayColumn, QSqlDriver::FieldName); - fList.append(QString::fromLatin1(" AS %1_%2").arg(relTableName).arg(displayColumn)); + fList.append(QString::fromLatin1(" AS %1_%2_%3").arg(relTableName).arg(displayColumn).arg(fieldNames.value(fieldList[i]))); + fieldNames.insert(fieldList[i], fieldNames.value(fieldList[i])-1); } // this needs fixing!! the below if is borken. diff --git a/tests/auto/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp b/tests/auto/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp index 05d546e..cb24a9f 100644 --- a/tests/auto/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp +++ b/tests/auto/qsqlrelationaltablemodel/tst_qsqlrelationaltablemodel.cpp @@ -837,10 +837,10 @@ void tst_QSqlRelationalTableModel::insertRecordDuplicateFieldNames() QVERIFY_SQL(model, select()); if (db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) { - QCOMPARE(model.record(1).value(qTableName("reltest4").append(QLatin1String("_name")).toUpper()).toString(), + QCOMPARE(model.record(1).value(qTableName("reltest4").append(QLatin1String("_name_2")).toUpper()).toString(), QString("Trondheim")); } else { - QCOMPARE(model.record(1).value(qTableName("reltest4").append(QLatin1String("_name"))).toString(), + QCOMPARE(model.record(1).value(qTableName("reltest4").append(QLatin1String("_name_2"))).toString(), QString("Trondheim")); } @@ -859,9 +859,9 @@ void tst_QSqlRelationalTableModel::insertRecordDuplicateFieldNames() // The duplicate field names is aliased because it's comes from the relation's display column. if(db.driverName().startsWith("QIBASE") || db.driverName().startsWith("QOCI") || db.driverName().startsWith("QDB2")) - QCOMPARE(rec.fieldName(2), (qTableName("reltest4").append(QLatin1String("_name"))).toUpper()); + QCOMPARE(rec.fieldName(2), (qTableName("reltest4").append(QLatin1String("_name_2"))).toUpper()); else - QCOMPARE(rec.fieldName(2), qTableName("reltest4").append(QLatin1String("_name"))); + QCOMPARE(rec.fieldName(2), qTableName("reltest4").append(QLatin1String("_name_2"))); QVERIFY(model.insertRecord(-1, rec)); QCOMPARE(model.data(model.index(2, 2)).toString(), QString("Oslo")); -- cgit v0.12 From 99e7dcf532b1c84aa64a7ab9af9a5fd14fd64033 Mon Sep 17 00:00:00 2001 From: Bill King Date: Tue, 13 Oct 2009 12:00:42 +1000 Subject: Fixes autotests now for MS Access via ODBC. --- .../q3sqlselectcursor/tst_q3sqlselectcursor.cpp | 8 ++++++-- tests/auto/qsqldatabase/tst_databases.h | 1 + tests/auto/qsqldatabase/tst_qsqldatabase.cpp | 21 +++++++++++++++------ tests/auto/qsqldriver/tst_qsqldriver.cpp | 12 ++++++++++-- tests/auto/qsqlthread/tst_qsqlthread.cpp | 2 ++ 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/tests/auto/q3sqlselectcursor/tst_q3sqlselectcursor.cpp b/tests/auto/q3sqlselectcursor/tst_q3sqlselectcursor.cpp index 5893687..237f29d 100644 --- a/tests/auto/q3sqlselectcursor/tst_q3sqlselectcursor.cpp +++ b/tests/auto/q3sqlselectcursor/tst_q3sqlselectcursor.cpp @@ -108,8 +108,12 @@ void tst_Q3SqlSelectCursor::createTestTables( QSqlDatabase db ) return; QSqlQuery q( db ); // please never ever change this table; otherwise fix all tests ;) - QVERIFY_SQL(q, exec( "create table " + qTableName( "qtest" ) + " ( id int not null, t_varchar varchar(40) not null," - "t_char char(40), t_numeric numeric(6, 3), primary key (id, t_varchar) )" )); + if (tst_Databases::isMSAccess(db)) + QVERIFY_SQL(q, exec( "create table " + qTableName( "qtest" ) + " ( id int not null, t_varchar varchar(40) not null," + "t_char char(40), t_numeric number, primary key (id, t_varchar) )" )); + else + QVERIFY_SQL(q, exec( "create table " + qTableName( "qtest" ) + " ( id int not null, t_varchar varchar(40) not null," + "t_char char(40), t_numeric numeric(6, 3), primary key (id, t_varchar) )" )); } void tst_Q3SqlSelectCursor::dropTestTables( QSqlDatabase db ) diff --git a/tests/auto/qsqldatabase/tst_databases.h b/tests/auto/qsqldatabase/tst_databases.h index c5c3663..25b1e2f 100644 --- a/tests/auto/qsqldatabase/tst_databases.h +++ b/tests/auto/qsqldatabase/tst_databases.h @@ -258,6 +258,7 @@ public: // addDb( "QTDS7", "testdb", "testuser", "Ee4Gabf6_", "bq-winserv2008" ); // addDb( "QODBC3", "DRIVER={SQL SERVER};SERVER=bq-winserv2003-x86-01.apac.nokia.com;DATABASE=testdb;PORT=1433", "testuser", "Ee4Gabf6_", "" ); // addDb( "QODBC3", "DRIVER={SQL SERVER};SERVER=bq-winserv2008-x86-01.apac.nokia.com;DATABASE=testdb;PORT=1433", "testuser", "Ee4Gabf6_", "" ); +// addDb( "QODBC", "DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:\\dbs\\access\\testdb.mdb", "", "", "" ); } void open() diff --git a/tests/auto/qsqldatabase/tst_qsqldatabase.cpp b/tests/auto/qsqldatabase/tst_qsqldatabase.cpp index c9c8f5e..ed13340 100644 --- a/tests/auto/qsqldatabase/tst_qsqldatabase.cpp +++ b/tests/auto/qsqldatabase/tst_qsqldatabase.cpp @@ -792,8 +792,8 @@ void tst_QSqlDatabase::checkValues(const FieldDef fieldDefs[], QSqlDatabase db) if (val1.type() == QVariant::DateTime || val1.type() == QVariant::Time) qDebug("Received Time: " + val1.toTime().toString("hh:mm:ss.zzz")); QFAIL(QString(" Expected: '%1' Received: '%2' for field %3 (etype %4 rtype %5) in checkValues").arg( - val2.toString()).arg( - val1.toString()).arg( + val2.type() == QVariant::ByteArray ? val2.toByteArray().toHex() : val2.toString()).arg( + val1.type() == QVariant::ByteArray ? val1.toByteArray().toHex() : val1.toString()).arg( fieldDefs[ i ].fieldName()).arg( val2.typeName()).arg( val1.typeName()) @@ -1292,9 +1292,9 @@ void tst_QSqlDatabase::recordAccess() FieldDef("varchar(20)", QVariant::String, QString("Blah1")), FieldDef("single", QVariant::Double, 1.12345), FieldDef("double", QVariant::Double, 1.123456), - FieldDef("byte", QVariant::Int, 255), + FieldDef("byte", QVariant::UInt, 255), #ifdef QT3_SUPPORT - FieldDef("binary", QVariant::ByteArray, Q3CString("Blah2")), + FieldDef("binary(5)", QVariant::ByteArray, Q3CString("Blah2")), #endif FieldDef("long", QVariant::Int, 2147483647), FieldDef("memo", QVariant::String, memo), @@ -1643,7 +1643,10 @@ void tst_QSqlDatabase::precisionPolicy() QSKIP("Driver or database doesn't support setting precision policy", SkipSingle); // Create a test table with some data - QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (id smallint, num numeric(18,5))").arg(tableName))); + if(tst_Databases::isMSAccess(db)) + QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (id smallint, num number)").arg(tableName))); + else + QVERIFY_SQL(q, exec(QString("CREATE TABLE %1 (id smallint, num numeric(18,5))").arg(tableName))); QVERIFY_SQL(q, prepare(QString("INSERT INTO %1 VALUES (?, ?)").arg(tableName))); q.bindValue(0, 1); q.bindValue(1, 123); @@ -2007,6 +2010,7 @@ void tst_QSqlDatabase::odbc_bindBoolean() QSKIP("MySql has inconsistent behaviour of bit field type across versions.", SkipSingle); return; } + QSqlQuery q(db); QVERIFY_SQL(q, exec("CREATE TABLE " + qTableName("qtestBindBool") + "(id int, boolvalue bit)")); @@ -2038,6 +2042,8 @@ void tst_QSqlDatabase::odbc_testqGetString() QSqlQuery q(db); if (tst_Databases::isSqlServer(db)) QVERIFY_SQL(q, exec("CREATE TABLE " + qTableName("testqGetString") + "(id int, vcvalue varchar(MAX))")); + else if(tst_Databases::isMSAccess(db)) + QVERIFY_SQL(q, exec("CREATE TABLE " + qTableName("testqGetString") + "(id int, vcvalue memo)")); else QVERIFY_SQL(q, exec("CREATE TABLE " + qTableName("testqGetString") + "(id int, vcvalue varchar(65538))")); @@ -2264,7 +2270,10 @@ void tst_QSqlDatabase::odbc_uintfield() unsigned int val = 4294967295U; QSqlQuery q(db); - q.exec(QString("CREATE TABLE %1(num numeric(10))").arg(tableName)); + if ( tst_Databases::isMSAccess( db ) ) + QVERIFY_SQL(q, exec(QString("CREATE TABLE %1(num number)").arg(tableName))); + else + QVERIFY_SQL(q, exec(QString("CREATE TABLE %1(num numeric(10))").arg(tableName))); q.prepare(QString("INSERT INTO %1 VALUES(?)").arg(tableName)); q.addBindValue(val); QVERIFY_SQL(q, exec()); diff --git a/tests/auto/qsqldriver/tst_qsqldriver.cpp b/tests/auto/qsqldriver/tst_qsqldriver.cpp index f463c9e..8fc38a7 100644 --- a/tests/auto/qsqldriver/tst_qsqldriver.cpp +++ b/tests/auto/qsqldriver/tst_qsqldriver.cpp @@ -160,7 +160,11 @@ void tst_QSqlDriver::record() //check that we can't get records using incorrect tablename casing that's been quoted rec = db.driver()->record(db.driver()->escapeIdentifier(tablename,QSqlDriver::TableName)); - if (tst_Databases::isMySQL(db) || db.driverName().startsWith("QSQLITE") || db.driverName().startsWith("QTDS") || tst_Databases::isSqlServer(db)) + if (tst_Databases::isMySQL(db) + || db.driverName().startsWith("QSQLITE") + || db.driverName().startsWith("QTDS") + || tst_Databases::isSqlServer(db) + || tst_Databases::isMSAccess(db)) QCOMPARE(rec.count(), 4); //mysql, sqlite and tds will match else QCOMPARE(rec.count(), 0); @@ -208,7 +212,11 @@ void tst_QSqlDriver::primaryIndex() tablename = tablename.toUpper(); index = db.driver()->primaryIndex(db.driver()->escapeIdentifier(tablename, QSqlDriver::TableName)); - if (tst_Databases::isMySQL(db) || db.driverName().startsWith("QSQLITE") || db.driverName().startsWith("QTDS") || tst_Databases::isSqlServer(db)) + if (tst_Databases::isMySQL(db) + || db.driverName().startsWith("QSQLITE") + || db.driverName().startsWith("QTDS") + || tst_Databases::isSqlServer(db) + || tst_Databases::isMSAccess(db)) QCOMPARE(index.count(), 1); //mysql will always find the table name regardless of casing else QCOMPARE(index.count(), 0); diff --git a/tests/auto/qsqlthread/tst_qsqlthread.cpp b/tests/auto/qsqlthread/tst_qsqlthread.cpp index c088a47..be66e9e 100644 --- a/tests/auto/qsqlthread/tst_qsqlthread.cpp +++ b/tests/auto/qsqlthread/tst_qsqlthread.cpp @@ -404,6 +404,8 @@ void tst_QSqlThread::readWriteThreading() if (db.databaseName() == ":memory:") QSKIP("does not work with in-memory databases", SkipSingle); + else if (tst_Databases::isMSAccess(db)) + QSKIP("does not work with MS Access databases", SkipSingle); SqlProducer producer(db); SqlConsumer consumer(db); -- cgit v0.12 From 276309001a6a5c2c1a62e8d0336cae9ebf65e213 Mon Sep 17 00:00:00 2001 From: Bill King Date: Tue, 13 Oct 2009 12:01:39 +1000 Subject: This autotest doesn't make sense for in-memory databases. --- tests/auto/qsqltablemodel/tst_qsqltablemodel.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/auto/qsqltablemodel/tst_qsqltablemodel.cpp b/tests/auto/qsqltablemodel/tst_qsqltablemodel.cpp index 49e087f..5405cb6 100644 --- a/tests/auto/qsqltablemodel/tst_qsqltablemodel.cpp +++ b/tests/auto/qsqltablemodel/tst_qsqltablemodel.cpp @@ -897,6 +897,8 @@ void tst_QSqlTableModel::sqlite_attachedDatabase() QFETCH(QString, dbName); QSqlDatabase db = QSqlDatabase::database(dbName); CHECK_DATABASE(db); + if(db.databaseName() == ":memory:") + QSKIP(":memory: database, skipping test", SkipSingle); QSqlDatabase attachedDb = QSqlDatabase::cloneDatabase(db, db.driverName() + QLatin1String("attached")); attachedDb.setDatabaseName(db.databaseName()+QLatin1String("attached.dat")); -- cgit v0.12 From de164c161c3ade4b5b5f4164495dd35578d31b1d Mon Sep 17 00:00:00 2001 From: Bill King Date: Tue, 13 Oct 2009 13:55:09 +1000 Subject: Fixes autotest: Savepoints are actually supported from 4.1 onwards. --- tests/auto/qsqldatabase/tst_qsqldatabase.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/auto/qsqldatabase/tst_qsqldatabase.cpp b/tests/auto/qsqldatabase/tst_qsqldatabase.cpp index ed13340..7149fab 100644 --- a/tests/auto/qsqldatabase/tst_qsqldatabase.cpp +++ b/tests/auto/qsqldatabase/tst_qsqldatabase.cpp @@ -2449,8 +2449,8 @@ void tst_QSqlDatabase::mysql_savepointtest() QFETCH(QString, dbName); QSqlDatabase db = QSqlDatabase::database(dbName); CHECK_DATABASE(db); - if ( db.driverName().startsWith( "QMYSQL" ) && tst_Databases::getMySqlVersion( db ).section( QChar('.'), 0, 0 ).toInt()<5 ) - QSKIP( "Test requires MySQL >= 5.0", SkipSingle ); + if ( db.driverName().startsWith( "QMYSQL" ) && tst_Databases::getMySqlVersion( db ).section( QChar('.'), 0, 1 ).toInt()<4.1 ) + QSKIP( "Test requires MySQL >= 4.1", SkipSingle ); QSqlQuery q(db); QVERIFY_SQL(q, exec("begin")); -- cgit v0.12 From 0ce1c5b4e27b85a122a02e8cc9c3eb49c9bc806f Mon Sep 17 00:00:00 2001 From: Stian Sandvik Thomassen Date: Tue, 13 Oct 2009 14:53:10 +1000 Subject: Doc: clarified what addDatabase() returns if the driver cannot be loaded If the driver cannot be loaded, isValid() returns false. Reviewed-by: Bill King --- src/sql/kernel/qsqldatabase.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/sql/kernel/qsqldatabase.cpp b/src/sql/kernel/qsqldatabase.cpp index 821760f..193aa7c 100644 --- a/src/sql/kernel/qsqldatabase.cpp +++ b/src/sql/kernel/qsqldatabase.cpp @@ -455,6 +455,8 @@ void QSqlDatabasePrivate::disable() The database connection is referred to by \a connectionName. The newly added database connection is returned. + If \a type is not available or could not be loaded, isValid() returns false. + If \a connectionName is not specified, the new connection becomes the default connection for the application, and subsequent calls to database() without the connection name argument will return the -- cgit v0.12