Source of file QueryParser.php
Size: 3,267 Bytes - Last Modified: 2015-11-10T11:45:23-05:00
../src/Query/QueryParser.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
Covered by 6 test(s):
64
Covered by 6 test(s):
65666768697071727374757677
Covered by 19 test(s):
78
Covered by 19 test(s):
79
Covered by 19 test(s):
808182
Covered by 19 test(s):
83
Covered by 19 test(s):
848586
Covered by 19 test(s):
8788
Covered by 19 test(s):
8990919293949596979899100101
Covered by 16 test(s):
102
Covered by 16 test(s):
103104105
Covered by 16 test(s):
106107
Covered by 16 test(s):
108
Covered by 16 test(s):
109
Covered by 16 test(s):
110
Covered by 16 test(s):
111
Covered by 16 test(s):
112113
Covered by 16 test(s):
114115116117118119120121122123124125126
Covered by 19 test(s):
127128
Covered by 19 test(s):
129130
Covered by 19 test(s):
131
Covered by 19 test(s):
132133
Covered by 19 test(s):
134135136137138
Covered by 1 test(s):
| <?php /** * Query * * Free Query Builder / Database Abstraction Layer * * @package Query * @author Timothy J. Warren * @copyright Copyright (c) 2012 - 2014 * @link https://github.com/aviat4ion/Query * @license http://philsturgeon.co.uk/code/dbad-license */ // -------------------------------------------------------------------------- namespace Query; /** * Utility Class to parse sql clauses for properly escaping identifiers * * @package Query * @subpackage Query_Builder */ class QueryParser { /** * DB Driver * * @var DriverInterface */ private $db; /** * 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 Driver\DriverInterface $db */ public function __construct(DriverInterface $db) { $this->db = $db; } // -------------------------------------------------------------------------- /** * Parser method for setting the parse string * * @param string $sql * @return array */ public function parse_join($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); return $this->matches; } // -------------------------------------------------------------------------- /** * Compiles a join condition after parsing * * @param string $condition * @return string */ public function compile_join($condition) { $parts = $this->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->db->quote_ident($parts['combined'][$i]); } } return implode('', $parts['combined']); } // -------------------------------------------------------------------------- /** * Returns a more useful match array * * @param array $array * @return array */ protected function filter_array($array) { $new_array = array(); foreach($array as $row) { $new_array[] = (is_array($row)) ? $row[0] : $row; } return $new_array; } } // End of QueryParser.php |