@memberjunction/generic-database-provider 6.2.0-edge.5 → 6.2.0-edge.7
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/README.md +165 -1
- package/dist/GenericDatabaseProvider.d.ts +599 -69
- package/dist/GenericDatabaseProvider.d.ts.map +1 -1
- package/dist/GenericDatabaseProvider.js +2055 -403
- package/dist/GenericDatabaseProvider.js.map +1 -1
- package/dist/SqlLogger.d.ts +26 -0
- package/dist/SqlLogger.d.ts.map +1 -1
- package/dist/SqlLogger.js +77 -20
- package/dist/SqlLogger.js.map +1 -1
- package/dist/clientClauseScreen.d.ts +44 -75
- package/dist/clientClauseScreen.d.ts.map +1 -1
- package/dist/clientClauseScreen.js +129 -267
- package/dist/clientClauseScreen.js.map +1 -1
- package/dist/composition/compositionAssembler.d.ts +33 -0
- package/dist/composition/compositionAssembler.d.ts.map +1 -0
- package/dist/composition/compositionAssembler.js +270 -0
- package/dist/composition/compositionAssembler.js.map +1 -0
- package/dist/composition/compositionInfo.d.ts +7 -0
- package/dist/composition/compositionInfo.d.ts.map +1 -0
- package/dist/composition/compositionInfo.js +31 -0
- package/dist/composition/compositionInfo.js.map +1 -0
- package/dist/composition/compositionLookup.d.ts +44 -0
- package/dist/composition/compositionLookup.d.ts.map +1 -0
- package/dist/composition/compositionLookup.js +122 -0
- package/dist/composition/compositionLookup.js.map +1 -0
- package/dist/composition/compositionRender.d.ts +136 -0
- package/dist/composition/compositionRender.d.ts.map +1 -0
- package/dist/composition/compositionRender.js +264 -0
- package/dist/composition/compositionRender.js.map +1 -0
- package/dist/composition/compositionTypes.d.ts +40 -0
- package/dist/composition/compositionTypes.d.ts.map +1 -0
- package/dist/composition/compositionTypes.js +2 -0
- package/dist/composition/compositionTypes.js.map +1 -0
- package/dist/composition/quotedTagRefusal.d.ts +44 -0
- package/dist/composition/quotedTagRefusal.d.ts.map +1 -0
- package/dist/composition/quotedTagRefusal.js +132 -0
- package/dist/composition/quotedTagRefusal.js.map +1 -0
- package/dist/composition/renderSteps.d.ts +16 -0
- package/dist/composition/renderSteps.d.ts.map +1 -0
- package/dist/composition/renderSteps.js +42 -0
- package/dist/composition/renderSteps.js.map +1 -0
- package/dist/composition/safetyGate.d.ts +46 -0
- package/dist/composition/safetyGate.d.ts.map +1 -0
- package/dist/composition/safetyGate.js +61 -0
- package/dist/composition/safetyGate.js.map +1 -0
- package/dist/composition/templateUnit.d.ts +95 -0
- package/dist/composition/templateUnit.d.ts.map +1 -0
- package/dist/composition/templateUnit.js +189 -0
- package/dist/composition/templateUnit.js.map +1 -0
- package/dist/describeStatement.d.ts +51 -0
- package/dist/describeStatement.d.ts.map +1 -0
- package/dist/describeStatement.js +19 -0
- package/dist/describeStatement.js.map +1 -0
- package/dist/fieldSecurity/projectionStars.d.ts +15 -0
- package/dist/fieldSecurity/projectionStars.d.ts.map +1 -0
- package/dist/fieldSecurity/projectionStars.js +116 -0
- package/dist/fieldSecurity/projectionStars.js.map +1 -0
- package/dist/fieldSecurity/queryColumnSecurity.d.ts +35 -0
- package/dist/fieldSecurity/queryColumnSecurity.d.ts.map +1 -0
- package/dist/fieldSecurity/queryColumnSecurity.js +84 -0
- package/dist/fieldSecurity/queryColumnSecurity.js.map +1 -0
- package/dist/index.d.ts +9 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -1
- package/dist/index.js.map +1 -1
- package/dist/pagingLayout.d.ts +68 -0
- package/dist/pagingLayout.d.ts.map +1 -0
- package/dist/pagingLayout.js +190 -0
- package/dist/pagingLayout.js.map +1 -0
- package/dist/queryCompositionEngine.d.ts +22 -222
- package/dist/queryCompositionEngine.d.ts.map +1 -1
- package/dist/queryCompositionEngine.js +35 -729
- package/dist/queryCompositionEngine.js.map +1 -1
- package/dist/queryPagingEngine.d.ts +26 -5
- package/dist/queryPagingEngine.d.ts.map +1 -1
- package/dist/queryPagingEngine.js +51 -15
- package/dist/queryPagingEngine.js.map +1 -1
- package/dist/recordChangeSnapshot.d.ts +43 -0
- package/dist/recordChangeSnapshot.d.ts.map +1 -0
- package/dist/recordChangeSnapshot.js +85 -0
- package/dist/recordChangeSnapshot.js.map +1 -0
- package/dist/renderPipeline.d.ts +91 -72
- package/dist/renderPipeline.d.ts.map +1 -1
- package/dist/renderPipeline.js +71 -179
- package/dist/renderPipeline.js.map +1 -1
- package/dist/statementMemo.d.ts +29 -0
- package/dist/statementMemo.d.ts.map +1 -0
- package/dist/statementMemo.js +61 -0
- package/dist/statementMemo.js.map +1 -0
- package/dist/systemUserFieldAccess.d.ts +16 -1
- package/dist/systemUserFieldAccess.d.ts.map +1 -1
- package/dist/systemUserFieldAccess.js +39 -8
- package/dist/systemUserFieldAccess.js.map +1 -1
- package/dist/typedQueryParameter.d.ts +55 -0
- package/dist/typedQueryParameter.d.ts.map +1 -0
- package/dist/typedQueryParameter.js +122 -0
- package/dist/typedQueryParameter.js.map +1 -0
- package/dist/types.d.ts +47 -2
- package/dist/types.d.ts.map +1 -1
- package/package.json +14 -14
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
import { MJQueryDiagnosticCodes, SqlTokens } from '@memberjunction/sql-parser';
|
|
2
|
+
import { QueryTextPosition } from '@memberjunction/core-entities';
|
|
3
|
+
/** The diagnostic code for each kind of quoted tag. */
|
|
4
|
+
const KIND_BY_CODE = new Map([
|
|
5
|
+
[MJQueryDiagnosticCodes.TagInStringLiteral, 'StringLiteral'],
|
|
6
|
+
[MJQueryDiagnosticCodes.TagInQuotedIdentifier, 'QuotedIdentifier'],
|
|
7
|
+
]);
|
|
8
|
+
/** What a dialect's opening identifier quote is called in a fix. */
|
|
9
|
+
const IDENTIFIER_QUOTE_NAMES = { '[': 'brackets', '"': 'double quotes', '`': 'backticks' };
|
|
10
|
+
/** The first name a `{{ … }}` tag reads, e.g. `Region` in `{{- Region | upper }}` or `Context.Now`. */
|
|
11
|
+
const TAG_PARAMETER = /^\{\{-?\s*([A-Za-z_][A-Za-z0-9_]*(?:\.[A-Za-z_][A-Za-z0-9_]*)?)/;
|
|
12
|
+
/** A composition tag, `{{query:"…"}}`. */
|
|
13
|
+
const COMPOSITION_TAG_TEXT = /^\{\{-?\s*query\s*:/;
|
|
14
|
+
/**
|
|
15
|
+
* Whether a diagnostic reports a template tag inside a SQL string literal
|
|
16
|
+
* (`MJQ_TAG_IN_STRING_LITERAL`) or a quoted identifier (`MJQ_TAG_IN_QUOTED_IDENTIFIER`).
|
|
17
|
+
*/
|
|
18
|
+
export function IsQuotedTagCode(code) {
|
|
19
|
+
return KIND_BY_CODE.has(code);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Whether a tag written inside a SQL string literal or a quoted identifier pastes a value there: a
|
|
23
|
+
* `{{ … }}` value tag. A composition tag there stays literal text, and a `{% %}` block or `{# #}`
|
|
24
|
+
* comment pastes no value, so a run does not stop for those (a save still refuses them).
|
|
25
|
+
*/
|
|
26
|
+
export function IsQuotedValueTag(tagText) {
|
|
27
|
+
return tagText.startsWith('{{') && !COMPOSITION_TAG_TEXT.test(tagText);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Why a query whose SQL writes a value tag inside a SQL string literal (`'{{ Region }}'`) or a
|
|
31
|
+
* quoted identifier (`[{{ Col }}]`) may not run, written for the people who own the query: which
|
|
32
|
+
* tags, where, why each kind is refused, the exact fix, and how to find every query with the same
|
|
33
|
+
* problem. One message covers both kinds when the query has both.
|
|
34
|
+
*
|
|
35
|
+
* The positions are read from `sql` as stored (comments included), so they match what an editor
|
|
36
|
+
* shows. Returns `null` when `sql` has no such tag.
|
|
37
|
+
*
|
|
38
|
+
* @param queryName - Names the query; a request that is not a saved query passes `null`.
|
|
39
|
+
*/
|
|
40
|
+
export function QuotedTagRefusalMessage(queryName, sql, dialect) {
|
|
41
|
+
const tags = FindQuotedTags(sql, dialect);
|
|
42
|
+
if (tags.length === 0)
|
|
43
|
+
return null;
|
|
44
|
+
const subject = queryName ? `Query "${queryName}"` : 'This query';
|
|
45
|
+
const literal = tags.find(t => t.Kind === 'StringLiteral');
|
|
46
|
+
const identifier = tags.find(t => t.Kind === 'QuotedIdentifier');
|
|
47
|
+
return [
|
|
48
|
+
`${subject} cannot run: its SQL writes a template tag inside quotes, ${tags.map(describeTag).join('; ')}.`,
|
|
49
|
+
...(literal ? literalGuidance(literal) : []),
|
|
50
|
+
...(identifier ? identifierGuidance(identifier) : []),
|
|
51
|
+
'MJ therefore refuses to run the query until it is fixed.',
|
|
52
|
+
findEveryQuery(literal !== undefined, identifier !== undefined),
|
|
53
|
+
].join(' ');
|
|
54
|
+
}
|
|
55
|
+
/** Why a tag inside a string literal is unsafe, and the filter that writes the whole literal. */
|
|
56
|
+
function literalGuidance(tag) {
|
|
57
|
+
return [
|
|
58
|
+
'In a string literal, MJ pastes the value between the quotes without escaping it, so a value that contains a quote can change the statement.',
|
|
59
|
+
literalFix(tag),
|
|
60
|
+
'When the value is not text, use the filter for its type instead (sqlNumber, sqlDate, sqlBoolean).',
|
|
61
|
+
];
|
|
62
|
+
}
|
|
63
|
+
/** The exact rewrite for a tag inside a string literal: the filter that writes the whole literal from the value. */
|
|
64
|
+
function literalFix(tag) {
|
|
65
|
+
const name = tag.Parameter ?? 'Value';
|
|
66
|
+
const body = tag.Quoted.slice(1, -1);
|
|
67
|
+
if (body === tag.Tag) {
|
|
68
|
+
return `Fix: replace ${tag.Quoted} with {{ ${name} | sqlString }} and drop the surrounding quotes; sqlString adds the quotes and escapes the value.`;
|
|
69
|
+
}
|
|
70
|
+
const like = body === `%${tag.Tag}%` ? 'sqlLikeContains' : body === `${tag.Tag}%` ? 'sqlLikeBegins' : body === `%${tag.Tag}` ? 'sqlLikeEnds' : null;
|
|
71
|
+
if (like) {
|
|
72
|
+
return `Fix: replace ${tag.Quoted} with {{ ${name} | ${like} }} and drop the surrounding quotes; ${like} adds the quotes, the % wildcards and the escaping.`;
|
|
73
|
+
}
|
|
74
|
+
return `Fix: take the tag out of the quotes and give it a filter, joining any fixed text in SQL, e.g. CONCAT('…', {{ ${name} | sqlString }}, '…') in place of ${tag.Quoted}; sqlString adds the quotes and escapes the value.`;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Why a tag inside a quoted identifier is unsafe, and the rewrite with `sqlIdentifier`, which
|
|
78
|
+
* quotes the name for the dialect and refuses a value that is not a plain name.
|
|
79
|
+
*/
|
|
80
|
+
function identifierGuidance(tag) {
|
|
81
|
+
const name = tag.Parameter ?? 'Name';
|
|
82
|
+
const closing = tag.Quoted.slice(-1);
|
|
83
|
+
const quotes = IDENTIFIER_QUOTE_NAMES[tag.Quoted[0]] ?? 'quotes';
|
|
84
|
+
const what = 'sqlIdentifier quotes the name for the database and refuses a value that is not a plain name (letters, digits and underscores).';
|
|
85
|
+
const fix = tag.Quoted.slice(1, -1) === tag.Tag
|
|
86
|
+
? `Fix: replace ${tag.Quoted} with {{ ${name} | sqlIdentifier }} and drop the surrounding ${quotes}; ${what}`
|
|
87
|
+
: `Fix: take the tag out of the ${quotes}: pass the whole name as the value and write {{ ${name} | sqlIdentifier }} in place of ${tag.Quoted}; ${what}`;
|
|
88
|
+
return [
|
|
89
|
+
`In a quoted identifier, MJ pastes the value between the identifier's ${quotes} without escaping it, so a value that contains the closing ${closing} can end the identifier and change the statement.`,
|
|
90
|
+
fix,
|
|
91
|
+
];
|
|
92
|
+
}
|
|
93
|
+
/** How to list every saved query with the same problem, naming the refresh code(s) reported. */
|
|
94
|
+
function findEveryQuery(hasLiteral, hasIdentifier) {
|
|
95
|
+
const codes = [];
|
|
96
|
+
if (hasLiteral)
|
|
97
|
+
codes.push(MJQueryDiagnosticCodes.TagInStringLiteral);
|
|
98
|
+
if (hasIdentifier)
|
|
99
|
+
codes.push(MJQueryDiagnosticCodes.TagInQuotedIdentifier);
|
|
100
|
+
return `To list every saved query with this problem, run \`mj query refresh --all --dry-run\`: each is reported as an Error (${codes.join(' or ')}).`;
|
|
101
|
+
}
|
|
102
|
+
/** The value tags `sql` writes inside SQL string literals or quoted identifiers, in source order. */
|
|
103
|
+
export function FindQuotedTags(sql, dialect) {
|
|
104
|
+
const tokens = SqlTokens.Lex(sql, dialect);
|
|
105
|
+
return tokens.Diagnostics
|
|
106
|
+
.filter(d => IsQuotedTagCode(d.Code) && IsQuotedValueTag(sql.substring(d.Span.Start, d.Span.End)))
|
|
107
|
+
.map(d => quotedTag(sql, d, enclosingQuoted(tokens, d.Span)));
|
|
108
|
+
}
|
|
109
|
+
function quotedTag(sql, diagnostic, quoted) {
|
|
110
|
+
const span = diagnostic.Span;
|
|
111
|
+
const tag = sql.substring(span.Start, span.End);
|
|
112
|
+
const at = QueryTextPosition(sql, span.Start);
|
|
113
|
+
return {
|
|
114
|
+
Kind: KIND_BY_CODE.get(diagnostic.Code) ?? 'StringLiteral',
|
|
115
|
+
Tag: tag,
|
|
116
|
+
Parameter: TAG_PARAMETER.exec(tag)?.[1] ?? null,
|
|
117
|
+
Quoted: sql.substring(quoted.Start, quoted.End),
|
|
118
|
+
Line: at.Line,
|
|
119
|
+
Column: at.Column,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
/** The full extent of the string literal or quoted identifier the tag at `span` interrupts; the tag itself when none is found. */
|
|
123
|
+
function enclosingQuoted(tokens, span) {
|
|
124
|
+
const piece = tokens.Tokens.find(t => t.Enclosing !== null && t.Enclosing.Start <= span.Start && t.Enclosing.End >= span.End);
|
|
125
|
+
return piece?.Enclosing ?? span;
|
|
126
|
+
}
|
|
127
|
+
function describeTag(tag) {
|
|
128
|
+
const kind = tag.Parameter?.startsWith('Context.') ? 'context value' : 'parameter';
|
|
129
|
+
const parameter = tag.Parameter ? ` (${kind} "${tag.Parameter}")` : '';
|
|
130
|
+
return `${tag.Tag}${parameter} at line ${tag.Line}, column ${tag.Column}: ${tag.Quoted}`;
|
|
131
|
+
}
|
|
132
|
+
//# sourceMappingURL=quotedTagRefusal.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"quotedTagRefusal.js","sourceRo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IAAI;QAC/C,MAAM,EAAE,GAAG,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,GAAG,CAAC;QAC/C,IAAI,EAAE,EAAE,CAAC,IAAI;QACb,MAAM,EAAE,EAAE,CAAC,MAAM;KACpB,CAAC;AACN,CAAC;AAED,kIAAkI;AAClI,SAAS,eAAe,CAAC,MAAiB,EAAE,IAAU;IAClD,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,SAAS,KAAK,IAAI,IAAI,CAAC,CAAC,SAAS,CAAC,KAAK,IAAI,IAAI,CAAC,KAAK,IAAI,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC;IAC9H,OAAO,KAAK,EAAE,SAAS,IAAI,IAAI,CAAC;AACpC,CAAC;AAED,SAAS,WAAW,CAAC,GAAc;IAC/B,MAAM,IAAI,GAAG,GAAG,CAAC,SAAS,EAAE,UAAU,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,eAAe,CAAC,CAAC,CAAC,WAAW,CAAC;IACnF,MAAM,SAAS,GAAG,GAAG,CAAC,SAAS,CAAC,CAAC,CAAC,KAAK,IAAI,KAAK,GAAG,CAAC,SAAS,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;IACvE,OAAO,GAAG,GAAG,CAAC,GAAG,GAAG,SAAS,YAAY,GAAG,CAAC,IAAI,YAAY,GAAG,CAAC,MAAM,KAAK,GAAG,CAAC,MAAM,EAAE,CAAC;AAC7F,CAAC"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { QueryTemplateContext } from '@memberjunction/core';
|
|
2
|
+
import type { SQLDialect } from '@memberjunction/sql-dialect';
|
|
3
|
+
import type { RenderContext } from '../renderPipeline.js';
|
|
4
|
+
import { type AssembledComposition } from './compositionAssembler.js';
|
|
5
|
+
import { CompositionRender, type CompositionInstance, type UnitRenderRequest } from './compositionRender.js';
|
|
6
|
+
/** A render for `ctx`. With `staticOnly`, the outer query is composed without being rendered. */
|
|
7
|
+
export declare function NewCompositionRender(ctx: RenderContext, dialect: SQLDialect, context: QueryTemplateContext, staticOnly: boolean): CompositionRender;
|
|
8
|
+
/**
|
|
9
|
+
* The outer query's render request. Its parameter definitions come from `QueryInfo` (saved
|
|
10
|
+
* queries) or `ParameterDefinitions`. Without definitions, or when the query composes others,
|
|
11
|
+
* a value for an undeclared name is accepted, as it always was.
|
|
12
|
+
*/
|
|
13
|
+
export declare function OuterRenderRequest(sql: string, ctx: RenderContext): UnitRenderRequest;
|
|
14
|
+
/** The plain SQL: the outer render alone, or assembled with its instances. */
|
|
15
|
+
export declare function AssembleComposition(outerSQL: string, instances: readonly CompositionInstance[], dialect: SQLDialect): AssembledComposition;
|
|
16
|
+
//# sourceMappingURL=renderSteps.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"renderSteps.d.ts","sourceRoot":"","sources":["../../src/composition/renderSteps.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,sBAAsB,CAAC;AAEjE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAwB,KAAK,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAC5F,OAAO,EAAE,iBAAiB,EAAE,KAAK,mBAAmB,EAAE,KAAK,iBAAiB,EAAE,MAAM,wBAAwB,CAAC;AAG7G,iGAAiG;AACjG,wBAAgB,oBAAoB,CAAC,GAAG,EAAE,aAAa,EAAE,OAAO,EAAE,UAAU,EAAE,OAAO,EAAE,oBAAoB,EAAE,UAAU,EAAE,OAAO,GAAG,iBAAiB,CAWnJ;AAED;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,aAAa,GAAG,iBAAiB,CAgBrF;AAED,8EAA8E;AAC9E,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,MAAM,EAAE,SAAS,EAAE,SAAS,mBAAmB,EAAE,EAAE,OAAO,EAAE,UAAU,GAAG,oBAAoB,CAE1I"}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { CompositionAssembler } from './compositionAssembler.js';
|
|
2
|
+
import { CompositionRender } from './compositionRender.js';
|
|
3
|
+
/** A render for `ctx`. With `staticOnly`, the outer query is composed without being rendered. */
|
|
4
|
+
export function NewCompositionRender(ctx, dialect, context, staticOnly) {
|
|
5
|
+
return new CompositionRender({
|
|
6
|
+
Platform: ctx.Platform,
|
|
7
|
+
Dialect: dialect,
|
|
8
|
+
DialectKey: ctx.DialectKey ?? ctx.Platform,
|
|
9
|
+
ContextUser: ctx.ContextUser,
|
|
10
|
+
ServerParameters: ctx.ServerParameters,
|
|
11
|
+
Context: context,
|
|
12
|
+
RootQueryID: ctx.QueryID,
|
|
13
|
+
StaticArgumentsOnly: staticOnly,
|
|
14
|
+
});
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The outer query's render request. Its parameter definitions come from `QueryInfo` (saved
|
|
18
|
+
* queries) or `ParameterDefinitions`. Without definitions, or when the query composes others,
|
|
19
|
+
* a value for an undeclared name is accepted, as it always was.
|
|
20
|
+
*/
|
|
21
|
+
export function OuterRenderRequest(sql, ctx) {
|
|
22
|
+
const definitions = ctx.QueryInfo?.Parameters ?? ctx.ParameterDefinitions ?? [];
|
|
23
|
+
const definitionsGiven = ctx.ParameterDefinitions !== undefined;
|
|
24
|
+
return {
|
|
25
|
+
SQL: sql,
|
|
26
|
+
RendersTemplate: ctx.UsesTemplate ?? ctx.QueryInfo?.UsesTemplate ?? false,
|
|
27
|
+
Label: 'of this query',
|
|
28
|
+
Name: ctx.QueryName,
|
|
29
|
+
Definitions: definitions,
|
|
30
|
+
Parameters: ctx.Parameters,
|
|
31
|
+
ServerParameters: ctx.ServerParameters,
|
|
32
|
+
SkipUnknownParameterCheck: (unit) => unit.References.length > 0 || !definitionsGiven,
|
|
33
|
+
InlineDependencies: ctx.Dependencies,
|
|
34
|
+
Depth: 0,
|
|
35
|
+
Stack: ctx.QueryID ? [ctx.QueryID] : [],
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
/** The plain SQL: the outer render alone, or assembled with its instances. */
|
|
39
|
+
export function AssembleComposition(outerSQL, instances, dialect) {
|
|
40
|
+
return instances.length > 0 ? CompositionAssembler.Assemble(outerSQL, instances, dialect) : { SQL: outerSQL, Instances: [] };
|
|
41
|
+
}
|
|
42
|
+
//# sourceMappingURL=renderSteps.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"renderSteps.js","sourceRoot":"","sources":["../../src/composition/renderSteps.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,oBAAoB,EAA6B,MAAM,2BAA2B,CAAC;AAC5F,OAAO,EAAE,iBAAiB,EAAoD,MAAM,wBAAwB,CAAC;AAG7G,iGAAiG;AACjG,MAAM,UAAU,oBAAoB,CAAC,GAAkB,EAAE,OAAmB,EAAE,OAA6B,EAAE,UAAmB;IAC5H,OAAO,IAAI,iBAAiB,CAAC;QACzB,QAAQ,EAAE,GAAG,CAAC,QAAQ;QACtB,OAAO,EAAE,OAAO;QAChB,UAAU,EAAE,GAAG,CAAC,UAAU,IAAI,GAAG,CAAC,QAAQ;QAC1C,WAAW,EAAE,GAAG,CAAC,WAAW;QAC5B,gBAAgB,EAAE,GAAG,CAAC,gBAAgB;QACtC,OAAO,EAAE,OAAO;QAChB,WAAW,EAAE,GAAG,CAAC,OAAO;QACxB,mBAAmB,EAAE,UAAU;KAClC,CAAC,CAAC;AACP,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,kBAAkB,CAAC,GAAW,EAAE,GAAkB;IAC9D,MAAM,WAAW,GAA4C,GAAG,CAAC,SAAS,EAAE,UAAU,IAAI,GAAG,CAAC,oBAAoB,IAAI,EAAE,CAAC;IACzH,MAAM,gBAAgB,GAAG,GAAG,CAAC,oBAAoB,KAAK,SAAS,CAAC;IAChE,OAAO;QACH,GAAG,EAAE,GAAG;QACR,eAAe,EAAE,GAAG,CAAC,YAAY,IAAI,GAAG,CAAC,SAAS,EAAE,YAAY,IAAI,KAAK;QACzE,KAAK,EAAE,eAAe;QACtB,IAAI,EAAE,GAAG,CAAC,SAAS;QACnB,WAAW,EAAE,WAAW;QACxB,UAAU,EAAE,GAAG,CAAC,UAAU;QAC1B,gBAAgB,EAAE,GAAG,CAAC,gBAAgB;QACtC,yBAAyB,EAAE,CAAC,IAAkB,EAAE,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,GAAG,CAAC,IAAI,CAAC,gBAAgB;QAClG,kBAAkB,EAAE,GAAG,CAAC,YAAY;QACpC,KAAK,EAAE,CAAC;QACR,KAAK,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE;KAC1C,CAAC;AACN,CAAC;AAED,8EAA8E;AAC9E,MAAM,UAAU,mBAAmB,CAAC,QAAgB,EAAE,SAAyC,EAAE,OAAmB;IAChH,OAAO,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,oBAAoB,CAAC,QAAQ,CAAC,QAAQ,EAAE,SAAS,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,QAAQ,EAAE,SAAS,EAAE,EAAE,EAAE,CAAC;AACjI,CAAC"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { SQLDialect } from '@memberjunction/sql-dialect';
|
|
2
|
+
/**
|
|
3
|
+
* The refusal {@link SafetyGate} throws: the SQL is not a single read query, or it calls a
|
|
4
|
+
* forbidden function. A caller that renders SQL for another purpose (a save describing the query)
|
|
5
|
+
* tells this refusal from any other render failure by its type ({@link IsQuerySafetyError}).
|
|
6
|
+
*/
|
|
7
|
+
export declare class QuerySafetyError extends Error {
|
|
8
|
+
/**
|
|
9
|
+
* Whether the SQL was refused because its structure could not be read (a WITH clause the
|
|
10
|
+
* dialect's lexer cannot split), rather than because a write or a forbidden call was found.
|
|
11
|
+
*/
|
|
12
|
+
readonly Unreadable: boolean;
|
|
13
|
+
constructor(message: string, unreadable?: boolean);
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Whether `error` is a {@link QuerySafetyError}. Matched by name as well as by class, so a refusal
|
|
17
|
+
* thrown from a second loaded copy of this module is still recognised.
|
|
18
|
+
*/
|
|
19
|
+
export declare function IsQuerySafetyError(error: unknown): error is QuerySafetyError;
|
|
20
|
+
/**
|
|
21
|
+
* The safety stage every rendered statement passes before it can run, on every path (saved
|
|
22
|
+
* queries, composed queries, ad hoc SQL, TestQuerySQL, specs, data contexts).
|
|
23
|
+
*
|
|
24
|
+
* It works from tokens, never from a complete parse, so a read query the structural parser cannot
|
|
25
|
+
* fully read (T-SQL `JOIN … JOIN … ON … ON`, `(NOLOCK)`) still runs, and no parse failure can let a
|
|
26
|
+
* write through:
|
|
27
|
+
* - one statement (no stacked statements), starting with `SELECT` or `WITH`;
|
|
28
|
+
* - every CTE a read, and the statement after the WITH clause a `SELECT`;
|
|
29
|
+
* - no top-level `SELECT … INTO` (it creates a table);
|
|
30
|
+
* - no call to a function the dialect lists in `CallerSQLForbiddenFunctions` (`OPENROWSET`,
|
|
31
|
+
* `pg_read_file`, `dblink`, …): they read what a check of the referenced tables cannot see.
|
|
32
|
+
*
|
|
33
|
+
* node-sql-parser plays no part. SQL with no statement at all (empty, only comments or semicolons)
|
|
34
|
+
* runs nothing and passes, unless the caller asks for a statement (`refuseEmpty`, for SQL a caller
|
|
35
|
+
* supplied).
|
|
36
|
+
*/
|
|
37
|
+
export declare class SafetyGate {
|
|
38
|
+
/**
|
|
39
|
+
* @param refuseEmpty - Whether SQL with no statement is refused too.
|
|
40
|
+
* @throws QuerySafetyError saying why `sql` may not run.
|
|
41
|
+
*/
|
|
42
|
+
static AssertSafeToExecute(sql: string, dialect: SQLDialect, refuseEmpty: boolean): void;
|
|
43
|
+
/** Whether `sql` holds no statement: nothing but whitespace, comments and semicolons. */
|
|
44
|
+
static IsEmpty(sql: string, dialect: SQLDialect): boolean;
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=safetyGate.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"safetyGate.d.ts","sourceRoot":"","sources":["../../src/composition/safetyGate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAG9D;;;;GAIG;AACH,qBAAa,gBAAiB,SAAQ,KAAK;IACvC;;;OAGG;IACH,SAAgB,UAAU,EAAE,OAAO,CAAC;gBAExB,OAAO,EAAE,MAAM,EAAE,UAAU,UAAQ;CAKlD;AAED;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,gBAAgB,CAE5E;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,UAAU;IACnB;;;OAGG;IACH,MAAM,CAAC,mBAAmB,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,OAAO,GAAG,IAAI;IAgBxF,yFAAyF;IACzF,MAAM,CAAC,OAAO,CAAC,GAAG,EAAE,MAAM,EAAE,OAAO,EAAE,UAAU,GAAG,OAAO;CAG5D"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { FindForbiddenFunctionCalls, IsReadOnlyQuery, LexSQL, SignificantTokens, UNREADABLE_WITH_CLAUSE_REASON } from '@memberjunction/sql-parser';
|
|
2
|
+
/**
|
|
3
|
+
* The refusal {@link SafetyGate} throws: the SQL is not a single read query, or it calls a
|
|
4
|
+
* forbidden function. A caller that renders SQL for another purpose (a save describing the query)
|
|
5
|
+
* tells this refusal from any other render failure by its type ({@link IsQuerySafetyError}).
|
|
6
|
+
*/
|
|
7
|
+
export class QuerySafetyError extends Error {
|
|
8
|
+
constructor(message, unreadable = false) {
|
|
9
|
+
super(message);
|
|
10
|
+
this.name = 'QuerySafetyError';
|
|
11
|
+
this.Unreadable = unreadable;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Whether `error` is a {@link QuerySafetyError}. Matched by name as well as by class, so a refusal
|
|
16
|
+
* thrown from a second loaded copy of this module is still recognised.
|
|
17
|
+
*/
|
|
18
|
+
export function IsQuerySafetyError(error) {
|
|
19
|
+
return error instanceof QuerySafetyError || (error instanceof Error && error.name === 'QuerySafetyError');
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* The safety stage every rendered statement passes before it can run, on every path (saved
|
|
23
|
+
* queries, composed queries, ad hoc SQL, TestQuerySQL, specs, data contexts).
|
|
24
|
+
*
|
|
25
|
+
* It works from tokens, never from a complete parse, so a read query the structural parser cannot
|
|
26
|
+
* fully read (T-SQL `JOIN … JOIN … ON … ON`, `(NOLOCK)`) still runs, and no parse failure can let a
|
|
27
|
+
* write through:
|
|
28
|
+
* - one statement (no stacked statements), starting with `SELECT` or `WITH`;
|
|
29
|
+
* - every CTE a read, and the statement after the WITH clause a `SELECT`;
|
|
30
|
+
* - no top-level `SELECT … INTO` (it creates a table);
|
|
31
|
+
* - no call to a function the dialect lists in `CallerSQLForbiddenFunctions` (`OPENROWSET`,
|
|
32
|
+
* `pg_read_file`, `dblink`, …): they read what a check of the referenced tables cannot see.
|
|
33
|
+
*
|
|
34
|
+
* node-sql-parser plays no part. SQL with no statement at all (empty, only comments or semicolons)
|
|
35
|
+
* runs nothing and passes, unless the caller asks for a statement (`refuseEmpty`, for SQL a caller
|
|
36
|
+
* supplied).
|
|
37
|
+
*/
|
|
38
|
+
export class SafetyGate {
|
|
39
|
+
/**
|
|
40
|
+
* @param refuseEmpty - Whether SQL with no statement is refused too.
|
|
41
|
+
* @throws QuerySafetyError saying why `sql` may not run.
|
|
42
|
+
*/
|
|
43
|
+
static AssertSafeToExecute(sql, dialect, refuseEmpty) {
|
|
44
|
+
if (!refuseEmpty && SafetyGate.IsEmpty(sql, dialect))
|
|
45
|
+
return;
|
|
46
|
+
const check = IsReadOnlyQuery(sql, dialect);
|
|
47
|
+
if (!check.IsReadOnly) {
|
|
48
|
+
throw new QuerySafetyError(`RenderPipeline: only a single read query may be run here, and this SQL is not one: ${check.Reason}.`, check.Reason === UNREADABLE_WITH_CLAUSE_REASON);
|
|
49
|
+
}
|
|
50
|
+
const forbidden = FindForbiddenFunctionCalls(sql, dialect);
|
|
51
|
+
if (forbidden.length > 0) {
|
|
52
|
+
throw new QuerySafetyError(`RenderPipeline: a query may not call ${forbidden.join(', ')}. These functions run SQL ` +
|
|
53
|
+
'given as a string, or read files or other databases, so what they read cannot be checked.');
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
/** Whether `sql` holds no statement: nothing but whitespace, comments and semicolons. */
|
|
57
|
+
static IsEmpty(sql, dialect) {
|
|
58
|
+
return SignificantTokens(LexSQL(sql, dialect)).every(t => t.Kind === 'semicolon');
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
//# sourceMappingURL=safetyGate.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"safetyGate.js","sourceRoot":"","sources":["../../src/composition/safetyGate.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,0BAA0B,EAAE,eAAe,EAAE,MAAM,EAAE,iBAAiB,EAAE,6BAA6B,EAAE,MAAM,4BAA4B,CAAC;AAEnJ;;;;GAIG;AACH,MAAM,OAAO,gBAAiB,SAAQ,KAAK;IAOvC,YAAY,OAAe,EAAE,UAAU,GAAG,KAAK;QAC3C,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,kBAAkB,CAAC;QAC/B,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IACjC,CAAC;CACJ;AAED;;;G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memberjunction/sql-parser';
|
|
3
|
+
import type { CompositionReference, TemplateExpression } from '@memberjunction/sql-parser';
|
|
4
|
+
import type { QueryParameterDefinition } from '@memberjunction/query-processor';
|
|
5
|
+
/** The name of the per-render composition recorder in a template's scope. */
|
|
6
|
+
export declare const COMPOSITION_RECORDER_NAME = "__mjComposition";
|
|
7
|
+
/**
|
|
8
|
+
* The MJ Query rules that apply to new and edited queries only: authoring rules (where a tag may
|
|
9
|
+
* sit, which values need a filter, hole and argument shapes, composition placement) that a query
|
|
10
|
+
* saved before the language existed may rely on. A save refuses them and a refresh reports them;
|
|
11
|
+
* a run of an existing query does not stop for them ("existing queries keep running"). Every
|
|
12
|
+
* other error stops a run, so a construct outside the language (an unsupported tag, a function
|
|
13
|
+
* call, attribute access other than `.length`, a subscript, an unknown filter, a malformed tag)
|
|
14
|
+
* never reaches Nunjucks. A code added later stops a run until it is listed here.
|
|
15
|
+
*
|
|
16
|
+
* A value tag inside quotes is not listed, whenever the query was saved: inside a SQL string
|
|
17
|
+
* literal (`MJQ_TAG_IN_STRING_LITERAL`, `'{{ Region }}'`) or a quoted identifier
|
|
18
|
+
* (`MJQ_TAG_IN_QUOTED_IDENTIFIER`, `[{{ Col }}]`, `"{{ Col }}"`, `` `{{ Col }}` ``), its value is
|
|
19
|
+
* pasted between the quotes unescaped, so a value holding the closing quote can end the literal or
|
|
20
|
+
* identifier and change the statement; a run stops for it. A composition tag, block or comment in
|
|
21
|
+
* either place pastes no value and does not stop a run.
|
|
22
|
+
*/
|
|
23
|
+
export declare const SAVE_TIME_ONLY_RULES: ReadonlySet<string>;
|
|
24
|
+
/**
|
|
25
|
+
* One query's SQL, read once for rendering: SQL comments removed (template tags in them are
|
|
26
|
+
* inert), the template parsed by the MJ Query language, and its compositions found.
|
|
27
|
+
*/
|
|
28
|
+
export declare class TemplateUnit {
|
|
29
|
+
/** The SQL with comments removed. */
|
|
30
|
+
readonly SQL: string;
|
|
31
|
+
/** The SQL as given, comments included: refusals give positions in it. */
|
|
32
|
+
private readonly source;
|
|
33
|
+
private readonly dialect;
|
|
34
|
+
readonly Template: QueryTemplate;
|
|
35
|
+
/**
|
|
36
|
+
* The compositions that are SQL (not inside a string literal or quoted identifier), in source
|
|
37
|
+
* order; the recorder call for the i-th passes index `i`.
|
|
38
|
+
*/
|
|
39
|
+
readonly References: readonly CompositionReference[];
|
|
40
|
+
private constructor();
|
|
41
|
+
/**
|
|
42
|
+
* Reads `sql` in `dialect`. Comments are removed first, so tags in them are inert, unless
|
|
43
|
+
* `keepComments` (for SQL that is composed but not rendered): composition tokens in comments
|
|
44
|
+
* are inert either way.
|
|
45
|
+
*/
|
|
46
|
+
static Read(sql: string, dialect: SQLDialect, keepComments?: boolean): TemplateUnit;
|
|
47
|
+
/**
|
|
48
|
+
* The interpreter gate: validates the template by the MJ Query language in Refresh mode and
|
|
49
|
+
* refuses it on any error other than the {@link SAVE_TIME_ONLY_RULES}. Nunjucks only ever
|
|
50
|
+
* evaluates a template that passed. A value tag inside a SQL string literal or a quoted
|
|
51
|
+
* identifier is refused with one message for the query's owners ({@link QuotedTagRefusalMessage}): where each tag is, why, the fix,
|
|
52
|
+
* and how to find every query with the same problem.
|
|
53
|
+
*
|
|
54
|
+
* Callers authorize the run (`UserCanRun`, for the query and each query it composes) before
|
|
55
|
+
* this gate, so the message only reaches a caller allowed to run the query.
|
|
56
|
+
*
|
|
57
|
+
* @param label - Names the query in the error.
|
|
58
|
+
* @param queryName - Names the query in the quoted-tag refusal; `null` for SQL that is not a saved query.
|
|
59
|
+
* @throws Error listing every refusing diagnostic.
|
|
60
|
+
*/
|
|
61
|
+
AssertRenderable(label: string, declared: ReadonlyArray<QueryParameterDefinition>, queryName?: string | null): void;
|
|
62
|
+
/**
|
|
63
|
+
* Whether an error stops a run: every error but the {@link SAVE_TIME_ONLY_RULES}, and, of the
|
|
64
|
+
* tags inside a SQL string literal or a quoted identifier, those that paste a value
|
|
65
|
+
* ({@link IsQuotedValueTag}).
|
|
66
|
+
*/
|
|
67
|
+
private stopsRun;
|
|
68
|
+
/**
|
|
69
|
+
* Whether the SQL holds template syntax besides compositions: a `{{ }}` value, a `{% %}` block or
|
|
70
|
+
* a `{# #}` comment, wherever it sits outside a SQL comment. Such SQL is rendered whatever its
|
|
71
|
+
* `UsesTemplate` flag says, because the flag can be stale (COMPAT-MAP B12).
|
|
72
|
+
*/
|
|
73
|
+
HasTemplateSyntax(): boolean;
|
|
74
|
+
/** The names a `{% set %}` or `{% for %}` assigns: they hold template values, not parameter values. */
|
|
75
|
+
AssignedNames(): ReadonlySet<string>;
|
|
76
|
+
/** The SQL with every composition replaced by its recorder call, for a template render. */
|
|
77
|
+
TemplateText(): string;
|
|
78
|
+
/** Only the recorder calls, for evaluating the arguments of a query that is not a template. */
|
|
79
|
+
CallsText(): string;
|
|
80
|
+
/** The SQL with the i-th composition replaced by `names[i]`. */
|
|
81
|
+
WithNames(names: readonly string[]): string;
|
|
82
|
+
private replaceReferences;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* The Nunjucks tag that records composition `index`: its arguments, in order, are evaluated in
|
|
86
|
+
* the scope where the reference stands, and the call writes the instance's CTE name.
|
|
87
|
+
*/
|
|
88
|
+
export declare function RecorderCall(index: number, reference: CompositionReference): string;
|
|
89
|
+
/**
|
|
90
|
+
* Writes an MJ Query expression as a Nunjucks expression with the same meaning. Every node is
|
|
91
|
+
* parenthesized, and literals are written by MJ, so nothing of the source text is copied.
|
|
92
|
+
* @throws Error for an expression outside the MJ Query grammar.
|
|
93
|
+
*/
|
|
94
|
+
export declare function ToNunjucksExpression(expression: TemplateExpression, argumentName: string): string;
|
|
95
|
+
//# sourceMappingURL=templateUnit.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"templateUnit.d.ts","sourceRoot":"","sources":["../../src/composition/templateUnit.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,6BAA6B,CAAC;AAC9D,OAAO,EA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BAAiB;CAQ5B;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,oBAAoB,GAAG,MAAM,CAGnF;AAED;;;;GAIG;AACH,wBAAgB,oBAAoB,CAAC,UAAU,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,GAAG,MAAM,CAuBjG"}
|
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { AllNodes, MJQueryDiagnosticCodes, QueryTemplate, SQLParser } from '@memberjunction/sql-parser';
|
|
2
|
+
import { IsQuotedTagCode, IsQuotedValueTag, QuotedTagRefusalMessage } from './quotedTagRefusal.js';
|
|
3
|
+
/** The body of a composition tag, `query:"…"`. */
|
|
4
|
+
const COMPOSITION_TAG = /^\s*query\s*:/;
|
|
5
|
+
/** The name of the per-render composition recorder in a template's scope. */
|
|
6
|
+
export const COMPOSITION_RECORDER_NAME = '__mjComposition';
|
|
7
|
+
/**
|
|
8
|
+
* The MJ Query rules that apply to new and edited queries only: authoring rules (where a tag may
|
|
9
|
+
* sit, which values need a filter, hole and argument shapes, composition placement) that a query
|
|
10
|
+
* saved before the language existed may rely on. A save refuses them and a refresh reports them;
|
|
11
|
+
* a run of an existing query does not stop for them ("existing queries keep running"). Every
|
|
12
|
+
* other error stops a run, so a construct outside the language (an unsupported tag, a function
|
|
13
|
+
* call, attribute access other than `.length`, a subscript, an unknown filter, a malformed tag)
|
|
14
|
+
* never reaches Nunjucks. A code added later stops a run until it is listed here.
|
|
15
|
+
*
|
|
16
|
+
* A value tag inside quotes is not listed, whenever the query was saved: inside a SQL string
|
|
17
|
+
* literal (`MJQ_TAG_IN_STRING_LITERAL`, `'{{ Region }}'`) or a quoted identifier
|
|
18
|
+
* (`MJQ_TAG_IN_QUOTED_IDENTIFIER`, `[{{ Col }}]`, `"{{ Col }}"`, `` `{{ Col }}` ``), its value is
|
|
19
|
+
* pasted between the quotes unescaped, so a value holding the closing quote can end the literal or
|
|
20
|
+
* identifier and change the statement; a run stops for it. A composition tag, block or comment in
|
|
21
|
+
* either place pastes no value and does not stop a run.
|
|
22
|
+
*/
|
|
23
|
+
export const SAVE_TIME_ONLY_RULES = new Set([
|
|
24
|
+
MJQueryDiagnosticCodes.UnfilteredValue,
|
|
25
|
+
MJQueryDiagnosticCodes.UnfilteredUntyped,
|
|
26
|
+
MJQueryDiagnosticCodes.ArithmeticInHole,
|
|
27
|
+
MJQueryDiagnosticCodes.HoleNotAValue,
|
|
28
|
+
MJQueryDiagnosticCodes.DefaultNotLiteral,
|
|
29
|
+
MJQueryDiagnosticCodes.ForIterable,
|
|
30
|
+
MJQueryDiagnosticCodes.FilterTypeMismatch,
|
|
31
|
+
MJQueryDiagnosticCodes.ParameterTypeConflict,
|
|
32
|
+
MJQueryDiagnosticCodes.CompositionPosition,
|
|
33
|
+
]);
|
|
34
|
+
/**
|
|
35
|
+
* One query's SQL, read once for rendering: SQL comments removed (template tags in them are
|
|
36
|
+
* inert), the template parsed by the MJ Query language, and its compositions found.
|
|
37
|
+
*/
|
|
38
|
+
export class TemplateUnit {
|
|
39
|
+
constructor(sql, dialect, keepComments) {
|
|
40
|
+
this.source = sql;
|
|
41
|
+
this.dialect = dialect;
|
|
42
|
+
this.SQL = keepComments ? sql : SQLParser.StripComments(sql, dialect);
|
|
43
|
+
this.Template = QueryTemplate.Parse(this.SQL, dialect);
|
|
44
|
+
this.References = this.Template.CompositionReferences().Value.filter(r => r.Node.SqlContext === 'Code');
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Reads `sql` in `dialect`. Comments are removed first, so tags in them are inert, unless
|
|
48
|
+
* `keepComments` (for SQL that is composed but not rendered): composition tokens in comments
|
|
49
|
+
* are inert either way.
|
|
50
|
+
*/
|
|
51
|
+
static Read(sql, dialect, keepComments = false) {
|
|
52
|
+
return new TemplateUnit(sql ?? '', dialect, keepComments);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* The interpreter gate: validates the template by the MJ Query language in Refresh mode and
|
|
56
|
+
* refuses it on any error other than the {@link SAVE_TIME_ONLY_RULES}. Nunjucks only ever
|
|
57
|
+
* evaluates a template that passed. A value tag inside a SQL string literal or a quoted
|
|
58
|
+
* identifier is refused with one message for the query's owners ({@link QuotedTagRefusalMessage}): where each tag is, why, the fix,
|
|
59
|
+
* and how to find every query with the same problem.
|
|
60
|
+
*
|
|
61
|
+
* Callers authorize the run (`UserCanRun`, for the query and each query it composes) before
|
|
62
|
+
* this gate, so the message only reaches a caller allowed to run the query.
|
|
63
|
+
*
|
|
64
|
+
* @param label - Names the query in the error.
|
|
65
|
+
* @param queryName - Names the query in the quoted-tag refusal; `null` for SQL that is not a saved query.
|
|
66
|
+
* @throws Error listing every refusing diagnostic.
|
|
67
|
+
*/
|
|
68
|
+
AssertRenderable(label, declared, queryName = null) {
|
|
69
|
+
const check = this.Template.Validate({ Mode: 'Refresh', DeclaredParameters: declaredParameters(declared) });
|
|
70
|
+
const refusals = check.Diagnostics.filter(d => d.Severity === 'Error' && this.stopsRun(d));
|
|
71
|
+
if (refusals.length === 0)
|
|
72
|
+
return;
|
|
73
|
+
const quoted = refusals.some(d => IsQuotedTagCode(d.Code))
|
|
74
|
+
? QuotedTagRefusalMessage(queryName, this.source, this.dialect) : null;
|
|
75
|
+
const others = refusals.filter(d => !IsQuotedTagCode(d.Code));
|
|
76
|
+
const rest = others.length > 0 ? `Query template ${label} cannot be rendered: ${others.map(describe).join(' ')}` : null;
|
|
77
|
+
throw new Error([quoted, rest].filter((m) => m !== null).join(' ')
|
|
78
|
+
|| `Query template ${label} cannot be rendered: ${refusals.map(describe).join(' ')}`);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Whether an error stops a run: every error but the {@link SAVE_TIME_ONLY_RULES}, and, of the
|
|
82
|
+
* tags inside a SQL string literal or a quoted identifier, those that paste a value
|
|
83
|
+
* ({@link IsQuotedValueTag}).
|
|
84
|
+
*/
|
|
85
|
+
stopsRun(diagnostic) {
|
|
86
|
+
if (IsQuotedTagCode(diagnostic.Code)) {
|
|
87
|
+
return IsQuotedValueTag(this.SQL.substring(diagnostic.Span.Start, diagnostic.Span.End));
|
|
88
|
+
}
|
|
89
|
+
return !SAVE_TIME_ONLY_RULES.has(diagnostic.Code);
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Whether the SQL holds template syntax besides compositions: a `{{ }}` value, a `{% %}` block or
|
|
93
|
+
* a `{# #}` comment, wherever it sits outside a SQL comment. Such SQL is rendered whatever its
|
|
94
|
+
* `UsesTemplate` flag says, because the flag can be stale (COMPAT-MAP B12).
|
|
95
|
+
*/
|
|
96
|
+
HasTemplateSyntax() {
|
|
97
|
+
return this.Template.Tokens.Tokens.some(t => t.Kind === 'TemplateBlock' || t.Kind === 'TemplateComment'
|
|
98
|
+
|| (t.Kind === 'TemplateExpression' && !COMPOSITION_TAG.test(t.Template?.Body ?? '')));
|
|
99
|
+
}
|
|
100
|
+
/** The names a `{% set %}` or `{% for %}` assigns: they hold template values, not parameter values. */
|
|
101
|
+
AssignedNames() {
|
|
102
|
+
const names = new Set();
|
|
103
|
+
for (const node of AllNodes(this.Template.Nodes)) {
|
|
104
|
+
if (node.Kind === 'Set')
|
|
105
|
+
names.add(node.Name);
|
|
106
|
+
else if (node.Kind === 'For')
|
|
107
|
+
names.add(node.LoopVariable);
|
|
108
|
+
}
|
|
109
|
+
return names;
|
|
110
|
+
}
|
|
111
|
+
/** The SQL with every composition replaced by its recorder call, for a template render. */
|
|
112
|
+
TemplateText() {
|
|
113
|
+
return this.replaceReferences(i => RecorderCall(i, this.References[i]));
|
|
114
|
+
}
|
|
115
|
+
/** Only the recorder calls, for evaluating the arguments of a query that is not a template. */
|
|
116
|
+
CallsText() {
|
|
117
|
+
return this.References.map((reference, i) => RecorderCall(i, reference)).join('\n');
|
|
118
|
+
}
|
|
119
|
+
/** The SQL with the i-th composition replaced by `names[i]`. */
|
|
120
|
+
WithNames(names) {
|
|
121
|
+
return this.replaceReferences(i => names[i]);
|
|
122
|
+
}
|
|
123
|
+
replaceReferences(replacement) {
|
|
124
|
+
let text = this.SQL;
|
|
125
|
+
for (let i = this.References.length - 1; i >= 0; i--) {
|
|
126
|
+
const span = this.References[i].Span;
|
|
127
|
+
text = text.substring(0, span.Start) + replacement(i) + text.substring(span.End);
|
|
128
|
+
}
|
|
129
|
+
return text;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* The Nunjucks tag that records composition `index`: its arguments, in order, are evaluated in
|
|
134
|
+
* the scope where the reference stands, and the call writes the instance's CTE name.
|
|
135
|
+
*/
|
|
136
|
+
export function RecorderCall(index, reference) {
|
|
137
|
+
const args = reference.Arguments.map(a => ToNunjucksExpression(a.Expression, a.Name)).join(', ');
|
|
138
|
+
return `{{ ${COMPOSITION_RECORDER_NAME}(${index}, [${args}]) }}`;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Writes an MJ Query expression as a Nunjucks expression with the same meaning. Every node is
|
|
142
|
+
* parenthesized, and literals are written by MJ, so nothing of the source text is copied.
|
|
143
|
+
* @throws Error for an expression outside the MJ Query grammar.
|
|
144
|
+
*/
|
|
145
|
+
export function ToNunjucksExpression(expression, argumentName) {
|
|
146
|
+
switch (expression.Kind) {
|
|
147
|
+
case 'Name': return identifier(expression.Name, argumentName);
|
|
148
|
+
case 'Context': return `Context.${identifier(expression.Token, argumentName)}`;
|
|
149
|
+
case 'LoopMember': return `loop.${identifier(expression.Member, argumentName)}`;
|
|
150
|
+
case 'Length': return `(${identifier(expression.Object.Name, argumentName)}.length)`;
|
|
151
|
+
case 'InlineIf': {
|
|
152
|
+
const body = ToNunjucksExpression(expression.Body, argumentName);
|
|
153
|
+
const condition = ToNunjucksExpression(expression.Condition, argumentName);
|
|
154
|
+
const otherwise = expression.Else === null ? '' : ` else ${ToNunjucksExpression(expression.Else, argumentName)}`;
|
|
155
|
+
return `(${body} if ${condition}${otherwise})`;
|
|
156
|
+
}
|
|
157
|
+
case 'Literal': return literal(expression.Value);
|
|
158
|
+
case 'Unary': return `(${expression.Operator === 'not' ? 'not ' : expression.Operator}${ToNunjucksExpression(expression.Operand, argumentName)})`;
|
|
159
|
+
case 'Binary': return `(${ToNunjucksExpression(expression.Left, argumentName)} ${expression.Operator} ${ToNunjucksExpression(expression.Right, argumentName)})`;
|
|
160
|
+
case 'Filter': {
|
|
161
|
+
const args = expression.Arguments.map(a => ToNunjucksExpression(a, argumentName));
|
|
162
|
+
const call = args.length > 0 ? `(${args.join(', ')})` : '';
|
|
163
|
+
return `(${ToNunjucksExpression(expression.Input, argumentName)} | ${identifier(expression.Name, argumentName)}${call})`;
|
|
164
|
+
}
|
|
165
|
+
default:
|
|
166
|
+
throw new Error(`Composition argument "${argumentName}" is not an MJ Query expression: ${expression.Text}`);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
function identifier(name, argumentName) {
|
|
170
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) {
|
|
171
|
+
throw new Error(`Composition argument "${argumentName}" reads "${name}", which is not a plain name.`);
|
|
172
|
+
}
|
|
173
|
+
return name;
|
|
174
|
+
}
|
|
175
|
+
function literal(value) {
|
|
176
|
+
if (value === null)
|
|
177
|
+
return 'none';
|
|
178
|
+
if (typeof value === 'number' || typeof value === 'boolean')
|
|
179
|
+
return String(value);
|
|
180
|
+
const escaped = value.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\n/g, '\\n').replace(/\r/g, '\\r').replace(/\t/g, '\\t');
|
|
181
|
+
return `"${escaped}"`;
|
|
182
|
+
}
|
|
183
|
+
function declaredParameters(definitions) {
|
|
184
|
+
return definitions.map(d => ({ Name: d.Name, Type: d.Type, IsServerSet: d.IsServerSet ?? false }));
|
|
185
|
+
}
|
|
186
|
+
function describe(diagnostic) {
|
|
187
|
+
return `${diagnostic.Code}: ${diagnostic.Message}`;
|
|
188
|
+
}
|
|
189
|
+
//# sourceMappingURL=templateUnit.js.map
|
|
@@ -0,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