SQL query parser for Hazaar DBI.
Parses SQL strings into structured components for query analysis and manipulation. Supports SELECT, JOIN, WHERE, GROUP BY, HAVING, UNION, INTERSECT, EXCEPT, ORDER BY, LIMIT, OFFSET, and FETCH clauses.
List of SQL clause keywords used for parsing queries.
private array $keywords = array (
0 => 'SELECT',
1 => 'FROM',
2 => 'WHERE',
3 => 'GROUP BY',
4 => 'HAVING',
5 => 'WINDOW',
6 => 'UNION',
7 => 'INTERSECT',
8 => 'EXCEPT',
9 => 'ORDER BY',
10 => 'LIMIT',
11 => 'OFFSET',
12 => 'FETCH',
)
The raw SQL string being parsed.
Construct a new SQLParser instance and optionally parse a SQL string.
public __construct(?string $sql): void
| Parameter | Type | Description |
|---|
$sql | string | optional SQL string to parse on construction |
Parse a SQL string into structured query components.
public parse(string $sql): bool
Only SELECT queries are supported. Throws on unknown clause keywords.
| Parameter | Type | Description |
|---|
$sql | string | the SQL string to parse |
public processSELECT(string $line): void
| Parameter | Type | Description |
|---|
$line | string | |
public processFROM(string $line): void
| Parameter | Type | Description |
|---|
$line | string | |
public processWHERE(string $line): void
| Parameter | Type | Description |
|---|
$line | string | |
public processGROUP_BY(string $line): void
| Parameter | Type | Description |
|---|
$line | string | |
public processHAVING(string $line): void
| Parameter | Type | Description |
|---|
$line | string | |
public parseUNION(string $line): void
| Parameter | Type | Description |
|---|
$line | string | |
public processINTERSECT(string $line): void
| Parameter | Type | Description |
|---|
$line | string | |
public processEXCEPT(string $line): void
| Parameter | Type | Description |
|---|
$line | string | |
public processORDER_BY(string $line): void
| Parameter | Type | Description |
|---|
$line | string | |
public processLIMIT(string $line): void
| Parameter | Type | Description |
|---|
$line | string | |
public processOFFSET(string $line): void
| Parameter | Type | Description |
|---|
$line | string | |
public processFETCH(string $line): void
| Parameter | Type | Description |
|---|
$line | string | |
Splits a string by the word boundaries of given SQL keywords.
private splitWordBoundaries(string $string, array $keywords, ?int $start_pos): void
| Parameter | Type | Description |
|---|
$string | string | the SQL string to split |
$keywords | array | list of keywords to split by |
$start_pos | int | reference to the position of the first found keyword |
Parses a SQL condition string into a nested associative array structure.
private parseCondition(string $line, bool $useRefs): void
This method is used to parse WHERE and HAVING clause conditions, supporting nested parentheses and logical operators (AND, OR).
| Parameter | Type | Description |
|---|
$line | string | the condition string to parse |
$useRefs | bool | if true, unresolved symbols are returned as references |
Helper for parseCondition: processes a single condition or a substituted group.
private processConditionPart(string $part, array $subs, bool $useRefs, string $): void
| Parameter | Type | Description |
|---|
$part | string | the condition part to process |
$subs | array | |
$useRefs | bool | whether to return unresolved symbols as references |
$ | string | |
Recursively resolve all substitution keys in an array structure.
private resolveSubsRecursive(mixed $data, array $subs, bool $useRefs): mixed
This is used to fully expand any nested conditions that were replaced with placeholders during parsing.
| Parameter | Type | Description |
|---|
$data | mixed | the data structure to resolve substitutions in (can be an array or a string) |
$subs | array | |
$useRefs | bool | whether to return unresolved symbols as references |
Generated by Hazaar API Doc Generator on Tue, 21 Apr 2026 04:00:24 +0000