final class InsertSelect
    implements Modification
    uses Snapshot
Public API: explicitly declared with @visibility public.

An INSERT of the rows of a query.

Mirrors PostgreSQL's InsertStmt whose selectStmt is a query other than a bare VALUES list (a VALUES list with WITH, ORDER BY, LIMIT or locking clauses is such a query). The query does not see the target table; its output columns are assigned to the target columns in order. The facts follow PG-INSERT-001. Source: https://www.postgresql.org/docs/17/sql-insert.html. Status: Implemented.

Reading the query of an INSERTdoctest
$insert = (new \SqlSemantics\Platform\PostgreSql\Semantics())->analyze('INSERT INTO t (a) SELECT 1 ON CONFLICT DO NOTHING');
    [$insert->statement->source instanceof \SqlSemantics\Platform\PostgreSql\Statement\Query\Select, $insert->toString()] // => [true, 'INSERT INTO t (a) SELECT 1 ON CONFLICT DO NOTHING']
Refusing a bare VALUES list, which is an INSERT of rowsdoctest
new \SqlSemantics\Platform\PostgreSql\Statement\Manipulation\InsertSelect(null, new \SqlSemantics\Platform\PostgreSql\Statement\Manipulation\TargetTable(new \SqlSemantics\Platform\PostgreSql\Statement\Name\RelationReference(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\QualifiedName(new \SqlSemantics\Platform\PostgreSql\Model\Statement\Identifier\Name('t')))), [], null, new \SqlSemantics\Platform\PostgreSql\Statement\Query\ValuesList([new \SqlSemantics\Platform\PostgreSql\Statement\Query\Clause\ValuesRow([new \SqlSemantics\Platform\PostgreSql\Statement\Literal\NullLiteral()])])) // throws \SqlSemantics\Platform\PostgreSql\Model\Diagnostic\InvalidConstruction

Properties§

public list<ColumnTarget> $columns
public list<Target> $returning
public CommonTables|null $with
public TargetTable $target
public Overriding|null $overriding
public Query $source
public ConflictDoNothing|ConflictDoUpdate|null $conflict = null

Methods§

public function __construct(
    public CommonTables|null $with,
    public TargetTable $target,
    list<ColumnTarget> $columns,
    public Overriding|null $overriding,
    public Query $source,
    public ConflictDoNothing|ConflictDoUpdate|null $conflict = null,
    list<Target> $returning = [],
)

Parameters

$withCommonTables|nullThe WITH clause
$targetTargetTableThe table written; never with ONLY
$columnslist<ColumnTarget>The column list in written order
$overridingOverriding|nullWhose values identity columns receive
$sourceQueryThe query whose rows are inserted: a selection, TABLE, a set operation, a query with clauses, or a parenthesized query that is not VALUES alone
$conflictConflictDoNothing|ConflictDoUpdate|nullThe ON CONFLICT clause
$returninglist<Target>The RETURNING list in written order

Throws

InvalidConstruction When the table is written with ONLY, the source is not such a query, or a list holds a foreign item
Calls 6
public function returnsRows(): bool

Tells whether the statement has a RETURNING list.

Returns

bool
public function commonTables(): ?CommonTables

Answers the WITH clause written before the statement.

Returns

public function deriveStatement(Derivation $derivation): void

Derives the statement as a root and records the rows of RETURNING as the output.

Parameters

$derivationDerivation
Calls 2
public function deriveQuery(Derivation $derivation, Environment $outer): QueryFact

Derives every part against an outer environment and answers the rows of RETURNING.

Parameters

$derivationDerivation
$outerEnvironment

Returns

Calls 2
public function render(Output $out): void

Writes the statement.

Parameters

$outOutput
Calls 3

Test cases 3§

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

Dedicated tests 1
Other tests reaching this symbol 2

Relations§

Instantiated in 1
Type declarations 2