@memberjunction/sql-parser 6.1.5 → 6.1.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/dist/aggregateExpression.d.ts +39 -0
- package/dist/aggregateExpression.d.ts.map +1 -0
- package/dist/aggregateExpression.js +447 -0
- package/dist/aggregateExpression.js.map +1 -0
- package/dist/balancedFragment.d.ts +51 -0
- package/dist/balancedFragment.d.ts.map +1 -0
- package/dist/balancedFragment.js +189 -0
- package/dist/balancedFragment.js.map +1 -0
- package/dist/callerSqlFunctions.js +4 -4
- package/dist/callerSqlFunctions.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/sql-parser.d.ts +45 -5
- package/dist/sql-parser.d.ts.map +1 -1
- package/dist/sql-parser.js +69 -48
- package/dist/sql-parser.js.map +1 -1
- package/dist/sqlLexer.d.ts +2 -1
- package/dist/sqlLexer.d.ts.map +1 -1
- package/dist/sqlLexer.js +9 -4
- package/dist/sqlLexer.js.map +1 -1
- package/package.json +2 -2
|
@@ -0,0 +1,189 @@
|
|
|
1
|
+
import { IsKeyword, LexSQL, SignificantTokens } from './sqlLexer.js';
|
|
2
|
+
/** Appended to the fragment before lexing; it lexes as its own token only if nothing is left open. */
|
|
3
|
+
const SENTINEL = ')';
|
|
4
|
+
const BALANCED = { IsBalanced: true, Reason: null };
|
|
5
|
+
const NON_ASCII = /[^\x00-\x7F]/;
|
|
6
|
+
/**
|
|
7
|
+
* Whether `fragment`, a WHERE-clause expression a caller supplied, stays inside one pair of
|
|
8
|
+
* parentheses put around it. Code that builds `(<fragment>) AND (<server filter>)` depends on this:
|
|
9
|
+
* a fragment such as `1=1) OR (1=1` closes the parentheses early, so the server filter narrows only
|
|
10
|
+
* one side of an OR the fragment controls.
|
|
11
|
+
*
|
|
12
|
+
* The fragment is read with the dialect's lexer ({@link LexSQL}), so a parenthesis inside a string
|
|
13
|
+
* literal, quoted identifier or comment does not count. Text the lexer may not read the way the
|
|
14
|
+
* database does is refused rather than guessed at. The fragment is refused when it:
|
|
15
|
+
* - ends inside a string literal, quoted identifier or comment, which would take in the SQL that
|
|
16
|
+
* follows it;
|
|
17
|
+
* - contains a comment, unless {@link BalancedFragmentOptions.AllowComments} is set;
|
|
18
|
+
* - closes a parenthesis it did not open, or leaves one open;
|
|
19
|
+
* - contains a statement separator (`;`);
|
|
20
|
+
* - on PostgreSQL (a dialect with escape or dollar-quoted strings):
|
|
21
|
+
* - has a character outside ASCII anywhere but inside a string literal, quoted identifier or
|
|
22
|
+
* comment. PostgreSQL reads every such character as part of a name, Unicode spaces included;
|
|
23
|
+
* the lexer does not;
|
|
24
|
+
* - has a dollar-quoted string, or a `$` anywhere but inside a name;
|
|
25
|
+
* - follows an escape string (`E'…'`) with another string literal. PostgreSQL joins literals that
|
|
26
|
+
* a line break separates and reads the second in escape mode; the lexer reads it as plain;
|
|
27
|
+
* - puts a string directly after a name that contains `@` or `#`, or after a number ending in `e`.
|
|
28
|
+
* The lexer reads `@` and `#` as name characters, but PostgreSQL reads them as operators (and
|
|
29
|
+
* splits `1e` before a quote).
|
|
30
|
+
*/
|
|
31
|
+
export function IsBalancedSQLFragment(fragment, dialect, options) {
|
|
32
|
+
const tokens = LexSQL(fragment + SENTINEL, dialect);
|
|
33
|
+
if (!endsWithSentinel(tokens, fragment.length)) {
|
|
34
|
+
return refuse('it ends inside an unterminated string literal, quoted identifier or comment');
|
|
35
|
+
}
|
|
36
|
+
const body = tokens.slice(0, -1);
|
|
37
|
+
const reason = findComment(body, options)
|
|
38
|
+
?? findParenthesisProblem(body)
|
|
39
|
+
?? findStatementSeparator(body)
|
|
40
|
+
?? findPostgreSQLAmbiguity(body, dialect);
|
|
41
|
+
return reason ? refuse(reason) : BALANCED;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Splits `fragment` at its top-level ANDs: those outside parentheses and outside CASE … END, other
|
|
45
|
+
* than the AND of a BETWEEN. Where the lexer reads `[` and `]` as operators (PostgreSQL), an AND or
|
|
46
|
+
* OR between them outside parentheses is part of a bracket name, not a top-level one. Returns the
|
|
47
|
+
* terms as written, or `null` when the fragment has a top-level OR, because putting each term in
|
|
48
|
+
* its own parentheses would then change its meaning. A fragment with no top-level AND comes back as
|
|
49
|
+
* its only term.
|
|
50
|
+
*/
|
|
51
|
+
export function SplitTopLevelAndTerms(fragment, dialect) {
|
|
52
|
+
const ands = findTopLevelAnds(SignificantTokens(LexSQL(fragment, dialect)));
|
|
53
|
+
if (!ands)
|
|
54
|
+
return null;
|
|
55
|
+
const terms = [];
|
|
56
|
+
let from = 0;
|
|
57
|
+
for (const and of ands) {
|
|
58
|
+
terms.push(fragment.substring(from, and.Start));
|
|
59
|
+
from = and.End;
|
|
60
|
+
}
|
|
61
|
+
terms.push(fragment.substring(from));
|
|
62
|
+
return terms;
|
|
63
|
+
}
|
|
64
|
+
function refuse(reason) {
|
|
65
|
+
return { IsBalanced: false, Reason: reason };
|
|
66
|
+
}
|
|
67
|
+
/** True when the sentinel lexed as a separate closing parenthesis, so nothing absorbed it. */
|
|
68
|
+
function endsWithSentinel(tokens, fragmentLength) {
|
|
69
|
+
const last = tokens[tokens.length - 1];
|
|
70
|
+
return last !== undefined && last.Kind === 'close' && last.Start === fragmentLength;
|
|
71
|
+
}
|
|
72
|
+
function findComment(tokens, options) {
|
|
73
|
+
if (options?.AllowComments)
|
|
74
|
+
return null;
|
|
75
|
+
return tokens.some(t => t.Kind === 'comment') ? 'it contains a comment' : null;
|
|
76
|
+
}
|
|
77
|
+
function findParenthesisProblem(tokens) {
|
|
78
|
+
let depth = 0;
|
|
79
|
+
for (const token of tokens) {
|
|
80
|
+
if (token.Kind === 'open')
|
|
81
|
+
depth++;
|
|
82
|
+
else if (token.Kind === 'close' && --depth < 0)
|
|
83
|
+
return 'it closes a parenthesis it did not open';
|
|
84
|
+
}
|
|
85
|
+
return depth > 0 ? 'it leaves a parenthesis open' : null;
|
|
86
|
+
}
|
|
87
|
+
function findStatementSeparator(tokens) {
|
|
88
|
+
return tokens.some(t => t.Kind === 'semicolon') ? 'it contains a statement separator (;)' : null;
|
|
89
|
+
}
|
|
90
|
+
/** The PostgreSQL refusals listed on {@link IsBalancedSQLFragment}. */
|
|
91
|
+
function findPostgreSQLAmbiguity(tokens, dialect) {
|
|
92
|
+
if (!dialect.SupportsEscapeStringLiterals && !dialect.SupportsDollarQuotedStrings)
|
|
93
|
+
return null;
|
|
94
|
+
return findNonASCIIOutsideQuotes(tokens)
|
|
95
|
+
?? findDollarSign(tokens)
|
|
96
|
+
?? findEscapeStringContinuation(tokens)
|
|
97
|
+
?? findAmbiguousString(tokens);
|
|
98
|
+
}
|
|
99
|
+
/** String literals, quoted identifiers and comments: tokens whose content is not read as code. */
|
|
100
|
+
function isQuotedOrComment(token) {
|
|
101
|
+
return token.Kind === 'string' || token.Kind === 'identifier' || token.Kind === 'comment';
|
|
102
|
+
}
|
|
103
|
+
function findNonASCIIOutsideQuotes(tokens) {
|
|
104
|
+
return tokens.some(t => !isQuotedOrComment(t) && NON_ASCII.test(t.Text))
|
|
105
|
+
? 'it has a character outside ASCII outside a string literal or quoted identifier'
|
|
106
|
+
: null;
|
|
107
|
+
}
|
|
108
|
+
function findDollarSign(tokens) {
|
|
109
|
+
const dollar = tokens.some(t => (t.Kind === 'string' && t.Text.startsWith('$')) || (t.Kind === 'operator' && t.Text === '$'));
|
|
110
|
+
return dollar ? 'it has a dollar-quoted string or a "$" outside a name' : null;
|
|
111
|
+
}
|
|
112
|
+
function findEscapeStringContinuation(tokens) {
|
|
113
|
+
const significant = SignificantTokens(tokens);
|
|
114
|
+
for (let i = 0; i + 1 < significant.length; i++) {
|
|
115
|
+
if (isEscapeString(significant[i]) && significant[i + 1].Kind === 'string') {
|
|
116
|
+
return 'it has an escape string followed by another string literal';
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return null;
|
|
120
|
+
}
|
|
121
|
+
function isEscapeString(token) {
|
|
122
|
+
return token.Kind === 'string' && /^[Ee]'/.test(token.Text);
|
|
123
|
+
}
|
|
124
|
+
/** Finds a token inside which PostgreSQL could start a string the lexer did not see. */
|
|
125
|
+
function findAmbiguousString(tokens) {
|
|
126
|
+
for (let i = 0; i < tokens.length; i++) {
|
|
127
|
+
const token = tokens[i];
|
|
128
|
+
const next = tokens[i + 1];
|
|
129
|
+
const escapeString = endsWithSplittableE(token) && next?.Kind === 'string' && next.Start === token.End;
|
|
130
|
+
const dollarString = token.Kind === 'word' && /[@#].*\$/.test(token.Text);
|
|
131
|
+
if (escapeString || dollarString) {
|
|
132
|
+
return `it is ambiguous on this dialect: PostgreSQL may start a string inside "${token.Text}"`;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return null;
|
|
136
|
+
}
|
|
137
|
+
/** A name holding `@` or `#` that ends in `e` (`@@E'…'`), or a number that ends in `e` (`1E'…'`). */
|
|
138
|
+
function endsWithSplittableE(token) {
|
|
139
|
+
if (token.Kind === 'word')
|
|
140
|
+
return /[@#]/.test(token.Text) && /[Ee]$/.test(token.Text);
|
|
141
|
+
if (token.Kind === 'number')
|
|
142
|
+
return /[Ee]$/.test(token.Text);
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
/**
|
|
146
|
+
* The top-level AND tokens, or `null` when there is a top-level OR (see {@link SplitTopLevelAndTerms}).
|
|
147
|
+
* `[` and `]` operator tokens count only outside parentheses, so a bracket left open inside a
|
|
148
|
+
* parenthesized term cannot hide a top-level AND after it.
|
|
149
|
+
*/
|
|
150
|
+
function findTopLevelAnds(tokens) {
|
|
151
|
+
const ands = [];
|
|
152
|
+
let depth = 0;
|
|
153
|
+
let bracketDepth = 0;
|
|
154
|
+
let caseDepth = 0;
|
|
155
|
+
let openBetweens = 0;
|
|
156
|
+
for (const token of tokens) {
|
|
157
|
+
if (token.Kind === 'open')
|
|
158
|
+
depth++;
|
|
159
|
+
else if (token.Kind === 'close')
|
|
160
|
+
depth--;
|
|
161
|
+
else if (depth !== 0)
|
|
162
|
+
continue;
|
|
163
|
+
else if (isOperator(token, '['))
|
|
164
|
+
bracketDepth++;
|
|
165
|
+
else if (isOperator(token, ']'))
|
|
166
|
+
bracketDepth = Math.max(0, bracketDepth - 1);
|
|
167
|
+
else if (bracketDepth > 0)
|
|
168
|
+
continue;
|
|
169
|
+
else if (IsKeyword(token, 'CASE'))
|
|
170
|
+
caseDepth++;
|
|
171
|
+
else if (IsKeyword(token, 'END') && caseDepth > 0)
|
|
172
|
+
caseDepth--;
|
|
173
|
+
else if (caseDepth > 0)
|
|
174
|
+
continue;
|
|
175
|
+
else if (IsKeyword(token, 'OR'))
|
|
176
|
+
return null;
|
|
177
|
+
else if (IsKeyword(token, 'BETWEEN'))
|
|
178
|
+
openBetweens++;
|
|
179
|
+
else if (IsKeyword(token, 'AND') && openBetweens > 0)
|
|
180
|
+
openBetweens--;
|
|
181
|
+
else if (IsKeyword(token, 'AND'))
|
|
182
|
+
ands.push(token);
|
|
183
|
+
}
|
|
184
|
+
return ands;
|
|
185
|
+
}
|
|
186
|
+
function isOperator(token, text) {
|
|
187
|
+
return token.Kind === 'operator' && token.Text === text;
|
|
188
|
+
}
|
|
189
|
+
//# sourceMappingURL=balancedFragment.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"balancedFragment.js","sourceRoot":"","sources":["../src/balancedFragment.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAmBrE,sGAAsG;AACtG,MAAM,QAAQ,GAAG,GAAG,CAAC;AAErB,MAAM,QAAQ,GAA0B,EAAE,UAAU,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;AAE3E,MAAM,SAAS,GAAG,cAAc,CAAC;AAEjC;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,MAAM,UAAU,qBAAqB,CACjC,QAA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,CAAC;YAAE,OAAO,IAAI,CAAC;aACxC,IAAI,SAAS,CAAC,KAAK,EAAE,SAAS,CAAC;YAAE,YAAY,EAAE,CAAC;aAChD,IAAI,SAAS,CAAC,KAAK,EAAE,KAAK,CAAC,IAAI,YAAY,GAAG,CAAC;YAAE,YAAY,EAAE,CAAC;aAChE,IAAI,SAAS,CAAC,KAAK,EAAE,KAAK,CAAC;YAAE,IAAI,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACvD,CAAC;IACD,OAAO,IAAI,CAAC;AAChB,CAAC;AAED,SAAS,UAAU,CAAC,KAAkB,EAAE,IAAY;IAChD,OAAO,KAAK,CAAC,IAAI,KAAK,UAAU,IAAI,KAAK,CAAC,IAAI,KAAK,IAAI,CAAC;AAC5D,CAAC"}
|
|
@@ -10,15 +10,15 @@ export function FindForbiddenFunctionCalls(sql, dialect) {
|
|
|
10
10
|
if (patterns.length === 0)
|
|
11
11
|
return [];
|
|
12
12
|
const tokens = SignificantTokens(LexSQL(sql, dialect));
|
|
13
|
-
const found =
|
|
13
|
+
const found = new Set();
|
|
14
14
|
tokens.forEach((token, i) => {
|
|
15
15
|
if (!isCallName(token, tokens[i + 1]))
|
|
16
16
|
return;
|
|
17
17
|
const name = unquote(token.Text).toLowerCase();
|
|
18
|
-
if (patterns.some(pattern => matches(name, pattern))
|
|
19
|
-
found.
|
|
18
|
+
if (!found.has(name) && patterns.some(pattern => matches(name, pattern)))
|
|
19
|
+
found.add(name);
|
|
20
20
|
});
|
|
21
|
-
return found;
|
|
21
|
+
return [...found];
|
|
22
22
|
}
|
|
23
23
|
function isCallName(token, next) {
|
|
24
24
|
return (token.Kind === 'word' || token.Kind === 'identifier') && next?.Kind === 'open';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"callerSqlFunctions.js","sourceRoot":"","sources":["../src/callerSqlFunctions.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;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|
7
7
|
export { SplitLeadingCTEs, IsReadOnlyQuery } from './sqlShape.js';
|
|
8
8
|
export type { LeadingCTEs, LeadingCTEDefinition, ReadOnlyQueryCheck } from './sqlShape.js';
|
|
9
|
+
export { IsBalancedSQLFragment, SplitTopLevelAndTerms } from './balancedFragment.js';
|
|
10
|
+
export type { BalancedFragmentCheck, BalancedFragmentOptions } from './balancedFragment.js';
|
|
9
11
|
export { AnalyzePagingShape } from './pagingShape.js';
|
|
10
12
|
export { FindForbiddenFunctionCalls } from './callerSqlFunctions.js';
|
|
13
|
+
export { CheckAggregateExpression } from './aggregateExpression.js';
|
|
14
|
+
export type { AggregateExpressionCheck } from './aggregateExpression.js';
|
|
11
15
|
export type { PagingShape, OwnRowCap } from './pagingShape.js';
|
|
12
16
|
export { ParseToIR, RenderIR } from './structuralParser.js';
|
|
13
17
|
export type { QueryIR, CTENode, Fragment, SQLFragment, TemplateExprFragment, BlockFragment, CommentFragment, CompositionRefFragment, CTEOrigin } from './compositionIR.js';
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAE5C,YAAY,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAEpE,OAAO,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,cAAc,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAC7I,YAAY,EAAE,eAAe,EAAE,MAAM,sBAAsB,CAAC;AAE5D,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AACrE,YAAY,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAClE,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAClE,YAAY,EAAE,WAAW,EAAE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BAAuB,CAAC;AAC5D,YAAY,EAAE,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,oBAAoB,EAAE,aAAa,EAAE,eAAe,EAAE,sBAAsB,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAC3K,YAAY,EACR,cAAc,EACd,iBAAiB,EACjB,kBAAkB,EAClB,eAAe,EACf,gBAAgB,EAChB,eAAe,EACf,cAAc,EACd,eAAe,EACf,gBAAgB,EAChB,UAAU,GACb,MAAM,iBAAiB,CAAC;AAGzB,YAAY,EAAE,GAAG,IAAI,MAAM,EAAE,MAAM,iBAAiB,CAAC;AAGrD,YAAY,EAER,MAAM,EACN,UAAU,EACV,cAAc,EACd,QAAQ,EACR,gBAAgB,EAChB,kBAAkB,EAClB,kBAAkB,EAClB,QAAQ,EACR,WAAW,EACX,SAAS,EACT,UAAU,EACV,aAAa,EACb,QAAQ,EAER,WAAW,EACX,OAAO,EACP,oBAAoB,EACpB,qBAAqB,EACrB,uBAAuB,EACvB,iBAAiB,EACjB,gBAAgB,EAChB,YAAY,EACZ,gBAAgB,EAEhB,kBAAkB,EAClB,gBAAgB,EAChB,6BAA6B,EAE7B,aAAa,EAEb,gBAAgB,EAChB,eAAe,EACf,eAAe,GAClB,MAAM,mBAAmB,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -5,8 +5,11 @@ export { AnalyzeTopLevelOrderBy, HasTopLevelOrderBy, ExtractOrderBy, findOrderBy
|
|
|
5
5
|
// Token-level SQL reading that works on any statement, including SQL the AST parser rejects
|
|
6
6
|
export { LexSQL, SignificantTokens, IsKeyword } from './sqlLexer.js';
|
|
7
7
|
export { SplitLeadingCTEs, IsReadOnlyQuery } from './sqlShape.js';
|
|
8
|
+
export { IsBalancedSQLFragment, SplitTopLevelAndTerms } from './balancedFragment.js';
|
|
8
9
|
export { AnalyzePagingShape } from './pagingShape.js';
|
|
9
10
|
export { FindForbiddenFunctionCalls } from './callerSqlFunctions.js';
|
|
11
|
+
// Screen for a caller-supplied RunView aggregate expression
|
|
12
|
+
export { CheckAggregateExpression } from './aggregateExpression.js';
|
|
10
13
|
// Structural parser and composition IR
|
|
11
14
|
export { ParseToIR, RenderIR } from './structuralParser.js';
|
|
12
15
|
//# sourceMappingURL=index.js.map
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAG5C,oEAAoE;AACpE,OAAO,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,cAAc,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAE7I,4FAA4F;AAC5F,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAErE,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAElE,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,EAAE,0BAA0B,EAAE,MAAM,yBAAyB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;AAG5C,oEAAoE;AACpE,OAAO,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,cAAc,EAAE,oBAAoB,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAC;AAE7I,4FAA4F;AAC5F,OAAO,EAAE,MAAM,EAAE,iBAAiB,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAErE,OAAO,EAAE,gBAAgB,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC;AAElE,OAAO,EAAE,qBAAqB,EAAE,qBAAqB,EAAE,MAAM,uBAAuB,CAAC;AAErF,OAAO,EAAE,kBAAkB,EAAE,MAAM,kBAAkB,CAAC;AACtD,OAAO,EAAE,0BAA0B,EAAE,MAAM,yBAAyB,CAAC;AACrE,4DAA4D;AAC5D,OAAO,EAAE,wBAAwB,EAAE,MAAM,0BAA0B,CAAC;AAGpE,uCAAuC;AACvC,OAAO,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC"}
|
package/dist/sql-parser.d.ts
CHANGED
|
@@ -165,8 +165,23 @@ export declare class SQLParser {
|
|
|
165
165
|
get IsValid(): boolean;
|
|
166
166
|
/** The raw AST. Prefer the typed accessors; this is an escape hatch. */
|
|
167
167
|
get AST(): NodeSqlParser.AST | NodeSqlParser.AST[] | null;
|
|
168
|
+
/**
|
|
169
|
+
* The bracket identifiers the fallback parse renamed, as the name in {@link AST} mapped to the
|
|
170
|
+
* original bracketed text (for example `[My CTE]`). Null when the direct parse succeeded, so
|
|
171
|
+
* every identifier in the tree is as written. A name in this map is in the tree only where the
|
|
172
|
+
* fallback put it: no such name occurs in the SQL, and none runs into the text next to it.
|
|
173
|
+
*/
|
|
174
|
+
get IdentifierAliases(): ReadonlyMap<string, string> | null;
|
|
175
|
+
/**
|
|
176
|
+
* True when `name` has the form of a name the fallback parse gives a renamed bracket
|
|
177
|
+
* identifier ({@link IdentifierAliases}), ignoring case: `_mjid_<n>`, or `_mjid<k>_<n>` when
|
|
178
|
+
* the SQL already contains `_mjid_`.
|
|
179
|
+
*/
|
|
180
|
+
static IsIdentifierAliasName(name: string): boolean;
|
|
168
181
|
/** The dialect supplied at construction. */
|
|
169
182
|
get Dialect(): SQLParserDialect;
|
|
183
|
+
/** The SQL supplied at construction, as written. */
|
|
184
|
+
get SQL(): string;
|
|
170
185
|
/**
|
|
171
186
|
* Fixes a known node-sql-parser bug where CAST(x AS NVARCHAR(MAX)) is
|
|
172
187
|
* serialized as CAST(x AS NVARCHARmax). Applies to NVARCHAR, VARCHAR,
|
|
@@ -290,6 +305,11 @@ export declare class SQLParser {
|
|
|
290
305
|
* quoted identifiers, and comments). A single trailing semicolon — or a run
|
|
291
306
|
* of them followed only by whitespace/comments — is allowed.
|
|
292
307
|
*
|
|
308
|
+
* Reads the tokens {@link LexSQL} produces for the dialect, so every quoted
|
|
309
|
+
* form the database recognizes hides its contents: `[…]` identifiers on SQL
|
|
310
|
+
* Server, `E'…'` strings with backslash escapes and dollar-quoted strings on
|
|
311
|
+
* PostgreSQL, and nested block comments on both.
|
|
312
|
+
*
|
|
293
313
|
* Unlike an AST check, this fires even when the trailing payload makes the
|
|
294
314
|
* SQL unparseable (`SELECT 1; EXEC xp_cmdshell '…'`, `SELECT 1; WAITFOR
|
|
295
315
|
* DELAY '…'`), which is exactly the stacked-injection class an AST scan
|
|
@@ -565,22 +585,42 @@ export declare class SQLParser {
|
|
|
565
585
|
* semicolon may follow it).
|
|
566
586
|
*/
|
|
567
587
|
private static splitTrailingOption;
|
|
588
|
+
/** The start of every alias name the fallback gives a bracket identifier. */
|
|
589
|
+
private static readonly BRACKET_ALIAS_STEM;
|
|
590
|
+
/** Every alias name {@link aliasPrefixFor} and {@link aliasBracketIdentifiers} can produce. */
|
|
591
|
+
private static readonly ALIAS_NAME;
|
|
592
|
+
/**
|
|
593
|
+
* An alias prefix in lower-case text, with its digits captured. The lookahead leaves the
|
|
594
|
+
* closing `_` unread, so a prefix that starts on that `_` is found too (`_mjid_mjid1_`).
|
|
595
|
+
*/
|
|
596
|
+
private static readonly ALIAS_PREFIX_IN_TEXT;
|
|
597
|
+
/** A character that would join an alias written next to it into one longer name. */
|
|
598
|
+
private static readonly NAME_CHAR;
|
|
568
599
|
/**
|
|
569
600
|
* Token-aware scan that aliases every bracket-quoted identifier. node-sql-parser
|
|
570
601
|
* cannot handle brackets around some identifiers at all — interiors with spaces
|
|
571
602
|
* or punctuation (`[Active People]`, `[my-cte]`, `[dbo.table]`), and CTE names of
|
|
572
|
-
* any kind (`WITH [x] AS …`).
|
|
573
|
-
*
|
|
603
|
+
* any kind (`WITH [x] AS …`). Applies only to bracket-quoting dialects (SQL Server).
|
|
604
|
+
*
|
|
605
|
+
* Each alias is `<prefix><seq>`, with a prefix that does not occur in the SQL
|
|
606
|
+
* ({@link aliasPrefixFor}). A space keeps an alias apart from a name character or
|
|
607
|
+
* bracket written right next to the brackets, as SQL Server keeps them apart. So
|
|
608
|
+
* an alias name appears in the rewritten SQL only where this scan put it.
|
|
574
609
|
*
|
|
575
610
|
* Returns the rewritten SQL plus a forward map (original interior → alias).
|
|
576
611
|
*/
|
|
577
|
-
private static readonly BRACKET_ALIAS_PREFIX;
|
|
578
612
|
private static aliasBracketIdentifiers;
|
|
613
|
+
/**
|
|
614
|
+
* The alias prefix for one SQL text: the first of `_mjid_`, `_mjid1_`, `_mjid2_`, … that does
|
|
615
|
+
* not occur in it, ignoring case. No name the SQL writes can then equal or contain an alias.
|
|
616
|
+
* One scan of the text collects the digits of every prefix it contains.
|
|
617
|
+
*/
|
|
618
|
+
private static aliasPrefixFor;
|
|
579
619
|
/**
|
|
580
620
|
* Reverses bracket-identifier aliasing on a sqlify result. node-sql-parser
|
|
581
621
|
* may emit the alias bare, bracketed, or backticked; all three forms are
|
|
582
|
-
* restored to the original bracketed identifier.
|
|
583
|
-
*
|
|
622
|
+
* restored to the original bracketed identifier. No alias occurs in the
|
|
623
|
+
* caller's SQL ({@link aliasPrefixFor}), so replacement is unambiguous.
|
|
584
624
|
*/
|
|
585
625
|
private static restoreAliases;
|
|
586
626
|
/**
|
package/dist/sql-parser.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"sql-parser.d.ts","sourceRoot":"","sources":["../src/sql-parser.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAEH,OAAO,aAAa,MAAM,iBAAiB,CAAC;AAK5C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,6BAA6B,CAAC;AAEpE,OAAO,EAAgD,KAAK,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAEvG,OAAO,EACH,OAA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ACH,OAAO,CAAC,MAAM,CAAC,kBAAkB;IAgBjC,2EAA2E;IAC3E,OAAO,CAAC,MAAM,CAAC,WAAW;IAU1B,OAAO,CAAC,MAAM,CAAC,mBAAmB;IAiBlC,OAAO,CAAC,MAAM,CAAC,qBAAqB;IAiBpC,OAAO,CAAC,MAAM,CAAC,iBAAiB;IAiBhC,OAAO,CAAC,MAAM,CAAC,eAAe;IAiB9B,OAAO,CAAC,MAAM,CAAC,iBAAiB;IA+BhC,OAAO,CAAC,MAAM,CAAC,mBAAmB;IA+BlC,OAAO,CAAC,MAAM,CAAC,cAAc;IAK7B,OAAO,CAAC,MAAM,CAAC,sBAAsB;IAsCrC,OAAO,CAAC,MAAM,CAAC,oBAAoB;IA+DnC,OAAO,CAAC,MAAM,CAAC,YAAY;IAqC3B,OAAO,CAAC,MAAM,CAAC,cAAc;IA6C7B,OAAO,CAAC,MAAM,CAAC,oBAAoB;IAmBnC,OAAO,CAAC,MAAM,CAAC,mBAAmB;IASlC;;;;;;OAMG;IACH,OAAO,CAAC,MAAM,CAAC,wBAAwB;IAKvC;;;;;;;;;OASG;IACH,OAAO,CAAC,MAAM,CAAC,0BAA0B;IAiBzC;;;;;;;OAOG;IACH,OAAO,CAAC,MAAM,CAAC,8BAA8B;IA8B7C,OAAO,CAAC,MAAM,CAAC,wBAAwB;IAwCvC,8EAA8E;IAC9E,OAAO,CAAC,MAAM,CAAC,QAAQ,CAAC,cAAc,CAGnC;IAEH,OAAO,CAAC,MAAM,CAAC,qBAAqB;IAmBpC,OAAO,CAAC,MAAM,CAAC,iBAAiB;CAoBnC"}
|
package/dist/sql-parser.js
CHANGED
|
@@ -83,10 +83,31 @@ export class SQLParser {
|
|
|
83
83
|
get AST() {
|
|
84
84
|
return this._ast;
|
|
85
85
|
}
|
|
86
|
+
/**
|
|
87
|
+
* The bracket identifiers the fallback parse renamed, as the name in {@link AST} mapped to the
|
|
88
|
+
* original bracketed text (for example `[My CTE]`). Null when the direct parse succeeded, so
|
|
89
|
+
* every identifier in the tree is as written. A name in this map is in the tree only where the
|
|
90
|
+
* fallback put it: no such name occurs in the SQL, and none runs into the text next to it.
|
|
91
|
+
*/
|
|
92
|
+
get IdentifierAliases() {
|
|
93
|
+
return this._aliasMap;
|
|
94
|
+
}
|
|
95
|
+
/**
|
|
96
|
+
* True when `name` has the form of a name the fallback parse gives a renamed bracket
|
|
97
|
+
* identifier ({@link IdentifierAliases}), ignoring case: `_mjid_<n>`, or `_mjid<k>_<n>` when
|
|
98
|
+
* the SQL already contains `_mjid_`.
|
|
99
|
+
*/
|
|
100
|
+
static IsIdentifierAliasName(name) {
|
|
101
|
+
return SQLParser.ALIAS_NAME.test(name);
|
|
102
|
+
}
|
|
86
103
|
/** The dialect supplied at construction. */
|
|
87
104
|
get Dialect() {
|
|
88
105
|
return this._dialect;
|
|
89
106
|
}
|
|
107
|
+
/** The SQL supplied at construction, as written. */
|
|
108
|
+
get SQL() {
|
|
109
|
+
return this._sql;
|
|
110
|
+
}
|
|
90
111
|
/**
|
|
91
112
|
* Fixes a known node-sql-parser bug where CAST(x AS NVARCHAR(MAX)) is
|
|
92
113
|
* serialized as CAST(x AS NVARCHARmax). Applies to NVARCHAR, VARCHAR,
|
|
@@ -338,6 +359,11 @@ export class SQLParser {
|
|
|
338
359
|
* quoted identifiers, and comments). A single trailing semicolon — or a run
|
|
339
360
|
* of them followed only by whitespace/comments — is allowed.
|
|
340
361
|
*
|
|
362
|
+
* Reads the tokens {@link LexSQL} produces for the dialect, so every quoted
|
|
363
|
+
* form the database recognizes hides its contents: `[…]` identifiers on SQL
|
|
364
|
+
* Server, `E'…'` strings with backslash escapes and dollar-quoted strings on
|
|
365
|
+
* PostgreSQL, and nested block comments on both.
|
|
366
|
+
*
|
|
341
367
|
* Unlike an AST check, this fires even when the trailing payload makes the
|
|
342
368
|
* SQL unparseable (`SELECT 1; EXEC xp_cmdshell '…'`, `SELECT 1; WAITFOR
|
|
343
369
|
* DELAY '…'`), which is exactly the stacked-injection class an AST scan
|
|
@@ -345,57 +371,19 @@ export class SQLParser {
|
|
|
345
371
|
* read query must be a single statement, so any internal `;` is rejected.
|
|
346
372
|
*/
|
|
347
373
|
static HasStackedStatements(sql, dialect) {
|
|
348
|
-
const quoteSample = dialect.QuoteIdentifier('x');
|
|
349
|
-
const recognizeBrackets = quoteSample.startsWith('[');
|
|
350
|
-
const recognizeBackticks = quoteSample.startsWith('`');
|
|
351
|
-
const n = sql.length;
|
|
352
|
-
let i = 0;
|
|
353
374
|
let sawSemicolon = false;
|
|
354
375
|
// A semicolon before any content (the `;WITH` idiom) separates nothing.
|
|
355
376
|
let sawContent = false;
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
// line / block comments
|
|
359
|
-
if (c === '-' && i + 1 < n && sql[i + 1] === '-') {
|
|
360
|
-
while (i < n && sql[i] !== '\n')
|
|
361
|
-
i++;
|
|
362
|
-
continue;
|
|
363
|
-
}
|
|
364
|
-
if (c === '/' && i + 1 < n && sql[i + 1] === '*') {
|
|
365
|
-
i += 2;
|
|
366
|
-
while (i < n && !(sql[i] === '*' && i + 1 < n && sql[i + 1] === '/'))
|
|
367
|
-
i++;
|
|
368
|
-
if (i < n)
|
|
369
|
-
i += 2;
|
|
370
|
-
continue;
|
|
371
|
-
}
|
|
372
|
-
// string literals + quoted identifiers (skip wholesale)
|
|
373
|
-
if (c === "'" || c === '"' ||
|
|
374
|
-
(recognizeBrackets && c === '[') ||
|
|
375
|
-
(recognizeBackticks && c === '`')) {
|
|
376
|
-
const close = c === '[' ? ']' : c;
|
|
377
|
-
if (sawSemicolon)
|
|
378
|
-
return true;
|
|
379
|
-
sawContent = true;
|
|
380
|
-
i = SQLParser.skipQuotedFrom(sql, i, close);
|
|
381
|
-
continue;
|
|
382
|
-
}
|
|
383
|
-
if (c === ';') {
|
|
377
|
+
for (const token of SignificantTokens(LexSQL(sql, dialect))) {
|
|
378
|
+
if (token.Kind === 'semicolon') {
|
|
384
379
|
if (sawContent)
|
|
385
380
|
sawSemicolon = true;
|
|
386
|
-
i++;
|
|
387
|
-
continue;
|
|
388
|
-
}
|
|
389
|
-
if (c === ' ' || c === '\t' || c === '\n' || c === '\r') {
|
|
390
|
-
i++;
|
|
391
381
|
continue;
|
|
392
382
|
}
|
|
393
|
-
//
|
|
394
|
-
// semicolon already appeared, this content is a second statement
|
|
383
|
+
// Real content after a separating semicolon is a second statement.
|
|
395
384
|
if (sawSemicolon)
|
|
396
385
|
return true;
|
|
397
386
|
sawContent = true;
|
|
398
|
-
i++;
|
|
399
387
|
}
|
|
400
388
|
return false;
|
|
401
389
|
}
|
|
@@ -1470,20 +1458,35 @@ export class SQLParser {
|
|
|
1470
1458
|
optionClause: sql.substring(tokens[open].Start, tokens[close].End).trim(),
|
|
1471
1459
|
};
|
|
1472
1460
|
}
|
|
1461
|
+
/** The start of every alias name the fallback gives a bracket identifier. */
|
|
1462
|
+
static { this.BRACKET_ALIAS_STEM = '_mjid'; }
|
|
1463
|
+
/** Every alias name {@link aliasPrefixFor} and {@link aliasBracketIdentifiers} can produce. */
|
|
1464
|
+
static { this.ALIAS_NAME = /^_mjid\d*_\d+$/i; }
|
|
1465
|
+
/**
|
|
1466
|
+
* An alias prefix in lower-case text, with its digits captured. The lookahead leaves the
|
|
1467
|
+
* closing `_` unread, so a prefix that starts on that `_` is found too (`_mjid_mjid1_`).
|
|
1468
|
+
*/
|
|
1469
|
+
static { this.ALIAS_PREFIX_IN_TEXT = /_mjid(\d*)(?=_)/g; }
|
|
1470
|
+
/** A character that would join an alias written next to it into one longer name. */
|
|
1471
|
+
static { this.NAME_CHAR = /[\w@#$\u0080-\uffff]/; }
|
|
1473
1472
|
/**
|
|
1474
1473
|
* Token-aware scan that aliases every bracket-quoted identifier. node-sql-parser
|
|
1475
1474
|
* cannot handle brackets around some identifiers at all — interiors with spaces
|
|
1476
1475
|
* or punctuation (`[Active People]`, `[my-cte]`, `[dbo.table]`), and CTE names of
|
|
1477
|
-
* any kind (`WITH [x] AS …`).
|
|
1478
|
-
*
|
|
1476
|
+
* any kind (`WITH [x] AS …`). Applies only to bracket-quoting dialects (SQL Server).
|
|
1477
|
+
*
|
|
1478
|
+
* Each alias is `<prefix><seq>`, with a prefix that does not occur in the SQL
|
|
1479
|
+
* ({@link aliasPrefixFor}). A space keeps an alias apart from a name character or
|
|
1480
|
+
* bracket written right next to the brackets, as SQL Server keeps them apart. So
|
|
1481
|
+
* an alias name appears in the rewritten SQL only where this scan put it.
|
|
1479
1482
|
*
|
|
1480
1483
|
* Returns the rewritten SQL plus a forward map (original interior → alias).
|
|
1481
1484
|
*/
|
|
1482
|
-
static { this.BRACKET_ALIAS_PREFIX = '_mjid_'; }
|
|
1483
1485
|
static aliasBracketIdentifiers(sql, dialect) {
|
|
1484
1486
|
const forward = new Map();
|
|
1485
1487
|
if (!dialect.QuoteIdentifier('x').startsWith('['))
|
|
1486
1488
|
return { rewritten: sql, forward };
|
|
1489
|
+
const prefix = SQLParser.aliasPrefixFor(sql);
|
|
1487
1490
|
const n = sql.length;
|
|
1488
1491
|
let out = '';
|
|
1489
1492
|
let i = 0;
|
|
@@ -1537,13 +1540,17 @@ export class SQLParser {
|
|
|
1537
1540
|
if (interior.length > 0) {
|
|
1538
1541
|
let alias = forward.get(interior);
|
|
1539
1542
|
if (!alias) {
|
|
1540
|
-
alias = `${
|
|
1543
|
+
alias = `${prefix}${seq++}`;
|
|
1541
1544
|
forward.set(interior, alias);
|
|
1542
1545
|
}
|
|
1543
1546
|
// Emit a BARE identifier — node-sql-parser rejects bracket-quoted
|
|
1544
1547
|
// CTE names entirely, so the alias must be unbracketed. sqlify
|
|
1545
1548
|
// re-quotes it ([_mjid_0]); restoreAliases handles every form.
|
|
1546
|
-
out
|
|
1549
|
+
// The character before the brackets comes from `sql`. A read from `out`, which
|
|
1550
|
+
// grows by `+=`, copies all of `out` each time.
|
|
1551
|
+
const before = i > 0 && SQLParser.NAME_CHAR.test(sql[i - 1]) ? ' ' : '';
|
|
1552
|
+
const after = j < n && (SQLParser.NAME_CHAR.test(sql[j]) || sql[j] === '[') ? ' ' : '';
|
|
1553
|
+
out += `${before}${alias}${after}`;
|
|
1547
1554
|
}
|
|
1548
1555
|
else {
|
|
1549
1556
|
out += sql.substring(i, j); // leave non-problematic identifiers untouched
|
|
@@ -1556,11 +1563,25 @@ export class SQLParser {
|
|
|
1556
1563
|
}
|
|
1557
1564
|
return { rewritten: out, forward };
|
|
1558
1565
|
}
|
|
1566
|
+
/**
|
|
1567
|
+
* The alias prefix for one SQL text: the first of `_mjid_`, `_mjid1_`, `_mjid2_`, … that does
|
|
1568
|
+
* not occur in it, ignoring case. No name the SQL writes can then equal or contain an alias.
|
|
1569
|
+
* One scan of the text collects the digits of every prefix it contains.
|
|
1570
|
+
*/
|
|
1571
|
+
static aliasPrefixFor(sql) {
|
|
1572
|
+
const used = new Set();
|
|
1573
|
+
for (const match of sql.toLowerCase().matchAll(SQLParser.ALIAS_PREFIX_IN_TEXT))
|
|
1574
|
+
used.add(match[1]);
|
|
1575
|
+
let digits = '';
|
|
1576
|
+
for (let k = 1; used.has(digits); k++)
|
|
1577
|
+
digits = String(k);
|
|
1578
|
+
return `${SQLParser.BRACKET_ALIAS_STEM}${digits}_`;
|
|
1579
|
+
}
|
|
1559
1580
|
/**
|
|
1560
1581
|
* Reverses bracket-identifier aliasing on a sqlify result. node-sql-parser
|
|
1561
1582
|
* may emit the alias bare, bracketed, or backticked; all three forms are
|
|
1562
|
-
* restored to the original bracketed identifier.
|
|
1563
|
-
*
|
|
1583
|
+
* restored to the original bracketed identifier. No alias occurs in the
|
|
1584
|
+
* caller's SQL ({@link aliasPrefixFor}), so replacement is unambiguous.
|
|
1564
1585
|
*/
|
|
1565
1586
|
static restoreAliases(sql, aliasMap) {
|
|
1566
1587
|
let out = sql;
|