4 * Test the abstract database layer
5 * This is a non DBMS depending test.
7 class DatabaseSQLTest
extends MediaWikiTestCase
{
8 /** @var DatabaseTestHelper */
11 protected function setUp() {
13 $this->database
= new DatabaseTestHelper( __CLASS__
, [ 'cliMode' => true ] );
16 protected function assertLastSql( $sqlText ) {
18 $this->database
->getLastSqls(),
23 protected function assertLastSqlDb( $sqlText, $db ) {
24 $this->assertEquals( $db->getLastSqls(), $sqlText );
28 * @dataProvider provideSelect
29 * @covers Database::select
31 public function testSelect( $sql, $sqlText ) {
32 $this->database
->select(
35 isset( $sql['conds'] ) ?
$sql['conds'] : [],
37 isset( $sql['options'] ) ?
$sql['options'] : [],
38 isset( $sql['join_conds'] ) ?
$sql['join_conds'] : []
40 $this->assertLastSql( $sqlText );
43 public static function provideSelect() {
48 'fields' => [ 'field', 'alias' => 'field2' ],
49 'conds' => [ 'alias' => 'text' ],
51 "SELECT field,field2 AS alias " .
53 "WHERE alias = 'text'"
58 'fields' => [ 'field', 'alias' => 'field2' ],
59 'conds' => [ 'alias' => 'text' ],
60 'options' => [ 'LIMIT' => 1, 'ORDER BY' => 'field' ],
62 "SELECT field,field2 AS alias " .
64 "WHERE alias = 'text' " .
70 'tables' => [ 'table', 't2' => 'table2' ],
71 'fields' => [ 'tid', 'field', 'alias' => 'field2', 't2.id' ],
72 'conds' => [ 'alias' => 'text' ],
73 'options' => [ 'LIMIT' => 1, 'ORDER BY' => 'field' ],
74 'join_conds' => [ 't2' => [
75 'LEFT JOIN', 'tid = t2.id'
78 "SELECT tid,field,field2 AS alias,t2.id " .
79 "FROM table LEFT JOIN table2 t2 ON ((tid = t2.id)) " .
80 "WHERE alias = 'text' " .
86 'tables' => [ 'table', 't2' => 'table2' ],
87 'fields' => [ 'tid', 'field', 'alias' => 'field2', 't2.id' ],
88 'conds' => [ 'alias' => 'text' ],
89 'options' => [ 'LIMIT' => 1, 'GROUP BY' => 'field', 'HAVING' => 'COUNT(*) > 1' ],
90 'join_conds' => [ 't2' => [
91 'LEFT JOIN', 'tid = t2.id'
94 "SELECT tid,field,field2 AS alias,t2.id " .
95 "FROM table LEFT JOIN table2 t2 ON ((tid = t2.id)) " .
96 "WHERE alias = 'text' " .
97 "GROUP BY field HAVING COUNT(*) > 1 " .
102 'tables' => [ 'table', 't2' => 'table2' ],
103 'fields' => [ 'tid', 'field', 'alias' => 'field2', 't2.id' ],
104 'conds' => [ 'alias' => 'text' ],
107 'GROUP BY' => [ 'field', 'field2' ],
108 'HAVING' => [ 'COUNT(*) > 1', 'field' => 1 ]
110 'join_conds' => [ 't2' => [
111 'LEFT JOIN', 'tid = t2.id'
114 "SELECT tid,field,field2 AS alias,t2.id " .
115 "FROM table LEFT JOIN table2 t2 ON ((tid = t2.id)) " .
116 "WHERE alias = 'text' " .
117 "GROUP BY field,field2 HAVING (COUNT(*) > 1) AND field = '1' " .
122 'tables' => [ 'table' ],
123 'fields' => [ 'alias' => 'field' ],
124 'conds' => [ 'alias' => [ 1, 2, 3, 4 ] ],
126 "SELECT field AS alias " .
128 "WHERE alias IN ('1','2','3','4')"
134 * @dataProvider provideUpdate
135 * @covers Database::update
137 public function testUpdate( $sql, $sqlText ) {
138 $this->database
->update(
143 isset( $sql['options'] ) ?
$sql['options'] : []
145 $this->assertLastSql( $sqlText );
148 public static function provideUpdate() {
153 'values' => [ 'field' => 'text', 'field2' => 'text2' ],
154 'conds' => [ 'alias' => 'text' ],
157 "SET field = 'text'" .
158 ",field2 = 'text2' " .
159 "WHERE alias = 'text'"
164 'values' => [ 'field = other', 'field2' => 'text2' ],
165 'conds' => [ 'id' => '1' ],
168 "SET field = other" .
169 ",field2 = 'text2' " .
175 'values' => [ 'field = other', 'field2' => 'text2' ],
179 "SET field = other" .
186 * @dataProvider provideDelete
187 * @covers Database::delete
189 public function testDelete( $sql, $sqlText ) {
190 $this->database
->delete(
195 $this->assertLastSql( $sqlText );
198 public static function provideDelete() {
203 'conds' => [ 'alias' => 'text' ],
205 "DELETE FROM table " .
206 "WHERE alias = 'text'"
219 * @dataProvider provideUpsert
220 * @covers Database::upsert
222 public function testUpsert( $sql, $sqlText ) {
223 $this->database
->upsert(
226 $sql['uniqueIndexes'],
230 $this->assertLastSql( $sqlText );
233 public static function provideUpsert() {
237 'table' => 'upsert_table',
238 'rows' => [ 'field' => 'text', 'field2' => 'text2' ],
239 'uniqueIndexes' => [ 'field' ],
240 'set' => [ 'field' => 'set' ],
243 "UPDATE upsert_table " .
244 "SET field = 'set' " .
245 "WHERE ((field = 'text')); " .
246 "INSERT IGNORE INTO upsert_table " .
248 "VALUES ('text','text2'); " .
255 * @dataProvider provideDeleteJoin
256 * @covers Database::deleteJoin
258 public function testDeleteJoin( $sql, $sqlText ) {
259 $this->database
->deleteJoin(
267 $this->assertLastSql( $sqlText );
270 public static function provideDeleteJoin() {
274 'delTable' => 'table',
275 'joinTable' => 'table_join',
277 'joinVar' => 'field_join',
278 'conds' => [ 'alias' => 'text' ],
280 "DELETE FROM table " .
282 "SELECT field_join FROM table_join WHERE alias = 'text'" .
287 'delTable' => 'table',
288 'joinTable' => 'table_join',
290 'joinVar' => 'field_join',
293 "DELETE FROM table " .
295 "SELECT field_join FROM table_join " .
302 * @dataProvider provideInsert
303 * @covers Database::insert
305 public function testInsert( $sql, $sqlText ) {
306 $this->database
->insert(
310 isset( $sql['options'] ) ?
$sql['options'] : []
312 $this->assertLastSql( $sqlText );
315 public static function provideInsert() {
320 'rows' => [ 'field' => 'text', 'field2' => 2 ],
322 "INSERT INTO table " .
324 "VALUES ('text','2')"
329 'rows' => [ 'field' => 'text', 'field2' => 2 ],
330 'options' => 'IGNORE',
332 "INSERT IGNORE INTO table " .
334 "VALUES ('text','2')"
340 [ 'field' => 'text', 'field2' => 2 ],
341 [ 'field' => 'multi', 'field2' => 3 ],
343 'options' => 'IGNORE',
345 "INSERT IGNORE INTO table " .
355 * @dataProvider provideInsertSelect
356 * @covers Database::insertSelect
358 public function testInsertSelect( $sql, $sqlTextNative, $sqlSelect, $sqlInsert ) {
359 $this->database
->insertSelect(
365 isset( $sql['insertOptions'] ) ?
$sql['insertOptions'] : [],
366 isset( $sql['selectOptions'] ) ?
$sql['selectOptions'] : []
368 $this->assertLastSql( $sqlTextNative );
370 $dbWeb = new DatabaseTestHelper( __CLASS__
, [ 'cliMode' => false ] );
371 $dbWeb->forceNextResult( [
372 array_flip( array_keys( $sql['varMap'] ) )
374 $dbWeb->insertSelect(
380 isset( $sql['insertOptions'] ) ?
$sql['insertOptions'] : [],
381 isset( $sql['selectOptions'] ) ?
$sql['selectOptions'] : []
383 $this->assertLastSqlDb( implode( '; ', [ $sqlSelect, $sqlInsert ] ), $dbWeb );
386 public static function provideInsertSelect() {
390 'destTable' => 'insert_table',
391 'srcTable' => 'select_table',
392 'varMap' => [ 'field_insert' => 'field_select', 'field' => 'field2' ],
395 "INSERT INTO insert_table " .
396 "(field_insert,field) " .
397 "SELECT field_select,field2 " .
399 "SELECT field_select AS field_insert,field2 AS field " .
400 "FROM select_table WHERE * FOR UPDATE",
401 "INSERT INTO insert_table (field_insert,field) VALUES ('0','1')"
405 'destTable' => 'insert_table',
406 'srcTable' => 'select_table',
407 'varMap' => [ 'field_insert' => 'field_select', 'field' => 'field2' ],
408 'conds' => [ 'field' => 2 ],
410 "INSERT INTO insert_table " .
411 "(field_insert,field) " .
412 "SELECT field_select,field2 " .
413 "FROM select_table " .
415 "SELECT field_select AS field_insert,field2 AS field FROM " .
416 "select_table WHERE field = '2' FOR UPDATE",
417 "INSERT INTO insert_table (field_insert,field) VALUES ('0','1')"
421 'destTable' => 'insert_table',
422 'srcTable' => 'select_table',
423 'varMap' => [ 'field_insert' => 'field_select', 'field' => 'field2' ],
424 'conds' => [ 'field' => 2 ],
425 'insertOptions' => 'IGNORE',
426 'selectOptions' => [ 'ORDER BY' => 'field' ],
428 "INSERT IGNORE INTO insert_table " .
429 "(field_insert,field) " .
430 "SELECT field_select,field2 " .
431 "FROM select_table " .
432 "WHERE field = '2' " .
434 "SELECT field_select AS field_insert,field2 AS field " .
435 "FROM select_table WHERE field = '2' ORDER BY field FOR UPDATE",
436 "INSERT IGNORE INTO insert_table (field_insert,field) VALUES ('0','1')"
442 * @dataProvider provideReplace
443 * @covers Database::replace
445 public function testReplace( $sql, $sqlText ) {
446 $this->database
->replace(
448 $sql['uniqueIndexes'],
452 $this->assertLastSql( $sqlText );
455 public static function provideReplace() {
459 'table' => 'replace_table',
460 'uniqueIndexes' => [ 'field' ],
461 'rows' => [ 'field' => 'text', 'field2' => 'text2' ],
463 "DELETE FROM replace_table " .
464 "WHERE ( field='text' ); " .
465 "INSERT INTO replace_table " .
467 "VALUES ('text','text2')"
471 'table' => 'module_deps',
472 'uniqueIndexes' => [ [ 'md_module', 'md_skin' ] ],
474 'md_module' => 'module',
479 "DELETE FROM module_deps " .
480 "WHERE ( md_module='module' AND md_skin='skin' ); " .
481 "INSERT INTO module_deps " .
482 "(md_module,md_skin,md_deps) " .
483 "VALUES ('module','skin','deps')"
487 'table' => 'module_deps',
488 'uniqueIndexes' => [ [ 'md_module', 'md_skin' ] ],
491 'md_module' => 'module',
495 'md_module' => 'module2',
496 'md_skin' => 'skin2',
497 'md_deps' => 'deps2',
501 "DELETE FROM module_deps " .
502 "WHERE ( md_module='module' AND md_skin='skin' ); " .
503 "INSERT INTO module_deps " .
504 "(md_module,md_skin,md_deps) " .
505 "VALUES ('module','skin','deps'); " .
506 "DELETE FROM module_deps " .
507 "WHERE ( md_module='module2' AND md_skin='skin2' ); " .
508 "INSERT INTO module_deps " .
509 "(md_module,md_skin,md_deps) " .
510 "VALUES ('module2','skin2','deps2')"
514 'table' => 'module_deps',
515 'uniqueIndexes' => [ 'md_module', 'md_skin' ],
518 'md_module' => 'module',
522 'md_module' => 'module2',
523 'md_skin' => 'skin2',
524 'md_deps' => 'deps2',
528 "DELETE FROM module_deps " .
529 "WHERE ( md_module='module' ) OR ( md_skin='skin' ); " .
530 "INSERT INTO module_deps " .
531 "(md_module,md_skin,md_deps) " .
532 "VALUES ('module','skin','deps'); " .
533 "DELETE FROM module_deps " .
534 "WHERE ( md_module='module2' ) OR ( md_skin='skin2' ); " .
535 "INSERT INTO module_deps " .
536 "(md_module,md_skin,md_deps) " .
537 "VALUES ('module2','skin2','deps2')"
541 'table' => 'module_deps',
542 'uniqueIndexes' => [],
544 'md_module' => 'module',
549 "INSERT INTO module_deps " .
550 "(md_module,md_skin,md_deps) " .
551 "VALUES ('module','skin','deps')"
557 * @dataProvider provideNativeReplace
558 * @covers Database::nativeReplace
560 public function testNativeReplace( $sql, $sqlText ) {
561 $this->database
->nativeReplace(
566 $this->assertLastSql( $sqlText );
569 public static function provideNativeReplace() {
573 'table' => 'replace_table',
574 'rows' => [ 'field' => 'text', 'field2' => 'text2' ],
576 "REPLACE INTO replace_table " .
578 "VALUES ('text','text2')"
584 * @dataProvider provideConditional
585 * @covers Database::conditional
587 public function testConditional( $sql, $sqlText ) {
588 $this->assertEquals( trim( $this->database
->conditional(
595 public static function provideConditional() {
599 'conds' => [ 'field' => 'text' ],
603 "(CASE WHEN field = 'text' THEN 1 ELSE NULL END)"
607 'conds' => [ 'field' => 'text', 'field2' => 'anothertext' ],
611 "(CASE WHEN field = 'text' AND field2 = 'anothertext' THEN 1 ELSE NULL END)"
615 'conds' => 'field=1',
619 "(CASE WHEN field=1 THEN 1 ELSE NULL END)"
625 * @dataProvider provideBuildConcat
626 * @covers Database::buildConcat
628 public function testBuildConcat( $stringList, $sqlText ) {
629 $this->assertEquals( trim( $this->database
->buildConcat(
634 public static function provideBuildConcat() {
637 [ 'field', 'field2' ],
638 "CONCAT(field,field2)"
641 [ "'test'", 'field2' ],
642 "CONCAT('test',field2)"
648 * @dataProvider provideBuildLike
649 * @covers Database::buildLike
651 public function testBuildLike( $array, $sqlText ) {
652 $this->assertEquals( trim( $this->database
->buildLike(
657 public static function provideBuildLike() {
664 [ 'text', new LikeMatch( '%' ) ],
668 [ 'text', new LikeMatch( '%' ), 'text2' ],
672 [ 'text', new LikeMatch( '_' ) ],
679 * @dataProvider provideUnionQueries
680 * @covers Database::unionQueries
682 public function testUnionQueries( $sql, $sqlText ) {
683 $this->assertEquals( trim( $this->database
->unionQueries(
689 public static function provideUnionQueries() {
693 'sqls' => [ 'RAW SQL', 'RAW2SQL' ],
696 "(RAW SQL) UNION ALL (RAW2SQL)"
700 'sqls' => [ 'RAW SQL', 'RAW2SQL' ],
703 "(RAW SQL) UNION (RAW2SQL)"
707 'sqls' => [ 'RAW SQL', 'RAW2SQL', 'RAW3SQL' ],
710 "(RAW SQL) UNION (RAW2SQL) UNION (RAW3SQL)"
716 * @covers Database::commit
718 public function testTransactionCommit() {
719 $this->database
->begin( __METHOD__
);
720 $this->database
->commit( __METHOD__
);
721 $this->assertLastSql( 'BEGIN; COMMIT' );
725 * @covers Database::rollback
727 public function testTransactionRollback() {
728 $this->database
->begin( __METHOD__
);
729 $this->database
->rollback( __METHOD__
);
730 $this->assertLastSql( 'BEGIN; ROLLBACK' );
734 * @covers Database::dropTable
736 public function testDropTable() {
737 $this->database
->setExistingTables( [ 'table' ] );
738 $this->database
->dropTable( 'table', __METHOD__
);
739 $this->assertLastSql( 'DROP TABLE table CASCADE' );
743 * @covers Database::dropTable
745 public function testDropNonExistingTable() {
747 $this->database
->dropTable( 'non_existing', __METHOD__
)
752 * @dataProvider provideMakeList
753 * @covers Database::makeList
755 public function testMakeList( $list, $mode, $sqlText ) {
756 $this->assertEquals( trim( $this->database
->makeList(
761 public static function provideMakeList() {
764 [ 'value', 'value2' ],
769 [ 'field', 'field2' ],
774 [ 'field' => 'value', 'field2' => 'value2' ],
776 "field = 'value' AND field2 = 'value2'"
779 [ 'field' => null, "field2 != 'value2'" ],
781 "field IS NULL AND (field2 != 'value2')"
784 [ 'field' => [ 'value', null, 'value2' ], 'field2' => 'value2' ],
786 "(field IN ('value','value2') OR field IS NULL) AND field2 = 'value2'"
789 [ 'field' => [ null ], 'field2' => null ],
791 "field IS NULL AND field2 IS NULL"
794 [ 'field' => 'value', 'field2' => 'value2' ],
796 "field = 'value' OR field2 = 'value2'"
799 [ 'field' => 'value', 'field2' => null ],
801 "field = 'value' OR field2 IS NULL"
804 [ 'field' => [ 'value', 'value2' ], 'field2' => [ 'value' ] ],
806 "field IN ('value','value2') OR field2 = 'value'"
809 [ 'field' => [ null, 'value', null, 'value2' ], "field2 != 'value2'" ],
811 "(field IN ('value','value2') OR field IS NULL) OR (field2 != 'value2')"
814 [ 'field' => 'value', 'field2' => 'value2' ],
816 "field = 'value',field2 = 'value2'"
819 [ 'field' => 'value', 'field2' => null ],
821 "field = 'value',field2 = NULL"
824 [ 'field' => 'value', "field2 != 'value2'" ],
826 "field = 'value',field2 != 'value2'"
831 public function testSessionTempTables() {
832 $temp1 = $this->database
->tableName( 'tmp_table_1' );
833 $temp2 = $this->database
->tableName( 'tmp_table_2' );
834 $temp3 = $this->database
->tableName( 'tmp_table_3' );
836 $this->database
->query( "CREATE TEMPORARY TABLE $temp1 LIKE orig_tbl", __METHOD__
);
837 $this->database
->query( "CREATE TEMPORARY TABLE $temp2 LIKE orig_tbl", __METHOD__
);
838 $this->database
->query( "CREATE TEMPORARY TABLE $temp3 LIKE orig_tbl", __METHOD__
);
840 $this->assertTrue( $this->database
->tableExists( "tmp_table_1", __METHOD__
) );
841 $this->assertTrue( $this->database
->tableExists( "tmp_table_2", __METHOD__
) );
842 $this->assertTrue( $this->database
->tableExists( "tmp_table_3", __METHOD__
) );
844 $this->database
->dropTable( 'tmp_table_1', __METHOD__
);
845 $this->database
->dropTable( 'tmp_table_2', __METHOD__
);
846 $this->database
->dropTable( 'tmp_table_3', __METHOD__
);
848 $this->assertFalse( $this->database
->tableExists( "tmp_table_1", __METHOD__
) );
849 $this->assertFalse( $this->database
->tableExists( "tmp_table_2", __METHOD__
) );
850 $this->assertFalse( $this->database
->tableExists( "tmp_table_3", __METHOD__
) );
852 $this->database
->query( "CREATE TEMPORARY TABLE tmp_table_1 LIKE orig_tbl", __METHOD__
);
853 $this->database
->query( "CREATE TEMPORARY TABLE 'tmp_table_2' LIKE orig_tbl", __METHOD__
);
854 $this->database
->query( "CREATE TEMPORARY TABLE `tmp_table_3` LIKE orig_tbl", __METHOD__
);
856 $this->assertTrue( $this->database
->tableExists( "tmp_table_1", __METHOD__
) );
857 $this->assertTrue( $this->database
->tableExists( "tmp_table_2", __METHOD__
) );
858 $this->assertTrue( $this->database
->tableExists( "tmp_table_3", __METHOD__
) );
860 $this->database
->query( "DROP TEMPORARY TABLE tmp_table_1 LIKE orig_tbl", __METHOD__
);
861 $this->database
->query( "DROP TEMPORARY TABLE 'tmp_table_2' LIKE orig_tbl", __METHOD__
);
862 $this->database
->query( "DROP TABLE `tmp_table_3` LIKE orig_tbl", __METHOD__
);
864 $this->assertFalse( $this->database
->tableExists( "tmp_table_1", __METHOD__
) );
865 $this->assertFalse( $this->database
->tableExists( "tmp_table_2", __METHOD__
) );
866 $this->assertFalse( $this->database
->tableExists( "tmp_table_3", __METHOD__
) );