Skip to content

Commit

Permalink
feat: enhance SQLPage content #6
Browse files Browse the repository at this point in the history
  • Loading branch information
shah committed Jan 4, 2024
1 parent b1bd8ea commit 1da20b4
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 94 deletions.
71 changes: 71 additions & 0 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/ahc-hrsn-elt/screening/csv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export class ScreeningCsvFileIngestSource<TableName extends string>
return {
ingestSQL: async (issac) => await this.ingestSQL(issac, sar),
assuranceSQL: async () => await this.assuranceSQL(sar),
exportResourceSQL: async (targetSchema) =>
await this.exportResourceSQL(targetSchema),
};
}

Expand Down Expand Up @@ -57,6 +59,12 @@ export class ScreeningCsvFileIngestSource<TableName extends string>
return govn.SQL`
${sar.tableRules.intValueInAllRows('SURVEY_ID')}`
}

// deno-lint-ignore require-await
async exportResourceSQL(targetSchema: string) {
const { govn: { SQL }, tableName } = this;
return SQL`CREATE TABLE ${targetSchema}.${tableName} AS SELECT * FROM ${tableName}`;
}
}

export function ingestCsvFilesSourcesSupplier(
Expand Down
16 changes: 8 additions & 8 deletions src/ahc-hrsn-elt/screening/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ export * as dax from "https://deno.land/x/[email protected]/mod.ts";
export * as safeText from "npm:string-ts";

// local: ../../../../../netspective-labs/sql-aide
// remote: https://raw.githubusercontent.com/netspective-labs/sql-aide/v0.11.23
// remote: https://raw.githubusercontent.com/netspective-labs/sql-aide/v0.11.24

export * as array from "https://raw.githubusercontent.com/netspective-labs/sql-aide/v0.11.23/lib/universal/array.ts";
export * as chainNB from "https://raw.githubusercontent.com/netspective-labs/sql-aide/v0.11.23/lib/notebook/chain-of-responsibility.ts";
export * as duckdb_shell from "https://raw.githubusercontent.com/netspective-labs/sql-aide/v0.11.23/lib/duckdb/mod.ts";
export * as ws from "https://raw.githubusercontent.com/netspective-labs/sql-aide/v0.11.23/lib/universal/whitespace.ts";
export * as array from "https://raw.githubusercontent.com/netspective-labs/sql-aide/v0.11.24/lib/universal/array.ts";
export * as chainNB from "https://raw.githubusercontent.com/netspective-labs/sql-aide/v0.11.24/lib/notebook/chain-of-responsibility.ts";
export * as duckdb_shell from "https://raw.githubusercontent.com/netspective-labs/sql-aide/v0.11.24/lib/duckdb/mod.ts";
export * as ws from "https://raw.githubusercontent.com/netspective-labs/sql-aide/v0.11.24/lib/universal/whitespace.ts";

export * as SQLa from "https://raw.githubusercontent.com/netspective-labs/sql-aide/v0.11.23/render/mod.ts";
export * as SQLa_ingest_duckdb from "https://raw.githubusercontent.com/netspective-labs/sql-aide/v0.11.23/pattern/ingest/duckdb/mod.ts";
export * as SQLa_sqlpage from "https://raw.githubusercontent.com/netspective-labs/sql-aide/v0.11.23/pattern/sqlpage/mod.ts";
export * as SQLa from "https://raw.githubusercontent.com/netspective-labs/sql-aide/v0.11.24/render/mod.ts";
export * as SQLa_ingest_duckdb from "https://raw.githubusercontent.com/netspective-labs/sql-aide/v0.11.24/pattern/ingest/duckdb/mod.ts";
export * as SQLa_sqlpage from "https://raw.githubusercontent.com/netspective-labs/sql-aide/v0.11.24/pattern/sqlpage/mod.ts";
15 changes: 15 additions & 0 deletions src/ahc-hrsn-elt/screening/excel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,13 @@ export class ExcelSheetTodoIngestSource<SheetName extends string>
${await issac.sessionEntryInsertDML()}
${await issac.issueInsertDML(`Excel workbook '${path.basename(this.uri)}' sheet '${this.sheetName}' has not been implemented yet.`, "TODO")}`,

assuranceSQL: () =>
this.govn.SQL`-- Sheet '${this.sheetName}' ingestion not implemented.`,

exportResourceSQL: (targetSchema: string) =>
this.govn.SQL`
-- Sheet '${this.sheetName}' exportResourceSQL(${targetSchema})`,
};
}
}
Expand Down Expand Up @@ -72,6 +77,8 @@ export class ScreeningExcelSheetIngestSource<TableName extends string>
return {
ingestSQL: async (issac) => await this.ingestSQL(issac, sar),
assuranceSQL: async () => await this.assuranceSQL(),
exportResourceSQL: async (targetSchema) =>
await this.exportResourceSQL(targetSchema),
};
}

Expand Down Expand Up @@ -104,6 +111,14 @@ export class ScreeningExcelSheetIngestSource<TableName extends string>
// deno-fmt-ignore
return this.govn.SQL`-- Sheet '${this.sheetName}' has no assurance SQL in Excel workbook '${path.basename(this.uri)}'`;
}

// deno-lint-ignore require-await
async exportResourceSQL(targetSchema: string) {
const { govn } = this;

// deno-fmt-ignore
return govn.SQL`-- Sheet '${this.sheetName}' exportResourceSQL(${targetSchema})`;
}
}

export function ingestExcelSourcesSupplier(
Expand Down
23 changes: 18 additions & 5 deletions src/ahc-hrsn-elt/screening/ingest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,15 @@ export class IngestEngine {
);

await this.duckdb.execute(assurableSQL.join("\n"), isc.current.nbCellID);
return ingestResult;
}

async emitResources(isc: IngestStepContext) {
async emitResources(
isc: IngestStepContext,
ingestResult: Awaited<
ReturnType<typeof IngestEngine.prototype.ensureContent>
>,
) {
const { args: { resourceDb, session }, govn: { emitCtx: ctx } } = this;
if (resourceDb) {
try {
Expand All @@ -327,6 +333,13 @@ export class IngestEngine {

const beforeFinalize = session.sqlCatalogSqlText("before-finalize");
const afterFinalize = session.sqlCatalogSqlText("after-finalize");
const rdbSchemaName = "resource_db";

const exportsSQL = await Promise.all(
ingestResult.map(async (sr) =>
(await sr.workflow.exportResourceSQL(rdbSchemaName)).SQL(ctx)
),
);

// `beforeFinalize` SQL includes state management SQL that log all the
// state changes between this notebooks' cells; however, the "exit" state
Expand All @@ -341,13 +354,13 @@ export class IngestEngine {
-- emit all the SQLPage content
${((await spc.sqlCells()).map(sc => sc.SQL(ctx))).join(";\n\n")};
ATTACH '${resourceDb}' AS resource_db (TYPE SQLITE);
ATTACH '${resourceDb}' AS ${rdbSchemaName} (TYPE SQLITE);
${adminTables.map(t => `CREATE TABLE resource_db.${t.tableName} AS SELECT * FROM ${t.tableName}`).join(";\n ")};
${adminTables.map(t => `CREATE TABLE ${rdbSchemaName}.${t.tableName} AS SELECT * FROM ${t.tableName}`).join(";\n ")};
-- {contentResult.map(cr => \`CREATE TABLE resource_db.\${cr.iaSqlSupplier.tableName} AS SELECT * FROM \${cr.tableName}\`).join(";")};
${exportsSQL.join(";\n ")};
DETACH DATABASE resource_db;
DETACH DATABASE ${rdbSchemaName};
${afterFinalize.length > 0 ? `${afterFinalize.join(";\n ")};` : "-- no after-finalize SQL provided"}`), isc.current.nbCellID);
}
}
Expand Down
Loading

0 comments on commit 1da20b4

Please sign in to comment.