* Fixed some coding style inconcistencies

* make the latest patch from Robin PHP3 compatible
This commit is contained in:
Loïc Chapeaux
2001-08-15 09:53:22 +00:00
parent 1a7c1cb690
commit bd303c2b02
2 changed files with 100 additions and 96 deletions

View File

@@ -82,8 +82,10 @@ if ($sql_query != '') {
$pieces = split_sql_file($sql_query, ';');
$pieces_count = count($pieces);
for($i = 0; $i < $pieces_count; $i++)
$pieces[$i] = expand_sql_query($pieces[$i]);
// Handles table aliases
for ($i = 0; $i < $pieces_count; $i++) {
$pieces[$i] = expand_sql_query($pieces[$i]);
}
// Only one query to run
if ($pieces_count == 1 && !empty($pieces[0]) && $view_bookmark == 0) {

View File

@@ -606,6 +606,102 @@ window.parent.frames['nav'].location.replace('./left.php3?lang=<?php echo $GLOBA
/* ----- Functions used to display records returned by a sql query ----- */
/**
* Expands table alias in SQL queries
*
* @param string a single sql query
*
* @return string the expanded SQL query
*
* @author Robin Johnson
*
* @version 1.1 - 10th August 2001
*/
function expand_sql_query($sql)
{
$sql = trim($sql);
$arr = explode(' ', $sql);
if (strtoupper($arr[0]) != 'SELECT') {
return $sql;
}
$start_table_ref = FALSE;
$end_table_ref = FALSE;
// Finds which block of text has the table reference data
for ($i = 0; $i < count($arr); $i++) {
$tmp = trim(strtoupper($arr[$i]));
if ($tmp == 'FROM') {
$start_table_ref = $i + 1;
}
else if ($tmp == 'WHERE' || $tmp == 'GROUP' || $tmp == 'HAVING' || $tmp == 'ORDER'
|| $tmp == 'LIMIT' || $tmp == 'PROCEDURE' || $tmp == 'FOR' || $tmp == 'BY'
|| $tmp == 'UPDATE' || $tmp == 'LOCK' || $tmp == 'IN' || $tmp == 'SHARE'
|| $tmp == 'MODE') {
// We want only the first one
if (!$end_table_ref) {
$end_table_ref = $i - 1;
}
}
} // end for
// In case the table reference was the last thing
if (!$end_table_ref) {
$end_table_ref = count($arr) - 1;
}
// Handles the table reference data
// First put it back together
$table_ref_data = '';
for ($i = $start_table_ref; $i <= $end_table_ref; $i++) {
$table_ref_data .= ' ' . $arr[$i];
} // end for
// Cleans out the extra spaces
$table_ref_data = trim($table_ref_data);
// Cleans up a bit
unset($arr);
unset($tmp);
unset($i);
// Breaks it apart into each table used
$arr = explode(',', $table_ref_data);
// Handles each table used
reset($arr);
while (list(, $table) = each ($arr)) {
$table = trim($table);
if (isset($data)) {
unset($data);
}
$data = explode(' ', $table); //will have at most 3 items
if (isset($data_count)) {
unset($data_count);
}
$data_count = count($data);
if (isset($match)) {
unset($match);
}
if ($data_count == 1) {
continue;
}
// Checks with 'as' keyword specified...
if ($data_count == 3) {
$data[1] = $data[2];
$match = $data[0] . ' as ' . $data[1];
}
// ... and now in form "departments d"
if ($data_count >= 2) {
$sql = preg_replace('/\W' . $data[1] . '\./i', ' ' . $data[0] . '.', $sql);
if (!isset($match)) {
$match = $data[0] . ' ' . $data[1];
}
$sql = str_replace($match, ' ' . $data[0], $sql);
} // end if
} // end while
return $sql;
} // end of the 'expand_sql_query()' function
/**
* Displays a navigation bar to browse among the results of a sql query
*
@@ -1549,100 +1645,6 @@ var errorMsg2 = '<?php echo(str_replace('\'', '\\\'', $GLOBALS['strNotValidNumbe
return $ret;
} // end of the 'split_sql_file()' function
/**
* @description Expand table alias in SQL queries
*
* @lastmodified Robin Johnson
*
* @version 1
*
* @date 10th August 2001
*
* @param string a single sql query
*
* @return string the expanded SQL query
*/
function expand_sql_query($sql)
{ $sql = trim($sql);
$arr = explode(" ",$sql);
if(strtoupper($arr[0]) != "SELECT")
return $sql;
$start_table_ref = FALSE;
$end_table_ref = FALSE;
// find which block of text has the table reference data
for($i = 0; $i < count($arr); $i++)
{ $tmp = trim(strtoupper($arr[$i]));
switch($tmp)
{ case "FROM" :
$start_table_ref = $i+1;
break;
case "WHERE" :
case "GROUP" :
case "HAVING" :
case "ORDER" :
case "LIMIT" :
case "PROCEDURE" :
case "FOR" :
case "BY" :
case "UPDATE" :
case "LOCK" :
case "IN" :
case "SHARE" :
case "MODE" :
//we want only the first one
if(!$end_table_ref)
$end_table_ref = $i-1;
break;
} //switch
} // for
// in case the table reference was the last thing
if (!$end_table_ref)
$end_table_ref = count($arr) - 1;
// handle the table reference data
// first put it back together
$table_ref_data = "";
for ($i = $start_table_ref; $i <= $end_table_ref; $i++)
{ $table_ref_data .= " ".$arr[$i];
} // for
// clean out the extra spaces
$table_ref_data = trim($table_ref_data);
// clean up a bit
unset($arr); unset($tmp); unset($i);
//break it apart into each table used
$arr = explode(",",$table_ref_data);
// handle each table used
foreach ($arr as $table)
{ $table = trim($table);
if(isset($data)) unset($data);
$data = explode(" ",$table); //will have at most 3 items
if(isset($data_count)) unset($data_count);
$data_count = count($data);
if(isset($match)) unset($match);
if ( $data_count == 1)
continue;
if ( $data_count == 3)
{ $data[1] = $data[2];
$match = $data[0]." as ".$data[1];
}
if ( $data_count >= 2) //now in form "departments d"
{ $sql = preg_replace("/\W$data[1]\./i"," ".$data[0].".",$sql);
if(!isset($match))
$match = $data[0]." ".$data[1];
$sql = str_replace($match," ".$data[0],$sql);
} //if
} //foreach
return $sql;
} // function expand_sql_query
/**
* Removes # type remarks from large sql files