MediaWiki REL1_33
DatabaseSQLTest.php
Go to the documentation of this file.
1<?php
2
6use Wikimedia\TestingAccessWrapper;
10
15class DatabaseSQLTest extends PHPUnit\Framework\TestCase {
16
17 use MediaWikiCoversValidator;
18 use PHPUnit4And6Compat;
19
21 private $database;
22
23 protected function setUp() {
24 parent::setUp();
25 $this->database = new DatabaseTestHelper( __CLASS__, [ 'cliMode' => true ] );
26 }
27
28 protected function assertLastSql( $sqlText ) {
29 $this->assertEquals(
30 $sqlText,
31 $this->database->getLastSqls()
32 );
33 }
34
35 protected function assertLastSqlDb( $sqlText, DatabaseTestHelper $db ) {
36 $this->assertEquals( $sqlText, $db->getLastSqls() );
37 }
38
52 public function testSelect( $sql, $sqlText ) {
53 $this->database->select(
54 $sql['tables'],
55 $sql['fields'],
56 $sql['conds'] ?? [],
57 __METHOD__,
58 $sql['options'] ?? [],
59 $sql['join_conds'] ?? []
60 );
61 $this->assertLastSql( $sqlText );
62 }
63
64 public static function provideSelect() {
65 return [
66 [
67 [
68 'tables' => 'table',
69 'fields' => [ 'field', 'alias' => 'field2' ],
70 'conds' => [ 'alias' => 'text' ],
71 ],
72 "SELECT field,field2 AS alias " .
73 "FROM table " .
74 "WHERE alias = 'text'"
75 ],
76 [
77 [
78 'tables' => 'table',
79 'fields' => [ 'field', 'alias' => 'field2' ],
80 'conds' => 'alias = \'text\'',
81 ],
82 "SELECT field,field2 AS alias " .
83 "FROM table " .
84 "WHERE alias = 'text'"
85 ],
86 [
87 [
88 'tables' => 'table',
89 'fields' => [ 'field', 'alias' => 'field2' ],
90 'conds' => [],
91 ],
92 "SELECT field,field2 AS alias " .
93 "FROM table"
94 ],
95 [
96 [
97 'tables' => 'table',
98 'fields' => [ 'field', 'alias' => 'field2' ],
99 'conds' => '',
100 ],
101 "SELECT field,field2 AS alias " .
102 "FROM table"
103 ],
104 [
105 [
106 'tables' => 'table',
107 'fields' => [ 'field', 'alias' => 'field2' ],
108 'conds' => '0', // T188314
109 ],
110 "SELECT field,field2 AS alias " .
111 "FROM table " .
112 "WHERE 0"
113 ],
114 [
115 [
116 // 'tables' with space prepended indicates pre-escaped table name
117 'tables' => ' table LEFT JOIN table2',
118 'fields' => [ 'field' ],
119 'conds' => [ 'field' => 'text' ],
120 ],
121 "SELECT field FROM table LEFT JOIN table2 WHERE field = 'text'"
122 ],
123 [
124 [
125 // Empty 'tables' is allowed
126 'tables' => '',
127 'fields' => [ 'SPECIAL_QUERY()' ],
128 ],
129 "SELECT SPECIAL_QUERY()"
130 ],
131 [
132 [
133 'tables' => 'table',
134 'fields' => [ 'field', 'alias' => 'field2' ],
135 'conds' => [ 'alias' => 'text' ],
136 'options' => [ 'LIMIT' => 1, 'ORDER BY' => 'field' ],
137 ],
138 "SELECT field,field2 AS alias " .
139 "FROM table " .
140 "WHERE alias = 'text' " .
141 "ORDER BY field " .
142 "LIMIT 1"
143 ],
144 [
145 [
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'
152 ] ],
153 ],
154 "SELECT tid,field,field2 AS alias,t2.id " .
155 "FROM table LEFT JOIN table2 t2 ON ((tid = t2.id)) " .
156 "WHERE alias = 'text' " .
157 "ORDER BY field " .
158 "LIMIT 1"
159 ],
160 [
161 [
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'
168 ] ],
169 ],
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 " .
174 "LIMIT 1"
175 ],
176 [
177 [
178 'tables' => [ 'table', 't2' => 'table2' ],
179 'fields' => [ 'tid', 'field', 'alias' => 'field2', 't2.id' ],
180 'conds' => [ 'alias' => 'text' ],
181 'options' => [
182 'LIMIT' => 1,
183 'GROUP BY' => [ 'field', 'field2' ],
184 'HAVING' => [ 'COUNT(*) > 1', 'field' => 1 ]
185 ],
186 'join_conds' => [ 't2' => [
187 'LEFT JOIN', 'tid = t2.id'
188 ] ],
189 ],
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' " .
194 "LIMIT 1"
195 ],
196 [
197 [
198 'tables' => [ 'table' ],
199 'fields' => [ 'alias' => 'field' ],
200 'conds' => [ 'alias' => [ 1, 2, 3, 4 ] ],
201 ],
202 "SELECT field AS alias " .
203 "FROM table " .
204 "WHERE alias IN ('1','2','3','4')"
205 ],
206 [
207 [
208 'tables' => 'table',
209 'fields' => [ 'field' ],
210 'options' => [ 'USE INDEX' => [ 'table' => 'X' ] ],
211 ],
212 // No-op by default
213 "SELECT field FROM table"
214 ],
215 [
216 [
217 'tables' => 'table',
218 'fields' => [ 'field' ],
219 'options' => [ 'IGNORE INDEX' => [ 'table' => 'X' ] ],
220 ],
221 // No-op by default
222 "SELECT field FROM table"
223 ],
224 [
225 [
226 'tables' => 'table',
227 'fields' => [ 'field' ],
228 'options' => [ 'DISTINCT' ],
229 ],
230 "SELECT DISTINCT field FROM table"
231 ],
232 [
233 [
234 'tables' => 'table',
235 'fields' => [ 'field' ],
236 'options' => [ 'LOCK IN SHARE MODE' ],
237 ],
238 "SELECT field FROM table LOCK IN SHARE MODE"
239 ],
240 [
241 [
242 'tables' => 'table',
243 'fields' => [ 'field' ],
244 'options' => [ 'EXPLAIN' => true ],
245 ],
246 'EXPLAIN SELECT field FROM table'
247 ],
248 [
249 [
250 'tables' => 'table',
251 'fields' => [ 'field' ],
252 'options' => [ 'FOR UPDATE' ],
253 ],
254 "SELECT field FROM table FOR UPDATE"
255 ],
256 ];
257 }
258
263 public function testLockForUpdate( $sql, $sqlText ) {
264 $this->database->startAtomic( __METHOD__ );
265 $this->database->lockForUpdate(
266 $sql['tables'],
267 $sql['conds'] ?? [],
268 __METHOD__,
269 $sql['options'] ?? [],
270 $sql['join_conds'] ?? []
271 );
272 $this->database->endAtomic( __METHOD__ );
273
274 $this->assertLastSql( "BEGIN; $sqlText; COMMIT" );
275 }
276
277 public static function provideLockForUpdate() {
278 return [
279 [
280 [
281 'tables' => [ 'table' ],
282 'conds' => [ 'field' => [ 1, 2, 3, 4 ] ],
283 ],
284 "SELECT COUNT(*) AS rowcount FROM " .
285 "(SELECT 1 FROM table WHERE field IN ('1','2','3','4') " .
286 "FOR UPDATE) tmp_count"
287 ],
288 [
289 [
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'
295 ] ],
296 ],
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"
300 ],
301 [
302 [
303 'tables' => 'table',
304 ],
305 "SELECT COUNT(*) AS rowcount FROM " .
306 "(SELECT 1 FROM table FOR UPDATE) tmp_count"
307 ],
308 ];
309 }
310
317 public function testSelectRowCount( $sql, $sqlText ) {
318 $this->database->selectRowCount(
319 $sql['tables'],
320 $sql['field'],
321 $sql['conds'] ?? [],
322 __METHOD__,
323 $sql['options'] ?? [],
324 $sql['join_conds'] ?? []
325 );
326 $this->assertLastSql( $sqlText );
327 }
328
329 public static function provideSelectRowCount() {
330 return [
331 [
332 [
333 'tables' => 'table',
334 'field' => [ '*' ],
335 'conds' => [ 'field' => 'text' ],
336 ],
337 "SELECT COUNT(*) AS rowcount FROM " .
338 "(SELECT 1 FROM table WHERE field = 'text' ) tmp_count"
339 ],
340 [
341 [
342 'tables' => 'table',
343 'field' => [ 'column' ],
344 'conds' => [ 'field' => 'text' ],
345 ],
346 "SELECT COUNT(*) AS rowcount FROM " .
347 "(SELECT 1 FROM table WHERE field = 'text' AND (column IS NOT NULL) ) tmp_count"
348 ],
349 [
350 [
351 'tables' => 'table',
352 'field' => [ 'alias' => 'column' ],
353 'conds' => [ 'field' => 'text' ],
354 ],
355 "SELECT COUNT(*) AS rowcount FROM " .
356 "(SELECT 1 FROM table WHERE field = 'text' AND (column IS NOT NULL) ) tmp_count"
357 ],
358 [
359 [
360 'tables' => 'table',
361 'field' => [ 'alias' => 'column' ],
362 'conds' => '',
363 ],
364 "SELECT COUNT(*) AS rowcount FROM " .
365 "(SELECT 1 FROM table WHERE (column IS NOT NULL) ) tmp_count"
366 ],
367 [
368 [
369 'tables' => 'table',
370 'field' => [ 'alias' => 'column' ],
371 'conds' => false,
372 ],
373 "SELECT COUNT(*) AS rowcount FROM " .
374 "(SELECT 1 FROM table WHERE (column IS NOT NULL) ) tmp_count"
375 ],
376 [
377 [
378 'tables' => 'table',
379 'field' => [ 'alias' => 'column' ],
380 'conds' => null,
381 ],
382 "SELECT COUNT(*) AS rowcount FROM " .
383 "(SELECT 1 FROM table WHERE (column IS NOT NULL) ) tmp_count"
384 ],
385 [
386 [
387 'tables' => 'table',
388 'field' => [ 'alias' => 'column' ],
389 'conds' => '1',
390 ],
391 "SELECT COUNT(*) AS rowcount FROM " .
392 "(SELECT 1 FROM table WHERE (1) AND (column IS NOT NULL) ) tmp_count"
393 ],
394 [
395 [
396 'tables' => 'table',
397 'field' => [ 'alias' => 'column' ],
398 'conds' => '0',
399 ],
400 "SELECT COUNT(*) AS rowcount FROM " .
401 "(SELECT 1 FROM table WHERE (0) AND (column IS NOT NULL) ) tmp_count"
402 ],
403 ];
404 }
405
412 public function testUpdate( $sql, $sqlText ) {
413 $this->database->update(
414 $sql['table'],
415 $sql['values'],
416 $sql['conds'],
417 __METHOD__,
418 $sql['options'] ?? []
419 );
420 $this->assertLastSql( $sqlText );
421 }
422
423 public static function provideUpdate() {
424 return [
425 [
426 [
427 'table' => 'table',
428 'values' => [ 'field' => 'text', 'field2' => 'text2' ],
429 'conds' => [ 'alias' => 'text' ],
430 ],
431 "UPDATE table " .
432 "SET field = 'text'" .
433 ",field2 = 'text2' " .
434 "WHERE alias = 'text'"
435 ],
436 [
437 [
438 'table' => 'table',
439 'values' => [ 'field = other', 'field2' => 'text2' ],
440 'conds' => [ 'id' => '1' ],
441 ],
442 "UPDATE table " .
443 "SET field = other" .
444 ",field2 = 'text2' " .
445 "WHERE id = '1'"
446 ],
447 [
448 [
449 'table' => 'table',
450 'values' => [ 'field = other', 'field2' => 'text2' ],
451 'conds' => '*',
452 ],
453 "UPDATE table " .
454 "SET field = other" .
455 ",field2 = 'text2'"
456 ],
457 ];
458 }
459
464 public function testDelete( $sql, $sqlText ) {
465 $this->database->delete(
466 $sql['table'],
467 $sql['conds'],
468 __METHOD__
469 );
470 $this->assertLastSql( $sqlText );
471 }
472
473 public static function provideDelete() {
474 return [
475 [
476 [
477 'table' => 'table',
478 'conds' => [ 'alias' => 'text' ],
479 ],
480 "DELETE FROM table " .
481 "WHERE alias = 'text'"
482 ],
483 [
484 [
485 'table' => 'table',
486 'conds' => '*',
487 ],
488 "DELETE FROM table"
489 ],
490 ];
491 }
492
497 public function testUpsert( $sql, $sqlText ) {
498 $this->database->upsert(
499 $sql['table'],
500 $sql['rows'],
501 $sql['uniqueIndexes'],
502 $sql['set'],
503 __METHOD__
504 );
505 $this->assertLastSql( $sqlText );
506 }
507
508 public static function provideUpsert() {
509 return [
510 [
511 [
512 'table' => 'upsert_table',
513 'rows' => [ 'field' => 'text', 'field2' => 'text2' ],
514 'uniqueIndexes' => [ 'field' ],
515 'set' => [ 'field' => 'set' ],
516 ],
517 "BEGIN; " .
518 "UPDATE upsert_table " .
519 "SET field = 'set' " .
520 "WHERE ((field = 'text')); " .
521 "INSERT IGNORE INTO upsert_table " .
522 "(field,field2) " .
523 "VALUES ('text','text2'); " .
524 "COMMIT"
525 ],
526 ];
527 }
528
533 public function testDeleteJoin( $sql, $sqlText ) {
534 $this->database->deleteJoin(
535 $sql['delTable'],
536 $sql['joinTable'],
537 $sql['delVar'],
538 $sql['joinVar'],
539 $sql['conds'],
540 __METHOD__
541 );
542 $this->assertLastSql( $sqlText );
543 }
544
545 public static function provideDeleteJoin() {
546 return [
547 [
548 [
549 'delTable' => 'table',
550 'joinTable' => 'table_join',
551 'delVar' => 'field',
552 'joinVar' => 'field_join',
553 'conds' => [ 'alias' => 'text' ],
554 ],
555 "DELETE FROM table " .
556 "WHERE field IN (" .
557 "SELECT field_join FROM table_join WHERE alias = 'text'" .
558 ")"
559 ],
560 [
561 [
562 'delTable' => 'table',
563 'joinTable' => 'table_join',
564 'delVar' => 'field',
565 'joinVar' => 'field_join',
566 'conds' => '*',
567 ],
568 "DELETE FROM table " .
569 "WHERE field IN (" .
570 "SELECT field_join FROM table_join " .
571 ")"
572 ],
573 ];
574 }
575
581 public function testInsert( $sql, $sqlText ) {
582 $this->database->insert(
583 $sql['table'],
584 $sql['rows'],
585 __METHOD__,
586 $sql['options'] ?? []
587 );
588 $this->assertLastSql( $sqlText );
589 }
590
591 public static function provideInsert() {
592 return [
593 [
594 [
595 'table' => 'table',
596 'rows' => [ 'field' => 'text', 'field2' => 2 ],
597 ],
598 "INSERT INTO table " .
599 "(field,field2) " .
600 "VALUES ('text','2')"
601 ],
602 [
603 [
604 'table' => 'table',
605 'rows' => [ 'field' => 'text', 'field2' => 2 ],
606 'options' => 'IGNORE',
607 ],
608 "INSERT IGNORE INTO table " .
609 "(field,field2) " .
610 "VALUES ('text','2')"
611 ],
612 [
613 [
614 'table' => 'table',
615 'rows' => [
616 [ 'field' => 'text', 'field2' => 2 ],
617 [ 'field' => 'multi', 'field2' => 3 ],
618 ],
619 'options' => 'IGNORE',
620 ],
621 "INSERT IGNORE INTO table " .
622 "(field,field2) " .
623 "VALUES " .
624 "('text','2')," .
625 "('multi','3')"
626 ],
627 ];
628 }
629
635 public function testInsertSelect( $sql, $sqlTextNative, $sqlSelect, $sqlInsert ) {
636 $this->database->insertSelect(
637 $sql['destTable'],
638 $sql['srcTable'],
639 $sql['varMap'],
640 $sql['conds'],
641 __METHOD__,
642 $sql['insertOptions'] ?? [],
643 $sql['selectOptions'] ?? [],
644 $sql['selectJoinConds'] ?? []
645 );
646 $this->assertLastSql( $sqlTextNative );
647
648 $dbWeb = new DatabaseTestHelper( __CLASS__, [ 'cliMode' => false ] );
649 $dbWeb->forceNextResult( [
650 array_flip( array_keys( $sql['varMap'] ) )
651 ] );
652 $dbWeb->insertSelect(
653 $sql['destTable'],
654 $sql['srcTable'],
655 $sql['varMap'],
656 $sql['conds'],
657 __METHOD__,
658 $sql['insertOptions'] ?? [],
659 $sql['selectOptions'] ?? [],
660 $sql['selectJoinConds'] ?? []
661 );
662 $this->assertLastSqlDb( implode( '; ', [ $sqlSelect, 'BEGIN', $sqlInsert, 'COMMIT' ] ), $dbWeb );
663 }
664
665 public static function provideInsertSelect() {
666 return [
667 [
668 [
669 'destTable' => 'insert_table',
670 'srcTable' => 'select_table',
671 'varMap' => [ 'field_insert' => 'field_select', 'field' => 'field2' ],
672 'conds' => '*',
673 ],
674 "INSERT INTO insert_table " .
675 "(field_insert,field) " .
676 "SELECT field_select,field2 " .
677 "FROM select_table",
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')"
681 ],
682 [
683 [
684 'destTable' => 'insert_table',
685 'srcTable' => 'select_table',
686 'varMap' => [ 'field_insert' => 'field_select', 'field' => 'field2' ],
687 'conds' => [ 'field' => 2 ],
688 ],
689 "INSERT INTO insert_table " .
690 "(field_insert,field) " .
691 "SELECT field_select,field2 " .
692 "FROM select_table " .
693 "WHERE field = '2'",
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')"
697 ],
698 [
699 [
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' ],
706 ],
707 "INSERT IGNORE INTO insert_table " .
708 "(field_insert,field) " .
709 "SELECT field_select,field2 " .
710 "FROM select_table " .
711 "WHERE field = '2' " .
712 "ORDER BY field",
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')"
716 ],
717 [
718 [
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' ] ],
727 ],
728 ],
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' " .
734 "ORDER BY field",
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')"
739 ],
740 ];
741 }
742
747 public function testInsertSelectBatching() {
748 $dbWeb = new DatabaseTestHelper( __CLASS__, [ 'cliMode' => false ] );
749 $rows = [];
750 for ( $i = 0; $i <= 25000; $i++ ) {
751 $rows[] = [ 'field' => $i ];
752 }
753 $dbWeb->forceNextResult( $rows );
754 $dbWeb->insertSelect(
755 'insert_table',
756 'select_table',
757 [ 'field' => 'field2' ],
758 '*',
759 __METHOD__
760 );
761 $this->assertLastSqlDb( implode( '; ', [
762 'SELECT field2 AS field FROM select_table FOR UPDATE',
763 'BEGIN',
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 ) ) . "')",
767 'COMMIT'
768 ] ), $dbWeb );
769 }
770
775 public function testReplace( $sql, $sqlText ) {
776 $this->database->replace(
777 $sql['table'],
778 $sql['uniqueIndexes'],
779 $sql['rows'],
780 __METHOD__
781 );
782 $this->assertLastSql( $sqlText );
783 }
784
785 public static function provideReplace() {
786 return [
787 [
788 [
789 'table' => 'replace_table',
790 'uniqueIndexes' => [ 'field' ],
791 'rows' => [ 'field' => 'text', 'field2' => 'text2' ],
792 ],
793 "BEGIN; DELETE FROM replace_table " .
794 "WHERE (field = 'text'); " .
795 "INSERT INTO replace_table " .
796 "(field,field2) " .
797 "VALUES ('text','text2'); COMMIT"
798 ],
799 [
800 [
801 'table' => 'module_deps',
802 'uniqueIndexes' => [ [ 'md_module', 'md_skin' ] ],
803 'rows' => [
804 'md_module' => 'module',
805 'md_skin' => 'skin',
806 'md_deps' => 'deps',
807 ],
808 ],
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"
814 ],
815 [
816 [
817 'table' => 'module_deps',
818 'uniqueIndexes' => [ [ 'md_module', 'md_skin' ] ],
819 'rows' => [
820 [
821 'md_module' => 'module',
822 'md_skin' => 'skin',
823 'md_deps' => 'deps',
824 ], [
825 'md_module' => 'module2',
826 'md_skin' => 'skin2',
827 'md_deps' => 'deps2',
828 ],
829 ],
830 ],
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"
841 ],
842 [
843 [
844 'table' => 'module_deps',
845 'uniqueIndexes' => [ 'md_module', 'md_skin' ],
846 'rows' => [
847 [
848 'md_module' => 'module',
849 'md_skin' => 'skin',
850 'md_deps' => 'deps',
851 ], [
852 'md_module' => 'module2',
853 'md_skin' => 'skin2',
854 'md_deps' => 'deps2',
855 ],
856 ],
857 ],
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"
868 ],
869 [
870 [
871 'table' => 'module_deps',
872 'uniqueIndexes' => [],
873 'rows' => [
874 'md_module' => 'module',
875 'md_skin' => 'skin',
876 'md_deps' => 'deps',
877 ],
878 ],
879 "BEGIN; INSERT INTO module_deps " .
880 "(md_module,md_skin,md_deps) " .
881 "VALUES ('module','skin','deps'); COMMIT"
882 ],
883 ];
884 }
885
890 public function testNativeReplace( $sql, $sqlText ) {
891 $this->database->nativeReplace(
892 $sql['table'],
893 $sql['rows'],
894 __METHOD__
895 );
896 $this->assertLastSql( $sqlText );
897 }
898
899 public static function provideNativeReplace() {
900 return [
901 [
902 [
903 'table' => 'replace_table',
904 'rows' => [ 'field' => 'text', 'field2' => 'text2' ],
905 ],
906 "REPLACE INTO replace_table " .
907 "(field,field2) " .
908 "VALUES ('text','text2')"
909 ],
910 ];
911 }
912
917 public function testConditional( $sql, $sqlText ) {
918 $this->assertEquals( trim( $this->database->conditional(
919 $sql['conds'],
920 $sql['true'],
921 $sql['false']
922 ) ), $sqlText );
923 }
924
925 public static function provideConditional() {
926 return [
927 [
928 [
929 'conds' => [ 'field' => 'text' ],
930 'true' => 1,
931 'false' => 'NULL',
932 ],
933 "(CASE WHEN field = 'text' THEN 1 ELSE NULL END)"
934 ],
935 [
936 [
937 'conds' => [ 'field' => 'text', 'field2' => 'anothertext' ],
938 'true' => 1,
939 'false' => 'NULL',
940 ],
941 "(CASE WHEN field = 'text' AND field2 = 'anothertext' THEN 1 ELSE NULL END)"
942 ],
943 [
944 [
945 'conds' => 'field=1',
946 'true' => 1,
947 'false' => 'NULL',
948 ],
949 "(CASE WHEN field=1 THEN 1 ELSE NULL END)"
950 ],
951 ];
952 }
953
958 public function testBuildConcat( $stringList, $sqlText ) {
959 $this->assertEquals( trim( $this->database->buildConcat(
960 $stringList
961 ) ), $sqlText );
962 }
963
964 public static function provideBuildConcat() {
965 return [
966 [
967 [ 'field', 'field2' ],
968 "CONCAT(field,field2)"
969 ],
970 [
971 [ "'test'", 'field2' ],
972 "CONCAT('test',field2)"
973 ],
974 ];
975 }
976
982 public function testBuildLike( $array, $sqlText ) {
983 $this->assertEquals( trim( $this->database->buildLike(
984 $array
985 ) ), $sqlText );
986 }
987
988 public static function provideBuildLike() {
989 return [
990 [
991 'text',
992 "LIKE 'text' ESCAPE '`'"
993 ],
994 [
995 [ 'text', new LikeMatch( '%' ) ],
996 "LIKE 'text%' ESCAPE '`'"
997 ],
998 [
999 [ 'text', new LikeMatch( '%' ), 'text2' ],
1000 "LIKE 'text%text2' ESCAPE '`'"
1001 ],
1002 [
1003 [ 'text', new LikeMatch( '_' ) ],
1004 "LIKE 'text_' ESCAPE '`'"
1005 ],
1006 [
1007 'more_text',
1008 "LIKE 'more`_text' ESCAPE '`'"
1009 ],
1010 [
1011 [ 'C:\\Windows\\', new LikeMatch( '%' ) ],
1012 "LIKE 'C:\\Windows\\%' ESCAPE '`'"
1013 ],
1014 [
1015 [ 'accent`_test`', new LikeMatch( '%' ) ],
1016 "LIKE 'accent```_test``%' ESCAPE '`'"
1017 ],
1018 ];
1019 }
1020
1025 public function testUnionQueries( $sql, $sqlText ) {
1026 $this->assertEquals( trim( $this->database->unionQueries(
1027 $sql['sqls'],
1028 $sql['all']
1029 ) ), $sqlText );
1030 }
1031
1032 public static function provideUnionQueries() {
1033 return [
1034 [
1035 [
1036 'sqls' => [ 'RAW SQL', 'RAW2SQL' ],
1037 'all' => true,
1038 ],
1039 "(RAW SQL) UNION ALL (RAW2SQL)"
1040 ],
1041 [
1042 [
1043 'sqls' => [ 'RAW SQL', 'RAW2SQL' ],
1044 'all' => false,
1045 ],
1046 "(RAW SQL) UNION (RAW2SQL)"
1047 ],
1048 [
1049 [
1050 'sqls' => [ 'RAW SQL', 'RAW2SQL', 'RAW3SQL' ],
1051 'all' => false,
1052 ],
1053 "(RAW SQL) UNION (RAW2SQL) UNION (RAW3SQL)"
1054 ],
1055 ];
1056 }
1057
1062 public function testUnionConditionPermutations( $params, $expect ) {
1063 if ( isset( $params['unionSupportsOrderAndLimit'] ) ) {
1064 $this->database->setUnionSupportsOrderAndLimit( $params['unionSupportsOrderAndLimit'] );
1065 }
1066
1067 $sql = trim( $this->database->unionConditionPermutations(
1068 $params['table'],
1069 $params['vars'],
1070 $params['permute_conds'],
1071 $params['extra_conds'] ?? '',
1072 'FNAME',
1073 $params['options'] ?? [],
1074 $params['join_conds'] ?? []
1075 ) );
1076 $this->assertEquals( $expect, $sql );
1077 }
1078
1079 public static function provideUnionConditionPermutations() {
1080 // phpcs:disable Generic.Files.LineLength
1081 return [
1082 [
1083 [
1084 'table' => [ 'table1', 'table2' ],
1085 'vars' => [ 'field1', 'alias' => 'field2' ],
1086 'permute_conds' => [
1087 'field3' => [ 1, 2, 3 ],
1088 'duplicates' => [ 4, 5, 4 ],
1089 'empty' => [],
1090 'single' => [ 0 ],
1091 ],
1092 'extra_conds' => 'table2.bar > 23',
1093 'options' => [
1094 'ORDER BY' => [ 'field1', 'alias' ],
1095 'INNER ORDER BY' => [ 'field1', 'field2' ],
1096 'LIMIT' => 100,
1097 ],
1098 'join_conds' => [
1099 'table2' => [ 'JOIN', 'table1.foo_id = table2.foo_id' ],
1100 ],
1101 ],
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"
1109 ],
1110 [
1111 [
1112 'table' => 'foo',
1113 'vars' => [ 'foo_id' ],
1114 'permute_conds' => [
1115 'bar' => [ 1, 2, 3 ],
1116 ],
1117 'extra_conds' => [ 'baz' => null ],
1118 'options' => [
1119 'NOTALL',
1120 'ORDER BY' => [ 'foo_id' ],
1121 'LIMIT' => 25,
1122 ],
1123 ],
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"
1128 ],
1129 [
1130 [
1131 'table' => 'foo',
1132 'vars' => [ 'foo_id' ],
1133 'permute_conds' => [
1134 'bar' => [ 1, 2, 3 ],
1135 ],
1136 'extra_conds' => [ 'baz' => null ],
1137 'options' => [
1138 'NOTALL' => true,
1139 'ORDER BY' => [ 'foo_id' ],
1140 'LIMIT' => 25,
1141 ],
1142 'unionSupportsOrderAndLimit' => false,
1143 ],
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"
1148 ],
1149 [
1150 [
1151 'table' => 'foo',
1152 'vars' => [ 'foo_id' ],
1153 'permute_conds' => [],
1154 'extra_conds' => [ 'baz' => null ],
1155 'options' => [
1156 'ORDER BY' => [ 'foo_id' ],
1157 'LIMIT' => 25,
1158 ],
1159 ],
1160 "SELECT foo_id FROM foo WHERE baz IS NULL ORDER BY foo_id LIMIT 25"
1161 ],
1162 [
1163 [
1164 'table' => 'foo',
1165 'vars' => [ 'foo_id' ],
1166 'permute_conds' => [
1167 'bar' => [],
1168 ],
1169 'extra_conds' => [ 'baz' => null ],
1170 'options' => [
1171 'ORDER BY' => [ 'foo_id' ],
1172 'LIMIT' => 25,
1173 ],
1174 ],
1175 "SELECT foo_id FROM foo WHERE baz IS NULL ORDER BY foo_id LIMIT 25"
1176 ],
1177 [
1178 [
1179 'table' => 'foo',
1180 'vars' => [ 'foo_id' ],
1181 'permute_conds' => [
1182 'bar' => [ 1 ],
1183 ],
1184 'options' => [
1185 'ORDER BY' => [ 'foo_id' ],
1186 'LIMIT' => 25,
1187 'OFFSET' => 150,
1188 ],
1189 ],
1190 "SELECT foo_id FROM foo WHERE bar = '1' ORDER BY foo_id LIMIT 150,25"
1191 ],
1192 [
1193 [
1194 'table' => 'foo',
1195 'vars' => [ 'foo_id' ],
1196 'permute_conds' => [],
1197 'extra_conds' => [ 'baz' => null ],
1198 'options' => [
1199 'ORDER BY' => [ 'foo_id' ],
1200 'LIMIT' => 25,
1201 'OFFSET' => 150,
1202 'INNER ORDER BY' => [ 'bar_id' ],
1203 ],
1204 ],
1205 "(SELECT foo_id FROM foo WHERE baz IS NULL ORDER BY bar_id LIMIT 175 ) ORDER BY foo_id LIMIT 150,25"
1206 ],
1207 [
1208 [
1209 'table' => 'foo',
1210 'vars' => [ 'foo_id' ],
1211 'permute_conds' => [],
1212 'extra_conds' => [ 'baz' => null ],
1213 'options' => [
1214 'ORDER BY' => [ 'foo_id' ],
1215 'LIMIT' => 25,
1216 'OFFSET' => 150,
1217 'INNER ORDER BY' => [ 'bar_id' ],
1218 ],
1219 'unionSupportsOrderAndLimit' => false,
1220 ],
1221 "SELECT foo_id FROM foo WHERE baz IS NULL ORDER BY foo_id LIMIT 150,25"
1222 ],
1223 ];
1224 // phpcs:enable
1225 }
1226
1231 public function testTransactionCommit() {
1232 $this->database->begin( __METHOD__ );
1233 $this->database->commit( __METHOD__ );
1234 $this->assertLastSql( 'BEGIN; COMMIT' );
1235 }
1236
1241 public function testTransactionRollback() {
1242 $this->database->begin( __METHOD__ );
1243 $this->database->rollback( __METHOD__ );
1244 $this->assertLastSql( 'BEGIN; ROLLBACK' );
1245 }
1246
1250 public function testDropTable() {
1251 $this->database->setExistingTables( [ 'table' ] );
1252 $this->database->dropTable( 'table', __METHOD__ );
1253 $this->assertLastSql( 'DROP TABLE table CASCADE' );
1254 }
1255
1259 public function testDropNonExistingTable() {
1260 $this->assertFalse(
1261 $this->database->dropTable( 'non_existing', __METHOD__ )
1262 );
1263 }
1264
1269 public function testMakeList( $list, $mode, $sqlText ) {
1270 $this->assertEquals( trim( $this->database->makeList(
1271 $list, $mode
1272 ) ), $sqlText );
1273 }
1274
1275 public static function provideMakeList() {
1276 return [
1277 [
1278 [ 'value', 'value2' ],
1279 LIST_COMMA,
1280 "'value','value2'"
1281 ],
1282 [
1283 [ 'field', 'field2' ],
1284 LIST_NAMES,
1285 "field,field2"
1286 ],
1287 [
1288 [ 'field' => 'value', 'field2' => 'value2' ],
1289 LIST_AND,
1290 "field = 'value' AND field2 = 'value2'"
1291 ],
1292 [
1293 [ 'field' => null, "field2 != 'value2'" ],
1294 LIST_AND,
1295 "field IS NULL AND (field2 != 'value2')"
1296 ],
1297 [
1298 [ 'field' => [ 'value', null, 'value2' ], 'field2' => 'value2' ],
1299 LIST_AND,
1300 "(field IN ('value','value2') OR field IS NULL) AND field2 = 'value2'"
1301 ],
1302 [
1303 [ 'field' => [ null ], 'field2' => null ],
1304 LIST_AND,
1305 "field IS NULL AND field2 IS NULL"
1306 ],
1307 [
1308 [ 'field' => 'value', 'field2' => 'value2' ],
1309 LIST_OR,
1310 "field = 'value' OR field2 = 'value2'"
1311 ],
1312 [
1313 [ 'field' => 'value', 'field2' => null ],
1314 LIST_OR,
1315 "field = 'value' OR field2 IS NULL"
1316 ],
1317 [
1318 [ 'field' => [ 'value', 'value2' ], 'field2' => [ 'value' ] ],
1319 LIST_OR,
1320 "field IN ('value','value2') OR field2 = 'value'"
1321 ],
1322 [
1323 [ 'field' => [ null, 'value', null, 'value2' ], "field2 != 'value2'" ],
1324 LIST_OR,
1325 "(field IN ('value','value2') OR field IS NULL) OR (field2 != 'value2')"
1326 ],
1327 [
1328 [ 'field' => 'value', 'field2' => 'value2' ],
1329 LIST_SET,
1330 "field = 'value',field2 = 'value2'"
1331 ],
1332 [
1333 [ 'field' => 'value', 'field2' => null ],
1334 LIST_SET,
1335 "field = 'value',field2 = NULL"
1336 ],
1337 [
1338 [ 'field' => 'value', "field2 != 'value2'" ],
1339 LIST_SET,
1340 "field = 'value',field2 != 'value2'"
1341 ],
1342 ];
1343 }
1344
1348 public function testSessionTempTables() {
1349 $temp1 = $this->database->tableName( 'tmp_table_1' );
1350 $temp2 = $this->database->tableName( 'tmp_table_2' );
1351 $temp3 = $this->database->tableName( 'tmp_table_3' );
1352
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__ );
1356
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__ ) );
1360
1361 $this->database->dropTable( 'tmp_table_1', __METHOD__ );
1362 $this->database->dropTable( 'tmp_table_2', __METHOD__ );
1363 $this->database->dropTable( 'tmp_table_3', __METHOD__ );
1364
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__ ) );
1368
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__ );
1372
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__ ) );
1376
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__ );
1380
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__ ) );
1384 }
1385
1386 public function provideBuildSubstring() {
1387 yield [ 'someField', 1, 2, 'SUBSTRING(someField FROM 1 FOR 2)' ];
1388 yield [ 'someField', 1, null, 'SUBSTRING(someField FROM 1)' ];
1389 }
1390
1395 public function testBuildSubstring( $input, $start, $length, $expected ) {
1396 $output = $this->database->buildSubstring( $input, $start, $length );
1397 $this->assertSame( $expected, $output );
1398 }
1399
1401 yield [ -1, 1 ];
1402 yield [ 1, -1 ];
1403 yield [ 1, 'foo' ];
1404 yield [ 'foo', 1 ];
1405 yield [ null, 1 ];
1406 yield [ 0, 1 ];
1407 }
1408
1414 public function testBuildSubstring_invalidParams( $start, $length ) {
1415 $this->setExpectedException( InvalidArgumentException::class );
1416 $this->database->buildSubstring( 'foo', $start, $length );
1417 }
1418
1422 public function testBuildIntegerCast() {
1423 $output = $this->database->buildIntegerCast( 'fieldName' );
1424 $this->assertSame( 'CAST( fieldName AS INTEGER )', $output );
1425 }
1426
1436 public function testAtomicSections() {
1437 $this->database->startAtomic( __METHOD__ );
1438 $this->database->endAtomic( __METHOD__ );
1439 $this->assertLastSql( 'BEGIN; COMMIT' );
1440
1441 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1442 $this->database->cancelAtomic( __METHOD__ );
1443 $this->assertLastSql( 'BEGIN; ROLLBACK' );
1444
1445 $this->database->begin( __METHOD__ );
1446 $this->database->startAtomic( __METHOD__ );
1447 $this->database->endAtomic( __METHOD__ );
1448 $this->database->commit( __METHOD__ );
1449 $this->assertLastSql( 'BEGIN; COMMIT' );
1450
1451 $this->database->begin( __METHOD__ );
1452 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1453 $this->database->endAtomic( __METHOD__ );
1454 $this->database->commit( __METHOD__ );
1455 // phpcs:ignore Generic.Files.LineLength
1456 $this->assertLastSql( 'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; RELEASE SAVEPOINT wikimedia_rdbms_atomic1; COMMIT' );
1457
1458 $this->database->begin( __METHOD__ );
1459 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1460 $this->database->cancelAtomic( __METHOD__ );
1461 $this->database->commit( __METHOD__ );
1462 // phpcs:ignore Generic.Files.LineLength
1463 $this->assertLastSql( 'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; COMMIT' );
1464
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__ );
1469 // phpcs:ignore Generic.Files.LineLength
1470 $this->assertLastSql( 'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; COMMIT' );
1471
1472 $noOpCallack = function () {
1473 };
1474
1475 $this->database->doAtomicSection( __METHOD__, $noOpCallack, IDatabase::ATOMIC_CANCELABLE );
1476 $this->assertLastSql( 'BEGIN; COMMIT' );
1477
1478 $this->database->doAtomicSection( __METHOD__, $noOpCallack );
1479 $this->assertLastSql( 'BEGIN; COMMIT' );
1480
1481 $this->database->begin( __METHOD__ );
1482 $this->database->doAtomicSection( __METHOD__, $noOpCallack, IDatabase::ATOMIC_CANCELABLE );
1483 $this->database->rollback( __METHOD__ );
1484 // phpcs:ignore Generic.Files.LineLength
1485 $this->assertLastSql( 'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; RELEASE SAVEPOINT wikimedia_rdbms_atomic1; ROLLBACK' );
1486
1487 $fname = __METHOD__;
1488 $triggerMap = [
1489 '-' => '-',
1490 IDatabase::TRIGGER_COMMIT => 'tCommit',
1491 IDatabase::TRIGGER_ROLLBACK => 'tRollback',
1492 IDatabase::TRIGGER_CANCEL => 'tCancel',
1493 ];
1494 $pcCallback = function ( IDatabase $db ) use ( $fname ) {
1495 $this->database->query( "SELECT 0", $fname );
1496 };
1497 $callback1 = function ( $trigger = '-' ) use ( $fname, $triggerMap ) {
1498 $this->database->query( "SELECT 1, {$triggerMap[$trigger]} AS t", $fname );
1499 };
1500 $callback2 = function ( $trigger = '-' ) use ( $fname, $triggerMap ) {
1501 $this->database->query( "SELECT 2, {$triggerMap[$trigger]} AS t", $fname );
1502 };
1503 $callback3 = function ( $trigger = '-' ) use ( $fname, $triggerMap ) {
1504 $this->database->query( "SELECT 3, {$triggerMap[$trigger]} AS t", $fname );
1505 };
1506
1507 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1508 $this->database->onTransactionPreCommitOrIdle( $pcCallback, __METHOD__ );
1509 $this->database->cancelAtomic( __METHOD__ );
1510 $this->assertLastSql( 'BEGIN; ROLLBACK' );
1511
1512 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1513 $this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ );
1514 $this->database->cancelAtomic( __METHOD__ );
1515 $this->assertLastSql( 'BEGIN; ROLLBACK' );
1516
1517 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1518 $this->database->onTransactionResolution( $callback1, __METHOD__ );
1519 $this->database->cancelAtomic( __METHOD__ );
1520 $this->assertLastSql( 'BEGIN; ROLLBACK; SELECT 1, tRollback AS t' );
1521
1522 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1523 $this->database->onAtomicSectionCancel( $callback1, __METHOD__ );
1524 $this->database->cancelAtomic( __METHOD__ );
1525 $this->assertLastSql( 'BEGIN; ROLLBACK; SELECT 1, tRollback AS t' );
1526
1527 $this->database->startAtomic( __METHOD__ . '_outer' );
1528 $this->database->onTransactionPreCommitOrIdle( $pcCallback, __METHOD__ );
1529 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1530 $this->database->onTransactionPreCommitOrIdle( $pcCallback, __METHOD__ );
1531 $this->database->cancelAtomic( __METHOD__ );
1532 $this->database->onTransactionPreCommitOrIdle( $pcCallback, __METHOD__ );
1533 $this->database->endAtomic( __METHOD__ . '_outer' );
1534 $this->assertLastSql( implode( "; ", [
1535 'BEGIN',
1536 'SAVEPOINT wikimedia_rdbms_atomic1',
1537 'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1',
1538 'SELECT 0',
1539 'SELECT 0',
1540 'COMMIT'
1541 ] ) );
1542
1543 $this->database->startAtomic( __METHOD__ . '_outer' );
1544 $this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ );
1545 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1546 $this->database->onTransactionCommitOrIdle( $callback2, __METHOD__ );
1547 $this->database->cancelAtomic( __METHOD__ );
1548 $this->database->onTransactionCommitOrIdle( $callback3, __METHOD__ );
1549 $this->database->endAtomic( __METHOD__ . '_outer' );
1550 $this->assertLastSql( implode( "; ", [
1551 'BEGIN',
1552 'SAVEPOINT wikimedia_rdbms_atomic1',
1553 'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1',
1554 'COMMIT',
1555 'SELECT 1, tCommit AS t',
1556 'SELECT 3, tCommit AS t'
1557 ] ) );
1558
1559 $this->database->startAtomic( __METHOD__ . '_outer' );
1560 $this->database->onTransactionResolution( $callback1, __METHOD__ );
1561 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1562 $this->database->onTransactionResolution( $callback2, __METHOD__ );
1563 $this->database->cancelAtomic( __METHOD__ );
1564 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1565 $this->database->endAtomic( __METHOD__ . '_outer' );
1566 $this->assertLastSql( implode( "; ", [
1567 'BEGIN',
1568 'SAVEPOINT wikimedia_rdbms_atomic1',
1569 'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1',
1570 'COMMIT',
1571 'SELECT 1, tCommit AS t',
1572 'SELECT 2, tRollback AS t',
1573 'SELECT 3, tCommit AS t'
1574 ] ) );
1575
1576 $this->database->startAtomic( __METHOD__ . '_outer' );
1577 $this->database->onAtomicSectionCancel( $callback1, __METHOD__ );
1578 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1579 $this->database->onAtomicSectionCancel( $callback2, __METHOD__ );
1580 $this->database->cancelAtomic( __METHOD__ );
1581 $this->database->onAtomicSectionCancel( $callback3, __METHOD__ );
1582 $this->database->endAtomic( __METHOD__ . '_outer' );
1583 $this->assertLastSql( implode( "; ", [
1584 'BEGIN',
1585 'SAVEPOINT wikimedia_rdbms_atomic1',
1586 'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1',
1587 'SELECT 2, tCancel AS t',
1588 'COMMIT',
1589 ] ) );
1590
1591 $makeCallback = function ( $id ) use ( $fname, $triggerMap ) {
1592 return function ( $trigger = '-' ) use ( $id, $fname, $triggerMap ) {
1593 $this->database->query( "SELECT $id, {$triggerMap[$trigger]} AS t", $fname );
1594 };
1595 };
1596
1597 $this->database->startAtomic( __METHOD__ . '_outer' );
1598 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1599 $this->database->onTransactionResolution( $makeCallback( 1 ), __METHOD__ );
1600 $this->database->cancelAtomic( __METHOD__ );
1601 $this->database->endAtomic( __METHOD__ . '_outer' );
1602 $this->assertLastSql( implode( "; ", [
1603 'BEGIN',
1604 'SAVEPOINT wikimedia_rdbms_atomic1',
1605 'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1',
1606 'COMMIT',
1607 'SELECT 1, tRollback AS t'
1608 ] ) );
1609
1610 $this->database->startAtomic( __METHOD__ . '_level1', IDatabase::ATOMIC_CANCELABLE );
1611 $this->database->onTransactionResolution( $makeCallback( 1 ), __METHOD__ );
1612 $this->database->startAtomic( __METHOD__ . '_level2' );
1613 $this->database->startAtomic( __METHOD__ . '_level3', IDatabase::ATOMIC_CANCELABLE );
1614 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1615 $this->database->onTransactionResolution( $makeCallback( 2 ), __METHOD__ );
1616 $this->database->endAtomic( __METHOD__ );
1617 $this->database->onTransactionResolution( $makeCallback( 3 ), __METHOD__ );
1618 $this->database->cancelAtomic( __METHOD__ . '_level3' );
1619 $this->database->endAtomic( __METHOD__ . '_level2' );
1620 $this->database->onTransactionResolution( $makeCallback( 4 ), __METHOD__ );
1621 $this->database->endAtomic( __METHOD__ . '_level1' );
1622 $this->assertLastSql( implode( "; ", [
1623 'BEGIN',
1624 'SAVEPOINT wikimedia_rdbms_atomic1',
1625 'SAVEPOINT wikimedia_rdbms_atomic2',
1626 'RELEASE SAVEPOINT wikimedia_rdbms_atomic2',
1627 'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1',
1628 'COMMIT; SELECT 1, tCommit AS t',
1629 'SELECT 2, tRollback AS t',
1630 'SELECT 3, tRollback AS t',
1631 'SELECT 4, tCommit AS t'
1632 ] ) );
1633
1634 $this->database->startAtomic( __METHOD__ . '_level1', IDatabase::ATOMIC_CANCELABLE );
1635 $this->database->onAtomicSectionCancel( $makeCallback( 1 ), __METHOD__ );
1636 $this->database->startAtomic( __METHOD__ . '_level2' );
1637 $this->database->startAtomic( __METHOD__ . '_level3', IDatabase::ATOMIC_CANCELABLE );
1638 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1639 $this->database->onAtomicSectionCancel( $makeCallback( 2 ), __METHOD__ );
1640 $this->database->endAtomic( __METHOD__ );
1641 $this->database->onAtomicSectionCancel( $makeCallback( 3 ), __METHOD__ );
1642 $this->database->cancelAtomic( __METHOD__ . '_level3' );
1643 $this->database->endAtomic( __METHOD__ . '_level2' );
1644 $this->database->onAtomicSectionCancel( $makeCallback( 4 ), __METHOD__ );
1645 $this->database->endAtomic( __METHOD__ . '_level1' );
1646 $this->assertLastSql( implode( "; ", [
1647 'BEGIN',
1648 'SAVEPOINT wikimedia_rdbms_atomic1',
1649 'SAVEPOINT wikimedia_rdbms_atomic2',
1650 'RELEASE SAVEPOINT wikimedia_rdbms_atomic2',
1651 'ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1',
1652 'SELECT 2, tCancel AS t',
1653 'SELECT 3, tCancel AS t',
1654 'COMMIT',
1655 ] ) );
1656 }
1657
1667 public function testAtomicSectionsRecovery() {
1668 $this->database->begin( __METHOD__ );
1669 try {
1670 $this->database->doAtomicSection(
1671 __METHOD__,
1672 function () {
1673 $this->database->startAtomic( 'inner_func1' );
1674 $this->database->startAtomic( 'inner_func2' );
1675
1676 throw new RuntimeException( 'Test exception' );
1677 },
1678 IDatabase::ATOMIC_CANCELABLE
1679 );
1680 $this->fail( 'Expected exception not thrown' );
1681 } catch ( RuntimeException $ex ) {
1682 $this->assertSame( 'Test exception', $ex->getMessage() );
1683 }
1684 $this->database->commit( __METHOD__ );
1685 // phpcs:ignore Generic.Files.LineLength
1686 $this->assertLastSql( 'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; COMMIT' );
1687
1688 $this->database->begin( __METHOD__ );
1689 try {
1690 $this->database->doAtomicSection(
1691 __METHOD__,
1692 function () {
1693 throw new RuntimeException( 'Test exception' );
1694 }
1695 );
1696 $this->fail( 'Test exception not thrown' );
1697 } catch ( RuntimeException $ex ) {
1698 $this->assertSame( 'Test exception', $ex->getMessage() );
1699 }
1700 try {
1701 $this->database->commit( __METHOD__ );
1702 $this->fail( 'Test exception not thrown' );
1703 } catch ( DBTransactionError $ex ) {
1704 $this->assertSame(
1705 'Cannot execute query from ' . __METHOD__ . ' while transaction status is ERROR.',
1706 $ex->getMessage()
1707 );
1708 }
1709 $this->database->rollback( __METHOD__ );
1710 $this->assertLastSql( 'BEGIN; ROLLBACK' );
1711 }
1712
1723 $fname = __METHOD__;
1724 $callback1Called = null;
1725 $callback1 = function ( $trigger = '-' ) use ( $fname, &$callback1Called ) {
1726 $callback1Called = $trigger;
1727 $this->database->query( "SELECT 1", $fname );
1728 };
1729 $callback2Called = null;
1730 $callback2 = function ( $trigger = '-' ) use ( $fname, &$callback2Called ) {
1731 $callback2Called = $trigger;
1732 $this->database->query( "SELECT 2", $fname );
1733 };
1734 $callback3Called = null;
1735 $callback3 = function ( $trigger = '-' ) use ( $fname, &$callback3Called ) {
1736 $callback3Called = $trigger;
1737 $this->database->query( "SELECT 3", $fname );
1738 };
1739 $callback4Called = 0;
1740 $callback4 = function () use ( $fname, &$callback4Called ) {
1741 $callback4Called++;
1742 $this->database->query( "SELECT 4", $fname );
1743 };
1744 $callback5Called = 0;
1745 $callback5 = function () use ( $fname, &$callback5Called ) {
1746 $callback5Called++;
1747 $this->database->query( "SELECT 5", $fname );
1748 };
1749
1750 $this->database->startAtomic( __METHOD__ . '_outer' );
1751 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1752 $this->database->startAtomic( __METHOD__ . '_inner' );
1753 $this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ );
1754 $this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ );
1755 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1756 $this->database->onAtomicSectionCancel( $callback4, __METHOD__ );
1757 $this->database->endAtomic( __METHOD__ . '_inner' );
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 );
1763 $this->assertEquals( 1, $callback4Called );
1764 // phpcs:ignore Generic.Files.LineLength
1765 $this->assertLastSql( 'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; SELECT 4; COMMIT; SELECT 3' );
1766
1767 $callback1Called = null;
1768 $callback2Called = null;
1769 $callback3Called = null;
1770 $callback4Called = 0;
1771 $this->database->startAtomic( __METHOD__ . '_outer' );
1772 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1773 $this->database->startAtomic( __METHOD__ . '_inner', IDatabase::ATOMIC_CANCELABLE );
1774 $this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ );
1775 $this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ );
1776 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1777 $this->database->onAtomicSectionCancel( $callback4, __METHOD__ );
1778 $this->database->endAtomic( __METHOD__ . '_inner' );
1779 $this->database->cancelAtomic( __METHOD__ );
1780 $this->database->endAtomic( __METHOD__ . '_outer' );
1781 $this->assertNull( $callback1Called );
1782 $this->assertNull( $callback2Called );
1783 $this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called );
1784 $this->assertEquals( 1, $callback4Called );
1785 // phpcs:ignore Generic.Files.LineLength
1786 $this->assertLastSql( 'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; SAVEPOINT wikimedia_rdbms_atomic2; RELEASE SAVEPOINT wikimedia_rdbms_atomic2; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; SELECT 4; COMMIT; SELECT 3' );
1787
1788 $callback1Called = null;
1789 $callback2Called = null;
1790 $callback3Called = null;
1791 $callback4Called = 0;
1792 $this->database->startAtomic( __METHOD__ . '_outer' );
1793 $atomicId = $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1794 $this->database->startAtomic( __METHOD__ . '_inner' );
1795 $this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ );
1796 $this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ );
1797 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1798 $this->database->onAtomicSectionCancel( $callback4, __METHOD__ );
1799 $this->database->cancelAtomic( __METHOD__, $atomicId );
1800 $this->database->endAtomic( __METHOD__ . '_outer' );
1801 $this->assertNull( $callback1Called );
1802 $this->assertNull( $callback2Called );
1803 $this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called );
1804 $this->assertEquals( 1, $callback4Called );
1805
1806 $callback1Called = null;
1807 $callback2Called = null;
1808 $callback3Called = null;
1809 $callback4Called = 0;
1810 $this->database->startAtomic( __METHOD__ . '_outer' );
1811 $atomicId = $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1812 $this->database->startAtomic( __METHOD__ . '_inner' );
1813 $this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ );
1814 $this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ );
1815 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1816 $this->database->onAtomicSectionCancel( $callback4, __METHOD__ );
1817 try {
1818 $this->database->cancelAtomic( __METHOD__ . '_X', $atomicId );
1819 } catch ( DBUnexpectedError $e ) {
1820 $m = __METHOD__;
1821 $this->assertSame(
1822 "Invalid atomic section ended (got {$m}_X but expected {$m}).",
1823 $e->getMessage()
1824 );
1825 }
1826 $this->database->cancelAtomic( __METHOD__ );
1827 $this->database->endAtomic( __METHOD__ . '_outer' );
1828 $this->assertNull( $callback1Called );
1829 $this->assertNull( $callback2Called );
1830 $this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called );
1831 $this->assertEquals( 1, $callback4Called );
1832
1833 $callback4Called = 0;
1834 $callback5Called = 0;
1835 $this->database->getLastSqls(); // flush
1836 $this->database->startAtomic( __METHOD__ . '_outer' );
1837 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1838 $this->database->onAtomicSectionCancel( $callback5, __METHOD__ );
1839 $this->database->startAtomic( __METHOD__ . '_inner', IDatabase::ATOMIC_CANCELABLE );
1840 $this->database->onAtomicSectionCancel( $callback4, __METHOD__ );
1841 $this->database->cancelAtomic( __METHOD__ . '_inner' );
1842 $this->database->cancelAtomic( __METHOD__ );
1843 $this->database->endAtomic( __METHOD__ . '_outer' );
1844 // phpcs:ignore Generic.Files.LineLength
1845 $this->assertLastSql( 'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; SAVEPOINT wikimedia_rdbms_atomic2; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic2; SELECT 4; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; SELECT 5; COMMIT' );
1846 $this->assertEquals( 1, $callback4Called );
1847 $this->assertEquals( 1, $callback5Called );
1848
1849 $callback4Called = 0;
1850 $callback5Called = 0;
1851 $this->database->startAtomic( __METHOD__ . '_outer' );
1852 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1853 $this->database->onAtomicSectionCancel( $callback5, __METHOD__ );
1854 $this->database->startAtomic( __METHOD__ . '_inner', IDatabase::ATOMIC_CANCELABLE );
1855 $this->database->onAtomicSectionCancel( $callback4, __METHOD__ );
1856 $this->database->endAtomic( __METHOD__ . '_inner' );
1857 $this->database->cancelAtomic( __METHOD__ );
1858 $this->database->endAtomic( __METHOD__ . '_outer' );
1859 // phpcs:ignore Generic.Files.LineLength
1860 $this->assertLastSql( 'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; SAVEPOINT wikimedia_rdbms_atomic2; RELEASE SAVEPOINT wikimedia_rdbms_atomic2; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; SELECT 5; SELECT 4; COMMIT' );
1861 $this->assertEquals( 1, $callback4Called );
1862 $this->assertEquals( 1, $callback5Called );
1863
1864 $callback4Called = 0;
1865 $callback5Called = 0;
1866 $this->database->startAtomic( __METHOD__ . '_outer' );
1867 $sectionId = $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1868 $this->database->onAtomicSectionCancel( $callback5, __METHOD__ );
1869 $this->database->startAtomic( __METHOD__ . '_inner', IDatabase::ATOMIC_CANCELABLE );
1870 $this->database->onAtomicSectionCancel( $callback4, __METHOD__ );
1871 $this->database->cancelAtomic( __METHOD__, $sectionId );
1872 $this->database->endAtomic( __METHOD__ . '_outer' );
1873 // phpcs:ignore Generic.Files.LineLength
1874 $this->assertLastSql( 'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; SAVEPOINT wikimedia_rdbms_atomic2; ROLLBACK TO SAVEPOINT wikimedia_rdbms_atomic1; SELECT 5; SELECT 4; COMMIT' );
1875 $this->assertEquals( 1, $callback4Called );
1876 $this->assertEquals( 1, $callback5Called );
1877
1878 $wrapper = TestingAccessWrapper::newFromObject( $this->database );
1879 $callback1Called = null;
1880 $callback2Called = null;
1881 $callback3Called = null;
1882 $callback4Called = 0;
1883 $this->database->startAtomic( __METHOD__ . '_outer' );
1884 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1885 $this->database->startAtomic( __METHOD__ . '_inner' );
1886 $this->database->onTransactionCommitOrIdle( $callback1, __METHOD__ );
1887 $this->database->onTransactionPreCommitOrIdle( $callback2, __METHOD__ );
1888 $this->database->onTransactionResolution( $callback3, __METHOD__ );
1889 $this->database->onAtomicSectionCancel( $callback4, __METHOD__ );
1890 $wrapper->trxStatus = Database::STATUS_TRX_ERROR;
1891 $this->database->cancelAtomic( __METHOD__ . '_inner' );
1892 $this->database->cancelAtomic( __METHOD__ );
1893 $this->database->endAtomic( __METHOD__ . '_outer' );
1894 $this->assertNull( $callback1Called );
1895 $this->assertNull( $callback2Called );
1896 $this->assertEquals( IDatabase::TRIGGER_ROLLBACK, $callback3Called );
1897 $this->assertEquals( 1, $callback4Called );
1898 }
1899
1909 public function testAtomicSectionsTrxRound() {
1910 $this->database->setFlag( IDatabase::DBO_TRX );
1911 $this->database->startAtomic( __METHOD__, IDatabase::ATOMIC_CANCELABLE );
1912 $this->database->query( 'SELECT 1', __METHOD__ );
1913 $this->database->endAtomic( __METHOD__ );
1914 $this->database->commit( __METHOD__, IDatabase::FLUSHING_ALL_PEERS );
1915 // phpcs:ignore Generic.Files.LineLength
1916 $this->assertLastSql( 'BEGIN; SAVEPOINT wikimedia_rdbms_atomic1; SELECT 1; RELEASE SAVEPOINT wikimedia_rdbms_atomic1; COMMIT' );
1917 }
1918
1919 public static function provideAtomicSectionMethodsForErrors() {
1920 return [
1921 [ 'endAtomic' ],
1922 [ 'cancelAtomic' ],
1923 ];
1924 }
1925
1931 public function testNoAtomicSection( $method ) {
1932 try {
1933 $this->database->$method( __METHOD__ );
1934 $this->fail( 'Expected exception not thrown' );
1935 } catch ( DBUnexpectedError $ex ) {
1936 $this->assertSame(
1937 'No atomic section is open (got ' . __METHOD__ . ').',
1938 $ex->getMessage()
1939 );
1940 }
1941 }
1942
1948 public function testInvalidAtomicSectionEnded( $method ) {
1949 $this->database->startAtomic( __METHOD__ . 'X' );
1950 try {
1951 $this->database->$method( __METHOD__ );
1952 $this->fail( 'Expected exception not thrown' );
1953 } catch ( DBUnexpectedError $ex ) {
1954 $this->assertSame(
1955 'Invalid atomic section ended (got ' . __METHOD__ . ' but expected ' .
1956 __METHOD__ . 'X).',
1957 $ex->getMessage()
1958 );
1959 }
1960 }
1961
1966 $this->database->startAtomic( __METHOD__ );
1967 try {
1968 $this->database->cancelAtomic( __METHOD__ );
1969 $this->database->select( 'test', '1', [], __METHOD__ );
1970 $this->fail( 'Expected exception not thrown' );
1971 } catch ( DBTransactionError $ex ) {
1972 $this->assertSame(
1973 'Cannot execute query from ' . __METHOD__ . ' while transaction status is ERROR.',
1974 $ex->getMessage()
1975 );
1976 }
1977 }
1978
1983 try {
1984 $this->database->onAtomicSectionCancel( function () {
1985 }, __METHOD__ );
1986 $this->fail( 'Expected exception not thrown' );
1987 } catch ( DBUnexpectedError $ex ) {
1988 $this->assertSame(
1989 'No atomic section is open (got ' . __METHOD__ . ').',
1990 $ex->getMessage()
1991 );
1992 }
1993 }
1994
1999 public function testTransactionErrorState1() {
2000 $wrapper = TestingAccessWrapper::newFromObject( $this->database );
2001
2002 $this->database->begin( __METHOD__ );
2003 $wrapper->trxStatus = Database::STATUS_TRX_ERROR;
2004 $this->database->delete( 'x', [ 'field' => 3 ], __METHOD__ );
2005 $this->database->commit( __METHOD__ );
2006 }
2007
2011 public function testTransactionErrorState2() {
2012 $wrapper = TestingAccessWrapper::newFromObject( $this->database );
2013
2014 $this->database->startAtomic( __METHOD__ );
2015 $wrapper->trxStatus = Database::STATUS_TRX_ERROR;
2016 $this->database->rollback( __METHOD__ );
2017 $this->assertEquals( 0, $this->database->trxLevel() );
2018 $this->assertEquals( Database::STATUS_TRX_NONE, $wrapper->trxStatus() );
2019 $this->assertLastSql( 'BEGIN; ROLLBACK' );
2020
2021 $this->database->startAtomic( __METHOD__ );
2022 $this->assertEquals( Database::STATUS_TRX_OK, $wrapper->trxStatus() );
2023 $this->database->delete( 'x', [ 'field' => 1 ], __METHOD__ );
2024 $this->database->endAtomic( __METHOD__ );
2025 $this->assertEquals( Database::STATUS_TRX_NONE, $wrapper->trxStatus() );
2026 $this->assertLastSql( 'BEGIN; DELETE FROM x WHERE field = \'1\'; COMMIT' );
2027 $this->assertEquals( 0, $this->database->trxLevel(), 'Use after rollback()' );
2028
2029 $this->database->begin( __METHOD__ );
2030 $this->database->startAtomic( __METHOD__, Database::ATOMIC_CANCELABLE );
2031 $this->database->update( 'y', [ 'a' => 1 ], [ 'field' => 1 ], __METHOD__ );
2032 $wrapper->trxStatus = Database::STATUS_TRX_ERROR;
2033 $this->database->cancelAtomic( __METHOD__ );
2034 $this->assertEquals( Database::STATUS_TRX_OK, $wrapper->trxStatus() );
2035 $this->database->startAtomic( __METHOD__ );
2036 $this->database->delete( 'y', [ 'field' => 1 ], __METHOD__ );
2037 $this->database->endAtomic( __METHOD__ );
2038 $this->database->commit( __METHOD__ );
2039 // phpcs:ignore Generic.Files.LineLength
2040 $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' );
2041 $this->assertEquals( 0, $this->database->trxLevel(), 'Use after rollback()' );
2042
2043 // Next transaction
2044 $this->database->startAtomic( __METHOD__ );
2045 $this->assertEquals( Database::STATUS_TRX_OK, $wrapper->trxStatus() );
2046 $this->database->delete( 'x', [ 'field' => 3 ], __METHOD__ );
2047 $this->database->endAtomic( __METHOD__ );
2048 $this->assertEquals( Database::STATUS_TRX_NONE, $wrapper->trxStatus() );
2049 $this->assertLastSql( 'BEGIN; DELETE FROM x WHERE field = \'3\'; COMMIT' );
2050 $this->assertEquals( 0, $this->database->trxLevel() );
2051 }
2052
2057 $doError = function () {
2058 $this->database->forceNextQueryError( 666, 'Evilness' );
2059 try {
2060 $this->database->delete( 'error', '1', __CLASS__ . '::SomeCaller' );
2061 $this->fail( 'Expected exception not thrown' );
2062 } catch ( DBError $e ) {
2063 $this->assertSame( 666, $e->errno );
2064 }
2065 };
2066
2067 $this->database->setFlag( Database::DBO_TRX );
2068
2069 // Implicit transaction does not get silently rolled back
2070 $this->database->begin( __METHOD__, Database::TRANSACTION_INTERNAL );
2071 call_user_func( $doError );
2072 try {
2073 $this->database->delete( 'x', [ 'field' => 1 ], __METHOD__ );
2074 $this->fail( 'Expected exception not thrown' );
2075 } catch ( DBTransactionError $e ) {
2076 $this->assertEquals(
2077 'Cannot execute query from ' . __METHOD__ . ' while transaction status is ERROR.',
2078 $e->getMessage()
2079 );
2080 }
2081 try {
2082 $this->database->commit( __METHOD__, Database::FLUSHING_INTERNAL );
2083 $this->fail( 'Expected exception not thrown' );
2084 } catch ( DBTransactionError $e ) {
2085 $this->assertEquals(
2086 'Cannot execute query from ' . __METHOD__ . ' while transaction status is ERROR.',
2087 $e->getMessage()
2088 );
2089 }
2090 $this->database->rollback( __METHOD__, Database::FLUSHING_INTERNAL );
2091 $this->assertLastSql( 'BEGIN; DELETE FROM error WHERE 1; ROLLBACK' );
2092
2093 // Likewise if there were prior writes
2094 $this->database->begin( __METHOD__, Database::TRANSACTION_INTERNAL );
2095 $this->database->delete( 'x', [ 'field' => 1 ], __METHOD__ );
2096 call_user_func( $doError );
2097 try {
2098 $this->database->delete( 'x', [ 'field' => 1 ], __METHOD__ );
2099 $this->fail( 'Expected exception not thrown' );
2100 } catch ( DBTransactionStateError $e ) {
2101 }
2102 $this->database->rollback( __METHOD__, Database::FLUSHING_INTERNAL );
2103 // phpcs:ignore
2104 $this->assertLastSql( 'BEGIN; DELETE FROM x WHERE field = \'1\'; DELETE FROM error WHERE 1; ROLLBACK' );
2105 }
2106
2111 $wrapper = TestingAccessWrapper::newFromObject( $this->database );
2112 $warning = [];
2113 $wrapper->deprecationLogger = function ( $msg ) use ( &$warning ) {
2114 $warning[] = $msg;
2115 };
2116
2117 $doError = function () {
2118 $this->database->forceNextQueryError( 666, 'Evilness', [
2119 'wasKnownStatementRollbackError' => true,
2120 ] );
2121 try {
2122 $this->database->delete( 'error', '1', __CLASS__ . '::SomeCaller' );
2123 $this->fail( 'Expected exception not thrown' );
2124 } catch ( DBError $e ) {
2125 $this->assertSame( 666, $e->errno );
2126 }
2127 };
2128 $expectWarning = 'Caller from ' . __METHOD__ .
2129 ' ignored an error originally raised from ' . __CLASS__ . '::SomeCaller: [666] Evilness';
2130
2131 // Rollback doesn't raise a warning
2132 $warning = [];
2133 $this->database->startAtomic( __METHOD__ );
2134 call_user_func( $doError );
2135 $this->database->rollback( __METHOD__ );
2136 $this->database->delete( 'x', [ 'field' => 1 ], __METHOD__ );
2137 $this->assertSame( [], $warning );
2138 // phpcs:ignore
2139 $this->assertLastSql( 'BEGIN; DELETE FROM error WHERE 1; ROLLBACK; DELETE FROM x WHERE field = \'1\'' );
2140
2141 // cancelAtomic() doesn't raise a warning
2142 $warning = [];
2143 $this->database->begin( __METHOD__ );
2144 $this->database->startAtomic( __METHOD__, Database::ATOMIC_CANCELABLE );
2145 call_user_func( $doError );
2146 $this->database->cancelAtomic( __METHOD__ );
2147 $this->database->delete( 'x', [ 'field' => 1 ], __METHOD__ );
2148 $this->database->commit( __METHOD__ );
2149 $this->assertSame( [], $warning );
2150 // phpcs:ignore
2151 $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' );
2152
2153 // Commit does raise a warning
2154 $warning = [];
2155 $this->database->begin( __METHOD__ );
2156 call_user_func( $doError );
2157 $this->database->commit( __METHOD__ );
2158 $this->assertSame( [ $expectWarning ], $warning );
2159 $this->assertLastSql( 'BEGIN; DELETE FROM error WHERE 1; COMMIT' );
2160
2161 // Deprecation only gets raised once
2162 $warning = [];
2163 $this->database->begin( __METHOD__ );
2164 call_user_func( $doError );
2165 $this->database->delete( 'x', [ 'field' => 1 ], __METHOD__ );
2166 $this->database->commit( __METHOD__ );
2167 $this->assertSame( [ $expectWarning ], $warning );
2168 // phpcs:ignore
2169 $this->assertLastSql( 'BEGIN; DELETE FROM error WHERE 1; DELETE FROM x WHERE field = \'1\'; COMMIT' );
2170 }
2171
2175 public function testPrematureClose1() {
2176 $fname = __METHOD__;
2177 $this->database->begin( __METHOD__ );
2178 $this->database->onTransactionCommitOrIdle( function () use ( $fname ) {
2179 $this->database->query( 'SELECT 1', $fname );
2180 } );
2181 $this->database->onTransactionResolution( function () use ( $fname ) {
2182 $this->database->query( 'SELECT 2', $fname );
2183 } );
2184 $this->database->delete( 'x', [ 'field' => 3 ], __METHOD__ );
2185 try {
2186 $this->database->close();
2187 $this->fail( 'Expected exception not thrown' );
2188 } catch ( DBUnexpectedError $ex ) {
2189 $this->assertSame(
2190 "Wikimedia\Rdbms\Database::close: transaction is still open (from $fname).",
2191 $ex->getMessage()
2192 );
2193 }
2194
2195 $this->assertFalse( $this->database->isOpen() );
2196 $this->assertLastSql( 'BEGIN; DELETE FROM x WHERE field = \'3\'; ROLLBACK; SELECT 2' );
2197 $this->assertEquals( 0, $this->database->trxLevel() );
2198 }
2199
2203 public function testPrematureClose2() {
2204 try {
2205 $fname = __METHOD__;
2206 $this->database->startAtomic( __METHOD__ );
2207 $this->database->onTransactionCommitOrIdle( function () use ( $fname ) {
2208 $this->database->query( 'SELECT 1', $fname );
2209 } );
2210 $this->database->onAtomicSectionCancel( function () use ( $fname ) {
2211 $this->database->query( 'SELECT 2', $fname );
2212 } );
2213 $this->database->delete( 'x', [ 'field' => 3 ], __METHOD__ );
2214 $this->database->close();
2215 $this->fail( 'Expected exception not thrown' );
2216 } catch ( DBUnexpectedError $ex ) {
2217 $this->assertSame(
2218 'Wikimedia\Rdbms\Database::close: atomic sections ' .
2219 'DatabaseSQLTest::testPrematureClose2 are still open.',
2220 $ex->getMessage()
2221 );
2222 }
2223
2224 $this->assertFalse( $this->database->isOpen() );
2225 $this->assertLastSql( 'BEGIN; DELETE FROM x WHERE field = \'3\'; ROLLBACK; SELECT 2' );
2226 $this->assertEquals( 0, $this->database->trxLevel() );
2227 }
2228
2232 public function testPrematureClose3() {
2233 try {
2234 $this->database->setFlag( IDatabase::DBO_TRX );
2235 $this->database->delete( 'x', [ 'field' => 3 ], __METHOD__ );
2236 $this->assertEquals( 1, $this->database->trxLevel() );
2237 $this->database->close();
2238 $this->fail( 'Expected exception not thrown' );
2239 } catch ( DBUnexpectedError $ex ) {
2240 $this->assertSame(
2241 'Wikimedia\Rdbms\Database::close: ' .
2242 'mass commit/rollback of peer transaction required (DBO_TRX set).',
2243 $ex->getMessage()
2244 );
2245 }
2246
2247 $this->assertFalse( $this->database->isOpen() );
2248 $this->assertLastSql( 'BEGIN; DELETE FROM x WHERE field = \'3\'; ROLLBACK' );
2249 $this->assertEquals( 0, $this->database->trxLevel() );
2250 }
2251
2255 public function testPrematureClose4() {
2256 $this->database->setFlag( IDatabase::DBO_TRX );
2257 $this->database->query( 'SELECT 1', __METHOD__ );
2258 $this->assertEquals( 1, $this->database->trxLevel() );
2259 $this->database->close();
2260 $this->database->clearFlag( IDatabase::DBO_TRX );
2261
2262 $this->assertFalse( $this->database->isOpen() );
2263 $this->assertLastSql( 'BEGIN; SELECT 1; ROLLBACK' );
2264 $this->assertEquals( 0, $this->database->trxLevel() );
2265 }
2266
2270 public function testSelectFieldValues() {
2271 $this->database->forceNextResult( [
2272 (object)[ 'value' => 'row1' ],
2273 (object)[ 'value' => 'row2' ],
2274 (object)[ 'value' => 'row3' ],
2275 ] );
2276
2277 $this->assertSame(
2278 [ 'row1', 'row2', 'row3' ],
2279 $this->database->selectFieldValues( 'table', 'table.field', 'conds', __METHOD__ )
2280 );
2281 $this->assertLastSql( 'SELECT table.field AS value FROM table WHERE conds' );
2282 }
2283}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
if(defined( 'MW_SETUP_CALLBACK')) $fname
Customization point after all loading (constants, functions, classes, DefaultSettings,...
Definition Setup.php:123
Test the parts of the Database abstract class that deal with creating SQL text.
static provideUnionConditionPermutations()
static provideNativeReplace()
testSessionTempTables()
Wikimedia\Rdbms\Database::registerTempTableWrite.
testSelect( $sql, $sqlText)
provideSelect Wikimedia\Rdbms\Database::select Wikimedia\Rdbms\Database::selectSQLText Wikimedia\Rdbm...
testBuildConcat( $stringList, $sqlText)
provideBuildConcat Wikimedia\Rdbms\Database::buildConcat
testInsertSelectBatching()
Wikimedia\Rdbms\Database::insertSelect Wikimedia\Rdbms\Database::nativeInsertSelect.
testDropTable()
Wikimedia\Rdbms\Database::dropTable.
testTransactionStatementRollbackIgnoring()
\Wikimedia\Rdbms\Database::query
testMakeList( $list, $mode, $sqlText)
provideMakeList Wikimedia\Rdbms\Database::makeList
testNoAtomicSectionForCallback()
\Wikimedia\Rdbms\Database::onAtomicSectionCancel
testUncancellableAtomicSection()
\Wikimedia\Rdbms\Database::cancelAtomic
testSelectFieldValues()
Wikimedia\Rdbms\Database::selectFieldValues()
static provideConditional()
testBuildSubstring( $input, $start, $length, $expected)
Wikimedia\Rdbms\Database::buildSubstring provideBuildSubstring.
static provideSelectRowCount()
testBuildLike( $array, $sqlText)
provideBuildLike Wikimedia\Rdbms\Database::buildLike Wikimedia\Rdbms\Database::escapeLikeInternal
testPrematureClose1()
\Wikimedia\Rdbms\Database::close
testImplicitTransactionRollback()
\Wikimedia\Rdbms\Database::query
DatabaseTestHelper Database $database
static provideAtomicSectionMethodsForErrors()
testConditional( $sql, $sqlText)
provideConditional Wikimedia\Rdbms\Database::conditional
testTransactionErrorState2()
\Wikimedia\Rdbms\Database::query
testTransactionRollback()
Wikimedia\Rdbms\Database::rollback Wikimedia\Rdbms\Database::doRollback.
static provideInsertSelect()
testNoAtomicSection( $method)
provideAtomicSectionMethodsForErrors \Wikimedia\Rdbms\Database::endAtomic \Wikimedia\Rdbms\Database::...
static provideLockForUpdate()
testBuildSubstring_invalidParams( $start, $length)
Wikimedia\Rdbms\Database::buildSubstring Wikimedia\Rdbms\Database::assertBuildSubstringParams provide...
static provideBuildConcat()
testReplace( $sql, $sqlText)
provideReplace Wikimedia\Rdbms\Database::replace
testSelectRowCount( $sql, $sqlText)
Wikimedia\Rdbms\Subquery provideSelectRowCount.
testUnionQueries( $sql, $sqlText)
provideUnionQueries Wikimedia\Rdbms\Database::unionQueries
testUnionConditionPermutations( $params, $expect)
provideUnionConditionPermutations Wikimedia\Rdbms\Database::unionConditionPermutations
testAtomicSectionsRecovery()
\Wikimedia\Rdbms\Database::doSavepoint \Wikimedia\Rdbms\Database::doReleaseSavepoint \Wikimedia\Rdbms...
testUpsert( $sql, $sqlText)
provideUpsert Wikimedia\Rdbms\Database::upsert
testDropNonExistingTable()
Wikimedia\Rdbms\Database::dropTable.
testUpdate( $sql, $sqlText)
provideUpdate Wikimedia\Rdbms\Database::update Wikimedia\Rdbms\Database::makeUpdateOptions Wikimedia\...
testInsert( $sql, $sqlText)
provideInsert Wikimedia\Rdbms\Database::insert Wikimedia\Rdbms\Database::makeInsertOptions
testPrematureClose3()
\Wikimedia\Rdbms\Database::close
testPrematureClose4()
\Wikimedia\Rdbms\Database::close
testDelete( $sql, $sqlText)
provideDelete Wikimedia\Rdbms\Database::delete
testPrematureClose2()
\Wikimedia\Rdbms\Database::close
testLockForUpdate( $sql, $sqlText)
provideLockForUpdate Wikimedia\Rdbms\Database::lockForUpdate
testBuildIntegerCast()
\Wikimedia\Rdbms\Database::buildIntegerCast
testNativeReplace( $sql, $sqlText)
provideNativeReplace Wikimedia\Rdbms\Database::nativeReplace
testTransactionErrorState1()
\Wikimedia\Rdbms\DBTransactionStateError \Wikimedia\Rdbms\Database::assertTransactionStatus
assertLastSql( $sqlText)
testAtomicSectionsTrxRound()
\Wikimedia\Rdbms\Database::doSavepoint \Wikimedia\Rdbms\Database::doReleaseSavepoint \Wikimedia\Rdbms...
testInvalidAtomicSectionEnded( $method)
provideAtomicSectionMethodsForErrors \Wikimedia\Rdbms\Database::endAtomic \Wikimedia\Rdbms\Database::...
testTransactionCommit()
Wikimedia\Rdbms\Database::commit Wikimedia\Rdbms\Database::doCommit.
testAtomicSections()
\Wikimedia\Rdbms\Database::doSavepoint \Wikimedia\Rdbms\Database::doReleaseSavepoint \Wikimedia\Rdbms...
testAtomicSectionsCallbackCancellation()
\Wikimedia\Rdbms\Database::doSavepoint \Wikimedia\Rdbms\Database::doReleaseSavepoint \Wikimedia\Rdbms...
assertLastSqlDb( $sqlText, DatabaseTestHelper $db)
testInsertSelect( $sql, $sqlTextNative, $sqlSelect, $sqlInsert)
provideInsertSelect Wikimedia\Rdbms\Database::insertSelect Wikimedia\Rdbms\Database::nativeInsertSele...
testDeleteJoin( $sql, $sqlText)
provideDeleteJoin Wikimedia\Rdbms\Database::deleteJoin
Helper for testing the methods from the Database class.
getLastSqls()
Returns SQL queries grouped by '; ' Clear the list of queries that have been done so far.
Relational database abstraction object.
Definition Database.php:49
Used by Database::buildLike() to represent characters that have special meaning in SQL LIKE clauses a...
Definition LikeMatch.php:10
const LIST_NAMES
Definition Defines.php:54
const LIST_COMMA
Definition Defines.php:51
const LIST_SET
Definition Defines.php:53
const LIST_OR
Definition Defines.php:55
const LIST_AND
Definition Defines.php:52
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults also a ContextSource after deleting those rows but within the same transaction $rows
Definition hooks.txt:2818
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition hooks.txt:783
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:2004
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title e g db for database replication lag or jobqueue for job queue size converted to pseudo seconds It is possible to add more fields and they will be returned to the user in the API response after the basic globals have been set but before ordinary actions take place $output
Definition hooks.txt:2272
returning false will NOT prevent logging $e
Definition hooks.txt:2175
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
if(is_array($mode)) switch( $mode) $input
$params