dt-sql-parser 4.1.2-beta.0 → 4.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (87) hide show
  1. package/README.md +51 -0
  2. package/dist/lib/flink/FlinkSqlLexer.d.ts +5 -5
  3. package/dist/lib/flink/FlinkSqlLexer.js +1991 -1992
  4. package/dist/lib/flink/FlinkSqlParser.d.ts +240 -230
  5. package/dist/lib/flink/FlinkSqlParser.js +3036 -2978
  6. package/dist/lib/flink/FlinkSqlParserListener.d.ts +81 -70
  7. package/dist/lib/flink/FlinkSqlParserVisitor.d.ts +49 -42
  8. package/dist/lib/hive/HiveSqlParser.d.ts +251 -231
  9. package/dist/lib/hive/HiveSqlParser.js +6264 -5674
  10. package/dist/lib/hive/HiveSqlParserListener.d.ts +32 -10
  11. package/dist/lib/hive/HiveSqlParserVisitor.d.ts +20 -6
  12. package/dist/lib/impala/ImpalaSqlLexer.d.ts +2 -2
  13. package/dist/lib/impala/ImpalaSqlLexer.js +587 -588
  14. package/dist/lib/impala/ImpalaSqlParser.d.ts +151 -113
  15. package/dist/lib/impala/ImpalaSqlParser.js +3464 -3250
  16. package/dist/lib/impala/ImpalaSqlParserListener.d.ts +49 -5
  17. package/dist/lib/impala/ImpalaSqlParserVisitor.d.ts +31 -3
  18. package/dist/lib/mysql/MySqlLexer.d.ts +2 -2
  19. package/dist/lib/mysql/MySqlLexer.js +3515 -3516
  20. package/dist/lib/mysql/MySqlParser.d.ts +138 -138
  21. package/dist/lib/mysql/MySqlParser.js +511 -511
  22. package/dist/lib/mysql/MySqlParserListener.d.ts +105 -105
  23. package/dist/lib/mysql/MySqlParserVisitor.d.ts +63 -63
  24. package/dist/lib/postgresql/PostgreSqlLexer.d.ts +14 -15
  25. package/dist/lib/postgresql/PostgreSqlLexer.js +2077 -2092
  26. package/dist/lib/postgresql/PostgreSqlParser.d.ts +2860 -2980
  27. package/dist/lib/postgresql/PostgreSqlParser.js +20155 -20660
  28. package/dist/lib/postgresql/PostgreSqlParserListener.d.ts +2110 -2181
  29. package/dist/lib/postgresql/PostgreSqlParserVisitor.d.ts +1261 -1301
  30. package/dist/lib/spark/SparkSqlLexer.d.ts +2 -2
  31. package/dist/lib/spark/SparkSqlLexer.js +949 -950
  32. package/dist/lib/spark/SparkSqlParser.d.ts +413 -645
  33. package/dist/lib/spark/SparkSqlParser.js +6840 -8200
  34. package/dist/lib/spark/SparkSqlParserListener.d.ts +68 -356
  35. package/dist/lib/spark/SparkSqlParserVisitor.d.ts +42 -225
  36. package/dist/lib/trino/TrinoSqlLexer.d.ts +2 -2
  37. package/dist/lib/trino/TrinoSqlLexer.js +319 -321
  38. package/dist/lib/trino/TrinoSqlListener.d.ts +67 -23
  39. package/dist/lib/trino/TrinoSqlParser.d.ts +210 -181
  40. package/dist/lib/trino/TrinoSqlParser.js +4854 -4695
  41. package/dist/lib/trino/TrinoSqlVisitor.d.ts +42 -14
  42. package/dist/parser/common/basicSQL.d.ts +43 -9
  43. package/dist/parser/common/basicSQL.js +147 -40
  44. package/dist/parser/common/semanticContextCollector.d.ts +50 -0
  45. package/dist/parser/common/semanticContextCollector.js +202 -0
  46. package/dist/parser/common/tokenUtils.d.ts +12 -0
  47. package/dist/parser/common/tokenUtils.js +38 -0
  48. package/dist/parser/common/types.d.ts +25 -0
  49. package/dist/parser/common/types.js +7 -0
  50. package/dist/parser/flink/flinkErrorListener.js +14 -2
  51. package/dist/parser/flink/flinkSemanticContextCollector.d.ts +9 -0
  52. package/dist/parser/flink/flinkSemanticContextCollector.js +14 -0
  53. package/dist/parser/flink/index.d.ts +4 -2
  54. package/dist/parser/flink/index.js +22 -14
  55. package/dist/parser/hive/hiveErrorListener.js +9 -3
  56. package/dist/parser/hive/hiveSemanticContextCollector.d.ts +9 -0
  57. package/dist/parser/hive/hiveSemanticContextCollector.js +14 -0
  58. package/dist/parser/hive/index.d.ts +4 -2
  59. package/dist/parser/hive/index.js +29 -14
  60. package/dist/parser/impala/ImpalaErrorListener.js +8 -2
  61. package/dist/parser/impala/impalaSemanticContextCollector.d.ts +9 -0
  62. package/dist/parser/impala/impalaSemanticContextCollector.js +14 -0
  63. package/dist/parser/impala/index.d.ts +4 -2
  64. package/dist/parser/impala/index.js +21 -14
  65. package/dist/parser/mysql/index.d.ts +4 -2
  66. package/dist/parser/mysql/index.js +10 -14
  67. package/dist/parser/mysql/mysqlErrorListener.js +6 -2
  68. package/dist/parser/mysql/mysqlSemanticContextCollector.d.ts +9 -0
  69. package/dist/parser/mysql/mysqlSemanticContextCollector.js +14 -0
  70. package/dist/parser/postgresql/index.d.ts +4 -2
  71. package/dist/parser/postgresql/index.js +51 -42
  72. package/dist/parser/postgresql/postgreEntityCollector.d.ts +5 -5
  73. package/dist/parser/postgresql/postgreEntityCollector.js +7 -7
  74. package/dist/parser/postgresql/postgreErrorListener.js +36 -30
  75. package/dist/parser/postgresql/postgreSemanticContextCollector.d.ts +9 -0
  76. package/dist/parser/postgresql/postgreSemanticContextCollector.js +14 -0
  77. package/dist/parser/spark/index.d.ts +4 -2
  78. package/dist/parser/spark/index.js +26 -14
  79. package/dist/parser/spark/sparkErrorListener.js +9 -3
  80. package/dist/parser/spark/sparkSemanticContextCollector.d.ts +9 -0
  81. package/dist/parser/spark/sparkSemanticContextCollector.js +14 -0
  82. package/dist/parser/trino/index.d.ts +4 -2
  83. package/dist/parser/trino/index.js +22 -14
  84. package/dist/parser/trino/trinoErrorListener.js +9 -3
  85. package/dist/parser/trino/trinoSemanticContextCollector.d.ts +9 -0
  86. package/dist/parser/trino/trinoSemanticContextCollector.js +14 -0
  87. package/package.json +2 -1
