2012-08-15 13:16:09 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test the abstract database layer
|
2013-04-13 14:40:04 +00:00
|
|
|
* This is a non DBMS depending test.
|
2012-08-15 13:16:09 +00:00
|
|
|
*/
|
|
|
|
|
class DatabaseSQLTest extends MediaWikiTestCase {
|
|
|
|
|
|
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();
|
2013-04-13 14:40:04 +00:00
|
|
|
$this->database = new DatabaseTestHelper( __CLASS__ );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function assertLastSql( $sqlText ) {
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
$this->database->getLastSqls(),
|
|
|
|
|
$sqlText
|
|
|
|
|
);
|
2012-08-15 13:16:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2013-04-13 14:40:04 +00:00
|
|
|
* @dataProvider provideSelect
|
2012-08-15 13:16:09 +00:00
|
|
|
*/
|
2013-04-13 14:40:04 +00:00
|
|
|
function testSelect( $sql, $sqlText ) {
|
|
|
|
|
$this->database->select(
|
|
|
|
|
$sql['tables'],
|
|
|
|
|
$sql['fields'],
|
2012-08-15 13:16:09 +00:00
|
|
|
isset( $sql['conds'] ) ? $sql['conds'] : array(),
|
|
|
|
|
__METHOD__,
|
|
|
|
|
isset( $sql['options'] ) ? $sql['options'] : array(),
|
|
|
|
|
isset( $sql['join_conds'] ) ? $sql['join_conds'] : array()
|
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() {
|
2012-08-15 13:16:09 +00:00
|
|
|
return array(
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'tables' => 'table',
|
|
|
|
|
'fields' => array( 'field', 'alias' => 'field2' ),
|
|
|
|
|
'conds' => array( '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'"
|
2012-08-15 13:16:09 +00:00
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'tables' => 'table',
|
|
|
|
|
'fields' => array( 'field', 'alias' => 'field2' ),
|
|
|
|
|
'conds' => array( 'alias' => 'text' ),
|
|
|
|
|
'options' => array( '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"
|
2012-08-15 13:16:09 +00:00
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'tables' => array( 'table', 't2' => 'table2' ),
|
|
|
|
|
'fields' => array( 'tid', 'field', 'alias' => 'field2', 't2.id' ),
|
|
|
|
|
'conds' => array( 'alias' => 'text' ),
|
|
|
|
|
'options' => array( 'LIMIT' => 1, 'ORDER BY' => 'field' ),
|
|
|
|
|
'join_conds' => array( 't2' => array(
|
|
|
|
|
'LEFT JOIN', 'tid = t2.id'
|
2013-02-14 13:10:38 +00:00
|
|
|
) ),
|
2012-08-15 13:16:09 +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"
|
2012-08-15 13:16:09 +00:00
|
|
|
),
|
2012-08-31 18:12:19 +00:00
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'tables' => array( 'table', 't2' => 'table2' ),
|
|
|
|
|
'fields' => array( 'tid', 'field', 'alias' => 'field2', 't2.id' ),
|
|
|
|
|
'conds' => array( 'alias' => 'text' ),
|
|
|
|
|
'options' => array( 'LIMIT' => 1, 'GROUP BY' => 'field', 'HAVING' => 'COUNT(*) > 1' ),
|
|
|
|
|
'join_conds' => array( 't2' => array(
|
|
|
|
|
'LEFT JOIN', 'tid = t2.id'
|
2013-02-14 13:10:38 +00:00
|
|
|
) ),
|
2012-08-31 18:12:19 +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"
|
2012-08-31 18:12:19 +00:00
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'tables' => array( 'table', 't2' => 'table2' ),
|
|
|
|
|
'fields' => array( 'tid', 'field', 'alias' => 'field2', 't2.id' ),
|
|
|
|
|
'conds' => array( 'alias' => 'text' ),
|
|
|
|
|
'options' => array( 'LIMIT' => 1, 'GROUP BY' => array( 'field', 'field2' ), 'HAVING' => array( 'COUNT(*) > 1', 'field' => 1 ) ),
|
|
|
|
|
'join_conds' => array( 't2' => array(
|
|
|
|
|
'LEFT JOIN', 'tid = t2.id'
|
2013-02-14 13:10:38 +00:00
|
|
|
) ),
|
2012-08-31 18:12:19 +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"
|
2012-08-31 18:12:19 +00:00
|
|
|
),
|
2013-04-13 14:40:04 +00:00
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'tables' => array( 'table' ),
|
|
|
|
|
'fields' => array( 'alias' => 'field' ),
|
|
|
|
|
'conds' => array( 'alias' => array( 1, 2, 3, 4 ) ),
|
|
|
|
|
),
|
|
|
|
|
"SELECT field AS alias " .
|
|
|
|
|
"FROM table " .
|
|
|
|
|
"WHERE alias IN ('1','2','3','4')"
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideUpdate
|
|
|
|
|
*/
|
|
|
|
|
function testUpdate( $sql, $sqlText ) {
|
|
|
|
|
$this->database->update(
|
|
|
|
|
$sql['table'],
|
|
|
|
|
$sql['values'],
|
|
|
|
|
$sql['conds'],
|
|
|
|
|
__METHOD__,
|
|
|
|
|
isset( $sql['options'] ) ? $sql['options'] : array()
|
|
|
|
|
);
|
|
|
|
|
$this->assertLastSql( $sqlText );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideUpdate() {
|
|
|
|
|
return array(
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'table' => 'table',
|
|
|
|
|
'values' => array( 'field' => 'text', 'field2' => 'text2' ),
|
|
|
|
|
'conds' => array( 'alias' => 'text' ),
|
|
|
|
|
),
|
|
|
|
|
"UPDATE table " .
|
|
|
|
|
"SET field = 'text'" .
|
|
|
|
|
",field2 = 'text2' " .
|
|
|
|
|
"WHERE alias = 'text'"
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'table' => 'table',
|
|
|
|
|
'values' => array( 'field = other', 'field2' => 'text2' ),
|
|
|
|
|
'conds' => array( 'id' => '1' ),
|
|
|
|
|
),
|
|
|
|
|
"UPDATE table " .
|
|
|
|
|
"SET field = other" .
|
|
|
|
|
",field2 = 'text2' " .
|
|
|
|
|
"WHERE id = '1'"
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'table' => 'table',
|
|
|
|
|
'values' => array( 'field = other', 'field2' => 'text2' ),
|
|
|
|
|
'conds' => '*',
|
|
|
|
|
),
|
|
|
|
|
"UPDATE table " .
|
|
|
|
|
"SET field = other" .
|
|
|
|
|
",field2 = 'text2'"
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideDelete
|
|
|
|
|
*/
|
|
|
|
|
function testDelete( $sql, $sqlText ) {
|
|
|
|
|
$this->database->delete(
|
|
|
|
|
$sql['table'],
|
|
|
|
|
$sql['conds'],
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
$this->assertLastSql( $sqlText );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideDelete() {
|
|
|
|
|
return array(
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'table' => 'table',
|
|
|
|
|
'conds' => array( 'alias' => 'text' ),
|
|
|
|
|
),
|
|
|
|
|
"DELETE FROM table " .
|
|
|
|
|
"WHERE alias = 'text'"
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'table' => 'table',
|
|
|
|
|
'conds' => '*',
|
|
|
|
|
),
|
|
|
|
|
"DELETE FROM table"
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideDeleteJoin
|
|
|
|
|
*/
|
|
|
|
|
function testDeleteJoin( $sql, $sqlText ) {
|
|
|
|
|
$this->database->deleteJoin(
|
|
|
|
|
$sql['delTable'],
|
|
|
|
|
$sql['joinTable'],
|
|
|
|
|
$sql['delVar'],
|
|
|
|
|
$sql['joinVar'],
|
|
|
|
|
$sql['conds'],
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
$this->assertLastSql( $sqlText );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideDeleteJoin() {
|
|
|
|
|
return array(
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'delTable' => 'table',
|
|
|
|
|
'joinTable' => 'table_join',
|
|
|
|
|
'delVar' => 'field',
|
|
|
|
|
'joinVar' => 'field_join',
|
|
|
|
|
'conds' => array( 'alias' => 'text' ),
|
|
|
|
|
),
|
|
|
|
|
"DELETE FROM table " .
|
|
|
|
|
"WHERE field IN (" .
|
|
|
|
|
"SELECT field_join FROM table_join WHERE alias = 'text'" .
|
|
|
|
|
")"
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'delTable' => 'table',
|
|
|
|
|
'joinTable' => 'table_join',
|
|
|
|
|
'delVar' => 'field',
|
|
|
|
|
'joinVar' => 'field_join',
|
|
|
|
|
'conds' => '*',
|
|
|
|
|
),
|
|
|
|
|
"DELETE FROM table " .
|
|
|
|
|
"WHERE field IN (" .
|
|
|
|
|
"SELECT field_join FROM table_join " .
|
|
|
|
|
")"
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideInsert
|
|
|
|
|
*/
|
|
|
|
|
function testInsert( $sql, $sqlText ) {
|
|
|
|
|
$this->database->insert(
|
|
|
|
|
$sql['table'],
|
|
|
|
|
$sql['rows'],
|
|
|
|
|
__METHOD__,
|
|
|
|
|
isset( $sql['options'] ) ? $sql['options'] : array()
|
|
|
|
|
);
|
|
|
|
|
$this->assertLastSql( $sqlText );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideInsert() {
|
|
|
|
|
return array(
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'table' => 'table',
|
|
|
|
|
'rows' => array( 'field' => 'text', 'field2' => 2 ),
|
|
|
|
|
),
|
|
|
|
|
"INSERT INTO table " .
|
|
|
|
|
"(field,field2) " .
|
|
|
|
|
"VALUES ('text','2')"
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'table' => 'table',
|
|
|
|
|
'rows' => array( 'field' => 'text', 'field2' => 2 ),
|
|
|
|
|
'options' => 'IGNORE',
|
|
|
|
|
),
|
|
|
|
|
"INSERT IGNORE INTO table " .
|
|
|
|
|
"(field,field2) " .
|
|
|
|
|
"VALUES ('text','2')"
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'table' => 'table',
|
|
|
|
|
'rows' => array(
|
|
|
|
|
array( 'field' => 'text', 'field2' => 2 ),
|
|
|
|
|
array( 'field' => 'multi', 'field2' => 3 ),
|
|
|
|
|
),
|
|
|
|
|
'options' => 'IGNORE',
|
|
|
|
|
),
|
|
|
|
|
"INSERT IGNORE INTO table " .
|
|
|
|
|
"(field,field2) " .
|
|
|
|
|
"VALUES " .
|
|
|
|
|
"('text','2')," .
|
|
|
|
|
"('multi','3')"
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideInsertSelect
|
|
|
|
|
*/
|
|
|
|
|
function testInsertSelect( $sql, $sqlText ) {
|
|
|
|
|
$this->database->insertSelect(
|
|
|
|
|
$sql['destTable'],
|
|
|
|
|
$sql['srcTable'],
|
|
|
|
|
$sql['varMap'],
|
|
|
|
|
$sql['conds'],
|
|
|
|
|
__METHOD__,
|
|
|
|
|
isset( $sql['insertOptions'] ) ? $sql['insertOptions'] : array(),
|
|
|
|
|
isset( $sql['selectOptions'] ) ? $sql['selectOptions'] : array()
|
|
|
|
|
);
|
|
|
|
|
$this->assertLastSql( $sqlText );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideInsertSelect() {
|
|
|
|
|
return array(
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'destTable' => 'insert_table',
|
|
|
|
|
'srcTable' => 'select_table',
|
|
|
|
|
'varMap' => array( 'field_insert' => 'field_select', 'field' => 'field2' ),
|
|
|
|
|
'conds' => '*',
|
|
|
|
|
),
|
|
|
|
|
"INSERT INTO insert_table " .
|
|
|
|
|
"(field_insert,field) " .
|
|
|
|
|
"SELECT field_select,field2 " .
|
|
|
|
|
"FROM select_table"
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'destTable' => 'insert_table',
|
|
|
|
|
'srcTable' => 'select_table',
|
|
|
|
|
'varMap' => array( 'field_insert' => 'field_select', 'field' => 'field2' ),
|
|
|
|
|
'conds' => array( 'field' => 2 ),
|
|
|
|
|
),
|
|
|
|
|
"INSERT INTO insert_table " .
|
|
|
|
|
"(field_insert,field) " .
|
|
|
|
|
"SELECT field_select,field2 " .
|
|
|
|
|
"FROM select_table " .
|
|
|
|
|
"WHERE field = '2'"
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'destTable' => 'insert_table',
|
|
|
|
|
'srcTable' => 'select_table',
|
|
|
|
|
'varMap' => array( 'field_insert' => 'field_select', 'field' => 'field2' ),
|
|
|
|
|
'conds' => array( 'field' => 2 ),
|
|
|
|
|
'insertOptions' => 'IGNORE',
|
|
|
|
|
'selectOptions' => array( 'ORDER BY' => 'field' ),
|
|
|
|
|
),
|
|
|
|
|
"INSERT IGNORE INTO insert_table " .
|
|
|
|
|
"(field_insert,field) " .
|
|
|
|
|
"SELECT field_select,field2 " .
|
|
|
|
|
"FROM select_table " .
|
|
|
|
|
"WHERE field = '2' " .
|
|
|
|
|
"ORDER BY field"
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideReplace
|
|
|
|
|
*/
|
|
|
|
|
function testReplace( $sql, $sqlText ) {
|
|
|
|
|
$this->database->replace(
|
|
|
|
|
$sql['table'],
|
|
|
|
|
$sql['uniqueIndexes'],
|
|
|
|
|
$sql['rows'],
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
$this->assertLastSql( $sqlText );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideReplace() {
|
|
|
|
|
return array(
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'table' => 'replace_table',
|
|
|
|
|
'uniqueIndexes' => array( 'field' ),
|
|
|
|
|
'rows' => array( 'field' => 'text', 'field2' => 'text2' ),
|
|
|
|
|
),
|
|
|
|
|
"DELETE FROM replace_table " .
|
|
|
|
|
"WHERE ( field='text' ); " .
|
|
|
|
|
"INSERT INTO replace_table " .
|
|
|
|
|
"(field,field2) " .
|
|
|
|
|
"VALUES ('text','text2')"
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'table' => 'module_deps',
|
|
|
|
|
'uniqueIndexes' => array( array( 'md_module', 'md_skin' ) ),
|
|
|
|
|
'rows' => array(
|
|
|
|
|
'md_module' => 'module',
|
|
|
|
|
'md_skin' => 'skin',
|
|
|
|
|
'md_deps' => 'deps',
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
"DELETE FROM module_deps " .
|
|
|
|
|
"WHERE ( md_module='module' AND md_skin='skin' ); " .
|
|
|
|
|
"INSERT INTO module_deps " .
|
|
|
|
|
"(md_module,md_skin,md_deps) " .
|
|
|
|
|
"VALUES ('module','skin','deps')"
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'table' => 'module_deps',
|
|
|
|
|
'uniqueIndexes' => array( array( 'md_module', 'md_skin' ) ),
|
|
|
|
|
'rows' => array(
|
|
|
|
|
array(
|
|
|
|
|
'md_module' => 'module',
|
|
|
|
|
'md_skin' => 'skin',
|
|
|
|
|
'md_deps' => 'deps',
|
|
|
|
|
), array(
|
|
|
|
|
'md_module' => 'module2',
|
|
|
|
|
'md_skin' => 'skin2',
|
|
|
|
|
'md_deps' => 'deps2',
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
"DELETE FROM module_deps " .
|
|
|
|
|
"WHERE ( md_module='module' AND md_skin='skin' ); " .
|
|
|
|
|
"INSERT INTO module_deps " .
|
|
|
|
|
"(md_module,md_skin,md_deps) " .
|
|
|
|
|
"VALUES ('module','skin','deps'); " .
|
|
|
|
|
"DELETE FROM module_deps " .
|
|
|
|
|
"WHERE ( md_module='module2' AND md_skin='skin2' ); " .
|
|
|
|
|
"INSERT INTO module_deps " .
|
|
|
|
|
"(md_module,md_skin,md_deps) " .
|
|
|
|
|
"VALUES ('module2','skin2','deps2')"
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'table' => 'module_deps',
|
|
|
|
|
'uniqueIndexes' => array( 'md_module', 'md_skin' ),
|
|
|
|
|
'rows' => array(
|
|
|
|
|
array(
|
|
|
|
|
'md_module' => 'module',
|
|
|
|
|
'md_skin' => 'skin',
|
|
|
|
|
'md_deps' => 'deps',
|
|
|
|
|
), array(
|
|
|
|
|
'md_module' => 'module2',
|
|
|
|
|
'md_skin' => 'skin2',
|
|
|
|
|
'md_deps' => 'deps2',
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
"DELETE FROM module_deps " .
|
|
|
|
|
"WHERE ( md_module='module' ) OR ( md_skin='skin' ); " .
|
|
|
|
|
"INSERT INTO module_deps " .
|
|
|
|
|
"(md_module,md_skin,md_deps) " .
|
|
|
|
|
"VALUES ('module','skin','deps'); " .
|
|
|
|
|
"DELETE FROM module_deps " .
|
|
|
|
|
"WHERE ( md_module='module2' ) OR ( md_skin='skin2' ); " .
|
|
|
|
|
"INSERT INTO module_deps " .
|
|
|
|
|
"(md_module,md_skin,md_deps) " .
|
|
|
|
|
"VALUES ('module2','skin2','deps2')"
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'table' => 'module_deps',
|
|
|
|
|
'uniqueIndexes' => array(),
|
|
|
|
|
'rows' => array(
|
|
|
|
|
'md_module' => 'module',
|
|
|
|
|
'md_skin' => 'skin',
|
|
|
|
|
'md_deps' => 'deps',
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
"INSERT INTO module_deps " .
|
|
|
|
|
"(md_module,md_skin,md_deps) " .
|
|
|
|
|
"VALUES ('module','skin','deps')"
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideNativeReplace
|
|
|
|
|
*/
|
|
|
|
|
function testNativeReplace( $sql, $sqlText ) {
|
|
|
|
|
$this->database->nativeReplace(
|
|
|
|
|
$sql['table'],
|
|
|
|
|
$sql['rows'],
|
|
|
|
|
__METHOD__
|
|
|
|
|
);
|
|
|
|
|
$this->assertLastSql( $sqlText );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideNativeReplace() {
|
|
|
|
|
return array(
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'table' => 'replace_table',
|
|
|
|
|
'rows' => array( 'field' => 'text', 'field2' => 'text2' ),
|
|
|
|
|
),
|
|
|
|
|
"REPLACE INTO replace_table " .
|
|
|
|
|
"(field,field2) " .
|
|
|
|
|
"VALUES ('text','text2')"
|
|
|
|
|
),
|
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
|
2012-08-25 18:24:59 +00:00
|
|
|
*/
|
|
|
|
|
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() {
|
2012-08-25 18:24:59 +00:00
|
|
|
return array(
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'conds' => array( 'field' => 'text' ),
|
|
|
|
|
'true' => 1,
|
|
|
|
|
'false' => 'NULL',
|
|
|
|
|
),
|
|
|
|
|
"(CASE WHEN field = 'text' THEN 1 ELSE NULL END)"
|
|
|
|
|
),
|
2012-08-31 18:12:19 +00:00
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'conds' => array( 'field' => 'text', 'field2' => 'anothertext' ),
|
|
|
|
|
'true' => 1,
|
|
|
|
|
'false' => 'NULL',
|
|
|
|
|
),
|
|
|
|
|
"(CASE WHEN field = 'text' AND field2 = 'anothertext' THEN 1 ELSE NULL END)"
|
|
|
|
|
),
|
2012-08-25 18:24:59 +00:00
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'conds' => 'field=1',
|
|
|
|
|
'true' => 1,
|
|
|
|
|
'false' => 'NULL',
|
|
|
|
|
),
|
|
|
|
|
"(CASE WHEN field=1 THEN 1 ELSE NULL END)"
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
2013-04-13 14:40:04 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideBuildConcat
|
|
|
|
|
*/
|
|
|
|
|
function testBuildConcat( $stringList, $sqlText ) {
|
|
|
|
|
$this->assertEquals( trim( $this->database->buildConcat(
|
|
|
|
|
$stringList
|
|
|
|
|
) ), $sqlText );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideBuildConcat() {
|
|
|
|
|
return array(
|
|
|
|
|
array(
|
|
|
|
|
array( 'field', 'field2' ),
|
|
|
|
|
"CONCAT(field,field2)"
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
array( "'test'", 'field2' ),
|
|
|
|
|
"CONCAT('test',field2)"
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideBuildLike
|
|
|
|
|
*/
|
|
|
|
|
function testBuildLike( $array, $sqlText ) {
|
|
|
|
|
$this->assertEquals( trim( $this->database->buildLike(
|
|
|
|
|
$array
|
|
|
|
|
) ), $sqlText );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideBuildLike() {
|
|
|
|
|
return array(
|
|
|
|
|
array(
|
|
|
|
|
'text',
|
|
|
|
|
"LIKE 'text'"
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
array( 'text', new LikeMatch( '%' ) ),
|
|
|
|
|
"LIKE 'text%'"
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
array( 'text', new LikeMatch( '%' ), 'text2' ),
|
|
|
|
|
"LIKE 'text%text2'"
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
array( 'text', new LikeMatch( '_' ) ),
|
|
|
|
|
"LIKE 'text_'"
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @dataProvider provideUnionQueries
|
|
|
|
|
*/
|
|
|
|
|
function testUnionQueries( $sql, $sqlText ) {
|
|
|
|
|
$this->assertEquals( trim( $this->database->unionQueries(
|
|
|
|
|
$sql['sqls'],
|
|
|
|
|
$sql['all']
|
|
|
|
|
) ), $sqlText );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideUnionQueries() {
|
|
|
|
|
return array(
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'sqls' => array( 'RAW SQL', 'RAW2SQL' ),
|
|
|
|
|
'all' => true,
|
|
|
|
|
),
|
|
|
|
|
"(RAW SQL) UNION ALL (RAW2SQL)"
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'sqls' => array( 'RAW SQL', 'RAW2SQL' ),
|
|
|
|
|
'all' => false,
|
|
|
|
|
),
|
|
|
|
|
"(RAW SQL) UNION (RAW2SQL)"
|
|
|
|
|
),
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'sqls' => array( 'RAW SQL', 'RAW2SQL', 'RAW3SQL' ),
|
|
|
|
|
'all' => false,
|
|
|
|
|
),
|
|
|
|
|
"(RAW SQL) UNION (RAW2SQL) UNION (RAW3SQL)"
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function testTransactionCommit() {
|
|
|
|
|
$this->database->begin( __METHOD__ );
|
|
|
|
|
$this->database->commit( __METHOD__ );
|
|
|
|
|
$this->assertLastSql( 'BEGIN; COMMIT' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function testTransactionRollback() {
|
|
|
|
|
$this->database->begin( __METHOD__ );
|
|
|
|
|
$this->database->rollback( __METHOD__ );
|
|
|
|
|
$this->assertLastSql( 'BEGIN; ROLLBACK' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function testDropTable() {
|
|
|
|
|
$this->database->setExistingTables( array( 'table' ) );
|
|
|
|
|
$this->database->dropTable( 'table', __METHOD__ );
|
|
|
|
|
$this->assertLastSql( 'DROP TABLE table' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function testDropNonExistingTable() {
|
|
|
|
|
$this->assertFalse(
|
|
|
|
|
$this->database->dropTable( 'non_existing', __METHOD__ )
|
|
|
|
|
);
|
|
|
|
|
}
|
2013-01-28 10:27:15 +00:00
|
|
|
}
|