SQL
SQL
SQL query builder for constructing and managing SQL statements in a structured, programmatic way.
class SQL implements \Hazaar\DBI\QueryBuilder\QueryBuilderSupports SELECT, INSERT, UPDATE, DELETE, TRUNCATE, and advanced SQL features such as joins, unions, conflict handling, and more.
Properties
type
The type of SQL query being constructed (SELECT, INSERT, etc.).
protected QueryType $type = 'QueryType'selectGroups
protected array $selectGroupsquoteSpecial
The character used to quote SQL identifiers (e.g., table or column names).
protected string $quoteSpecial = '"'reservedWords
protected array $reservedWordsselect
The list of columns to select in the query.
protected mixed $selectThis array holds the column names or expressions that will be included in the SELECT clause of the generated SQL statement.
fields
The fields to update or insert.
protected mixed $fieldsThis can be a bunch of things, like an array, strings, Table or even a Model or stdClass.
primaryTable
protected array $primaryTabletables
protected array $tableswhere
protected array $wheregroup
protected array $grouphaving
protected array $havingwindow
protected array $windowjoins
protected array $joinscombine
protected array $combineunions
protected array $unionsorder
protected array $orderfetch
protected array $fetchreturning
The columns or expressions to return from an INSERT, UPDATE, or DELETE statement.
protected mixed $returningcascade
Whether to use CASCADE for TRUNCATE statements.
protected bool $cascadedistinct
public bool $distinctlimit
public int $limitoffset
public int $offsetconflictTarget
public null $conflictTargetconflictUpdate
public null $conflictUpdateschemaName
public string $schemaNamevalueIndex
private array $valueIndexMethods
__construct
Constructs a new SQL query builder instance.
public __construct(?string $schemaName): voidParameters
| Parameter | Type | Description |
|---|---|---|
$schemaName | string | optional schema name to use for table references |
__toString
Returns the SQL query as a string, terminated with a semicolon.
public __toString(): stringsetReservedWords
Sets the list of reserved SQL words for identifier quoting.
public setReservedWords(array $words): voidParameters
| Parameter | Type | Description |
|---|---|---|
$words | array | list of reserved words |
getSchemaName
Gets the schema name used for table references.
public getSchemaName(): ?stringparseSchemaName
public parseSchemaName(string $tableName): voidParameters
| Parameter | Type | Description |
|---|---|---|
$tableName | string |
schemaName
public schemaName(string $tableName): stringParameters
| Parameter | Type | Description |
|---|---|---|
$tableName | string |
quote
Quotes a string value for use in SQL queries, optionally escaping special characters.
public quote(string $string, bool $addSlashes = true): stringParameters
| Parameter | Type | Description |
|---|---|---|
$string | string | the string to quote |
$addSlashes | bool | whether to escape special characters |
quoteSpecial
Quotes SQL identifiers (such as table or column names) using the configured quote character.
public quoteSpecial(mixed $value): mixedParameters
| Parameter | Type | Description |
|---|---|---|
$value | mixed | the identifier to quote |
reset
Resets the query builder to its initial state, clearing all query components.
public reset(): selfcreate
Generates a CREATE statement for the specified object type and name.
public create(string $name, string $type, bool $ifNotExists): stringParameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The name of the object to create (table, index, etc.). |
$type | string | The type of object to create (e.g., 'table', 'index'). |
$ifNotExists | bool | whether to include IF NOT EXISTS in the statement |
insert
Sets the query type to INSERT and assigns the fields to insert.
public insert(mixed $fields): selfParameters
| Parameter | Type | Description |
|---|---|---|
$fields | mixed | The fields to insert (array, Table, Model, etc.). |
update
Sets the query type to UPDATE and assigns the fields to update.
public update(array $fields): selfParameters
| Parameter | Type | Description |
|---|---|---|
$fields | array | the fields to update |
delete
Sets the query type to DELETE.
public delete(): selftruncate
Sets the query type to TRUNCATE and optionally enables CASCADE.
public truncate(bool $cascade): selfParameters
| Parameter | Type | Description |
|---|---|---|
$cascade | bool | whether to use CASCADE for the TRUNCATE statement |
count
Sets the SELECT clause to COUNT(*) and returns the resulting SQL string.
public count(): stringexists
Generates a SELECT EXISTS query for the specified table and criteria.
public exists(string $tableName, mixed $criteria): stringParameters
| Parameter | Type | Description |
|---|---|---|
$tableName | string | the table to check for existence |
$criteria | mixed | optional criteria for the WHERE clause |
select
Sets the query type to SELECT and assigns the columns to select.
public select(mixed $columns): selfParameters
| Parameter | Type | Description |
|---|---|---|
$columns | mixed |
distinct
Selects only distinct rows that match based on the specified expressions.
public distinct(string $columns): selfParameters
| Parameter | Type | Description |
|---|---|---|
$columns | string |
from
Sets the table to select from, with optional alias.
public from(string $table, ?string $alias): selfParameters
| Parameter | Type | Description |
|---|---|---|
$table | string | the table name |
$alias | string | optional alias for the table |
table
Sets the primary table to use for INSERT, UPDATE or DELETE.
public table(string $table, ?string $alias): selfParameters
| Parameter | Type | Description |
|---|---|---|
$table | string | The name of the table |
$alias | string | An optional alias for the table |
where
Defines a WHERE selection criteria.
public where(string $criteria): selfParameters
| Parameter | Type | Description |
|---|---|---|
$criteria | string |
group
Sets the GROUP BY columns for the query.
public group(string $columns): selfParameters
| Parameter | Type | Description |
|---|---|---|
$columns | string |
having
Sets the HAVING clause for the query.
public having(array $columns): selfParameters
| Parameter | Type | Description |
|---|---|---|
$columns | array | the columns or conditions for the HAVING clause |
window
public window(string $name, string $partitionBy, null $orderBy): selfParameters
| Parameter | Type | Description |
|---|---|---|
$name | string | |
$partitionBy | string | |
$orderBy | null |
join
Adds a JOIN clause to the query.
public join(string $references, null $on, ?string $alias, string $type = 'INNER'): selfParameters
| Parameter | Type | Description |
|---|---|---|
$references | string | the table or reference to join |
$on | null | the join condition(s) |
$alias | string | optional alias for the joined table |
$type | string | The type of join (e.g., 'INNER', 'LEFT'). |
union
Adds a UNION clause to the query with another QueryBuilder instance.
public union(QueryBuilder $query): selfParameters
| Parameter | Type | Description |
|---|---|---|
$query | QueryBuilder | the query to union with |
unionAll
Adds a UNION ALL clause to the query with another QueryBuilder instance.
public unionAll(QueryBuilder $query): QueryBuilderParameters
| Parameter | Type | Description |
|---|---|---|
$query | QueryBuilder | the query to union all with |
intersect
Adds an INTERSECT clause to the query with another QueryBuilder instance.
public intersect(QueryBuilder $query): selfParameters
| Parameter | Type | Description |
|---|---|---|
$query | QueryBuilder | the query to intersect with |
except
Adds an EXCEPT clause to the query with another QueryBuilder instance.
public except(QueryBuilder $query): selfParameters
| Parameter | Type | Description |
|---|---|---|
$query | QueryBuilder | the query to except with |
order
Sets the ORDER BY clause for the query.
public order(string $fieldDef, int $sortDirection = 'SORT_ASC'): selfParameters
| Parameter | Type | Description |
|---|---|---|
$fieldDef | string | the field(s) to order by |
$sortDirection | int | the sort direction (SORT_ASC or SORT_DESC) |
limit
Sets or gets the LIMIT value for the query.
public limit(?int $limit): selfParameters
| Parameter | Type | Description |
|---|---|---|
$limit | int | the limit value to set, or null to get the current value |
offset
Sets or gets the OFFSET value for the query.
public offset(?int $offset): selfParameters
| Parameter | Type | Description |
|---|---|---|
$offset | int | the offset value to set, or null to get the current value |
toString
Return the current selection as a valid SQL string.
public toString(bool $terminateWithColon): stringParameters
| Parameter | Type | Description |
|---|---|---|
$terminateWithColon | bool |
field
Quotes a field name if it is a reserved word.
public field(string $string): stringParameters
| Parameter | Type | Description |
|---|---|---|
$string | string | the field name |
prepareFields
public prepareFields(mixed $fields, array $exclude, array $tables): stringParameters
| Parameter | Type | Description |
|---|---|---|
$fields | mixed | |
$exclude | array | |
$tables | array |
prepareValue
Prepares a value for use in a SQL statement, handling special value actions and parameter indexing.
public prepareValue(string $key, mixed $value): mixedParameters
| Parameter | Type | Description |
|---|---|---|
$key | string | the parameter key |
$value | mixed | the value to prepare |
prepareArrayAliases
public prepareArrayAliases(array $array): voidParameters
| Parameter | Type | Description |
|---|---|---|
$array | array |
prepareCriteria
public prepareCriteria(string $criteria, string $bindType = 'AND', string $tissue = '=', ?string $parentRef, bool $setKey = true, int $depth): stringParameters
| Parameter | Type | Description |
|---|---|---|
$criteria | string | |
$bindType | string | |
$tissue | string | |
$parentRef | string | |
$setKey | bool | |
$depth | int |
getCriteriaValues
Retrieves the criteria values by combining the provided parameter values with the default values.
public getCriteriaValues(): voidIterates over the internal value index and, for each key and its associated values, constructs a criteria value array. If a value for the key exists in the provided $paramValues array, it is used; otherwise, the default value is used.
prepareCriteriaValues
Prepares criteria values for use in SQL queries by mapping parameter values to indexed keys.
public prepareCriteriaValues(array $paramValues): voidParameters
| Parameter | Type | Description |
|---|---|---|
$paramValues | array |
returning
public returning(mixed $columns): selfParameters
| Parameter | Type | Description |
|---|---|---|
$columns | mixed |
onConflict
public onConflict(null $target, null $update): selfParameters
| Parameter | Type | Description |
|---|---|---|
$target | null | |
$update | null |
prepareValueAction
private prepareValueAction(string $action, mixed $value): mixedParameters
| Parameter | Type | Description |
|---|---|---|
$action | string | |
$value | mixed |
prepareCriteriaAction
private prepareCriteriaAction(string $action, mixed $value, string $key, ?string $tissue = '=', bool $setKey = true, int $depth, null $): voidParameters
| Parameter | Type | Description |
|---|---|---|
$action | string | |
$value | mixed | |
$key | string | |
$tissue | string | |
$setKey | bool | |
$depth | int | |
$ | null |
tables
private tables(): voidprepareOrder
private prepareOrder(string $orderDefinition): stringParameters
| Parameter | Type | Description |
|---|---|---|
$orderDefinition | string |
toSELECTString
private toSELECTString(): stringtoINSERTString
private toINSERTString(): stringtoUPDATEString
private toUPDATEString(): stringtoDELETEString
private toDELETEString(): stringtoTRUNCATEString
private toTRUNCATEString(): stringGenerated by Hazaar API Doc Generator on Tue, 21 Apr 2026 04:00:24 +0000