24 $this->assertEquals(
$db->getLastSqls(), $sqlText );
35 isset( $sql[
'conds'] ) ? $sql[
'conds'] : [],
37 isset( $sql[
'options'] ) ? $sql[
'options'] : [],
38 isset( $sql[
'join_conds'] ) ? $sql[
'join_conds'] : []
48 'fields' => [
'field',
'alias' =>
'field2' ],
49 'conds' => [
'alias' =>
'text' ],
51 "SELECT field,field2 AS alias " .
53 "WHERE alias = 'text'"
58 'fields' => [
'field',
'alias' =>
'field2' ],
59 'conds' => [
'alias' =>
'text' ],
60 'options' => [
'LIMIT' => 1,
'ORDER BY' =>
'field' ],
62 "SELECT field,field2 AS alias " .
64 "WHERE alias = 'text' " .
70 'tables' => [
'table',
't2' =>
'table2' ],
71 'fields' => [
'tid',
'field',
'alias' =>
'field2',
't2.id' ],
72 'conds' => [
'alias' =>
'text' ],
73 'options' => [
'LIMIT' => 1,
'ORDER BY' =>
'field' ],
74 'join_conds' => [
't2' => [
75 'LEFT JOIN',
'tid = t2.id'
78 "SELECT tid,field,field2 AS alias,t2.id " .
79 "FROM table LEFT JOIN table2 t2 ON ((tid = t2.id)) " .
80 "WHERE alias = 'text' " .
86 'tables' => [
'table',
't2' =>
'table2' ],
87 'fields' => [
'tid',
'field',
'alias' =>
'field2',
't2.id' ],
88 'conds' => [
'alias' =>
'text' ],
89 'options' => [
'LIMIT' => 1,
'GROUP BY' =>
'field',
'HAVING' =>
'COUNT(*) > 1' ],
90 'join_conds' => [
't2' => [
91 'LEFT JOIN',
'tid = t2.id'
94 "SELECT tid,field,field2 AS alias,t2.id " .
95 "FROM table LEFT JOIN table2 t2 ON ((tid = t2.id)) " .
96 "WHERE alias = 'text' " .
97 "GROUP BY field HAVING COUNT(*) > 1 " .
102 'tables' => [
'table',
't2' =>
'table2' ],
103 'fields' => [
'tid',
'field',
'alias' =>
'field2',
't2.id' ],
104 'conds' => [
'alias' =>
'text' ],
107 'GROUP BY' => [
'field',
'field2' ],
108 'HAVING' => [
'COUNT(*) > 1',
'field' => 1 ]
110 'join_conds' => [
't2' => [
111 'LEFT JOIN',
'tid = t2.id'
114 "SELECT tid,field,field2 AS alias,t2.id " .
115 "FROM table LEFT JOIN table2 t2 ON ((tid = t2.id)) " .
116 "WHERE alias = 'text' " .
117 "GROUP BY field,field2 HAVING (COUNT(*) > 1) AND field = '1' " .
122 'tables' => [
'table' ],
123 'fields' => [
'alias' =>
'field' ],
124 'conds' => [
'alias' => [ 1, 2, 3, 4 ] ],
126 "SELECT field AS alias " .
128 "WHERE alias IN ('1','2','3','4')"
143 isset( $sql[
'options'] ) ? $sql[
'options'] : []
153 'values' => [
'field' =>
'text',
'field2' =>
'text2' ],
154 'conds' => [
'alias' =>
'text' ],
157 "SET field = 'text'" .
158 ",field2 = 'text2' " .
159 "WHERE alias = 'text'"
164 'values' => [
'field = other',
'field2' =>
'text2' ],
165 'conds' => [
'id' =>
'1' ],
168 "SET field = other" .
169 ",field2 = 'text2' " .
175 'values' => [
'field = other',
'field2' =>
'text2' ],
179 "SET field = other" .
203 'conds' => [
'alias' =>
'text' ],
205 "DELETE FROM table " .
206 "WHERE alias = 'text'"
226 $sql[
'uniqueIndexes'],
237 'table' =>
'upsert_table',
238 'rows' => [
'field' =>
'text',
'field2' =>
'text2' ],
239 'uniqueIndexes' => [
'field' ],
240 'set' => [
'field' =>
'set' ],
243 "UPDATE upsert_table " .
244 "SET field = 'set' " .
245 "WHERE ((field = 'text')); " .
246 "INSERT IGNORE INTO upsert_table " .
248 "VALUES ('text','text2'); " .
274 'delTable' =>
'table',
275 'joinTable' =>
'table_join',
277 'joinVar' =>
'field_join',
278 'conds' => [
'alias' =>
'text' ],
280 "DELETE FROM table " .
282 "SELECT field_join FROM table_join WHERE alias = 'text'" .
287 'delTable' =>
'table',
288 'joinTable' =>
'table_join',
290 'joinVar' =>
'field_join',
293 "DELETE FROM table " .
295 "SELECT field_join FROM table_join " .
310 isset( $sql[
'options'] ) ? $sql[
'options'] : []
320 'rows' => [
'field' =>
'text',
'field2' => 2 ],
322 "INSERT INTO table " .
324 "VALUES ('text','2')"
329 'rows' => [
'field' =>
'text',
'field2' => 2 ],
330 'options' =>
'IGNORE',
332 "INSERT IGNORE INTO table " .
334 "VALUES ('text','2')"
340 [
'field' =>
'text',
'field2' => 2 ],
341 [
'field' =>
'multi',
'field2' => 3 ],
343 'options' =>
'IGNORE',
345 "INSERT IGNORE INTO table " .
365 isset( $sql[
'insertOptions'] ) ? $sql[
'insertOptions'] : [],
366 isset( $sql[
'selectOptions'] ) ? $sql[
'selectOptions'] : []
371 $dbWeb->forceNextResult( [
372 array_flip( array_keys( $sql[
'varMap'] ) )
374 $dbWeb->insertSelect(
380 isset( $sql[
'insertOptions'] ) ? $sql[
'insertOptions'] : [],
381 isset( $sql[
'selectOptions'] ) ? $sql[
'selectOptions'] : []
383 $this->
assertLastSqlDb( implode(
'; ', [ $sqlSelect, $sqlInsert ] ), $dbWeb );
390 'destTable' =>
'insert_table',
391 'srcTable' =>
'select_table',
392 'varMap' => [
'field_insert' =>
'field_select',
'field' =>
'field2' ],
395 "INSERT INTO insert_table " .
396 "(field_insert,field) " .
397 "SELECT field_select,field2 " .
399 "SELECT field_select AS field_insert,field2 AS field " .
400 "FROM select_table WHERE * FOR UPDATE",
401 "INSERT INTO insert_table (field_insert,field) VALUES ('0','1')"
405 'destTable' =>
'insert_table',
406 'srcTable' =>
'select_table',
407 'varMap' => [
'field_insert' =>
'field_select',
'field' =>
'field2' ],
408 'conds' => [
'field' => 2 ],
410 "INSERT INTO insert_table " .
411 "(field_insert,field) " .
412 "SELECT field_select,field2 " .
413 "FROM select_table " .
415 "SELECT field_select AS field_insert,field2 AS field FROM " .
416 "select_table WHERE field = '2' FOR UPDATE",
417 "INSERT INTO insert_table (field_insert,field) VALUES ('0','1')"
421 'destTable' =>
'insert_table',
422 'srcTable' =>
'select_table',
423 'varMap' => [
'field_insert' =>
'field_select',
'field' =>
'field2' ],
424 'conds' => [
'field' => 2 ],
425 'insertOptions' =>
'IGNORE',
426 'selectOptions' => [
'ORDER BY' =>
'field' ],
428 "INSERT IGNORE INTO insert_table " .
429 "(field_insert,field) " .
430 "SELECT field_select,field2 " .
431 "FROM select_table " .
432 "WHERE field = '2' " .
434 "SELECT field_select AS field_insert,field2 AS field " .
435 "FROM select_table WHERE field = '2' ORDER BY field FOR UPDATE",
436 "INSERT IGNORE INTO insert_table (field_insert,field) VALUES ('0','1')"
448 $sql[
'uniqueIndexes'],
459 'table' =>
'replace_table',
460 'uniqueIndexes' => [
'field' ],
461 'rows' => [
'field' =>
'text',
'field2' =>
'text2' ],
463 "DELETE FROM replace_table " .
464 "WHERE ( field='text' ); " .
465 "INSERT INTO replace_table " .
467 "VALUES ('text','text2')"
471 'table' =>
'module_deps',
472 'uniqueIndexes' => [ [
'md_module',
'md_skin' ] ],
474 'md_module' =>
'module',
479 "DELETE FROM module_deps " .
480 "WHERE ( md_module='module' AND md_skin='skin' ); " .
481 "INSERT INTO module_deps " .
482 "(md_module,md_skin,md_deps) " .
483 "VALUES ('module','skin','deps')"
487 'table' =>
'module_deps',
488 'uniqueIndexes' => [ [
'md_module',
'md_skin' ] ],
491 'md_module' =>
'module',
495 'md_module' =>
'module2',
496 'md_skin' =>
'skin2',
497 'md_deps' =>
'deps2',
501 "DELETE FROM module_deps " .
502 "WHERE ( md_module='module' AND md_skin='skin' ); " .
503 "INSERT INTO module_deps " .
504 "(md_module,md_skin,md_deps) " .
505 "VALUES ('module','skin','deps'); " .
506 "DELETE FROM module_deps " .
507 "WHERE ( md_module='module2' AND md_skin='skin2' ); " .
508 "INSERT INTO module_deps " .
509 "(md_module,md_skin,md_deps) " .
510 "VALUES ('module2','skin2','deps2')"
514 'table' =>
'module_deps',
515 'uniqueIndexes' => [
'md_module',
'md_skin' ],
518 'md_module' =>
'module',
522 'md_module' =>
'module2',
523 'md_skin' =>
'skin2',
524 'md_deps' =>
'deps2',
528 "DELETE FROM module_deps " .
529 "WHERE ( md_module='module' ) OR ( md_skin='skin' ); " .
530 "INSERT INTO module_deps " .
531 "(md_module,md_skin,md_deps) " .
532 "VALUES ('module','skin','deps'); " .
533 "DELETE FROM module_deps " .
534 "WHERE ( md_module='module2' ) OR ( md_skin='skin2' ); " .
535 "INSERT INTO module_deps " .
536 "(md_module,md_skin,md_deps) " .
537 "VALUES ('module2','skin2','deps2')"
541 'table' =>
'module_deps',
542 'uniqueIndexes' => [],
544 'md_module' =>
'module',
549 "INSERT INTO module_deps " .
550 "(md_module,md_skin,md_deps) " .
551 "VALUES ('module','skin','deps')"
573 'table' =>
'replace_table',
574 'rows' => [
'field' =>
'text',
'field2' =>
'text2' ],
576 "REPLACE INTO replace_table " .
578 "VALUES ('text','text2')"
588 $this->assertEquals( trim( $this->
database->conditional(
599 'conds' => [
'field' =>
'text' ],
603 "(CASE WHEN field = 'text' THEN 1 ELSE NULL END)"
607 'conds' => [
'field' =>
'text',
'field2' =>
'anothertext' ],
611 "(CASE WHEN field = 'text' AND field2 = 'anothertext' THEN 1 ELSE NULL END)"
615 'conds' =>
'field=1',
619 "(CASE WHEN field=1 THEN 1 ELSE NULL END)"
629 $this->assertEquals( trim( $this->
database->buildConcat(
637 [
'field',
'field2' ],
638 "CONCAT(field,field2)"
641 [
"'test'",
'field2' ],
642 "CONCAT('test',field2)"
652 $this->assertEquals( trim( $this->
database->buildLike(
668 [
'text',
new LikeMatch(
'%' ),
'text2' ],
683 $this->assertEquals( trim( $this->
database->unionQueries(
693 'sqls' => [
'RAW SQL',
'RAW2SQL' ],
696 "(RAW SQL) UNION ALL (RAW2SQL)"
700 'sqls' => [
'RAW SQL',
'RAW2SQL' ],
703 "(RAW SQL) UNION (RAW2SQL)"
707 'sqls' => [
'RAW SQL',
'RAW2SQL',
'RAW3SQL' ],
710 "(RAW SQL) UNION (RAW2SQL) UNION (RAW3SQL)"
719 $this->
database->begin( __METHOD__ );
720 $this->
database->commit( __METHOD__ );
728 $this->
database->begin( __METHOD__ );
729 $this->
database->rollback( __METHOD__ );
737 $this->
database->setExistingTables( [
'table' ] );
738 $this->
database->dropTable(
'table', __METHOD__ );
747 $this->
database->dropTable(
'non_existing', __METHOD__ )
756 $this->assertEquals( trim( $this->
database->makeList(
764 [
'value',
'value2' ],
769 [
'field',
'field2' ],
774 [
'field' =>
'value',
'field2' =>
'value2' ],
776 "field = 'value' AND field2 = 'value2'"
779 [
'field' => null,
"field2 != 'value2'" ],
781 "field IS NULL AND (field2 != 'value2')"
784 [
'field' => [
'value', null,
'value2' ],
'field2' =>
'value2' ],
786 "(field IN ('value','value2') OR field IS NULL) AND field2 = 'value2'"
789 [
'field' => [ null ],
'field2' => null ],
791 "field IS NULL AND field2 IS NULL"
794 [
'field' =>
'value',
'field2' =>
'value2' ],
796 "field = 'value' OR field2 = 'value2'"
799 [
'field' =>
'value',
'field2' => null ],
801 "field = 'value' OR field2 IS NULL"
804 [
'field' => [
'value',
'value2' ],
'field2' => [
'value' ] ],
806 "field IN ('value','value2') OR field2 = 'value'"
809 [
'field' => [ null,
'value', null,
'value2' ],
"field2 != 'value2'" ],
811 "(field IN ('value','value2') OR field IS NULL) OR (field2 != 'value2')"
814 [
'field' =>
'value',
'field2' =>
'value2' ],
816 "field = 'value',field2 = 'value2'"
819 [
'field' =>
'value',
'field2' => null ],
821 "field = 'value',field2 = NULL"
824 [
'field' =>
'value',
"field2 != 'value2'" ],
826 "field = 'value',field2 != 'value2'"
832 $temp1 = $this->
database->tableName(
'tmp_table_1' );
833 $temp2 = $this->
database->tableName(
'tmp_table_2' );
834 $temp3 = $this->
database->tableName(
'tmp_table_3' );
836 $this->
database->query(
"CREATE TEMPORARY TABLE $temp1 LIKE orig_tbl", __METHOD__ );
837 $this->
database->query(
"CREATE TEMPORARY TABLE $temp2 LIKE orig_tbl", __METHOD__ );
838 $this->
database->query(
"CREATE TEMPORARY TABLE $temp3 LIKE orig_tbl", __METHOD__ );
840 $this->assertTrue( $this->
database->tableExists(
"tmp_table_1", __METHOD__ ) );
841 $this->assertTrue( $this->
database->tableExists(
"tmp_table_2", __METHOD__ ) );
842 $this->assertTrue( $this->
database->tableExists(
"tmp_table_3", __METHOD__ ) );
844 $this->
database->dropTable(
'tmp_table_1', __METHOD__ );
845 $this->
database->dropTable(
'tmp_table_2', __METHOD__ );
846 $this->
database->dropTable(
'tmp_table_3', __METHOD__ );
848 $this->assertFalse( $this->
database->tableExists(
"tmp_table_1", __METHOD__ ) );
849 $this->assertFalse( $this->
database->tableExists(
"tmp_table_2", __METHOD__ ) );
850 $this->assertFalse( $this->
database->tableExists(
"tmp_table_3", __METHOD__ ) );
852 $this->
database->query(
"CREATE TEMPORARY TABLE tmp_table_1 LIKE orig_tbl", __METHOD__ );
853 $this->
database->query(
"CREATE TEMPORARY TABLE 'tmp_table_2' LIKE orig_tbl", __METHOD__ );
854 $this->
database->query(
"CREATE TEMPORARY TABLE `tmp_table_3` LIKE orig_tbl", __METHOD__ );
856 $this->assertTrue( $this->
database->tableExists(
"tmp_table_1", __METHOD__ ) );
857 $this->assertTrue( $this->
database->tableExists(
"tmp_table_2", __METHOD__ ) );
858 $this->assertTrue( $this->
database->tableExists(
"tmp_table_3", __METHOD__ ) );
860 $this->
database->query(
"DROP TEMPORARY TABLE tmp_table_1 LIKE orig_tbl", __METHOD__ );
861 $this->
database->query(
"DROP TEMPORARY TABLE 'tmp_table_2' LIKE orig_tbl", __METHOD__ );
862 $this->
database->query(
"DROP TABLE `tmp_table_3` LIKE orig_tbl", __METHOD__ );
864 $this->assertFalse( $this->
database->tableExists(
"tmp_table_1", __METHOD__ ) );
865 $this->assertFalse( $this->
database->tableExists(
"tmp_table_2", __METHOD__ ) );
866 $this->assertFalse( $this->
database->tableExists(
"tmp_table_3", __METHOD__ ) );
testConditional($sql, $sqlText)
provideConditional Database::conditional
testSelect($sql, $sqlText)
provideSelect Database::select
testTransactionCommit()
Database::commit.
testUnionQueries($sql, $sqlText)
provideUnionQueries Database::unionQueries
static provideBuildConcat()
static provideUnionQueries()
testTransactionRollback()
Database::rollback.
static provideInsertSelect()
testDeleteJoin($sql, $sqlText)
provideDeleteJoin Database::deleteJoin
testNativeReplace($sql, $sqlText)
provideNativeReplace Database::nativeReplace
testBuildConcat($stringList, $sqlText)
provideBuildConcat Database::buildConcat
static provideDeleteJoin()
Helper for testing the methods from the Database class.
testDropNonExistingTable()
Database::dropTable.
testUpdate($sql, $sqlText)
provideUpdate Database::update
testBuildLike($array, $sqlText)
provideBuildLike Database::buildLike
testInsertSelect($sql, $sqlTextNative, $sqlSelect, $sqlInsert)
provideInsertSelect Database::insertSelect
Used by Database::buildLike() to represent characters that have special meaning in SQL LIKE clauses a...
testDelete($sql, $sqlText)
provideDelete Database::delete
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the database
static provideConditional()
static provideNativeReplace()
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
testUpsert($sql, $sqlText)
provideUpsert Database::upsert
DatabaseTestHelper $database
static provideBuildLike()
testInsert($sql, $sqlText)
provideInsert Database::insert
assertLastSqlDb($sqlText, $db)
testReplace($sql, $sqlText)
provideReplace Database::replace
testMakeList($list, $mode, $sqlText)
provideMakeList Database::makeList
testDropTable()
Database::dropTable.
Test the abstract database layer This is a non DBMS depending test.