-
Notifications
You must be signed in to change notification settings - Fork 6
StatementFactory
Alexey Borzov edited this page Feb 25, 2025
·
4 revisions
Warning
These docs are outdated and are no longer maintained. The manual is now on pg-builder.readthedocs.io
sad_spirit\pg_builder\StatementFactory
is, as its name implies, a class that is used for creating statements. It also takes care of properly configuring objects needed to create these statements based on current PostgreSQL connection, if one is available.
-
__construct(Parser $parser = null, StatementToStringWalker $builder = null, bool $PDOCompatible = false)
.StatementToStringWalker
here is an interface implemented bySqlBuilderWalker
,$PDOCompatible
flag triggers generating queries targeting PDO rather than native pgsql extension. If$parser
and$builder
are not provided, default implementations will be used. There are also two "named constructors":-
StatementFactory::forConnection(\pg_wrapper\Connection $connection): self
- sets up aStatementFactory
based on properties of native connection; -
StatementFactory::forPDO(\PDO $pdo): self
- sets up aStatementFactory
based on properties of PDO connection (and with compatibility enabled).
-
-
getParser(): Parser
- Returns theParser
for converting SQL fragments to ASTs. -
getBuilder(): StatementToStringWalker
- Returns the SQL builder object.
When using a named constructor, the following settings will be configured from connection:
-
Lexer
instance used byParser
will follow server'sstandard_conforming_strings
setting. - If
client_encoding
is anything exceptUTF-8
thenSqlBuilderWalker
will haveescape_unicode
enabled. - Additionally,
Parser
will reuse the metadata cache used byConnection
for caching ASTs.
These use Parser
and SqlBuilderWalker
to convert query from SQL string to AST and back:
-
createFromString(string $sql): Statement
- Creates an AST representing a complete statement from SQL string. Returns an instance ofStatement
subclass, already having a instance ofParser
added to it (so it can accept strings as query parts). ThrowsSyntaxException
on parse failure. -
createFromAST(Statement $ast): NativeStatement
- Creates an object containing SQL statement string and parameter mappings from AST. The returnedNativeStatement
object can be easily cached to prevent re-running expensive parsing and building operations.
The following methods are wrappers around Statement
subclasses' constructors:
-
delete(string|nodes\range\UpdateOrDeleteTarget $from): Delete
- Creates aDELETE
statement object. This method accepts a string unlikeDelete
's constructor and the resultant object hasParser
already added. -
insert(string|nodes\QualifiedName|nodes\range\InsertTarget $into): Insert
- Creates anINSERT
statement object. This method accepts a string unlikeInsert
's constructor and the resultant object hasParser
already added. -
select(string|array|nodes\lists\TargetList $list, string|array|nodes\lists\FromList $from = null): Select
- Creates aSELECT
statement object. Arguments can be strings or arrays of strings and the resultant object hasParser
already added. -
update(string|nodes\range\UpdateOrDeleteTarget $table, string|array|nodes\lists\SetClauseList $set): Update
- Creates anUPDATE
statement object. Arguments can be strings,$set
can be an array of strings and the resultant object hasParser
already added. -
values(string|array|nodes\lists\RowList $rows): Values
- Creates aVALUES
statement object. Argument can be a string or an array of strings and the resultant object hasParser
already added.