MediaWiki  REL1_20
DatabaseSQLTest.php
Go to the documentation of this file.
00001 <?php
00002 
00009 class DatabaseSQLTest extends MediaWikiTestCase {
00010 
00011         public function setUp() {
00012                 // TODO support other DBMS or find another way to do it
00013                 if( $this->db->getType() !== 'mysql' ) {
00014                         $this->markTestSkipped( 'No mysql database' );
00015                 }
00016         }
00017 
00021         function testSelectSQLText( $sql, $sqlText ) {
00022                 $this->assertEquals( trim( $this->db->selectSQLText(
00023                         isset( $sql['tables'] ) ? $sql['tables'] : array(),
00024                         isset( $sql['fields'] ) ? $sql['fields'] : array(),
00025                         isset( $sql['conds'] ) ? $sql['conds'] : array(),
00026                         __METHOD__,
00027                         isset( $sql['options'] ) ? $sql['options'] : array(),
00028                         isset( $sql['join_conds'] ) ? $sql['join_conds'] : array()
00029                 ) ), $sqlText );
00030         }
00031 
00032         function dataSelectSQLText() {
00033                 return array(
00034                         array(
00035                                 array(
00036                                         'tables' => 'table',
00037                                         'fields' => array( 'field', 'alias' => 'field2' ),
00038                                         'conds' => array( 'alias' => 'text' ),
00039                                 ),
00040                                 "SELECT  field,field2 AS alias  " .
00041                                 "FROM `unittest_table`  " .
00042                                 "WHERE alias = 'text'"
00043                         ),
00044                         array(
00045                                 array(
00046                                         'tables' => 'table',
00047                                         'fields' => array( 'field', 'alias' => 'field2' ),
00048                                         'conds' => array( 'alias' => 'text' ),
00049                                         'options' => array( 'LIMIT' => 1, 'ORDER BY' => 'field' ),
00050                                 ),
00051                                 "SELECT  field,field2 AS alias  " .
00052                                 "FROM `unittest_table`  " .
00053                                 "WHERE alias = 'text'  " .
00054                                 "ORDER BY field " .
00055                                 "LIMIT 1"
00056                         ),
00057                         array(
00058                                 array(
00059                                         'tables' => array( 'table', 't2' => 'table2' ),
00060                                         'fields' => array( 'tid', 'field', 'alias' => 'field2', 't2.id' ),
00061                                         'conds' => array( 'alias' => 'text' ),
00062                                         'options' => array( 'LIMIT' => 1, 'ORDER BY' => 'field' ),
00063                                         'join_conds' => array( 't2' => array(
00064                                                 'LEFT JOIN', 'tid = t2.id'
00065                                         )),
00066                                 ),
00067                                 "SELECT  tid,field,field2 AS alias,t2.id  " .
00068                                 "FROM `unittest_table` LEFT JOIN `unittest_table2` `t2` ON ((tid = t2.id))  " .
00069                                 "WHERE alias = 'text'  " .
00070                                 "ORDER BY field " .
00071                                 "LIMIT 1"
00072                         ),
00073                         array(
00074                                 array(
00075                                         'tables' => array( 'table', 't2' => 'table2' ),
00076                                         'fields' => array( 'tid', 'field', 'alias' => 'field2', 't2.id' ),
00077                                         'conds' => array( 'alias' => 'text' ),
00078                                         'options' => array( 'LIMIT' => 1, 'GROUP BY' => 'field', 'HAVING' => 'COUNT(*) > 1' ),
00079                                         'join_conds' => array( 't2' => array(
00080                                                 'LEFT JOIN', 'tid = t2.id'
00081                                         )),
00082                                 ),
00083                                 "SELECT  tid,field,field2 AS alias,t2.id  " .
00084                                 "FROM `unittest_table` LEFT JOIN `unittest_table2` `t2` ON ((tid = t2.id))  " .
00085                                 "WHERE alias = 'text'  " .
00086                                 "GROUP BY field HAVING COUNT(*) > 1 " .
00087                                 "LIMIT 1"
00088                         ),
00089                         array(
00090                                 array(
00091                                         'tables' => array( 'table', 't2' => 'table2' ),
00092                                         'fields' => array( 'tid', 'field', 'alias' => 'field2', 't2.id' ),
00093                                         'conds' => array( 'alias' => 'text' ),
00094                                         'options' => array( 'LIMIT' => 1, 'GROUP BY' => array( 'field', 'field2' ), 'HAVING' => array( 'COUNT(*) > 1', 'field' => 1 ) ),
00095                                         'join_conds' => array( 't2' => array(
00096                                                 'LEFT JOIN', 'tid = t2.id'
00097                                         )),
00098                                 ),
00099                                 "SELECT  tid,field,field2 AS alias,t2.id  " .
00100                                 "FROM `unittest_table` LEFT JOIN `unittest_table2` `t2` ON ((tid = t2.id))  " .
00101                                 "WHERE alias = 'text'  " .
00102                                 "GROUP BY field,field2 HAVING (COUNT(*) > 1) AND field = '1' " .
00103                                 "LIMIT 1"
00104                         ),
00105                 );
00106         }
00107 
00111         function testConditional( $sql, $sqlText ) {
00112                 $this->assertEquals( trim( $this->db->conditional(
00113                         $sql['conds'],
00114                         $sql['true'],
00115                         $sql['false']
00116                 ) ), $sqlText );
00117         }
00118 
00119         function dataConditional() {
00120                 return array(
00121                         array(
00122                                 array(
00123                                         'conds' => array( 'field' => 'text' ),
00124                                         'true' => 1,
00125                                         'false' => 'NULL',
00126                                 ),
00127                                 "(CASE WHEN field = 'text' THEN 1 ELSE NULL END)"
00128                         ),
00129                         array(
00130                                 array(
00131                                         'conds' => array( 'field' => 'text', 'field2' => 'anothertext' ),
00132                                         'true' => 1,
00133                                         'false' => 'NULL',
00134                                 ),
00135                                 "(CASE WHEN field = 'text' AND field2 = 'anothertext' THEN 1 ELSE NULL END)"
00136                         ),
00137                         array(
00138                                 array(
00139                                         'conds' => 'field=1',
00140                                         'true' => 1,
00141                                         'false' => 'NULL',
00142                                 ),
00143                                 "(CASE WHEN field=1 THEN 1 ELSE NULL END)"
00144                         ),
00145                 );
00146         }
00147 }