2012-08-15 13:16:09 +00:00
< ? php
2017-02-07 05:20:39 +00:00
use Wikimedia\Rdbms\LikeMatch ;
2012-08-15 13:16:09 +00:00
/**
2017-07-20 20:17:11 +00:00
* Test the parts of the Database abstract class that deal
* with creating SQL text .
2012-08-15 13:16:09 +00:00
*/
2018-02-17 12:29:13 +00:00
class DatabaseSQLTest extends PHPUnit\Framework\TestCase {
2017-12-29 23:22:37 +00:00
use MediaWikiCoversValidator ;
2016-08-27 17:10:46 +00:00
/** @var DatabaseTestHelper */
2013-04-13 14:40:04 +00:00
private $database ;
2012-10-08 10:56:20 +00:00
protected function setUp () {
2012-12-06 16:57:37 +00:00
parent :: setUp ();
2016-08-27 17:10:46 +00:00
$this -> database = new DatabaseTestHelper ( __CLASS__ , [ 'cliMode' => true ] );
2013-04-13 14:40:04 +00:00
}
protected function assertLastSql ( $sqlText ) {
$this -> assertEquals (
2017-06-09 16:58:09 +00:00
$sqlText ,
$this -> database -> getLastSqls ()
2013-04-13 14:40:04 +00:00
);
2012-08-15 13:16:09 +00:00
}
2017-07-25 02:21:12 +00:00
protected function assertLastSqlDb ( $sqlText , DatabaseTestHelper $db ) {
2017-06-09 16:58:09 +00:00
$this -> assertEquals ( $sqlText , $db -> getLastSqls () );
2016-08-27 17:10:46 +00:00
}
2012-08-15 13:16:09 +00:00
/**
2013-04-13 14:40:04 +00:00
* @ dataProvider provideSelect
2017-07-20 20:17:11 +00:00
* @ covers Wikimedia\Rdbms\Database :: select
2017-07-25 02:21:12 +00:00
* @ covers Wikimedia\Rdbms\Database :: selectSQLText
* @ covers Wikimedia\Rdbms\Database :: tableNamesWithIndexClauseOrJOIN
2017-07-28 04:19:53 +00:00
* @ covers Wikimedia\Rdbms\Database :: useIndexClause
* @ covers Wikimedia\Rdbms\Database :: ignoreIndexClause
2017-07-25 02:21:12 +00:00
* @ covers Wikimedia\Rdbms\Database :: makeSelectOptions
* @ covers Wikimedia\Rdbms\Database :: makeOrderBy
* @ covers Wikimedia\Rdbms\Database :: makeGroupByWithHaving
2012-08-15 13:16:09 +00:00
*/
2013-10-23 22:51:31 +00:00
public function testSelect ( $sql , $sqlText ) {
2013-04-13 14:40:04 +00:00
$this -> database -> select (
$sql [ 'tables' ],
$sql [ 'fields' ],
2016-02-17 09:09:32 +00:00
isset ( $sql [ 'conds' ] ) ? $sql [ 'conds' ] : [],
2012-08-15 13:16:09 +00:00
__METHOD__ ,
2016-02-17 09:09:32 +00:00
isset ( $sql [ 'options' ] ) ? $sql [ 'options' ] : [],
isset ( $sql [ 'join_conds' ] ) ? $sql [ 'join_conds' ] : []
2013-04-13 14:40:04 +00:00
);
$this -> assertLastSql ( $sqlText );
2012-08-15 13:16:09 +00:00
}
2013-04-13 14:40:04 +00:00
public static function provideSelect () {
2016-02-17 09:09:32 +00:00
return [
[
[
2012-08-15 13:16:09 +00:00
'tables' => 'table' ,
2016-02-17 09:09:32 +00:00
'fields' => [ 'field' , 'alias' => 'field2' ],
'conds' => [ 'alias' => 'text' ],
],
2013-04-13 14:40:04 +00:00
" SELECT field,field2 AS alias " .
" FROM table " .
2013-02-14 13:10:38 +00:00
" WHERE alias = 'text' "
2016-02-17 09:09:32 +00:00
],
2017-07-25 02:49:17 +00:00
[
[
// 'tables' with space prepended indicates pre-escaped table name
'tables' => ' table LEFT JOIN table2' ,
'fields' => [ 'field' ],
'conds' => [ 'field' => 'text' ],
],
" SELECT field FROM table LEFT JOIN table2 WHERE field = 'text' "
],
[
[
// Empty 'tables' is allowed
'tables' => '' ,
'fields' => [ 'SPECIAL_QUERY()' ],
],
" SELECT SPECIAL_QUERY() "
],
2016-02-17 09:09:32 +00:00
[
[
2012-08-15 13:16:09 +00:00
'tables' => 'table' ,
2016-02-17 09:09:32 +00:00
'fields' => [ 'field' , 'alias' => 'field2' ],
'conds' => [ 'alias' => 'text' ],
'options' => [ 'LIMIT' => 1 , 'ORDER BY' => 'field' ],
],
2013-04-13 14:40:04 +00:00
" SELECT field,field2 AS alias " .
" FROM table " .
" WHERE alias = 'text' " .
2013-02-14 13:10:38 +00:00
" ORDER BY field " .
" LIMIT 1 "
2016-02-17 09:09:32 +00:00
],
[
[
'tables' => [ 'table' , 't2' => 'table2' ],
'fields' => [ 'tid' , 'field' , 'alias' => 'field2' , 't2.id' ],
'conds' => [ 'alias' => 'text' ],
'options' => [ 'LIMIT' => 1 , 'ORDER BY' => 'field' ],
'join_conds' => [ 't2' => [
2012-08-15 13:16:09 +00:00
'LEFT JOIN' , 'tid = t2.id'
2016-02-17 09:09:32 +00:00
] ],
],
2013-04-13 14:40:04 +00:00
" SELECT tid,field,field2 AS alias,t2.id " .
" FROM table LEFT JOIN table2 t2 ON ((tid = t2.id)) " .
" WHERE alias = 'text' " .
2013-02-14 13:10:38 +00:00
" ORDER BY field " .
" LIMIT 1 "
2016-02-17 09:09:32 +00:00
],
[
[
'tables' => [ 'table' , 't2' => 'table2' ],
'fields' => [ 'tid' , 'field' , 'alias' => 'field2' , 't2.id' ],
'conds' => [ 'alias' => 'text' ],
'options' => [ 'LIMIT' => 1 , 'GROUP BY' => 'field' , 'HAVING' => 'COUNT(*) > 1' ],
'join_conds' => [ 't2' => [
2012-08-31 18:12:19 +00:00
'LEFT JOIN' , 'tid = t2.id'
2016-02-17 09:09:32 +00:00
] ],
],
2013-04-13 14:40:04 +00:00
" SELECT tid,field,field2 AS alias,t2.id " .
" FROM table LEFT JOIN table2 t2 ON ((tid = t2.id)) " .
" WHERE alias = 'text' " .
2013-02-14 13:10:38 +00:00
" GROUP BY field HAVING COUNT(*) > 1 " .
" LIMIT 1 "
2016-02-17 09:09:32 +00:00
],
[
[
'tables' => [ 'table' , 't2' => 'table2' ],
'fields' => [ 'tid' , 'field' , 'alias' => 'field2' , 't2.id' ],
'conds' => [ 'alias' => 'text' ],
'options' => [
2014-04-24 16:06:21 +00:00
'LIMIT' => 1 ,
2016-02-17 09:09:32 +00:00
'GROUP BY' => [ 'field' , 'field2' ],
'HAVING' => [ 'COUNT(*) > 1' , 'field' => 1 ]
],
'join_conds' => [ 't2' => [
2012-08-31 18:12:19 +00:00
'LEFT JOIN' , 'tid = t2.id'
2016-02-17 09:09:32 +00:00
] ],
],
2013-04-13 14:40:04 +00:00
" SELECT tid,field,field2 AS alias,t2.id " .
" FROM table LEFT JOIN table2 t2 ON ((tid = t2.id)) " .
" WHERE alias = 'text' " .
2013-02-14 13:10:38 +00:00
" GROUP BY field,field2 HAVING (COUNT(*) > 1) AND field = '1' " .
" LIMIT 1 "
2016-02-17 09:09:32 +00:00
],
[
[
'tables' => [ 'table' ],
'fields' => [ 'alias' => 'field' ],
'conds' => [ 'alias' => [ 1 , 2 , 3 , 4 ] ],
],
2013-04-13 14:40:04 +00:00
" SELECT field AS alias " .
" FROM table " .
" WHERE alias IN ('1','2','3','4') "
2016-02-17 09:09:32 +00:00
],
2017-07-28 04:19:53 +00:00
[
[
'tables' => 'table' ,
'fields' => [ 'field' ],
'options' => [ 'USE INDEX' => [ 'table' => 'X' ] ],
],
// No-op by default
" SELECT field FROM table "
],
[
[
'tables' => 'table' ,
'fields' => [ 'field' ],
'options' => [ 'IGNORE INDEX' => [ 'table' => 'X' ] ],
],
// No-op by default
" SELECT field FROM table "
],
2017-07-25 02:49:17 +00:00
[
[
'tables' => 'table' ,
'fields' => [ 'field' ],
'options' => [ 'DISTINCT' , 'LOCK IN SHARE MODE' ],
],
" SELECT DISTINCT field FROM table LOCK IN SHARE MODE "
],
[
[
'tables' => 'table' ,
'fields' => [ 'field' ],
'options' => [ 'EXPLAIN' => true ],
],
'EXPLAIN SELECT field FROM table'
],
[
[
'tables' => 'table' ,
'fields' => [ 'field' ],
'options' => [ 'FOR UPDATE' ],
],
" SELECT field FROM table FOR UPDATE "
],
2016-02-17 09:09:32 +00:00
];
2013-04-13 14:40:04 +00:00
}
/**
* @ dataProvider provideUpdate
2017-07-20 20:17:11 +00:00
* @ covers Wikimedia\Rdbms\Database :: update
2017-07-25 02:21:12 +00:00
* @ covers Wikimedia\Rdbms\Database :: makeUpdateOptions
* @ covers Wikimedia\Rdbms\Database :: makeUpdateOptionsArray
2013-04-13 14:40:04 +00:00
*/
2013-10-23 22:51:31 +00:00
public function testUpdate ( $sql , $sqlText ) {
2013-04-13 14:40:04 +00:00
$this -> database -> update (
$sql [ 'table' ],
$sql [ 'values' ],
$sql [ 'conds' ],
__METHOD__ ,
2016-02-17 09:09:32 +00:00
isset ( $sql [ 'options' ] ) ? $sql [ 'options' ] : []
2013-04-13 14:40:04 +00:00
);
$this -> assertLastSql ( $sqlText );
}
public static function provideUpdate () {
2016-02-17 09:09:32 +00:00
return [
[
[
2013-04-13 14:40:04 +00:00
'table' => 'table' ,
2016-02-17 09:09:32 +00:00
'values' => [ 'field' => 'text' , 'field2' => 'text2' ],
'conds' => [ 'alias' => 'text' ],
],
2013-04-13 14:40:04 +00:00
" UPDATE table " .
" SET field = 'text' " .
" ,field2 = 'text2' " .
" WHERE alias = 'text' "
2016-02-17 09:09:32 +00:00
],
[
[
2013-04-13 14:40:04 +00:00
'table' => 'table' ,
2016-02-17 09:09:32 +00:00
'values' => [ 'field = other' , 'field2' => 'text2' ],
'conds' => [ 'id' => '1' ],
],
2013-04-13 14:40:04 +00:00
" UPDATE table " .
" SET field = other " .
" ,field2 = 'text2' " .
" WHERE id = '1' "
2016-02-17 09:09:32 +00:00
],
[
[
2013-04-13 14:40:04 +00:00
'table' => 'table' ,
2016-02-17 09:09:32 +00:00
'values' => [ 'field = other' , 'field2' => 'text2' ],
2013-04-13 14:40:04 +00:00
'conds' => '*' ,
2016-02-17 09:09:32 +00:00
],
2013-04-13 14:40:04 +00:00
" UPDATE table " .
" SET field = other " .
" ,field2 = 'text2' "
2016-02-17 09:09:32 +00:00
],
];
2013-04-13 14:40:04 +00:00
}
/**
* @ dataProvider provideDelete
2017-07-20 20:17:11 +00:00
* @ covers Wikimedia\Rdbms\Database :: delete
2013-04-13 14:40:04 +00:00
*/
2013-10-23 22:51:31 +00:00
public function testDelete ( $sql , $sqlText ) {
2013-04-13 14:40:04 +00:00
$this -> database -> delete (
$sql [ 'table' ],
$sql [ 'conds' ],
__METHOD__
);
$this -> assertLastSql ( $sqlText );
}
public static function provideDelete () {
2016-02-17 09:09:32 +00:00
return [
[
[
2013-04-13 14:40:04 +00:00
'table' => 'table' ,
2016-02-17 09:09:32 +00:00
'conds' => [ 'alias' => 'text' ],
],
2013-04-13 14:40:04 +00:00
" DELETE FROM table " .
" WHERE alias = 'text' "
2016-02-17 09:09:32 +00:00
],
[
[
2013-04-13 14:40:04 +00:00
'table' => 'table' ,
'conds' => '*' ,
2016-02-17 09:09:32 +00:00
],
2013-04-13 14:40:04 +00:00
" DELETE FROM table "
2016-02-17 09:09:32 +00:00
],
];
2013-04-13 14:40:04 +00:00
}
2013-06-04 18:34:49 +00:00
/**
* @ dataProvider provideUpsert
2017-07-20 20:17:11 +00:00
* @ covers Wikimedia\Rdbms\Database :: upsert
2013-06-04 18:34:49 +00:00
*/
2013-10-23 22:51:31 +00:00
public function testUpsert ( $sql , $sqlText ) {
2013-06-04 18:34:49 +00:00
$this -> database -> upsert (
$sql [ 'table' ],
$sql [ 'rows' ],
$sql [ 'uniqueIndexes' ],
$sql [ 'set' ],
__METHOD__
);
$this -> assertLastSql ( $sqlText );
}
public static function provideUpsert () {
2016-02-17 09:09:32 +00:00
return [
[
[
2013-06-04 18:34:49 +00:00
'table' => 'upsert_table' ,
2016-02-17 09:09:32 +00:00
'rows' => [ 'field' => 'text' , 'field2' => 'text2' ],
'uniqueIndexes' => [ 'field' ],
'set' => [ 'field' => 'set' ],
],
2013-06-04 18:34:49 +00:00
" BEGIN; " .
" UPDATE upsert_table " .
" SET field = 'set' " .
" WHERE ((field = 'text')); " .
" INSERT IGNORE INTO upsert_table " .
" (field,field2) " .
" VALUES ('text','text2'); " .
" COMMIT "
2016-02-17 09:09:32 +00:00
],
];
2013-06-04 18:34:49 +00:00
}
2013-04-13 14:40:04 +00:00
/**
* @ dataProvider provideDeleteJoin
2017-07-20 20:17:11 +00:00
* @ covers Wikimedia\Rdbms\Database :: deleteJoin
2013-04-13 14:40:04 +00:00
*/
2013-10-23 22:51:31 +00:00
public function testDeleteJoin ( $sql , $sqlText ) {
2013-04-13 14:40:04 +00:00
$this -> database -> deleteJoin (
$sql [ 'delTable' ],
$sql [ 'joinTable' ],
$sql [ 'delVar' ],
$sql [ 'joinVar' ],
$sql [ 'conds' ],
__METHOD__
);
$this -> assertLastSql ( $sqlText );
}
public static function provideDeleteJoin () {
2016-02-17 09:09:32 +00:00
return [
[
[
2013-04-13 14:40:04 +00:00
'delTable' => 'table' ,
'joinTable' => 'table_join' ,
'delVar' => 'field' ,
'joinVar' => 'field_join' ,
2016-02-17 09:09:32 +00:00
'conds' => [ 'alias' => 'text' ],
],
2013-04-13 14:40:04 +00:00
" DELETE FROM table " .
" WHERE field IN ( " .
2013-04-26 12:00:22 +00:00
" SELECT field_join FROM table_join WHERE alias = 'text' " .
2013-04-13 14:40:04 +00:00
" ) "
2016-02-17 09:09:32 +00:00
],
[
[
2013-04-13 14:40:04 +00:00
'delTable' => 'table' ,
'joinTable' => 'table_join' ,
'delVar' => 'field' ,
'joinVar' => 'field_join' ,
'conds' => '*' ,
2016-02-17 09:09:32 +00:00
],
2013-04-13 14:40:04 +00:00
" DELETE FROM table " .
" WHERE field IN ( " .
2013-04-26 12:00:22 +00:00
" SELECT field_join FROM table_join " .
2013-04-13 14:40:04 +00:00
" ) "
2016-02-17 09:09:32 +00:00
],
];
2013-04-13 14:40:04 +00:00
}
/**
* @ dataProvider provideInsert
2017-07-20 20:17:11 +00:00
* @ covers Wikimedia\Rdbms\Database :: insert
2017-07-25 02:21:12 +00:00
* @ covers Wikimedia\Rdbms\Database :: makeInsertOptions
2013-04-13 14:40:04 +00:00
*/
2013-10-23 22:51:31 +00:00
public function testInsert ( $sql , $sqlText ) {
2013-04-13 14:40:04 +00:00
$this -> database -> insert (
$sql [ 'table' ],
$sql [ 'rows' ],
__METHOD__ ,
2016-02-17 09:09:32 +00:00
isset ( $sql [ 'options' ] ) ? $sql [ 'options' ] : []
2013-04-13 14:40:04 +00:00
);
$this -> assertLastSql ( $sqlText );
}
public static function provideInsert () {
2016-02-17 09:09:32 +00:00
return [
[
[
2013-04-13 14:40:04 +00:00
'table' => 'table' ,
2016-02-17 09:09:32 +00:00
'rows' => [ 'field' => 'text' , 'field2' => 2 ],
],
2013-04-13 14:40:04 +00:00
" INSERT INTO table " .
" (field,field2) " .
" VALUES ('text','2') "
2016-02-17 09:09:32 +00:00
],
[
[
2013-04-13 14:40:04 +00:00
'table' => 'table' ,
2016-02-17 09:09:32 +00:00
'rows' => [ 'field' => 'text' , 'field2' => 2 ],
2013-04-13 14:40:04 +00:00
'options' => 'IGNORE' ,
2016-02-17 09:09:32 +00:00
],
2013-04-13 14:40:04 +00:00
" INSERT IGNORE INTO table " .
" (field,field2) " .
" VALUES ('text','2') "
2016-02-17 09:09:32 +00:00
],
[
[
2013-04-13 14:40:04 +00:00
'table' => 'table' ,
2016-02-17 09:09:32 +00:00
'rows' => [
[ 'field' => 'text' , 'field2' => 2 ],
[ 'field' => 'multi' , 'field2' => 3 ],
],
2013-04-13 14:40:04 +00:00
'options' => 'IGNORE' ,
2016-02-17 09:09:32 +00:00
],
2013-04-13 14:40:04 +00:00
" INSERT IGNORE INTO table " .
" (field,field2) " .
" VALUES " .
2013-04-26 12:00:22 +00:00
" ('text','2'), " .
" ('multi','3') "
2016-02-17 09:09:32 +00:00
],
];
2013-04-13 14:40:04 +00:00
}
/**
* @ dataProvider provideInsertSelect
2017-07-20 20:17:11 +00:00
* @ covers Wikimedia\Rdbms\Database :: insertSelect
2017-07-25 02:21:12 +00:00
* @ covers Wikimedia\Rdbms\Database :: nativeInsertSelect
2013-04-13 14:40:04 +00:00
*/
2016-08-27 17:10:46 +00:00
public function testInsertSelect ( $sql , $sqlTextNative , $sqlSelect , $sqlInsert ) {
2013-04-13 14:40:04 +00:00
$this -> database -> insertSelect (
$sql [ 'destTable' ],
$sql [ 'srcTable' ],
$sql [ 'varMap' ],
$sql [ 'conds' ],
__METHOD__ ,
2016-02-17 09:09:32 +00:00
isset ( $sql [ 'insertOptions' ] ) ? $sql [ 'insertOptions' ] : [],
2017-06-09 16:58:09 +00:00
isset ( $sql [ 'selectOptions' ] ) ? $sql [ 'selectOptions' ] : [],
isset ( $sql [ 'selectJoinConds' ] ) ? $sql [ 'selectJoinConds' ] : []
2013-04-13 14:40:04 +00:00
);
2016-08-27 17:10:46 +00:00
$this -> assertLastSql ( $sqlTextNative );
$dbWeb = new DatabaseTestHelper ( __CLASS__ , [ 'cliMode' => false ] );
$dbWeb -> forceNextResult ( [
array_flip ( array_keys ( $sql [ 'varMap' ] ) )
] );
$dbWeb -> insertSelect (
$sql [ 'destTable' ],
$sql [ 'srcTable' ],
$sql [ 'varMap' ],
$sql [ 'conds' ],
__METHOD__ ,
isset ( $sql [ 'insertOptions' ] ) ? $sql [ 'insertOptions' ] : [],
2017-06-09 16:58:09 +00:00
isset ( $sql [ 'selectOptions' ] ) ? $sql [ 'selectOptions' ] : [],
isset ( $sql [ 'selectJoinConds' ] ) ? $sql [ 'selectJoinConds' ] : []
2016-08-27 17:10:46 +00:00
);
2018-02-08 19:16:29 +00:00
$this -> assertLastSqlDb ( implode ( '; ' , [ $sqlSelect , 'BEGIN' , $sqlInsert , 'COMMIT' ] ), $dbWeb );
2013-04-13 14:40:04 +00:00
}
public static function provideInsertSelect () {
2016-02-17 09:09:32 +00:00
return [
[
[
2013-04-13 14:40:04 +00:00
'destTable' => 'insert_table' ,
'srcTable' => 'select_table' ,
2016-02-17 09:09:32 +00:00
'varMap' => [ 'field_insert' => 'field_select' , 'field' => 'field2' ],
2013-04-13 14:40:04 +00:00
'conds' => '*' ,
2016-02-17 09:09:32 +00:00
],
2013-04-13 14:40:04 +00:00
" INSERT INTO insert_table " .
" (field_insert,field) " .
" SELECT field_select,field2 " .
2017-06-09 16:58:09 +00:00
" FROM select_table WHERE * " ,
2016-08-27 17:10:46 +00:00
" SELECT field_select AS field_insert,field2 AS field " .
" FROM select_table WHERE * FOR UPDATE " ,
" INSERT INTO insert_table (field_insert,field) VALUES ('0','1') "
2016-02-17 09:09:32 +00:00
],
[
[
2013-04-13 14:40:04 +00:00
'destTable' => 'insert_table' ,
'srcTable' => 'select_table' ,
2016-02-17 09:09:32 +00:00
'varMap' => [ 'field_insert' => 'field_select' , 'field' => 'field2' ],
'conds' => [ 'field' => 2 ],
],
2013-04-13 14:40:04 +00:00
" INSERT INTO insert_table " .
" (field_insert,field) " .
" SELECT field_select,field2 " .
2013-04-26 12:00:22 +00:00
" FROM select_table " .
2016-08-27 17:10:46 +00:00
" WHERE field = '2' " ,
" SELECT field_select AS field_insert,field2 AS field FROM " .
" select_table WHERE field = '2' FOR UPDATE " ,
" INSERT INTO insert_table (field_insert,field) VALUES ('0','1') "
2016-02-17 09:09:32 +00:00
],
[
[
2013-04-13 14:40:04 +00:00
'destTable' => 'insert_table' ,
'srcTable' => 'select_table' ,
2016-02-17 09:09:32 +00:00
'varMap' => [ 'field_insert' => 'field_select' , 'field' => 'field2' ],
'conds' => [ 'field' => 2 ],
2013-04-13 14:40:04 +00:00
'insertOptions' => 'IGNORE' ,
2016-02-17 09:09:32 +00:00
'selectOptions' => [ 'ORDER BY' => 'field' ],
],
2013-04-13 14:40:04 +00:00
" INSERT IGNORE INTO insert_table " .
" (field_insert,field) " .
" SELECT field_select,field2 " .
2013-04-26 12:00:22 +00:00
" FROM select_table " .
" WHERE field = '2' " .
2016-08-27 17:10:46 +00:00
" ORDER BY field " ,
" SELECT field_select AS field_insert,field2 AS field " .
" FROM select_table WHERE field = '2' ORDER BY field FOR UPDATE " ,
" INSERT IGNORE INTO insert_table (field_insert,field) VALUES ('0','1') "
2016-02-17 09:09:32 +00:00
],
2017-06-09 16:58:09 +00:00
[
[
'destTable' => 'insert_table' ,
'srcTable' => [ 'select_table1' , 'select_table2' ],
'varMap' => [ 'field_insert' => 'field_select' , 'field' => 'field2' ],
'conds' => [ 'field' => 2 ],
2018-02-08 19:18:24 +00:00
'insertOptions' => [ 'NO_AUTO_COLUMNS' ],
2017-06-09 16:58:09 +00:00
'selectOptions' => [ 'ORDER BY' => 'field' , 'FORCE INDEX' => [ 'select_table1' => 'index1' ] ],
'selectJoinConds' => [
'select_table2' => [ 'LEFT JOIN' , [ 'select_table1.foo = select_table2.bar' ] ],
],
],
" INSERT INTO insert_table " .
" (field_insert,field) " .
" SELECT field_select,field2 " .
" FROM select_table1 LEFT JOIN select_table2 ON ((select_table1.foo = select_table2.bar)) " .
" WHERE field = '2' " .
" ORDER BY field " ,
" SELECT field_select AS field_insert,field2 AS field " .
" FROM select_table1 LEFT JOIN select_table2 ON ((select_table1.foo = select_table2.bar)) " .
" WHERE field = '2' ORDER BY field FOR UPDATE " ,
" INSERT INTO insert_table (field_insert,field) VALUES ('0','1') "
],
2016-02-17 09:09:32 +00:00
];
2013-04-13 14:40:04 +00:00
}
2018-02-08 19:16:29 +00:00
public function testInsertSelectBatching () {
$dbWeb = new DatabaseTestHelper ( __CLASS__ , [ 'cliMode' => false ] );
$rows = [];
for ( $i = 0 ; $i <= 25000 ; $i ++ ) {
$rows [] = [ 'field' => $i ];
}
$dbWeb -> forceNextResult ( $rows );
$dbWeb -> insertSelect (
'insert_table' ,
'select_table' ,
[ 'field' => 'field2' ],
'*' ,
__METHOD__
);
$this -> assertLastSqlDb ( implode ( '; ' , [
'SELECT field2 AS field FROM select_table WHERE * FOR UPDATE' ,
'BEGIN' ,
" INSERT INTO insert_table (field) VALUES (' " . implode ( " '),(' " , range ( 0 , 9999 ) ) . " ') " ,
" INSERT INTO insert_table (field) VALUES (' " . implode ( " '),(' " , range ( 10000 , 19999 ) ) . " ') " ,
" INSERT INTO insert_table (field) VALUES (' " . implode ( " '),(' " , range ( 20000 , 25000 ) ) . " ') " ,
'COMMIT'
] ), $dbWeb );
}
2013-04-13 14:40:04 +00:00
/**
* @ dataProvider provideReplace
2017-07-20 20:17:11 +00:00
* @ covers Wikimedia\Rdbms\Database :: replace
2013-04-13 14:40:04 +00:00
*/
2013-10-23 22:51:31 +00:00
public function testReplace ( $sql , $sqlText ) {
2013-04-13 14:40:04 +00:00
$this -> database -> replace (
$sql [ 'table' ],
$sql [ 'uniqueIndexes' ],
$sql [ 'rows' ],
__METHOD__
);
$this -> assertLastSql ( $sqlText );
}
public static function provideReplace () {
2016-02-17 09:09:32 +00:00
return [
[
[
2013-04-13 14:40:04 +00:00
'table' => 'replace_table' ,
2016-02-17 09:09:32 +00:00
'uniqueIndexes' => [ 'field' ],
'rows' => [ 'field' => 'text' , 'field2' => 'text2' ],
],
2018-02-21 03:16:51 +00:00
" BEGIN; DELETE FROM replace_table " .
2018-01-14 03:57:51 +00:00
" WHERE (field = 'text'); " .
2013-04-13 14:40:04 +00:00
" INSERT INTO replace_table " .
2013-04-26 12:00:22 +00:00
" (field,field2) " .
2018-02-21 03:16:51 +00:00
" VALUES ('text','text2'); COMMIT "
2016-02-17 09:09:32 +00:00
],
[
[
2013-04-13 14:40:04 +00:00
'table' => 'module_deps' ,
2016-02-17 09:09:32 +00:00
'uniqueIndexes' => [ [ 'md_module' , 'md_skin' ] ],
'rows' => [
2013-04-13 14:40:04 +00:00
'md_module' => 'module' ,
'md_skin' => 'skin' ,
'md_deps' => 'deps' ,
2016-02-17 09:09:32 +00:00
],
],
2018-02-21 03:16:51 +00:00
" BEGIN; DELETE FROM module_deps " .
2018-01-14 03:57:51 +00:00
" WHERE (md_module = 'module' AND md_skin = 'skin'); " .
2013-04-13 14:40:04 +00:00
" INSERT INTO module_deps " .
2013-04-26 12:00:22 +00:00
" (md_module,md_skin,md_deps) " .
2018-02-21 03:16:51 +00:00
" VALUES ('module','skin','deps'); COMMIT "
2016-02-17 09:09:32 +00:00
],
[
[
2013-04-13 14:40:04 +00:00
'table' => 'module_deps' ,
2016-02-17 09:09:32 +00:00
'uniqueIndexes' => [ [ 'md_module' , 'md_skin' ] ],
'rows' => [
[
2013-04-13 14:40:04 +00:00
'md_module' => 'module' ,
'md_skin' => 'skin' ,
'md_deps' => 'deps' ,
2016-02-17 09:09:32 +00:00
], [
2013-04-13 14:40:04 +00:00
'md_module' => 'module2' ,
'md_skin' => 'skin2' ,
'md_deps' => 'deps2' ,
2016-02-17 09:09:32 +00:00
],
],
],
2018-02-21 03:16:51 +00:00
" BEGIN; DELETE FROM module_deps " .
2018-01-14 03:57:51 +00:00
" WHERE (md_module = 'module' AND md_skin = 'skin'); " .
2013-04-13 14:40:04 +00:00
" INSERT INTO module_deps " .
2013-04-26 12:00:22 +00:00
" (md_module,md_skin,md_deps) " .
" VALUES ('module','skin','deps'); " .
" DELETE FROM module_deps " .
2018-01-14 03:57:51 +00:00
" WHERE (md_module = 'module2' AND md_skin = 'skin2'); " .
2013-04-13 14:40:04 +00:00
" INSERT INTO module_deps " .
2013-04-26 12:00:22 +00:00
" (md_module,md_skin,md_deps) " .
2018-02-21 03:16:51 +00:00
" VALUES ('module2','skin2','deps2'); COMMIT "
2016-02-17 09:09:32 +00:00
],
[
[
2013-04-13 14:40:04 +00:00
'table' => 'module_deps' ,
2016-02-17 09:09:32 +00:00
'uniqueIndexes' => [ 'md_module' , 'md_skin' ],
'rows' => [
[
2013-04-13 14:40:04 +00:00
'md_module' => 'module' ,
'md_skin' => 'skin' ,
'md_deps' => 'deps' ,
2016-02-17 09:09:32 +00:00
], [
2013-04-13 14:40:04 +00:00
'md_module' => 'module2' ,
'md_skin' => 'skin2' ,
'md_deps' => 'deps2' ,
2016-02-17 09:09:32 +00:00
],
],
],
2018-02-21 03:16:51 +00:00
" BEGIN; DELETE FROM module_deps " .
2018-01-14 03:57:51 +00:00
" WHERE (md_module = 'module') OR (md_skin = 'skin'); " .
2013-04-13 14:40:04 +00:00
" INSERT INTO module_deps " .
2013-04-26 12:00:22 +00:00
" (md_module,md_skin,md_deps) " .
" VALUES ('module','skin','deps'); " .
" DELETE FROM module_deps " .
2018-01-14 03:57:51 +00:00
" WHERE (md_module = 'module2') OR (md_skin = 'skin2'); " .
2013-04-13 14:40:04 +00:00
" INSERT INTO module_deps " .
2013-04-26 12:00:22 +00:00
" (md_module,md_skin,md_deps) " .
2018-02-21 03:16:51 +00:00
" VALUES ('module2','skin2','deps2'); COMMIT "
2016-02-17 09:09:32 +00:00
],
[
[
2013-04-13 14:40:04 +00:00
'table' => 'module_deps' ,
2016-02-17 09:09:32 +00:00
'uniqueIndexes' => [],
'rows' => [
2013-04-13 14:40:04 +00:00
'md_module' => 'module' ,
'md_skin' => 'skin' ,
'md_deps' => 'deps' ,
2016-02-17 09:09:32 +00:00
],
],
2018-02-21 03:16:51 +00:00
" BEGIN; INSERT INTO module_deps " .
2013-04-13 14:40:04 +00:00
" (md_module,md_skin,md_deps) " .
2018-02-21 03:16:51 +00:00
" VALUES ('module','skin','deps'); COMMIT "
2016-02-17 09:09:32 +00:00
],
];
2013-04-13 14:40:04 +00:00
}
/**
* @ dataProvider provideNativeReplace
2017-07-20 20:17:11 +00:00
* @ covers Wikimedia\Rdbms\Database :: nativeReplace
2013-04-13 14:40:04 +00:00
*/
2013-10-23 22:51:31 +00:00
public function testNativeReplace ( $sql , $sqlText ) {
2013-04-13 14:40:04 +00:00
$this -> database -> nativeReplace (
$sql [ 'table' ],
$sql [ 'rows' ],
__METHOD__
);
$this -> assertLastSql ( $sqlText );
}
public static function provideNativeReplace () {
2016-02-17 09:09:32 +00:00
return [
[
[
2013-04-13 14:40:04 +00:00
'table' => 'replace_table' ,
2016-02-17 09:09:32 +00:00
'rows' => [ 'field' => 'text' , 'field2' => 'text2' ],
],
2013-04-13 14:40:04 +00:00
" REPLACE INTO replace_table " .
" (field,field2) " .
" VALUES ('text','text2') "
2016-02-17 09:09:32 +00:00
],
];
2012-08-15 13:16:09 +00:00
}
2012-08-25 18:24:59 +00:00
/**
2012-10-08 10:56:20 +00:00
* @ dataProvider provideConditional
2017-07-20 20:17:11 +00:00
* @ covers Wikimedia\Rdbms\Database :: conditional
2012-08-25 18:24:59 +00:00
*/
2013-10-23 22:51:31 +00:00
public function testConditional ( $sql , $sqlText ) {
2013-04-13 14:40:04 +00:00
$this -> assertEquals ( trim ( $this -> database -> conditional (
2012-08-25 18:24:59 +00:00
$sql [ 'conds' ],
$sql [ 'true' ],
$sql [ 'false' ]
) ), $sqlText );
}
2012-10-08 10:56:20 +00:00
public static function provideConditional () {
2016-02-17 09:09:32 +00:00
return [
[
[
'conds' => [ 'field' => 'text' ],
2012-08-25 18:24:59 +00:00
'true' => 1 ,
'false' => 'NULL' ,
2016-02-17 09:09:32 +00:00
],
2012-08-25 18:24:59 +00:00
" (CASE WHEN field = 'text' THEN 1 ELSE NULL END) "
2016-02-17 09:09:32 +00:00
],
[
[
'conds' => [ 'field' => 'text' , 'field2' => 'anothertext' ],
2012-08-31 18:12:19 +00:00
'true' => 1 ,
'false' => 'NULL' ,
2016-02-17 09:09:32 +00:00
],
2012-08-31 18:12:19 +00:00
" (CASE WHEN field = 'text' AND field2 = 'anothertext' THEN 1 ELSE NULL END) "
2016-02-17 09:09:32 +00:00
],
[
[
2012-08-25 18:24:59 +00:00
'conds' => 'field=1' ,
'true' => 1 ,
'false' => 'NULL' ,
2016-02-17 09:09:32 +00:00
],
2012-08-25 18:24:59 +00:00
" (CASE WHEN field=1 THEN 1 ELSE NULL END) "
2016-02-17 09:09:32 +00:00
],
];
2012-08-25 18:24:59 +00:00
}
2013-04-13 14:40:04 +00:00
/**
* @ dataProvider provideBuildConcat
2017-07-20 20:17:11 +00:00
* @ covers Wikimedia\Rdbms\Database :: buildConcat
2013-04-13 14:40:04 +00:00
*/
2013-10-23 22:51:31 +00:00
public function testBuildConcat ( $stringList , $sqlText ) {
2013-04-13 14:40:04 +00:00
$this -> assertEquals ( trim ( $this -> database -> buildConcat (
$stringList
) ), $sqlText );
}
public static function provideBuildConcat () {
2016-02-17 09:09:32 +00:00
return [
[
[ 'field' , 'field2' ],
2013-04-13 14:40:04 +00:00
" CONCAT(field,field2) "
2016-02-17 09:09:32 +00:00
],
[
[ " 'test' " , 'field2' ],
2013-04-13 14:40:04 +00:00
" CONCAT('test',field2) "
2016-02-17 09:09:32 +00:00
],
];
2013-04-13 14:40:04 +00:00
}
/**
* @ dataProvider provideBuildLike
2017-07-20 20:17:11 +00:00
* @ covers Wikimedia\Rdbms\Database :: buildLike
2017-07-25 02:21:12 +00:00
* @ covers Wikimedia\Rdbms\Database :: escapeLikeInternal
2013-04-13 14:40:04 +00:00
*/
2013-10-23 22:51:31 +00:00
public function testBuildLike ( $array , $sqlText ) {
2013-04-13 14:40:04 +00:00
$this -> assertEquals ( trim ( $this -> database -> buildLike (
$array
) ), $sqlText );
}
public static function provideBuildLike () {
2016-02-17 09:09:32 +00:00
return [
[
2013-04-13 14:40:04 +00:00
'text' ,
New maintenance script to clean up rows with invalid DB keys
The TitleValue constructor, used by the link cache among other things,
throws an exception for DB keys which do not satisfy a simple sanity test
(starting or ending with _, or containing a space, tab, CR or LF
character). This has broken certain special pages on a number of WMF sites;
see T99736, T146778 and T155091.
The new cleanupInvalidDbKeys.php script allows these bogus entries to be
removed from the DB, making sure these exceptions won't be thrown in the
future. It cleans the title columns of the page, archive, redirect,
logging, category, protected_titles, recentchanges, watchlist, pagelinks,
templatelinks, and categorylinks tables.
The script doesn't support batching; most wikis should have fewer than 500
broken entries in each table. If need be, the script can be run several
times.
To make the LIKE queries work properly I had to fix the broken escaping
behaviour of Database::buildLike() -- previously it had a habit of double-
escaping things. Now an ESCAPE clause is added to change the escape
character from the problematic default backslash, and tests are added to
cover the changes.
Bug: T155091
Change-Id: I908e795e884e35be91852c0eaf056d6acfda31d8
2017-03-10 13:27:27 +00:00
" LIKE 'text' ESCAPE '`' "
2016-02-17 09:09:32 +00:00
],
[
[ 'text' , new LikeMatch ( '%' ) ],
New maintenance script to clean up rows with invalid DB keys
The TitleValue constructor, used by the link cache among other things,
throws an exception for DB keys which do not satisfy a simple sanity test
(starting or ending with _, or containing a space, tab, CR or LF
character). This has broken certain special pages on a number of WMF sites;
see T99736, T146778 and T155091.
The new cleanupInvalidDbKeys.php script allows these bogus entries to be
removed from the DB, making sure these exceptions won't be thrown in the
future. It cleans the title columns of the page, archive, redirect,
logging, category, protected_titles, recentchanges, watchlist, pagelinks,
templatelinks, and categorylinks tables.
The script doesn't support batching; most wikis should have fewer than 500
broken entries in each table. If need be, the script can be run several
times.
To make the LIKE queries work properly I had to fix the broken escaping
behaviour of Database::buildLike() -- previously it had a habit of double-
escaping things. Now an ESCAPE clause is added to change the escape
character from the problematic default backslash, and tests are added to
cover the changes.
Bug: T155091
Change-Id: I908e795e884e35be91852c0eaf056d6acfda31d8
2017-03-10 13:27:27 +00:00
" LIKE 'text%' ESCAPE '`' "
2016-02-17 09:09:32 +00:00
],
[
[ 'text' , new LikeMatch ( '%' ), 'text2' ],
New maintenance script to clean up rows with invalid DB keys
The TitleValue constructor, used by the link cache among other things,
throws an exception for DB keys which do not satisfy a simple sanity test
(starting or ending with _, or containing a space, tab, CR or LF
character). This has broken certain special pages on a number of WMF sites;
see T99736, T146778 and T155091.
The new cleanupInvalidDbKeys.php script allows these bogus entries to be
removed from the DB, making sure these exceptions won't be thrown in the
future. It cleans the title columns of the page, archive, redirect,
logging, category, protected_titles, recentchanges, watchlist, pagelinks,
templatelinks, and categorylinks tables.
The script doesn't support batching; most wikis should have fewer than 500
broken entries in each table. If need be, the script can be run several
times.
To make the LIKE queries work properly I had to fix the broken escaping
behaviour of Database::buildLike() -- previously it had a habit of double-
escaping things. Now an ESCAPE clause is added to change the escape
character from the problematic default backslash, and tests are added to
cover the changes.
Bug: T155091
Change-Id: I908e795e884e35be91852c0eaf056d6acfda31d8
2017-03-10 13:27:27 +00:00
" LIKE 'text%text2' ESCAPE '`' "
2016-02-17 09:09:32 +00:00
],
[
[ 'text' , new LikeMatch ( '_' ) ],
New maintenance script to clean up rows with invalid DB keys
The TitleValue constructor, used by the link cache among other things,
throws an exception for DB keys which do not satisfy a simple sanity test
(starting or ending with _, or containing a space, tab, CR or LF
character). This has broken certain special pages on a number of WMF sites;
see T99736, T146778 and T155091.
The new cleanupInvalidDbKeys.php script allows these bogus entries to be
removed from the DB, making sure these exceptions won't be thrown in the
future. It cleans the title columns of the page, archive, redirect,
logging, category, protected_titles, recentchanges, watchlist, pagelinks,
templatelinks, and categorylinks tables.
The script doesn't support batching; most wikis should have fewer than 500
broken entries in each table. If need be, the script can be run several
times.
To make the LIKE queries work properly I had to fix the broken escaping
behaviour of Database::buildLike() -- previously it had a habit of double-
escaping things. Now an ESCAPE clause is added to change the escape
character from the problematic default backslash, and tests are added to
cover the changes.
Bug: T155091
Change-Id: I908e795e884e35be91852c0eaf056d6acfda31d8
2017-03-10 13:27:27 +00:00
" LIKE 'text_' ESCAPE '`' "
],
[
'more_text' ,
" LIKE 'more`_text' ESCAPE '`' "
],
[
[ 'C:\\Windows\\' , new LikeMatch ( '%' ) ],
" LIKE 'C: \\ Windows \\ %' ESCAPE '`' "
],
[
[ 'accent`_test`' , new LikeMatch ( '%' ) ],
" LIKE 'accent```_test``%' ESCAPE '`' "
2016-02-17 09:09:32 +00:00
],
];
2013-04-13 14:40:04 +00:00
}
/**
* @ dataProvider provideUnionQueries
2017-07-20 20:17:11 +00:00
* @ covers Wikimedia\Rdbms\Database :: unionQueries
2013-04-13 14:40:04 +00:00
*/
2013-10-23 22:51:31 +00:00
public function testUnionQueries ( $sql , $sqlText ) {
2013-04-13 14:40:04 +00:00
$this -> assertEquals ( trim ( $this -> database -> unionQueries (
$sql [ 'sqls' ],
$sql [ 'all' ]
) ), $sqlText );
}
public static function provideUnionQueries () {
2016-02-17 09:09:32 +00:00
return [
[
[
'sqls' => [ 'RAW SQL' , 'RAW2SQL' ],
2013-04-13 14:40:04 +00:00
'all' => true ,
2016-02-17 09:09:32 +00:00
],
2013-04-13 14:40:04 +00:00
" (RAW SQL) UNION ALL (RAW2SQL) "
2016-02-17 09:09:32 +00:00
],
[
[
'sqls' => [ 'RAW SQL' , 'RAW2SQL' ],
2013-04-13 14:40:04 +00:00
'all' => false ,
2016-02-17 09:09:32 +00:00
],
2013-04-13 14:40:04 +00:00
" (RAW SQL) UNION (RAW2SQL) "
2016-02-17 09:09:32 +00:00
],
[
[
'sqls' => [ 'RAW SQL' , 'RAW2SQL' , 'RAW3SQL' ],
2013-04-13 14:40:04 +00:00
'all' => false ,
2016-02-17 09:09:32 +00:00
],
2013-04-13 14:40:04 +00:00
" (RAW SQL) UNION (RAW2SQL) UNION (RAW3SQL) "
2016-02-17 09:09:32 +00:00
],
];
2013-04-13 14:40:04 +00:00
}
2017-06-16 17:32:03 +00:00
/**
* @ dataProvider provideUnionConditionPermutations
2017-07-20 20:17:11 +00:00
* @ covers Wikimedia\Rdbms\Database :: unionConditionPermutations
2017-06-16 17:32:03 +00:00
*/
public function testUnionConditionPermutations ( $params , $expect ) {
if ( isset ( $params [ 'unionSupportsOrderAndLimit' ] ) ) {
$this -> database -> setUnionSupportsOrderAndLimit ( $params [ 'unionSupportsOrderAndLimit' ] );
}
$sql = trim ( $this -> database -> unionConditionPermutations (
$params [ 'table' ],
$params [ 'vars' ],
$params [ 'permute_conds' ],
isset ( $params [ 'extra_conds' ] ) ? $params [ 'extra_conds' ] : '' ,
'FNAME' ,
isset ( $params [ 'options' ] ) ? $params [ 'options' ] : [],
isset ( $params [ 'join_conds' ] ) ? $params [ 'join_conds' ] : []
) );
$this -> assertEquals ( $expect , $sql );
}
public static function provideUnionConditionPermutations () {
2018-01-01 13:10:16 +00:00
// phpcs:disable Generic.Files.LineLength
2017-06-16 17:32:03 +00:00
return [
[
[
'table' => [ 'table1' , 'table2' ],
'vars' => [ 'field1' , 'alias' => 'field2' ],
'permute_conds' => [
'field3' => [ 1 , 2 , 3 ],
'duplicates' => [ 4 , 5 , 4 ],
'empty' => [],
'single' => [ 0 ],
],
'extra_conds' => 'table2.bar > 23' ,
'options' => [
'ORDER BY' => [ 'field1' , 'alias' ],
'INNER ORDER BY' => [ 'field1' , 'field2' ],
'LIMIT' => 100 ,
],
'join_conds' => [
'table2' => [ 'JOIN' , 'table1.foo_id = table2.foo_id' ],
],
],
" (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 " .
" (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 " .
" (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 " .
" (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 " .
" (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 " .
" (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 ) " .
" ORDER BY field1,alias LIMIT 100 "
],
[
[
'table' => 'foo' ,
'vars' => [ 'foo_id' ],
'permute_conds' => [
'bar' => [ 1 , 2 , 3 ],
],
'extra_conds' => [ 'baz' => null ],
'options' => [
'NOTALL' ,
'ORDER BY' => [ 'foo_id' ],
'LIMIT' => 25 ,
],
],
" (SELECT foo_id FROM foo WHERE bar = '1' AND baz IS NULL ORDER BY foo_id LIMIT 25 ) UNION " .
" (SELECT foo_id FROM foo WHERE bar = '2' AND baz IS NULL ORDER BY foo_id LIMIT 25 ) UNION " .
" (SELECT foo_id FROM foo WHERE bar = '3' AND baz IS NULL ORDER BY foo_id LIMIT 25 ) " .
" ORDER BY foo_id LIMIT 25 "
],
[
[
'table' => 'foo' ,
'vars' => [ 'foo_id' ],
'permute_conds' => [
'bar' => [ 1 , 2 , 3 ],
],
'extra_conds' => [ 'baz' => null ],
'options' => [
'NOTALL' => true ,
'ORDER BY' => [ 'foo_id' ],
'LIMIT' => 25 ,
],
'unionSupportsOrderAndLimit' => false ,
],
" (SELECT foo_id FROM foo WHERE bar = '1' AND baz IS NULL ) UNION " .
" (SELECT foo_id FROM foo WHERE bar = '2' AND baz IS NULL ) UNION " .
" (SELECT foo_id FROM foo WHERE bar = '3' AND baz IS NULL ) " .
" ORDER BY foo_id LIMIT 25 "
],
[
[
'table' => 'foo' ,
'vars' => [ 'foo_id' ],
'permute_conds' => [],
'extra_conds' => [ 'baz' => null ],
'options' => [
'ORDER BY' => [ 'foo_id' ],
'LIMIT' => 25 ,
],
],
" SELECT foo_id FROM foo WHERE baz IS NULL ORDER BY foo_id LIMIT 25 "
],
[
[
'table' => 'foo' ,
'vars' => [ 'foo_id' ],
'permute_conds' => [
'bar' => [],
],
'extra_conds' => [ 'baz' => null ],
'options' => [
'ORDER BY' => [ 'foo_id' ],
'LIMIT' => 25 ,
],
],
" SELECT foo_id FROM foo WHERE baz IS NULL ORDER BY foo_id LIMIT 25 "
],
[
[
'table' => 'foo' ,
'vars' => [ 'foo_id' ],
'permute_conds' => [
'bar' => [ 1 ],
],
'options' => [
'ORDER BY' => [ 'foo_id' ],
'LIMIT' => 25 ,
'OFFSET' => 150 ,
],
],
" SELECT foo_id FROM foo WHERE bar = '1' ORDER BY foo_id LIMIT 150,25 "
],
[
[
'table' => 'foo' ,
'vars' => [ 'foo_id' ],
'permute_conds' => [],
'extra_conds' => [ 'baz' => null ],
'options' => [
'ORDER BY' => [ 'foo_id' ],
'LIMIT' => 25 ,
'OFFSET' => 150 ,
'INNER ORDER BY' => [ 'bar_id' ],
],
],
" (SELECT foo_id FROM foo WHERE baz IS NULL ORDER BY bar_id LIMIT 175 ) ORDER BY foo_id LIMIT 150,25 "
],
[
[
'table' => 'foo' ,
'vars' => [ 'foo_id' ],
'permute_conds' => [],
'extra_conds' => [ 'baz' => null ],
'options' => [
'ORDER BY' => [ 'foo_id' ],
'LIMIT' => 25 ,
'OFFSET' => 150 ,
'INNER ORDER BY' => [ 'bar_id' ],
],
'unionSupportsOrderAndLimit' => false ,
],
" SELECT foo_id FROM foo WHERE baz IS NULL ORDER BY foo_id LIMIT 150,25 "
],
];
2018-01-01 13:10:16 +00:00
// phpcs:enable
2017-06-16 17:32:03 +00:00
}
2013-10-18 10:36:09 +00:00
/**
2017-07-20 20:17:11 +00:00
* @ covers Wikimedia\Rdbms\Database :: commit
2017-07-25 02:21:12 +00:00
* @ covers Wikimedia\Rdbms\Database :: doCommit
2013-10-18 10:36:09 +00:00
*/
2013-10-23 22:51:31 +00:00
public function testTransactionCommit () {
2013-04-13 14:40:04 +00:00
$this -> database -> begin ( __METHOD__ );
$this -> database -> commit ( __METHOD__ );
$this -> assertLastSql ( 'BEGIN; COMMIT' );
}
2013-10-18 10:36:09 +00:00
/**
2017-07-20 20:17:11 +00:00
* @ covers Wikimedia\Rdbms\Database :: rollback
2017-07-25 02:21:12 +00:00
* @ covers Wikimedia\Rdbms\Database :: doRollback
2013-10-18 10:36:09 +00:00
*/
2013-10-23 22:51:31 +00:00
public function testTransactionRollback () {
2013-04-13 14:40:04 +00:00
$this -> database -> begin ( __METHOD__ );
$this -> database -> rollback ( __METHOD__ );
$this -> assertLastSql ( 'BEGIN; ROLLBACK' );
}
2013-10-18 10:36:09 +00:00
/**
2017-07-20 20:17:11 +00:00
* @ covers Wikimedia\Rdbms\Database :: dropTable
2013-10-18 10:36:09 +00:00
*/
2013-10-23 22:51:31 +00:00
public function testDropTable () {
2016-02-17 09:09:32 +00:00
$this -> database -> setExistingTables ( [ 'table' ] );
2013-04-13 14:40:04 +00:00
$this -> database -> dropTable ( 'table' , __METHOD__ );
2016-09-16 03:14:58 +00:00
$this -> assertLastSql ( 'DROP TABLE table CASCADE' );
2013-04-13 14:40:04 +00:00
}
2013-10-18 10:36:09 +00:00
/**
2017-07-20 20:17:11 +00:00
* @ covers Wikimedia\Rdbms\Database :: dropTable
2013-10-18 10:36:09 +00:00
*/
2013-10-23 22:51:31 +00:00
public function testDropNonExistingTable () {
2013-04-13 14:40:04 +00:00
$this -> assertFalse (
$this -> database -> dropTable ( 'non_existing' , __METHOD__ )
);
}
2014-12-29 20:00:02 +00:00
/**
* @ dataProvider provideMakeList
2017-07-20 20:17:11 +00:00
* @ covers Wikimedia\Rdbms\Database :: makeList
2014-12-29 20:00:02 +00:00
*/
public function testMakeList ( $list , $mode , $sqlText ) {
$this -> assertEquals ( trim ( $this -> database -> makeList (
$list , $mode
) ), $sqlText );
}
public static function provideMakeList () {
2016-02-17 09:09:32 +00:00
return [
[
[ 'value' , 'value2' ],
2014-12-29 20:00:02 +00:00
LIST_COMMA ,
" 'value','value2' "
2016-02-17 09:09:32 +00:00
],
[
[ 'field' , 'field2' ],
2014-12-29 20:00:02 +00:00
LIST_NAMES ,
" field,field2 "
2016-02-17 09:09:32 +00:00
],
[
[ 'field' => 'value' , 'field2' => 'value2' ],
2014-12-29 20:00:02 +00:00
LIST_AND ,
" field = 'value' AND field2 = 'value2' "
2016-02-17 09:09:32 +00:00
],
[
[ 'field' => null , " field2 != 'value2' " ],
2014-12-29 20:00:02 +00:00
LIST_AND ,
" field IS NULL AND (field2 != 'value2') "
2016-02-17 09:09:32 +00:00
],
[
[ 'field' => [ 'value' , null , 'value2' ], 'field2' => 'value2' ],
2014-12-23 21:34:36 +00:00
LIST_AND ,
" (field IN ('value','value2') OR field IS NULL) AND field2 = 'value2' "
2016-02-17 09:09:32 +00:00
],
[
[ 'field' => [ null ], 'field2' => null ],
2014-12-23 21:34:36 +00:00
LIST_AND ,
" field IS NULL AND field2 IS NULL "
2016-02-17 09:09:32 +00:00
],
[
[ 'field' => 'value' , 'field2' => 'value2' ],
2014-12-29 20:00:02 +00:00
LIST_OR ,
" field = 'value' OR field2 = 'value2' "
2016-02-17 09:09:32 +00:00
],
[
[ 'field' => 'value' , 'field2' => null ],
2014-12-29 20:00:02 +00:00
LIST_OR ,
" field = 'value' OR field2 IS NULL "
2016-02-17 09:09:32 +00:00
],
[
[ 'field' => [ 'value' , 'value2' ], 'field2' => [ 'value' ] ],
2014-12-29 20:00:02 +00:00
LIST_OR ,
" field IN ('value','value2') OR field2 = 'value' "
2016-02-17 09:09:32 +00:00
],
[
[ 'field' => [ null , 'value' , null , 'value2' ], " field2 != 'value2' " ],
2014-12-23 21:34:36 +00:00
LIST_OR ,
" (field IN ('value','value2') OR field IS NULL) OR (field2 != 'value2') "
2016-02-17 09:09:32 +00:00
],
[
[ 'field' => 'value' , 'field2' => 'value2' ],
2014-12-29 20:00:02 +00:00
LIST_SET ,
" field = 'value',field2 = 'value2' "
2016-02-17 09:09:32 +00:00
],
[
[ 'field' => 'value' , 'field2' => null ],
2014-12-29 20:00:02 +00:00
LIST_SET ,
" field = 'value',field2 = NULL "
2016-02-17 09:09:32 +00:00
],
[
[ 'field' => 'value' , " field2 != 'value2' " ],
2014-12-29 20:00:02 +00:00
LIST_SET ,
" field = 'value',field2 != 'value2' "
2016-02-17 09:09:32 +00:00
],
];
2014-12-29 20:00:02 +00:00
}
2016-09-21 17:44:15 +00:00
2017-07-25 02:21:12 +00:00
/**
* @ covers Wikimedia\Rdbms\Database :: registerTempTableOperation
*/
2016-09-21 17:44:15 +00:00
public function testSessionTempTables () {
$temp1 = $this -> database -> tableName ( 'tmp_table_1' );
$temp2 = $this -> database -> tableName ( 'tmp_table_2' );
$temp3 = $this -> database -> tableName ( 'tmp_table_3' );
$this -> database -> query ( " CREATE TEMPORARY TABLE $temp1 LIKE orig_tbl " , __METHOD__ );
$this -> database -> query ( " CREATE TEMPORARY TABLE $temp2 LIKE orig_tbl " , __METHOD__ );
$this -> database -> query ( " CREATE TEMPORARY TABLE $temp3 LIKE orig_tbl " , __METHOD__ );
$this -> assertTrue ( $this -> database -> tableExists ( " tmp_table_1 " , __METHOD__ ) );
$this -> assertTrue ( $this -> database -> tableExists ( " tmp_table_2 " , __METHOD__ ) );
$this -> assertTrue ( $this -> database -> tableExists ( " tmp_table_3 " , __METHOD__ ) );
$this -> database -> dropTable ( 'tmp_table_1' , __METHOD__ );
$this -> database -> dropTable ( 'tmp_table_2' , __METHOD__ );
$this -> database -> dropTable ( 'tmp_table_3' , __METHOD__ );
$this -> assertFalse ( $this -> database -> tableExists ( " tmp_table_1 " , __METHOD__ ) );
$this -> assertFalse ( $this -> database -> tableExists ( " tmp_table_2 " , __METHOD__ ) );
$this -> assertFalse ( $this -> database -> tableExists ( " tmp_table_3 " , __METHOD__ ) );
$this -> database -> query ( " CREATE TEMPORARY TABLE tmp_table_1 LIKE orig_tbl " , __METHOD__ );
$this -> database -> query ( " CREATE TEMPORARY TABLE 'tmp_table_2' LIKE orig_tbl " , __METHOD__ );
$this -> database -> query ( " CREATE TEMPORARY TABLE `tmp_table_3` LIKE orig_tbl " , __METHOD__ );
$this -> assertTrue ( $this -> database -> tableExists ( " tmp_table_1 " , __METHOD__ ) );
$this -> assertTrue ( $this -> database -> tableExists ( " tmp_table_2 " , __METHOD__ ) );
$this -> assertTrue ( $this -> database -> tableExists ( " tmp_table_3 " , __METHOD__ ) );
$this -> database -> query ( " DROP TEMPORARY TABLE tmp_table_1 LIKE orig_tbl " , __METHOD__ );
$this -> database -> query ( " DROP TEMPORARY TABLE 'tmp_table_2' LIKE orig_tbl " , __METHOD__ );
$this -> database -> query ( " DROP TABLE `tmp_table_3` LIKE orig_tbl " , __METHOD__ );
$this -> assertFalse ( $this -> database -> tableExists ( " tmp_table_1 " , __METHOD__ ) );
$this -> assertFalse ( $this -> database -> tableExists ( " tmp_table_2 " , __METHOD__ ) );
$this -> assertFalse ( $this -> database -> tableExists ( " tmp_table_3 " , __METHOD__ ) );
}
2013-01-28 10:27:15 +00:00
}