classInsertSelect
final class InsertSelect
implements Modification
uses Snapshot
@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.
$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']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\InvalidConstructionProperties§
public list<ColumnTarget> $columnspublic list<Target> $returningpublic CommonTables|null $withpublic TargetTable $targetpublic Overriding|null $overridingpublic Query $sourcepublic ConflictDoNothing|ConflictDoUpdate|null $conflict = nullMethods§
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
$with | CommonTables|null | The WITH clause |
$target | TargetTable | The table written; never with ONLY |
$columns | list<ColumnTarget> | The column list in written order |
$overriding | Overriding|null | Whose values identity columns receive |
$source | Query | The query whose rows are inserted: a selection, TABLE, a set operation, a query with clauses, or a parenthesized query that is not VALUES alone |
$conflict | ConflictDoNothing|ConflictDoUpdate|null | The ON CONFLICT clause |
$returning | list<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 itemCalls 6
- static-call Check::input() line 75
- method-call Sources::insertable() line 76
- new Sources line 76
- static-call Check::listOf() line 77
- class-const ColumnTarget::class() line 77
- class-const Target::class() line 78
public function returnsRows(): boolTells whether the statement has a RETURNING list.
Returns
boolpublic function commonTables(): ?CommonTablesAnswers the WITH clause written before the statement.
Returns
public function deriveStatement(Derivation $derivation): voidDerives the statement as a root and records the rows of RETURNING as the output.
Parameters
$derivation | Derivation |
Calls 2
- method-call Roots::derive() line 102
- new Roots line 102
public function deriveQuery(Derivation $derivation, Environment $outer): QueryFactDerives every part against an outer environment and answers the rows of RETURNING.
Parameters
$derivation | Derivation | |
$outer | Environment |
Returns
Calls 2
- method-call InsertFacts::select() line 110
- new InsertFacts line 110
public function render(Output $out): voidWrites the statement.
Parameters
$out | Output |
Calls 3
- method-call Output::node() line 118
- method-call Output::symbol() line 120
- method-call Output::keyword() line 123
Test cases 3§
Test cases that cover or call this symbol, from the coverage report and from the analyzed test sources.