21 define(
'SQL_PARSER_WRAPPER_ERROR', 101);
22 require_once
'SQL/Parser.php';
31 $this->_data =& $data;
32 $this->_tableLookup = array();
33 $this->_parser =
new SQL_Parser(null, $dialect);
41 if ( !isset( $this->_tableLookup[$columnname] ) ){
42 if ( strpos($columnname,
'.') ===
false ) $this->_tableLookup[$columnname] = null;
45 list(
$table, $column) = explode(
'.', $columnname);
46 if ( isset( $data[
'table_aliases'] ) ){
47 for ($i=0; $i<count($data[
'table_aliases']); $i++){
48 if ( $data[
'table_aliases'][$i] ==
$table ){
49 $table = $data[
'table_names'][$i];
54 $this->_tableLookup[$columnname] =
$table;
58 return $this->_tableLookup[$columnname];
69 return PEAR::raiseError(
"Table not found in query",
SQL_PARSER_WRAPPER_ERROR, E_USER_WARNING, null,
"The table '$tablename' was requested in SQL_Parser_wrapper::getTableAlias() and not found in the sql query");
72 if ( isset( $this->_data[
'table_aliases']) and isset( $this->_data[
'table_aliases'][
$index]) and
73 $this->_data[
'table_aliases'][$index] ) {
74 return $this->_data[
'table_aliases'][
$index];
93 if ( strpos($columnname,
".") !==
false ){
94 list($junk, $col) = explode(
'.', $columnname);
114 if ( strpos($columnname,
'.') !==
false ){
115 list(
$table,$column) = explode(
'.', $columnname);
122 $index = array_search(
$table, $this->_data[
'table_aliases']);
133 $tablename->addUserInfo(
"In SQL_Parser_wrapper attempted to unresolve column '$columnname' but the table does not exist as either an alias or a column name.");
141 return $this->_data[
'column_names'][
$index];
154 foreach ( array_keys($haystack) as
$index ){
155 if ( preg_match($needle, $haystack[$index]) ){
163 if ( !is_array($clause) )
return;
164 if ( isset($clause[
'type']) and $clause[
'type'] ===
'ident' ){
167 foreach ( array_keys($clause) as $key){
180 $columnNames =& $this->_data[
'column_names'];
181 $index = array_search($columnname, $columnNames);
183 array_splice($columnNames,
$index, 1);
184 if ( isset( $this->_data[
'column_aliases'] ) ){
185 array_splice($this->_data[
'column_aliases'],
$index, 1);
199 $columnNames =& $this->_data[
'column_names'];
201 foreach ( $columnNames as $name ){
204 if ( $res ) $count++;
214 $this->_data[
'column_names'][] = $columnname;
215 $this->_data[
'column_aliases'][] = $columnalias;
224 if ( isset( $data[
'where_clause']) and $data[
'where_clause'] ) {
226 if ( (isset( $data[
'where_clause'][
'type']) and $data[
'where_clause'][
'type'] ==
'subclause')
227 or count($data[
'where_clause']) ===1 or
228 (isset($data[
'where_clause'][
'op']) and !in_array($data[
'where_clause'][
'op'], array(
'and',
'or')) )
230 $arg1 = $data[
'where_clause'];
232 $arg1 = array(
"value"=>$data[
'where_clause'],
"type"=>
"subclause");
235 if ( (isset($clause[
'type']) and $clause[
'type'] ==
'subclause') or
236 count($clause) === 1 or
237 (isset($clause[
'op']) and !in_array($clause[
'op'], array(
'and',
'or')))
241 $arg2 = array(
"value"=>$clause,
"type"=>
"subclause");
244 $data[
'where_clause'] = array(
"arg_1"=> $arg1,
'op'=>$op,
"arg_2"=> $arg2);
246 $data[
'where_clause'] = $clause;
254 $sql =
"SELECT * FROM foo WHERE $whereStr";
255 $parsed = $this->_parser->parse($sql);
268 $sql =
"SELECT * FROM foo ORDER BY $sortStr";
269 $parsed = $this->_parser->parse($sql);
271 $sort_order = array();
272 foreach (array_keys($parsed[
'sort_order']) as $sort_col){
274 $sort_order[] =& $parsed[
'sort_order'][$sort_col];
278 $this->_data[
'sort_order'] = $sort_order;
286 $sql =
"SELECT * FROM foo ORDER BY $sortStr";
287 $parsed = $this->_parser->parse($sql);
289 $sort_order =& $this->_data[
'sort_order'];
290 foreach (array_keys($parsed[
'sort_order']) as $sort_col){
292 $sort_order[] =& $parsed[
'sort_order'][$sort_col];
307 $this->_data[
'where_clause'] = $this->
_removeClause_rec($clause, $this->_data[
'where_clause']);
308 if ( $this->_data[
'where_clause'] == null ) {
309 unset($this->_data[
'where_clause']);
312 return $this->_data[
'where_clause'];
316 if ( is_array($this->_data[
'table_join_clause']) ){
317 $new_clauses = array();
318 $new_joins = array();
320 foreach ( $this->_data[
'table_join_clause'] as
$index=>$jc){
322 if ( $new_clause == null ) $new_clause =
'';
323 $new_clauses[] = $new_clause;
324 if (
sizeof($new_clauses) > 1 ){
325 if ( $new_clause ==
'' ) $new_joins[] =
',';
326 else $new_joins[] = $this->_data[
'table_join'][
$index-1];
329 $this->_data[
'table_join_clause'] = $new_clauses;
330 $this->_data[
'table_join'] = $new_joins;
339 if ( isset( $root[
'arg_1'] ) and isset( $root[
'arg_2']) ){
341 if ( $clause == $root ){
347 if ( $root[
'arg_1'] == null and $root[
'arg_2'] == null )
return null;
348 else if ( $root[
'arg_1'] != null and $root[
'arg_2'] == null )
return $root[
'arg_1'];
349 else if ( $root[
'arg_2'] != null and $root[
'arg_1'] == null )
return $root[
'arg_2'];
355 else if ( isset( $root[
'arg_1'] ) ){
358 return $root[
'arg_1'];
362 else if ( isset( $root[
'type']) and $root[
'type'] ==
'subclause' ){
365 if ( $root[
'value'] == null )
return null;
381 if ( isset($this->_data[
'where_clause']) and is_array($this->_data[
'where_clause']) ){
391 if ( is_array($this->_data[
'table_join_clause']) ){
392 foreach ( $this->_data[
'table_join_clause'] as
$index=>$jc){
434 if ( strpos($columnName,
'.') !==
false ){
435 list(
$table, $shortName) = explode(
'.', $columnName);
437 $shortName = $columnName;
440 $aliasName = str_replace(
'.',
'_',$columnName);
445 return $aliasColumnName;
447 $func = array(
'name'=>
'length',
'args'=>array(array(
'type'=>
'ident',
'value'=>$aliasColumnName)),
'alias'=>
'__'.($fullColumnNames ? $aliasName : $shortName).
'_length');
449 if ( !isset( $this->_data[
'set_function'] ) ){
450 $this->_data[
'set_function'] = array();
456 $index = array_search($func, $this->_data[
'set_function']);
458 $this->_data[
'set_function'][] = $func;
469 if ( !isset( $this->_data[
'column_names']) )
return;
470 foreach ( $this->_data[
'column_names'] as $columnName){
478 foreach ( array(
'arg_1',
'arg_2') as $arg){
479 if ( !isset( $root[$arg]) )
continue;
480 $type = (isset( $root[$arg][
'type'] ) ? $root[$arg][
'type'] : null);
487 if ( $this->
getTableName($root[$arg][
'value']) ==
$table ) array_push($clauses, $root);
500 if ( isset($this->_data[
'where_clause']) and is_array($this->_data[
'where_clause']) ){
508 if ( is_array($this->_data[
'table_join_clause']) ){
509 foreach ( $this->_data[
'table_join_clause'] as $jc ){
520 foreach ( array(
'arg_1',
'arg_2') as $arg){
522 if ( !isset( $root[$arg]) )
continue;
523 $type = (isset( $root[$arg][
'type'] ) ? $root[$arg][
'type'] : null);
535 if ( preg_match($regex, $root[$arg][
'value']) ) array_push($clauses, $root);
550 foreach ($clauses as $clause){
559 foreach ($clauses as $clause){
567 foreach ($clauses as $clause){
575 foreach ($clauses as $clause){
586 $selected_tables = array();
589 foreach ($this->_data[
'column_names'] as $column){
591 $selected_tables = array_unique($selected_tables);
594 $removed_tables = array();
595 foreach ( $this->_data[
'table_names'] as
$index=>$table_name){
598 if ( in_array($table_name, $exempt) )
continue;
602 if ( in_array($table_name, $selected_tables) )
continue;
608 if ( count($found) > 0 )
continue;
612 if ( count($found) > 0 )
continue;
617 $removed_tables[] = $table_name;
620 $table_names = array();
621 $table_join = array();
622 $table_join_clause = array();
623 $table_aliases = array();
625 foreach ( $this->_data[
'table_names'] as
$index=>$table_name){
626 if ( !in_array($table_name, $removed_tables) ){
627 $table_names[] = $table_name;
628 if (
$index > 0 and $newIndex > 0 ){
629 $table_join[] = $this->_data[
'table_join'][
$index-1];
631 $table_join_clause[] = $this->_data[
'table_join_clause'][
$index];
632 $table_aliases[] = $this->_data[
'table_aliases'][
$index];
637 $this->_data[
'table_names'] = $table_names;
638 $this->_data[
'table_aliases'] = $table_aliases;
639 $this->_data[
'table_join'] = $table_join;
640 $this->_data[
'table_join_clause'] = $table_join_clause;
647 throw new Exception($this->_data->toString(), E_USER_ERROR);
649 for ($i=0; $i<count($this->_data[
'column_names']); $i++){
650 $name =& $this->_data[
'column_names'][$i];
651 if ( strpos($name,
'.') === strlen($name)-1 ) $name .=
'*';
660 if ( isset( $this->_data[
'where_clause'] ) and is_array( $this->_data[
'where_clause'] )){
661 $roots[] =& $this->_data[
'where_clause'];
663 if ( isset( $this->_data[
'table_join_clause'] ) and is_array( $this->_data[
'table_join_clause']) ){
664 foreach ( $this->_data[
'table_join_clause'] as $clause ){
665 if ( is_array($clause) ){
670 foreach ($roots as $root){
671 $this->_makeEquivalenceLabels($labels, $values, $root);
691 return array_unique($tables);
695 if ( isset($root[
'table_names']) ){
696 foreach ($root[
'table_names'] as
$table){
700 foreach ( $root as $key=>$val ){
701 if ( is_array($val) ){