diff --git a/examples/jobs-mssql/jobs-mssql-sqla_test.fixture-01.sql b/examples/jobs-mssql/jobs-mssql-sqla_test.fixture-01.sql index cc649cfd..a64501d8 100644 --- a/examples/jobs-mssql/jobs-mssql-sqla_test.fixture-01.sql +++ b/examples/jobs-mssql/jobs-mssql-sqla_test.fixture-01.sql @@ -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, diff --git a/pattern/data-vault/data-vault.ts b/pattern/data-vault/data-vault.ts index 8e7b8752..d042f4aa 100644 --- a/pattern/data-vault/data-vault.ts +++ b/pattern/data-vault/data-vault.ts @@ -131,6 +131,7 @@ export function dataVaultGovn( // 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, diff --git a/pattern/typical/enum-table.ts b/pattern/typical/enum-table.ts index 4fa3aa36..4f7b54b1 100644 --- a/pattern/typical/enum-table.ts +++ b/pattern/typical/enum-table.ts @@ -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 = { @@ -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 = { @@ -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 = { diff --git a/render/dml/insert.ts b/render/dml/insert.ts index 5344574f..555d634b 100644 --- a/render/dml/insert.ts +++ b/render/dml/insert.ts @@ -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; @@ -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, @@ -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; } @@ -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][][] = []; @@ -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, @@ -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; @@ -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 { @@ -294,7 +304,7 @@ 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, @@ -302,7 +312,8 @@ export function typicalInsertStmtSqlPreparerSync< ir, names, values, - ns, + tns, + cns, ctx, ) : SQL;