-
Notifications
You must be signed in to change notification settings - Fork 6
Statement
Warning
These docs are outdated and are no longer maintained. The manual is now on pg-builder.readthedocs.io
Subclasses of \sad_spirit\pg_builder\Statement
represent the complete SQL statements.
Note that all relative class names below assume \sad_spirit\pg_builder\
prefix, which is omitted for readability.
-
Node
-
Statement
- Abstract base class for statements. Loosely corresponds toPreparableStmt
production in PostgreSQL's grammar.-
Delete
- RepresentsDELETE
statement. -
Insert
- RepresentsINSERT
statement. -
Update
- RepresentsUPDATE
statement. -
SelectCommon
- Abstract base class forSELECT
-type statements.-
Select
- Represents a simpleSELECT
statement. -
SetOpSelect
- Represents a set operator (UNION
,INTERSECT
,EXCEPT
) applied to twoSELECT
statements. -
Values
- RepresentsVALUES
statement. Note that this can be an independent statement in PostgreSQL, not only a part ofINSERT
.
-
-
-
Child nodes of Statement
s (representing SQL clauses) are exposed as properties of the relevant objects. Those properties will be writable if a corresponding setProperty()
method is defined for $property
and read-only (set only via constructor) otherwise.
-
$with: nodes\WithClause
- represents aWITH
clause containing Common Table Expressions attached to a primary statement. Has a correspondingsetWith()
method. -
setParser(Parser $parser)
- Sets the parser instance to use. If you add aParser
toStatement
then you'll be able to add parts of query as strings that will be parsed automatically. -
getParser(): ?Parser
- Returns the parser instance, if available.
It is always possible to add query parts by instantiating the relevant Node
subclasses manually, but this is very tedious:
$select->list[] = new nodes\TargetElement(new nodes\ColumnReference('foo', 'bar'), new nodes\Identifier('alias'));
vs
$select->list[] = 'foo.bar as alias';
the result of the above is the same, as the string will be parsed and an instance of TargetElement
added. Usually all setProperty()
methods accept a string as its argument and all NodeList
subclasses allow strings to be set for array offsets.
This is a subclass of NodeList
so individual CTEs (instances of nodes\CommonTableExpression
) are accessible as array offsets. It also has a boolean $recursive
property with a corresponding setRecursive()
method.
$select->with[] = 'foobar as (select foo.*, bar.* from foo natural join bar)';
echo "WITH clause is " . ($select->with->recursive ? 'recursive' : 'not recursive');
echo "Statement of first CTE is " . get_class($select->with[0]->statement);
-
$relation: nodes\range\UpdateOrDeleteTarget
- name of the table to delete from. Can be set only via constructor. -
$using: nodes\lists\FromList
- list of tables whose columns may appear inWHERE
clause.FromList
is a subclass ofNodeList
and behaves like an array containing only instances ofnodes\range\FromElement
. While the$using
property itself cannot be replaced all its children can. -
$where: nodes\WhereOrHavingClause
-WHERE
clause ofDELETE
.$where
property is not writable, use its methods to build theWHERE
clause. -
$returning: nodes\lists\TargetList
-RETURNING
clause ofDELETE
, if presentDELETE
will return values based on each deleted row.TargetList
is essentially an array containing only instances ofnodes\TargetElement
.
-
$relation: nodes\range\InsertTarget
- name of the table to insert into. Can be set only via constructor. -
$cols: nodes\lists\SetTargetList
- list of table's columns to use.TargetList
is essentially an array containing only instances ofnodes\SetTargetElement
. -
$values: SelectCommon
- actual values to insert. Has a correspondingsetValues()
clause. -
$overriding: string|null
-OVERRIDING
clause. The property can contain onlysystem
oruser
string and has correspondingsetOverriding()
method accepting either of these two values (or null). -
$onConflict: nodes\OnConflictClause
-ON CONFLICT
clause used to specify an alternative action to raising a unique constraint or exclusion constraint violation error. Has a correspondingsetOnConflict()
method. -
$returning: nodes\lists\TargetList
-RETURNING
clause ofINSERT
, if presentINSERT
will return values based on each inserted (or maybe updated in case ofON CONFLICT ... DO UPDATE
) row.TargetList
is essentially an array containing only instances ofnodes\TargetElement
.
-
$relation: nodes\range\UpdateOrDeleteTarget
- name of the table to update. Can be set only via constructor. -
$set: nodes\lists\SetClauseList
-SET
clause ofUPDATE
statement.SetClauseList
is essentially an array containing only instances of eithernodes\SingleSetClause
ornodes\MultipleSetClause
. -
$from: nodes\lists\FromList
- list of tables whose columns may appear inWHERE
condition and the update expressions.FromList
is essentially an array containing only instances ofnodes\range\FromElement
. -
$where: WhereOrHavingClause
-WHERE
clause ofUPDATE
. -
$returning: nodes\lists\TargetList
-RETURNING
clause ofUPDATE
, if presentUPDATE
will return values based on each updated row.TargetList
is essentially an array containing only instances ofnodes\TargetElement
.
-
$order: nodes\lists\OrderByList
-ORDER BY
clause ofSELECT
statement.OrderByList
is essentially an array containing only instances ofnodes\OrderByElement
. -
$limit: nodes\ScalarExpression
-LIMIT
clause ofSELECT
statement. Has a correspondingsetLimit()
method. -
$limitWithTies
- If true, triggers generating SQL standardFETCH FIRST ... ROWS WITH TIES
clause. Has a correspondingsetLimitWithTies()
method. -
$offset: nodes\ScalarExpression
-OFFSET
clause ofSELECT
statement. Has a correspondingsetOffset()
method. -
$locking: nodes\lists\LockList
- Locking clause ofSELECT
statement, consisting of e.g.FOR UPDATE ...
clauses.LockList
is essentially an array containing only instances ofnodes\LockingElement
.
SelectCommon
also defines methods for applying set operators:
-
union($select, $distinct = true): SetOpSelect
- Combines thisSELECT
statement with another one usingUNION [ALL]
operator -
intersect($select, $distinct = true): SetOpSelect
- Combines thisSELECT
statement with another one usingINTERSECT [ALL]
operator -
except($select, $distinct = true): SetOpSelect
- Combines thisSELECT
statement with another one usingEXCEPT [ALL]
operator
If these methods are called on a SELECT
statement that is a part of some larger statement then result will replace the original statement:
use sad_spirit\pg_builder\{
StatementFactory,
Select
};
$factory = new StatementFactory();
/** @var Select $select */
$select = $factory->createFromString(
'select foo.*, bar.* from (select * from foosource) as foo, bar where foo.id = bar.id'
);
$select->from[0]->query->union('select * from othersource');
echo $factory->createFromAST($select)->getSql();
will output
select foo.*, bar.*
from (
select *
from foosource
union
select *
from othersource
) as foo, bar
where foo.id = bar.id
-
$list: nodes\lists\TargetList
- list of columns returned bySELECT
.TargetList
is essentially an array containing only instances ofnodes\TargetElement
. -
$distinct: bool|nodes\lists\ExpressionList
-true
here representsDISTINCT
clause, list of expressions -DISTINCT ON (...)
clause. Has a correspondingsetDistinct()
clause.ExpressionList
is essentially an array containing only objects implementingnodes\ScalarExpression
. -
$from: nodes\lists\FromList
- list of tables to select from.FromList
is essentially an array containing only instances ofnodes\range\FromElement
. -
$where: nodes\WhereOrHavingClause
-WHERE
clause ofSELECT
. -
group: nodes\group\GroupByClause
-GROUP BY
clause ofSELECT
.GroupByList
is essentially an array containing only objects implementing eithernodes\ScalarExpression
ornodes\group\GroupByElement
interface. -
$having: nodes\WhereOrHavingClause
-HAVING
clause ofSELECT
. -
$window: nodes\lists\WindowList
-WINDOW
clause ofSELECT
.WindowList
is essentially an array containing only instances ofnodes\WindowDefinition
.
-
$left: SelectCommon
- first operand of set operation. Has a correspondingsetLeft()
method. -
$right: SelectCommon
- second operand of set operation. Has a correspondingsetRight()
method. -
$operator: string
- operator, either ofunion [all]
,intersect [all]
,except [all]
. Can be set only via contructor.
-
$rows: nodes\lists\RowList
- list of rows inVALUES
.RowList
is essentially an array containing only instances ofnodes\expressions\RowExpression
.