@@ -13,6 +13,7 @@ export class ImpalaErrorListener extends ParseErrorListener {
13
13
  [ImpalaSqlParser.RULE_viewNameCreate, 'view'],
14
14
  [ImpalaSqlParser.RULE_functionNamePath, 'function'],
15
15
  [ImpalaSqlParser.RULE_functionNameCreate, 'function'],
16
+ [ImpalaSqlParser.RULE_columnName, 'column'],
16
17
  [ImpalaSqlParser.RULE_columnNamePath, 'column'],
17
18
  [ImpalaSqlParser.RULE_columnNamePathCreate, 'column'],
18
19
  ]);
@@ -48,8 +49,11 @@ export class ImpalaErrorListener extends ParseErrorListener {
48
49
  case ImpalaSqlParser.RULE_tableNamePath:
49
50
  case ImpalaSqlParser.RULE_functionNamePath:
50
51
  case ImpalaSqlParser.RULE_viewNamePath:
52
+ case ImpalaSqlParser.RULE_columnName:
51
53
  case ImpalaSqlParser.RULE_columnNamePath: {
52
- result.push(`{existing}${name}`);
54
+ if (!result.includes(`{existing}${name}`)) {
55
+ result.push(`{existing}${name}`);
56
+ }
53
57
  break;
54
58
  }
55
59
  case ImpalaSqlParser.RULE_databaseNameCreate:
@@ -57,7 +61,9 @@ export class ImpalaErrorListener extends ParseErrorListener {
57
61
  case ImpalaSqlParser.RULE_functionNameCreate:
58
62
  case ImpalaSqlParser.RULE_viewNameCreate:
59
63
  case ImpalaSqlParser.RULE_columnNamePathCreate: {
60
- result.push(`{new}${name}`);
64
+ if (!result.includes(`{new}${name}`)) {
65
+ result.push(`{new}${name}`);
66
+ }
61
67
  break;
62
68
  }
63
69
  }
@@ -0,0 +1,9 @@
1
+ import { ImpalaSqlParserListener } from '../../lib';
2
+ import { SingleStatementContext } from '../../lib/impala/ImpalaSqlParser';
3
+ import SemanticContextCollector from '../common/semanticContextCollector';
4
+ declare class ImpalaSemanticContextCollector extends SemanticContextCollector implements ImpalaSqlParserListener {
5
+ getWhiteSpaceRuleType(): number;
6
+ getStatementRuleType(): number;
7
+ enterSingleStatement(ctx: SingleStatementContext): void;
8
+ }
9
+ export { ImpalaSemanticContextCollector };
@@ -0,0 +1,14 @@
1
+ import { ImpalaSqlParser } from '../../lib/impala/ImpalaSqlParser';
2
+ import SemanticContextCollector from '../common/semanticContextCollector';
3
+ class ImpalaSemanticContextCollector extends SemanticContextCollector {
4
+ getWhiteSpaceRuleType() {
5
+ return ImpalaSqlParser.WHITE_SPACE;
6
+ }
7
+ getStatementRuleType() {
8
+ return ImpalaSqlParser.RULE_singleStatement;
9
+ }
10
+ enterSingleStatement(ctx) {
11
+ this.visitStatement(ctx);
12
+ }
13
+ }
14
+ export { ImpalaSemanticContextCollector };
@@ -3,11 +3,12 @@ import { CharStream, CommonTokenStream, Token } from 'antlr4ng';
3
3
  import { ImpalaSqlLexer } from '../../lib/impala/ImpalaSqlLexer';
4
4
  import { ImpalaSqlParser, ProgramContext } from '../../lib/impala/ImpalaSqlParser';
5
5
  import { BasicSQL } from '../common/basicSQL';
6
+ import { CaretPosition, SemanticCollectOptions, Suggestions } from '../common/types';
6
7
  import { ErrorListener } from '../common/parseErrorListener';
7
- import { Suggestions } from '../common/types';
8
8
  import { ImpalaEntityCollector } from './impalaEntityCollector';
9
9
  import { ImpalaErrorListener } from './ImpalaErrorListener';
10
10
  import { ImpalaSqlSplitListener } from './impalaSplitListener';
11
+ import { ImpalaSemanticContextCollector } from './impalaSemanticContextCollector';
11
12
  export { ImpalaEntityCollector, ImpalaSqlSplitListener };
12
13
  export declare class ImpalaSQL extends BasicSQL<ImpalaSqlLexer, ProgramContext, ImpalaSqlParser> {
13
14
  protected createLexerFromCharStream(charStreams: CharStream): ImpalaSqlLexer;
@@ -16,5 +17,6 @@ export declare class ImpalaSQL extends BasicSQL<ImpalaSqlLexer, ProgramContext,
16
17
  protected get splitListener(): ImpalaSqlSplitListener;
17
18
  protected createErrorListener(_errorListener: ErrorListener): ImpalaErrorListener;
18
19
  protected createEntityCollector(input: string, allTokens?: Token[], caretTokenIndex?: number): ImpalaEntityCollector;
19
- protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number, tokenIndexOffset: number): Suggestions<Token>;
20
+ protected createSemanticContextCollector(input: string, caretPosition: CaretPosition, allTokens: Token[], options?: SemanticCollectOptions): ImpalaSemanticContextCollector;
21
+ protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number): Suggestions<Token>;
20
22
  }
