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