Line data Source code
1 : import { quoteIdentifier, CatalogSource, DEFAULT_DATABASE_NAME } from './catalog_sql_generator.js';
2 :
3 : export interface FunctionMetadata {
4 : schemaName: string | null;
5 : functionName: string;
6 : arguments: string | null;
7 : returnType: string | null;
8 : isAggregate: boolean;
9 : }
10 :
11 1 : export function generateCreateFunctionSQL(databaseName: string | null | undefined, fn: FunctionMetadata): string {
12 1 : const db = databaseName || DEFAULT_DATABASE_NAME;
13 1 : let qualifiedName: string;
14 1 : if (fn.schemaName != null) {
15 1 : qualifiedName = `${quoteIdentifier(db)}.${quoteIdentifier(fn.schemaName)}.${quoteIdentifier(fn.functionName)}`;
16 : } else {
17 1 : qualifiedName = `${quoteIdentifier(db)}.${quoteIdentifier(fn.functionName)}`;
18 : }
19 1 : const keyword = fn.isAggregate ? 'AGGREGATE' : 'FUNCTION';
20 1 : return `CREATE ${keyword} ${qualifiedName}(${fn.arguments ?? ''}) RETURNS ${fn.returnType ?? "any"}`;
21 1 : }
22 :
23 1 : export function generateFunctionsSQL(databaseName: string | null | undefined, functions: FunctionMetadata[]): string {
24 1 : const sorted = [...functions].sort((a, b) => {
25 1 : const schemaCmp = (a.schemaName ?? '').localeCompare(b.schemaName ?? '');
26 1 : if (schemaCmp !== 0) return schemaCmp;
27 1 : return a.functionName.localeCompare(b.functionName);
28 1 : });
29 1 : return sorted.map(fn => generateCreateFunctionSQL(databaseName, fn) + ';').join('\n');
30 1 : }
31 :
32 1 : export function generateFunctionScriptHeaderForSource(source: string, updatedAt: Date = new Date()): string {
33 1 : return `-- DashQL Connection Functions.
34 : -- This file is auto-generated and can only be updated through a catalog refresh.
35 : --
36 1 : -- Catalog Source: ${source}
37 1 : -- Last Refresh: ${updatedAt.toISOString()}
38 :
39 : `;
40 1 : }
41 :
42 1 : export function generateFunctionScriptHeader(method: CatalogSource, updatedAt: Date = new Date()): string {
43 1 : let methodStr: string;
44 1 : switch (method) {
45 1 : case CatalogSource.InformationSchema: methodStr = 'SQL information_schema'; break;
46 1 : case CatalogSource.PgClass: methodStr = 'SQL pg_proc'; break;
47 1 : case CatalogSource.SalesforceMetadataApi: methodStr = 'Salesforce Connect /ssot/metadata API'; break;
48 1 : case CatalogSource.Hyper: methodStr = 'Bundled Hyper function catalog'; break;
49 1 : default: methodStr = '-'; break;
50 1 : }
51 1 : return generateFunctionScriptHeaderForSource(methodStr, updatedAt);
52 1 : }
|