@@ -1,10 +1,12 @@
1
+ import { processTokenCandidates } from '../common/tokenUtils';
1
2
  import { ImpalaSqlLexer } from '../../lib/impala/ImpalaSqlLexer';
2
3
  import { ImpalaSqlParser } from '../../lib/impala/ImpalaSqlParser';
3
4
  import { BasicSQL } from '../common/basicSQL';
4
- import { EntityContextType } from '../common/types';
5
+ import { EntityContextType, } from '../common/types';
5
6
  import { ImpalaEntityCollector } from './impalaEntityCollector';
6
7
  import { ImpalaErrorListener } from './ImpalaErrorListener';
7
8
  import { ImpalaSqlSplitListener } from './impalaSplitListener';
9
+ import { ImpalaSemanticContextCollector } from './impalaSemanticContextCollector';
8
10
  export { ImpalaEntityCollector, ImpalaSqlSplitListener };
9
11
  export class ImpalaSQL extends BasicSQL {
10
12
  constructor() {
@@ -20,6 +22,7 @@ export class ImpalaSQL extends BasicSQL {
20
22
  ImpalaSqlParser.RULE_viewNamePath,
21
23
  ImpalaSqlParser.RULE_databaseNamePath,
22
24
  ImpalaSqlParser.RULE_columnNamePath,
25
+ ImpalaSqlParser.RULE_columnName,
23
26
  ]);
24
27
  }
