final class CreateTableAs
    implements Statement
    uses Snapshot
Public API: explicitly declared with @visibility public.

A request to create a table from the rows of a query.

Mirrors PostgreSQL's CreateTableAsStmt with objtype TABLE (query, into, if_not_exists) and skipData (WITH [NO] DATA). The statement provides one declaration (PG-QUERY-TABLE-001): a column per output column of the query, renamed by the written column names, able to be NULL, with the system columns. WITH [NO] DATA after a query that ends with an IS JSON test without a uniqueness clause is rejected: the test would take the WITH (PG-QUERY-TRAILING-001). Source: https://www.postgresql.org/docs/17/sql-createtableas.html.

Declaring the table of a querydoctest
$create = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze("CREATE TEMP TABLE t (n) AS SELECT 1, 'x' AS s WITH NO DATA");
    $table = $create->declarations()[0];
    [$table->name->schema->value, array_map(static fn ($column) => $column->name->value . ' ' . $column->type->name(), $table->columns)] // => ['pg_temp', ['n integer', 's text']]

Properties§

public CreateTarget $target
public Query&Statement $query
public Persistence $persistence = Persistence::Permanent
public bool $ifNotExists = false
public bool|null $withData = null

Methods§

public function __construct(
    public CreateTarget $target,
    public Query&Statement $query,
    public Persistence $persistence = Persistence::Permanent,
    public bool $ifNotExists = false,
    public bool|null $withData = null,
)

Parameters

$targetCreateTargetThe new table
$queryQuery&StatementThe query
$persistencePersistenceThe persistence
$ifNotExistsboolWhether IF NOT EXISTS is written
$withDatabool|nullTrue for WITH DATA, false for WITH NO DATA, null when not written
Calls 4
public function deriveStatement(Derivation $derivation): void

Derives the query and provides the declaration of its table.

Parameters

$derivationDerivation
Calls 9
public function render(Output $out): void

Writes the statement.

Parameters

$outOutput
Calls 4

Test cases 1§

Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.

Dedicated tests 1

Relations§

Instantiated in 1
Type declarations 1