Query/apiDocumentation/files/src/QueryParser.php.txt

109 lines
2.8 KiB
Plaintext
Raw Normal View History

2023-01-20 11:30:51 -05:00
<?php declare(strict_types=1);
/**
* Query
*
* SQL Query Builder / Database Abstraction Layer
*
* PHP version 8.1
*
* @package Query
* @author Timothy J. Warren <tim@timshome.page>
* @copyright 2012 - 2023 Timothy J. Warren
* @license http://www.opensource.org/licenses/mit-license.html MIT License
* @link https://git.timshomepage.net/aviat/Query
* @version 4.0.0
*/
2023-03-17 16:28:07 -04:00
2023-01-20 11:30:51 -05:00
namespace Query;
use Query\Drivers\DriverInterface;
/**
* Utility Class to parse sql clauses for properly escaping identifiers
*/
2023-03-17 16:28:07 -04:00
class QueryParser
{
2023-01-20 11:30:51 -05:00
/**
* Regex matches
*/
public array $matches = [
'functions' => [],
'identifiers' => [],
'operators' => [],
'combined' => [],
];
2023-03-17 16:28:07 -04:00
/**
* Regex patterns for various syntax components
*/
private array $matchPatterns = [
'function' => '([a-zA-Z0-9_]+\((.*?)\))',
'identifier' => '([a-zA-Z0-9_-]+\.?)+',
'operator' => '=|AND|&&?|~|\|\|?|\^|/|>=?|<=?|-|%|OR|\+|NOT|\!=?|<>|XOR',
];
2023-01-20 11:30:51 -05:00
/**
* Constructor/entry point into parser
*/
public function __construct(private readonly DriverInterface $db)
{
}
/**
* Parser method for setting the parse string
*
* @return array[]
*/
public function parseJoin(string $sql): array
{
// Get sql clause components
2023-03-17 16:28:07 -04:00
preg_match_all('`' . $this->matchPatterns['function'] . '`', $sql, $this->matches['functions'], PREG_SET_ORDER);
preg_match_all('`' . $this->matchPatterns['identifier'] . '`', $sql, $this->matches['identifiers'], PREG_SET_ORDER);
preg_match_all('`' . $this->matchPatterns['operator'] . '`', $sql, $this->matches['operators'], PREG_SET_ORDER);
2023-01-20 11:30:51 -05:00
// Get everything at once for ordering
2023-03-17 16:28:07 -04:00
$fullPattern = '`' . $this->matchPatterns['function'] . '+|' . $this->matchPatterns['identifier'] . '|(' . $this->matchPatterns['operator'] . ')+`i';
2023-01-20 11:30:51 -05:00
preg_match_all($fullPattern, $sql, $this->matches['combined'], PREG_SET_ORDER);
// Go through the matches, and get the most relevant matches
2023-03-17 16:28:07 -04:00
$this->matches = array_map($this->filterArray(...), $this->matches);
2023-01-20 11:30:51 -05:00
return $this->matches;
}
/**
* Compiles a join condition after parsing
*/
public function compileJoin(string $condition): string
{
$parts = $this->parseJoin($condition);
$count = is_countable($parts['identifiers']) ? count($parts['identifiers']) : 0;
// Go through and quote the identifiers
2023-03-17 16:28:07 -04:00
for ($i=0; $i <= $count; $i++)
2023-01-20 11:30:51 -05:00
{
2023-03-17 16:28:07 -04:00
if (in_array($parts['combined'][$i], $parts['identifiers'], TRUE) && ! is_numeric($parts['combined'][$i]))
2023-01-20 11:30:51 -05:00
{
$parts['combined'][$i] = $this->db->quoteIdent($parts['combined'][$i]);
}
}
return implode('', $parts['combined']);
}
/**
* Returns a more useful match array
*/
protected function filterArray(array $array): array
{
$newArray = [];
2023-03-17 16:28:07 -04:00
foreach ($array as $row)
2023-01-20 11:30:51 -05:00
{
$newArray[] = (is_array($row)) ? $row[0] : $row;
}
return $newArray;
}
2023-03-17 16:28:07 -04:00
}