25
28
  createLexerFromCharStream(charStreams) {
@@ -38,13 +41,15 @@ export class ImpalaSQL extends BasicSQL {
38
41
  createEntityCollector(input, allTokens, caretTokenIndex) {
39
42
  return new ImpalaEntityCollector(input, allTokens, caretTokenIndex);
40
43
  }
41
- processCandidates(candidates, allTokens, caretTokenIndex, tokenIndexOffset) {
44
+ createSemanticContextCollector(input, caretPosition, allTokens, options) {
45
+ return new ImpalaSemanticContextCollector(input, caretPosition, allTokens, options);
46
+ }
47
+ processCandidates(candidates, allTokens, caretTokenIndex) {
42
48
  const originalSyntaxSuggestions = [];
43
49
  const keywords = [];
44
50
  for (let candidate of candidates.rules) {
45
51
  const [ruleType, candidateRule] = candidate;
46
- const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset;
47
- const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + tokenIndexOffset + 1);
52
+ const tokenRanges = allTokens.slice(candidateRule.startTokenIndex, caretTokenIndex + 1);
48
53
  let syntaxContextType = void 0;
49
54
  switch (ruleType) {
50
55
  case ImpalaSqlParser.RULE_functionNameCreate: {
@@ -86,6 +91,16 @@ export class ImpalaSQL extends BasicSQL {
86
91
  case ImpalaSqlParser.RULE_columnNamePath: {
87
92
  syntaxContextType = EntityContextType.COLUMN;
88
93
  }
94
+ case ImpalaSqlParser.RULE_columnName: {
95
+ if (candidateRule.ruleList.includes(ImpalaSqlParser.RULE_columnItem) ||
96
+ candidateRule.ruleList.includes(ImpalaSqlParser.RULE_havingClause) ||
97
+ candidateRule.ruleList.includes(ImpalaSqlParser.RULE_whereClause) ||
98
+ candidateRule.ruleList.includes(ImpalaSqlParser.RULE_whenClause) ||
99
+ candidateRule.ruleList.includes(ImpalaSqlParser.RULE_partitionByClause) ||
100
+ candidateRule.ruleList.includes(ImpalaSqlParser.RULE_relation)) {
101
+ syntaxContextType = EntityContextType.COLUMN;
102
+ }
103
+ }
89
104
  default:
90
105
  break;
91
106
  }
@@ -96,16 +111,8 @@ export class ImpalaSQL extends BasicSQL {
96
111
  });
97
112
  }
98
113
  }
99
- for (let candidate of candidates.tokens) {
100
- const symbolicName = this._parser.vocabulary.getSymbolicName(candidate[0]);
101
- const displayName = this._parser.vocabulary.getDisplayName(candidate[0]);
102
- if (displayName && symbolicName && symbolicName.startsWith('KW_')) {
103
- const keyword = displayName.startsWith("'") && displayName.endsWith("'")
104
- ? displayName.slice(1, -1)
105
- : displayName;
106
- keywords.push(keyword);
107
- }
108
- }
114
+ const processedKeywords = processTokenCandidates(this._parser, candidates.tokens);
115
+ keywords.push(...processedKeywords);
109
116
  return {
110
117
  syntax: originalSyntaxSuggestions,
111
118
  keywords,
@@ -3,11 +3,12 @@ import { CharStream, CommonTokenStream, Token } from 'antlr4ng';
3
3
  import { MySqlLexer } from '../../lib/mysql/MySqlLexer';
4
4
  import { MySqlParser, ProgramContext } from '../../lib/mysql/MySqlParser';
5
5
  import { BasicSQL } from '../common/basicSQL';
6
+ import { Suggestions, CaretPosition, SemanticCollectOptions } from '../common/types';
6
7
  import { ErrorListener } from '../common/parseErrorListener';
7
- import { Suggestions } from '../common/types';
8
8
  import { MySqlEntityCollector } from './mysqlEntityCollector';
9
9
  import { MysqlErrorListener } from './mysqlErrorListener';
10
10
  import { MysqlSplitListener } from './mysqlSplitListener';
11
+ import { MySqlSemanticContextCollector } from '../mysql/mysqlSemanticContextCollector';
11
12
  export { MySqlEntityCollector, MysqlSplitListener };
12
13
  export declare class MySQL extends BasicSQL<MySqlLexer, ProgramContext, MySqlParser> {
13
14
  protected createLexerFromCharStream(charStreams: CharStream): MySqlLexer;
@@ -16,5 +17,6 @@ export declare class MySQL extends BasicSQL<MySqlLexer, ProgramContext, MySqlPar
16
17
  protected get splitListener(): MysqlSplitListener;
17
18
  protected createErrorListener(_errorListener: ErrorListener): MysqlErrorListener;
18
19
  protected createEntityCollector(input: string, allTokens?: Token[], caretTokenIndex?: number): MySqlEntityCollector;
19
- protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number, tokenIndexOffset: number): Suggestions<Token>;
20
+ protected createSemanticContextCollector(input: string, caretPosition: CaretPosition, allTokens: Token[], options?: SemanticCollectOptions): MySqlSemanticContextCollector;
21
+ protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number): Suggestions<Token>;
20
22
  }
@@ -1,10 +1,12 @@
1
+ import { processTokenCandidates } from '../common/tokenUtils';
1
2
  import { MySqlLexer } from '../../lib/mysql/MySqlLexer';
2
3
  import { MySqlParser } from '../../lib/mysql/MySqlParser';
3
4
  import { BasicSQL } from '../common/basicSQL';
4
- import { EntityContextType } from '../common/types';
5
+ import { EntityContextType, } from '../common/types';
5
6
  import { MySqlEntityCollector } from './mysqlEntityCollector';
6
7
  import { MysqlErrorListener } from './mysqlErrorListener';
7
8
  import { MysqlSplitListener } from './mysqlSplitListener';
9
+ import { MySqlSemanticContextCollector } from '../mysql/mysqlSemanticContextCollector';
8
10
  export { MySqlEntityCollector, MysqlSplitListener };
9
11
  export class MySQL extends BasicSQL {
10
12
  constructor() {
@@ -38,13 +40,15 @@ export class MySQL extends BasicSQL {
38
40
  createEntityCollector(input, allTokens, caretTokenIndex) {
39
41
  return new MySqlEntityCollector(input, allTokens, caretTokenIndex);
40
42
  }
41
- processCandidates(candidates, allTokens, caretTokenIndex, tokenIndexOffset) {
43
+ createSemanticContextCollector(input, caretPosition, allTokens, options) {
44
+ return new MySqlSemanticContextCollector(input, caretPosition, allTokens, options);
45
+ }
46
+ processCandidates(candidates, allTokens, caretTokenIndex) {
42
47
  const originalSyntaxSuggestions = [];
43
48
  const keywords = [];
44
49
  for (const candidate of candidates.rules) {
45
50
  const [ruleType, candidateRule] = candidate;
46
- const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset;
47
- const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + tokenIndexOffset + 1);
51
+ const tokenRanges = allTokens.slice(candidateRule.startTokenIndex, caretTokenIndex + 1);
48
52
  let syntaxContextType = void 0;
49
53
  switch (ruleType) {
50
54
  case MySqlParser.RULE_databaseName: {
@@ -97,16 +101,8 @@ export class MySQL extends BasicSQL {
97
101
  });
98
102
  }
99
103
  }
100
- for (const candidate of candidates.tokens) {
101
- const symbolicName = this._parser.vocabulary.getSymbolicName(candidate[0]);
102
- const displayName = this._parser.vocabulary.getDisplayName(candidate[0]);
103
- if (displayName && symbolicName && symbolicName.startsWith('KW_')) {
104
- const keyword = displayName.startsWith("'") && displayName.endsWith("'")
105
- ? displayName.slice(1, -1)
106
- : displayName;
107
- keywords.push(keyword);
108
- }
109
- }
104
+ const processedKeywords = processTokenCandidates(this._parser, candidates.tokens);
105
+ keywords.push(...processedKeywords);
110
106
  return {
111
107
  syntax: originalSyntaxSuggestions,
112
108
  keywords,
@@ -49,7 +49,9 @@ export class MysqlErrorListener extends ParseErrorListener {
49
49
  case MySqlParser.RULE_functionName:
50
50
  case MySqlParser.RULE_viewName:
51
51
  case MySqlParser.RULE_columnName: {
52
- result.push(`{existing}${name}`);
52
+ if (!result.includes(`{existing}${name}`)) {
53
+ result.push(`{existing}${name}`);
54
+ }
53
55
  break;
54
56
  }
55
57
  case MySqlParser.RULE_databaseNameCreate:
@@ -57,7 +59,9 @@ export class MysqlErrorListener extends ParseErrorListener {
57
59
  case MySqlParser.RULE_functionNameCreate:
58
60
  case MySqlParser.RULE_viewNameCreate:
59
61
  case MySqlParser.RULE_columnNameCreate: {
60
- result.push(`{new}${name}`);
62
+ if (!result.includes(`{new}${name}`)) {
63
+ result.push(`{new}${name}`);
64
+ }
61
65
  break;
62
66
  }
63
67
  }
@@ -0,0 +1,9 @@
1
+ import { MySqlParserListener } from '../../lib';
2
+ import { SingleStatementContext } from '../../lib/mysql/MySqlParser';
3
+ import SemanticContextCollector from '../common/semanticContextCollector';
4
+ declare class MySqlSemanticContextCollector extends SemanticContextCollector implements MySqlParserListener {
5
+ getWhiteSpaceRuleType(): number;
6
+ getStatementRuleType(): number;
7
+ enterSingleStatement(ctx: SingleStatementContext): void;
8
+ }
9
+ export { MySqlSemanticContextCollector };
@@ -0,0 +1,14 @@
1
+ import { MySqlParser } from '../../lib/mysql/MySqlParser';
2
+ import SemanticContextCollector from '../common/semanticContextCollector';
3
+ class MySqlSemanticContextCollector extends SemanticContextCollector {
4
+ getWhiteSpaceRuleType() {
5
+ return MySqlParser.WHITE_SPACE;
6
+ }
7
+ getStatementRuleType() {
8
+ return MySqlParser.RULE_singleStatement;
9
+ }
10
+ enterSingleStatement(ctx) {
11
+ this.visitStatement(ctx);
12
+ }
13
+ }
14
+ export { MySqlSemanticContextCollector };
@@ -2,12 +2,13 @@ import { CandidatesCollection } from 'antlr4-c3';
2
2
  import { CharStream, CommonTokenStream, Token } from 'antlr4ng';
3
3
  import { PostgreSqlLexer } from '../../lib/postgresql/PostgreSqlLexer';
4
4
  import { PostgreSqlParser, ProgramContext } from '../../lib/postgresql/PostgreSqlParser';
5
+ import { CaretPosition, SemanticCollectOptions, Suggestions } from '../common/types';
5
6
  import { BasicSQL } from '../common/basicSQL';
6
7
  import { ErrorListener } from '../common/parseErrorListener';
7
- import { Suggestions } from '../common/types';
8
8
  import { PostgreSqlEntityCollector } from './postgreEntityCollector';
9
9
  import { PostgreSqlErrorListener } from './postgreErrorListener';
10
10
  import { PostgreSqlSplitListener } from './postgreSplitListener';
11
+ import { PostgreSemanticContextCollector } from './postgreSemanticContextCollector';
11
12
  export { PostgreSqlEntityCollector, PostgreSqlSplitListener };
12
13
  export declare class PostgreSQL extends BasicSQL<PostgreSqlLexer, ProgramContext, PostgreSqlParser> {
13
14
  protected createLexerFromCharStream(charStreams: CharStream): PostgreSqlLexer;
@@ -16,5 +17,6 @@ export declare class PostgreSQL extends BasicSQL<PostgreSqlLexer, ProgramContext
16
17
  protected get splitListener(): PostgreSqlSplitListener;
17
18
  protected createErrorListener(_errorListener: ErrorListener): PostgreSqlErrorListener;
18
19
  protected createEntityCollector(input: string, allTokens?: Token[], caretTokenIndex?: number): PostgreSqlEntityCollector;
19
- protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number, tokenIndexOffset: number): Suggestions<Token>;
20
+ protected createSemanticContextCollector(input: string, caretPosition: CaretPosition, allTokens: Token[], options?: SemanticCollectOptions): PostgreSemanticContextCollector;
21
+ protected processCandidates(candidates: CandidatesCollection, allTokens: Token[], caretTokenIndex: number): Suggestions<Token>;
20
22
  }
@@ -1,29 +1,32 @@
1
+ import { processTokenCandidates } from '../common/tokenUtils';
1
2
  import { PostgreSqlLexer } from '../../lib/postgresql/PostgreSqlLexer';
2
3
  import { PostgreSqlParser } from '../../lib/postgresql/PostgreSqlParser';
4
+ import { EntityContextType, } from '../common/types';
3
5
  import { BasicSQL } from '../common/basicSQL';
4
- import { EntityContextType } from '../common/types';
5
6
  import { PostgreSqlEntityCollector } from './postgreEntityCollector';
6
7
  import { PostgreSqlErrorListener } from './postgreErrorListener';
7
8
  import { PostgreSqlSplitListener } from './postgreSplitListener';
9
+ import { PostgreSemanticContextCollector } from './postgreSemanticContextCollector';
8
10
  export { PostgreSqlEntityCollector, PostgreSqlSplitListener };
9
11
  export class PostgreSQL extends BasicSQL {
10
12
  constructor() {
11
13
  super(...arguments);
12
14
  this.preferredRules = new Set([
13
- PostgreSqlParser.RULE_table_name_create, // table name
14
- PostgreSqlParser.RULE_table_name, // table name that will be created
15
- PostgreSqlParser.RULE_function_name, // function name
16
- PostgreSqlParser.RULE_function_name_create, // function name that will be created
17
- PostgreSqlParser.RULE_schema_name_create, // schema name that will be created
18
- PostgreSqlParser.RULE_schema_name, // schema name
19
- PostgreSqlParser.RULE_view_name_create, // view name that will be created
20
- PostgreSqlParser.RULE_view_name, // view name
21
- PostgreSqlParser.RULE_database_name_create, // database name that will be created
22
- PostgreSqlParser.RULE_database_name, // database name
23
- PostgreSqlParser.RULE_procedure_name_create, // procedure name that will be created
24
- PostgreSqlParser.RULE_procedure_name, // procedure name
25
- PostgreSqlParser.RULE_column_name_create, // column name that will be created
26
- PostgreSqlParser.RULE_column_name, // column name
15
+ PostgreSqlParser.RULE_tableNameCreate, // table name
16
+ PostgreSqlParser.RULE_tableName, // table name that will be created
17
+ PostgreSqlParser.RULE_functionName, // function name
18
+ PostgreSqlParser.RULE_functionNameCreate, // function name that will be created
19
+ PostgreSqlParser.RULE_schemaNameCreate, // schema name that will be created
20
+ PostgreSqlParser.RULE_schemaName, // schema name
21
+ PostgreSqlParser.RULE_viewNameCreate, // view name that will be created
22
+ PostgreSqlParser.RULE_viewName, // view name
23
+ PostgreSqlParser.RULE_databaseNameCreate, // database name that will be created
24
+ PostgreSqlParser.RULE_databaseName, // database name
25
+ PostgreSqlParser.RULE_procedureNameCreate, // procedure name that will be created
26
+ PostgreSqlParser.RULE_procedureName, // procedure name
27
+ PostgreSqlParser.RULE_columnNameCreate, // column name that will be created
28
+ PostgreSqlParser.RULE_columnName, // column name
29
+ PostgreSqlParser.RULE_columnNamePath, // column name
27
30
  ]);
28
31
  }
29
32
  createLexerFromCharStream(charStreams) {
@@ -42,71 +45,85 @@ export class PostgreSQL extends BasicSQL {
42
45
  createEntityCollector(input, allTokens, caretTokenIndex) {
43
46
  return new PostgreSqlEntityCollector(input, allTokens, caretTokenIndex);
44
47
  }
45
- processCandidates(candidates, allTokens, caretTokenIndex, tokenIndexOffset) {
48
+ createSemanticContextCollector(input, caretPosition, allTokens, options) {
49
+ return new PostgreSemanticContextCollector(input, caretPosition, allTokens, options);
50
+ }
51
+ processCandidates(candidates, allTokens, caretTokenIndex) {
46
52
  const originalSyntaxSuggestions = [];
47
53
  const keywords = [];
48
54
  for (let candidate of candidates.rules) {
49
55
  const [ruleType, candidateRule] = candidate;
50
- const startTokenIndex = candidateRule.startTokenIndex + tokenIndexOffset;
51
- const tokenRanges = allTokens.slice(startTokenIndex, caretTokenIndex + tokenIndexOffset + 1);
56
+ const tokenRanges = allTokens.slice(candidateRule.startTokenIndex, caretTokenIndex + 1);
52
57
  let syntaxContextType = void 0;
53
58
  switch (ruleType) {
54
- case PostgreSqlParser.RULE_table_name_create: {
59
+ case PostgreSqlParser.RULE_tableNameCreate: {
55
60
  syntaxContextType = EntityContextType.TABLE_CREATE;
56
61
  break;
57
62
  }
58
- case PostgreSqlParser.RULE_table_name: {
63
+ case PostgreSqlParser.RULE_tableName: {
59
64
  syntaxContextType = EntityContextType.TABLE;
60
65
  break;
61
66
  }
62
- case PostgreSqlParser.RULE_function_name_create: {
67
+ case PostgreSqlParser.RULE_functionNameCreate: {
63
68
  syntaxContextType = EntityContextType.FUNCTION_CREATE;
64
69
  break;
65
70
  }
66
- case PostgreSqlParser.RULE_function_name: {
71
+ case PostgreSqlParser.RULE_functionName: {
67
72
  syntaxContextType = EntityContextType.FUNCTION;
68
73
  break;
69
74
  }
70
- case PostgreSqlParser.RULE_schema_name_create: {
75
+ case PostgreSqlParser.RULE_schemaNameCreate: {
71
76
  syntaxContextType = EntityContextType.DATABASE_CREATE;
72
77
  break;
73
78
  }
74
- case PostgreSqlParser.RULE_schema_name: {
79
+ case PostgreSqlParser.RULE_schemaName: {
75
80
  syntaxContextType = EntityContextType.DATABASE;
76
81
  break;
77
82
  }
78
- case PostgreSqlParser.RULE_view_name_create: {
83
+ case PostgreSqlParser.RULE_viewNameCreate: {
79
84
  syntaxContextType = EntityContextType.VIEW_CREATE;
80
85
  break;
81
86
  }
82
- case PostgreSqlParser.RULE_view_name: {
87
+ case PostgreSqlParser.RULE_viewName: {
83
88
  syntaxContextType = EntityContextType.VIEW;
84
89
  break;
85
90
  }
86
- case PostgreSqlParser.RULE_database_name_create: {
91
+ case PostgreSqlParser.RULE_databaseNameCreate: {
87
92
  syntaxContextType = EntityContextType.DATABASE_CREATE;
88
93
  break;
89
94
  }
90
- case PostgreSqlParser.RULE_database_name: {
95
+ case PostgreSqlParser.RULE_databaseName: {
91
96
  syntaxContextType = EntityContextType.DATABASE;
92
97
  break;
93
98
  }
94
- case PostgreSqlParser.RULE_procedure_name_create: {
99
+ case PostgreSqlParser.RULE_procedureNameCreate: {
95
100
  syntaxContextType = EntityContextType.PROCEDURE_CREATE;
96
101
  break;
97
102
  }
98
- case PostgreSqlParser.RULE_procedure_name: {
103
+ case PostgreSqlParser.RULE_procedureName: {
99
104
  syntaxContextType = EntityContextType.PROCEDURE;
100
105
  break;
101
106
  }
102
- case PostgreSqlParser.RULE_column_name_create: {
107
+ case PostgreSqlParser.RULE_columnNameCreate: {
103
108
  syntaxContextType = EntityContextType.COLUMN_CREATE;
104
109
  break;
105
110
  }
106
- case PostgreSqlParser.RULE_column_name: {
111
+ case PostgreSqlParser.RULE_columnName: {
107
112
  syntaxContextType = EntityContextType.COLUMN;
108
113
  break;
109
114
  }
115
+ case PostgreSqlParser.RULE_columnNamePath: {
116
+ if (candidateRule.ruleList.includes(PostgreSqlParser.RULE_groupClause) ||
117
+ candidateRule.ruleList.includes(PostgreSqlParser.RULE_sortClause) ||
118
+ candidateRule.ruleList.includes(PostgreSqlParser.RULE_limitClause) ||
119
+ candidateRule.ruleList.includes(PostgreSqlParser.RULE_whereClause) ||
120
+ candidateRule.ruleList.includes(PostgreSqlParser.RULE_havingClause) ||
121
+ candidateRule.ruleList.includes(PostgreSqlParser.RULE_windowClause) ||
122
+ candidateRule.ruleList.includes(PostgreSqlParser.RULE_triggerWhen)) {
123
+ syntaxContextType = EntityContextType.COLUMN;
124
+ }
125
+ break;
126
+ }
110
127
  default:
111
128
  break;
112
129
  }
@@ -117,16 +134,8 @@ export class PostgreSQL extends BasicSQL {
117
134
  });
118
135
  }
119
136
  }
120
- for (let candidate of candidates.tokens) {
121
- const symbolicName = this._parser.vocabulary.getSymbolicName(candidate[0]);
122
- const displayName = this._parser.vocabulary.getDisplayName(candidate[0]);
123
- if (displayName && symbolicName && symbolicName.startsWith('KW_')) {
124
- const keyword = displayName.startsWith("'") && displayName.endsWith("'")
125
- ? displayName.slice(1, -1)
126
- : displayName;
127
- keywords.push(keyword);
128
- }
129
- }
137
+ const processedKeywords = processTokenCandidates(this._parser, candidates.tokens);
138
+ keywords.push(...processedKeywords);
130
139
  return {
131
140
  syntax: originalSyntaxSuggestions,
132
141
  keywords,
@@ -1,4 +1,4 @@
1
- import { type AltertablestmtContext, type ColumnCreateTableContext, type ColumnNameCreateContext, type CreateDatabaseContext, type CreateForeignTableContext, type CreatefunctionstmtContext, type CreateMaterializedViewContext, type CreatePartitionForeignTableContext, type CreateViewContext, type DatabaseNameContext, type DatabaseNameCreateContext, type FunctionNameCreateContext, type InsertStatementContext, type ProcedureNameContext, type ProcedureNameCreateContext, type QueryCreateTableContext, type SelectStatementContext, type SingleStmtContext, type TableNameContext, type TableNameCreateContext, type ViewNameContext, type ViewNameCreateContext } from '../../lib/postgresql/PostgreSqlParser';
1
+ import { type AlterTableStmtContext, type ColumnCreateTableContext, type ColumnNameCreateContext, type CreateDatabaseContext, type CreateForeignTableContext, type CreateFunctionStmtContext, type CreateMaterializedViewContext, type CreatePartitionForeignTableContext, type CreateViewContext, type DatabaseNameContext, type DatabaseNameCreateContext, type FunctionNameCreateContext, type InsertStatementContext, type ProcedureNameContext, type ProcedureNameCreateContext, type QueryCreateTableContext, type SelectStatementContext, type SingleStmtContext, type TableNameContext, type TableNameCreateContext, type ViewNameContext, type ViewNameCreateContext } from '../../lib/postgresql/PostgreSqlParser';
2
2
  import type { PostgreSqlParserListener } from '../../lib/postgresql/PostgreSqlParserListener';
3
3
  import { EntityCollector } from '../common/entityCollector';
4
4
  export declare class PostgreSqlEntityCollector extends EntityCollector implements PostgreSqlParserListener {
@@ -34,8 +34,8 @@ export declare class PostgreSqlEntityCollector extends EntityCollector implement
34
34
  exitSelectStatement(ctx: SelectStatementContext): void;
35
35
  enterInsertStatement(ctx: InsertStatementContext): void;
36
36
  exitInsertStatement(ctx: InsertStatementContext): void;
37
- enterCreatefunctionstmt(ctx: CreatefunctionstmtContext): void;
38
- exitCreatefunctionstmt(ctx: CreatefunctionstmtContext): void;
39
- enterAltertablestmt(ctx: AltertablestmtContext): void;
40
- exitAltertablestmt(ctx: AltertablestmtContext): void;
37
+ enterCreateFunctionStmt(ctx: CreateFunctionStmtContext): void;
38
+ exitCreateFunctionStmt(ctx: CreateFunctionStmtContext): void;
39
+ enterAlterTableStmt(ctx: AlterTableStmtContext): void;
40
+ exitAlterTableStmt(ctx: AlterTableStmtContext): void;
41
41
  }
@@ -1,4 +1,4 @@
1
- import { Column_defContext, Table_refContext, } from '../../lib/postgresql/PostgreSqlParser';
1
+ import { Column_defContext, TableRefContext, } from '../../lib/postgresql/PostgreSqlParser';
2
2
  import { AttrName, EntityCollector, StmtContextType } from '../common/entityCollector';
3
3
  import { EntityContextType } from '../common/types';
4
4
  export class PostgreSqlEntityCollector extends EntityCollector {
@@ -16,7 +16,7 @@ export class PostgreSqlEntityCollector extends EntityCollector {
16
16
  ? [
17
17
  {
18
18
  attrName: AttrName.alias,
19
- endContextList: [Table_refContext.name],
19
+ endContextList: [TableRefContext.name],
20
20
  },
21
21
  ]
22
22
  : undefined);
@@ -31,7 +31,7 @@ export class PostgreSqlEntityCollector extends EntityCollector {
31
31
  ? [
32
32
  {
33
33
  attrName: AttrName.alias,
34
- endContextList: [Table_refContext.name],
34
+ endContextList: [TableRefContext.name],
35
35
  },
36
36
  ]
37
37
  : undefined);
@@ -121,16 +121,16 @@ export class PostgreSqlEntityCollector extends EntityCollector {
121
121
  exitInsertStatement(ctx) {
122
122
  this.popStmt();
123
123
  }
124
- enterCreatefunctionstmt(ctx) {
124
+ enterCreateFunctionStmt(ctx) {
125
125
  this.pushStmt(ctx, StmtContextType.CREATE_FUNCTION_STMT);
126
126
  }
127
- exitCreatefunctionstmt(ctx) {
127
+ exitCreateFunctionStmt(ctx) {
128
128
  this.popStmt();
129
129
  }
130
- enterAltertablestmt(ctx) {
130
+ enterAlterTableStmt(ctx) {
131
131
  this.pushStmt(ctx, StmtContextType.ALTER_TABLE_STMT);
132
132
  }
133
- exitAltertablestmt(ctx) {
133
+ exitAlterTableStmt(ctx) {
134
134
  this.popStmt();
135
135
  }
136
136
  }
@@ -5,20 +5,21 @@ export class PostgreSqlErrorListener extends ParseErrorListener {
5
5
  constructor() {
6
6
  super(...arguments);
7
7
  this.objectNames = new Map([
8
- [PostgreSqlParser.RULE_database_name, 'database'],
9
- [PostgreSqlParser.RULE_database_name_create, 'database'],
10
- [PostgreSqlParser.RULE_table_name, 'table'],
11
- [PostgreSqlParser.RULE_table_name_create, 'table'],
12
- [PostgreSqlParser.RULE_view_name, 'view'],
13
- [PostgreSqlParser.RULE_view_name_create, 'view'],
14
- [PostgreSqlParser.RULE_function_name, 'function'],
15
- [PostgreSqlParser.RULE_function_name_create, 'function'],
16
- [PostgreSqlParser.RULE_column_name, 'column'],
17
- [PostgreSqlParser.RULE_column_name_create, 'column'],
18
- [PostgreSqlParser.RULE_schema_name_create, 'schema'],
19
- [PostgreSqlParser.RULE_schema_name, 'schema'],
20
- [PostgreSqlParser.RULE_procedure_name_create, 'procedure'],
21
- [PostgreSqlParser.RULE_procedure_name, 'procedure'],
8
+ [PostgreSqlParser.RULE_databaseName, 'database'],
9
+ [PostgreSqlParser.RULE_databaseNameCreate, 'database'],
10
+ [PostgreSqlParser.RULE_tableName, 'table'],
11
+ [PostgreSqlParser.RULE_tableNameCreate, 'table'],
12
+ [PostgreSqlParser.RULE_viewName, 'view'],
13
+ [PostgreSqlParser.RULE_viewNameCreate, 'view'],
14
+ [PostgreSqlParser.RULE_functionName, 'function'],
15
+ [PostgreSqlParser.RULE_functionNameCreate, 'function'],
16
+ [PostgreSqlParser.RULE_columnName, 'column'],
17
+ [PostgreSqlParser.RULE_columnNameCreate, 'column'],
18
+ [PostgreSqlParser.RULE_columnNamePath, 'column'],
19
+ [PostgreSqlParser.RULE_schemaNameCreate, 'schema'],
20
+ [PostgreSqlParser.RULE_schemaName, 'schema'],
21
+ [PostgreSqlParser.RULE_procedureNameCreate, 'procedure'],
22
+ [PostgreSqlParser.RULE_procedureName, 'procedure'],
22
23
  ]);
23
24
  }
24
25
  getExpectedText(parser, token) {
@@ -48,24 +49,29 @@ export class PostgreSqlErrorListener extends ParseErrorListener {
48
49
  const [ruleType] = candidate;
49
50
  const name = this.objectNames.get(ruleType);
50
51
  switch (ruleType) {
51
- case PostgreSqlParser.RULE_table_name:
52
- case PostgreSqlParser.RULE_function_name:
53
- case PostgreSqlParser.RULE_schema_name:
54
- case PostgreSqlParser.RULE_view_name:
55
- case PostgreSqlParser.RULE_database_name:
56
- case PostgreSqlParser.RULE_procedure_name:
57
- case PostgreSqlParser.RULE_column_name: {
58
- result.push(`{existing}${name}`);
52
+ case PostgreSqlParser.RULE_tableName:
53
+ case PostgreSqlParser.RULE_functionName:
54
+ case PostgreSqlParser.RULE_schemaName:
55
+ case PostgreSqlParser.RULE_viewName:
56
+ case PostgreSqlParser.RULE_databaseName:
57
+ case PostgreSqlParser.RULE_procedureName:
58
+ case PostgreSqlParser.RULE_columnName:
59
+ case PostgreSqlParser.RULE_columnNamePath: {
60
+ if (!result.includes(`{existing}${name}`)) {
61
+ result.push(`{existing}${name}`);
62
+ }
59
63
  break;
60
64
  }
61
- case PostgreSqlParser.RULE_table_name_create:
62
- case PostgreSqlParser.RULE_function_name_create:
63
- case PostgreSqlParser.RULE_schema_name_create:
64
- case PostgreSqlParser.RULE_view_name_create:
65
- case PostgreSqlParser.RULE_database_name_create:
66
- case PostgreSqlParser.RULE_procedure_name_create:
67
- case PostgreSqlParser.RULE_column_name_create: {
68
- result.push(`{new}${name}`);
65
+ case PostgreSqlParser.RULE_tableNameCreate:
66
+ case PostgreSqlParser.RULE_functionNameCreate:
67
+ case PostgreSqlParser.RULE_schemaNameCreate:
68
+ case PostgreSqlParser.RULE_viewNameCreate:
69
+ case PostgreSqlParser.RULE_databaseNameCreate:
70
+ case PostgreSqlParser.RULE_procedureNameCreate:
71
+ case PostgreSqlParser.RULE_columnNameCreate: {
72
+ if (!result.includes(`{new}${name}`)) {
73
+ result.push(`{new}${name}`);
74
+ }
69
75
  break;
70
76
  }
71
77
  }