6 use Wikimedia\TestingAccessWrapper;
17 use MediaWikiCoversValidator;
18 use PHPUnit4And6Compat;
31 $this->database->getLastSqls()
36 $this->assertEquals( $sqlText, $db->
getLastSqls() );
53 $this->database->select(
58 $sql[
'options'] ?? [],
59 $sql[
'join_conds'] ?? []
69 'fields' => [
'field',
'alias' =>
'field2' ],
70 'conds' => [
'alias' =>
'text' ],
72 "SELECT field,field2 AS alias " .
74 "WHERE alias = 'text'"
79 'fields' => [
'field',
'alias' =>
'field2' ],
80 'conds' =>
'alias = \'text\'',
82 "SELECT field,field2 AS alias " .
84 "WHERE alias = 'text'"
89 'fields' => [
'field',
'alias' =>
'field2' ],
92 "SELECT field,field2 AS alias " .
98 'fields' => [
'field',
'alias' =>
'field2' ],
101 "SELECT field,field2 AS alias " .
107 'fields' => [
'field',
'alias' =>
'field2' ],
110 "SELECT field,field2 AS alias " .
117 'tables' =>
' table LEFT JOIN table2',
118 'fields' => [
'field' ],
119 'conds' => [
'field' =>
'text' ],
121 "SELECT field FROM table LEFT JOIN table2 WHERE field = 'text'"
127 'fields' => [
'SPECIAL_QUERY()' ],
129 "SELECT SPECIAL_QUERY()"
134 'fields' => [
'field',
'alias' =>
'field2' ],
135 'conds' => [
'alias' =>
'text' ],
136 'options' => [
'LIMIT' => 1,
'ORDER BY' =>
'field' ],
138 "SELECT field,field2 AS alias " .
140 "WHERE alias = 'text' " .
146 'tables' => [
'table',
't2' =>
'table2' ],
147 'fields' => [
'tid',
'field',
'alias' =>
'field2',
't2.id' ],
148 'conds' => [
'alias' =>
'text' ],
149 'options' => [
'LIMIT' => 1,
'ORDER BY' =>
'field' ],
150 'join_conds' => [
't2' => [
151 'LEFT JOIN',
'tid = t2.id'
154 "SELECT tid,field,field2 AS alias,t2.id " .
155 "FROM table LEFT JOIN table2 t2 ON ((tid = t2.id)) " .
156 "WHERE alias = 'text' " .
162 'tables' => [
'table',
't2' =>
'table2' ],
163 'fields' => [
'tid',
'field',
'alias' =>
'field2',
't2.id' ],
164 'conds' => [
'alias' =>
'text' ],
165 'options' => [
'LIMIT' => 1,
'GROUP BY' =>
'field',
'HAVING' =>
'COUNT(*) > 1' ],
166 'join_conds' => [
't2' => [
167 'LEFT JOIN',
'tid = t2.id'
170 "SELECT tid,field,field2 AS alias,t2.id " .
171 "FROM table LEFT JOIN table2 t2 ON ((tid = t2.id)) " .
172 "WHERE alias = 'text' " .
173 "GROUP BY field HAVING COUNT(*) > 1 " .
178 'tables' => [
'table',
't2' =>
'table2' ],
179 'fields' => [
'tid',
'field',
'alias' =>
'field2',
't2.id' ],
180 'conds' => [
'alias' =>
'text' ],
183 'GROUP BY' => [
'field',
'field2' ],
184 'HAVING' => [
'COUNT(*) > 1',
'field' => 1 ]
186 'join_conds' => [
't2' => [
187 'LEFT JOIN',
'tid = t2.id'
190 "SELECT tid,field,field2 AS alias,t2.id " .
191 "FROM table LEFT JOIN table2 t2 ON ((tid = t2.id)) " .
192 "WHERE alias = 'text' " .
193 "GROUP BY field,field2 HAVING (COUNT(*) > 1) AND field = '1' " .
198 'tables' => [
'table' ],
199 'fields' => [
'alias' =>
'field' ],
200 'conds' => [
'alias' => [ 1, 2, 3, 4 ] ],
202 "SELECT field AS alias " .
204 "WHERE alias IN ('1','2','3','4')"
209 'fields' => [
'field' ],
210 'options' => [
'USE INDEX' => [
'table' =>
'X' ] ],
213 "SELECT field FROM table"
218 'fields' => [
'field' ],
219 'options' => [
'IGNORE INDEX' => [
'table' =>
'X' ] ],
222 "SELECT field FROM table"
227 'fields' => [
'field' ],
228 'options' => [
'DISTINCT' ],
230 "SELECT DISTINCT field FROM table"
235 'fields' => [
'field' ],
236 'options' => [
'LOCK IN SHARE MODE' ],
238 "SELECT field FROM table LOCK IN SHARE MODE"
243 'fields' => [
'field' ],
244 'options' => [
'EXPLAIN' =>
true ],
246 'EXPLAIN SELECT field FROM table'
251 'fields' => [
'field' ],
252 'options' => [
'FOR UPDATE' ],
254 "SELECT field FROM table FOR UPDATE"
264 $this->database->startAtomic( __METHOD__ );
265 $this->database->lockForUpdate(
269 $sql[
'options'] ?? [],
270 $sql[
'join_conds'] ?? []
272 $this->database->endAtomic( __METHOD__ );
281 'tables' => [
'table' ],
282 'conds' => [
'field' => [ 1, 2, 3, 4 ] ],
284 "SELECT COUNT(*) AS rowcount FROM " .
285 "(SELECT 1 FROM table WHERE field IN ('1','2','3','4') " .
286 "FOR UPDATE) tmp_count"
290 'tables' => [
'table',
't2' =>
'table2' ],
291 'conds' => [
'field' =>
'text' ],
292 'options' => [
'LIMIT' => 1,
'ORDER BY' =>
'field' ],
293 'join_conds' => [
't2' => [
294 'LEFT JOIN',
'tid = t2.id'
297 "SELECT COUNT(*) AS rowcount FROM " .
298 "(SELECT 1 FROM table LEFT JOIN table2 t2 ON ((tid = t2.id)) " .
299 "WHERE field = 'text' ORDER BY field LIMIT 1 FOR UPDATE) tmp_count"
305 "SELECT COUNT(*) AS rowcount FROM " .
306 "(SELECT 1 FROM table FOR UPDATE) tmp_count"
318 $this->database->selectRowCount(
323 $sql[
'options'] ?? [],
324 $sql[
'join_conds'] ?? []
335 'conds' => [
'field' =>
'text' ],
337 "SELECT COUNT(*) AS rowcount FROM " .
338 "(SELECT 1 FROM table WHERE field = 'text' ) tmp_count"
343 'field' => [
'column' ],
344 'conds' => [
'field' =>
'text' ],
346 "SELECT COUNT(*) AS rowcount FROM " .
347 "(SELECT 1 FROM table WHERE field = 'text' AND (column IS NOT NULL) ) tmp_count"
352 'field' => [
'alias' =>
'column' ],
353 'conds' => [
'field' =>
'text' ],
355 "SELECT COUNT(*) AS rowcount FROM " .
356 "(SELECT 1 FROM table WHERE field = 'text' AND (column IS NOT NULL) ) tmp_count"
361 'field' => [
'alias' =>
'column' ],
364 "SELECT COUNT(*) AS rowcount FROM " .
365 "(SELECT 1 FROM table WHERE (column IS NOT NULL) ) tmp_count"
370 'field' => [
'alias' =>
'column' ],
373 "SELECT COUNT(*) AS rowcount FROM " .
374 "(SELECT 1 FROM table WHERE (column IS NOT NULL) ) tmp_count"
379 'field' => [
'alias' =>
'column' ],
382 "SELECT COUNT(*) AS rowcount FROM " .
383 "(SELECT 1 FROM table WHERE (column IS NOT NULL) ) tmp_count"
388 'field' => [
'alias' =>
'column' ],
391 "SELECT COUNT(*) AS rowcount FROM " .
392 "(SELECT 1 FROM table WHERE (1) AND (column IS NOT NULL) ) tmp_count"
397 'field' => [
'alias' =>
'column' ],
400 "SELECT COUNT(*) AS rowcount FROM " .
401 "(SELECT 1 FROM table WHERE (0) AND (column IS NOT NULL) ) tmp_count"
413 $this->database->update(
418 $sql[
'options'] ?? []
428 'values' => [
'field' =>
'text',
'field2' =>
'text2' ],
429 'conds' => [
'alias' =>
'text' ],
432 "SET field = 'text'" .
433 ",field2 = 'text2' " .
434 "WHERE alias = 'text'"
439 'values' => [
'field = other',
'field2' =>
'text2' ],
440 'conds' => [
'id' =>
'1' ],
443 "SET field = other" .
444 ",field2 = 'text2' " .
450 'values' => [
'field = other',
'field2' =>
'text2' ],
454 "SET field = other" .
465 $this->database->delete(
478 'conds' => [
'alias' =>
'text' ],
480 "DELETE FROM table " .
481 "WHERE alias = 'text'"
498 $this->database->upsert(
501 $sql[
'uniqueIndexes'],
512 'table' =>
'upsert_table',
513 'rows' => [
'field' =>
'text',
'field2' =>
'text2' ],
514 'uniqueIndexes' => [
'field' ],
515 'set' => [
'field' =>
'set' ],
518 "UPDATE upsert_table " .
519 "SET field = 'set' " .
520 "WHERE ((field = 'text')); " .
521 "INSERT IGNORE INTO upsert_table " .
523 "VALUES ('text','text2'); " .
534 $this->database->deleteJoin(
549 'delTable' =>
'table',
550 'joinTable' =>
'table_join',
552 'joinVar' =>
'field_join',
553 'conds' => [
'alias' =>
'text' ],
555 "DELETE FROM table " .
557 "SELECT field_join FROM table_join WHERE alias = 'text'" .
562 'delTable' =>
'table',
563 'joinTable' =>
'table_join',
565 'joinVar' =>
'field_join',
568 "DELETE FROM table " .
570 "SELECT field_join FROM table_join " .
582 $this->database->insert(
586 $sql[
'options'] ?? []
596 'rows' => [
'field' =>
'text',
'field2' => 2 ],
598 "INSERT INTO table " .
600 "VALUES ('text','2')"
605 'rows' => [
'field' =>
'text',
'field2' => 2 ],
606 'options' =>
'IGNORE',
608 "INSERT IGNORE INTO table " .
610 "VALUES ('text','2')"
616 [
'field' =>
'text',
'field2' => 2 ],
617 [
'field' =>
'multi',
'field2' => 3 ],
619 'options' =>
'IGNORE',
621 "INSERT IGNORE INTO table " .
636 $this->database->insertSelect(
642 $sql[
'insertOptions'] ?? [],
643 $sql[
'selectOptions'] ?? [],
644 $sql[
'selectJoinConds'] ?? []
649 $dbWeb->forceNextResult( [
650 array_flip( array_keys( $sql[
'varMap'] ) )
652 $dbWeb->insertSelect(
658 $sql[
'insertOptions'] ?? [],
659 $sql[
'selectOptions'] ?? [],
660 $sql[
'selectJoinConds'] ?? []
662 $this->
assertLastSqlDb( implode(
'; ', [ $sqlSelect,
'BEGIN', $sqlInsert,
'COMMIT' ] ), $dbWeb );
669 'destTable' =>
'insert_table',
670 'srcTable' =>
'select_table',
671 'varMap' => [
'field_insert' =>
'field_select',
'field' =>
'field2' ],
674 "INSERT INTO insert_table " .
675 "(field_insert,field) " .
676 "SELECT field_select,field2 " .
678 "SELECT field_select AS field_insert,field2 AS field " .
679 "FROM select_table FOR UPDATE",
680 "INSERT INTO insert_table (field_insert,field) VALUES ('0','1')"
684 'destTable' =>
'insert_table',
685 'srcTable' =>
'select_table',
686 'varMap' => [
'field_insert' =>
'field_select',
'field' =>
'field2' ],
687 'conds' => [
'field' => 2 ],
689 "INSERT INTO insert_table " .
690 "(field_insert,field) " .
691 "SELECT field_select,field2 " .
692 "FROM select_table " .
694 "SELECT field_select AS field_insert,field2 AS field FROM " .
695 "select_table WHERE field = '2' FOR UPDATE",
696 "INSERT INTO insert_table (field_insert,field) VALUES ('0','1')"
700 'destTable' =>
'insert_table',
701 'srcTable' =>
'select_table',
702 'varMap' => [
'field_insert' =>
'field_select',
'field' =>
'field2' ],
703 'conds' => [
'field' => 2 ],
704 'insertOptions' =>
'IGNORE',
705 'selectOptions' => [
'ORDER BY' =>
'field' ],
707 "INSERT IGNORE INTO insert_table " .
708 "(field_insert,field) " .
709 "SELECT field_select,field2 " .
710 "FROM select_table " .
711 "WHERE field = '2' " .
713 "SELECT field_select AS field_insert,field2 AS field " .
714 "FROM select_table WHERE field = '2' ORDER BY field FOR UPDATE",
715 "INSERT IGNORE INTO insert_table (field_insert,field) VALUES ('0','1')"
719 'destTable' =>
'insert_table',
720 'srcTable' => [
'select_table1',
'select_table2' ],
721 'varMap' => [
'field_insert' =>
'field_select',
'field' =>
'field2' ],
722 'conds' => [
'field' => 2 ],
723 'insertOptions' => [
'NO_AUTO_COLUMNS' ],
724 'selectOptions' => [
'ORDER BY' =>
'field',
'FORCE INDEX' => [
'select_table1' =>
'index1' ] ],
725 'selectJoinConds' => [
726 'select_table2' => [
'LEFT JOIN', [
'select_table1.foo = select_table2.bar' ] ],
729 "INSERT INTO insert_table " .
730 "(field_insert,field) " .
731 "SELECT field_select,field2 " .
732 "FROM select_table1 LEFT JOIN select_table2 ON ((select_table1.foo = select_table2.bar)) " .
733 "WHERE field = '2' " .
735 "SELECT field_select AS field_insert,field2 AS field " .
736 "FROM select_table1 LEFT JOIN select_table2 ON ((select_table1.foo = select_table2.bar)) " .
737 "WHERE field = '2' ORDER BY field FOR UPDATE",
738 "INSERT INTO insert_table (field_insert,field) VALUES ('0','1')"
746 for ( $i = 0; $i <= 25000; $i++ ) {
747 $rows[] = [
'field' => $i ];
749 $dbWeb->forceNextResult(
$rows );
750 $dbWeb->insertSelect(
753 [
'field' =>
'field2' ],
758 'SELECT field2 AS field FROM select_table FOR UPDATE',
760 "INSERT INTO insert_table (field) VALUES ('" . implode(
"'),('", range( 0, 9999 ) ) .
"')",
761 "INSERT INTO insert_table (field) VALUES ('" . implode(
"'),('", range( 10000, 19999 ) ) .
"')",
762 "INSERT INTO insert_table (field) VALUES ('" . implode(
"'),('", range( 20000, 25000 ) ) .
"')",
772 $this->database->replace(
774 $sql[
'uniqueIndexes'],
785 'table' =>
'replace_table',
786 'uniqueIndexes' => [
'field' ],
787 'rows' => [
'field' =>
'text',
'field2' =>
'text2' ],
789 "BEGIN; DELETE FROM replace_table " .
790 "WHERE (field = 'text'); " .
791 "INSERT INTO replace_table " .
793 "VALUES ('text','text2'); COMMIT"
797 'table' =>
'module_deps',
798 'uniqueIndexes' => [ [
'md_module',
'md_skin' ] ],
800 'md_module' =>
'module',
805 "BEGIN; DELETE FROM module_deps " .
806 "WHERE (md_module = 'module' AND md_skin = 'skin'); " .
807 "INSERT INTO module_deps " .
808 "(md_module,md_skin,md_deps) " .
809 "VALUES ('module','skin','deps'); COMMIT"
813 'table' =>
'module_deps',
814 'uniqueIndexes' => [ [
'md_module',
'md_skin' ] ],
817 'md_module' =>
'module',
821 'md_module' =>
'module2',
822 'md_skin' =>
'skin2',
823 'md_deps' =>
'deps2',
827 "BEGIN; DELETE FROM module_deps " .
828 "WHERE (md_module = 'module' AND md_skin = 'skin'); " .
829 "INSERT INTO module_deps " .
830 "(md_module,md_skin,md_deps) " .
831 "VALUES ('module','skin','deps'); " .
832 "DELETE FROM module_deps " .
833 "WHERE (md_module = 'module2' AND md_skin = 'skin2'); " .
834 "INSERT INTO module_deps " .
835 "(md_module,md_skin,md_deps) " .
836 "VALUES ('module2','skin2','deps2'); COMMIT"
840 'table' =>
'module_deps',
841 'uniqueIndexes' => [
'md_module',
'md_skin' ],
844 'md_module' =>
'module',
848 'md_module' =>
'module2',
849 'md_skin' =>
'skin2',
850 'md_deps' =>
'deps2',
854 "BEGIN; DELETE FROM module_deps " .
855 "WHERE (md_module = 'module') OR (md_skin = 'skin'); " .
856 "INSERT INTO module_deps " .
857 "(md_module,md_skin,md_deps) " .
858 "VALUES ('module','skin','deps'); " .
859 "DELETE FROM module_deps " .
860 "WHERE (md_module = 'module2') OR (md_skin = 'skin2'); " .
861 "INSERT INTO module_deps " .
862 "(md_module,md_skin,md_deps) " .
863 "VALUES ('module2','skin2','deps2'); COMMIT"
867 'table' =>
'module_deps',
868 'uniqueIndexes' => [],
870 'md_module' =>
'module',
875 "BEGIN; INSERT INTO module_deps " .
876 "(md_module,md_skin,md_deps) " .
877 "VALUES ('module','skin','deps'); COMMIT"
887 $this->database->nativeReplace(
899 'table' =>
'replace_table',
900 'rows' => [
'field' =>
'text',
'field2' =>
'text2' ],
902 "REPLACE INTO replace_table " .
904 "VALUES ('text','text2')"
914 $this->assertEquals( trim( $this->database->conditional(
925 'conds' => [
'field' =>
'text' ],
929 "(CASE WHEN field = 'text' THEN 1 ELSE NULL END)"
933 'conds' => [
'field' =>
'text',
'field2' =>
'anothertext' ],
937 "(CASE WHEN field = 'text' AND field2 = 'anothertext' THEN 1 ELSE NULL END)"
941 'conds' =>
'field=1',
945 "(CASE WHEN field=1 THEN 1 ELSE NULL END)"
955 $this->assertEquals( trim( $this->database->buildConcat(
963 [
'field',
'field2' ],
964 "CONCAT(field,field2)"
967 [
"'test'",
'field2' ],
968 "CONCAT('test',field2)"
979 $this->assertEquals( trim( $this->database->buildLike(
988 "LIKE 'text' ESCAPE '`'"
992 "LIKE 'text%' ESCAPE '`'"
995 [
'text',
new LikeMatch(
'%' ),
'text2' ],
996 "LIKE 'text%text2' ESCAPE '`'"
1000 "LIKE 'text_' ESCAPE '`'"
1004 "LIKE 'more`_text' ESCAPE '`'"
1007 [
'C:\\Windows\\',
new LikeMatch(
'%' ) ],
1008 "LIKE 'C:\\Windows\\%' ESCAPE '`'"
1011 [
'accent`_test`',
new LikeMatch(
'%' ) ],
1012 "LIKE 'accent```_test``%' ESCAPE '`'"
1022 $this->assertEquals( trim( $this->database->unionQueries(
1032 'sqls' => [
'RAW SQL',
'RAW2SQL' ],
1035 "(RAW SQL) UNION ALL (RAW2SQL)"
1039 'sqls' => [
'RAW SQL',
'RAW2SQL' ],
1042 "(RAW SQL) UNION (RAW2SQL)"
1046 'sqls' => [
'RAW SQL',
'RAW2SQL',
'RAW3SQL' ],
1049 "(RAW SQL) UNION (RAW2SQL) UNION (RAW3SQL)"
1059 if ( isset(
$params[
'unionSupportsOrderAndLimit'] ) ) {
1060 $this->database->setUnionSupportsOrderAndLimit(
$params[
'unionSupportsOrderAndLimit'] );
1063 $sql = trim( $this->database->unionConditionPermutations(
1072 $this->assertEquals( $expect, $sql );
1080 'table' => [
'table1',
'table2' ],
1081 'vars' => [
'field1',
'alias' =>
'field2' ],
1082 'permute_conds' => [
1083 'field3' => [ 1, 2, 3 ],
1084 'duplicates' => [ 4, 5, 4 ],
1088 'extra_conds' =>
'table2.bar > 23',
1090 'ORDER BY' => [
'field1',
'alias' ],
1091 'INNER ORDER BY' => [
'field1',
'field2' ],
1095 'table2' => [
'JOIN',
'table1.foo_id = table2.foo_id' ],
1098 "(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 " .
1099 "(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 " .
1100 "(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 " .
1101 "(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 " .
1102 "(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 " .
1103 "(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 ) " .
1104 "ORDER BY field1,alias LIMIT 100"
1109 'vars' => [
'foo_id' ],
1110 'permute_conds' => [
1111 'bar' => [ 1, 2, 3 ],
1113 'extra_conds' => [
'baz' => null ],
1116 'ORDER BY' => [
'foo_id' ],
1120 "(SELECT foo_id FROM foo WHERE bar = '1' AND baz IS NULL ORDER BY foo_id LIMIT 25 ) UNION " .
1121 "(SELECT foo_id FROM foo WHERE bar = '2' AND baz IS NULL ORDER BY foo_id LIMIT 25 ) UNION " .
1122 "(SELECT foo_id FROM foo WHERE bar = '3' AND baz IS NULL ORDER BY foo_id LIMIT 25 ) " .
1123 "ORDER BY foo_id LIMIT 25"
1128 'vars' => [
'foo_id' ],
1129 'permute_conds' => [
1130 'bar' => [ 1, 2, 3 ],
1132 'extra_conds' => [
'baz' => null ],
1135 'ORDER BY' => [
'foo_id' ],
1138 'unionSupportsOrderAndLimit' =>
false,
1140 "(SELECT foo_id FROM foo WHERE bar = '1' AND baz IS NULL ) UNION " .
1141 "(SELECT foo_id FROM foo WHERE bar = '2' AND baz IS NULL ) UNION " .
1142 "(SELECT foo_id FROM foo WHERE bar = '3' AND baz IS NULL ) " .
1143 "ORDER BY foo_id LIMIT 25"
1148 'vars' => [
'foo_id' ],
1149 'permute_conds' => [],
1150 'extra_conds' => [
'baz' => null ],
1152 'ORDER BY' => [
'foo_id' ],
1156 "SELECT foo_id FROM foo WHERE baz IS NULL ORDER BY foo_id LIMIT 25"
1161 'vars' => [
'foo_id' ],
1162 'permute_conds' => [
1165 'extra_conds' => [
'baz' => null ],
1167 'ORDER BY' => [
'foo_id' ],
1171 "SELECT foo_id FROM foo WHERE baz IS NULL ORDER BY foo_id LIMIT 25"
1176 'vars' => [
'foo_id' ],
1177 'permute_conds' => [
1181 'ORDER BY' => [
'foo_id' ],
1186 "SELECT foo_id FROM foo WHERE bar = '1' ORDER BY foo_id LIMIT 150,25"
1191 'vars' => [
'foo_id' ],
1192 'permute_conds' => [],
1193 'extra_conds' => [
'baz' => null ],
1195 'ORDER BY' => [
'foo_id' ],
1198 'INNER ORDER BY' => [
'bar_id' ],
1201 "(SELECT foo_id FROM foo WHERE baz IS NULL ORDER BY bar_id LIMIT 175 ) ORDER BY foo_id LIMIT 150,25"
1206 'vars' => [
'foo_id' ],
1207 'permute_conds' => [],
1208 'extra_conds' => [
'baz' => null ],
1210 'ORDER BY' => [
'foo_id' ],
1213 'INNER ORDER BY' => [
'bar_id' ],
1215 'unionSupportsOrderAndLimit' =>
false,
1217 "SELECT foo_id FROM foo WHERE baz IS NULL ORDER BY foo_id LIMIT 150,25"
1228 $this->database->begin( __METHOD__ );
1229 $this->database->commit( __METHOD__ );
1238 $this->database->begin( __METHOD__ );
1239 $this->database->rollback( __METHOD__ );
1247 $this->database->setExistingTables( [
'table' ] );
1248 $this->database->dropTable(
'table', __METHOD__ );
1257 $this->database->dropTable(
'non_existing', __METHOD__ )
1266 $this->assertEquals( trim( $this->database->makeList(
1274 [
'value',
'value2' ],
1279 [
'field',
'field2' ],
1284 [
'field' =>
'value',
'field2' =>
'value2' ],
1286 "field = 'value' AND field2 = 'value2'"
1289 [
'field' =>
null,
"field2 != 'value2'" ],
1291 "field IS NULL AND (field2 != 'value2')"
1294 [
'field' => [
'value',
null,
'value2' ],
'field2' =>
'value2' ],
1296 "(field IN ('value','value2') OR field IS NULL) AND field2 = 'value2'"
1299 [
'field' => [ null ],
'field2' => null ],
1301 "field IS NULL AND field2 IS NULL"
1304 [
'field' =>
'value',
'field2' =>
'value2' ],
1306 "field = 'value' OR field2 = 'value2'"
1309 [
'field' =>
'value',
'field2' => null ],
1311 "field = 'value' OR field2 IS NULL"
1314 [
'field' => [
'value',
'value2' ],
'field2' => [
'value' ] ],
1316 "field IN ('value','value2') OR field2 = 'value'"
1319 [
'field' => [
null,
'value',
null,
'value2' ],
"field2 != 'value2'" ],
1321 "(field IN ('value','value2') OR field IS NULL) OR (field2 != 'value2')"
1324 [
'field' =>
'value',
'field2' =>
'value2' ],
1326 "field = 'value',field2 = 'value2'"
1329 [
'field' =>
'value',
'field2' => null ],
1331 "field = 'value',field2 = NULL"
1334 [
'field' =>
'value',
"field2 != 'value2'" ],
1336 "field = 'value',field2 != 'value2'"
1345 $temp1 = $this->database->tableName(
'tmp_table_1' );
1346 $temp2 = $this->database->tableName(
'tmp_table_2' );
1347 $temp3 = $this->database->tableName(
'tmp_table_3' );
1349 $this->database->query(
"CREATE TEMPORARY TABLE $temp1 LIKE orig_tbl", __METHOD__ );
1350 $this->database->query(
"CREATE TEMPORARY TABLE $temp2 LIKE orig_tbl", __METHOD__ );
1351 $this->database->query(
"CREATE TEMPORARY TABLE $temp3 LIKE orig_tbl", __METHOD__ );
1353 $this->assertTrue( $this->database->tableExists(
"tmp_table_1", __METHOD__ ) );
1354 $this->assertTrue( $this->database->tableExists(
"tmp_table_2", __METHOD__ ) );
1355 $this->assertTrue( $this->database->tableExists(
"tmp_table_3", __METHOD__ ) );
1357 $this->database->dropTable(
'tmp_table_1', __METHOD__ );
1358 $this->database->dropTable(
'tmp_table_2', __METHOD__ );
1359 $this->database->dropTable(
'tmp_table_3', __METHOD__ );
1361 $this->assertFalse( $this->database->tableExists(
"tmp_table_1", __METHOD__ ) );
1362 $this->assertFalse( $this->database->tableExists(
"tmp_table_2", __METHOD__ ) );
1363 $this->assertFalse( $this->database->tableExists(
"tmp_table_3", __METHOD__ ) );
1365 $this->database->query(
"CREATE TEMPORARY TABLE tmp_table_1 LIKE orig_tbl", __METHOD__ );
1366 $this->database->query(
"CREATE TEMPORARY TABLE 'tmp_table_2' LIKE orig_tbl", __METHOD__ );
1367 $this->database->query(
"CREATE TEMPORARY TABLE `tmp_table_3` LIKE orig_tbl", __METHOD__ );
1369 $this->assertTrue( $this->database->tableExists(
"tmp_table_1", __METHOD__ ) );
1370 $this->assertTrue( $this->database->tableExists(
"tmp_table_2", __METHOD__ ) );
1371 $this->assertTrue( $this->database->tableExists(
"tmp_table_3", __METHOD__ ) );
1373 $this->database->query(
"DROP TEMPORARY TABLE tmp_table_1 LIKE orig_tbl", __METHOD__ );
1374 $this->database->query(
"DROP TEMPORARY TABLE 'tmp_table_2' LIKE orig_tbl", __METHOD__ );
1375 $this->database->query(
"DROP TABLE `tmp_table_3` LIKE orig_tbl", __METHOD__ );
1377 $this->assertFalse( $this->database->tableExists(
"tmp_table_1", __METHOD__ ) );
1378 $this->assertFalse( $this->database->tableExists(
"tmp_table_2", __METHOD__ ) );
1379 $this->assertFalse( $this->database->tableExists(
"tmp_table_3", __METHOD__ ) );
1383 yield [
'someField', 1, 2,
'SUBSTRING(someField FROM 1 FOR 2)' ];
1384 yield [
'someField', 1,
null,
'SUBSTRING(someField FROM 1)' ];
1392 $output = $this->database->buildSubstring(
$input, $start, $length );
1393 $this->assertSame( $expected,
$output );
1412 $this->database->buildSubstring(
'foo', $start, $length );
1419 $output = $this->database->buildIntegerCast(
'fieldName' );
1420 $this->assertSame(
'CAST( fieldName AS INTEGER )',
$output );
1433 $this->database->startAtomic( __METHOD__ );
1434 $this->database->endAtomic( __METHOD__ );
1437 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1438 $this->database->cancelAtomic( __METHOD__ );
1441 $this->database->begin( __METHOD__ );
1442 $this->database->startAtomic( __METHOD__ );
1443 $this->database->endAtomic( __METHOD__ );
1444 $this->database->commit( __METHOD__ );
1447 $this->database->begin( __METHOD__ );
1448 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1449 $this->database->endAtomic( __METHOD__ );
1450 $this->database->commit( __METHOD__ );
1452 $this->
assertLastSql(
'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; RELEASE SAVEPOINT wikimedia_rdbms_atomic1; COMMIT' );
1454 $this->database->begin( __METHOD__ );
1455 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1456 $this->database->cancelAtomic( __METHOD__ );
1457 $this->database->commit( __METHOD__ );
1459 $this->
assertLastSql(
'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; COMMIT' );
1461 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1462 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1463 $this->database->cancelAtomic( __METHOD__ );
1464 $this->database->endAtomic( __METHOD__ );
1466 $this->
assertLastSql(
'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; COMMIT' );
1468 $noOpCallack =
function () {
1471 $this->database->doAtomicSection( __METHOD__, $noOpCallack, IDatabase::ATOMIC_CANCELABLE );
1474 $this->database->doAtomicSection( __METHOD__, $noOpCallack );
1477 $this->database->begin( __METHOD__ );
1478 $this->database->doAtomicSection( __METHOD__, $noOpCallack, IDatabase::ATOMIC_CANCELABLE );
1479 $this->database->rollback( __METHOD__ );
1481 $this->
assertLastSql(
'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; RELEASE SAVEPOINT wikimedia_rdbms_atomic1; ROLLBACK' );
1486 IDatabase::TRIGGER_COMMIT =>
'tCommit',
1487 IDatabase::TRIGGER_ROLLBACK =>
'tRollback'
1490 $this->database->query(
"SELECT 0",
$fname );
1492 $callback1 =
function ( $trigger =
'-' )
use (
$fname, $triggerMap ) {
1493 $this->database->query(
"SELECT 1, {$triggerMap[$trigger]} AS t",
$fname );
1495 $callback2 =
function ( $trigger =
'-' )
use (
$fname, $triggerMap ) {
1496 $this->database->query(
"SELECT 2, {$triggerMap[$trigger]} AS t",
$fname );
1498 $callback3 =
function ( $trigger =
'-' )
use (
$fname, $triggerMap ) {
1499 $this->database->query(
"SELECT 3, {$triggerMap[$trigger]} AS t",
$fname );
1502 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1503 $this->database->onTransactionPreCommitOrIdle( $pcCallback, __METHOD__ );
1504 $this->database->cancelAtomic( __METHOD__ );
1507 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1508 $this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ );
1509 $this->database->cancelAtomic( __METHOD__ );
1512 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1513 $this->database->onTransactionResolution( $callback1, __METHOD__ );
1514 $this->database->cancelAtomic( __METHOD__ );
1515 $this->
assertLastSql(
'BEGIN; ROLLBACK; SELECT 1, tRollback AS t' );
1517 $this->database->startAtomic( __METHOD__ .
'_outer' );
1518 $this->database->onTransactionPreCommitOrIdle( $pcCallback, __METHOD__ );
1519 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1520 $this->database->onTransactionPreCommitOrIdle( $pcCallback, __METHOD__ );
1521 $this->database->cancelAtomic( __METHOD__ );
1522 $this->database->onTransactionPreCommitOrIdle( $pcCallback, __METHOD__ );
1523 $this->database->endAtomic( __METHOD__ .
'_outer' );
1526 'SAVEPOINT wikimedia_rdbms_atomic1',
1527 'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1',
1533 $this->database->startAtomic( __METHOD__ .
'_outer' );
1534 $this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ );
1535 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1536 $this->database->onTransactionCommitOrIdle( $callback2, __METHOD__ );
1537 $this->database->cancelAtomic( __METHOD__ );
1538 $this->database->onTransactionCommitOrIdle( $callback3, __METHOD__ );
1539 $this->database->endAtomic( __METHOD__ .
'_outer' );
1542 'SAVEPOINT wikimedia_rdbms_atomic1',
1543 'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1',
1545 'SELECT 1, tCommit AS t',
1546 'SELECT 3, tCommit AS t'
1549 $this->database->startAtomic( __METHOD__ .
'_outer' );
1550 $this->database->onTransactionResolution( $callback1, __METHOD__ );
1551 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1552 $this->database->onTransactionResolution( $callback2, __METHOD__ );
1553 $this->database->cancelAtomic( __METHOD__ );
1554 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1555 $this->database->endAtomic( __METHOD__ .
'_outer' );
1558 'SAVEPOINT wikimedia_rdbms_atomic1',
1559 'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1',
1561 'SELECT 1, tCommit AS t',
1562 'SELECT 2, tRollback AS t',
1563 'SELECT 3, tCommit AS t'
1566 $makeCallback =
function ( $id )
use (
$fname, $triggerMap ) {
1567 return function ( $trigger =
'-' )
use ( $id,
$fname, $triggerMap ) {
1568 $this->database->query(
"SELECT $id, {$triggerMap[$trigger]} AS t",
$fname );
1572 $this->database->startAtomic( __METHOD__ .
'_outer' );
1573 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1574 $this->database->onTransactionResolution( $makeCallback( 1 ), __METHOD__ );
1575 $this->database->cancelAtomic( __METHOD__ );
1576 $this->database->endAtomic( __METHOD__ .
'_outer' );
1579 'SAVEPOINT wikimedia_rdbms_atomic1',
1580 'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1',
1582 'SELECT 1, tRollback AS t'
1585 $this->database->startAtomic( __METHOD__ .
'_level1', IDatabase::ATOMIC_CANCELABLE );
1586 $this->database->onTransactionResolution( $makeCallback( 1 ), __METHOD__ );
1587 $this->database->startAtomic( __METHOD__ .
'_level2' );
1588 $this->database->startAtomic( __METHOD__ .
'_level3', IDatabase::ATOMIC_CANCELABLE );
1589 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1590 $this->database->onTransactionResolution( $makeCallback( 2 ), __METHOD__ );
1591 $this->database->endAtomic( __METHOD__ );
1592 $this->database->onTransactionResolution( $makeCallback( 3 ), __METHOD__ );
1593 $this->database->cancelAtomic( __METHOD__ .
'_level3' );
1594 $this->database->endAtomic( __METHOD__ .
'_level2' );
1595 $this->database->onTransactionResolution( $makeCallback( 4 ), __METHOD__ );
1596 $this->database->endAtomic( __METHOD__ .
'_level1' );
1599 'SAVEPOINT wikimedia_rdbms_atomic1',
1600 'SAVEPOINT wikimedia_rdbms_atomic2',
1601 'RELEASE SAVEPOINT wikimedia_rdbms_atomic2',
1602 'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1',
1603 'COMMIT; SELECT 1, tCommit AS t',
1604 'SELECT 2, tRollback AS t',
1605 'SELECT 3, tRollback AS t',
1606 'SELECT 4, tCommit AS t'
1620 $this->database->begin( __METHOD__ );
1622 $this->database->doAtomicSection(
1625 $this->database->startAtomic(
'inner_func1' );
1626 $this->database->startAtomic(
'inner_func2' );
1628 throw new RuntimeException(
'Test exception' );
1630 IDatabase::ATOMIC_CANCELABLE
1632 $this->fail(
'Expected exception not thrown' );
1633 }
catch ( RuntimeException $ex ) {
1634 $this->assertSame(
'Test exception', $ex->getMessage() );
1636 $this->database->commit( __METHOD__ );
1638 $this->
assertLastSql(
'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; COMMIT' );
1640 $this->database->begin( __METHOD__ );
1642 $this->database->doAtomicSection(
1645 throw new RuntimeException(
'Test exception' );
1648 $this->fail(
'Test exception not thrown' );
1649 }
catch ( RuntimeException $ex ) {
1650 $this->assertSame(
'Test exception', $ex->getMessage() );
1653 $this->database->commit( __METHOD__ );
1654 $this->fail(
'Test exception not thrown' );
1657 'Cannot execute query from ' . __METHOD__ .
' while transaction status is ERROR.',
1661 $this->database->rollback( __METHOD__ );
1676 $callback1Called =
null;
1677 $callback1 =
function ( $trigger =
'-' )
use (
$fname, &$callback1Called ) {
1678 $callback1Called = $trigger;
1679 $this->database->query(
"SELECT 1",
$fname );
1681 $callback2Called =
null;
1682 $callback2 =
function ( $trigger =
'-' )
use (
$fname, &$callback2Called ) {
1683 $callback2Called = $trigger;
1684 $this->database->query(
"SELECT 2",
$fname );
1686 $callback3Called =
null;
1687 $callback3 =
function ( $trigger =
'-' )
use (
$fname, &$callback3Called ) {
1688 $callback3Called = $trigger;
1689 $this->database->query(
"SELECT 3",
$fname );
1692 $this->database->startAtomic( __METHOD__ .
'_outer' );
1693 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1694 $this->database->startAtomic( __METHOD__ .
'_inner' );
1695 $this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ );
1696 $this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ );
1697 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1698 $this->database->endAtomic( __METHOD__ .
'_inner' );
1699 $this->database->cancelAtomic( __METHOD__ );
1700 $this->database->endAtomic( __METHOD__ .
'_outer' );
1701 $this->assertNull( $callback1Called );
1702 $this->assertNull( $callback2Called );
1703 $this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called );
1705 $this->
assertLastSql(
'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; COMMIT; SELECT 3' );
1707 $callback1Called =
null;
1708 $callback2Called =
null;
1709 $callback3Called =
null;
1710 $this->database->startAtomic( __METHOD__ .
'_outer' );
1711 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1712 $this->database->startAtomic( __METHOD__ .
'_inner', IDatabase::ATOMIC_CANCELABLE );
1713 $this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ );
1714 $this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ );
1715 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1716 $this->database->endAtomic( __METHOD__ .
'_inner' );
1717 $this->database->cancelAtomic( __METHOD__ );
1718 $this->database->endAtomic( __METHOD__ .
'_outer' );
1719 $this->assertNull( $callback1Called );
1720 $this->assertNull( $callback2Called );
1721 $this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called );
1723 $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' );
1725 $callback1Called =
null;
1726 $callback2Called =
null;
1727 $callback3Called =
null;
1728 $this->database->startAtomic( __METHOD__ .
'_outer' );
1729 $atomicId = $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1730 $this->database->startAtomic( __METHOD__ .
'_inner' );
1731 $this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ );
1732 $this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ );
1733 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1734 $this->database->cancelAtomic( __METHOD__, $atomicId );
1735 $this->database->endAtomic( __METHOD__ .
'_outer' );
1736 $this->assertNull( $callback1Called );
1737 $this->assertNull( $callback2Called );
1738 $this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called );
1740 $callback1Called =
null;
1741 $callback2Called =
null;
1742 $callback3Called =
null;
1743 $this->database->startAtomic( __METHOD__ .
'_outer' );
1744 $atomicId = $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1745 $this->database->startAtomic( __METHOD__ .
'_inner' );
1746 $this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ );
1747 $this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ );
1748 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1750 $this->database->cancelAtomic( __METHOD__ .
'_X', $atomicId );
1754 "Invalid atomic section ended (got {$m}_X but expected {$m}).",
1758 $this->database->cancelAtomic( __METHOD__ );
1759 $this->database->endAtomic( __METHOD__ .
'_outer' );
1760 $this->assertNull( $callback1Called );
1761 $this->assertNull( $callback2Called );
1762 $this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called );
1764 $this->database->startAtomic( __METHOD__ .
'_outer' );
1765 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1766 $this->database->startAtomic( __METHOD__ .
'_inner' );
1767 $this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ );
1768 $this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ );
1769 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1770 $this->database->cancelAtomic( __METHOD__ .
'_inner' );
1771 $this->database->cancelAtomic( __METHOD__ );
1772 $this->database->endAtomic( __METHOD__ .
'_outer' );
1773 $this->assertNull( $callback1Called );
1774 $this->assertNull( $callback2Called );
1775 $this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called );
1777 $wrapper = TestingAccessWrapper::newFromObject( $this->database );
1778 $callback1Called =
null;
1779 $callback2Called =
null;
1780 $callback3Called =
null;
1781 $this->database->startAtomic( __METHOD__ .
'_outer' );
1782 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1783 $this->database->startAtomic( __METHOD__ .
'_inner' );
1784 $this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ );
1785 $this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ );
1786 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1787 $wrapper->trxStatus = Database::STATUS_TRX_ERROR;
1788 $this->database->cancelAtomic( __METHOD__ .
'_inner' );
1789 $this->database->cancelAtomic( __METHOD__ );
1790 $this->database->endAtomic( __METHOD__ .
'_outer' );
1791 $this->assertNull( $callback1Called );
1792 $this->assertNull( $callback2Called );
1793 $this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called );
1807 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1808 $this->database->query(
'SELECT 1', __METHOD__ );
1809 $this->database->endAtomic( __METHOD__ );
1810 $this->database->commit( __METHOD__, IDatabase::FLUSHING_ALL_PEERS );
1812 $this->
assertLastSql(
'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; SELECT 1; RELEASE SAVEPOINT wikimedia_rdbms_atomic1; COMMIT' );
1829 $this->database->$method( __METHOD__ );
1830 $this->fail(
'Expected exception not thrown' );
1833 'No atomic section is open (got ' . __METHOD__ .
').',
1845 $this->database->startAtomic( __METHOD__ .
'X' );
1847 $this->database->$method( __METHOD__ );
1848 $this->fail(
'Expected exception not thrown' );
1851 'Invalid atomic section ended (got ' . __METHOD__ .
' but expected ' .
1852 __METHOD__ .
'X' .
').',
1862 $this->database->startAtomic( __METHOD__ );
1864 $this->database->cancelAtomic( __METHOD__ );
1865 $this->database->select(
'test',
'1', [], __METHOD__ );
1866 $this->fail(
'Expected exception not thrown' );
1869 'Cannot execute query from ' . __METHOD__ .
' while transaction status is ERROR.',
1879 $wrapper = TestingAccessWrapper::newFromObject( $this->database );
1881 $this->database->begin( __METHOD__ );
1882 $wrapper->trxStatus = Database::STATUS_TRX_ERROR;
1883 $this->database->delete(
'x', [
'field' => 3 ], __METHOD__ );
1884 $this->database->commit( __METHOD__ );
1891 $wrapper = TestingAccessWrapper::newFromObject( $this->database );
1893 $this->database->startAtomic( __METHOD__ );
1894 $wrapper->trxStatus = Database::STATUS_TRX_ERROR;
1895 $this->database->rollback( __METHOD__ );
1896 $this->assertEquals( 0, $this->database->trxLevel() );
1897 $this->assertEquals( Database::STATUS_TRX_NONE, $wrapper->trxStatus() );
1900 $this->database->startAtomic( __METHOD__ );
1901 $this->assertEquals( Database::STATUS_TRX_OK, $wrapper->trxStatus() );
1902 $this->database->delete(
'x', [
'field' => 1 ], __METHOD__ );
1903 $this->database->endAtomic( __METHOD__ );
1904 $this->assertEquals( Database::STATUS_TRX_NONE, $wrapper->trxStatus() );
1905 $this->
assertLastSql(
'BEGIN; DELETE FROM x WHERE field = \'1\'; COMMIT' );
1906 $this->assertEquals( 0, $this->database->trxLevel(),
'Use after rollback()' );
1908 $this->database->begin( __METHOD__ );
1909 $this->database->startAtomic( __METHOD__, Database::ATOMIC_CANCELABLE );
1910 $this->database->update(
'y', [
'a' => 1 ], [
'field' => 1 ], __METHOD__ );
1911 $wrapper->trxStatus = Database::STATUS_TRX_ERROR;
1912 $this->database->cancelAtomic( __METHOD__ );
1913 $this->assertEquals( Database::STATUS_TRX_OK, $wrapper->trxStatus() );
1914 $this->database->startAtomic( __METHOD__ );
1915 $this->database->delete(
'y', [
'field' => 1 ], __METHOD__ );
1916 $this->database->endAtomic( __METHOD__ );
1917 $this->database->commit( __METHOD__ );
1919 $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' );
1920 $this->assertEquals( 0, $this->database->trxLevel(),
'Use after rollback()' );
1923 $this->database->startAtomic( __METHOD__ );
1924 $this->assertEquals( Database::STATUS_TRX_OK, $wrapper->trxStatus() );
1925 $this->database->delete(
'x', [
'field' => 3 ], __METHOD__ );
1926 $this->database->endAtomic( __METHOD__ );
1927 $this->assertEquals( Database::STATUS_TRX_NONE, $wrapper->trxStatus() );
1928 $this->
assertLastSql(
'BEGIN; DELETE FROM x WHERE field = \'3\'; COMMIT' );
1929 $this->assertEquals( 0, $this->database->trxLevel() );
1936 $doError =
function () {
1937 $this->database->forceNextQueryError( 666,
'Evilness' );
1939 $this->database->delete(
'error',
'1', __CLASS__ .
'::SomeCaller' );
1940 $this->fail(
'Expected exception not thrown' );
1941 }
catch ( DBError
$e ) {
1942 $this->assertSame( 666,
$e->errno );
1949 $this->database->begin( __METHOD__, Database::TRANSACTION_INTERNAL );
1950 call_user_func( $doError );
1952 $this->database->delete(
'x', [
'field' => 1 ], __METHOD__ );
1953 $this->fail(
'Expected exception not thrown' );
1955 $this->assertEquals(
1956 'Cannot execute query from ' . __METHOD__ .
' while transaction status is ERROR.',
1961 $this->database->commit( __METHOD__, Database::FLUSHING_INTERNAL );
1962 $this->fail(
'Expected exception not thrown' );
1964 $this->assertEquals(
1965 'Cannot execute query from ' . __METHOD__ .
' while transaction status is ERROR.',
1969 $this->database->rollback( __METHOD__, Database::FLUSHING_INTERNAL );
1970 $this->
assertLastSql(
'BEGIN; DELETE FROM error WHERE 1; ROLLBACK' );
1973 $this->database->begin( __METHOD__, Database::TRANSACTION_INTERNAL );
1974 $this->database->delete(
'x', [
'field' => 1 ], __METHOD__ );
1975 call_user_func( $doError );
1977 $this->database->delete(
'x', [
'field' => 1 ], __METHOD__ );
1978 $this->fail(
'Expected exception not thrown' );
1981 $this->database->rollback( __METHOD__, Database::FLUSHING_INTERNAL );
1983 $this->
assertLastSql(
'BEGIN; DELETE FROM x WHERE field = \'1\'; DELETE FROM error WHERE 1; ROLLBACK' );
1990 $wrapper = TestingAccessWrapper::newFromObject( $this->database );
1992 $wrapper->deprecationLogger =
function ( $msg )
use ( &$warning ) {
1996 $doError =
function () {
1997 $this->database->forceNextQueryError( 666,
'Evilness', [
1998 'wasKnownStatementRollbackError' =>
true,
2001 $this->database->delete(
'error',
'1', __CLASS__ .
'::SomeCaller' );
2002 $this->fail(
'Expected exception not thrown' );
2003 }
catch ( DBError
$e ) {
2004 $this->assertSame( 666,
$e->errno );
2007 $expectWarning =
'Caller from ' . __METHOD__ .
2008 ' ignored an error originally raised from ' . __CLASS__ .
'::SomeCaller: [666] Evilness';
2012 $this->database->startAtomic( __METHOD__ );
2013 call_user_func( $doError );
2014 $this->database->rollback( __METHOD__ );
2015 $this->database->delete(
'x', [
'field' => 1 ], __METHOD__ );
2016 $this->assertSame( [], $warning );
2018 $this->
assertLastSql(
'BEGIN; DELETE FROM error WHERE 1; ROLLBACK; DELETE FROM x WHERE field = \'1\'' );
2022 $this->database->begin( __METHOD__ );
2023 $this->database->startAtomic( __METHOD__, Database::ATOMIC_CANCELABLE );
2024 call_user_func( $doError );
2025 $this->database->cancelAtomic( __METHOD__ );
2026 $this->database->delete(
'x', [
'field' => 1 ], __METHOD__ );
2027 $this->database->commit( __METHOD__ );
2028 $this->assertSame( [], $warning );
2030 $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' );
2034 $this->database->begin( __METHOD__ );
2035 call_user_func( $doError );
2036 $this->database->commit( __METHOD__ );
2037 $this->assertSame( [ $expectWarning ], $warning );
2038 $this->
assertLastSql(
'BEGIN; DELETE FROM error WHERE 1; COMMIT' );
2042 $this->database->begin( __METHOD__ );
2043 call_user_func( $doError );
2044 $this->database->delete(
'x', [
'field' => 1 ], __METHOD__ );
2045 $this->database->commit( __METHOD__ );
2046 $this->assertSame( [ $expectWarning ], $warning );
2048 $this->
assertLastSql(
'BEGIN; DELETE FROM error WHERE 1; DELETE FROM x WHERE field = \'1\'; COMMIT' );
2056 $this->database->begin( __METHOD__ );
2057 $this->database->onTransactionCommitOrIdle(
function ()
use (
$fname ) {
2058 $this->database->query(
'SELECT 1',
$fname );
2060 $this->database->onTransactionResolution(
function ()
use (
$fname ) {
2061 $this->database->query(
'SELECT 2',
$fname );
2063 $this->database->delete(
'x', [
'field' => 3 ], __METHOD__ );
2065 $this->database->close();
2066 $this->fail(
'Expected exception not thrown' );
2069 "Wikimedia\Rdbms\Database::close: transaction is still open (from $fname).",
2074 $this->assertFalse( $this->database->isOpen() );
2075 $this->
assertLastSql(
'BEGIN; DELETE FROM x WHERE field = \'3\'; ROLLBACK; SELECT 2' );
2076 $this->assertEquals( 0, $this->database->trxLevel() );
2085 $this->database->startAtomic( __METHOD__ );
2086 $this->database->onTransactionCommitOrIdle(
function ()
use (
$fname ) {
2087 $this->database->query(
'SELECT 1',
$fname );
2089 $this->database->delete(
'x', [
'field' => 3 ], __METHOD__ );
2090 $this->database->close();
2091 $this->fail(
'Expected exception not thrown' );
2094 'Wikimedia\Rdbms\Database::close: atomic sections ' .
2095 'DatabaseSQLTest::testPrematureClose2 are still open.',
2100 $this->assertFalse( $this->database->isOpen() );
2101 $this->
assertLastSql(
'BEGIN; DELETE FROM x WHERE field = \'3\'; ROLLBACK' );
2102 $this->assertEquals( 0, $this->database->trxLevel() );
2111 $this->database->delete(
'x', [
'field' => 3 ], __METHOD__ );
2112 $this->assertEquals( 1, $this->database->trxLevel() );
2113 $this->database->close();
2114 $this->fail(
'Expected exception not thrown' );
2117 'Wikimedia\Rdbms\Database::close: ' .
2118 'mass commit/rollback of peer transaction required (DBO_TRX set).',
2123 $this->assertFalse( $this->database->isOpen() );
2124 $this->
assertLastSql(
'BEGIN; DELETE FROM x WHERE field = \'3\'; ROLLBACK' );
2125 $this->assertEquals( 0, $this->database->trxLevel() );
2133 $this->database->query(
'SELECT 1', __METHOD__ );
2134 $this->assertEquals( 1, $this->database->trxLevel() );
2135 $this->database->close();
2138 $this->assertFalse( $this->database->isOpen() );
2140 $this->assertEquals( 0, $this->database->trxLevel() );
2147 $this->database->forceNextResult( [
2148 (
object)[
'value' =>
'row1' ],
2149 (
object)[
'value' =>
'row2' ],
2150 (
object)[
'value' =>
'row3' ],
2154 [
'row1',
'row2',
'row3' ],
2155 $this->database->selectFieldValues(
'table',
'table.field',
'conds', __METHOD__ )
2157 $this->
assertLastSql(
'SELECT table.field AS value FROM table WHERE conds' );