2012-08-15 13:16:09 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test the abstract database layer
|
|
|
|
|
* Using Mysql for the sql at the moment TODO
|
|
|
|
|
*
|
|
|
|
|
* @group Database
|
|
|
|
|
*/
|
|
|
|
|
class DatabaseSQLTest extends MediaWikiTestCase {
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
protected function setUp() {
|
2012-12-06 16:57:37 +00:00
|
|
|
parent::setUp();
|
2012-08-15 13:16:09 +00:00
|
|
|
// TODO support other DBMS or find another way to do it
|
2012-10-08 10:56:20 +00:00
|
|
|
if ( $this->db->getType() !== 'mysql' ) {
|
2012-08-15 13:16:09 +00:00
|
|
|
$this->markTestSkipped( 'No mysql database' );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-10-08 10:56:20 +00:00
|
|
|
* @dataProvider provideSelectSQLText
|
2012-08-15 13:16:09 +00:00
|
|
|
*/
|
2012-08-25 18:24:59 +00:00
|
|
|
function testSelectSQLText( $sql, $sqlText ) {
|
2012-08-15 13:16:09 +00:00
|
|
|
$this->assertEquals( trim( $this->db->selectSQLText(
|
|
|
|
|
isset( $sql['tables'] ) ? $sql['tables'] : array(),
|
|
|
|
|
isset( $sql['fields'] ) ? $sql['fields'] : array(),
|
|
|
|
|
isset( $sql['conds'] ) ? $sql['conds'] : array(),
|
|
|
|
|
__METHOD__,
|
|
|
|
|
isset( $sql['options'] ) ? $sql['options'] : array(),
|
|
|
|
|
isset( $sql['join_conds'] ) ? $sql['join_conds'] : array()
|
|
|
|
|
) ), $sqlText );
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
public static function provideSelectSQLText() {
|
2012-08-15 13:16:09 +00:00
|
|
|
return array(
|
|
|
|
|
array(
|
|
|
|
|
array(
|
|
|
|
|
'tables' => 'table',
|
|
|
|
|
'fields' => array( 'field', 'alias' => 'field2' ),
|
|
|
|
|
'conds' => array( 'alias' => 'text' ),
|
|
|
|
|
),
|
|
|
|
|
"SELECT field,field2 AS alias " .
|
2013-02-14 13:10:38 +00:00
|
|
|
"FROM `unittest_table` " .
|
|
|
|
|
"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' ),
|
|
|
|
|
),
|
|
|
|
|
"SELECT field,field2 AS alias " .
|
2013-02-14 13:10:38 +00:00
|
|
|
"FROM `unittest_table` " .
|
|
|
|
|
"WHERE alias = 'text' " .
|
|
|
|
|
"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
|
|
|
),
|
|
|
|
|
"SELECT tid,field,field2 AS alias,t2.id " .
|
2013-02-14 13:10:38 +00:00
|
|
|
"FROM `unittest_table` LEFT JOIN `unittest_table2` `t2` ON ((tid = t2.id)) " .
|
|
|
|
|
"WHERE alias = 'text' " .
|
|
|
|
|
"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
|
|
|
),
|
|
|
|
|
"SELECT tid,field,field2 AS alias,t2.id " .
|
2013-02-14 13:10:38 +00:00
|
|
|
"FROM `unittest_table` LEFT JOIN `unittest_table2` `t2` ON ((tid = t2.id)) " .
|
|
|
|
|
"WHERE alias = 'text' " .
|
|
|
|
|
"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
|
|
|
),
|
|
|
|
|
"SELECT tid,field,field2 AS alias,t2.id " .
|
2013-02-14 13:10:38 +00:00
|
|
|
"FROM `unittest_table` LEFT JOIN `unittest_table2` `t2` ON ((tid = t2.id)) " .
|
|
|
|
|
"WHERE alias = 'text' " .
|
|
|
|
|
"GROUP BY field,field2 HAVING (COUNT(*) > 1) AND field = '1' " .
|
|
|
|
|
"LIMIT 1"
|
2012-08-31 18:12:19 +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
|
2012-08-25 18:24:59 +00:00
|
|
|
*/
|
|
|
|
|
function testConditional( $sql, $sqlText ) {
|
|
|
|
|
$this->assertEquals( trim( $this->db->conditional(
|
|
|
|
|
$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-01-28 10:27:15 +00:00
|
|
|
}
|