Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | 131x 131x 594591x 131x 3966x 3966x 3966x 7090x 4x 10x 3966x 20503x 20496x 21055x 142467x 3966x 3966x 3966x 135556x 452136x 452136x 452136x | import {
SqlAst,
type ExprNode,
type InsertNode,
type OnConflictNode,
type QueryNode,
} from '../sql/ast.ts';
import type { AttributeColumns } from './attribute-columns.ts';
import type { AttributeDefinition, ModelDefinition } from './model-definition.ts';
const ast = new SqlAst();
const isAutoIncrementEmpty = (
attribute: AttributeDefinition | undefined,
value: unknown,
): boolean => attribute?.autoIncrement === true && (value === null || value === undefined);
/** Options of {@link InsertQueryBuilder.build}. */
export type InsertBuildOptions = Readonly<{
/** `true`: all stored columns; a list: these expressions. */
returning?: boolean | readonly ExprNode[];
/** `ON CONFLICT` clause. */
onConflict?: OnConflictNode;
/** `OVERRIDING { SYSTEM | USER } VALUE`. */
overriding?: 'system' | 'user';
}>;
/** Builds INSERT statements: multi-row `VALUES` from attribute-keyed rows, or `INSERT ... SELECT`. */
export class InsertQueryBuilder {
readonly #columns: AttributeColumns;
/**
* @param columns - Attribute to column mapping.
*/
constructor(columns: AttributeColumns) {
this.#columns = columns;
}
/**
* Builds a multi-row INSERT from attribute-keyed rows. Columns no row provides are left out;
* missing values and empty auto-increment keys are written as `DEFAULT`.
*
* @param definition - Model definition.
* @param rows - Values by attribute name.
* @param options - RETURNING, ON CONFLICT, OVERRIDING.
* @returns INSERT node.
*/
build(
definition: ModelDefinition,
rows: readonly Readonly<Record<string, unknown>>[],
options: InsertBuildOptions = {},
): InsertNode {
const attributes = this.#insertable(definition, rows);
const returning = this.#returning(definition, options.returning);
return {
kind: 'insert',
table: this.#columns.table(
definition,
options.onConflict === undefined ? undefined : definition.name,
),
columns: attributes.map((name) => definition.attributes.get(name)?.field ?? name),
rows: this.#rows(definition, rows, attributes),
...(options.onConflict === undefined ? {} : { onConflict: options.onConflict }),
...(options.overriding === undefined ? {} : { overriding: options.overriding }),
...(returning === undefined ? {} : { returning }),
};
}
/**
* Builds `INSERT INTO ... (columns) SELECT ...`.
*
* @param definition - Model definition.
* @param columns - Target attribute names.
* @param select - Source query.
* @param onConflict - `ON CONFLICT` clause.
* @param returning - RETURNING list.
* @returns INSERT node.
*/
fromSelect(
definition: ModelDefinition,
columns: readonly string[],
select: QueryNode,
onConflict: OnConflictNode | undefined,
returning: readonly ExprNode[],
): InsertNode {
return {
kind: 'insert' as const,
table: this.#columns.table(
definition,
onConflict === undefined ? undefined : definition.name,
),
columns: columns.map((name) => definition.attributes.get(name)?.field ?? name),
rows: [],
select,
...(onConflict === undefined ? {} : { onConflict }),
returning,
};
}
#insertable(
definition: ModelDefinition,
rows: readonly Readonly<Record<string, unknown>>[],
): readonly string[] {
return this.#columns
.stored(definition)
.filter((attribute) => attribute.generated === undefined)
.map((attribute) => attribute.fieldName)
.filter((name) => rows.some((row) => row[name] !== undefined))
.filter(
(name) =>
!rows.every((row) => isAutoIncrementEmpty(definition.attributes.get(name), row[name])),
);
}
#returning(
definition: ModelDefinition,
returning: InsertBuildOptions['returning'],
): readonly ExprNode[] | undefined {
return returning === true
? this.#columns.returningAll(definition)
: returning === false
? undefined
: returning;
}
#rows(
definition: ModelDefinition,
rows: readonly Readonly<Record<string, unknown>>[],
attributes: readonly string[],
): readonly (readonly ExprNode[])[] {
const ctx = this.#columns.whereCompiler(definition, undefined);
return rows.map((row) =>
attributes.map((name) => {
const attribute = definition.attributes.get(name);
const value = row[name];
return value === undefined || isAutoIncrementEmpty(attribute, value)
? ast.raw('DEFAULT')
: this.#columns.value(attribute, value, ctx);
}),
);
}
}
|