Skip to content

Commit

Permalink
fix: honor schema names in insert statements
Browse files Browse the repository at this point in the history
  • Loading branch information
shah committed Jun 19, 2024
1 parent b0fd63e commit d8eb8bd
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 11 deletions.
10 changes: 5 additions & 5 deletions examples/jobs-mssql/jobs-mssql-sqla_test.fixture-01.sql
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
[created_at] DATETIME2 DEFAULT GETDATE()
);

INSERT INTO [job_position_status] ([code], [value]) VALUES ('Current', 'Current');
INSERT INTO [job_position_status] ([code], [value]) VALUES ('Past', 'Past');
INSERT INTO [job_position_status] ([code], [value]) VALUES ('Future', 'Future');
INSERT INTO [job_position_status] ([code], [value]) VALUES ('Draft', 'Draft');
INSERT INTO [job_position_status] ([code], [value]) VALUES ('Archive', 'Archive');
INSERT INTO [dbo].[job_position_status] ([code], [value]) VALUES ('Current', 'Current');
INSERT INTO [dbo].[job_position_status] ([code], [value]) VALUES ('Past', 'Past');
INSERT INTO [dbo].[job_position_status] ([code], [value]) VALUES ('Future', 'Future');
INSERT INTO [dbo].[job_position_status] ([code], [value]) VALUES ('Draft', 'Draft');
INSERT INTO [dbo].[job_position_status] ([code], [value]) VALUES ('Archive', 'Archive');

CREATE TABLE [dbo].[job_position] (
[job_position_id] INTEGER IDENTITY(1,1) PRIMARY KEY,
Expand Down
1 change: 1 addition & 0 deletions pattern/data-vault/data-vault.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export function dataVaultGovn<Context extends SQLa.SqlEmitContext>(
// to emit it as part of the an insert DML SQL statement
isColumnEmittable: (name) =>
name == "created_at" || name == "created_by" || name == "provenance",
sqlNS: ddlOptions?.sqlNS,
};
return result as SQLa.InsertStmtPreparerOptions<
Any,
Expand Down
3 changes: 3 additions & 0 deletions pattern/typical/enum-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export function ordinalEnumTable<
// emit it as part of the insert DML statement
defaultIspOptions: {
isColumnEmittable: (name) => name == "created_at" ? false : true,
sqlNS: tdOptions?.sqlNS,
},
});
const etn: EnumTableDefn<Context> = {
Expand Down Expand Up @@ -278,6 +279,7 @@ export function textEnumTable<
// emit it as part of the insert DML statement
defaultIspOptions: {
isColumnEmittable: (name) => name == "created_at" ? false : true,
sqlNS: tdOptions?.sqlNS,
},
});
const etn: EnumTableDefn<Context> = {
Expand Down Expand Up @@ -434,6 +436,7 @@ export function varCharEnumTable<
// emit it as part of the insert DML statement
defaultIspOptions: {
isColumnEmittable: (name) => name == "created_at" ? false : true,
sqlNS: tdOptions?.sqlNS,
},
});
const etn: EnumTableDefn<Context> = {
Expand Down
23 changes: 17 additions & 6 deletions render/dml/insert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as safety from "../../lib/universal/safety.ts";
import * as r from "../../lib/universal/record.ts";
import * as tmpl from "../emit/sql.ts";
import * as d from "../domain/mod.ts";
import * as ns from "../emit/namespace.ts";

// deno-lint-ignore no-explicit-any
type Any = any;
Expand All @@ -26,6 +27,7 @@ export interface InsertStmtPreparerOptions<
DomainQS extends d.SqlDomainQS,
InsertableColumnName extends keyof InsertableRecord = keyof InsertableRecord,
> {
readonly sqlNS?: ns.SqlNamespaceSupplier;
readonly isColumnEmittable?: (
columnName: keyof InsertableRecord,
record: InsertableRecord,
Expand Down Expand Up @@ -73,7 +75,8 @@ export interface InsertStmtPreparerOptions<
records: InsertableRecord | InsertableRecord[],
names: InsertableColumnName[],
values: [value: unknown, sqlText: string][][],
ns: tmpl.SqlObjectNames,
tableNS: tmpl.SqlObjectNames,
columnsNS: tmpl.SqlObjectNames,
ctx: Context,
) => string;
}
Expand Down Expand Up @@ -159,6 +162,7 @@ export function typicalInsertValuesSqlPreparerSync<
const { sqlTextEmitOptions: eo } = ctx;
const ns = ctx.sqlNamingStrategy(ctx, {
quoteIdentifiers: true,
qnss: ispOptions?.sqlNS,
});
const names: InsertableColumnName[] = [];
const values: [value: unknown, valueSqlText: string][][] = [];
Expand Down Expand Up @@ -242,7 +246,13 @@ export function typicalInsertStmtSqlPreparerSync<
return {
SQL: (ctx) => {
const { returning: returningArg, where, onConflict } = ispOptions ?? {};
const ns = ctx.sqlNamingStrategy(ctx, { quoteIdentifiers: true });
const tns = ctx.sqlNamingStrategy(ctx, {
quoteIdentifiers: true,
qnss: ispOptions?.sqlNS,
});
const cns = ctx.sqlNamingStrategy(ctx, {
quoteIdentifiers: true,
});
const { names, values } = typicalInsertValuesSqlPreparerSync(
ctx,
ir,
Expand Down Expand Up @@ -273,7 +283,7 @@ export function typicalInsertStmtSqlPreparerSync<
case "primary-keys":
returningSQL = ` RETURNING ${
candidateColumns("primary-keys").map((isd) =>
ns.tableColumnName({ tableName, columnName: isd.identity })
cns.tableColumnName({ tableName, columnName: isd.identity })
).join(", ")
}`;
break;
Expand All @@ -282,7 +292,7 @@ export function typicalInsertStmtSqlPreparerSync<
if (returning.columns) {
returningSQL = ` RETURNING ${
returning!.columns!.map((n) =>
ns.tableColumnName({ tableName, columnName: String(n) })
cns.tableColumnName({ tableName, columnName: String(n) })
).join(", ")
}`;
} else {
Expand All @@ -294,15 +304,16 @@ export function typicalInsertStmtSqlPreparerSync<
`(${row.map((value) => value[1]).join(", ")})`
).join(",\n ");
// deno-fmt-ignore
const SQL = `INSERT INTO ${ns.tableName(tableName)} (${names.map(n => ns.tableColumnName({ tableName, columnName: String(n) })).join(", ")})${multiValues ? "\n " : " "}VALUES ${valuesClause}${sqlText(where)}${sqlText(onConflict)}${returningSQL}`;
const SQL = `INSERT INTO ${tns.tableName(tableName)} (${names.map(n => cns.tableColumnName({ tableName, columnName: String(n) })).join(", ")})${multiValues ? "\n " : " "}VALUES ${valuesClause}${sqlText(where)}${sqlText(onConflict)}${returningSQL}`;
return ispOptions?.transformSQL
? ispOptions?.transformSQL(
SQL,
tableName,
ir,
names,
values,
ns,
tns,
cns,
ctx,
)
: SQL;
Expand Down

0 comments on commit d8eb8bd

Please sign in to comment.