146 } |
146 } |
147 |
147 |
148 void tst_QSqlQueryModel::dropTestTables(QSqlDatabase db) |
148 void tst_QSqlQueryModel::dropTestTables(QSqlDatabase db) |
149 { |
149 { |
150 QStringList tableNames; |
150 QStringList tableNames; |
151 tableNames << qTableName("test") |
151 tableNames << qTableName("test", __FILE__) |
152 << qTableName("test2") |
152 << qTableName("test2", __FILE__) |
153 << qTableName("test3") |
153 << qTableName("test3", __FILE__) |
154 << qTableName("many"); |
154 << qTableName("many", __FILE__); |
155 tst_Databases::safeDropTables(db, tableNames); |
155 tst_Databases::safeDropTables(db, tableNames); |
156 } |
156 } |
157 |
157 |
158 void tst_QSqlQueryModel::createTestTables(QSqlDatabase db) |
158 void tst_QSqlQueryModel::createTestTables(QSqlDatabase db) |
159 { |
159 { |
160 dropTestTables(db); |
160 dropTestTables(db); |
161 QSqlQuery q(db); |
161 QSqlQuery q(db); |
162 if(tst_Databases::isPostgreSQL(db)) |
162 if(tst_Databases::isPostgreSQL(db)) |
163 QVERIFY_SQL( q, exec("set client_min_messages='warning'")); |
163 QVERIFY_SQL( q, exec("set client_min_messages='warning'")); |
164 QVERIFY_SQL( q, exec("create table " + qTableName("test") + "(id integer not null, name varchar(20), title integer, primary key (id))")); |
164 QVERIFY_SQL( q, exec("create table " + qTableName("test", __FILE__) + "(id integer not null, name varchar(20), title integer, primary key (id))")); |
165 QVERIFY_SQL( q, exec("create table " + qTableName("test2") + "(id integer not null, title varchar(20), primary key (id))")); |
165 QVERIFY_SQL( q, exec("create table " + qTableName("test2", __FILE__) + "(id integer not null, title varchar(20), primary key (id))")); |
166 QVERIFY_SQL( q, exec("create table " + qTableName("test3") + "(id integer not null, primary key (id))")); |
166 QVERIFY_SQL( q, exec("create table " + qTableName("test3", __FILE__) + "(id integer not null, primary key (id))")); |
167 QVERIFY_SQL( q, exec("create table " + qTableName("many") + "(id integer not null, name varchar(20), primary key (id))")); |
167 QVERIFY_SQL( q, exec("create table " + qTableName("many", __FILE__) + "(id integer not null, name varchar(20), primary key (id))")); |
168 } |
168 } |
169 |
169 |
170 void tst_QSqlQueryModel::populateTestTables(QSqlDatabase db) |
170 void tst_QSqlQueryModel::populateTestTables(QSqlDatabase db) |
171 { |
171 { |
172 qWarning() << "Populating test tables, this can take quite a while... ZZZzzz..."; |
172 qWarning() << "Populating test tables, this can take quite a while... ZZZzzz..."; |
173 bool hasTransactions = db.driver()->hasFeature(QSqlDriver::Transactions); |
173 bool hasTransactions = db.driver()->hasFeature(QSqlDriver::Transactions); |
174 |
174 |
175 QSqlQuery q(db), q2(db); |
175 QSqlQuery q(db), q2(db); |
176 |
176 |
177 tst_Databases::safeDropTables(db, QStringList() << qTableName("manytmp") << qTableName("test3tmp")); |
177 tst_Databases::safeDropTables(db, QStringList() << qTableName("manytmp", __FILE__) << qTableName("test3tmp", __FILE__)); |
178 QVERIFY_SQL(q, exec("create table " + qTableName("manytmp") + "(id integer not null, name varchar(20), primary key (id))")); |
178 QVERIFY_SQL(q, exec("create table " + qTableName("manytmp", __FILE__) + "(id integer not null, name varchar(20), primary key (id))")); |
179 QVERIFY_SQL(q, exec("create table " + qTableName("test3tmp") + "(id integer not null, primary key (id))")); |
179 QVERIFY_SQL(q, exec("create table " + qTableName("test3tmp", __FILE__) + "(id integer not null, primary key (id))")); |
180 |
180 |
181 if (hasTransactions) QVERIFY_SQL(db, transaction()); |
181 if (hasTransactions) QVERIFY_SQL(db, transaction()); |
182 |
182 |
183 QVERIFY_SQL(q, exec("insert into " + qTableName("test") + " values(1, 'harry', 1)")); |
183 QVERIFY_SQL(q, exec("insert into " + qTableName("test", __FILE__) + " values(1, 'harry', 1)")); |
184 QVERIFY_SQL(q, exec("insert into " + qTableName("test") + " values(2, 'trond', 2)")); |
184 QVERIFY_SQL(q, exec("insert into " + qTableName("test", __FILE__) + " values(2, 'trond', 2)")); |
185 QVERIFY_SQL(q, exec("insert into " + qTableName("test2") + " values(1, 'herr')")); |
185 QVERIFY_SQL(q, exec("insert into " + qTableName("test2", __FILE__) + " values(1, 'herr')")); |
186 QVERIFY_SQL(q, exec("insert into " + qTableName("test2") + " values(2, 'mister')")); |
186 QVERIFY_SQL(q, exec("insert into " + qTableName("test2", __FILE__) + " values(2, 'mister')")); |
187 |
187 |
188 QVERIFY_SQL(q, exec(QString("insert into " + qTableName("test3") + " values(0)"))); |
188 QVERIFY_SQL(q, exec(QString("insert into " + qTableName("test3", __FILE__) + " values(0)"))); |
189 QVERIFY_SQL(q, prepare("insert into "+qTableName("test3")+"(id) select id + ? from "+qTableName("test3tmp"))); |
189 QVERIFY_SQL(q, prepare("insert into "+qTableName("test3", __FILE__)+"(id) select id + ? from "+qTableName("test3tmp", __FILE__))); |
190 for (int i=1; i<260; i*=2) { |
190 for (int i=1; i<260; i*=2) { |
191 q2.exec("delete from "+qTableName("test3tmp")); |
191 q2.exec("delete from "+qTableName("test3tmp", __FILE__)); |
192 QVERIFY_SQL(q2, exec("insert into "+qTableName("test3tmp")+"(id) select id from "+qTableName("test3"))); |
192 QVERIFY_SQL(q2, exec("insert into "+qTableName("test3tmp", __FILE__)+"(id) select id from "+qTableName("test3", __FILE__))); |
193 q.bindValue(0, i); |
193 q.bindValue(0, i); |
194 QVERIFY_SQL(q, exec()); |
194 QVERIFY_SQL(q, exec()); |
195 } |
195 } |
196 |
196 |
197 QVERIFY_SQL(q, exec(QString("insert into " + qTableName("many") + "(id, name) values (0, \'harry\')"))); |
197 QVERIFY_SQL(q, exec(QString("insert into " + qTableName("many", __FILE__) + "(id, name) values (0, \'harry\')"))); |
198 QVERIFY_SQL(q, prepare("insert into "+qTableName("many")+"(id, name) select id + ?, name from "+qTableName("manytmp"))); |
198 QVERIFY_SQL(q, prepare("insert into "+qTableName("many", __FILE__)+"(id, name) select id + ?, name from "+qTableName("manytmp", __FILE__))); |
199 for (int i=1; i < 2048; i*=2) { |
199 for (int i=1; i < 2048; i*=2) { |
200 q2.exec("delete from "+qTableName("manytmp")); |
200 q2.exec("delete from "+qTableName("manytmp", __FILE__)); |
201 QVERIFY_SQL(q2, exec("insert into "+qTableName("manytmp")+"(id, name) select id, name from "+qTableName("many"))); |
201 QVERIFY_SQL(q2, exec("insert into "+qTableName("manytmp", __FILE__)+"(id, name) select id, name from "+qTableName("many", __FILE__))); |
202 q.bindValue(0, i); |
202 q.bindValue(0, i); |
203 QVERIFY_SQL(q, exec()); |
203 QVERIFY_SQL(q, exec()); |
204 } |
204 } |
205 |
205 |
206 if (hasTransactions) QVERIFY_SQL(db, commit()); |
206 if (hasTransactions) QVERIFY_SQL(db, commit()); |
207 |
207 |
208 tst_Databases::safeDropTables(db, QStringList() << qTableName("manytmp") << qTableName("test3tmp")); |
208 tst_Databases::safeDropTables(db, QStringList() << qTableName("manytmp", __FILE__) << qTableName("test3tmp", __FILE__)); |
209 } |
209 } |
210 |
210 |
211 void tst_QSqlQueryModel::generic_data(const QString& engine) |
211 void tst_QSqlQueryModel::generic_data(const QString& engine) |
212 { |
212 { |
213 if ( dbs.fillTestTable(engine) == 0 ) { |
213 if ( dbs.fillTestTable(engine) == 0 ) { |
489 |
489 |
490 if (db.driver()->hasFeature(QSqlDriver::QuerySize)) |
490 if (db.driver()->hasFeature(QSqlDriver::QuerySize)) |
491 QSKIP("Test applies only for drivers not reporting the query size.", SkipSingle); |
491 QSKIP("Test applies only for drivers not reporting the query size.", SkipSingle); |
492 |
492 |
493 QSqlQueryModel model; |
493 QSqlQueryModel model; |
494 model.setQuery(QSqlQuery("SELECT * FROM " + qTableName("test3"), db)); |
494 model.setQuery(QSqlQuery("SELECT * FROM " + qTableName("test3", __FILE__), db)); |
495 QSortFilterProxyModel proxy; |
495 QSortFilterProxyModel proxy; |
496 proxy.setSourceModel(&model); |
496 proxy.setSourceModel(&model); |
497 |
497 |
498 QTableView view; |
498 QTableView view; |
499 view.setModel(&proxy); |
499 view.setModel(&proxy); |
500 |
500 |
501 QSignalSpy modelRowsRemovedSpy(&model, SIGNAL(rowsRemoved(const QModelIndex &, int, int))); |
501 QSignalSpy modelRowsRemovedSpy(&model, SIGNAL(rowsRemoved(const QModelIndex &, int, int))); |
502 QSignalSpy modelRowsInsertedSpy(&model, SIGNAL(rowsInserted(const QModelIndex &, int, int))); |
502 QSignalSpy modelRowsInsertedSpy(&model, SIGNAL(rowsInserted(const QModelIndex &, int, int))); |
503 model.setQuery(QSqlQuery("SELECT * FROM " + qTableName("test3"), db)); |
503 model.setQuery(QSqlQuery("SELECT * FROM " + qTableName("test3", __FILE__), db)); |
504 view.scrollToBottom(); |
504 view.scrollToBottom(); |
505 |
505 |
506 QTestEventLoop::instance().enterLoop(1); |
506 QTestEventLoop::instance().enterLoop(1); |
507 |
507 |
508 QCOMPARE(proxy.rowCount(), 511); |
508 QCOMPARE(proxy.rowCount(), 511); |
534 QSqlQueryModel model; |
534 QSqlQueryModel model; |
535 QSignalSpy modelRowsAboutToBeRemovedSpy(&model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int))); |
535 QSignalSpy modelRowsAboutToBeRemovedSpy(&model, SIGNAL(rowsAboutToBeRemoved(const QModelIndex &, int, int))); |
536 QSignalSpy modelRowsRemovedSpy(&model, SIGNAL(rowsRemoved(const QModelIndex &, int, int))); |
536 QSignalSpy modelRowsRemovedSpy(&model, SIGNAL(rowsRemoved(const QModelIndex &, int, int))); |
537 |
537 |
538 // First select, the model was empty and no rows had to be removed! |
538 // First select, the model was empty and no rows had to be removed! |
539 model.setQuery(QSqlQuery("SELECT * FROM " + qTableName("test"), db)); |
539 model.setQuery(QSqlQuery("SELECT * FROM " + qTableName("test", __FILE__), db)); |
540 QCOMPARE(modelRowsAboutToBeRemovedSpy.count(), 0); |
540 QCOMPARE(modelRowsAboutToBeRemovedSpy.count(), 0); |
541 QCOMPARE(modelRowsRemovedSpy.count(), 0); |
541 QCOMPARE(modelRowsRemovedSpy.count(), 0); |
542 |
542 |
543 // Second select, the model wasn't empty and two rows had to be removed! |
543 // Second select, the model wasn't empty and two rows had to be removed! |
544 model.setQuery(QSqlQuery("SELECT * FROM " + qTableName("test"), db)); |
544 model.setQuery(QSqlQuery("SELECT * FROM " + qTableName("test", __FILE__), db)); |
545 QCOMPARE(modelRowsAboutToBeRemovedSpy.count(), 1); |
545 QCOMPARE(modelRowsAboutToBeRemovedSpy.count(), 1); |
546 QCOMPARE(modelRowsAboutToBeRemovedSpy.value(0).value(1).toInt(), 0); |
546 QCOMPARE(modelRowsAboutToBeRemovedSpy.value(0).value(1).toInt(), 0); |
547 QCOMPARE(modelRowsAboutToBeRemovedSpy.value(0).value(2).toInt(), 1); |
547 QCOMPARE(modelRowsAboutToBeRemovedSpy.value(0).value(2).toInt(), 1); |
548 QCOMPARE(modelRowsRemovedSpy.count(), 1); |
548 QCOMPARE(modelRowsRemovedSpy.count(), 1); |
549 QCOMPARE(modelRowsRemovedSpy.value(0).value(1).toInt(), 0); |
549 QCOMPARE(modelRowsRemovedSpy.value(0).value(1).toInt(), 0); |
576 void tst_QSqlQueryModel::task_180617() |
576 void tst_QSqlQueryModel::task_180617() |
577 { |
577 { |
578 QFETCH(QString, dbName); |
578 QFETCH(QString, dbName); |
579 QSqlDatabase db = QSqlDatabase::database(dbName); |
579 QSqlDatabase db = QSqlDatabase::database(dbName); |
580 CHECK_DATABASE(db); |
580 CHECK_DATABASE(db); |
|
581 const QString test3(qTableName("test3", __FILE__)); |
581 |
582 |
582 QTableView view; |
583 QTableView view; |
583 QCOMPARE(view.columnAt(0), -1); |
584 QCOMPARE(view.columnAt(0), -1); |
584 QCOMPARE(view.rowAt(0), -1); |
585 QCOMPARE(view.rowAt(0), -1); |
585 |
586 |
586 QSqlQueryModel model; |
587 QSqlQueryModel model; |
587 model.setQuery( "SELECT TOP 0 * FROM " + qTableName("test3"), db ); |
588 model.setQuery( "SELECT TOP 0 * FROM " + test3, db ); |
588 view.setModel(&model); |
589 view.setModel(&model); |
589 |
590 |
590 bool error = false; |
591 bool error = false; |
591 // Usually a syntax error |
592 // Usually a syntax error |
592 if (model.lastError().isValid()) // usually a syntax error |
593 if (model.lastError().isValid()) // usually a syntax error |
593 error = true; |
594 error = true; |
594 |
595 |
595 QCOMPARE(view.columnAt(0), (error)?-1:0 ); |
596 QCOMPARE(view.columnAt(0), (error)?-1:0 ); |
596 QCOMPARE(view.rowAt(0), -1); |
597 QCOMPARE(view.rowAt(0), -1); |
597 |
598 |
598 model.setQuery( "SELECT TOP 0 * FROM " + qTableName("test3"), db ); |
599 model.setQuery( "SELECT TOP 0 * FROM " + test3, db ); |
599 model.setQuery( "SELECT TOP 0 * FROM " + qTableName("test3"), db ); |
600 model.setQuery( "SELECT TOP 0 * FROM " + test3, db ); |
600 model.setQuery( "SELECT TOP 0 * FROM " + qTableName("test3"), db ); |
601 model.setQuery( "SELECT TOP 0 * FROM " + test3, db ); |
601 model.setQuery( "SELECT TOP 0 * FROM " + qTableName("test3"), db ); |
602 model.setQuery( "SELECT TOP 0 * FROM " + test3, db ); |
602 |
603 |
603 QCOMPARE(view.columnAt(0), (error)?-1:0 ); |
604 QCOMPARE(view.columnAt(0), (error)?-1:0 ); |
604 QCOMPARE(view.rowAt(0), -1); |
605 QCOMPARE(view.rowAt(0), -1); |
605 } |
606 } |
606 |
607 |