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')"
750 for ( $i = 0; $i <= 25000; $i++ ) {
751 $rows[] = [
'field' => $i ];
753 $dbWeb->forceNextResult(
$rows );
754 $dbWeb->insertSelect(
757 [
'field' =>
'field2' ],
762 'SELECT field2 AS field FROM select_table FOR UPDATE',
764 "INSERT INTO insert_table (field) VALUES ('" . implode(
"'),('", range( 0, 9999 ) ) .
"')",
765 "INSERT INTO insert_table (field) VALUES ('" . implode(
"'),('", range( 10000, 19999 ) ) .
"')",
766 "INSERT INTO insert_table (field) VALUES ('" . implode(
"'),('", range( 20000, 25000 ) ) .
"')",
776 $this->database->replace(
778 $sql[
'uniqueIndexes'],
789 'table' =>
'replace_table',
790 'uniqueIndexes' => [
'field' ],
791 'rows' => [
'field' =>
'text',
'field2' =>
'text2' ],
793 "BEGIN; DELETE FROM replace_table " .
794 "WHERE (field = 'text'); " .
795 "INSERT INTO replace_table " .
797 "VALUES ('text','text2'); COMMIT"
801 'table' =>
'module_deps',
802 'uniqueIndexes' => [ [
'md_module',
'md_skin' ] ],
804 'md_module' =>
'module',
809 "BEGIN; DELETE FROM module_deps " .
810 "WHERE (md_module = 'module' AND md_skin = 'skin'); " .
811 "INSERT INTO module_deps " .
812 "(md_module,md_skin,md_deps) " .
813 "VALUES ('module','skin','deps'); COMMIT"
817 'table' =>
'module_deps',
818 'uniqueIndexes' => [ [
'md_module',
'md_skin' ] ],
821 'md_module' =>
'module',
825 'md_module' =>
'module2',
826 'md_skin' =>
'skin2',
827 'md_deps' =>
'deps2',
831 "BEGIN; DELETE FROM module_deps " .
832 "WHERE (md_module = 'module' AND md_skin = 'skin'); " .
833 "INSERT INTO module_deps " .
834 "(md_module,md_skin,md_deps) " .
835 "VALUES ('module','skin','deps'); " .
836 "DELETE FROM module_deps " .
837 "WHERE (md_module = 'module2' AND md_skin = 'skin2'); " .
838 "INSERT INTO module_deps " .
839 "(md_module,md_skin,md_deps) " .
840 "VALUES ('module2','skin2','deps2'); COMMIT"
844 'table' =>
'module_deps',
845 'uniqueIndexes' => [
'md_module',
'md_skin' ],
848 'md_module' =>
'module',
852 'md_module' =>
'module2',
853 'md_skin' =>
'skin2',
854 'md_deps' =>
'deps2',
858 "BEGIN; DELETE FROM module_deps " .
859 "WHERE (md_module = 'module') OR (md_skin = 'skin'); " .
860 "INSERT INTO module_deps " .
861 "(md_module,md_skin,md_deps) " .
862 "VALUES ('module','skin','deps'); " .
863 "DELETE FROM module_deps " .
864 "WHERE (md_module = 'module2') OR (md_skin = 'skin2'); " .
865 "INSERT INTO module_deps " .
866 "(md_module,md_skin,md_deps) " .
867 "VALUES ('module2','skin2','deps2'); COMMIT"
871 'table' =>
'module_deps',
872 'uniqueIndexes' => [],
874 'md_module' =>
'module',
879 "BEGIN; INSERT INTO module_deps " .
880 "(md_module,md_skin,md_deps) " .
881 "VALUES ('module','skin','deps'); COMMIT"
891 $this->database->nativeReplace(
903 'table' =>
'replace_table',
904 'rows' => [
'field' =>
'text',
'field2' =>
'text2' ],
906 "REPLACE INTO replace_table " .
908 "VALUES ('text','text2')"
918 $this->assertEquals( trim( $this->database->conditional(
929 'conds' => [
'field' =>
'text' ],
933 "(CASE WHEN field = 'text' THEN 1 ELSE NULL END)"
937 'conds' => [
'field' =>
'text',
'field2' =>
'anothertext' ],
941 "(CASE WHEN field = 'text' AND field2 = 'anothertext' THEN 1 ELSE NULL END)"
945 'conds' =>
'field=1',
949 "(CASE WHEN field=1 THEN 1 ELSE NULL END)"
959 $this->assertEquals( trim( $this->database->buildConcat(
967 [
'field',
'field2' ],
968 "CONCAT(field,field2)"
971 [
"'test'",
'field2' ],
972 "CONCAT('test',field2)"
983 $this->assertEquals( trim( $this->database->buildLike(
992 "LIKE 'text' ESCAPE '`'"
996 "LIKE 'text%' ESCAPE '`'"
999 [
'text',
new LikeMatch(
'%' ),
'text2' ],
1000 "LIKE 'text%text2' ESCAPE '`'"
1004 "LIKE 'text_' ESCAPE '`'"
1008 "LIKE 'more`_text' ESCAPE '`'"
1011 [
'C:\\Windows\\',
new LikeMatch(
'%' ) ],
1012 "LIKE 'C:\\Windows\\%' ESCAPE '`'"
1015 [
'accent`_test`',
new LikeMatch(
'%' ) ],
1016 "LIKE 'accent```_test``%' ESCAPE '`'"
1026 $this->assertEquals( trim( $this->database->unionQueries(
1036 'sqls' => [
'RAW SQL',
'RAW2SQL' ],
1039 "(RAW SQL) UNION ALL (RAW2SQL)"
1043 'sqls' => [
'RAW SQL',
'RAW2SQL' ],
1046 "(RAW SQL) UNION (RAW2SQL)"
1050 'sqls' => [
'RAW SQL',
'RAW2SQL',
'RAW3SQL' ],
1053 "(RAW SQL) UNION (RAW2SQL) UNION (RAW3SQL)"
1063 if ( isset(
$params[
'unionSupportsOrderAndLimit'] ) ) {
1064 $this->database->setUnionSupportsOrderAndLimit(
$params[
'unionSupportsOrderAndLimit'] );
1067 $sql = trim( $this->database->unionConditionPermutations(
1076 $this->assertEquals( $expect, $sql );
1084 'table' => [
'table1',
'table2' ],
1085 'vars' => [
'field1',
'alias' =>
'field2' ],
1086 'permute_conds' => [
1087 'field3' => [ 1, 2, 3 ],
1088 'duplicates' => [ 4, 5, 4 ],
1092 'extra_conds' =>
'table2.bar > 23',
1094 'ORDER BY' => [
'field1',
'alias' ],
1095 'INNER ORDER BY' => [
'field1',
'field2' ],
1099 'table2' => [
'JOIN',
'table1.foo_id = table2.foo_id' ],
1102 "(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 " .
1103 "(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 " .
1104 "(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 " .
1105 "(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 " .
1106 "(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 " .
1107 "(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 ) " .
1108 "ORDER BY field1,alias LIMIT 100"
1113 'vars' => [
'foo_id' ],
1114 'permute_conds' => [
1115 'bar' => [ 1, 2, 3 ],
1117 'extra_conds' => [
'baz' =>
null ],
1120 'ORDER BY' => [
'foo_id' ],
1124 "(SELECT foo_id FROM foo WHERE bar = '1' AND baz IS NULL ORDER BY foo_id LIMIT 25 ) UNION " .
1125 "(SELECT foo_id FROM foo WHERE bar = '2' AND baz IS NULL ORDER BY foo_id LIMIT 25 ) UNION " .
1126 "(SELECT foo_id FROM foo WHERE bar = '3' AND baz IS NULL ORDER BY foo_id LIMIT 25 ) " .
1127 "ORDER BY foo_id LIMIT 25"
1132 'vars' => [
'foo_id' ],
1133 'permute_conds' => [
1134 'bar' => [ 1, 2, 3 ],
1136 'extra_conds' => [
'baz' =>
null ],
1139 'ORDER BY' => [
'foo_id' ],
1142 'unionSupportsOrderAndLimit' =>
false,
1144 "(SELECT foo_id FROM foo WHERE bar = '1' AND baz IS NULL ) UNION " .
1145 "(SELECT foo_id FROM foo WHERE bar = '2' AND baz IS NULL ) UNION " .
1146 "(SELECT foo_id FROM foo WHERE bar = '3' AND baz IS NULL ) " .
1147 "ORDER BY foo_id LIMIT 25"
1152 'vars' => [
'foo_id' ],
1153 'permute_conds' => [],
1154 'extra_conds' => [
'baz' =>
null ],
1156 'ORDER BY' => [
'foo_id' ],
1160 "SELECT foo_id FROM foo WHERE baz IS NULL ORDER BY foo_id LIMIT 25"
1165 'vars' => [
'foo_id' ],
1166 'permute_conds' => [
1169 'extra_conds' => [
'baz' =>
null ],
1171 'ORDER BY' => [
'foo_id' ],
1175 "SELECT foo_id FROM foo WHERE baz IS NULL ORDER BY foo_id LIMIT 25"
1180 'vars' => [
'foo_id' ],
1181 'permute_conds' => [
1185 'ORDER BY' => [
'foo_id' ],
1190 "SELECT foo_id FROM foo WHERE bar = '1' ORDER BY foo_id LIMIT 150,25"
1195 'vars' => [
'foo_id' ],
1196 'permute_conds' => [],
1197 'extra_conds' => [
'baz' =>
null ],
1199 'ORDER BY' => [
'foo_id' ],
1202 'INNER ORDER BY' => [
'bar_id' ],
1205 "(SELECT foo_id FROM foo WHERE baz IS NULL ORDER BY bar_id LIMIT 175 ) ORDER BY foo_id LIMIT 150,25"
1210 'vars' => [
'foo_id' ],
1211 'permute_conds' => [],
1212 'extra_conds' => [
'baz' =>
null ],
1214 'ORDER BY' => [
'foo_id' ],
1217 'INNER ORDER BY' => [
'bar_id' ],
1219 'unionSupportsOrderAndLimit' =>
false,
1221 "SELECT foo_id FROM foo WHERE baz IS NULL ORDER BY foo_id LIMIT 150,25"
1232 $this->database->begin( __METHOD__ );
1233 $this->database->commit( __METHOD__ );
1242 $this->database->begin( __METHOD__ );
1243 $this->database->rollback( __METHOD__ );
1251 $this->database->setExistingTables( [
'table' ] );
1252 $this->database->dropTable(
'table', __METHOD__ );
1261 $this->database->dropTable(
'non_existing', __METHOD__ )
1270 $this->assertEquals( trim( $this->database->makeList(
1278 [
'value',
'value2' ],
1283 [
'field',
'field2' ],
1288 [
'field' =>
'value',
'field2' =>
'value2' ],
1290 "field = 'value' AND field2 = 'value2'"
1293 [
'field' =>
null,
"field2 != 'value2'" ],
1295 "field IS NULL AND (field2 != 'value2')"
1298 [
'field' => [
'value',
null,
'value2' ],
'field2' =>
'value2' ],
1300 "(field IN ('value','value2') OR field IS NULL) AND field2 = 'value2'"
1303 [
'field' => [
null ],
'field2' =>
null ],
1305 "field IS NULL AND field2 IS NULL"
1308 [
'field' =>
'value',
'field2' =>
'value2' ],
1310 "field = 'value' OR field2 = 'value2'"
1313 [
'field' =>
'value',
'field2' =>
null ],
1315 "field = 'value' OR field2 IS NULL"
1318 [
'field' => [
'value',
'value2' ],
'field2' => [
'value' ] ],
1320 "field IN ('value','value2') OR field2 = 'value'"
1323 [
'field' => [
null,
'value',
null,
'value2' ],
"field2 != 'value2'" ],
1325 "(field IN ('value','value2') OR field IS NULL) OR (field2 != 'value2')"
1328 [
'field' =>
'value',
'field2' =>
'value2' ],
1330 "field = 'value',field2 = 'value2'"
1333 [
'field' =>
'value',
'field2' =>
null ],
1335 "field = 'value',field2 = NULL"
1338 [
'field' =>
'value',
"field2 != 'value2'" ],
1340 "field = 'value',field2 != 'value2'"
1349 $temp1 = $this->database->tableName(
'tmp_table_1' );
1350 $temp2 = $this->database->tableName(
'tmp_table_2' );
1351 $temp3 = $this->database->tableName(
'tmp_table_3' );
1353 $this->database->query(
"CREATE TEMPORARY TABLE $temp1 LIKE orig_tbl", __METHOD__ );
1354 $this->database->query(
"CREATE TEMPORARY TABLE $temp2 LIKE orig_tbl", __METHOD__ );
1355 $this->database->query(
"CREATE TEMPORARY TABLE $temp3 LIKE orig_tbl", __METHOD__ );
1357 $this->assertTrue( $this->database->tableExists(
"tmp_table_1", __METHOD__ ) );
1358 $this->assertTrue( $this->database->tableExists(
"tmp_table_2", __METHOD__ ) );
1359 $this->assertTrue( $this->database->tableExists(
"tmp_table_3", __METHOD__ ) );
1361 $this->database->dropTable(
'tmp_table_1', __METHOD__ );
1362 $this->database->dropTable(
'tmp_table_2', __METHOD__ );
1363 $this->database->dropTable(
'tmp_table_3', __METHOD__ );
1365 $this->assertFalse( $this->database->tableExists(
"tmp_table_1", __METHOD__ ) );
1366 $this->assertFalse( $this->database->tableExists(
"tmp_table_2", __METHOD__ ) );
1367 $this->assertFalse( $this->database->tableExists(
"tmp_table_3", __METHOD__ ) );
1369 $this->database->query(
"CREATE TEMPORARY TABLE tmp_table_1 LIKE orig_tbl", __METHOD__ );
1370 $this->database->query(
"CREATE TEMPORARY TABLE 'tmp_table_2' LIKE orig_tbl", __METHOD__ );
1371 $this->database->query(
"CREATE TEMPORARY TABLE `tmp_table_3` LIKE orig_tbl", __METHOD__ );
1373 $this->assertTrue( $this->database->tableExists(
"tmp_table_1", __METHOD__ ) );
1374 $this->assertTrue( $this->database->tableExists(
"tmp_table_2", __METHOD__ ) );
1375 $this->assertTrue( $this->database->tableExists(
"tmp_table_3", __METHOD__ ) );
1377 $this->database->query(
"DROP TEMPORARY TABLE tmp_table_1 LIKE orig_tbl", __METHOD__ );
1378 $this->database->query(
"DROP TEMPORARY TABLE 'tmp_table_2' LIKE orig_tbl", __METHOD__ );
1379 $this->database->query(
"DROP TABLE `tmp_table_3` LIKE orig_tbl", __METHOD__ );
1381 $this->assertFalse( $this->database->tableExists(
"tmp_table_1", __METHOD__ ) );
1382 $this->assertFalse( $this->database->tableExists(
"tmp_table_2", __METHOD__ ) );
1383 $this->assertFalse( $this->database->tableExists(
"tmp_table_3", __METHOD__ ) );
1387 yield [
'someField', 1, 2,
'SUBSTRING(someField FROM 1 FOR 2)' ];
1388 yield [
'someField', 1,
null,
'SUBSTRING(someField FROM 1)' ];
1396 $output = $this->database->buildSubstring(
$input, $start, $length );
1397 $this->assertSame( $expected,
$output );
1416 $this->database->buildSubstring(
'foo', $start, $length );
1423 $output = $this->database->buildIntegerCast(
'fieldName' );
1424 $this->assertSame(
'CAST( fieldName AS INTEGER )',
$output );
1437 $this->database->startAtomic( __METHOD__ );
1438 $this->database->endAtomic( __METHOD__ );
1441 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1442 $this->database->cancelAtomic( __METHOD__ );
1445 $this->database->begin( __METHOD__ );
1446 $this->database->startAtomic( __METHOD__ );
1447 $this->database->endAtomic( __METHOD__ );
1448 $this->database->commit( __METHOD__ );
1451 $this->database->begin( __METHOD__ );
1452 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1453 $this->database->endAtomic( __METHOD__ );
1454 $this->database->commit( __METHOD__ );
1456 $this->
assertLastSql(
'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; RELEASE SAVEPOINT wikimedia_rdbms_atomic1; COMMIT' );
1458 $this->database->begin( __METHOD__ );
1459 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1460 $this->database->cancelAtomic( __METHOD__ );
1461 $this->database->commit( __METHOD__ );
1463 $this->
assertLastSql(
'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; COMMIT' );
1465 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1466 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1467 $this->database->cancelAtomic( __METHOD__ );
1468 $this->database->endAtomic( __METHOD__ );
1470 $this->
assertLastSql(
'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; COMMIT' );
1472 $noOpCallack =
function () {
1475 $this->database->doAtomicSection( __METHOD__, $noOpCallack, IDatabase::ATOMIC_CANCELABLE );
1478 $this->database->doAtomicSection( __METHOD__, $noOpCallack );
1481 $this->database->begin( __METHOD__ );
1482 $this->database->doAtomicSection( __METHOD__, $noOpCallack, IDatabase::ATOMIC_CANCELABLE );
1483 $this->database->rollback( __METHOD__ );
1485 $this->
assertLastSql(
'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; RELEASE SAVEPOINT wikimedia_rdbms_atomic1; ROLLBACK' );
1490 IDatabase::TRIGGER_COMMIT =>
'tCommit',
1491 IDatabase::TRIGGER_ROLLBACK =>
'tRollback'
1494 $this->database->query(
"SELECT 0",
$fname );
1496 $callback1 =
function ( $trigger =
'-' )
use (
$fname, $triggerMap ) {
1497 $this->database->query(
"SELECT 1, {$triggerMap[$trigger]} AS t",
$fname );
1499 $callback2 =
function ( $trigger =
'-' )
use (
$fname, $triggerMap ) {
1500 $this->database->query(
"SELECT 2, {$triggerMap[$trigger]} AS t",
$fname );
1502 $callback3 =
function ( $trigger =
'-' )
use (
$fname, $triggerMap ) {
1503 $this->database->query(
"SELECT 3, {$triggerMap[$trigger]} AS t",
$fname );
1506 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1507 $this->database->onTransactionPreCommitOrIdle( $pcCallback, __METHOD__ );
1508 $this->database->cancelAtomic( __METHOD__ );
1511 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1512 $this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ );
1513 $this->database->cancelAtomic( __METHOD__ );
1516 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1517 $this->database->onTransactionResolution( $callback1, __METHOD__ );
1518 $this->database->cancelAtomic( __METHOD__ );
1519 $this->
assertLastSql(
'BEGIN; ROLLBACK; SELECT 1, tRollback AS t' );
1521 $this->database->startAtomic( __METHOD__ .
'_outer' );
1522 $this->database->onTransactionPreCommitOrIdle( $pcCallback, __METHOD__ );
1523 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1524 $this->database->onTransactionPreCommitOrIdle( $pcCallback, __METHOD__ );
1525 $this->database->cancelAtomic( __METHOD__ );
1526 $this->database->onTransactionPreCommitOrIdle( $pcCallback, __METHOD__ );
1527 $this->database->endAtomic( __METHOD__ .
'_outer' );
1530 'SAVEPOINT wikimedia_rdbms_atomic1',
1531 'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1',
1537 $this->database->startAtomic( __METHOD__ .
'_outer' );
1538 $this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ );
1539 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1540 $this->database->onTransactionCommitOrIdle( $callback2, __METHOD__ );
1541 $this->database->cancelAtomic( __METHOD__ );
1542 $this->database->onTransactionCommitOrIdle( $callback3, __METHOD__ );
1543 $this->database->endAtomic( __METHOD__ .
'_outer' );
1546 'SAVEPOINT wikimedia_rdbms_atomic1',
1547 'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1',
1549 'SELECT 1, tCommit AS t',
1550 'SELECT 3, tCommit AS t'
1553 $this->database->startAtomic( __METHOD__ .
'_outer' );
1554 $this->database->onTransactionResolution( $callback1, __METHOD__ );
1555 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1556 $this->database->onTransactionResolution( $callback2, __METHOD__ );
1557 $this->database->cancelAtomic( __METHOD__ );
1558 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1559 $this->database->endAtomic( __METHOD__ .
'_outer' );
1562 'SAVEPOINT wikimedia_rdbms_atomic1',
1563 'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1',
1565 'SELECT 1, tCommit AS t',
1566 'SELECT 2, tRollback AS t',
1567 'SELECT 3, tCommit AS t'
1570 $makeCallback =
function ( $id )
use (
$fname, $triggerMap ) {
1571 return function ( $trigger =
'-' )
use ( $id,
$fname, $triggerMap ) {
1572 $this->database->query(
"SELECT $id, {$triggerMap[$trigger]} AS t",
$fname );
1576 $this->database->startAtomic( __METHOD__ .
'_outer' );
1577 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1578 $this->database->onTransactionResolution( $makeCallback( 1 ), __METHOD__ );
1579 $this->database->cancelAtomic( __METHOD__ );
1580 $this->database->endAtomic( __METHOD__ .
'_outer' );
1583 'SAVEPOINT wikimedia_rdbms_atomic1',
1584 'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1',
1586 'SELECT 1, tRollback AS t'
1589 $this->database->startAtomic( __METHOD__ .
'_level1', IDatabase::ATOMIC_CANCELABLE );
1590 $this->database->onTransactionResolution( $makeCallback( 1 ), __METHOD__ );
1591 $this->database->startAtomic( __METHOD__ .
'_level2' );
1592 $this->database->startAtomic( __METHOD__ .
'_level3', IDatabase::ATOMIC_CANCELABLE );
1593 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1594 $this->database->onTransactionResolution( $makeCallback( 2 ), __METHOD__ );
1595 $this->database->endAtomic( __METHOD__ );
1596 $this->database->onTransactionResolution( $makeCallback( 3 ), __METHOD__ );
1597 $this->database->cancelAtomic( __METHOD__ .
'_level3' );
1598 $this->database->endAtomic( __METHOD__ .
'_level2' );
1599 $this->database->onTransactionResolution( $makeCallback( 4 ), __METHOD__ );
1600 $this->database->endAtomic( __METHOD__ .
'_level1' );
1603 'SAVEPOINT wikimedia_rdbms_atomic1',
1604 'SAVEPOINT wikimedia_rdbms_atomic2',
1605 'RELEASE SAVEPOINT wikimedia_rdbms_atomic2',
1606 'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1',
1607 'COMMIT; SELECT 1, tCommit AS t',
1608 'SELECT 2, tRollback AS t',
1609 'SELECT 3, tRollback AS t',
1610 'SELECT 4, tCommit AS t'
1624 $this->database->begin( __METHOD__ );
1626 $this->database->doAtomicSection(
1629 $this->database->startAtomic(
'inner_func1' );
1630 $this->database->startAtomic(
'inner_func2' );
1632 throw new RuntimeException(
'Test exception' );
1634 IDatabase::ATOMIC_CANCELABLE
1636 $this->fail(
'Expected exception not thrown' );
1637 }
catch ( RuntimeException $ex ) {
1638 $this->assertSame(
'Test exception', $ex->getMessage() );
1640 $this->database->commit( __METHOD__ );
1642 $this->
assertLastSql(
'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; COMMIT' );
1644 $this->database->begin( __METHOD__ );
1646 $this->database->doAtomicSection(
1649 throw new RuntimeException(
'Test exception' );
1652 $this->fail(
'Test exception not thrown' );
1653 }
catch ( RuntimeException $ex ) {
1654 $this->assertSame(
'Test exception', $ex->getMessage() );
1657 $this->database->commit( __METHOD__ );
1658 $this->fail(
'Test exception not thrown' );
1661 'Cannot execute query from ' . __METHOD__ .
' while transaction status is ERROR.',
1665 $this->database->rollback( __METHOD__ );
1680 $callback1Called =
null;
1681 $callback1 =
function ( $trigger =
'-' )
use (
$fname, &$callback1Called ) {
1682 $callback1Called = $trigger;
1683 $this->database->query(
"SELECT 1",
$fname );
1685 $callback2Called =
null;
1686 $callback2 =
function ( $trigger =
'-' )
use (
$fname, &$callback2Called ) {
1687 $callback2Called = $trigger;
1688 $this->database->query(
"SELECT 2",
$fname );
1690 $callback3Called =
null;
1691 $callback3 =
function ( $trigger =
'-' )
use (
$fname, &$callback3Called ) {
1692 $callback3Called = $trigger;
1693 $this->database->query(
"SELECT 3",
$fname );
1696 $this->database->startAtomic( __METHOD__ .
'_outer' );
1697 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1698 $this->database->startAtomic( __METHOD__ .
'_inner' );
1699 $this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ );
1700 $this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ );
1701 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1702 $this->database->endAtomic( __METHOD__ .
'_inner' );
1703 $this->database->cancelAtomic( __METHOD__ );
1704 $this->database->endAtomic( __METHOD__ .
'_outer' );
1705 $this->assertNull( $callback1Called );
1706 $this->assertNull( $callback2Called );
1707 $this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called );
1709 $this->
assertLastSql(
'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; COMMIT; SELECT 3' );
1711 $callback1Called =
null;
1712 $callback2Called =
null;
1713 $callback3Called =
null;
1714 $this->database->startAtomic( __METHOD__ .
'_outer' );
1715 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1716 $this->database->startAtomic( __METHOD__ .
'_inner', IDatabase::ATOMIC_CANCELABLE );
1717 $this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ );
1718 $this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ );
1719 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1720 $this->database->endAtomic( __METHOD__ .
'_inner' );
1721 $this->database->cancelAtomic( __METHOD__ );
1722 $this->database->endAtomic( __METHOD__ .
'_outer' );
1723 $this->assertNull( $callback1Called );
1724 $this->assertNull( $callback2Called );
1725 $this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called );
1727 $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' );
1729 $callback1Called =
null;
1730 $callback2Called =
null;
1731 $callback3Called =
null;
1732 $this->database->startAtomic( __METHOD__ .
'_outer' );
1733 $atomicId = $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1734 $this->database->startAtomic( __METHOD__ .
'_inner' );
1735 $this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ );
1736 $this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ );
1737 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1738 $this->database->cancelAtomic( __METHOD__, $atomicId );
1739 $this->database->endAtomic( __METHOD__ .
'_outer' );
1740 $this->assertNull( $callback1Called );
1741 $this->assertNull( $callback2Called );
1742 $this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called );
1744 $callback1Called =
null;
1745 $callback2Called =
null;
1746 $callback3Called =
null;
1747 $this->database->startAtomic( __METHOD__ .
'_outer' );
1748 $atomicId = $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1749 $this->database->startAtomic( __METHOD__ .
'_inner' );
1750 $this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ );
1751 $this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ );
1752 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1754 $this->database->cancelAtomic( __METHOD__ .
'_X', $atomicId );
1758 "Invalid atomic section ended (got {$m}_X but expected {$m}).",
1762 $this->database->cancelAtomic( __METHOD__ );
1763 $this->database->endAtomic( __METHOD__ .
'_outer' );
1764 $this->assertNull( $callback1Called );
1765 $this->assertNull( $callback2Called );
1766 $this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called );
1768 $this->database->startAtomic( __METHOD__ .
'_outer' );
1769 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1770 $this->database->startAtomic( __METHOD__ .
'_inner' );
1771 $this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ );
1772 $this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ );
1773 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1774 $this->database->cancelAtomic( __METHOD__ .
'_inner' );
1775 $this->database->cancelAtomic( __METHOD__ );
1776 $this->database->endAtomic( __METHOD__ .
'_outer' );
1777 $this->assertNull( $callback1Called );
1778 $this->assertNull( $callback2Called );
1779 $this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called );
1781 $wrapper = TestingAccessWrapper::newFromObject( $this->database );
1782 $callback1Called =
null;
1783 $callback2Called =
null;
1784 $callback3Called =
null;
1785 $this->database->startAtomic( __METHOD__ .
'_outer' );
1786 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1787 $this->database->startAtomic( __METHOD__ .
'_inner' );
1788 $this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ );
1789 $this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ );
1790 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1791 $wrapper->trxStatus = Database::STATUS_TRX_ERROR;
1792 $this->database->cancelAtomic( __METHOD__ .
'_inner' );
1793 $this->database->cancelAtomic( __METHOD__ );
1794 $this->database->endAtomic( __METHOD__ .
'_outer' );
1795 $this->assertNull( $callback1Called );
1796 $this->assertNull( $callback2Called );
1797 $this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called );
1811 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1812 $this->database->query(
'SELECT 1', __METHOD__ );
1813 $this->database->endAtomic( __METHOD__ );
1814 $this->database->commit( __METHOD__, IDatabase::FLUSHING_ALL_PEERS );
1816 $this->
assertLastSql(
'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; SELECT 1; RELEASE SAVEPOINT wikimedia_rdbms_atomic1; COMMIT' );
1833 $this->database->$method( __METHOD__ );
1834 $this->fail(
'Expected exception not thrown' );
1837 'No atomic section is open (got ' . __METHOD__ .
').',
1849 $this->database->startAtomic( __METHOD__ .
'X' );
1851 $this->database->$method( __METHOD__ );
1852 $this->fail(
'Expected exception not thrown' );
1855 'Invalid atomic section ended (got ' . __METHOD__ .
' but expected ' .
1866 $this->database->startAtomic( __METHOD__ );
1868 $this->database->cancelAtomic( __METHOD__ );
1869 $this->database->select(
'test',
'1', [], __METHOD__ );
1870 $this->fail(
'Expected exception not thrown' );
1873 'Cannot execute query from ' . __METHOD__ .
' while transaction status is ERROR.',
1884 $wrapper = TestingAccessWrapper::newFromObject( $this->database );
1886 $this->database->begin( __METHOD__ );
1887 $wrapper->trxStatus = Database::STATUS_TRX_ERROR;
1888 $this->database->delete(
'x', [
'field' => 3 ], __METHOD__ );
1889 $this->database->commit( __METHOD__ );
1896 $wrapper = TestingAccessWrapper::newFromObject( $this->database );
1898 $this->database->startAtomic( __METHOD__ );
1899 $wrapper->trxStatus = Database::STATUS_TRX_ERROR;
1900 $this->database->rollback( __METHOD__ );
1901 $this->assertEquals( 0, $this->database->trxLevel() );
1902 $this->assertEquals( Database::STATUS_TRX_NONE, $wrapper->trxStatus() );
1905 $this->database->startAtomic( __METHOD__ );
1906 $this->assertEquals( Database::STATUS_TRX_OK, $wrapper->trxStatus() );
1907 $this->database->delete(
'x', [
'field' => 1 ], __METHOD__ );
1908 $this->database->endAtomic( __METHOD__ );
1909 $this->assertEquals( Database::STATUS_TRX_NONE, $wrapper->trxStatus() );
1910 $this->
assertLastSql(
'BEGIN; DELETE FROM x WHERE field = \'1\'; COMMIT' );
1911 $this->assertEquals( 0, $this->database->trxLevel(),
'Use after rollback()' );
1913 $this->database->begin( __METHOD__ );
1914 $this->database->startAtomic( __METHOD__, Database::ATOMIC_CANCELABLE );
1915 $this->database->update(
'y', [
'a' => 1 ], [
'field' => 1 ], __METHOD__ );
1916 $wrapper->trxStatus = Database::STATUS_TRX_ERROR;
1917 $this->database->cancelAtomic( __METHOD__ );
1918 $this->assertEquals( Database::STATUS_TRX_OK, $wrapper->trxStatus() );
1919 $this->database->startAtomic( __METHOD__ );
1920 $this->database->delete(
'y', [
'field' => 1 ], __METHOD__ );
1921 $this->database->endAtomic( __METHOD__ );
1922 $this->database->commit( __METHOD__ );
1924 $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' );
1925 $this->assertEquals( 0, $this->database->trxLevel(),
'Use after rollback()' );
1928 $this->database->startAtomic( __METHOD__ );
1929 $this->assertEquals( Database::STATUS_TRX_OK, $wrapper->trxStatus() );
1930 $this->database->delete(
'x', [
'field' => 3 ], __METHOD__ );
1931 $this->database->endAtomic( __METHOD__ );
1932 $this->assertEquals( Database::STATUS_TRX_NONE, $wrapper->trxStatus() );
1933 $this->
assertLastSql(
'BEGIN; DELETE FROM x WHERE field = \'3\'; COMMIT' );
1934 $this->assertEquals( 0, $this->database->trxLevel() );
1941 $doError =
function () {
1942 $this->database->forceNextQueryError( 666,
'Evilness' );
1944 $this->database->delete(
'error',
'1', __CLASS__ .
'::SomeCaller' );
1945 $this->fail(
'Expected exception not thrown' );
1946 }
catch ( DBError
$e ) {
1947 $this->assertSame( 666,
$e->errno );
1954 $this->database->begin( __METHOD__, Database::TRANSACTION_INTERNAL );
1955 call_user_func( $doError );
1957 $this->database->delete(
'x', [
'field' => 1 ], __METHOD__ );
1958 $this->fail(
'Expected exception not thrown' );
1960 $this->assertEquals(
1961 'Cannot execute query from ' . __METHOD__ .
' while transaction status is ERROR.',
1966 $this->database->commit( __METHOD__, Database::FLUSHING_INTERNAL );
1967 $this->fail(
'Expected exception not thrown' );
1969 $this->assertEquals(
1970 'Cannot execute query from ' . __METHOD__ .
' while transaction status is ERROR.',
1974 $this->database->rollback( __METHOD__, Database::FLUSHING_INTERNAL );
1975 $this->
assertLastSql(
'BEGIN; DELETE FROM error WHERE 1; ROLLBACK' );
1978 $this->database->begin( __METHOD__, Database::TRANSACTION_INTERNAL );
1979 $this->database->delete(
'x', [
'field' => 1 ], __METHOD__ );
1980 call_user_func( $doError );
1982 $this->database->delete(
'x', [
'field' => 1 ], __METHOD__ );
1983 $this->fail(
'Expected exception not thrown' );
1986 $this->database->rollback( __METHOD__, Database::FLUSHING_INTERNAL );
1988 $this->
assertLastSql(
'BEGIN; DELETE FROM x WHERE field = \'1\'; DELETE FROM error WHERE 1; ROLLBACK' );
1995 $wrapper = TestingAccessWrapper::newFromObject( $this->database );
1997 $wrapper->deprecationLogger =
function ( $msg )
use ( &$warning ) {
2001 $doError =
function () {
2002 $this->database->forceNextQueryError( 666,
'Evilness', [
2003 'wasKnownStatementRollbackError' =>
true,
2006 $this->database->delete(
'error',
'1', __CLASS__ .
'::SomeCaller' );
2007 $this->fail(
'Expected exception not thrown' );
2008 }
catch ( DBError
$e ) {
2009 $this->assertSame( 666,
$e->errno );
2012 $expectWarning =
'Caller from ' . __METHOD__ .
2013 ' ignored an error originally raised from ' . __CLASS__ .
'::SomeCaller: [666] Evilness';
2017 $this->database->startAtomic( __METHOD__ );
2018 call_user_func( $doError );
2019 $this->database->rollback( __METHOD__ );
2020 $this->database->delete(
'x', [
'field' => 1 ], __METHOD__ );
2021 $this->assertSame( [], $warning );
2023 $this->
assertLastSql(
'BEGIN; DELETE FROM error WHERE 1; ROLLBACK; DELETE FROM x WHERE field = \'1\'' );
2027 $this->database->begin( __METHOD__ );
2028 $this->database->startAtomic( __METHOD__, Database::ATOMIC_CANCELABLE );
2029 call_user_func( $doError );
2030 $this->database->cancelAtomic( __METHOD__ );
2031 $this->database->delete(
'x', [
'field' => 1 ], __METHOD__ );
2032 $this->database->commit( __METHOD__ );
2033 $this->assertSame( [], $warning );
2035 $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' );
2039 $this->database->begin( __METHOD__ );
2040 call_user_func( $doError );
2041 $this->database->commit( __METHOD__ );
2042 $this->assertSame( [ $expectWarning ], $warning );
2043 $this->
assertLastSql(
'BEGIN; DELETE FROM error WHERE 1; COMMIT' );
2047 $this->database->begin( __METHOD__ );
2048 call_user_func( $doError );
2049 $this->database->delete(
'x', [
'field' => 1 ], __METHOD__ );
2050 $this->database->commit( __METHOD__ );
2051 $this->assertSame( [ $expectWarning ], $warning );
2053 $this->
assertLastSql(
'BEGIN; DELETE FROM error WHERE 1; DELETE FROM x WHERE field = \'1\'; COMMIT' );
2061 $this->database->begin( __METHOD__ );
2062 $this->database->onTransactionCommitOrIdle(
function ()
use (
$fname ) {
2063 $this->database->query(
'SELECT 1',
$fname );
2065 $this->database->onTransactionResolution(
function ()
use (
$fname ) {
2066 $this->database->query(
'SELECT 2',
$fname );
2068 $this->database->delete(
'x', [
'field' => 3 ], __METHOD__ );
2070 $this->database->close();
2071 $this->fail(
'Expected exception not thrown' );
2074 "Wikimedia\Rdbms\Database::close: transaction is still open (from $fname).",
2079 $this->assertFalse( $this->database->isOpen() );
2080 $this->
assertLastSql(
'BEGIN; DELETE FROM x WHERE field = \'3\'; ROLLBACK; SELECT 2' );
2081 $this->assertEquals( 0, $this->database->trxLevel() );
2090 $this->database->startAtomic( __METHOD__ );
2091 $this->database->onTransactionCommitOrIdle(
function ()
use (
$fname ) {
2092 $this->database->query(
'SELECT 1',
$fname );
2094 $this->database->delete(
'x', [
'field' => 3 ], __METHOD__ );
2095 $this->database->close();
2096 $this->fail(
'Expected exception not thrown' );
2099 'Wikimedia\Rdbms\Database::close: atomic sections ' .
2100 'DatabaseSQLTest::testPrematureClose2 are still open.',
2105 $this->assertFalse( $this->database->isOpen() );
2106 $this->
assertLastSql(
'BEGIN; DELETE FROM x WHERE field = \'3\'; ROLLBACK' );
2107 $this->assertEquals( 0, $this->database->trxLevel() );
2116 $this->database->delete(
'x', [
'field' => 3 ], __METHOD__ );
2117 $this->assertEquals( 1, $this->database->trxLevel() );
2118 $this->database->close();
2119 $this->fail(
'Expected exception not thrown' );
2122 'Wikimedia\Rdbms\Database::close: ' .
2123 'mass commit/rollback of peer transaction required (DBO_TRX set).',
2128 $this->assertFalse( $this->database->isOpen() );
2129 $this->
assertLastSql(
'BEGIN; DELETE FROM x WHERE field = \'3\'; ROLLBACK' );
2130 $this->assertEquals( 0, $this->database->trxLevel() );
2138 $this->database->query(
'SELECT 1', __METHOD__ );
2139 $this->assertEquals( 1, $this->database->trxLevel() );
2140 $this->database->close();
2143 $this->assertFalse( $this->database->isOpen() );
2145 $this->assertEquals( 0, $this->database->trxLevel() );
2152 $this->database->forceNextResult( [
2153 (
object)[
'value' =>
'row1' ],
2154 (
object)[
'value' =>
'row2' ],
2155 (
object)[
'value' =>
'row3' ],
2159 [
'row1',
'row2',
'row3' ],
2160 $this->database->selectFieldValues(
'table',
'table.field',
'conds', __METHOD__ )
2162 $this->
assertLastSql(
'SELECT table.field AS value FROM table WHERE conds' );