6use Wikimedia\TestingAccessWrapper;
17 use MediaWikiCoversValidator;
18 use PHPUnit4And6Compat;
31 $this->database->getLastSqls()
36 $this->assertEquals( $sqlText, $db->
getLastSqls() );
51 $this->database->select(
54 isset( $sql[
'conds'] ) ? $sql[
'conds'] : [],
56 isset( $sql[
'options'] ) ? $sql[
'options'] : [],
57 isset( $sql[
'join_conds'] ) ? $sql[
'join_conds'] : []
67 'fields' => [
'field',
'alias' =>
'field2' ],
68 'conds' => [
'alias' =>
'text' ],
70 "SELECT field,field2 AS alias " .
72 "WHERE alias = 'text'"
77 'fields' => [
'field',
'alias' =>
'field2' ],
78 'conds' =>
'alias = \'text\'',
80 "SELECT field,field2 AS alias " .
82 "WHERE alias = 'text'"
87 'fields' => [
'field',
'alias' =>
'field2' ],
90 "SELECT field,field2 AS alias " .
96 'fields' => [
'field',
'alias' =>
'field2' ],
99 "SELECT field,field2 AS alias " .
105 'fields' => [
'field',
'alias' =>
'field2' ],
108 "SELECT field,field2 AS alias " .
115 'tables' =>
' table LEFT JOIN table2',
116 'fields' => [
'field' ],
117 'conds' => [
'field' =>
'text' ],
119 "SELECT field FROM table LEFT JOIN table2 WHERE field = 'text'"
125 'fields' => [
'SPECIAL_QUERY()' ],
127 "SELECT SPECIAL_QUERY()"
132 'fields' => [
'field',
'alias' =>
'field2' ],
133 'conds' => [
'alias' =>
'text' ],
134 'options' => [
'LIMIT' => 1,
'ORDER BY' =>
'field' ],
136 "SELECT field,field2 AS alias " .
138 "WHERE alias = 'text' " .
144 'tables' => [
'table',
't2' =>
'table2' ],
145 'fields' => [
'tid',
'field',
'alias' =>
'field2',
't2.id' ],
146 'conds' => [
'alias' =>
'text' ],
147 'options' => [
'LIMIT' => 1,
'ORDER BY' =>
'field' ],
148 'join_conds' => [
't2' => [
149 'LEFT JOIN',
'tid = t2.id'
152 "SELECT tid,field,field2 AS alias,t2.id " .
153 "FROM table LEFT JOIN table2 t2 ON ((tid = t2.id)) " .
154 "WHERE alias = 'text' " .
160 'tables' => [
'table',
't2' =>
'table2' ],
161 'fields' => [
'tid',
'field',
'alias' =>
'field2',
't2.id' ],
162 'conds' => [
'alias' =>
'text' ],
163 'options' => [
'LIMIT' => 1,
'GROUP BY' =>
'field',
'HAVING' =>
'COUNT(*) > 1' ],
164 'join_conds' => [
't2' => [
165 'LEFT JOIN',
'tid = t2.id'
168 "SELECT tid,field,field2 AS alias,t2.id " .
169 "FROM table LEFT JOIN table2 t2 ON ((tid = t2.id)) " .
170 "WHERE alias = 'text' " .
171 "GROUP BY field HAVING COUNT(*) > 1 " .
176 'tables' => [
'table',
't2' =>
'table2' ],
177 'fields' => [
'tid',
'field',
'alias' =>
'field2',
't2.id' ],
178 'conds' => [
'alias' =>
'text' ],
181 'GROUP BY' => [
'field',
'field2' ],
182 'HAVING' => [
'COUNT(*) > 1',
'field' => 1 ]
184 'join_conds' => [
't2' => [
185 'LEFT JOIN',
'tid = t2.id'
188 "SELECT tid,field,field2 AS alias,t2.id " .
189 "FROM table LEFT JOIN table2 t2 ON ((tid = t2.id)) " .
190 "WHERE alias = 'text' " .
191 "GROUP BY field,field2 HAVING (COUNT(*) > 1) AND field = '1' " .
196 'tables' => [
'table' ],
197 'fields' => [
'alias' =>
'field' ],
198 'conds' => [
'alias' => [ 1, 2, 3, 4 ] ],
200 "SELECT field AS alias " .
202 "WHERE alias IN ('1','2','3','4')"
207 'fields' => [
'field' ],
208 'options' => [
'USE INDEX' => [
'table' =>
'X' ] ],
211 "SELECT field FROM table"
216 'fields' => [
'field' ],
217 'options' => [
'IGNORE INDEX' => [
'table' =>
'X' ] ],
220 "SELECT field FROM table"
225 'fields' => [
'field' ],
226 'options' => [
'DISTINCT',
'LOCK IN SHARE MODE' ],
228 "SELECT DISTINCT field FROM table LOCK IN SHARE MODE"
233 'fields' => [
'field' ],
234 'options' => [
'EXPLAIN' =>
true ],
236 'EXPLAIN SELECT field FROM table'
241 'fields' => [
'field' ],
242 'options' => [
'FOR UPDATE' ],
244 "SELECT field FROM table FOR UPDATE"
256 $this->database->selectRowCount(
259 isset( $sql[
'conds'] ) ? $sql[
'conds'] : [],
261 isset( $sql[
'options'] ) ? $sql[
'options'] : [],
262 isset( $sql[
'join_conds'] ) ? $sql[
'join_conds'] : []
273 'conds' => [
'field' =>
'text' ],
275 "SELECT COUNT(*) AS rowcount FROM " .
276 "(SELECT 1 FROM table WHERE field = 'text' ) tmp_count"
281 'field' => [
'column' ],
282 'conds' => [
'field' =>
'text' ],
284 "SELECT COUNT(*) AS rowcount FROM " .
285 "(SELECT 1 FROM table WHERE field = 'text' AND (column IS NOT NULL) ) tmp_count"
290 'field' => [
'alias' =>
'column' ],
291 'conds' => [
'field' =>
'text' ],
293 "SELECT COUNT(*) AS rowcount FROM " .
294 "(SELECT 1 FROM table WHERE field = 'text' AND (column IS NOT NULL) ) tmp_count"
299 'field' => [
'alias' =>
'column' ],
302 "SELECT COUNT(*) AS rowcount FROM " .
303 "(SELECT 1 FROM table WHERE (column IS NOT NULL) ) tmp_count"
308 'field' => [
'alias' =>
'column' ],
311 "SELECT COUNT(*) AS rowcount FROM " .
312 "(SELECT 1 FROM table WHERE (column IS NOT NULL) ) tmp_count"
317 'field' => [
'alias' =>
'column' ],
320 "SELECT COUNT(*) AS rowcount FROM " .
321 "(SELECT 1 FROM table WHERE (column IS NOT NULL) ) tmp_count"
326 'field' => [
'alias' =>
'column' ],
329 "SELECT COUNT(*) AS rowcount FROM " .
330 "(SELECT 1 FROM table WHERE (1) AND (column IS NOT NULL) ) tmp_count"
335 'field' => [
'alias' =>
'column' ],
338 "SELECT COUNT(*) AS rowcount FROM " .
339 "(SELECT 1 FROM table WHERE (0) AND (column IS NOT NULL) ) tmp_count"
351 $this->database->update(
356 isset( $sql[
'options'] ) ? $sql[
'options'] : []
366 'values' => [
'field' =>
'text',
'field2' =>
'text2' ],
367 'conds' => [
'alias' =>
'text' ],
370 "SET field = 'text'" .
371 ",field2 = 'text2' " .
372 "WHERE alias = 'text'"
377 'values' => [
'field = other',
'field2' =>
'text2' ],
378 'conds' => [
'id' =>
'1' ],
381 "SET field = other" .
382 ",field2 = 'text2' " .
388 'values' => [
'field = other',
'field2' =>
'text2' ],
392 "SET field = other" .
403 $this->database->delete(
416 'conds' => [
'alias' =>
'text' ],
418 "DELETE FROM table " .
419 "WHERE alias = 'text'"
436 $this->database->upsert(
439 $sql[
'uniqueIndexes'],
450 'table' =>
'upsert_table',
451 'rows' => [
'field' =>
'text',
'field2' =>
'text2' ],
452 'uniqueIndexes' => [
'field' ],
453 'set' => [
'field' =>
'set' ],
456 "UPDATE upsert_table " .
457 "SET field = 'set' " .
458 "WHERE ((field = 'text')); " .
459 "INSERT IGNORE INTO upsert_table " .
461 "VALUES ('text','text2'); " .
472 $this->database->deleteJoin(
487 'delTable' =>
'table',
488 'joinTable' =>
'table_join',
490 'joinVar' =>
'field_join',
491 'conds' => [
'alias' =>
'text' ],
493 "DELETE FROM table " .
495 "SELECT field_join FROM table_join WHERE alias = 'text'" .
500 'delTable' =>
'table',
501 'joinTable' =>
'table_join',
503 'joinVar' =>
'field_join',
506 "DELETE FROM table " .
508 "SELECT field_join FROM table_join " .
520 $this->database->insert(
524 isset( $sql[
'options'] ) ? $sql[
'options'] : []
534 'rows' => [
'field' =>
'text',
'field2' => 2 ],
536 "INSERT INTO table " .
538 "VALUES ('text','2')"
543 'rows' => [
'field' =>
'text',
'field2' => 2 ],
544 'options' =>
'IGNORE',
546 "INSERT IGNORE INTO table " .
548 "VALUES ('text','2')"
554 [
'field' =>
'text',
'field2' => 2 ],
555 [
'field' =>
'multi',
'field2' => 3 ],
557 'options' =>
'IGNORE',
559 "INSERT IGNORE INTO table " .
574 $this->database->insertSelect(
580 isset( $sql[
'insertOptions'] ) ? $sql[
'insertOptions'] : [],
581 isset( $sql[
'selectOptions'] ) ? $sql[
'selectOptions'] : [],
582 isset( $sql[
'selectJoinConds'] ) ? $sql[
'selectJoinConds'] : []
587 $dbWeb->forceNextResult( [
588 array_flip( array_keys( $sql[
'varMap'] ) )
590 $dbWeb->insertSelect(
596 isset( $sql[
'insertOptions'] ) ? $sql[
'insertOptions'] : [],
597 isset( $sql[
'selectOptions'] ) ? $sql[
'selectOptions'] : [],
598 isset( $sql[
'selectJoinConds'] ) ? $sql[
'selectJoinConds'] : []
600 $this->
assertLastSqlDb( implode(
'; ', [ $sqlSelect,
'BEGIN', $sqlInsert,
'COMMIT' ] ), $dbWeb );
607 'destTable' =>
'insert_table',
608 'srcTable' =>
'select_table',
609 'varMap' => [
'field_insert' =>
'field_select',
'field' =>
'field2' ],
612 "INSERT INTO insert_table " .
613 "(field_insert,field) " .
614 "SELECT field_select,field2 " .
615 "FROM select_table WHERE *",
616 "SELECT field_select AS field_insert,field2 AS field " .
617 "FROM select_table WHERE * FOR UPDATE",
618 "INSERT INTO insert_table (field_insert,field) VALUES ('0','1')"
622 'destTable' =>
'insert_table',
623 'srcTable' =>
'select_table',
624 'varMap' => [
'field_insert' =>
'field_select',
'field' =>
'field2' ],
625 'conds' => [
'field' => 2 ],
627 "INSERT INTO insert_table " .
628 "(field_insert,field) " .
629 "SELECT field_select,field2 " .
630 "FROM select_table " .
632 "SELECT field_select AS field_insert,field2 AS field FROM " .
633 "select_table WHERE field = '2' FOR UPDATE",
634 "INSERT INTO insert_table (field_insert,field) VALUES ('0','1')"
638 'destTable' =>
'insert_table',
639 'srcTable' =>
'select_table',
640 'varMap' => [
'field_insert' =>
'field_select',
'field' =>
'field2' ],
641 'conds' => [
'field' => 2 ],
642 'insertOptions' =>
'IGNORE',
643 'selectOptions' => [
'ORDER BY' =>
'field' ],
645 "INSERT IGNORE INTO insert_table " .
646 "(field_insert,field) " .
647 "SELECT field_select,field2 " .
648 "FROM select_table " .
649 "WHERE field = '2' " .
651 "SELECT field_select AS field_insert,field2 AS field " .
652 "FROM select_table WHERE field = '2' ORDER BY field FOR UPDATE",
653 "INSERT IGNORE INTO insert_table (field_insert,field) VALUES ('0','1')"
657 'destTable' =>
'insert_table',
658 'srcTable' => [
'select_table1',
'select_table2' ],
659 'varMap' => [
'field_insert' =>
'field_select',
'field' =>
'field2' ],
660 'conds' => [
'field' => 2 ],
661 'insertOptions' => [
'NO_AUTO_COLUMNS' ],
662 'selectOptions' => [
'ORDER BY' =>
'field',
'FORCE INDEX' => [
'select_table1' =>
'index1' ] ],
663 'selectJoinConds' => [
664 'select_table2' => [
'LEFT JOIN', [
'select_table1.foo = select_table2.bar' ] ],
667 "INSERT INTO insert_table " .
668 "(field_insert,field) " .
669 "SELECT field_select,field2 " .
670 "FROM select_table1 LEFT JOIN select_table2 ON ((select_table1.foo = select_table2.bar)) " .
671 "WHERE field = '2' " .
673 "SELECT field_select AS field_insert,field2 AS field " .
674 "FROM select_table1 LEFT JOIN select_table2 ON ((select_table1.foo = select_table2.bar)) " .
675 "WHERE field = '2' ORDER BY field FOR UPDATE",
676 "INSERT INTO insert_table (field_insert,field) VALUES ('0','1')"
684 for ( $i = 0; $i <= 25000; $i++ ) {
685 $rows[] = [
'field' => $i ];
687 $dbWeb->forceNextResult(
$rows );
688 $dbWeb->insertSelect(
691 [
'field' =>
'field2' ],
696 'SELECT field2 AS field FROM select_table WHERE * FOR UPDATE',
698 "INSERT INTO insert_table (field) VALUES ('" . implode(
"'),('", range( 0, 9999 ) ) .
"')",
699 "INSERT INTO insert_table (field) VALUES ('" . implode(
"'),('", range( 10000, 19999 ) ) .
"')",
700 "INSERT INTO insert_table (field) VALUES ('" . implode(
"'),('", range( 20000, 25000 ) ) .
"')",
710 $this->database->replace(
712 $sql[
'uniqueIndexes'],
723 'table' =>
'replace_table',
724 'uniqueIndexes' => [
'field' ],
725 'rows' => [
'field' =>
'text',
'field2' =>
'text2' ],
727 "BEGIN; DELETE FROM replace_table " .
728 "WHERE (field = 'text'); " .
729 "INSERT INTO replace_table " .
731 "VALUES ('text','text2'); COMMIT"
735 'table' =>
'module_deps',
736 'uniqueIndexes' => [ [
'md_module',
'md_skin' ] ],
738 'md_module' =>
'module',
743 "BEGIN; DELETE FROM module_deps " .
744 "WHERE (md_module = 'module' AND md_skin = 'skin'); " .
745 "INSERT INTO module_deps " .
746 "(md_module,md_skin,md_deps) " .
747 "VALUES ('module','skin','deps'); COMMIT"
751 'table' =>
'module_deps',
752 'uniqueIndexes' => [ [
'md_module',
'md_skin' ] ],
755 'md_module' =>
'module',
759 'md_module' =>
'module2',
760 'md_skin' =>
'skin2',
761 'md_deps' =>
'deps2',
765 "BEGIN; DELETE FROM module_deps " .
766 "WHERE (md_module = 'module' AND md_skin = 'skin'); " .
767 "INSERT INTO module_deps " .
768 "(md_module,md_skin,md_deps) " .
769 "VALUES ('module','skin','deps'); " .
770 "DELETE FROM module_deps " .
771 "WHERE (md_module = 'module2' AND md_skin = 'skin2'); " .
772 "INSERT INTO module_deps " .
773 "(md_module,md_skin,md_deps) " .
774 "VALUES ('module2','skin2','deps2'); COMMIT"
778 'table' =>
'module_deps',
779 'uniqueIndexes' => [
'md_module',
'md_skin' ],
782 'md_module' =>
'module',
786 'md_module' =>
'module2',
787 'md_skin' =>
'skin2',
788 'md_deps' =>
'deps2',
792 "BEGIN; DELETE FROM module_deps " .
793 "WHERE (md_module = 'module') OR (md_skin = 'skin'); " .
794 "INSERT INTO module_deps " .
795 "(md_module,md_skin,md_deps) " .
796 "VALUES ('module','skin','deps'); " .
797 "DELETE FROM module_deps " .
798 "WHERE (md_module = 'module2') OR (md_skin = 'skin2'); " .
799 "INSERT INTO module_deps " .
800 "(md_module,md_skin,md_deps) " .
801 "VALUES ('module2','skin2','deps2'); COMMIT"
805 'table' =>
'module_deps',
806 'uniqueIndexes' => [],
808 'md_module' =>
'module',
813 "BEGIN; INSERT INTO module_deps " .
814 "(md_module,md_skin,md_deps) " .
815 "VALUES ('module','skin','deps'); COMMIT"
825 $this->database->nativeReplace(
837 'table' =>
'replace_table',
838 'rows' => [
'field' =>
'text',
'field2' =>
'text2' ],
840 "REPLACE INTO replace_table " .
842 "VALUES ('text','text2')"
852 $this->assertEquals( trim( $this->database->conditional(
863 'conds' => [
'field' =>
'text' ],
867 "(CASE WHEN field = 'text' THEN 1 ELSE NULL END)"
871 'conds' => [
'field' =>
'text',
'field2' =>
'anothertext' ],
875 "(CASE WHEN field = 'text' AND field2 = 'anothertext' THEN 1 ELSE NULL END)"
879 'conds' =>
'field=1',
883 "(CASE WHEN field=1 THEN 1 ELSE NULL END)"
893 $this->assertEquals( trim( $this->database->buildConcat(
901 [
'field',
'field2' ],
902 "CONCAT(field,field2)"
905 [
"'test'",
'field2' ],
906 "CONCAT('test',field2)"
917 $this->assertEquals( trim( $this->database->buildLike(
926 "LIKE 'text' ESCAPE '`'"
930 "LIKE 'text%' ESCAPE '`'"
933 [
'text',
new LikeMatch(
'%' ),
'text2' ],
934 "LIKE 'text%text2' ESCAPE '`'"
938 "LIKE 'text_' ESCAPE '`'"
942 "LIKE 'more`_text' ESCAPE '`'"
945 [
'C:\\Windows\\',
new LikeMatch(
'%' ) ],
946 "LIKE 'C:\\Windows\\%' ESCAPE '`'"
949 [
'accent`_test`',
new LikeMatch(
'%' ) ],
950 "LIKE 'accent```_test``%' ESCAPE '`'"
960 $this->assertEquals( trim( $this->database->unionQueries(
970 'sqls' => [
'RAW SQL',
'RAW2SQL' ],
973 "(RAW SQL) UNION ALL (RAW2SQL)"
977 'sqls' => [
'RAW SQL',
'RAW2SQL' ],
980 "(RAW SQL) UNION (RAW2SQL)"
984 'sqls' => [
'RAW SQL',
'RAW2SQL',
'RAW3SQL' ],
987 "(RAW SQL) UNION (RAW2SQL) UNION (RAW3SQL)"
997 if ( isset(
$params[
'unionSupportsOrderAndLimit'] ) ) {
998 $this->database->setUnionSupportsOrderAndLimit(
$params[
'unionSupportsOrderAndLimit'] );
1001 $sql = trim( $this->database->unionConditionPermutations(
1010 $this->assertEquals( $expect, $sql );
1018 'table' => [
'table1',
'table2' ],
1019 'vars' => [
'field1',
'alias' =>
'field2' ],
1020 'permute_conds' => [
1021 'field3' => [ 1, 2, 3 ],
1022 'duplicates' => [ 4, 5, 4 ],
1026 'extra_conds' =>
'table2.bar > 23',
1028 'ORDER BY' => [
'field1',
'alias' ],
1029 'INNER ORDER BY' => [
'field1',
'field2' ],
1033 'table2' => [
'JOIN',
'table1.foo_id = table2.foo_id' ],
1036 "(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 " .
1037 "(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 " .
1038 "(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 " .
1039 "(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 " .
1040 "(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 " .
1041 "(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 ) " .
1042 "ORDER BY field1,alias LIMIT 100"
1047 'vars' => [
'foo_id' ],
1048 'permute_conds' => [
1049 'bar' => [ 1, 2, 3 ],
1051 'extra_conds' => [
'baz' => null ],
1054 'ORDER BY' => [
'foo_id' ],
1058 "(SELECT foo_id FROM foo WHERE bar = '1' AND baz IS NULL ORDER BY foo_id LIMIT 25 ) UNION " .
1059 "(SELECT foo_id FROM foo WHERE bar = '2' AND baz IS NULL ORDER BY foo_id LIMIT 25 ) UNION " .
1060 "(SELECT foo_id FROM foo WHERE bar = '3' AND baz IS NULL ORDER BY foo_id LIMIT 25 ) " .
1061 "ORDER BY foo_id LIMIT 25"
1066 'vars' => [
'foo_id' ],
1067 'permute_conds' => [
1068 'bar' => [ 1, 2, 3 ],
1070 'extra_conds' => [
'baz' => null ],
1073 'ORDER BY' => [
'foo_id' ],
1076 'unionSupportsOrderAndLimit' =>
false,
1078 "(SELECT foo_id FROM foo WHERE bar = '1' AND baz IS NULL ) UNION " .
1079 "(SELECT foo_id FROM foo WHERE bar = '2' AND baz IS NULL ) UNION " .
1080 "(SELECT foo_id FROM foo WHERE bar = '3' AND baz IS NULL ) " .
1081 "ORDER BY foo_id LIMIT 25"
1086 'vars' => [
'foo_id' ],
1087 'permute_conds' => [],
1088 'extra_conds' => [
'baz' => null ],
1090 'ORDER BY' => [
'foo_id' ],
1094 "SELECT foo_id FROM foo WHERE baz IS NULL ORDER BY foo_id LIMIT 25"
1099 'vars' => [
'foo_id' ],
1100 'permute_conds' => [
1103 'extra_conds' => [
'baz' => null ],
1105 'ORDER BY' => [
'foo_id' ],
1109 "SELECT foo_id FROM foo WHERE baz IS NULL ORDER BY foo_id LIMIT 25"
1114 'vars' => [
'foo_id' ],
1115 'permute_conds' => [
1119 'ORDER BY' => [
'foo_id' ],
1124 "SELECT foo_id FROM foo WHERE bar = '1' ORDER BY foo_id LIMIT 150,25"
1129 'vars' => [
'foo_id' ],
1130 'permute_conds' => [],
1131 'extra_conds' => [
'baz' => null ],
1133 'ORDER BY' => [
'foo_id' ],
1136 'INNER ORDER BY' => [
'bar_id' ],
1139 "(SELECT foo_id FROM foo WHERE baz IS NULL ORDER BY bar_id LIMIT 175 ) ORDER BY foo_id LIMIT 150,25"
1144 'vars' => [
'foo_id' ],
1145 'permute_conds' => [],
1146 'extra_conds' => [
'baz' => null ],
1148 'ORDER BY' => [
'foo_id' ],
1151 'INNER ORDER BY' => [
'bar_id' ],
1153 'unionSupportsOrderAndLimit' =>
false,
1155 "SELECT foo_id FROM foo WHERE baz IS NULL ORDER BY foo_id LIMIT 150,25"
1166 $this->database->begin( __METHOD__ );
1167 $this->database->commit( __METHOD__ );
1176 $this->database->begin( __METHOD__ );
1177 $this->database->rollback( __METHOD__ );
1185 $this->database->setExistingTables( [
'table' ] );
1186 $this->database->dropTable(
'table', __METHOD__ );
1195 $this->database->dropTable(
'non_existing', __METHOD__ )
1204 $this->assertEquals( trim( $this->database->makeList(
1212 [
'value',
'value2' ],
1217 [
'field',
'field2' ],
1222 [
'field' =>
'value',
'field2' =>
'value2' ],
1224 "field = 'value' AND field2 = 'value2'"
1227 [
'field' =>
null,
"field2 != 'value2'" ],
1229 "field IS NULL AND (field2 != 'value2')"
1232 [
'field' => [
'value',
null,
'value2' ],
'field2' =>
'value2' ],
1234 "(field IN ('value','value2') OR field IS NULL) AND field2 = 'value2'"
1237 [
'field' => [ null ],
'field2' => null ],
1239 "field IS NULL AND field2 IS NULL"
1242 [
'field' =>
'value',
'field2' =>
'value2' ],
1244 "field = 'value' OR field2 = 'value2'"
1247 [
'field' =>
'value',
'field2' => null ],
1249 "field = 'value' OR field2 IS NULL"
1252 [
'field' => [
'value',
'value2' ],
'field2' => [
'value' ] ],
1254 "field IN ('value','value2') OR field2 = 'value'"
1257 [
'field' => [
null,
'value',
null,
'value2' ],
"field2 != 'value2'" ],
1259 "(field IN ('value','value2') OR field IS NULL) OR (field2 != 'value2')"
1262 [
'field' =>
'value',
'field2' =>
'value2' ],
1264 "field = 'value',field2 = 'value2'"
1267 [
'field' =>
'value',
'field2' => null ],
1269 "field = 'value',field2 = NULL"
1272 [
'field' =>
'value',
"field2 != 'value2'" ],
1274 "field = 'value',field2 != 'value2'"
1283 $temp1 = $this->database->tableName(
'tmp_table_1' );
1284 $temp2 = $this->database->tableName(
'tmp_table_2' );
1285 $temp3 = $this->database->tableName(
'tmp_table_3' );
1287 $this->database->query(
"CREATE TEMPORARY TABLE $temp1 LIKE orig_tbl", __METHOD__ );
1288 $this->database->query(
"CREATE TEMPORARY TABLE $temp2 LIKE orig_tbl", __METHOD__ );
1289 $this->database->query(
"CREATE TEMPORARY TABLE $temp3 LIKE orig_tbl", __METHOD__ );
1291 $this->assertTrue( $this->database->tableExists(
"tmp_table_1", __METHOD__ ) );
1292 $this->assertTrue( $this->database->tableExists(
"tmp_table_2", __METHOD__ ) );
1293 $this->assertTrue( $this->database->tableExists(
"tmp_table_3", __METHOD__ ) );
1295 $this->database->dropTable(
'tmp_table_1', __METHOD__ );
1296 $this->database->dropTable(
'tmp_table_2', __METHOD__ );
1297 $this->database->dropTable(
'tmp_table_3', __METHOD__ );
1299 $this->assertFalse( $this->database->tableExists(
"tmp_table_1", __METHOD__ ) );
1300 $this->assertFalse( $this->database->tableExists(
"tmp_table_2", __METHOD__ ) );
1301 $this->assertFalse( $this->database->tableExists(
"tmp_table_3", __METHOD__ ) );
1303 $this->database->query(
"CREATE TEMPORARY TABLE tmp_table_1 LIKE orig_tbl", __METHOD__ );
1304 $this->database->query(
"CREATE TEMPORARY TABLE 'tmp_table_2' LIKE orig_tbl", __METHOD__ );
1305 $this->database->query(
"CREATE TEMPORARY TABLE `tmp_table_3` LIKE orig_tbl", __METHOD__ );
1307 $this->assertTrue( $this->database->tableExists(
"tmp_table_1", __METHOD__ ) );
1308 $this->assertTrue( $this->database->tableExists(
"tmp_table_2", __METHOD__ ) );
1309 $this->assertTrue( $this->database->tableExists(
"tmp_table_3", __METHOD__ ) );
1311 $this->database->query(
"DROP TEMPORARY TABLE tmp_table_1 LIKE orig_tbl", __METHOD__ );
1312 $this->database->query(
"DROP TEMPORARY TABLE 'tmp_table_2' LIKE orig_tbl", __METHOD__ );
1313 $this->database->query(
"DROP TABLE `tmp_table_3` LIKE orig_tbl", __METHOD__ );
1315 $this->assertFalse( $this->database->tableExists(
"tmp_table_1", __METHOD__ ) );
1316 $this->assertFalse( $this->database->tableExists(
"tmp_table_2", __METHOD__ ) );
1317 $this->assertFalse( $this->database->tableExists(
"tmp_table_3", __METHOD__ ) );
1321 yield [
'someField', 1, 2,
'SUBSTRING(someField FROM 1 FOR 2)' ];
1322 yield [
'someField', 1,
null,
'SUBSTRING(someField FROM 1)' ];
1330 $output = $this->database->buildSubstring(
$input, $start, $length );
1331 $this->assertSame( $expected,
$output );
1349 $this->setExpectedException( InvalidArgumentException::class );
1350 $this->database->buildSubstring(
'foo', $start, $length );
1357 $output = $this->database->buildIntegerCast(
'fieldName' );
1358 $this->assertSame(
'CAST( fieldName AS INTEGER )',
$output );
1371 $this->database->startAtomic( __METHOD__ );
1372 $this->database->endAtomic( __METHOD__ );
1375 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1376 $this->database->cancelAtomic( __METHOD__ );
1379 $this->database->begin( __METHOD__ );
1380 $this->database->startAtomic( __METHOD__ );
1381 $this->database->endAtomic( __METHOD__ );
1382 $this->database->commit( __METHOD__ );
1385 $this->database->begin( __METHOD__ );
1386 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1387 $this->database->endAtomic( __METHOD__ );
1388 $this->database->commit( __METHOD__ );
1390 $this->
assertLastSql(
'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; RELEASE SAVEPOINT wikimedia_rdbms_atomic1; COMMIT' );
1392 $this->database->begin( __METHOD__ );
1393 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1394 $this->database->cancelAtomic( __METHOD__ );
1395 $this->database->commit( __METHOD__ );
1397 $this->
assertLastSql(
'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; COMMIT' );
1399 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1400 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1401 $this->database->cancelAtomic( __METHOD__ );
1402 $this->database->endAtomic( __METHOD__ );
1404 $this->
assertLastSql(
'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; COMMIT' );
1406 $noOpCallack =
function () {
1409 $this->database->doAtomicSection( __METHOD__, $noOpCallack, IDatabase::ATOMIC_CANCELABLE );
1412 $this->database->doAtomicSection( __METHOD__, $noOpCallack );
1415 $this->database->begin( __METHOD__ );
1416 $this->database->doAtomicSection( __METHOD__, $noOpCallack, IDatabase::ATOMIC_CANCELABLE );
1417 $this->database->rollback( __METHOD__ );
1419 $this->
assertLastSql(
'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; RELEASE SAVEPOINT wikimedia_rdbms_atomic1; ROLLBACK' );
1424 IDatabase::TRIGGER_COMMIT =>
'tCommit',
1425 IDatabase::TRIGGER_ROLLBACK =>
'tRollback'
1427 $callback1 =
function ( $trigger =
'-' ) use (
$fname, $triggerMap ) {
1428 $this->database->query(
"SELECT 1, {$triggerMap[$trigger]} AS t",
$fname );
1430 $callback2 =
function ( $trigger =
'-' ) use (
$fname, $triggerMap ) {
1431 $this->database->query(
"SELECT 2, {$triggerMap[$trigger]} AS t",
$fname );
1433 $callback3 =
function ( $trigger =
'-' ) use (
$fname, $triggerMap ) {
1434 $this->database->query(
"SELECT 3, {$triggerMap[$trigger]} AS t",
$fname );
1437 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1438 $this->database->onTransactionPreCommitOrIdle( $callback1, __METHOD__ );
1439 $this->database->cancelAtomic( __METHOD__ );
1442 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1443 $this->database->onTransactionIdle( $callback1, __METHOD__ );
1444 $this->database->cancelAtomic( __METHOD__ );
1447 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1448 $this->database->onTransactionResolution( $callback1, __METHOD__ );
1449 $this->database->cancelAtomic( __METHOD__ );
1450 $this->
assertLastSql(
'BEGIN; ROLLBACK; SELECT 1, tRollback AS t' );
1452 $this->database->startAtomic( __METHOD__ .
'_outer' );
1453 $this->database->onTransactionPreCommitOrIdle( $callback1, __METHOD__ );
1454 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1455 $this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ );
1456 $this->database->cancelAtomic( __METHOD__ );
1457 $this->database->onTransactionPreCommitOrIdle( $callback3, __METHOD__ );
1458 $this->database->endAtomic( __METHOD__ .
'_outer' );
1461 'SAVEPOINT wikimedia_rdbms_atomic1',
1462 'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1',
1468 $this->database->startAtomic( __METHOD__ .
'_outer' );
1469 $this->database->onTransactionIdle( $callback1, __METHOD__ );
1470 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1471 $this->database->onTransactionIdle( $callback2, __METHOD__ );
1472 $this->database->cancelAtomic( __METHOD__ );
1473 $this->database->onTransactionIdle( $callback3, __METHOD__ );
1474 $this->database->endAtomic( __METHOD__ .
'_outer' );
1477 'SAVEPOINT wikimedia_rdbms_atomic1',
1478 'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1',
1480 'SELECT 1, tCommit AS t',
1481 'SELECT 3, tCommit AS t'
1484 $this->database->startAtomic( __METHOD__ .
'_outer' );
1485 $this->database->onTransactionResolution( $callback1, __METHOD__ );
1486 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1487 $this->database->onTransactionResolution( $callback2, __METHOD__ );
1488 $this->database->cancelAtomic( __METHOD__ );
1489 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1490 $this->database->endAtomic( __METHOD__ .
'_outer' );
1493 'SAVEPOINT wikimedia_rdbms_atomic1',
1494 'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1',
1496 'SELECT 1, tCommit AS t',
1497 'SELECT 2, tRollback AS t',
1498 'SELECT 3, tCommit AS t'
1501 $makeCallback =
function ( $id ) use (
$fname, $triggerMap ) {
1502 return function ( $trigger =
'-' ) use ( $id,
$fname, $triggerMap ) {
1503 $this->database->query(
"SELECT $id, {$triggerMap[$trigger]} AS t",
$fname );
1507 $this->database->startAtomic( __METHOD__ .
'_outer' );
1508 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1509 $this->database->onTransactionResolution( $makeCallback( 1 ), __METHOD__ );
1510 $this->database->cancelAtomic( __METHOD__ );
1511 $this->database->endAtomic( __METHOD__ .
'_outer' );
1514 'SAVEPOINT wikimedia_rdbms_atomic1',
1515 'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1',
1517 'SELECT 1, tRollback AS t'
1520 $this->database->startAtomic( __METHOD__ .
'_level1', IDatabase::ATOMIC_CANCELABLE );
1521 $this->database->onTransactionResolution( $makeCallback( 1 ), __METHOD__ );
1522 $this->database->startAtomic( __METHOD__ .
'_level2' );
1523 $this->database->startAtomic( __METHOD__ .
'_level3', IDatabase::ATOMIC_CANCELABLE );
1524 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1525 $this->database->onTransactionResolution( $makeCallback( 2 ), __METHOD__ );
1526 $this->database->endAtomic( __METHOD__ );
1527 $this->database->onTransactionResolution( $makeCallback( 3 ), __METHOD__ );
1528 $this->database->cancelAtomic( __METHOD__ .
'_level3' );
1529 $this->database->endAtomic( __METHOD__ .
'_level2' );
1530 $this->database->onTransactionResolution( $makeCallback( 4 ), __METHOD__ );
1531 $this->database->endAtomic( __METHOD__ .
'_level1' );
1534 'SAVEPOINT wikimedia_rdbms_atomic1',
1535 'SAVEPOINT wikimedia_rdbms_atomic2',
1536 'RELEASE SAVEPOINT wikimedia_rdbms_atomic2',
1537 'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1',
1538 'COMMIT; SELECT 1, tCommit AS t',
1539 'SELECT 2, tRollback AS t',
1540 'SELECT 3, tRollback AS t',
1541 'SELECT 4, tCommit AS t'
1555 $this->database->begin( __METHOD__ );
1557 $this->database->doAtomicSection(
1560 $this->database->startAtomic(
'inner_func1' );
1561 $this->database->startAtomic(
'inner_func2' );
1563 throw new RuntimeException(
'Test exception' );
1565 IDatabase::ATOMIC_CANCELABLE
1567 $this->fail(
'Expected exception not thrown' );
1568 }
catch ( RuntimeException $ex ) {
1569 $this->assertSame(
'Test exception', $ex->getMessage() );
1571 $this->database->commit( __METHOD__ );
1573 $this->
assertLastSql(
'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; COMMIT' );
1575 $this->database->begin( __METHOD__ );
1577 $this->database->doAtomicSection(
1580 throw new RuntimeException(
'Test exception' );
1583 $this->fail(
'Test exception not thrown' );
1584 }
catch ( RuntimeException $ex ) {
1585 $this->assertSame(
'Test exception', $ex->getMessage() );
1588 $this->database->commit( __METHOD__ );
1589 $this->fail(
'Test exception not thrown' );
1592 'Cannot execute query from ' . __METHOD__ .
' while transaction status is ERROR.',
1596 $this->database->rollback( __METHOD__ );
1611 $callback1Called =
null;
1612 $callback1 =
function ( $trigger =
'-' ) use (
$fname, &$callback1Called ) {
1613 $callback1Called = $trigger;
1614 $this->database->query(
"SELECT 1",
$fname );
1616 $callback2Called =
null;
1617 $callback2 =
function ( $trigger =
'-' ) use (
$fname, &$callback2Called ) {
1618 $callback2Called = $trigger;
1619 $this->database->query(
"SELECT 2",
$fname );
1621 $callback3Called =
null;
1622 $callback3 =
function ( $trigger =
'-' ) use (
$fname, &$callback3Called ) {
1623 $callback3Called = $trigger;
1624 $this->database->query(
"SELECT 3",
$fname );
1627 $this->database->startAtomic( __METHOD__ .
'_outer' );
1628 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1629 $this->database->startAtomic( __METHOD__ .
'_inner' );
1630 $this->database->onTransactionIdle( $callback1, __METHOD__ );
1631 $this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ );
1632 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1633 $this->database->endAtomic( __METHOD__ .
'_inner' );
1634 $this->database->cancelAtomic( __METHOD__ );
1635 $this->database->endAtomic( __METHOD__ .
'_outer' );
1636 $this->assertNull( $callback1Called );
1637 $this->assertNull( $callback2Called );
1638 $this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called );
1640 $this->
assertLastSql(
'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; COMMIT; SELECT 3' );
1642 $callback1Called =
null;
1643 $callback2Called =
null;
1644 $callback3Called =
null;
1645 $this->database->startAtomic( __METHOD__ .
'_outer' );
1646 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1647 $this->database->startAtomic( __METHOD__ .
'_inner', IDatabase::ATOMIC_CANCELABLE );
1648 $this->database->onTransactionIdle( $callback1, __METHOD__ );
1649 $this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ );
1650 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1651 $this->database->endAtomic( __METHOD__ .
'_inner' );
1652 $this->database->cancelAtomic( __METHOD__ );
1653 $this->database->endAtomic( __METHOD__ .
'_outer' );
1654 $this->assertNull( $callback1Called );
1655 $this->assertNull( $callback2Called );
1656 $this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called );
1658 $this->
assertLastSql(
'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; SAVEPOINT wikimedia_rdbms_atomic2; RELEASE SAVEPOINT wikimedia_rdbms_atomic2; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; COMMIT; SELECT 3' );
1660 $callback1Called =
null;
1661 $callback2Called =
null;
1662 $callback3Called =
null;
1663 $this->database->startAtomic( __METHOD__ .
'_outer' );
1664 $atomicId = $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1665 $this->database->startAtomic( __METHOD__ .
'_inner' );
1666 $this->database->onTransactionIdle( $callback1, __METHOD__ );
1667 $this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ );
1668 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1669 $this->database->cancelAtomic( __METHOD__, $atomicId );
1670 $this->database->endAtomic( __METHOD__ .
'_outer' );
1671 $this->assertNull( $callback1Called );
1672 $this->assertNull( $callback2Called );
1673 $this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called );
1675 $callback1Called =
null;
1676 $callback2Called =
null;
1677 $callback3Called =
null;
1678 $this->database->startAtomic( __METHOD__ .
'_outer' );
1679 $atomicId = $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1680 $this->database->startAtomic( __METHOD__ .
'_inner' );
1681 $this->database->onTransactionIdle( $callback1, __METHOD__ );
1682 $this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ );
1683 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1685 $this->database->cancelAtomic( __METHOD__ .
'_X', $atomicId );
1689 "Invalid atomic section ended (got {$m}_X but expected {$m}).",
1693 $this->database->cancelAtomic( __METHOD__ );
1694 $this->database->endAtomic( __METHOD__ .
'_outer' );
1695 $this->assertNull( $callback1Called );
1696 $this->assertNull( $callback2Called );
1697 $this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called );
1699 $this->database->startAtomic( __METHOD__ .
'_outer' );
1700 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1701 $this->database->startAtomic( __METHOD__ .
'_inner' );
1702 $this->database->onTransactionIdle( $callback1, __METHOD__ );
1703 $this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ );
1704 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1705 $this->database->cancelAtomic( __METHOD__ .
'_inner' );
1706 $this->database->cancelAtomic( __METHOD__ );
1707 $this->database->endAtomic( __METHOD__ .
'_outer' );
1708 $this->assertNull( $callback1Called );
1709 $this->assertNull( $callback2Called );
1710 $this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called );
1712 $wrapper = TestingAccessWrapper::newFromObject( $this->database );
1713 $callback1Called =
null;
1714 $callback2Called =
null;
1715 $callback3Called =
null;
1716 $this->database->startAtomic( __METHOD__ .
'_outer' );
1717 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1718 $this->database->startAtomic( __METHOD__ .
'_inner' );
1719 $this->database->onTransactionIdle( $callback1, __METHOD__ );
1720 $this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ );
1721 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1722 $wrapper->trxStatus = Database::STATUS_TRX_ERROR;
1723 $this->database->cancelAtomic( __METHOD__ .
'_inner' );
1724 $this->database->cancelAtomic( __METHOD__ );
1725 $this->database->endAtomic( __METHOD__ .
'_outer' );
1726 $this->assertNull( $callback1Called );
1727 $this->assertNull( $callback2Called );
1728 $this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called );
1741 $this->database->setFlag( IDatabase::DBO_TRX );
1742 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1743 $this->database->query(
'SELECT 1', __METHOD__ );
1744 $this->database->endAtomic( __METHOD__ );
1745 $this->database->commit( __METHOD__, IDatabase::FLUSHING_ALL_PEERS );
1747 $this->
assertLastSql(
'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; SELECT 1; RELEASE SAVEPOINT wikimedia_rdbms_atomic1; COMMIT' );
1764 $this->database->$method( __METHOD__ );
1765 $this->fail(
'Expected exception not thrown' );
1768 'No atomic section is open (got ' . __METHOD__ .
').',
1780 $this->database->startAtomic( __METHOD__ .
'X' );
1782 $this->database->$method( __METHOD__ );
1783 $this->fail(
'Expected exception not thrown' );
1786 'Invalid atomic section ended (got ' . __METHOD__ .
' but expected ' .
1787 __METHOD__ .
'X' .
').',
1797 $this->database->startAtomic( __METHOD__ );
1799 $this->database->cancelAtomic( __METHOD__ );
1800 $this->database->select(
'test',
'1', [], __METHOD__ );
1801 $this->fail(
'Expected exception not thrown' );
1804 'Cannot execute query from ' . __METHOD__ .
' while transaction status is ERROR.',
1814 $wrapper = TestingAccessWrapper::newFromObject( $this->database );
1816 $this->database->begin( __METHOD__ );
1817 $wrapper->trxStatus = Database::STATUS_TRX_ERROR;
1818 $this->database->delete(
'x', [
'field' => 3 ], __METHOD__ );
1819 $this->database->commit( __METHOD__ );
1826 $wrapper = TestingAccessWrapper::newFromObject( $this->database );
1828 $this->database->startAtomic( __METHOD__ );
1829 $wrapper->trxStatus = Database::STATUS_TRX_ERROR;
1830 $this->database->rollback( __METHOD__ );
1831 $this->assertEquals( 0, $this->database->trxLevel() );
1832 $this->assertEquals( Database::STATUS_TRX_NONE, $wrapper->trxStatus() );
1835 $this->database->startAtomic( __METHOD__ );
1836 $this->assertEquals( Database::STATUS_TRX_OK, $wrapper->trxStatus() );
1837 $this->database->delete(
'x', [
'field' => 1 ], __METHOD__ );
1838 $this->database->endAtomic( __METHOD__ );
1839 $this->assertEquals( Database::STATUS_TRX_NONE, $wrapper->trxStatus() );
1840 $this->
assertLastSql(
'BEGIN; DELETE FROM x WHERE field = \'1\'; COMMIT' );
1841 $this->assertEquals( 0, $this->database->trxLevel(),
'Use after rollback()' );
1843 $this->database->begin( __METHOD__ );
1844 $this->database->startAtomic( __METHOD__, Database::ATOMIC_CANCELABLE );
1845 $this->database->update(
'y', [
'a' => 1 ], [
'field' => 1 ], __METHOD__ );
1846 $wrapper->trxStatus = Database::STATUS_TRX_ERROR;
1847 $this->database->cancelAtomic( __METHOD__ );
1848 $this->assertEquals( Database::STATUS_TRX_OK, $wrapper->trxStatus() );
1849 $this->database->startAtomic( __METHOD__ );
1850 $this->database->delete(
'y', [
'field' => 1 ], __METHOD__ );
1851 $this->database->endAtomic( __METHOD__ );
1852 $this->database->commit( __METHOD__ );
1854 $this->
assertLastSql(
'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; UPDATE y SET a = \'1\' WHERE field = \'1\'; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; DELETE FROM y WHERE field = \'1\'; COMMIT' );
1855 $this->assertEquals( 0, $this->database->trxLevel(),
'Use after rollback()' );
1858 $this->database->startAtomic( __METHOD__ );
1859 $this->assertEquals( Database::STATUS_TRX_OK, $wrapper->trxStatus() );
1860 $this->database->delete(
'x', [
'field' => 3 ], __METHOD__ );
1861 $this->database->endAtomic( __METHOD__ );
1862 $this->assertEquals( Database::STATUS_TRX_NONE, $wrapper->trxStatus() );
1863 $this->
assertLastSql(
'BEGIN; DELETE FROM x WHERE field = \'3\'; COMMIT' );
1864 $this->assertEquals( 0, $this->database->trxLevel() );
1871 $doError =
function () {
1872 $this->database->forceNextQueryError( 666,
'Evilness' );
1874 $this->database->delete(
'error',
'1', __CLASS__ .
'::SomeCaller' );
1875 $this->fail(
'Expected exception not thrown' );
1876 }
catch ( DBError
$e ) {
1877 $this->assertSame( 666,
$e->errno );
1881 $this->database->setFlag( Database::DBO_TRX );
1884 $this->database->begin( __METHOD__, Database::TRANSACTION_INTERNAL );
1885 call_user_func( $doError );
1886 $this->database->delete(
'x', [
'field' => 1 ], __METHOD__ );
1887 $this->database->commit( __METHOD__, Database::FLUSHING_INTERNAL );
1889 $this->
assertLastSql(
'BEGIN; DELETE FROM error WHERE 1; ROLLBACK; BEGIN; DELETE FROM x WHERE field = \'1\'; COMMIT' );
1892 $this->database->begin( __METHOD__, Database::TRANSACTION_INTERNAL );
1893 $this->database->delete(
'x', [
'field' => 1 ], __METHOD__ );
1894 call_user_func( $doError );
1896 $this->database->delete(
'x', [
'field' => 1 ], __METHOD__ );
1897 $this->fail(
'Expected exception not thrown' );
1900 $this->database->rollback( __METHOD__, Database::FLUSHING_INTERNAL );
1902 $this->
assertLastSql(
'BEGIN; DELETE FROM x WHERE field = \'1\'; DELETE FROM error WHERE 1; ROLLBACK' );
1909 $wrapper = TestingAccessWrapper::newFromObject( $this->database );
1911 $wrapper->deprecationLogger =
function ( $msg ) use ( &$warning ) {
1915 $doError =
function () {
1916 $this->database->forceNextQueryError( 666,
'Evilness', [
1917 'wasKnownStatementRollbackError' =>
true,
1920 $this->database->delete(
'error',
'1', __CLASS__ .
'::SomeCaller' );
1921 $this->fail(
'Expected exception not thrown' );
1922 }
catch ( DBError
$e ) {
1923 $this->assertSame( 666,
$e->errno );
1926 $expectWarning =
'Caller from ' . __METHOD__ .
1927 ' ignored an error originally raised from ' . __CLASS__ .
'::SomeCaller: [666] Evilness';
1931 $this->database->startAtomic( __METHOD__ );
1932 call_user_func( $doError );
1933 $this->database->rollback( __METHOD__ );
1934 $this->database->delete(
'x', [
'field' => 1 ], __METHOD__ );
1935 $this->assertSame( [], $warning );
1937 $this->
assertLastSql(
'BEGIN; DELETE FROM error WHERE 1; ROLLBACK; DELETE FROM x WHERE field = \'1\'' );
1941 $this->database->begin( __METHOD__ );
1942 $this->database->startAtomic( __METHOD__, Database::ATOMIC_CANCELABLE );
1943 call_user_func( $doError );
1944 $this->database->cancelAtomic( __METHOD__ );
1945 $this->database->delete(
'x', [
'field' => 1 ], __METHOD__ );
1946 $this->database->commit( __METHOD__ );
1947 $this->assertSame( [], $warning );
1949 $this->
assertLastSql(
'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; DELETE FROM error WHERE 1; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; DELETE FROM x WHERE field = \'1\'; COMMIT' );
1953 $this->database->begin( __METHOD__ );
1954 call_user_func( $doError );
1955 $this->database->commit( __METHOD__ );
1956 $this->assertSame( [ $expectWarning ], $warning );
1957 $this->
assertLastSql(
'BEGIN; DELETE FROM error WHERE 1; COMMIT' );
1961 $this->database->begin( __METHOD__ );
1962 call_user_func( $doError );
1963 $this->database->delete(
'x', [
'field' => 1 ], __METHOD__ );
1964 $this->database->commit( __METHOD__ );
1965 $this->assertSame( [ $expectWarning ], $warning );
1967 $this->
assertLastSql(
'BEGIN; DELETE FROM error WHERE 1; DELETE FROM x WHERE field = \'1\'; COMMIT' );
1975 $this->database->begin( __METHOD__ );
1976 $this->database->onTransactionIdle(
function () use (
$fname ) {
1977 $this->database->query(
'SELECT 1',
$fname );
1979 $this->database->delete(
'x', [
'field' => 3 ], __METHOD__ );
1980 $this->database->close();
1982 $this->assertFalse( $this->database->isOpen() );
1983 $this->
assertLastSql(
'BEGIN; DELETE FROM x WHERE field = \'3\'; COMMIT; SELECT 1' );
1984 $this->assertEquals( 0, $this->database->trxLevel() );
1993 $this->database->startAtomic( __METHOD__ );
1994 $this->database->onTransactionIdle(
function () use (
$fname ) {
1995 $this->database->query(
'SELECT 1',
$fname );
1997 $this->database->delete(
'x', [
'field' => 3 ], __METHOD__ );
1998 $this->database->close();
1999 $this->fail(
'Expected exception not thrown' );
2002 'Wikimedia\Rdbms\Database::close: atomic sections ' .
2003 'DatabaseSQLTest::testPrematureClose2 are still open.',
2008 $this->assertFalse( $this->database->isOpen() );
2009 $this->
assertLastSql(
'BEGIN; DELETE FROM x WHERE field = \'3\'; ROLLBACK' );
2010 $this->assertEquals( 0, $this->database->trxLevel() );
2018 $this->database->setFlag( IDatabase::DBO_TRX );
2019 $this->database->delete(
'x', [
'field' => 3 ], __METHOD__ );
2020 $this->assertEquals( 1, $this->database->trxLevel() );
2021 $this->database->close();
2022 $this->fail(
'Expected exception not thrown' );
2025 'Wikimedia\Rdbms\Database::close: ' .
2026 'mass commit/rollback of peer transaction required (DBO_TRX set).',
2031 $this->assertFalse( $this->database->isOpen() );
2032 $this->
assertLastSql(
'BEGIN; DELETE FROM x WHERE field = \'3\'; ROLLBACK' );
2033 $this->assertEquals( 0, $this->database->trxLevel() );
2040 $this->database->setFlag( IDatabase::DBO_TRX );
2041 $this->database->query(
'SELECT 1', __METHOD__ );
2042 $this->assertEquals( 1, $this->database->trxLevel() );
2043 $this->database->close();
2044 $this->database->clearFlag( IDatabase::DBO_TRX );
2046 $this->assertFalse( $this->database->isOpen() );
2048 $this->assertEquals( 0, $this->database->trxLevel() );
2055 $this->database->forceNextResult( [
2056 (
object)[
'value' =>
'row1' ],
2057 (
object)[
'value' =>
'row2' ],
2058 (
object)[
'value' =>
'row3' ],
2062 [
'row1',
'row2',
'row3' ],
2063 $this->database->selectFieldValues(
'table',
'table.field',
'conds', __METHOD__ )
2065 $this->
assertLastSql(
'SELECT table.field AS value FROM table WHERE conds' );
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Test the parts of the Database abstract class that deal with creating SQL text.
static provideUnionConditionPermutations()
static provideNativeReplace()
static provideBuildLike()
static provideDeleteJoin()
testSessionTempTables()
Wikimedia\Rdbms\Database::registerTempTableOperation.
testSelect( $sql, $sqlText)
provideSelect Wikimedia\Rdbms\Database::select Wikimedia\Rdbms\Database::selectSQLText Wikimedia\Rdbm...
testBuildConcat( $stringList, $sqlText)
provideBuildConcat Wikimedia\Rdbms\Database::buildConcat
testInsertSelectBatching()
testDropTable()
Wikimedia\Rdbms\Database::dropTable.
testTransactionStatementRollbackIgnoring()
\Wikimedia\Rdbms\Database::query
testMakeList( $list, $mode, $sqlText)
provideMakeList Wikimedia\Rdbms\Database::makeList
provideBuildSubstring_invalidParams()
testUncancellableAtomicSection()
\Wikimedia\Rdbms\Database::cancelAtomic
testSelectFieldValues()
Wikimedia\Rdbms\Database::selectFieldValues()
static provideConditional()
testBuildSubstring( $input, $start, $length, $expected)
Wikimedia\Rdbms\Database::buildSubstring provideBuildSubstring.
static provideSelectRowCount()
testBuildLike( $array, $sqlText)
provideBuildLike Wikimedia\Rdbms\Database::buildLike Wikimedia\Rdbms\Database::escapeLikeInternal
testPrematureClose1()
\Wikimedia\Rdbms\Database::close
testImplicitTransactionRollback()
\Wikimedia\Rdbms\Database::query
static provideUnionQueries()
DatabaseTestHelper Database $database
static provideAtomicSectionMethodsForErrors()
testConditional( $sql, $sqlText)
provideConditional Wikimedia\Rdbms\Database::conditional
testTransactionErrorState2()
\Wikimedia\Rdbms\Database::query
testTransactionRollback()
Wikimedia\Rdbms\Database::rollback Wikimedia\Rdbms\Database::doRollback.
static provideInsertSelect()
testNoAtomicSection( $method)
provideAtomicSectionMethodsForErrors \Wikimedia\Rdbms\Database::endAtomic \Wikimedia\Rdbms\Database::...
testBuildSubstring_invalidParams( $start, $length)
Wikimedia\Rdbms\Database::buildSubstring Wikimedia\Rdbms\Database::assertBuildSubstringParams provide...
static provideBuildConcat()
testReplace( $sql, $sqlText)
provideReplace Wikimedia\Rdbms\Database::replace
testSelectRowCount( $sql, $sqlText)
Wikimedia\Rdbms\Subquery provideSelectRowCount.
testUnionQueries( $sql, $sqlText)
provideUnionQueries Wikimedia\Rdbms\Database::unionQueries
testUnionConditionPermutations( $params, $expect)
provideUnionConditionPermutations Wikimedia\Rdbms\Database::unionConditionPermutations
testAtomicSectionsRecovery()
\Wikimedia\Rdbms\Database::doSavepoint \Wikimedia\Rdbms\Database::doReleaseSavepoint \Wikimedia\Rdbms...
testUpsert( $sql, $sqlText)
provideUpsert Wikimedia\Rdbms\Database::upsert
testDropNonExistingTable()
Wikimedia\Rdbms\Database::dropTable.
testUpdate( $sql, $sqlText)
provideUpdate Wikimedia\Rdbms\Database::update Wikimedia\Rdbms\Database::makeUpdateOptions Wikimedia\...
testInsert( $sql, $sqlText)
provideInsert Wikimedia\Rdbms\Database::insert Wikimedia\Rdbms\Database::makeInsertOptions
testPrematureClose3()
\Wikimedia\Rdbms\Database::close
testPrematureClose4()
\Wikimedia\Rdbms\Database::close
testDelete( $sql, $sqlText)
provideDelete Wikimedia\Rdbms\Database::delete
testPrematureClose2()
\Wikimedia\Rdbms\Database::close
testBuildIntegerCast()
\Wikimedia\Rdbms\Database::buildIntegerCast
testNativeReplace( $sql, $sqlText)
provideNativeReplace Wikimedia\Rdbms\Database::nativeReplace
testTransactionErrorState1()
\Wikimedia\Rdbms\DBTransactionStateError
testAtomicSectionsTrxRound()
\Wikimedia\Rdbms\Database::doSavepoint \Wikimedia\Rdbms\Database::doReleaseSavepoint \Wikimedia\Rdbms...
testInvalidAtomicSectionEnded( $method)
provideAtomicSectionMethodsForErrors \Wikimedia\Rdbms\Database::endAtomic \Wikimedia\Rdbms\Database::...
testTransactionCommit()
Wikimedia\Rdbms\Database::commit Wikimedia\Rdbms\Database::doCommit.
testAtomicSections()
\Wikimedia\Rdbms\Database::doSavepoint \Wikimedia\Rdbms\Database::doReleaseSavepoint \Wikimedia\Rdbms...
testAtomicSectionsCallbackCancellation()
\Wikimedia\Rdbms\Database::doSavepoint \Wikimedia\Rdbms\Database::doReleaseSavepoint \Wikimedia\Rdbms...
assertLastSqlDb( $sqlText, DatabaseTestHelper $db)
testInsertSelect( $sql, $sqlTextNative, $sqlSelect, $sqlInsert)
provideInsertSelect Wikimedia\Rdbms\Database::insertSelect Wikimedia\Rdbms\Database::nativeInsertSele...
testDeleteJoin( $sql, $sqlText)
provideDeleteJoin Wikimedia\Rdbms\Database::deleteJoin
Helper for testing the methods from the Database class.
getLastSqls()
Returns SQL queries grouped by '; ' Clear the list of queries that have been done so far.
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction $rows
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place $output
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
returning false will NOT prevent logging $e
if(is_array($mode)) switch( $mode) $input