import {ITable, TableUtil, InsertableTable} from "../table" import {RawExprNoUsedRef} from "../raw-expr"; import * as InsertUtil from "./util"; import {IConnection} from "../execution"; import {Row} from "../row"; export type InsertRow = ( { [name in TableUtil.RequiredColumnNames] : ( RawExprNoUsedRef< ReturnType > ) } & { [name in TableUtil.OptionalColumnNames]? : ( RawExprNoUsedRef< ReturnType > ) } ); export type InsertRowLiteral = ( { [name in TableUtil.RequiredColumnNames] : ( ReturnType ) } & { [name in TableUtil.OptionalColumnNames]? : ( ReturnType ) } ); export enum InsertModifier { IGNORE = "IGNORE", REPLACE = "REPLACE", } export interface InsertData { readonly _table : InsertableTable, readonly _values : InsertRow[]|undefined, readonly _modifier : InsertModifier|undefined, } export interface IInsert { readonly _table : DataT["_table"]; readonly _values : DataT["_values"]; readonly _modifier : DataT["_modifier"]; } export class Insert implements IInsert { readonly _table : DataT["_table"]; readonly _values : DataT["_values"]; readonly _modifier : DataT["_modifier"]; constructor ( data : DataT ) { if (!data._table.insertAllowed) { throw new Error(`Cannot insert into table ${data._table.alias}`); } this._table = data._table; this._values = data._values; this._modifier = data._modifier; } values ( ...values : InsertRow[] ) : InsertUtil.Values { return InsertUtil.values(this, ...values); } execute ( this : Extract[] }>, connection : IConnection ) : ( Promise[] }> >> ) { return InsertUtil.execute(this, connection) as any; } executeAndFetch ( this : Extract[] }>, connection : ( IConnection & TableUtil.AssertHasCandidateKey ) ) : ( Promise> ) { return InsertUtil.executeAndFetch(this, connection) as any; } printSql ( this : Extract[] }> ) : this { InsertUtil.printSql(this); return this; } } export type ExecutableInsert = IInsert<{ readonly _table : InsertableTable, readonly _values : InsertRow[], readonly _modifier : InsertModifier|undefined, }>;