3 use Wikimedia\Rdbms\LikeMatch
;
6 * Test the abstract database layer
7 * This is a non DBMS depending test.
9 class DatabaseSQLTest
extends MediaWikiTestCase
{
10 /** @var DatabaseTestHelper */
13 protected function setUp() {
15 $this->database
= new DatabaseTestHelper( __CLASS__
, [ 'cliMode' => true ] );
18 protected function assertLastSql( $sqlText ) {
20 $this->database
->getLastSqls(),
25 protected function assertLastSqlDb( $sqlText, $db ) {
26 $this->assertEquals( $db->getLastSqls(), $sqlText );
30 * @dataProvider provideSelect
31 * @covers Database::select
33 public function testSelect( $sql, $sqlText ) {
34 $this->database
->select(
37 isset( $sql['conds'] ) ?
$sql['conds'] : [],
39 isset( $sql['options'] ) ?
$sql['options'] : [],
40 isset( $sql['join_conds'] ) ?
$sql['join_conds'] : []
42 $this->assertLastSql( $sqlText );
45 public static function provideSelect() {
50 'fields' => [ 'field', 'alias' => 'field2' ],
51 'conds' => [ 'alias' => 'text' ],
53 "SELECT field,field2 AS alias " .
55 "WHERE alias = 'text'"
60 'fields' => [ 'field', 'alias' => 'field2' ],
61 'conds' => [ 'alias' => 'text' ],
62 'options' => [ 'LIMIT' => 1, 'ORDER BY' => 'field' ],
64 "SELECT field,field2 AS alias " .
66 "WHERE alias = 'text' " .
72 'tables' => [ 'table', 't2' => 'table2' ],
73 'fields' => [ 'tid', 'field', 'alias' => 'field2', 't2.id' ],
74 'conds' => [ 'alias' => 'text' ],
75 'options' => [ 'LIMIT' => 1, 'ORDER BY' => 'field' ],
76 'join_conds' => [ 't2' => [
77 'LEFT JOIN', 'tid = t2.id'
80 "SELECT tid,field,field2 AS alias,t2.id " .
81 "FROM table LEFT JOIN table2 t2 ON ((tid = t2.id)) " .
82 "WHERE alias = 'text' " .
88 'tables' => [ 'table', 't2' => 'table2' ],
89 'fields' => [ 'tid', 'field', 'alias' => 'field2', 't2.id' ],
90 'conds' => [ 'alias' => 'text' ],
91 'options' => [ 'LIMIT' => 1, 'GROUP BY' => 'field', 'HAVING' => 'COUNT(*) > 1' ],
92 'join_conds' => [ 't2' => [
93 'LEFT JOIN', 'tid = t2.id'
96 "SELECT tid,field,field2 AS alias,t2.id " .
97 "FROM table LEFT JOIN table2 t2 ON ((tid = t2.id)) " .
98 "WHERE alias = 'text' " .
99 "GROUP BY field HAVING COUNT(*) > 1 " .
104 'tables' => [ 'table', 't2' => 'table2' ],
105 'fields' => [ 'tid', 'field', 'alias' => 'field2', 't2.id' ],
106 'conds' => [ 'alias' => 'text' ],
109 'GROUP BY' => [ 'field', 'field2' ],
110 'HAVING' => [ 'COUNT(*) > 1', 'field' => 1 ]
112 'join_conds' => [ 't2' => [
113 'LEFT JOIN', 'tid = t2.id'
116 "SELECT tid,field,field2 AS alias,t2.id " .
117 "FROM table LEFT JOIN table2 t2 ON ((tid = t2.id)) " .
118 "WHERE alias = 'text' " .
119 "GROUP BY field,field2 HAVING (COUNT(*) > 1) AND field = '1' " .
124 'tables' => [ 'table' ],
125 'fields' => [ 'alias' => 'field' ],
126 'conds' => [ 'alias' => [ 1, 2, 3, 4 ] ],
128 "SELECT field AS alias " .
130 "WHERE alias IN ('1','2','3','4')"
136 * @dataProvider provideUpdate
137 * @covers Database::update
139 public function testUpdate( $sql, $sqlText ) {
140 $this->database
->update(
145 isset( $sql['options'] ) ?
$sql['options'] : []
147 $this->assertLastSql( $sqlText );
150 public static function provideUpdate() {
155 'values' => [ 'field' => 'text', 'field2' => 'text2' ],
156 'conds' => [ 'alias' => 'text' ],
159 "SET field = 'text'" .
160 ",field2 = 'text2' " .
161 "WHERE alias = 'text'"
166 'values' => [ 'field = other', 'field2' => 'text2' ],
167 'conds' => [ 'id' => '1' ],
170 "SET field = other" .
171 ",field2 = 'text2' " .
177 'values' => [ 'field = other', 'field2' => 'text2' ],
181 "SET field = other" .
188 * @dataProvider provideDelete
189 * @covers Database::delete
191 public function testDelete( $sql, $sqlText ) {
192 $this->database
->delete(
197 $this->assertLastSql( $sqlText );
200 public static function provideDelete() {
205 'conds' => [ 'alias' => 'text' ],
207 "DELETE FROM table " .
208 "WHERE alias = 'text'"
221 * @dataProvider provideUpsert
222 * @covers Database::upsert
224 public function testUpsert( $sql, $sqlText ) {
225 $this->database
->upsert(
228 $sql['uniqueIndexes'],
232 $this->assertLastSql( $sqlText );
235 public static function provideUpsert() {
239 'table' => 'upsert_table',
240 'rows' => [ 'field' => 'text', 'field2' => 'text2' ],
241 'uniqueIndexes' => [ 'field' ],
242 'set' => [ 'field' => 'set' ],
245 "UPDATE upsert_table " .
246 "SET field = 'set' " .
247 "WHERE ((field = 'text')); " .
248 "INSERT IGNORE INTO upsert_table " .
250 "VALUES ('text','text2'); " .
257 * @dataProvider provideDeleteJoin
258 * @covers Database::deleteJoin
260 public function testDeleteJoin( $sql, $sqlText ) {
261 $this->database
->deleteJoin(
269 $this->assertLastSql( $sqlText );
272 public static function provideDeleteJoin() {
276 'delTable' => 'table',
277 'joinTable' => 'table_join',
279 'joinVar' => 'field_join',
280 'conds' => [ 'alias' => 'text' ],
282 "DELETE FROM table " .
284 "SELECT field_join FROM table_join WHERE alias = 'text'" .
289 'delTable' => 'table',
290 'joinTable' => 'table_join',
292 'joinVar' => 'field_join',
295 "DELETE FROM table " .
297 "SELECT field_join FROM table_join " .
304 * @dataProvider provideInsert
305 * @covers Database::insert
307 public function testInsert( $sql, $sqlText ) {
308 $this->database
->insert(
312 isset( $sql['options'] ) ?
$sql['options'] : []
314 $this->assertLastSql( $sqlText );
317 public static function provideInsert() {
322 'rows' => [ 'field' => 'text', 'field2' => 2 ],
324 "INSERT INTO table " .
326 "VALUES ('text','2')"
331 'rows' => [ 'field' => 'text', 'field2' => 2 ],
332 'options' => 'IGNORE',
334 "INSERT IGNORE INTO table " .
336 "VALUES ('text','2')"
342 [ 'field' => 'text', 'field2' => 2 ],
343 [ 'field' => 'multi', 'field2' => 3 ],
345 'options' => 'IGNORE',
347 "INSERT IGNORE INTO table " .
357 * @dataProvider provideInsertSelect
358 * @covers Database::insertSelect
360 public function testInsertSelect( $sql, $sqlTextNative, $sqlSelect, $sqlInsert ) {
361 $this->database
->insertSelect(
367 isset( $sql['insertOptions'] ) ?
$sql['insertOptions'] : [],
368 isset( $sql['selectOptions'] ) ?
$sql['selectOptions'] : []
370 $this->assertLastSql( $sqlTextNative );
372 $dbWeb = new DatabaseTestHelper( __CLASS__
, [ 'cliMode' => false ] );
373 $dbWeb->forceNextResult( [
374 array_flip( array_keys( $sql['varMap'] ) )
376 $dbWeb->insertSelect(
382 isset( $sql['insertOptions'] ) ?
$sql['insertOptions'] : [],
383 isset( $sql['selectOptions'] ) ?
$sql['selectOptions'] : []
385 $this->assertLastSqlDb( implode( '; ', [ $sqlSelect, $sqlInsert ] ), $dbWeb );
388 public static function provideInsertSelect() {
392 'destTable' => 'insert_table',
393 'srcTable' => 'select_table',
394 'varMap' => [ 'field_insert' => 'field_select', 'field' => 'field2' ],
397 "INSERT INTO insert_table " .
398 "(field_insert,field) " .
399 "SELECT field_select,field2 " .
401 "SELECT field_select AS field_insert,field2 AS field " .
402 "FROM select_table WHERE * FOR UPDATE",
403 "INSERT INTO insert_table (field_insert,field) VALUES ('0','1')"
407 'destTable' => 'insert_table',
408 'srcTable' => 'select_table',
409 'varMap' => [ 'field_insert' => 'field_select', 'field' => 'field2' ],
410 'conds' => [ 'field' => 2 ],
412 "INSERT INTO insert_table " .
413 "(field_insert,field) " .
414 "SELECT field_select,field2 " .
415 "FROM select_table " .
417 "SELECT field_select AS field_insert,field2 AS field FROM " .
418 "select_table WHERE field = '2' FOR UPDATE",
419 "INSERT INTO insert_table (field_insert,field) VALUES ('0','1')"
423 'destTable' => 'insert_table',
424 'srcTable' => 'select_table',
425 'varMap' => [ 'field_insert' => 'field_select', 'field' => 'field2' ],
426 'conds' => [ 'field' => 2 ],
427 'insertOptions' => 'IGNORE',
428 'selectOptions' => [ 'ORDER BY' => 'field' ],
430 "INSERT IGNORE INTO insert_table " .
431 "(field_insert,field) " .
432 "SELECT field_select,field2 " .
433 "FROM select_table " .
434 "WHERE field = '2' " .
436 "SELECT field_select AS field_insert,field2 AS field " .
437 "FROM select_table WHERE field = '2' ORDER BY field FOR UPDATE",
438 "INSERT IGNORE INTO insert_table (field_insert,field) VALUES ('0','1')"
444 * @dataProvider provideReplace
445 * @covers Database::replace
447 public function testReplace( $sql, $sqlText ) {
448 $this->database
->replace(
450 $sql['uniqueIndexes'],
454 $this->assertLastSql( $sqlText );
457 public static function provideReplace() {
461 'table' => 'replace_table',
462 'uniqueIndexes' => [ 'field' ],
463 'rows' => [ 'field' => 'text', 'field2' => 'text2' ],
465 "DELETE FROM replace_table " .
466 "WHERE ( field='text' ); " .
467 "INSERT INTO replace_table " .
469 "VALUES ('text','text2')"
473 'table' => 'module_deps',
474 'uniqueIndexes' => [ [ 'md_module', 'md_skin' ] ],
476 'md_module' => 'module',
481 "DELETE FROM module_deps " .
482 "WHERE ( md_module='module' AND md_skin='skin' ); " .
483 "INSERT INTO module_deps " .
484 "(md_module,md_skin,md_deps) " .
485 "VALUES ('module','skin','deps')"
489 'table' => 'module_deps',
490 'uniqueIndexes' => [ [ 'md_module', 'md_skin' ] ],
493 'md_module' => 'module',
497 'md_module' => 'module2',
498 'md_skin' => 'skin2',
499 'md_deps' => 'deps2',
503 "DELETE FROM module_deps " .
504 "WHERE ( md_module='module' AND md_skin='skin' ); " .
505 "INSERT INTO module_deps " .
506 "(md_module,md_skin,md_deps) " .
507 "VALUES ('module','skin','deps'); " .
508 "DELETE FROM module_deps " .
509 "WHERE ( md_module='module2' AND md_skin='skin2' ); " .
510 "INSERT INTO module_deps " .
511 "(md_module,md_skin,md_deps) " .
512 "VALUES ('module2','skin2','deps2')"
516 'table' => 'module_deps',
517 'uniqueIndexes' => [ 'md_module', 'md_skin' ],
520 'md_module' => 'module',
524 'md_module' => 'module2',
525 'md_skin' => 'skin2',
526 'md_deps' => 'deps2',
530 "DELETE FROM module_deps " .
531 "WHERE ( md_module='module' ) OR ( md_skin='skin' ); " .
532 "INSERT INTO module_deps " .
533 "(md_module,md_skin,md_deps) " .
534 "VALUES ('module','skin','deps'); " .
535 "DELETE FROM module_deps " .
536 "WHERE ( md_module='module2' ) OR ( md_skin='skin2' ); " .
537 "INSERT INTO module_deps " .
538 "(md_module,md_skin,md_deps) " .
539 "VALUES ('module2','skin2','deps2')"
543 'table' => 'module_deps',
544 'uniqueIndexes' => [],
546 'md_module' => 'module',
551 "INSERT INTO module_deps " .
552 "(md_module,md_skin,md_deps) " .
553 "VALUES ('module','skin','deps')"
559 * @dataProvider provideNativeReplace
560 * @covers Database::nativeReplace
562 public function testNativeReplace( $sql, $sqlText ) {
563 $this->database
->nativeReplace(
568 $this->assertLastSql( $sqlText );
571 public static function provideNativeReplace() {
575 'table' => 'replace_table',
576 'rows' => [ 'field' => 'text', 'field2' => 'text2' ],
578 "REPLACE INTO replace_table " .
580 "VALUES ('text','text2')"
586 * @dataProvider provideConditional
587 * @covers Database::conditional
589 public function testConditional( $sql, $sqlText ) {
590 $this->assertEquals( trim( $this->database
->conditional(
597 public static function provideConditional() {
601 'conds' => [ 'field' => 'text' ],
605 "(CASE WHEN field = 'text' THEN 1 ELSE NULL END)"
609 'conds' => [ 'field' => 'text', 'field2' => 'anothertext' ],
613 "(CASE WHEN field = 'text' AND field2 = 'anothertext' THEN 1 ELSE NULL END)"
617 'conds' => 'field=1',
621 "(CASE WHEN field=1 THEN 1 ELSE NULL END)"
627 * @dataProvider provideBuildConcat
628 * @covers Database::buildConcat
630 public function testBuildConcat( $stringList, $sqlText ) {
631 $this->assertEquals( trim( $this->database
->buildConcat(
636 public static function provideBuildConcat() {
639 [ 'field', 'field2' ],
640 "CONCAT(field,field2)"
643 [ "'test'", 'field2' ],
644 "CONCAT('test',field2)"
650 * @dataProvider provideBuildLike
651 * @covers Database::buildLike
653 public function testBuildLike( $array, $sqlText ) {
654 $this->assertEquals( trim( $this->database
->buildLike(
659 public static function provideBuildLike() {
666 [ 'text', new LikeMatch( '%' ) ],
670 [ 'text', new LikeMatch( '%' ), 'text2' ],
674 [ 'text', new LikeMatch( '_' ) ],
681 * @dataProvider provideUnionQueries
682 * @covers Database::unionQueries
684 public function testUnionQueries( $sql, $sqlText ) {
685 $this->assertEquals( trim( $this->database
->unionQueries(
691 public static function provideUnionQueries() {
695 'sqls' => [ 'RAW SQL', 'RAW2SQL' ],
698 "(RAW SQL) UNION ALL (RAW2SQL)"
702 'sqls' => [ 'RAW SQL', 'RAW2SQL' ],
705 "(RAW SQL) UNION (RAW2SQL)"
709 'sqls' => [ 'RAW SQL', 'RAW2SQL', 'RAW3SQL' ],
712 "(RAW SQL) UNION (RAW2SQL) UNION (RAW3SQL)"
718 * @covers Database::commit
720 public function testTransactionCommit() {
721 $this->database
->begin( __METHOD__
);
722 $this->database
->commit( __METHOD__
);
723 $this->assertLastSql( 'BEGIN; COMMIT' );
727 * @covers Database::rollback
729 public function testTransactionRollback() {
730 $this->database
->begin( __METHOD__
);
731 $this->database
->rollback( __METHOD__
);
732 $this->assertLastSql( 'BEGIN; ROLLBACK' );
736 * @covers Database::dropTable
738 public function testDropTable() {
739 $this->database
->setExistingTables( [ 'table' ] );
740 $this->database
->dropTable( 'table', __METHOD__
);
741 $this->assertLastSql( 'DROP TABLE table CASCADE' );
745 * @covers Database::dropTable
747 public function testDropNonExistingTable() {
749 $this->database
->dropTable( 'non_existing', __METHOD__
)
754 * @dataProvider provideMakeList
755 * @covers Database::makeList
757 public function testMakeList( $list, $mode, $sqlText ) {
758 $this->assertEquals( trim( $this->database
->makeList(
763 public static function provideMakeList() {
766 [ 'value', 'value2' ],
771 [ 'field', 'field2' ],
776 [ 'field' => 'value', 'field2' => 'value2' ],
778 "field = 'value' AND field2 = 'value2'"
781 [ 'field' => null, "field2 != 'value2'" ],
783 "field IS NULL AND (field2 != 'value2')"
786 [ 'field' => [ 'value', null, 'value2' ], 'field2' => 'value2' ],
788 "(field IN ('value','value2') OR field IS NULL) AND field2 = 'value2'"
791 [ 'field' => [ null ], 'field2' => null ],
793 "field IS NULL AND field2 IS NULL"
796 [ 'field' => 'value', 'field2' => 'value2' ],
798 "field = 'value' OR field2 = 'value2'"
801 [ 'field' => 'value', 'field2' => null ],
803 "field = 'value' OR field2 IS NULL"
806 [ 'field' => [ 'value', 'value2' ], 'field2' => [ 'value' ] ],
808 "field IN ('value','value2') OR field2 = 'value'"
811 [ 'field' => [ null, 'value', null, 'value2' ], "field2 != 'value2'" ],
813 "(field IN ('value','value2') OR field IS NULL) OR (field2 != 'value2')"
816 [ 'field' => 'value', 'field2' => 'value2' ],
818 "field = 'value',field2 = 'value2'"
821 [ 'field' => 'value', 'field2' => null ],
823 "field = 'value',field2 = NULL"
826 [ 'field' => 'value', "field2 != 'value2'" ],
828 "field = 'value',field2 != 'value2'"
833 public function testSessionTempTables() {
834 $temp1 = $this->database
->tableName( 'tmp_table_1' );
835 $temp2 = $this->database
->tableName( 'tmp_table_2' );
836 $temp3 = $this->database
->tableName( 'tmp_table_3' );
838 $this->database
->query( "CREATE TEMPORARY TABLE $temp1 LIKE orig_tbl", __METHOD__
);
839 $this->database
->query( "CREATE TEMPORARY TABLE $temp2 LIKE orig_tbl", __METHOD__
);
840 $this->database
->query( "CREATE TEMPORARY TABLE $temp3 LIKE orig_tbl", __METHOD__
);
842 $this->assertTrue( $this->database
->tableExists( "tmp_table_1", __METHOD__
) );
843 $this->assertTrue( $this->database
->tableExists( "tmp_table_2", __METHOD__
) );
844 $this->assertTrue( $this->database
->tableExists( "tmp_table_3", __METHOD__
) );
846 $this->database
->dropTable( 'tmp_table_1', __METHOD__
);
847 $this->database
->dropTable( 'tmp_table_2', __METHOD__
);
848 $this->database
->dropTable( 'tmp_table_3', __METHOD__
);
850 $this->assertFalse( $this->database
->tableExists( "tmp_table_1", __METHOD__
) );
851 $this->assertFalse( $this->database
->tableExists( "tmp_table_2", __METHOD__
) );
852 $this->assertFalse( $this->database
->tableExists( "tmp_table_3", __METHOD__
) );
854 $this->database
->query( "CREATE TEMPORARY TABLE tmp_table_1 LIKE orig_tbl", __METHOD__
);
855 $this->database
->query( "CREATE TEMPORARY TABLE 'tmp_table_2' LIKE orig_tbl", __METHOD__
);
856 $this->database
->query( "CREATE TEMPORARY TABLE `tmp_table_3` LIKE orig_tbl", __METHOD__
);
858 $this->assertTrue( $this->database
->tableExists( "tmp_table_1", __METHOD__
) );
859 $this->assertTrue( $this->database
->tableExists( "tmp_table_2", __METHOD__
) );
860 $this->assertTrue( $this->database
->tableExists( "tmp_table_3", __METHOD__
) );
862 $this->database
->query( "DROP TEMPORARY TABLE tmp_table_1 LIKE orig_tbl", __METHOD__
);
863 $this->database
->query( "DROP TEMPORARY TABLE 'tmp_table_2' LIKE orig_tbl", __METHOD__
);
864 $this->database
->query( "DROP TABLE `tmp_table_3` LIKE orig_tbl", __METHOD__
);
866 $this->assertFalse( $this->database
->tableExists( "tmp_table_1", __METHOD__
) );
867 $this->assertFalse( $this->database
->tableExists( "tmp_table_2", __METHOD__
) );
868 $this->assertFalse( $this->database
->tableExists( "tmp_table_3", __METHOD__
) );