所以这大致是我想出的:
class Query_Parser {
/**
* Regex patterns for various syntax components
*
* @var array
*/
private $match_patterns = array(
'function' => '([a-zA-Z0-9_]+\((.*?)\))',
'identifier' => '([a-zA-Z0-9_-]+\.?)+',
'operator' => '=|AND|&&?|~|\|\|?|\^|/|>=?|<=?|-|%|OR|\+|NOT|\!=?|<>|XOR'
);
/**
* Regex matches
*
* @var array
*/
public $matches = array(
'functions' => array(),
'identifiers' => array(),
'operators' => array(),
'combined' => array(),
);
/**
* Constructor/entry point into parser
*
* @param string
*/
public function __construct($sql = '')
{
// Get sql clause components
preg_match_all('`'.$this->match_patterns['function'].'`', $sql, $this->matches['functions'], PREG_SET_ORDER);
preg_match_all('`'.$this->match_patterns['identifier'].'`', $sql, $this->matches['identifiers'], PREG_SET_ORDER);
preg_match_all('`'.$this->match_patterns['operator'].'`', $sql, $this->matches['operators'], PREG_SET_ORDER);
// Get everything at once for ordering
$full_pattern = '`'.$this->match_patterns['function'].'+|'.$this->match_patterns['identifier'].'|('.$this->match_patterns['operator'].')+`i';
preg_match_all($full_pattern, $sql, $this->matches['combined'], PREG_SET_ORDER);
// Go through the matches, and get the most relevant matches
$this->matches = array_map(array($this, 'filter_array'), $this->matches);
}
// --------------------------------------------------------------------------
/**
* Public parser method for seting the parse string
*
* @param string
*/
public function parse_join($sql)
{
$this->__construct($sql);
return $this->matches;
}
// --------------------------------------------------------------------------
/**
* Returns a more useful match array
*
* @param array
* @return array
*/
private function filter_array($array)
{
$new_array = array();
foreach($array as $row)
{
if (is_array($row))
{
$new_array[] = $row[0];
}
else
{
$new_array[] = $row;
}
}
return $new_array;
}
}
然后我在我的 Query Builder 类中运行它,在子句中引用标识符,然后将它串在一起:
// Parse out the join condition
$parts = $parser->parse_join($condition);
$count = count($parts['identifiers']);
// Go through and quote the identifiers
for($i=0; $i <= $count; $i++)
{
if (in_array($parts['combined'][$i], $parts['identifiers']) && ! is_numeric($parts['combined'][$i]))
{
$parts['combined'][$i] = $this->quote_ident($parts['combined'][$i]);
}
}
$parsed_condition = implode(' ', $parts['combined']);