@memberjunction/generic-database-provider 5.13.0 → 5.14.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/GenericDatabaseProvider.d.ts +27 -6
- package/dist/GenericDatabaseProvider.d.ts.map +1 -1
- package/dist/GenericDatabaseProvider.js +121 -15
- package/dist/GenericDatabaseProvider.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/queryCompositionEngine.d.ts +267 -0
- package/dist/queryCompositionEngine.d.ts.map +1 -0
- package/dist/queryCompositionEngine.js +870 -0
- package/dist/queryCompositionEngine.js.map +1 -0
- package/package.json +12 -10
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
import { DatabasePlatform, UserInfo, QueryDependencySpec } from "@memberjunction/core";
|
|
2
|
+
/**
|
|
3
|
+
* Metadata about a single CTE generated during composition resolution.
|
|
4
|
+
*/
|
|
5
|
+
export interface CompositionCTEInfo {
|
|
6
|
+
/** ID of the referenced query */
|
|
7
|
+
QueryID: string;
|
|
8
|
+
/** Name of the referenced query */
|
|
9
|
+
QueryName: string;
|
|
10
|
+
/** Category path as written in the reference */
|
|
11
|
+
CategoryPath: string;
|
|
12
|
+
/** Generated CTE alias name */
|
|
13
|
+
CTEName: string;
|
|
14
|
+
/** Original SQL of the referenced query before parameter resolution */
|
|
15
|
+
OriginalSQL: string;
|
|
16
|
+
/** SQL after parameter values have been substituted */
|
|
17
|
+
ResolvedSQL: string;
|
|
18
|
+
/** Parameter values applied (key → resolved value) */
|
|
19
|
+
Parameters: Record<string, string>;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Result returned by the composition engine after resolving all {{query:"..."}} tokens.
|
|
23
|
+
*/
|
|
24
|
+
export interface CompositionResult {
|
|
25
|
+
/** The fully resolved SQL with CTEs prepended */
|
|
26
|
+
ResolvedSQL: string;
|
|
27
|
+
/** Metadata about each CTE generated */
|
|
28
|
+
CTEs: CompositionCTEInfo[];
|
|
29
|
+
/** Directed dependency graph: queryId → [dependsOnQueryIds] */
|
|
30
|
+
DependencyGraph: Map<string, string[]>;
|
|
31
|
+
/** Whether any composition tokens were found and resolved */
|
|
32
|
+
HasCompositions: boolean;
|
|
33
|
+
/** True if any resolved dependency query has UsesTemplate = true (depth-first, transitive) */
|
|
34
|
+
AnyDependencyUsesTemplates: boolean;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Parsed representation of a single {{query:"..."}} token found in SQL.
|
|
38
|
+
*/
|
|
39
|
+
interface ParsedCompositionToken {
|
|
40
|
+
/** The full original token text including {{ and }} */
|
|
41
|
+
FullToken: string;
|
|
42
|
+
/** Category path segments (everything before the query name) */
|
|
43
|
+
CategorySegments: string[];
|
|
44
|
+
/** The query name (last path segment) */
|
|
45
|
+
QueryName: string;
|
|
46
|
+
/** Full path as written (CategorySegments joined with /) */
|
|
47
|
+
FullPath: string;
|
|
48
|
+
/** Parsed parameter mappings */
|
|
49
|
+
Parameters: ParsedParameter[];
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* A single parameter from a composition reference.
|
|
53
|
+
*/
|
|
54
|
+
interface ParsedParameter {
|
|
55
|
+
/** Parameter name */
|
|
56
|
+
Name: string;
|
|
57
|
+
/** If quoted literal: the static value. Otherwise null. */
|
|
58
|
+
StaticValue: string | null;
|
|
59
|
+
/** If bare name: the pass-through parameter name from the outer query. Otherwise null. */
|
|
60
|
+
PassThroughName: string | null;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* QueryCompositionEngine resolves {{query:"CategoryPath/QueryName(params)"}} tokens
|
|
64
|
+
* in query SQL into Common Table Expressions (CTEs). It handles:
|
|
65
|
+
*
|
|
66
|
+
* - Recursive resolution of nested compositions
|
|
67
|
+
* - Cycle detection via an in-progress set
|
|
68
|
+
* - Parameter modes: static literals and pass-through from outer query
|
|
69
|
+
* - Deduplication of identical query+params references
|
|
70
|
+
* - Platform-aware SQL resolution via QueryInfo.GetPlatformSQL()
|
|
71
|
+
*
|
|
72
|
+
* This engine runs BEFORE Nunjucks template processing, so regular {{param}}
|
|
73
|
+
* tokens are preserved for later substitution.
|
|
74
|
+
*/
|
|
75
|
+
export declare class QueryCompositionEngine {
|
|
76
|
+
/**
|
|
77
|
+
* Checks whether SQL contains any {{query:"..."}} composition tokens.
|
|
78
|
+
* Use this as a fast guard before calling ResolveComposition().
|
|
79
|
+
* Only considers tokens outside of SQL comments.
|
|
80
|
+
*/
|
|
81
|
+
HasCompositionTokens(sql: string): boolean;
|
|
82
|
+
/**
|
|
83
|
+
* Parses all {{query:"..."}} tokens from SQL without resolving them.
|
|
84
|
+
* Useful for dependency extraction during the save pipeline.
|
|
85
|
+
* Only considers tokens outside of SQL comments.
|
|
86
|
+
*
|
|
87
|
+
* @param sql - The SQL text to parse
|
|
88
|
+
* @returns Array of parsed token metadata
|
|
89
|
+
*/
|
|
90
|
+
ParseCompositionTokens(sql: string): ParsedCompositionToken[];
|
|
91
|
+
/**
|
|
92
|
+
* Resolves all {{query:"..."}} composition tokens in the given SQL into CTEs.
|
|
93
|
+
*
|
|
94
|
+
* @param sql - The SQL containing composition tokens
|
|
95
|
+
* @param platform - Target database platform for SQL resolution
|
|
96
|
+
* @param contextUser - User context for permission checks on referenced queries
|
|
97
|
+
* @param outerParams - Parameter values from the outer/parent query (for pass-through resolution)
|
|
98
|
+
* @param inlineDependencies - Optional inline dependency specs for transient query testing.
|
|
99
|
+
* When provided, these are checked first before falling back to Metadata.Provider.Queries.
|
|
100
|
+
* Inline dependencies skip governance validation (Reusable, IsApproved, UserCanRun).
|
|
101
|
+
* @returns CompositionResult with fully resolved SQL and provenance metadata
|
|
102
|
+
* @throws Error if a referenced query is not found, not composable, or creates a cycle
|
|
103
|
+
*/
|
|
104
|
+
ResolveComposition(sql: string, platform: DatabasePlatform, contextUser: UserInfo, outerParams?: Record<string, string>, inlineDependencies?: QueryDependencySpec[]): CompositionResult;
|
|
105
|
+
/**
|
|
106
|
+
* Recursively resolves composition tokens in SQL, building up CTE entries.
|
|
107
|
+
*/
|
|
108
|
+
private resolveTokensRecursive;
|
|
109
|
+
/**
|
|
110
|
+
* Parses the content inside a {{query:"..."}} token.
|
|
111
|
+
*/
|
|
112
|
+
private parseTokenContent;
|
|
113
|
+
/**
|
|
114
|
+
* Splits parameter string by commas, respecting quoted values.
|
|
115
|
+
*/
|
|
116
|
+
private splitParams;
|
|
117
|
+
/**
|
|
118
|
+
* Looks up a query by category path + name in the metadata provider.
|
|
119
|
+
*/
|
|
120
|
+
private lookupQuery;
|
|
121
|
+
/**
|
|
122
|
+
* Looks up a query by category path + name, checking inline dependencies first,
|
|
123
|
+
* then falling back to the metadata provider.
|
|
124
|
+
*
|
|
125
|
+
* For inline dependencies, creates a synthetic QueryInfo with the SQL and flags set
|
|
126
|
+
* appropriately. Inline queries skip governance validation (Reusable, IsApproved, etc.)
|
|
127
|
+
* since they are inherently authorized by the caller.
|
|
128
|
+
*/
|
|
129
|
+
private lookupQueryWithInline;
|
|
130
|
+
/**
|
|
131
|
+
* Finds a matching inline dependency spec for the given token.
|
|
132
|
+
*/
|
|
133
|
+
private findInlineDependency;
|
|
134
|
+
/**
|
|
135
|
+
* Builds a synthetic QueryInfo from an inline dependency spec.
|
|
136
|
+
* Sets flags so that GetPlatformSQL returns the inline SQL directly.
|
|
137
|
+
*/
|
|
138
|
+
private buildSyntheticQueryInfo;
|
|
139
|
+
/**
|
|
140
|
+
* Looks up a query by category path + name from the metadata provider only.
|
|
141
|
+
*/
|
|
142
|
+
private lookupQueryFromMetadata;
|
|
143
|
+
/**
|
|
144
|
+
* Validates that a referenced query is eligible for composition.
|
|
145
|
+
*/
|
|
146
|
+
private validateQueryComposable;
|
|
147
|
+
/**
|
|
148
|
+
* Resolves parameter values for a composition reference.
|
|
149
|
+
* Static values are used directly; pass-through values are looked up from outer params.
|
|
150
|
+
*/
|
|
151
|
+
private resolveParameters;
|
|
152
|
+
/**
|
|
153
|
+
* Substitutes resolved parameter values into a query's SQL.
|
|
154
|
+
* - Static values: replaces {{paramName}} with 'value'
|
|
155
|
+
* - Pass-through values: renames {{paramName}} to {{outerParamName}} so
|
|
156
|
+
* the downstream Nunjucks processor can resolve it from the outer query's parameters.
|
|
157
|
+
*/
|
|
158
|
+
private substituteStaticParams;
|
|
159
|
+
/**
|
|
160
|
+
* Builds a deduplication key for a CTE based on query ID and sorted parameter values.
|
|
161
|
+
*/
|
|
162
|
+
private buildDeduplicationKey;
|
|
163
|
+
/**
|
|
164
|
+
* Generates a SQL-safe CTE name from the query name + short hash for uniqueness.
|
|
165
|
+
*/
|
|
166
|
+
private generateCTEName;
|
|
167
|
+
/**
|
|
168
|
+
* Resolves a DatabasePlatform string to the corresponding SQLDialect instance.
|
|
169
|
+
*/
|
|
170
|
+
private getDialect;
|
|
171
|
+
/**
|
|
172
|
+
* Simple string hash for generating short, deterministic suffixes.
|
|
173
|
+
*/
|
|
174
|
+
private simpleHash;
|
|
175
|
+
/**
|
|
176
|
+
* Assembles CTE entries into a WITH clause prepended to the main SQL.
|
|
177
|
+
*/
|
|
178
|
+
private assembleCTEs;
|
|
179
|
+
/**
|
|
180
|
+
* Strips a trailing ORDER BY clause from SQL that will be wrapped in a CTE.
|
|
181
|
+
*
|
|
182
|
+
* SQL Server disallows ORDER BY inside CTEs unless TOP, OFFSET, or FOR XML is present.
|
|
183
|
+
* PostgreSQL allows ORDER BY in CTEs, so no stripping is needed there.
|
|
184
|
+
*
|
|
185
|
+
* Uses a 4-tier strategy:
|
|
186
|
+
* 1. Fast exit — no ORDER keyword at all, or dialect allows ORDER BY in CTEs
|
|
187
|
+
* 2. AST path — parse (with Nunjucks preprocessing if needed), check if ORDER BY is legal
|
|
188
|
+
* (TOP/OFFSET/FOR XML via AST nodes), null out orderby if not, regenerate.
|
|
189
|
+
* Handles window functions, UNION/EXCEPT, subqueries, string literals, and Nunjucks templates.
|
|
190
|
+
* 3. Regex fallback — paren-depth heuristic for SQL the parser still can't handle
|
|
191
|
+
* (e.g. STRING_AGG WITHIN GROUP)
|
|
192
|
+
* 4. OFFSET 0 ROWS injection — last resort when both AST and regex fail to strip ORDER BY.
|
|
193
|
+
* Injects OFFSET 0 ROWS after the ORDER BY clause to make it legal in CTEs.
|
|
194
|
+
* This is semantically neutral (returns all rows starting from 0) but switches
|
|
195
|
+
* SQL Server into paging mode internally, which may affect query plan shape.
|
|
196
|
+
*/
|
|
197
|
+
private stripTrailingOrderBy;
|
|
198
|
+
/**
|
|
199
|
+
* Attempts to strip the top-level ORDER BY clause using AST parsing.
|
|
200
|
+
* Tries direct parsing first, then Nunjucks-preprocessed parsing if the SQL
|
|
201
|
+
* contains template syntax. Handles UNION/EXCEPT by walking the _next chain.
|
|
202
|
+
*/
|
|
203
|
+
private stripOrderByViaAST;
|
|
204
|
+
/**
|
|
205
|
+
* Core AST stripping: parse, analyze, and regenerate SQL without ORDER BY.
|
|
206
|
+
*/
|
|
207
|
+
private tryASTStrip;
|
|
208
|
+
/**
|
|
209
|
+
* Walks the _next chain (UNION/EXCEPT/INTERSECT) to find the statement
|
|
210
|
+
* that carries the ORDER BY clause.
|
|
211
|
+
*/
|
|
212
|
+
private findOrderByStatement;
|
|
213
|
+
/**
|
|
214
|
+
* Nunjucks-aware ORDER BY stripping: preprocess templates into placeholder SQL,
|
|
215
|
+
* parse with AST to confirm top-level ORDER BY exists, then use the position-aware
|
|
216
|
+
* scanner on the original SQL to strip only the last top-level ORDER BY.
|
|
217
|
+
*/
|
|
218
|
+
private tryNunjucksAwareStrip;
|
|
219
|
+
/**
|
|
220
|
+
* Checks AST properties to determine if ORDER BY is legal in a CTE context.
|
|
221
|
+
*/
|
|
222
|
+
private isOrderByLegalInCTE;
|
|
223
|
+
/**
|
|
224
|
+
* Strips the last top-level ORDER BY clause using position-aware scanning.
|
|
225
|
+
* Skips strings, comments, Nunjucks tags, and tracks paren depth.
|
|
226
|
+
*/
|
|
227
|
+
private stripLastTopLevelOrderBy;
|
|
228
|
+
/**
|
|
229
|
+
* Finds character positions of all ORDER BY keywords at the outermost level
|
|
230
|
+
* (paren depth 0, not inside strings, comments, or Nunjucks tags).
|
|
231
|
+
*/
|
|
232
|
+
private findTopLevelOrderByPositions;
|
|
233
|
+
/**
|
|
234
|
+
* Preprocesses Nunjucks templates into valid SQL for AST parsing.
|
|
235
|
+
*/
|
|
236
|
+
private preprocessNunjucks;
|
|
237
|
+
/**
|
|
238
|
+
* Injects OFFSET 0 ROWS after the last top-level ORDER BY clause to make it
|
|
239
|
+
* legal in a CTE without changing the result set. Uses the position-aware scanner
|
|
240
|
+
* to find the correct insertion point after the ORDER BY columns.
|
|
241
|
+
*/
|
|
242
|
+
private injectOffset0Rows;
|
|
243
|
+
/**
|
|
244
|
+
* Regex-based fallback for stripping trailing ORDER BY.
|
|
245
|
+
* Uses parenthesis depth counting to avoid stripping ORDER BY inside subqueries.
|
|
246
|
+
*/
|
|
247
|
+
private stripOrderByViaRegex;
|
|
248
|
+
/**
|
|
249
|
+
* Strips SQL comments from the input string so that composition tokens
|
|
250
|
+
* inside comments are not treated as real references.
|
|
251
|
+
* Handles both single-line (-- ...) and multi-line block comments.
|
|
252
|
+
* Preserves string literals (single-quoted) to avoid stripping inside them.
|
|
253
|
+
*/
|
|
254
|
+
private stripSQLComments;
|
|
255
|
+
/**
|
|
256
|
+
* Escapes {{ and }} inside SQL comments so that Nunjucks doesn't try to parse them.
|
|
257
|
+
* This is needed because dependency queries may carry comments containing
|
|
258
|
+
* {{query:"..."}} examples or documentation that would otherwise cause
|
|
259
|
+
* Nunjucks "expected variable end" errors.
|
|
260
|
+
*
|
|
261
|
+
* Only modifies content inside -- single-line and block comments.
|
|
262
|
+
* Leaves string literals and normal SQL untouched.
|
|
263
|
+
*/
|
|
264
|
+
private escapeTemplateTokensInComments;
|
|
265
|
+
}
|
|
266
|
+
export {};
|
|
267
|
+
//# sourceMappingURL=queryCompositionEngine.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"queryCompositionEngine.d.ts","sourceRoot":"","sources":["../src/queryCompositionEngine.ts"],"names":[],"mappings":"AAEA,OAAO,EAAuB,gBAAgB,EAAE,QAAQ,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AA4B5G;;GAEG;AACH,MAAM,WAAW,kBAAkB;IAC/B,iCAAiC;IACjC,OAAO,EAAE,MAAM,CAAC;IAChB,mCAAmC;IACnC,SAAS,EAAE,MAAM,CAAC;IAClB,gDAAgD;IAChD,YAAY,EAAE,MAAM,CAAC;IACrB,+BAA+B;IAC/B,OAAO,EAAE,MAAM,CAAC;IAChB,uEAAuE;IACvE,WAAW,EAAE,MAAM,CAAC;IACpB,uDAAuD;IACvD,WAAW,EAAE,MAAM,CAAC;IACpB,sDAAsD;IACtD,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACtC;AAED;;GAEG;AACH,MAAM,WAAW,iBAAiB;IAC9B,iDAAiD;IACjD,WAAW,EAAE,MAAM,CAAC;IACpB,wCAAwC;IACxC,IAAI,EAAE,kBAAkB,EAAE,CAAC;IAC3B,+DAA+D;IAC/D,eAAe,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,CAAC,CAAC;IACvC,6DAA6D;IAC7D,eAAe,EAAE,OAAO,CAAC;IACzB,8FAA8F;IAC9F,0BAA0B,EAAE,OAAO,CAAC;CACvC;AAED;;GAEG;AACH,UAAU,sBAAsB;IAC5B,uDAAuD;IACvD,SAAS,EAAE,MAAM,CAAC;IAClB,gEAAgE;IAChE,gBAAgB,EAAE,MAAM,EAAE,CAAC;IAC3B,yCAAyC;IACzC,SAAS,EAAE,MAAM,CAAC;IAClB,4DAA4D;IAC5D,QAAQ,EAAE,MAAM,CAAC;IACjB,gCAAgC;IAChC,UAAU,EAAE,eAAe,EAAE,CAAC;CACjC;AAED;;GAEG;AACH,UAAU,eAAe;IACrB,qBAAqB;IACrB,IAA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