SQLParser
SQLParser
SQL query parser for Hazaar DBI.
class SQLParserParses 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.
Properties
type
public string $typeType: string
table
public string $tableType: string
alias
public string $aliasType: string
columns
public array $columnsType: array
joins
public array $joinsType: array
where
public array $whereType: array
group
public array $groupType: array
having
public array $havingType: array
combine
public array $combineType: array
order
public array $orderType: array
limit
public int $limitType: int
offset
public int $offsetType: int
fetch
public array $fetchType: array
keywords
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',
)Type: array
sql
The raw SQL string being parsed.
private string $sqlType: string
Methods
__construct
Construct a new SQLParser instance and optionally parse a SQL string.
public __construct(?string $sql): voidParameters
| Parameter | Type | Description |
|---|---|---|
$sql | string | optional SQL string to parse on construction |
parse
Parse a SQL string into structured query components.
public parse(string $sql): boolReturns: bool
Only SELECT queries are supported. Throws on unknown clause keywords.
Parameters
| Parameter | Type | Description |
|---|---|---|
$sql | string | the SQL string to parse |
processSELECT
public processSELECT(string $line): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$line | string |
processFROM
public processFROM(string $line): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$line | string |
processWHERE
public processWHERE(string $line): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$line | string |
processGROUP_BY
public processGROUP_BY(string $line): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$line | string |
processHAVING
public processHAVING(string $line): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$line | string |
parseUNION
public parseUNION(string $line): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$line | string |
processINTERSECT
public processINTERSECT(string $line): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$line | string |
processEXCEPT
public processEXCEPT(string $line): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$line | string |
processORDER_BY
public processORDER_BY(string $line): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$line | string |
processLIMIT
public processLIMIT(string $line): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$line | string |
processOFFSET
public processOFFSET(string $line): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$line | string |
processFETCH
public processFETCH(string $line): voidReturns: void
Parameters
| Parameter | Type | Description |
|---|---|---|
$line | string |
splitWordBoundaries
Splits a string by the word boundaries of given SQL keywords.
private splitWordBoundaries(string $string, array $keywords, ?int $start_pos): arrayReturns: array
Parameters
| 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 |
parseCondition
Parses a SQL condition string into a nested associative array structure.
private parseCondition(string $line, bool $useRefs): arrayReturns: array
This method is used to parse WHERE and HAVING clause conditions, supporting nested parentheses and logical operators (AND, OR).
Parameters
| Parameter | Type | Description |
|---|---|---|
$line | string | the condition string to parse |
$useRefs | bool | if true, unresolved symbols are returned as references |
processConditionPart
Helper for parseCondition: processes a single condition or a substituted group.
private processConditionPart(string $part, array $subs, bool $useRefs): array|stringReturns: array|string
Parameters
| Parameter | Type | Description |
|---|---|---|
$part | string | the condition part to process |
$subs | array | |
$useRefs | bool | whether to return unresolved symbols as references |
resolveSubsRecursive
Recursively resolve all substitution keys in an array structure.
private resolveSubsRecursive(mixed $data, array $subs, bool $useRefs): mixedReturns: mixed
This is used to fully expand any nested conditions that were replaced with placeholders during parsing.
Parameters
| 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 Fri, 04 Sep 2026 23:32:16 +0000