dt-sql-parser 4.5.2-beta.1 → 4.5.2-beta.2
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 +8 -8
- package/dist/lib/generic/GenericSqlListener.d.ts +90 -5
- package/dist/lib/generic/GenericSqlParser.d.ts +102 -33
- package/dist/lib/generic/GenericSqlParser.js +1329 -930
- package/dist/lib/generic/GenericSqlVisitor.d.ts +56 -3
- package/dist/parser/common/entityCollector.js +5 -0
- package/dist/parser/generic/genericEntityCollector.d.ts +6 -3
- package/dist/parser/generic/genericEntityCollector.js +30 -42
- package/dist/parser/generic/index.js +13 -10
- package/package.json +1 -1
|
@@ -18,7 +18,10 @@ import { QueryTermContext } from "./GenericSqlParser.js";
|
|
|
18
18
|
import { QueryPrimaryContext } from "./GenericSqlParser.js";
|
|
19
19
|
import { QuerySpecificationContext } from "./GenericSqlParser.js";
|
|
20
20
|
import { SetQuantifierContext } from "./GenericSqlParser.js";
|
|
21
|
-
import {
|
|
21
|
+
import { SelectExpressionElementContext } from "./GenericSqlParser.js";
|
|
22
|
+
import { SelectStarElementContext } from "./GenericSqlParser.js";
|
|
23
|
+
import { SelectAllElementContext } from "./GenericSqlParser.js";
|
|
24
|
+
import { SelectEmptyElementContext } from "./GenericSqlParser.js";
|
|
22
25
|
import { FromClauseContext } from "./GenericSqlParser.js";
|
|
23
26
|
import { SimpleRelationContext } from "./GenericSqlParser.js";
|
|
24
27
|
import { JoinRelationContext } from "./GenericSqlParser.js";
|
|
@@ -40,6 +43,7 @@ import { DeleteStatementContext } from "./GenericSqlParser.js";
|
|
|
40
43
|
import { CreateTableStatementContext } from "./GenericSqlParser.js";
|
|
41
44
|
import { TableElementContext } from "./GenericSqlParser.js";
|
|
42
45
|
import { ColumnDefinitionContext } from "./GenericSqlParser.js";
|
|
46
|
+
import { ColumnRefCreateContext } from "./GenericSqlParser.js";
|
|
43
47
|
import { TableConstraintContext } from "./GenericSqlParser.js";
|
|
44
48
|
import { AlterTableStatementContext } from "./GenericSqlParser.js";
|
|
45
49
|
import { DropTableStatementContext } from "./GenericSqlParser.js";
|
|
@@ -87,8 +91,11 @@ import { BinaryLiteralContext } from "./GenericSqlParser.js";
|
|
|
87
91
|
import { IdentifierContext } from "./GenericSqlParser.js";
|
|
88
92
|
import { QualifiedNameContext } from "./GenericSqlParser.js";
|
|
89
93
|
import { ColumnRefContext } from "./GenericSqlParser.js";
|
|
94
|
+
import { ColumnNameContext } from "./GenericSqlParser.js";
|
|
95
|
+
import { EmptyColumnContext } from "./GenericSqlParser.js";
|
|
90
96
|
import { TableNameContext } from "./GenericSqlParser.js";
|
|
91
97
|
import { TableNameCreateContext } from "./GenericSqlParser.js";
|
|
98
|
+
import { FunctionNameContext } from "./GenericSqlParser.js";
|
|
92
99
|
import { NonReservedContext } from "./GenericSqlParser.js";
|
|
93
100
|
/**
|
|
94
101
|
* This interface defines a complete generic visitor for a parse tree produced
|
|
@@ -220,11 +227,33 @@ export declare class GenericSqlVisitor<Result> extends AbstractParseTreeVisitor<
|
|
|
220
227
|
*/
|
|
221
228
|
visitSetQuantifier?: (ctx: SetQuantifierContext) => Result;
|
|
222
229
|
/**
|
|
223
|
-
* Visit a parse tree produced by `
|
|
230
|
+
* Visit a parse tree produced by the `selectExpressionElement`
|
|
231
|
+
* labeled alternative in `GenericSqlParser.selectItem`.
|
|
224
232
|
* @param ctx the parse tree
|
|
225
233
|
* @return the visitor result
|
|
226
234
|
*/
|
|
227
|
-
|
|
235
|
+
visitSelectExpressionElement?: (ctx: SelectExpressionElementContext) => Result;
|
|
236
|
+
/**
|
|
237
|
+
* Visit a parse tree produced by the `selectStarElement`
|
|
238
|
+
* labeled alternative in `GenericSqlParser.selectItem`.
|
|
239
|
+
* @param ctx the parse tree
|
|
240
|
+
* @return the visitor result
|
|
241
|
+
*/
|
|
242
|
+
visitSelectStarElement?: (ctx: SelectStarElementContext) => Result;
|
|
243
|
+
/**
|
|
244
|
+
* Visit a parse tree produced by the `selectAllElement`
|
|
245
|
+
* labeled alternative in `GenericSqlParser.selectItem`.
|
|
246
|
+
* @param ctx the parse tree
|
|
247
|
+
* @return the visitor result
|
|
248
|
+
*/
|
|
249
|
+
visitSelectAllElement?: (ctx: SelectAllElementContext) => Result;
|
|
250
|
+
/**
|
|
251
|
+
* Visit a parse tree produced by the `selectEmptyElement`
|
|
252
|
+
* labeled alternative in `GenericSqlParser.selectItem`.
|
|
253
|
+
* @param ctx the parse tree
|
|
254
|
+
* @return the visitor result
|
|
255
|
+
*/
|
|
256
|
+
visitSelectEmptyElement?: (ctx: SelectEmptyElementContext) => Result;
|
|
228
257
|
/**
|
|
229
258
|
* Visit a parse tree produced by `GenericSqlParser.fromClause`.
|
|
230
259
|
* @param ctx the parse tree
|
|
@@ -355,6 +384,12 @@ export declare class GenericSqlVisitor<Result> extends AbstractParseTreeVisitor<
|
|
|
355
384
|
* @return the visitor result
|
|
356
385
|
*/
|
|
357
386
|
visitColumnDefinition?: (ctx: ColumnDefinitionContext) => Result;
|
|
387
|
+
/**
|
|
388
|
+
* Visit a parse tree produced by `GenericSqlParser.columnRefCreate`.
|
|
389
|
+
* @param ctx the parse tree
|
|
390
|
+
* @return the visitor result
|
|
391
|
+
*/
|
|
392
|
+
visitColumnRefCreate?: (ctx: ColumnRefCreateContext) => Result;
|
|
358
393
|
/**
|
|
359
394
|
* Visit a parse tree produced by `GenericSqlParser.tableConstraint`.
|
|
360
395
|
* @param ctx the parse tree
|
|
@@ -670,6 +705,18 @@ export declare class GenericSqlVisitor<Result> extends AbstractParseTreeVisitor<
|
|
|
670
705
|
* @return the visitor result
|
|
671
706
|
*/
|
|
672
707
|
visitColumnRef?: (ctx: ColumnRefContext) => Result;
|
|
708
|
+
/**
|
|
709
|
+
* Visit a parse tree produced by `GenericSqlParser.columnName`.
|
|
710
|
+
* @param ctx the parse tree
|
|
711
|
+
* @return the visitor result
|
|
712
|
+
*/
|
|
713
|
+
visitColumnName?: (ctx: ColumnNameContext) => Result;
|
|
714
|
+
/**
|
|
715
|
+
* Visit a parse tree produced by `GenericSqlParser.emptyColumn`.
|
|
716
|
+
* @param ctx the parse tree
|
|
717
|
+
* @return the visitor result
|
|
718
|
+
*/
|
|
719
|
+
visitEmptyColumn?: (ctx: EmptyColumnContext) => Result;
|
|
673
720
|
/**
|
|
674
721
|
* Visit a parse tree produced by `GenericSqlParser.tableName`.
|
|
675
722
|
* @param ctx the parse tree
|
|
@@ -682,6 +729,12 @@ export declare class GenericSqlVisitor<Result> extends AbstractParseTreeVisitor<
|
|
|
682
729
|
* @return the visitor result
|
|
683
730
|
*/
|
|
684
731
|
visitTableNameCreate?: (ctx: TableNameCreateContext) => Result;
|
|
732
|
+
/**
|
|
733
|
+
* Visit a parse tree produced by `GenericSqlParser.functionName`.
|
|
734
|
+
* @param ctx the parse tree
|
|
735
|
+
* @return the visitor result
|
|
736
|
+
*/
|
|
737
|
+
visitFunctionName?: (ctx: FunctionNameContext) => Result;
|
|
685
738
|
/**
|
|
686
739
|
* Visit a parse tree produced by `GenericSqlParser.nonReserved`.
|
|
687
740
|
* @param ctx the parse tree
|
|
@@ -233,6 +233,11 @@ const CTE_CLAUSE_CONTEXT_NAMES = new Set([
|
|
|
233
233
|
'CtesContext',
|
|
234
234
|
'WithContext',
|
|
235
235
|
]);
|
|
236
|
+
/**
|
|
237
|
+
* Strip outer quotes/brackets and lowercase for CTE name matching.
|
|
238
|
+
* Quoted forms are lowercased too on purpose — loose match for completion UX
|
|
239
|
+
* across dialects, not dialect-accurate case folding (e.g. PostgreSQL quoted ids).
|
|
240
|
+
*/
|
|
236
241
|
function normalizeIdent(text) {
|
|
237
242
|
let value = text.trim();
|
|
238
243
|
if ((value.startsWith('`') && value.endsWith('`') && value.length >= 2) ||
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { GenericSqlListener } from '../../lib/generic/GenericSqlListener';
|
|
2
|
-
import { AlterTableContext,
|
|
2
|
+
import { AlterTableContext, ColumnRefContext, ColumnRefCreateContext, CreateTableContext, DeleteContext, DropTableContext, InsertContext, NamedQueryContext, QuerySpecificationContext, QueryStatementContext, SelectAllElementContext, SelectEmptyElementContext, SelectExpressionElementContext, SelectStarElementContext, SingleStatementContext, TableNameContext, TableNameCreateContext, UpdateContext } from '../../lib/generic/GenericSqlParser';
|
|
3
3
|
import { CommonEntityContext, EntityCollector } from '../common/entityCollector';
|
|
4
4
|
export declare class GenericEntityCollector extends EntityCollector implements GenericSqlListener {
|
|
5
5
|
/** Select-list source text for each query result. The result itself keeps the full statement. */
|
|
@@ -10,10 +10,13 @@ export declare class GenericEntityCollector extends EntityCollector implements G
|
|
|
10
10
|
exitNamedQuery(ctx: NamedQueryContext): void;
|
|
11
11
|
exitTableNameCreate(ctx: TableNameCreateContext): void;
|
|
12
12
|
exitColumnRef(ctx: ColumnRefContext): void;
|
|
13
|
-
|
|
13
|
+
exitColumnRefCreate(ctx: ColumnRefCreateContext): void;
|
|
14
14
|
exitQuerySpecification(ctx: QuerySpecificationContext): void;
|
|
15
15
|
protected adaptOutwardQueryResult(entity: CommonEntityContext): CommonEntityContext;
|
|
16
|
-
|
|
16
|
+
exitSelectExpressionElement(ctx: SelectExpressionElementContext): void;
|
|
17
|
+
exitSelectStarElement(ctx: SelectStarElementContext): void;
|
|
18
|
+
exitSelectAllElement(ctx: SelectAllElementContext): void;
|
|
19
|
+
exitSelectEmptyElement(ctx: SelectEmptyElementContext): void;
|
|
17
20
|
private isSimpleColumnReference;
|
|
18
21
|
/** ===== Statement begin */
|
|
19
22
|
enterSingleStatement(ctx: SingleStatementContext): void;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ColumnDefinitionContext, AliasedRelationContext, CreateTableStatementContext, } from '../../lib/generic/GenericSqlParser';
|
|
2
|
-
import { AttrName, ColumnDeclareType, EntityCollector,
|
|
2
|
+
import { AttrName, ColumnDeclareType, EntityCollector, isCommonEntityContext, StmtContextType, TableDeclareType, } from '../common/entityCollector';
|
|
3
3
|
import { EntityContextType } from '../common/types';
|
|
4
4
|
import { tokenToWord } from '../common/textAndWord';
|
|
5
5
|
export class GenericEntityCollector extends EntityCollector {
|
|
@@ -38,33 +38,17 @@ export class GenericEntityCollector extends EntityCollector {
|
|
|
38
38
|
exitColumnRef(ctx) {
|
|
39
39
|
this.pushEntity(ctx, EntityContextType.COLUMN);
|
|
40
40
|
}
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
41
|
+
exitColumnRefCreate(ctx) {
|
|
42
|
+
this.pushEntity(ctx, EntityContextType.COLUMN_CREATE, [
|
|
43
|
+
{
|
|
44
|
+
attrName: AttrName.colType,
|
|
45
|
+
endContextList: [ColumnDefinitionContext.name],
|
|
46
|
+
},
|
|
46
47
|
{
|
|
47
48
|
attrName: AttrName.comment,
|
|
48
49
|
endContextList: [ColumnDefinitionContext.name],
|
|
49
50
|
},
|
|
50
51
|
]);
|
|
51
|
-
const dataType = ctx.dataType();
|
|
52
|
-
if (entity && isColumnEntityContext(entity) && (dataType === null || dataType === void 0 ? void 0 : dataType.start) && dataType.stop) {
|
|
53
|
-
const startIndex = dataType.start.start;
|
|
54
|
-
const endIndex = dataType.stop.stop;
|
|
55
|
-
const typeText = this.sourceSlice(startIndex, endIndex);
|
|
56
|
-
const startColumn = dataType.start.column + 1;
|
|
57
|
-
entity[AttrName.colType] = {
|
|
58
|
-
text: typeText,
|
|
59
|
-
line: dataType.start.line,
|
|
60
|
-
startIndex,
|
|
61
|
-
endIndex,
|
|
62
|
-
startColumn,
|
|
63
|
-
endColumn: dataType.stop.line === dataType.start.line
|
|
64
|
-
? startColumn + typeText.length
|
|
65
|
-
: dataType.stop.column + 1 + ((_b = (_a = dataType.stop.text) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0),
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
52
|
}
|
|
69
53
|
exitQuerySpecification(ctx) {
|
|
70
54
|
var _a, _b;
|
|
@@ -82,28 +66,32 @@ export class GenericEntityCollector extends EntityCollector {
|
|
|
82
66
|
return entity;
|
|
83
67
|
return Object.assign(Object.assign({}, entity), { text });
|
|
84
68
|
}
|
|
85
|
-
|
|
69
|
+
exitSelectExpressionElement(ctx) {
|
|
86
70
|
var _a;
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
const entity = this.pushEntity(ctx, EntityContextType.COLUMN, [], {
|
|
95
|
-
declareType: isSimpleColumn
|
|
96
|
-
? ColumnDeclareType.LITERAL
|
|
97
|
-
: ColumnDeclareType.EXPRESSION,
|
|
98
|
-
});
|
|
99
|
-
// The alias sits on this select item. Searching parents would pick a
|
|
100
|
-
// later item's alias and label every column with that name.
|
|
101
|
-
const aliasToken = (_a = ctx._alias) === null || _a === void 0 ? void 0 : _a.start;
|
|
102
|
-
if (entity && aliasToken) {
|
|
103
|
-
entity[AttrName.alias] = tokenToWord(aliasToken, '');
|
|
104
|
-
}
|
|
71
|
+
const isSimpleColumn = this.isSimpleColumnReference(ctx);
|
|
72
|
+
const entity = this.pushEntity(ctx, EntityContextType.COLUMN, [], {
|
|
73
|
+
declareType: isSimpleColumn ? ColumnDeclareType.LITERAL : ColumnDeclareType.EXPRESSION,
|
|
74
|
+
});
|
|
75
|
+
const aliasToken = (_a = ctx._alias) === null || _a === void 0 ? void 0 : _a.start;
|
|
76
|
+
if (entity && aliasToken) {
|
|
77
|
+
entity[AttrName.alias] = tokenToWord(aliasToken, '');
|
|
105
78
|
}
|
|
106
79
|
}
|
|
80
|
+
exitSelectStarElement(ctx) {
|
|
81
|
+
this.pushEntity(ctx, EntityContextType.COLUMN, [], {
|
|
82
|
+
declareType: ColumnDeclareType.ALL,
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
exitSelectAllElement(ctx) {
|
|
86
|
+
this.pushEntity(ctx, EntityContextType.COLUMN, [], {
|
|
87
|
+
declareType: ColumnDeclareType.ALL,
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
exitSelectEmptyElement(ctx) {
|
|
91
|
+
this.pushEntity(ctx, EntityContextType.COLUMN, [], {
|
|
92
|
+
declareType: ColumnDeclareType.LITERAL,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
107
95
|
isSimpleColumnReference(ctx) {
|
|
108
96
|
const text = ctx.getText();
|
|
109
97
|
// Simple column reference: no parentheses, no operators, no spaces
|
|
@@ -19,7 +19,9 @@ export class GenericSQL extends BasicSQL {
|
|
|
19
19
|
GenericSqlParser.RULE_tableName,
|
|
20
20
|
GenericSqlParser.RULE_tableNameCreate,
|
|
21
21
|
GenericSqlParser.RULE_columnRef,
|
|
22
|
-
GenericSqlParser.
|
|
22
|
+
GenericSqlParser.RULE_columnRefCreate,
|
|
23
|
+
GenericSqlParser.RULE_columnName,
|
|
24
|
+
GenericSqlParser.RULE_functionName,
|
|
23
25
|
...this.excludeKeywordRules,
|
|
24
26
|
]);
|
|
25
27
|
this._diagnostics = (options === null || options === void 0 ? void 0 : options.diagnostics) !== false;
|
|
@@ -69,15 +71,16 @@ export class GenericSQL extends BasicSQL {
|
|
|
69
71
|
syntaxContextType = EntityContextType.COLUMN;
|
|
70
72
|
break;
|
|
71
73
|
}
|
|
72
|
-
case GenericSqlParser.
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
74
|
+
case GenericSqlParser.RULE_columnRefCreate: {
|
|
75
|
+
syntaxContextType = EntityContextType.COLUMN_CREATE;
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
case GenericSqlParser.RULE_columnName: {
|
|
79
|
+
syntaxContextType = EntityContextType.COLUMN;
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
case GenericSqlParser.RULE_functionName: {
|
|
83
|
+
syntaxContextType = EntityContextType.FUNCTION;
|
|
81
84
|
break;
|
|
82
85
|
}
|
|
83
86
|
default:
|