3 use Wikimedia\Rdbms\LikeMatch
;
6 * Test the parts of the Database abstract class that deal
7 * with creating SQL text.
9 class DatabaseSQLTest
extends PHPUnit_Framework_TestCase
{
10 /** @var DatabaseTestHelper */
13 protected function setUp() {
15 $this->database
= new DatabaseTestHelper( __CLASS__
, [ 'cliMode' => true ] );
18 protected function assertLastSql( $sqlText ) {
21 $this->database
->getLastSqls()
25 protected function assertLastSqlDb( $sqlText, DatabaseTestHelper
$db ) {
26 $this->assertEquals( $sqlText, $db->getLastSqls() );
30 * @dataProvider provideSelect
31 * @covers Wikimedia\Rdbms\Database::select
32 * @covers Wikimedia\Rdbms\Database::selectSQLText
33 * @covers Wikimedia\Rdbms\Database::tableNamesWithIndexClauseOrJOIN
34 * @covers Wikimedia\Rdbms\Database::useIndexClause
35 * @covers Wikimedia\Rdbms\Database::ignoreIndexClause
36 * @covers Wikimedia\Rdbms\Database::makeSelectOptions
37 * @covers Wikimedia\Rdbms\Database::makeOrderBy
38 * @covers Wikimedia\Rdbms\Database::makeGroupByWithHaving
40 public function testSelect( $sql, $sqlText ) {
41 $this->database
->select(
44 isset( $sql['conds'] ) ?
$sql['conds'] : [],
46 isset( $sql['options'] ) ?
$sql['options'] : [],
47 isset( $sql['join_conds'] ) ?
$sql['join_conds'] : []
49 $this->assertLastSql( $sqlText );
52 public static function provideSelect() {
57 'fields' => [ 'field', 'alias' => 'field2' ],
58 'conds' => [ 'alias' => 'text' ],
60 "SELECT field,field2 AS alias " .
62 "WHERE alias = 'text'"
66 // 'tables' with space prepended indicates pre-escaped table name
67 'tables' => ' table LEFT JOIN table2',
68 'fields' => [ 'field' ],
69 'conds' => [ 'field' => 'text' ],
71 "SELECT field FROM table LEFT JOIN table2 WHERE field = 'text'"
75 // Empty 'tables' is allowed
77 'fields' => [ 'SPECIAL_QUERY()' ],
79 "SELECT SPECIAL_QUERY()"
84 'fields' => [ 'field', 'alias' => 'field2' ],
85 'conds' => [ 'alias' => 'text' ],
86 'options' => [ 'LIMIT' => 1, 'ORDER BY' => 'field' ],
88 "SELECT field,field2 AS alias " .
90 "WHERE alias = 'text' " .
96 'tables' => [ 'table', 't2' => 'table2' ],
97 'fields' => [ 'tid', 'field', 'alias' => 'field2', 't2.id' ],
98 'conds' => [ 'alias' => 'text' ],
99 'options' => [ 'LIMIT' => 1, 'ORDER BY' => 'field' ],
100 'join_conds' => [ 't2' => [
101 'LEFT JOIN', 'tid = t2.id'
104 "SELECT tid,field,field2 AS alias,t2.id " .
105 "FROM table LEFT JOIN table2 t2 ON ((tid = t2.id)) " .
106 "WHERE alias = 'text' " .
112 'tables' => [ 'table', 't2' => 'table2' ],
113 'fields' => [ 'tid', 'field', 'alias' => 'field2', 't2.id' ],
114 'conds' => [ 'alias' => 'text' ],
115 'options' => [ 'LIMIT' => 1, 'GROUP BY' => 'field', 'HAVING' => 'COUNT(*) > 1' ],
116 'join_conds' => [ 't2' => [
117 'LEFT JOIN', 'tid = t2.id'
120 "SELECT tid,field,field2 AS alias,t2.id " .
121 "FROM table LEFT JOIN table2 t2 ON ((tid = t2.id)) " .
122 "WHERE alias = 'text' " .
123 "GROUP BY field HAVING COUNT(*) > 1 " .
128 'tables' => [ 'table', 't2' => 'table2' ],
129 'fields' => [ 'tid', 'field', 'alias' => 'field2', 't2.id' ],
130 'conds' => [ 'alias' => 'text' ],
133 'GROUP BY' => [ 'field', 'field2' ],
134 'HAVING' => [ 'COUNT(*) > 1', 'field' => 1 ]
136 'join_conds' => [ 't2' => [
137 'LEFT JOIN', 'tid = t2.id'
140 "SELECT tid,field,field2 AS alias,t2.id " .
141 "FROM table LEFT JOIN table2 t2 ON ((tid = t2.id)) " .
142 "WHERE alias = 'text' " .
143 "GROUP BY field,field2 HAVING (COUNT(*) > 1) AND field = '1' " .
148 'tables' => [ 'table' ],
149 'fields' => [ 'alias' => 'field' ],
150 'conds' => [ 'alias' => [ 1, 2, 3, 4 ] ],
152 "SELECT field AS alias " .
154 "WHERE alias IN ('1','2','3','4')"
159 'fields' => [ 'field' ],
160 'options' => [ 'USE INDEX' => [ 'table' => 'X' ] ],
163 "SELECT field FROM table"
168 'fields' => [ 'field' ],
169 'options' => [ 'IGNORE INDEX' => [ 'table' => 'X' ] ],
172 "SELECT field FROM table"
177 'fields' => [ 'field' ],
178 'options' => [ 'DISTINCT', 'LOCK IN SHARE MODE' ],
180 "SELECT DISTINCT field FROM table LOCK IN SHARE MODE"
185 'fields' => [ 'field' ],
186 'options' => [ 'EXPLAIN' => true ],
188 'EXPLAIN SELECT field FROM table'
193 'fields' => [ 'field' ],
194 'options' => [ 'FOR UPDATE' ],
196 "SELECT field FROM table FOR UPDATE"
202 * @dataProvider provideUpdate
203 * @covers Wikimedia\Rdbms\Database::update
204 * @covers Wikimedia\Rdbms\Database::makeUpdateOptions
205 * @covers Wikimedia\Rdbms\Database::makeUpdateOptionsArray
207 public function testUpdate( $sql, $sqlText ) {
208 $this->database
->update(
213 isset( $sql['options'] ) ?
$sql['options'] : []
215 $this->assertLastSql( $sqlText );
218 public static function provideUpdate() {
223 'values' => [ 'field' => 'text', 'field2' => 'text2' ],
224 'conds' => [ 'alias' => 'text' ],
227 "SET field = 'text'" .
228 ",field2 = 'text2' " .
229 "WHERE alias = 'text'"
234 'values' => [ 'field = other', 'field2' => 'text2' ],
235 'conds' => [ 'id' => '1' ],
238 "SET field = other" .
239 ",field2 = 'text2' " .
245 'values' => [ 'field = other', 'field2' => 'text2' ],
249 "SET field = other" .
256 * @dataProvider provideDelete
257 * @covers Wikimedia\Rdbms\Database::delete
259 public function testDelete( $sql, $sqlText ) {
260 $this->database
->delete(
265 $this->assertLastSql( $sqlText );
268 public static function provideDelete() {
273 'conds' => [ 'alias' => 'text' ],
275 "DELETE FROM table " .
276 "WHERE alias = 'text'"
289 * @dataProvider provideUpsert
290 * @covers Wikimedia\Rdbms\Database::upsert
292 public function testUpsert( $sql, $sqlText ) {
293 $this->database
->upsert(
296 $sql['uniqueIndexes'],
300 $this->assertLastSql( $sqlText );
303 public static function provideUpsert() {
307 'table' => 'upsert_table',
308 'rows' => [ 'field' => 'text', 'field2' => 'text2' ],
309 'uniqueIndexes' => [ 'field' ],
310 'set' => [ 'field' => 'set' ],
313 "UPDATE upsert_table " .
314 "SET field = 'set' " .
315 "WHERE ((field = 'text')); " .
316 "INSERT IGNORE INTO upsert_table " .
318 "VALUES ('text','text2'); " .
325 * @dataProvider provideDeleteJoin
326 * @covers Wikimedia\Rdbms\Database::deleteJoin
328 public function testDeleteJoin( $sql, $sqlText ) {
329 $this->database
->deleteJoin(
337 $this->assertLastSql( $sqlText );
340 public static function provideDeleteJoin() {
344 'delTable' => 'table',
345 'joinTable' => 'table_join',
347 'joinVar' => 'field_join',
348 'conds' => [ 'alias' => 'text' ],
350 "DELETE FROM table " .
352 "SELECT field_join FROM table_join WHERE alias = 'text'" .
357 'delTable' => 'table',
358 'joinTable' => 'table_join',
360 'joinVar' => 'field_join',
363 "DELETE FROM table " .
365 "SELECT field_join FROM table_join " .
372 * @dataProvider provideInsert
373 * @covers Wikimedia\Rdbms\Database::insert
374 * @covers Wikimedia\Rdbms\Database::makeInsertOptions
376 public function testInsert( $sql, $sqlText ) {
377 $this->database
->insert(
381 isset( $sql['options'] ) ?
$sql['options'] : []
383 $this->assertLastSql( $sqlText );
386 public static function provideInsert() {
391 'rows' => [ 'field' => 'text', 'field2' => 2 ],
393 "INSERT INTO table " .
395 "VALUES ('text','2')"
400 'rows' => [ 'field' => 'text', 'field2' => 2 ],
401 'options' => 'IGNORE',
403 "INSERT IGNORE INTO table " .
405 "VALUES ('text','2')"
411 [ 'field' => 'text', 'field2' => 2 ],
412 [ 'field' => 'multi', 'field2' => 3 ],
414 'options' => 'IGNORE',
416 "INSERT IGNORE INTO table " .
426 * @dataProvider provideInsertSelect
427 * @covers Wikimedia\Rdbms\Database::insertSelect
428 * @covers Wikimedia\Rdbms\Database::nativeInsertSelect
430 public function testInsertSelect( $sql, $sqlTextNative, $sqlSelect, $sqlInsert ) {
431 $this->database
->insertSelect(
437 isset( $sql['insertOptions'] ) ?
$sql['insertOptions'] : [],
438 isset( $sql['selectOptions'] ) ?
$sql['selectOptions'] : [],
439 isset( $sql['selectJoinConds'] ) ?
$sql['selectJoinConds'] : []
441 $this->assertLastSql( $sqlTextNative );
443 $dbWeb = new DatabaseTestHelper( __CLASS__
, [ 'cliMode' => false ] );
444 $dbWeb->forceNextResult( [
445 array_flip( array_keys( $sql['varMap'] ) )
447 $dbWeb->insertSelect(
453 isset( $sql['insertOptions'] ) ?
$sql['insertOptions'] : [],
454 isset( $sql['selectOptions'] ) ?
$sql['selectOptions'] : [],
455 isset( $sql['selectJoinConds'] ) ?
$sql['selectJoinConds'] : []
457 $this->assertLastSqlDb( implode( '; ', [ $sqlSelect, $sqlInsert ] ), $dbWeb );
460 public static function provideInsertSelect() {
464 'destTable' => 'insert_table',
465 'srcTable' => 'select_table',
466 'varMap' => [ 'field_insert' => 'field_select', 'field' => 'field2' ],
469 "INSERT INTO insert_table " .
470 "(field_insert,field) " .
471 "SELECT field_select,field2 " .
472 "FROM select_table WHERE *",
473 "SELECT field_select AS field_insert,field2 AS field " .
474 "FROM select_table WHERE * FOR UPDATE",
475 "INSERT INTO insert_table (field_insert,field) VALUES ('0','1')"
479 'destTable' => 'insert_table',
480 'srcTable' => 'select_table',
481 'varMap' => [ 'field_insert' => 'field_select', 'field' => 'field2' ],
482 'conds' => [ 'field' => 2 ],
484 "INSERT INTO insert_table " .
485 "(field_insert,field) " .
486 "SELECT field_select,field2 " .
487 "FROM select_table " .
489 "SELECT field_select AS field_insert,field2 AS field FROM " .
490 "select_table WHERE field = '2' FOR UPDATE",
491 "INSERT INTO insert_table (field_insert,field) VALUES ('0','1')"
495 'destTable' => 'insert_table',
496 'srcTable' => 'select_table',
497 'varMap' => [ 'field_insert' => 'field_select', 'field' => 'field2' ],
498 'conds' => [ 'field' => 2 ],
499 'insertOptions' => 'IGNORE',
500 'selectOptions' => [ 'ORDER BY' => 'field' ],
502 "INSERT IGNORE INTO insert_table " .
503 "(field_insert,field) " .
504 "SELECT field_select,field2 " .
505 "FROM select_table " .
506 "WHERE field = '2' " .
508 "SELECT field_select AS field_insert,field2 AS field " .
509 "FROM select_table WHERE field = '2' ORDER BY field FOR UPDATE",
510 "INSERT IGNORE INTO insert_table (field_insert,field) VALUES ('0','1')"
514 'destTable' => 'insert_table',
515 'srcTable' => [ 'select_table1', 'select_table2' ],
516 'varMap' => [ 'field_insert' => 'field_select', 'field' => 'field2' ],
517 'conds' => [ 'field' => 2 ],
518 'selectOptions' => [ 'ORDER BY' => 'field', 'FORCE INDEX' => [ 'select_table1' => 'index1' ] ],
519 'selectJoinConds' => [
520 'select_table2' => [ 'LEFT JOIN', [ 'select_table1.foo = select_table2.bar' ] ],
523 "INSERT INTO insert_table " .
524 "(field_insert,field) " .
525 "SELECT field_select,field2 " .
526 "FROM select_table1 LEFT JOIN select_table2 ON ((select_table1.foo = select_table2.bar)) " .
527 "WHERE field = '2' " .
529 "SELECT field_select AS field_insert,field2 AS field " .
530 "FROM select_table1 LEFT JOIN select_table2 ON ((select_table1.foo = select_table2.bar)) " .
531 "WHERE field = '2' ORDER BY field FOR UPDATE",
532 "INSERT INTO insert_table (field_insert,field) VALUES ('0','1')"
538 * @dataProvider provideReplace
539 * @covers Wikimedia\Rdbms\Database::replace
541 public function testReplace( $sql, $sqlText ) {
542 $this->database
->replace(
544 $sql['uniqueIndexes'],
548 $this->assertLastSql( $sqlText );
551 public static function provideReplace() {
555 'table' => 'replace_table',
556 'uniqueIndexes' => [ 'field' ],
557 'rows' => [ 'field' => 'text', 'field2' => 'text2' ],
559 "DELETE FROM replace_table " .
560 "WHERE ( field='text' ); " .
561 "INSERT INTO replace_table " .
563 "VALUES ('text','text2')"
567 'table' => 'module_deps',
568 'uniqueIndexes' => [ [ 'md_module', 'md_skin' ] ],
570 'md_module' => 'module',
575 "DELETE FROM module_deps " .
576 "WHERE ( md_module='module' AND md_skin='skin' ); " .
577 "INSERT INTO module_deps " .
578 "(md_module,md_skin,md_deps) " .
579 "VALUES ('module','skin','deps')"
583 'table' => 'module_deps',
584 'uniqueIndexes' => [ [ 'md_module', 'md_skin' ] ],
587 'md_module' => 'module',
591 'md_module' => 'module2',
592 'md_skin' => 'skin2',
593 'md_deps' => 'deps2',
597 "DELETE FROM module_deps " .
598 "WHERE ( md_module='module' AND md_skin='skin' ); " .
599 "INSERT INTO module_deps " .
600 "(md_module,md_skin,md_deps) " .
601 "VALUES ('module','skin','deps'); " .
602 "DELETE FROM module_deps " .
603 "WHERE ( md_module='module2' AND md_skin='skin2' ); " .
604 "INSERT INTO module_deps " .
605 "(md_module,md_skin,md_deps) " .
606 "VALUES ('module2','skin2','deps2')"
610 'table' => 'module_deps',
611 'uniqueIndexes' => [ 'md_module', 'md_skin' ],
614 'md_module' => 'module',
618 'md_module' => 'module2',
619 'md_skin' => 'skin2',
620 'md_deps' => 'deps2',
624 "DELETE FROM module_deps " .
625 "WHERE ( md_module='module' ) OR ( md_skin='skin' ); " .
626 "INSERT INTO module_deps " .
627 "(md_module,md_skin,md_deps) " .
628 "VALUES ('module','skin','deps'); " .
629 "DELETE FROM module_deps " .
630 "WHERE ( md_module='module2' ) OR ( md_skin='skin2' ); " .
631 "INSERT INTO module_deps " .
632 "(md_module,md_skin,md_deps) " .
633 "VALUES ('module2','skin2','deps2')"
637 'table' => 'module_deps',
638 'uniqueIndexes' => [],
640 'md_module' => 'module',
645 "INSERT INTO module_deps " .
646 "(md_module,md_skin,md_deps) " .
647 "VALUES ('module','skin','deps')"
653 * @dataProvider provideNativeReplace
654 * @covers Wikimedia\Rdbms\Database::nativeReplace
656 public function testNativeReplace( $sql, $sqlText ) {
657 $this->database
->nativeReplace(
662 $this->assertLastSql( $sqlText );
665 public static function provideNativeReplace() {
669 'table' => 'replace_table',
670 'rows' => [ 'field' => 'text', 'field2' => 'text2' ],
672 "REPLACE INTO replace_table " .
674 "VALUES ('text','text2')"
680 * @dataProvider provideConditional
681 * @covers Wikimedia\Rdbms\Database::conditional
683 public function testConditional( $sql, $sqlText ) {
684 $this->assertEquals( trim( $this->database
->conditional(
691 public static function provideConditional() {
695 'conds' => [ 'field' => 'text' ],
699 "(CASE WHEN field = 'text' THEN 1 ELSE NULL END)"
703 'conds' => [ 'field' => 'text', 'field2' => 'anothertext' ],
707 "(CASE WHEN field = 'text' AND field2 = 'anothertext' THEN 1 ELSE NULL END)"
711 'conds' => 'field=1',
715 "(CASE WHEN field=1 THEN 1 ELSE NULL END)"
721 * @dataProvider provideBuildConcat
722 * @covers Wikimedia\Rdbms\Database::buildConcat
724 public function testBuildConcat( $stringList, $sqlText ) {
725 $this->assertEquals( trim( $this->database
->buildConcat(
730 public static function provideBuildConcat() {
733 [ 'field', 'field2' ],
734 "CONCAT(field,field2)"
737 [ "'test'", 'field2' ],
738 "CONCAT('test',field2)"
744 * @dataProvider provideBuildLike
745 * @covers Wikimedia\Rdbms\Database::buildLike
746 * @covers Wikimedia\Rdbms\Database::escapeLikeInternal
748 public function testBuildLike( $array, $sqlText ) {
749 $this->assertEquals( trim( $this->database
->buildLike(
754 public static function provideBuildLike() {
758 "LIKE 'text' ESCAPE '`'"
761 [ 'text', new LikeMatch( '%' ) ],
762 "LIKE 'text%' ESCAPE '`'"
765 [ 'text', new LikeMatch( '%' ), 'text2' ],
766 "LIKE 'text%text2' ESCAPE '`'"
769 [ 'text', new LikeMatch( '_' ) ],
770 "LIKE 'text_' ESCAPE '`'"
774 "LIKE 'more`_text' ESCAPE '`'"
777 [ 'C:\\Windows\\', new LikeMatch( '%' ) ],
778 "LIKE 'C:\\Windows\\%' ESCAPE '`'"
781 [ 'accent`_test`', new LikeMatch( '%' ) ],
782 "LIKE 'accent```_test``%' ESCAPE '`'"
788 * @dataProvider provideUnionQueries
789 * @covers Wikimedia\Rdbms\Database::unionQueries
791 public function testUnionQueries( $sql, $sqlText ) {
792 $this->assertEquals( trim( $this->database
->unionQueries(
798 public static function provideUnionQueries() {
802 'sqls' => [ 'RAW SQL', 'RAW2SQL' ],
805 "(RAW SQL) UNION ALL (RAW2SQL)"
809 'sqls' => [ 'RAW SQL', 'RAW2SQL' ],
812 "(RAW SQL) UNION (RAW2SQL)"
816 'sqls' => [ 'RAW SQL', 'RAW2SQL', 'RAW3SQL' ],
819 "(RAW SQL) UNION (RAW2SQL) UNION (RAW3SQL)"
825 * @dataProvider provideUnionConditionPermutations
826 * @covers Wikimedia\Rdbms\Database::unionConditionPermutations
828 public function testUnionConditionPermutations( $params, $expect ) {
829 if ( isset( $params['unionSupportsOrderAndLimit'] ) ) {
830 $this->database
->setUnionSupportsOrderAndLimit( $params['unionSupportsOrderAndLimit'] );
833 $sql = trim( $this->database
->unionConditionPermutations(
836 $params['permute_conds'],
837 isset( $params['extra_conds'] ) ?
$params['extra_conds'] : '',
839 isset( $params['options'] ) ?
$params['options'] : [],
840 isset( $params['join_conds'] ) ?
$params['join_conds'] : []
842 $this->assertEquals( $expect, $sql );
845 public static function provideUnionConditionPermutations() {
847 // @codingStandardsIgnoreStart Generic.Files.LineLength.TooLong
850 'table' => [ 'table1', 'table2' ],
851 'vars' => [ 'field1', 'alias' => 'field2' ],
853 'field3' => [ 1, 2, 3 ],
854 'duplicates' => [ 4, 5, 4 ],
858 'extra_conds' => 'table2.bar > 23',
860 'ORDER BY' => [ 'field1', 'alias' ],
861 'INNER ORDER BY' => [ 'field1', 'field2' ],
865 'table2' => [ 'JOIN', 'table1.foo_id = table2.foo_id' ],
868 "(SELECT field1,field2 AS alias FROM table1 JOIN table2 ON ((table1.foo_id = table2.foo_id)) WHERE field3 = '1' AND duplicates = '4' AND single = '0' AND (table2.bar > 23) ORDER BY field1,field2 LIMIT 100 ) UNION ALL " .
869 "(SELECT field1,field2 AS alias FROM table1 JOIN table2 ON ((table1.foo_id = table2.foo_id)) WHERE field3 = '1' AND duplicates = '5' AND single = '0' AND (table2.bar > 23) ORDER BY field1,field2 LIMIT 100 ) UNION ALL " .
870 "(SELECT field1,field2 AS alias FROM table1 JOIN table2 ON ((table1.foo_id = table2.foo_id)) WHERE field3 = '2' AND duplicates = '4' AND single = '0' AND (table2.bar > 23) ORDER BY field1,field2 LIMIT 100 ) UNION ALL " .
871 "(SELECT field1,field2 AS alias FROM table1 JOIN table2 ON ((table1.foo_id = table2.foo_id)) WHERE field3 = '2' AND duplicates = '5' AND single = '0' AND (table2.bar > 23) ORDER BY field1,field2 LIMIT 100 ) UNION ALL " .
872 "(SELECT field1,field2 AS alias FROM table1 JOIN table2 ON ((table1.foo_id = table2.foo_id)) WHERE field3 = '3' AND duplicates = '4' AND single = '0' AND (table2.bar > 23) ORDER BY field1,field2 LIMIT 100 ) UNION ALL " .
873 "(SELECT field1,field2 AS alias FROM table1 JOIN table2 ON ((table1.foo_id = table2.foo_id)) WHERE field3 = '3' AND duplicates = '5' AND single = '0' AND (table2.bar > 23) ORDER BY field1,field2 LIMIT 100 ) " .
874 "ORDER BY field1,alias LIMIT 100"
879 'vars' => [ 'foo_id' ],
881 'bar' => [ 1, 2, 3 ],
883 'extra_conds' => [ 'baz' => null ],
886 'ORDER BY' => [ 'foo_id' ],
890 "(SELECT foo_id FROM foo WHERE bar = '1' AND baz IS NULL ORDER BY foo_id LIMIT 25 ) UNION " .
891 "(SELECT foo_id FROM foo WHERE bar = '2' AND baz IS NULL ORDER BY foo_id LIMIT 25 ) UNION " .
892 "(SELECT foo_id FROM foo WHERE bar = '3' AND baz IS NULL ORDER BY foo_id LIMIT 25 ) " .
893 "ORDER BY foo_id LIMIT 25"
898 'vars' => [ 'foo_id' ],
900 'bar' => [ 1, 2, 3 ],
902 'extra_conds' => [ 'baz' => null ],
905 'ORDER BY' => [ 'foo_id' ],
908 'unionSupportsOrderAndLimit' => false,
910 "(SELECT foo_id FROM foo WHERE bar = '1' AND baz IS NULL ) UNION " .
911 "(SELECT foo_id FROM foo WHERE bar = '2' AND baz IS NULL ) UNION " .
912 "(SELECT foo_id FROM foo WHERE bar = '3' AND baz IS NULL ) " .
913 "ORDER BY foo_id LIMIT 25"
918 'vars' => [ 'foo_id' ],
919 'permute_conds' => [],
920 'extra_conds' => [ 'baz' => null ],
922 'ORDER BY' => [ 'foo_id' ],
926 "SELECT foo_id FROM foo WHERE baz IS NULL ORDER BY foo_id LIMIT 25"
931 'vars' => [ 'foo_id' ],
935 'extra_conds' => [ 'baz' => null ],
937 'ORDER BY' => [ 'foo_id' ],
941 "SELECT foo_id FROM foo WHERE baz IS NULL ORDER BY foo_id LIMIT 25"
946 'vars' => [ 'foo_id' ],
951 'ORDER BY' => [ 'foo_id' ],
956 "SELECT foo_id FROM foo WHERE bar = '1' ORDER BY foo_id LIMIT 150,25"
961 'vars' => [ 'foo_id' ],
962 'permute_conds' => [],
963 'extra_conds' => [ 'baz' => null ],
965 'ORDER BY' => [ 'foo_id' ],
968 'INNER ORDER BY' => [ 'bar_id' ],
971 "(SELECT foo_id FROM foo WHERE baz IS NULL ORDER BY bar_id LIMIT 175 ) ORDER BY foo_id LIMIT 150,25"
976 'vars' => [ 'foo_id' ],
977 'permute_conds' => [],
978 'extra_conds' => [ 'baz' => null ],
980 'ORDER BY' => [ 'foo_id' ],
983 'INNER ORDER BY' => [ 'bar_id' ],
985 'unionSupportsOrderAndLimit' => false,
987 "SELECT foo_id FROM foo WHERE baz IS NULL ORDER BY foo_id LIMIT 150,25"
989 // @codingStandardsIgnoreEnd
994 * @covers Wikimedia\Rdbms\Database::commit
995 * @covers Wikimedia\Rdbms\Database::doCommit
997 public function testTransactionCommit() {
998 $this->database
->begin( __METHOD__
);
999 $this->database
->commit( __METHOD__
);
1000 $this->assertLastSql( 'BEGIN; COMMIT' );
1004 * @covers Wikimedia\Rdbms\Database::rollback
1005 * @covers Wikimedia\Rdbms\Database::doRollback
1007 public function testTransactionRollback() {
1008 $this->database
->begin( __METHOD__
);
1009 $this->database
->rollback( __METHOD__
);
1010 $this->assertLastSql( 'BEGIN; ROLLBACK' );
1014 * @covers Wikimedia\Rdbms\Database::dropTable
1016 public function testDropTable() {
1017 $this->database
->setExistingTables( [ 'table' ] );
1018 $this->database
->dropTable( 'table', __METHOD__
);
1019 $this->assertLastSql( 'DROP TABLE table CASCADE' );
1023 * @covers Wikimedia\Rdbms\Database::dropTable
1025 public function testDropNonExistingTable() {
1027 $this->database
->dropTable( 'non_existing', __METHOD__
)
1032 * @dataProvider provideMakeList
1033 * @covers Wikimedia\Rdbms\Database::makeList
1035 public function testMakeList( $list, $mode, $sqlText ) {
1036 $this->assertEquals( trim( $this->database
->makeList(
1041 public static function provideMakeList() {
1044 [ 'value', 'value2' ],
1049 [ 'field', 'field2' ],
1054 [ 'field' => 'value', 'field2' => 'value2' ],
1056 "field = 'value' AND field2 = 'value2'"
1059 [ 'field' => null, "field2 != 'value2'" ],
1061 "field IS NULL AND (field2 != 'value2')"
1064 [ 'field' => [ 'value', null, 'value2' ], 'field2' => 'value2' ],
1066 "(field IN ('value','value2') OR field IS NULL) AND field2 = 'value2'"
1069 [ 'field' => [ null ], 'field2' => null ],
1071 "field IS NULL AND field2 IS NULL"
1074 [ 'field' => 'value', 'field2' => 'value2' ],
1076 "field = 'value' OR field2 = 'value2'"
1079 [ 'field' => 'value', 'field2' => null ],
1081 "field = 'value' OR field2 IS NULL"
1084 [ 'field' => [ 'value', 'value2' ], 'field2' => [ 'value' ] ],
1086 "field IN ('value','value2') OR field2 = 'value'"
1089 [ 'field' => [ null, 'value', null, 'value2' ], "field2 != 'value2'" ],
1091 "(field IN ('value','value2') OR field IS NULL) OR (field2 != 'value2')"
1094 [ 'field' => 'value', 'field2' => 'value2' ],
1096 "field = 'value',field2 = 'value2'"
1099 [ 'field' => 'value', 'field2' => null ],
1101 "field = 'value',field2 = NULL"
1104 [ 'field' => 'value', "field2 != 'value2'" ],
1106 "field = 'value',field2 != 'value2'"
1112 * @covers Wikimedia\Rdbms\Database::registerTempTableOperation
1114 public function testSessionTempTables() {
1115 $temp1 = $this->database
->tableName( 'tmp_table_1' );
1116 $temp2 = $this->database
->tableName( 'tmp_table_2' );
1117 $temp3 = $this->database
->tableName( 'tmp_table_3' );
1119 $this->database
->query( "CREATE TEMPORARY TABLE $temp1 LIKE orig_tbl", __METHOD__
);
1120 $this->database
->query( "CREATE TEMPORARY TABLE $temp2 LIKE orig_tbl", __METHOD__
);
1121 $this->database
->query( "CREATE TEMPORARY TABLE $temp3 LIKE orig_tbl", __METHOD__
);
1123 $this->assertTrue( $this->database
->tableExists( "tmp_table_1", __METHOD__
) );
1124 $this->assertTrue( $this->database
->tableExists( "tmp_table_2", __METHOD__
) );
1125 $this->assertTrue( $this->database
->tableExists( "tmp_table_3", __METHOD__
) );
1127 $this->database
->dropTable( 'tmp_table_1', __METHOD__
);
1128 $this->database
->dropTable( 'tmp_table_2', __METHOD__
);
1129 $this->database
->dropTable( 'tmp_table_3', __METHOD__
);
1131 $this->assertFalse( $this->database
->tableExists( "tmp_table_1", __METHOD__
) );
1132 $this->assertFalse( $this->database
->tableExists( "tmp_table_2", __METHOD__
) );
1133 $this->assertFalse( $this->database
->tableExists( "tmp_table_3", __METHOD__
) );
1135 $this->database
->query( "CREATE TEMPORARY TABLE tmp_table_1 LIKE orig_tbl", __METHOD__
);
1136 $this->database
->query( "CREATE TEMPORARY TABLE 'tmp_table_2' LIKE orig_tbl", __METHOD__
);
1137 $this->database
->query( "CREATE TEMPORARY TABLE `tmp_table_3` LIKE orig_tbl", __METHOD__
);
1139 $this->assertTrue( $this->database
->tableExists( "tmp_table_1", __METHOD__
) );
1140 $this->assertTrue( $this->database
->tableExists( "tmp_table_2", __METHOD__
) );
1141 $this->assertTrue( $this->database
->tableExists( "tmp_table_3", __METHOD__
) );
1143 $this->database
->query( "DROP TEMPORARY TABLE tmp_table_1 LIKE orig_tbl", __METHOD__
);
1144 $this->database
->query( "DROP TEMPORARY TABLE 'tmp_table_2' LIKE orig_tbl", __METHOD__
);
1145 $this->database
->query( "DROP TABLE `tmp_table_3` LIKE orig_tbl", __METHOD__
);
1147 $this->assertFalse( $this->database
->tableExists( "tmp_table_1", __METHOD__
) );
1148 $this->assertFalse( $this->database
->tableExists( "tmp_table_2", __METHOD__
) );
1149 $this->assertFalse( $this->database
->tableExists( "tmp_table_3", __METHOD__
) );