Skip to content

StatementFactory

Alexey Borzov edited this page Feb 25, 2025 · 4 revisions

StatementFactory helper class

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.

Setup methods

  • __construct(Parser $parser = null, StatementToStringWalker $builder = null, bool $PDOCompatible = false). StatementToStringWalker here is an interface implemented by SqlBuilderWalker, $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 a StatementFactory based on properties of native connection;
    • StatementFactory::forPDO(\PDO $pdo): self - sets up a StatementFactory based on properties of PDO connection (and with compatibility enabled).
  • getParser(): Parser - Returns the Parser 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 by Parser will follow server's standard_conforming_strings setting.
  • If client_encoding is anything except UTF-8 then SqlBuilderWalker will have escape_unicode enabled.
  • Additionally, Parser will reuse the metadata cache used by Connection for caching ASTs.

Conversion methods

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 of Statement subclass, already having a instance of Parser added to it (so it can accept strings as query parts). Throws SyntaxException on parse failure.
  • createFromAST(Statement $ast): NativeStatement - Creates an object containing SQL statement string and parameter mappings from AST. The returned NativeStatement object can be easily cached to prevent re-running expensive parsing and building operations.

Creating Statements

The following methods are wrappers around Statement subclasses' constructors:

  • delete(string|nodes\range\UpdateOrDeleteTarget $from): Delete - Creates a DELETE statement object. This method accepts a string unlike Delete's constructor and the resultant object has Parser already added.
  • insert(string|nodes\QualifiedName|nodes\range\InsertTarget $into): Insert - Creates an INSERT statement object. This method accepts a string unlike Insert's constructor and the resultant object has Parser already added.
  • select(string|array|nodes\lists\TargetList $list, string|array|nodes\lists\FromList $from = null): Select - Creates a SELECT statement object. Arguments can be strings or arrays of strings and the resultant object has Parser already added.
  • update(string|nodes\range\UpdateOrDeleteTarget $table, string|array|nodes\lists\SetClauseList $set): Update - Creates an UPDATE statement object. Arguments can be strings, $set can be an array of strings and the resultant object has Parser already added.
  • values(string|array|nodes\lists\RowList $rows): Values - Creates a VALUES statement object. Argument can be a string or an array of strings and the resultant object has Parser already added.