356 lines
8.6 KiB
PHP
356 lines
8.6 KiB
PHP
<?php
|
|
require __DIR__ . '/vendor/autoload.php';
|
|
|
|
class sqlparser
|
|
{
|
|
/**
|
|
* Parser object of SqlParser.
|
|
*
|
|
* @var object
|
|
* @access public
|
|
*/
|
|
public $parser;
|
|
|
|
/**
|
|
* Statements of parser.
|
|
*
|
|
* @var array
|
|
* @access public
|
|
*/
|
|
public $statements;
|
|
|
|
/**
|
|
* First statement.
|
|
*
|
|
* @var object
|
|
* @access public
|
|
*/
|
|
public $statement;
|
|
|
|
/**
|
|
* Count statements.
|
|
*
|
|
* @var int
|
|
* @access public
|
|
*/
|
|
public $statementsCount = 0;
|
|
|
|
/**
|
|
* Is first statemnt select type.
|
|
*
|
|
* @var bool
|
|
* @access public
|
|
*/
|
|
public $isSelect = false;
|
|
|
|
/**
|
|
* DAO
|
|
*
|
|
* @var object
|
|
* @access public
|
|
*/
|
|
public $dao = null;
|
|
|
|
/**
|
|
* Origin tables.
|
|
*
|
|
* @var array
|
|
* @access public
|
|
*/
|
|
public $originTables = array();
|
|
|
|
/**
|
|
* Columns.
|
|
*
|
|
* @var array
|
|
* @access public
|
|
*/
|
|
public $columns = array();
|
|
|
|
/**
|
|
* Tables.
|
|
*
|
|
* @var array
|
|
* @access public
|
|
*/
|
|
public $tables = array();
|
|
|
|
public function __construct($query)
|
|
{
|
|
$query = $this->skipLineBreak($query);
|
|
if(empty($query)) return;
|
|
|
|
$this->parser = new PhpMyAdmin\SqlParser\Parser($query);
|
|
$this->statements = $this->parser->statements;
|
|
$this->statementsCount = count($this->statements);
|
|
$this->statement = $this->statementsCount > 0 ? current($this->statements) : null;
|
|
|
|
$this->isSelect = $this->statement instanceof PhpMyAdmin\SqlParser\Statements\SelectStatement === true;
|
|
}
|
|
|
|
/**
|
|
* Set dao.
|
|
*
|
|
* @param object $dao
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function setDAO($dao)
|
|
{
|
|
$this->dao = $dao;
|
|
}
|
|
|
|
/**
|
|
* Parse statement.
|
|
*
|
|
* @access public
|
|
* @return void
|
|
*/
|
|
public function parseStatement()
|
|
{
|
|
if(empty($this->statement)) return;
|
|
|
|
$this->columns = $this->parseColumns();
|
|
$this->tables = $this->parseTables();
|
|
}
|
|
|
|
/**
|
|
* Get function.
|
|
*
|
|
* @param string $name
|
|
* @param mixed $args
|
|
* @access public
|
|
* @return string
|
|
*/
|
|
public function getFunction($name, ...$args)
|
|
{
|
|
$name = strtoupper($name);
|
|
$argStr = implode(', ', $args);
|
|
return "$name($argStr)";
|
|
}
|
|
|
|
/**
|
|
* Match columns with table.
|
|
*
|
|
* @access public
|
|
* @return array
|
|
*/
|
|
public function matchColumnsWithTable()
|
|
{
|
|
if(empty($this->statement)) return array();
|
|
|
|
if(count($this->tables) == 1) return $this->combineSingleTable();
|
|
return $this->combineMultipleTable();
|
|
}
|
|
|
|
/**
|
|
* Combine single table to columns.
|
|
*
|
|
* @access private
|
|
* @return array
|
|
*/
|
|
private function combineSingleTable()
|
|
{
|
|
$combineColumns = array();
|
|
$fromTable = current($this->tables);
|
|
foreach($this->columns as $columnName => $column)
|
|
{
|
|
$column['table'] = array_merge($fromTable, array('column' => $column['origin']));
|
|
|
|
$combineColumns[$columnName] = $column;
|
|
}
|
|
|
|
return $combineColumns;
|
|
}
|
|
|
|
/**
|
|
* Combine multiple table to columns.
|
|
*
|
|
* @access private
|
|
* @return array
|
|
*/
|
|
private function combineMultipleTable()
|
|
{
|
|
foreach($this->columns as $columnName => $column)
|
|
{
|
|
$column['table'] = $this->searchTables($column['table'], $this->tables, $column['origin']);
|
|
$combineColumns[$columnName] = $column;
|
|
}
|
|
|
|
return $combineColumns;
|
|
}
|
|
|
|
/**
|
|
* Search table from origin tables.
|
|
*
|
|
* @param string $tableName
|
|
* @param string $tables
|
|
* @param string $column
|
|
* @access private
|
|
* @return string|false
|
|
*/
|
|
private function searchTables($tableName, $tables, $column)
|
|
{
|
|
/* 如果能使用别名匹配上,那么直接返回。*/
|
|
/* If it can be matched using an alias, then it returns. */
|
|
foreach($tables as $table) if($tableName == $table['alias']) return array_merge($table, array('column' => $column));
|
|
|
|
/* 如果匹配不上,则字段没有使用别名进行限制,那么需要通过字段去遍历所有表。*/
|
|
/* If it doesn't match, then the field is not aliased, and you need to iterate over all tables using the field. */
|
|
foreach($tables as $table)
|
|
{
|
|
$isTable = $table['isTable'];
|
|
$originTable = $table['originTable'];
|
|
|
|
/* 如果是原始表,并且列在原始表中存在,那么返回这个表。*/
|
|
/* If it is the original table and the column exists in the original table, then the table is returned. */
|
|
if($isTable && $this->columnExistInOriginTable($originTable, $column)) return array_merge($table, array('column' => $column));
|
|
/* 如果不是原始表,并且列在子句中存在,那么返回子句中这个字段对应的表。*/
|
|
/* If it is not the original table and the column exists in the clause, then the table corresponding to the field in the clause is returned. */
|
|
if(!$isTable && isset($originTable[$column])) return array_merge($originTable[$column]['table'], array('column' => $originTable[$column]['origin']));
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Parse columns.
|
|
*
|
|
* @access private
|
|
* @return void
|
|
*/
|
|
private function parseColumns()
|
|
{
|
|
$fields = array();
|
|
foreach($this->statement->expr as $expr)
|
|
{
|
|
/* 获取查询数据后真正展示出来的列名 */
|
|
$columnName = empty($expr->alias) ? $expr->column : $expr->alias;
|
|
|
|
$fields[$columnName] = array('origin' => $expr->column, 'table' => $expr->table);
|
|
}
|
|
|
|
return $fields;
|
|
}
|
|
|
|
/**
|
|
* Parse tables.
|
|
*
|
|
* @access private
|
|
* @return void
|
|
*/
|
|
private function parseTables()
|
|
{
|
|
$from = current($this->statement->from);
|
|
$joins = $this->statement->join;
|
|
|
|
$tables = array();
|
|
$tables[] = $this->parseTable($from, 'from');
|
|
|
|
foreach($joins as $join) $tables[] = $this->parseTable($join->expr, 'join');
|
|
|
|
return $tables;
|
|
}
|
|
|
|
/**
|
|
* Parse table.
|
|
*
|
|
* @param object $expr
|
|
* @param string $type
|
|
* @access private
|
|
* @return void
|
|
*/
|
|
private function parseTable($expr, $type)
|
|
{
|
|
$isTable = empty($expr->subquery);
|
|
|
|
$table = array('alias' => $expr->alias, 'isTable' => $isTable, 'type' => $type);
|
|
$table['originTable'] = $this->getOriginTable($expr->expr, $isTable);
|
|
|
|
return $table;
|
|
}
|
|
|
|
/**
|
|
* Get origin table from table name or expr.
|
|
*
|
|
* @param string $table
|
|
* @param bool $isTable
|
|
* @access private
|
|
* @return string|array
|
|
*/
|
|
private function getOriginTable($table, $isTable)
|
|
{
|
|
if(!$isTable)
|
|
{
|
|
$parser = new sqlparser($table);
|
|
$parser->setDAO($this->dao);
|
|
$parser->parseStatement();
|
|
return $parser->matchColumnsWithTable();
|
|
}
|
|
|
|
$this->storeOriginTable($table);
|
|
|
|
return $table;
|
|
}
|
|
|
|
/**
|
|
* Judge column exist in origin table or not.
|
|
*
|
|
* @param string $table
|
|
* @param string $column
|
|
* @access private
|
|
* @return bool
|
|
*/
|
|
private function columnExistInOriginTable($table, $column)
|
|
{
|
|
$originTable = $this->getOriginTableColumns($table);
|
|
if(!$originTable) return false;
|
|
|
|
return isset($originTable[$column]);
|
|
}
|
|
|
|
/**
|
|
* Get origin table columns.
|
|
*
|
|
* @param string $table
|
|
* @access private
|
|
* @return array|null
|
|
*/
|
|
private function getOriginTableColumns($table)
|
|
{
|
|
$originTables = $this->originTables;
|
|
return isset($originTables[$table]) ? $originTables[$table] : null;
|
|
}
|
|
|
|
/**
|
|
* Store origin table.
|
|
*
|
|
* @param string $table
|
|
* @access private
|
|
* @return void
|
|
*/
|
|
private function storeOriginTable($table)
|
|
{
|
|
if(!isset($this->originTables[$table])) $this->originTables[$table] = $this->dao->descTable($table);
|
|
}
|
|
|
|
/**
|
|
* Skip line break in sql.
|
|
*
|
|
* @param string $sql
|
|
* @access private
|
|
* @return string
|
|
*/
|
|
private function skipLineBreak($sql)
|
|
{
|
|
$sql = str_replace("\n\t", " ", $sql);
|
|
$sql = str_replace("\t\n", " ", $sql);
|
|
$sql = str_replace("\n\r", " ", $sql);
|
|
$sql = str_replace("\r\n", " ", $sql);
|
|
$sql = str_replace("\r", " ", $sql);
|
|
$sql = str_replace("\n", " ", $sql);
|
|
|
|
return $sql;
|
|
}
|
|
}
|