dt-sql-parser 4.0.0-ng.2 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +119 -112
- package/dist/index.d.ts +14 -6
- package/dist/index.js +11 -4
- package/dist/lib/SQLParserBase.d.ts +8 -0
- package/dist/lib/SQLParserBase.js +14 -0
- package/dist/lib/{flinksql → flink}/FlinkSqlLexer.d.ts +2 -2
- package/dist/lib/{flinksql → flink}/FlinkSqlLexer.js +2 -1
- package/dist/lib/{flinksql → flink}/FlinkSqlParser.d.ts +5 -11
- package/dist/lib/{flinksql → flink}/FlinkSqlParser.js +2065 -2054
- package/dist/lib/{flinksql → flink}/FlinkSqlParserListener.d.ts +0 -13
- package/dist/lib/{flinksql → flink}/FlinkSqlParserListener.js +1 -1
- package/dist/lib/{flinksql → flink}/FlinkSqlParserVisitor.d.ts +0 -8
- package/dist/lib/{flinksql → flink}/FlinkSqlParserVisitor.js +2 -1
- package/dist/lib/hive/HiveSqlLexer.d.ts +258 -257
- package/dist/lib/hive/HiveSqlLexer.js +1794 -1786
- package/dist/lib/hive/HiveSqlParser.d.ts +1266 -3176
- package/dist/lib/hive/HiveSqlParser.js +23046 -33454
- package/dist/lib/hive/HiveSqlParserListener.d.ts +141 -2036
- package/dist/lib/hive/HiveSqlParserVisitor.d.ts +151 -1358
- package/dist/lib/hive/HiveSqlParserVisitor.js +1 -0
- package/dist/lib/impala/ImpalaSqlLexer.d.ts +170 -154
- package/dist/lib/impala/ImpalaSqlLexer.js +1179 -1099
- package/dist/lib/impala/ImpalaSqlParser.d.ts +282 -240
- package/dist/lib/impala/ImpalaSqlParser.js +3881 -3744
- package/dist/lib/impala/ImpalaSqlParserListener.d.ts +22 -11
- package/dist/lib/impala/ImpalaSqlParserVisitor.d.ts +14 -7
- package/dist/lib/impala/ImpalaSqlParserVisitor.js +1 -0
- package/dist/lib/index.d.ts +6 -6
- package/dist/lib/index.js +6 -6
- package/dist/lib/mysql/MySqlLexer.d.ts +2 -2
- package/dist/lib/mysql/MySqlLexer.js +1 -0
- package/dist/lib/mysql/MySqlParser.d.ts +347 -418
- package/dist/lib/mysql/MySqlParser.js +10484 -11099
- package/dist/lib/mysql/MySqlParserListener.d.ts +41 -126
- package/dist/lib/mysql/MySqlParserVisitor.d.ts +23 -76
- package/dist/lib/mysql/MySqlParserVisitor.js +1 -0
- package/dist/lib/plsql/PlSqlBaseLexer.d.ts +1 -2
- package/dist/lib/plsql/PlSqlBaseLexer.js +1 -1
- package/dist/lib/plsql/PlSqlBaseParser.d.ts +3 -2
- package/dist/lib/plsql/PlSqlBaseParser.js +2 -2
- package/dist/lib/plsql/PlSqlLexer.d.ts +3 -3
- package/dist/lib/plsql/PlSqlLexer.js +2 -1
- package/dist/lib/plsql/PlSqlParser.d.ts +3 -3
- package/dist/lib/plsql/PlSqlParser.js +2 -1
- package/dist/lib/plsql/PlSqlParserVisitor.js +1 -0
- package/dist/lib/{pgsql/PostgreSQLLexer.d.ts → postgresql/PostgreSqlLexer.d.ts} +3 -3
- package/dist/lib/{pgsql/PostgreSQLLexer.js → postgresql/PostgreSqlLexer.js} +618 -617
- package/dist/lib/{pgsql/PostgreSQLParser.d.ts → postgresql/PostgreSqlParser.d.ts} +3598 -3484
- package/dist/lib/{pgsql/PostgreSQLParser.js → postgresql/PostgreSqlParser.js} +41502 -42831
- package/dist/lib/{pgsql/PostgreSQLParserListener.d.ts → postgresql/PostgreSqlParserListener.d.ts} +2797 -2730
- package/dist/lib/{pgsql/PostgreSQLParserListener.js → postgresql/PostgreSqlParserListener.js} +3 -3
- package/dist/lib/{pgsql/PostgreSQLParserVisitor.d.ts → postgresql/PostgreSqlParserVisitor.d.ts} +1845 -1810
- package/dist/lib/{pgsql/PostgreSQLParserVisitor.js → postgresql/PostgreSqlParserVisitor.js} +4 -3
- package/dist/lib/spark/SparkSqlLexer.d.ts +305 -298
- package/dist/lib/spark/SparkSqlLexer.js +1777 -1735
- package/dist/lib/spark/SparkSqlParser.d.ts +1764 -669
- package/dist/lib/spark/SparkSqlParser.js +15460 -10769
- package/dist/lib/spark/SparkSqlParserListener.d.ts +1285 -35
- package/dist/lib/spark/SparkSqlParserVisitor.d.ts +790 -21
- package/dist/lib/spark/SparkSqlParserVisitor.js +1 -0
- package/dist/lib/{trinosql → trino}/TrinoSqlLexer.d.ts +2 -2
- package/dist/lib/{trinosql → trino}/TrinoSqlLexer.js +2 -1
- package/dist/lib/{trinosql → trino}/TrinoSqlListener.d.ts +7 -5
- package/dist/lib/{trinosql → trino}/TrinoSqlListener.js +1 -1
- package/dist/lib/{trinosql → trino}/TrinoSqlParser.d.ts +11 -5
- package/dist/lib/{trinosql → trino}/TrinoSqlParser.js +360 -320
- package/dist/lib/{trinosql → trino}/TrinoSqlVisitor.d.ts +4 -3
- package/dist/lib/{trinosql → trino}/TrinoSqlVisitor.js +2 -1
- package/dist/parser/common/{basicParser.d.ts → basicSQL.d.ts} +22 -19
- package/dist/parser/common/{basicParser.js → basicSQL.js} +52 -34
- package/dist/parser/common/entityCollector.d.ts +78 -0
- package/dist/parser/common/entityCollector.js +191 -0
- package/dist/parser/common/{utils/findCaretTokenIndex.d.ts → findCaretTokenIndex.d.ts} +2 -2
- package/dist/parser/common/{utils/findCaretTokenIndex.js → findCaretTokenIndex.js} +3 -2
- package/dist/parser/common/parseErrorListener.d.ts +6 -6
- package/dist/parser/common/parseErrorListener.js +1 -1
- package/dist/parser/common/simpleStack.d.ts +10 -0
- package/dist/parser/common/simpleStack.js +23 -0
- package/dist/parser/common/splitListener.d.ts +8 -0
- package/dist/parser/common/splitListener.js +12 -0
- package/dist/parser/common/textAndWord.d.ts +52 -0
- package/dist/parser/common/textAndWord.js +58 -0
- package/dist/parser/common/{basic-parser-types.d.ts → types.d.ts} +4 -31
- package/dist/parser/common/types.js +34 -0
- package/dist/parser/flink/flinkEntityCollector.d.ts +33 -0
- package/dist/parser/flink/flinkEntityCollector.js +84 -0
- package/dist/parser/flink/flinkSplitListener.d.ts +6 -0
- package/dist/parser/flink/flinkSplitListener.js +6 -0
- package/dist/parser/flink/index.d.ts +17 -0
- package/dist/parser/{flinksql.js → flink/index.js} +26 -37
- package/dist/parser/hive/hiveEntityCollector.d.ts +35 -0
- package/dist/parser/hive/hiveEntityCollector.js +90 -0
- package/dist/parser/hive/hiveSplitListener.d.ts +6 -0
- package/dist/parser/hive/hiveSplitListener.js +6 -0
- package/dist/parser/hive/index.d.ts +17 -0
- package/dist/parser/{hive.js → hive/index.js} +24 -34
- package/dist/parser/impala/impalaEntityCollector.d.ts +35 -0
- package/dist/parser/impala/impalaEntityCollector.js +90 -0
- package/dist/parser/impala/impalaSplitListener.d.ts +6 -0
- package/dist/parser/impala/impalaSplitListener.js +6 -0
- package/dist/parser/impala/index.d.ts +17 -0
- package/dist/parser/{impala.js → impala/index.js} +24 -34
- package/dist/parser/index.d.ts +8 -8
- package/dist/parser/index.js +8 -8
- package/dist/parser/mysql/index.d.ts +17 -0
- package/dist/parser/{mysql.js → mysql/index.js} +24 -34
- package/dist/parser/mysql/mysqlEntityCollector.d.ts +37 -0
- package/dist/parser/mysql/mysqlEntityCollector.js +96 -0
- package/dist/parser/mysql/mysqlSplitListener.d.ts +6 -0
- package/dist/parser/mysql/mysqlSplitListener.js +6 -0
- package/dist/parser/plsql.d.ts +7 -6
- package/dist/parser/plsql.js +6 -4
- package/dist/parser/postgresql/index.d.ts +17 -0
- package/dist/parser/postgresql/index.js +130 -0
- package/dist/parser/postgresql/postgreEntityCollector.d.ts +37 -0
- package/dist/parser/postgresql/postgreEntityCollector.js +96 -0
- package/dist/parser/postgresql/postgreSplitListener.d.ts +6 -0
- package/dist/parser/postgresql/postgreSplitListener.js +6 -0
- package/dist/parser/spark/index.d.ts +17 -0
- package/dist/parser/{spark.js → spark/index.js} +29 -40
- package/dist/parser/spark/sparkEntityCollector.d.ts +37 -0
- package/dist/parser/spark/sparkEntityCollector.js +96 -0
- package/dist/parser/spark/sparkSplitListener.d.ts +6 -0
- package/dist/parser/spark/sparkSplitListener.js +6 -0
- package/dist/parser/trino/index.d.ts +17 -0
- package/dist/parser/{trinosql.js → trino/index.js} +25 -36
- package/dist/parser/trino/trinoEntityCollector.d.ts +30 -0
- package/dist/parser/trino/trinoEntityCollector.js +75 -0
- package/dist/parser/trino/trinoSplitListener.d.ts +6 -0
- package/dist/parser/trino/trinoSplitListener.js +6 -0
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/token.d.ts +1 -1
- package/package.json +1 -1
- package/dist/parser/common/basic-parser-types.js +0 -34
- package/dist/parser/flinksql.d.ts +0 -23
- package/dist/parser/hive.d.ts +0 -23
- package/dist/parser/impala.d.ts +0 -23
- package/dist/parser/mysql.d.ts +0 -23
- package/dist/parser/pgsql.d.ts +0 -23
- package/dist/parser/pgsql.js +0 -140
- package/dist/parser/spark.d.ts +0 -23
- package/dist/parser/trinosql.d.ts +0 -23
package/README.md
CHANGED
|
@@ -13,42 +13,27 @@
|
|
|
13
13
|
|
|
14
14
|
English | [简体中文](./README-zh_CN.md)
|
|
15
15
|
|
|
16
|
-
dt-sql-parser is a **SQL Parser** project built with [ANTLR4](https://github.com/antlr/antlr4), and it's mainly for the **BigData** field. The [ANTLR4](https://github.com/antlr/antlr4) generated the basic Parser, Visitor, and Listener, so it's easy to complete the **
|
|
16
|
+
dt-sql-parser is a **SQL Parser** project built with [ANTLR4](https://github.com/antlr/antlr4), and it's mainly for the **BigData** field. The [ANTLR4](https://github.com/antlr/antlr4) generated the basic Parser, Visitor, and Listener, so it's easy to complete the **Lexer**, **Parser**, **traverse the AST**, and so on features.
|
|
17
17
|
|
|
18
|
-
Additionally, it provides
|
|
18
|
+
Additionally, it provides advanced features such as **SQL Validation**, **Code Completion** and **Collecting Table and Columns in SQL**.
|
|
19
19
|
|
|
20
20
|
**Supported SQL**:
|
|
21
21
|
|
|
22
22
|
- MySQL
|
|
23
|
-
- Flink
|
|
24
|
-
- Spark
|
|
25
|
-
- Hive
|
|
23
|
+
- Flink
|
|
24
|
+
- Spark
|
|
25
|
+
- Hive
|
|
26
26
|
- PostgreSQL
|
|
27
|
-
- Trino
|
|
28
|
-
- Impala
|
|
27
|
+
- Trino
|
|
28
|
+
- Impala
|
|
29
29
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
| SQL Type | SQL Spliting | Code Completion |
|
|
33
|
-
| ----------- | ------------ | --------------- |
|
|
34
|
-
| MySQL | ✅ | ✅ |
|
|
35
|
-
| Flink SQL | ✅ | ✅ |
|
|
36
|
-
| Spark SQL | ✅ | ✅ |
|
|
37
|
-
| Hive SQL | ✅ | ✅ |
|
|
38
|
-
| PostgreSQL | ✅ | ✅ |
|
|
39
|
-
| Trino SQL | ✅ | ✅ |
|
|
40
|
-
| Impala SQL | ✅ | ✅ |
|
|
41
|
-
|
|
42
|
-
>Tips: This project is the default for Javascript language, also you can try to compile it to other languages if you need.
|
|
30
|
+
>Tips: This project is the default for Typescript target, also you can try to compile it to other languages if you need.
|
|
43
31
|
|
|
44
32
|
<br/>
|
|
45
33
|
|
|
46
34
|
## Integrating SQL Parser with Monaco Editor
|
|
47
35
|
|
|
48
|
-
We have provided [monaco-sql-languages](https://github.com/DTStack/monaco-sql-languages)
|
|
49
|
-
|
|
50
|
-
>Tips: If you want to run `dt-sql-parser` in browser, don't forget to install the `assert` and `util` polyfills, and define the global variable `process.env`.
|
|
51
|
-
None of this is needed in the node environment, because node has them built-in.
|
|
36
|
+
We also have provided [monaco-sql-languages](https://github.com/DTStack/monaco-sql-languages) to easily to integrate `dt-sql-parser` with `monaco-editor`.
|
|
52
37
|
|
|
53
38
|
<br/>
|
|
54
39
|
|
|
@@ -65,45 +50,30 @@ yarn add dt-sql-parser
|
|
|
65
50
|
<br/>
|
|
66
51
|
|
|
67
52
|
## Usage
|
|
68
|
-
We recommend learning the
|
|
53
|
+
We recommend learning the fundamentals usage before continuing. The dt-sql-parser library provides SQL classes for different types of SQL.
|
|
69
54
|
```javascript
|
|
70
|
-
import { MySQL, FlinkSQL, SparkSQL, HiveSQL,
|
|
55
|
+
import { MySQL, FlinkSQL, SparkSQL, HiveSQL, PostgreSQL, TrinoSQL, ImpalaSQL } from 'dt-sql-parser';
|
|
71
56
|
```
|
|
72
57
|
|
|
73
58
|
Before using syntax validation, code completion, and other features, it is necessary to instantiate the Parser of the relevant SQL type.
|
|
74
59
|
For instance, one can consider using `MySQL` as an example:
|
|
75
60
|
```javascript
|
|
76
|
-
const
|
|
61
|
+
const mysql = new MySQL();
|
|
77
62
|
```
|
|
78
63
|
|
|
79
64
|
The following usage examples will utilize the `MySQL`, and the Parser for other SQL types will be used in a similar manner as `MySQL`.
|
|
80
65
|
|
|
81
66
|
### Syntax Validation
|
|
82
|
-
|
|
83
|
-
import { MySQL } from 'dt-sql-parser';
|
|
84
|
-
|
|
85
|
-
const parser = new MySQL();
|
|
86
|
-
|
|
87
|
-
const correctSql = 'select id,name from user1;';
|
|
88
|
-
const errors = parser.validate(correctSql);
|
|
89
|
-
console.log(errors);
|
|
90
|
-
```
|
|
91
|
-
|
|
92
|
-
*output:*
|
|
67
|
+
First instanced a Parser object, then call the **validate** method on the SQL instance to validate the sql content, if failed returns an array includes **error** message.
|
|
93
68
|
|
|
94
69
|
```javascript
|
|
95
|
-
|
|
96
|
-
[]
|
|
97
|
-
*/
|
|
98
|
-
```
|
|
70
|
+
import { MySQL } from 'dt-sql-parser';
|
|
99
71
|
|
|
100
|
-
|
|
72
|
+
const mysql = new MySQL();
|
|
73
|
+
const incorrectSql = 'selec id,name from user1;';
|
|
74
|
+
const errors = mysql.validate(incorrectSql);
|
|
101
75
|
|
|
102
|
-
```javascript
|
|
103
|
-
const incorrectSql = 'selec id,name from user1;'
|
|
104
|
-
const errors = parser.validate(incorrectSql);
|
|
105
76
|
console.log(errors);
|
|
106
|
-
|
|
107
77
|
```
|
|
108
78
|
|
|
109
79
|
*output:*
|
|
@@ -116,25 +86,24 @@ console.log(errors);
|
|
|
116
86
|
endLine: 1,
|
|
117
87
|
startCol: 0,
|
|
118
88
|
startLine: 1,
|
|
119
|
-
message: "
|
|
89
|
+
message: "..."
|
|
120
90
|
}
|
|
121
91
|
]
|
|
122
92
|
*/
|
|
123
93
|
```
|
|
124
94
|
|
|
125
|
-
We instanced a Parser object, and use the **validate** method to check the SQL syntax, if failed
|
|
126
|
-
returns an array object includes **error** message.
|
|
127
95
|
|
|
128
96
|
### Tokenizer
|
|
129
97
|
|
|
130
|
-
|
|
98
|
+
Call the `getAllTokens` method on the SQL instance:
|
|
131
99
|
|
|
132
100
|
```javascript
|
|
133
101
|
import { MySQL } from 'dt-sql-parser';
|
|
134
102
|
|
|
135
|
-
const
|
|
103
|
+
const mysql = new MySQL()
|
|
136
104
|
const sql = 'select id,name,sex from user1;'
|
|
137
|
-
const tokens =
|
|
105
|
+
const tokens = mysql.getAllTokens(sql)
|
|
106
|
+
|
|
138
107
|
console.log(tokens)
|
|
139
108
|
```
|
|
140
109
|
|
|
@@ -164,36 +133,28 @@ console.log(tokens)
|
|
|
164
133
|
Traverse the tree node by the Visitor:
|
|
165
134
|
|
|
166
135
|
```typescript
|
|
167
|
-
import { MySQL,
|
|
168
|
-
import type { MySqlParserVisitor } from 'dt-sql-parser';
|
|
169
|
-
|
|
170
|
-
const parser = new MySQL();
|
|
171
|
-
const sql = `select id,name from user1;`;
|
|
172
|
-
const tree = parser.parse(sql);
|
|
136
|
+
import { MySQL, MySqlParserVisitor } from 'dt-sql-parser';
|
|
173
137
|
|
|
174
|
-
|
|
138
|
+
const mysql = new MySQL();
|
|
139
|
+
const sql = `select id, name from user1;`;
|
|
140
|
+
const parseTree = mysql.parse(sql);
|
|
175
141
|
|
|
176
|
-
class MyVisitor extends
|
|
177
|
-
|
|
178
|
-
return '';
|
|
179
|
-
}
|
|
180
|
-
visitTableName(ctx) {
|
|
181
|
-
let tableName = ctx.text.toLowerCase();
|
|
182
|
-
console.log('TableName:', tableName);
|
|
183
|
-
return '';
|
|
184
|
-
}
|
|
185
|
-
visitSelectElements(ctx) {
|
|
186
|
-
let selectElements = ctx.text.toLowerCase();
|
|
187
|
-
console.log('SelectElements:', selectElements);
|
|
142
|
+
class MyVisitor extends MySqlParserVisitor<string> {
|
|
143
|
+
defaultResult(): string {
|
|
188
144
|
return '';
|
|
189
145
|
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
return 'Return by program context'
|
|
146
|
+
aggregateResult(aggregate: string, nextResult: string): string {
|
|
147
|
+
return aggregate + nextResult;
|
|
193
148
|
}
|
|
149
|
+
visitProgram = (ctx) => {
|
|
150
|
+
return this.visitChildren(ctx);
|
|
151
|
+
};
|
|
152
|
+
visitTableName = (ctx) => {
|
|
153
|
+
return ctx.getText();
|
|
154
|
+
};
|
|
194
155
|
}
|
|
195
156
|
const visitor = new MyVisitor();
|
|
196
|
-
const result = visitor.visit(
|
|
157
|
+
const result = visitor.visit(parseTree);
|
|
197
158
|
|
|
198
159
|
console.log(result);
|
|
199
160
|
```
|
|
@@ -202,60 +163,52 @@ console.log(result);
|
|
|
202
163
|
|
|
203
164
|
```javascript
|
|
204
165
|
/*
|
|
205
|
-
|
|
206
|
-
TableName: user1
|
|
207
|
-
*/
|
|
208
|
-
/*
|
|
209
|
-
Return by program node
|
|
166
|
+
user1
|
|
210
167
|
*/
|
|
211
168
|
```
|
|
212
169
|
|
|
213
|
-
> Tips: The node's method name can be found in the Visitor file under the corresponding SQL directory
|
|
214
|
-
|
|
215
170
|
### Listener
|
|
216
171
|
|
|
217
172
|
Access the specified node in the AST by the Listener
|
|
218
173
|
|
|
219
174
|
```typescript
|
|
220
|
-
import { MySQL } from 'dt-sql-parser';
|
|
221
|
-
import type { MySqlParserListener } from 'dt-sql-parser';
|
|
175
|
+
import { MySQL, MySqlParserListener } from 'dt-sql-parser';
|
|
222
176
|
|
|
223
|
-
const
|
|
224
|
-
const sql = 'select id,name from user1;';
|
|
225
|
-
const parseTree =
|
|
177
|
+
const mysql = new MySQL();
|
|
178
|
+
const sql = 'select id, name from user1;';
|
|
179
|
+
const parseTree = mysql.parse(sql);
|
|
226
180
|
|
|
227
|
-
class MyListener
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
}
|
|
232
|
-
enterSelectElements(ctx) {
|
|
233
|
-
let selectElements = ctx.text.toLowerCase();
|
|
234
|
-
console.log('SelectElements:', selectElements);
|
|
235
|
-
}
|
|
181
|
+
class MyListener extends MySqlParserListener {
|
|
182
|
+
result = '';
|
|
183
|
+
enterTableName = (ctx): void => {
|
|
184
|
+
this.result = ctx.getText();
|
|
185
|
+
};
|
|
236
186
|
}
|
|
237
|
-
|
|
238
|
-
|
|
187
|
+
|
|
188
|
+
const listener = new MyListener();
|
|
189
|
+
mysql.listen(listener, parseTree);
|
|
190
|
+
|
|
191
|
+
console.log(listener.result)
|
|
239
192
|
```
|
|
240
193
|
|
|
241
194
|
*output:*
|
|
242
195
|
|
|
243
196
|
```javascript
|
|
244
197
|
/*
|
|
245
|
-
|
|
246
|
-
TableName: user1
|
|
198
|
+
user1
|
|
247
199
|
*/
|
|
248
200
|
```
|
|
249
201
|
|
|
250
|
-
> Tips: The node's method name can be found in the Listener file under the corresponding SQL directory
|
|
251
|
-
|
|
252
202
|
### Splitting SQL statements
|
|
253
|
-
Take `FlinkSQL` as an example:
|
|
203
|
+
Take `FlinkSQL` as an example, call the `splitSQLByStatement` method on the SQL instance:
|
|
204
|
+
|
|
254
205
|
```javascript
|
|
255
206
|
import { FlinkSQL } from 'dt-sql-parser';
|
|
256
|
-
|
|
207
|
+
|
|
208
|
+
const flink = new FlinkSQL();
|
|
257
209
|
const sql = 'SHOW TABLES;\nSELECT * FROM tb;';
|
|
258
|
-
const sqlSlices =
|
|
210
|
+
const sqlSlices = flink.splitSQLByStatement(sql);
|
|
211
|
+
|
|
259
212
|
console.log(sqlSlices)
|
|
260
213
|
```
|
|
261
214
|
|
|
@@ -288,17 +241,18 @@ console.log(sqlSlices)
|
|
|
288
241
|
|
|
289
242
|
### Code Completion
|
|
290
243
|
Obtaining code completion information at a specified position in SQL.
|
|
291
|
-
We can refer to the example of using `FlinkSQL`.
|
|
292
244
|
|
|
293
|
-
|
|
245
|
+
Call the `getAllEntities` method on the SQL instance, pass the SQL content and the row and column numbers indicating the position where code completion is desired. The following are some additional explanations about [CaretPosition](#caretposition-of-code-completion).
|
|
294
246
|
+ **keyword candidates list**
|
|
295
247
|
|
|
296
248
|
```javascript
|
|
297
249
|
import { FlinkSQL } from 'dt-sql-parser';
|
|
298
|
-
|
|
250
|
+
|
|
251
|
+
const flink = new FlinkSQL();
|
|
299
252
|
const sql = 'CREATE ';
|
|
300
253
|
const pos = { lineNumber: 1, column: 16 }; // the end position
|
|
301
|
-
const keywords =
|
|
254
|
+
const keywords = flink.getSuggestionAtCaretPosition(sql, pos)?.keywords;
|
|
255
|
+
|
|
302
256
|
console.log(keywords);
|
|
303
257
|
```
|
|
304
258
|
*output:*
|
|
@@ -309,10 +263,13 @@ Invoke the `getSuggestionAtCaretPosition` method, pass the SQL content and the r
|
|
|
309
263
|
```
|
|
310
264
|
+ **Obtaining information related to grammar completion**
|
|
311
265
|
```javascript
|
|
312
|
-
|
|
266
|
+
import { FlinkSQL } from 'dt-sql-parser';
|
|
267
|
+
|
|
268
|
+
const flink = new FlinkSQL();
|
|
313
269
|
const sql = 'SELECT * FROM tb';
|
|
314
270
|
const pos = { lineNumber: 1, column: 16 }; // after 'tb'
|
|
315
|
-
const syntaxSuggestions =
|
|
271
|
+
const syntaxSuggestions = flink.getSuggestionAtCaretPosition(sql, pos)?.syntax;
|
|
272
|
+
|
|
316
273
|
console.log(syntaxSuggestions);
|
|
317
274
|
```
|
|
318
275
|
*output:*
|
|
@@ -350,6 +307,55 @@ Invoke the `getSuggestionAtCaretPosition` method, pass the SQL content and the r
|
|
|
350
307
|
```
|
|
351
308
|
The grammar-related code completion information returns an array, where each item represents what grammar can be filled in at that position. For example, the output in the above example represents that the position can be filled with either a **table name** or **a view name**. In this case, `syntaxContextType` represents the type of grammar that can be completed, and `wordRanges` represents the content that has already been filled.
|
|
352
309
|
|
|
310
|
+
|
|
311
|
+
### Get all entities in SQL (e.g. table, column)
|
|
312
|
+
Call the `getAllEntities` method on the SQL instance, and pass in the sql text and the row and column numbers at the specified location to easily get them.
|
|
313
|
+
|
|
314
|
+
```typescript
|
|
315
|
+
import { FlinkSQL } from 'dt-sql-parser';
|
|
316
|
+
|
|
317
|
+
const flink = new FlinkSQL();
|
|
318
|
+
const sql = 'SELECT * FROM tb;';
|
|
319
|
+
const pos = { lineNumber: 1, column: 16 }; // tb 的后面
|
|
320
|
+
const entities = flink.getAllEntities(sql, pos);
|
|
321
|
+
|
|
322
|
+
console.log(entities);
|
|
323
|
+
```
|
|
324
|
+
|
|
325
|
+
*output*
|
|
326
|
+
|
|
327
|
+
```typescript
|
|
328
|
+
/*
|
|
329
|
+
[
|
|
330
|
+
{
|
|
331
|
+
entityContextType: 'table',
|
|
332
|
+
text: 'tb',
|
|
333
|
+
position: {
|
|
334
|
+
line: 1,
|
|
335
|
+
startIndex: 14,
|
|
336
|
+
endIndex: 15,
|
|
337
|
+
startColumn: 15,
|
|
338
|
+
endColumn: 17
|
|
339
|
+
},
|
|
340
|
+
belongStmt: {
|
|
341
|
+
stmtContextType: 'selectStmt',
|
|
342
|
+
position: [Object],
|
|
343
|
+
rootStmt: [Object],
|
|
344
|
+
parentStmt: [Object],
|
|
345
|
+
isContainCaret: true
|
|
346
|
+
},
|
|
347
|
+
relatedEntities: null,
|
|
348
|
+
columns: null,
|
|
349
|
+
isAlias: false,
|
|
350
|
+
origin: null,
|
|
351
|
+
alias: null
|
|
352
|
+
}
|
|
353
|
+
]
|
|
354
|
+
*/
|
|
355
|
+
```
|
|
356
|
+
|
|
357
|
+
Position is not required, if the position is passed, then in the collected entities, if the entity is located under the statement where the corresponding position is located, then the statement object to which the entity belongs will be marked with `isContainCaret`, which can help you quickly filter out the required entities when combined with the code completion function.
|
|
358
|
+
|
|
353
359
|
### Other API
|
|
354
360
|
|
|
355
361
|
- `createLexer` Create an instance of Antlr4 Lexer and return it;
|
|
@@ -428,6 +434,7 @@ At this time, dt-sql-parser will think that `SHOW` is already a complete Token,
|
|
|
428
434
|
For the editor, this strategy is also more intuitive. After the user enters `SHOW`, before pressing the space key, the user probably has not finished entering, maybe the user wants to enter something like `SHOWS`. When the user presses the space key, the editor thinks that the user wants to enter the next Token, and it is time to ask dt-sql-parser what can be filled in the next Token position.
|
|
429
435
|
|
|
430
436
|
<br/>
|
|
437
|
+
|
|
431
438
|
## License
|
|
432
439
|
|
|
433
440
|
[MIT](./LICENSE)
|
package/dist/index.d.ts
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
3
|
-
export
|
|
4
|
-
export {
|
|
5
|
-
|
|
1
|
+
export { MySQL, FlinkSQL, SparkSQL, HiveSQL, PostgreSQL, TrinoSQL, ImpalaSQL, PLSQL, } from './parser';
|
|
2
|
+
export { MySqlParserListener, MySqlParserVisitor, FlinkSqlParserListener, FlinkSqlParserVisitor, SparkSqlParserListener, SparkSqlParserVisitor, HiveSqlParserListener, HiveSqlParserVisitor, PlSqlParserListener, PlSqlParserVisitor, PostgreSqlParserListener, PostgreSqlParserVisitor, TrinoSqlListener, TrinoSqlVisitor, ImpalaSqlParserListener, ImpalaSqlParserVisitor, } from './lib';
|
|
3
|
+
export { EntityContextType } from './parser/common/types';
|
|
4
|
+
export {
|
|
5
|
+
/**
|
|
6
|
+
* @deprecated SyntaxContextType has been renamed to {@link EntityContextType},
|
|
7
|
+
* It will be removed when the stable version is released.
|
|
8
|
+
*/
|
|
9
|
+
EntityContextType as SyntaxContextType, } from './parser/common/types';
|
|
10
|
+
export { StmtContextType } from './parser/common/entityCollector';
|
|
11
|
+
export type { CaretPosition, Suggestions, SyntaxSuggestion } from './parser/common/types';
|
|
12
|
+
export type { WordRange, TextSlice } from './parser/common/textAndWord';
|
|
6
13
|
export type { SyntaxError, ParseError, ErrorListener } from './parser/common/parseErrorListener';
|
|
14
|
+
export type { StmtContext, EntityContext } from './parser/common/entityCollector';
|
|
7
15
|
/**
|
|
8
|
-
* @deprecated
|
|
16
|
+
* @deprecated Legacy utils will be removed when the stable version is released.
|
|
9
17
|
*/
|
|
10
18
|
export * from './utils';
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,14 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export {
|
|
3
|
-
export {
|
|
1
|
+
export { MySQL, FlinkSQL, SparkSQL, HiveSQL, PostgreSQL, TrinoSQL, ImpalaSQL, PLSQL, } from './parser';
|
|
2
|
+
export { MySqlParserListener, MySqlParserVisitor, FlinkSqlParserListener, FlinkSqlParserVisitor, SparkSqlParserListener, SparkSqlParserVisitor, HiveSqlParserListener, HiveSqlParserVisitor, PlSqlParserListener, PlSqlParserVisitor, PostgreSqlParserListener, PostgreSqlParserVisitor, TrinoSqlListener, TrinoSqlVisitor, ImpalaSqlParserListener, ImpalaSqlParserVisitor, } from './lib';
|
|
3
|
+
export { EntityContextType } from './parser/common/types';
|
|
4
|
+
export {
|
|
4
5
|
/**
|
|
5
|
-
* @deprecated
|
|
6
|
+
* @deprecated SyntaxContextType has been renamed to {@link EntityContextType},
|
|
7
|
+
* It will be removed when the stable version is released.
|
|
8
|
+
*/
|
|
9
|
+
EntityContextType as SyntaxContextType, } from './parser/common/types';
|
|
10
|
+
export { StmtContextType } from './parser/common/entityCollector';
|
|
11
|
+
/**
|
|
12
|
+
* @deprecated Legacy utils will be removed when the stable version is released.
|
|
6
13
|
*/
|
|
7
14
|
export * from './utils';
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import * as antlr from "antlr4ng";
|
|
2
|
+
export declare abstract class SQLParserBase<T = antlr.ParserRuleContext> extends antlr.Parser {
|
|
3
|
+
constructor(input: antlr.TokenStream);
|
|
4
|
+
abstract program(): T;
|
|
5
|
+
caretTokenIndex: number;
|
|
6
|
+
entityCollecting: boolean;
|
|
7
|
+
shouldMatchEmpty(): boolean;
|
|
8
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import * as antlr from "antlr4ng";
|
|
2
|
+
export class SQLParserBase extends antlr.Parser {
|
|
3
|
+
constructor(input) {
|
|
4
|
+
super(input);
|
|
5
|
+
this.caretTokenIndex = -1;
|
|
6
|
+
this.entityCollecting = false;
|
|
7
|
+
}
|
|
8
|
+
shouldMatchEmpty() {
|
|
9
|
+
var _a, _b, _c, _d;
|
|
10
|
+
return this.entityCollecting
|
|
11
|
+
&& ((_b = (_a = this.tokenStream.LT(-1)) === null || _a === void 0 ? void 0 : _a.tokenIndex) !== null && _b !== void 0 ? _b : Infinity) <= this.caretTokenIndex
|
|
12
|
+
&& ((_d = (_c = this.tokenStream.LT(1)) === null || _c === void 0 ? void 0 : _c.tokenIndex) !== null && _d !== void 0 ? _d : -Infinity) >= this.caretTokenIndex;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -542,8 +542,8 @@ export declare class FlinkSqlLexer extends antlr.Lexer {
|
|
|
542
542
|
static readonly BIT_STRING = 540;
|
|
543
543
|
static readonly ID_LITERAL = 541;
|
|
544
544
|
static readonly channelNames: string[];
|
|
545
|
-
static readonly literalNames: string[];
|
|
546
|
-
static readonly symbolicNames: string[];
|
|
545
|
+
static readonly literalNames: (string | null)[];
|
|
546
|
+
static readonly symbolicNames: (string | null)[];
|
|
547
547
|
static readonly modeNames: string[];
|
|
548
548
|
static readonly ruleNames: string[];
|
|
549
549
|
constructor(input: antlr.CharStream);
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
// Generated from dt-sql-parser/src/grammar/
|
|
1
|
+
// Generated from dt-sql-parser/src/grammar/flink/FlinkSqlLexer.g4 by ANTLR 4.13.1
|
|
2
|
+
// @ts-nocheck
|
|
2
3
|
import * as antlr from "antlr4ng";
|
|
3
4
|
class FlinkSqlLexer extends antlr.Lexer {
|
|
4
5
|
constructor(input) {
|
|
@@ -2,7 +2,8 @@ import * as antlr from "antlr4ng";
|
|
|
2
2
|
import { Token } from "antlr4ng";
|
|
3
3
|
import { FlinkSqlParserListener } from "./FlinkSqlParserListener.js";
|
|
4
4
|
import { FlinkSqlParserVisitor } from "./FlinkSqlParserVisitor.js";
|
|
5
|
-
|
|
5
|
+
import { SQLParserBase } from '../SQLParserBase';
|
|
6
|
+
export declare class FlinkSqlParser extends SQLParserBase {
|
|
6
7
|
static readonly SPACE = 1;
|
|
7
8
|
static readonly COMMENT_INPUT = 2;
|
|
8
9
|
static readonly LINE_COMMENT = 3;
|
|
@@ -732,8 +733,8 @@ export declare class FlinkSqlParser extends antlr.Parser {
|
|
|
732
733
|
static readonly RULE_reservedKeywordsUsedAsFuncParam = 185;
|
|
733
734
|
static readonly RULE_reservedKeywordsUsedAsFuncName = 186;
|
|
734
735
|
static readonly RULE_nonReservedKeywords = 187;
|
|
735
|
-
static readonly literalNames: string[];
|
|
736
|
-
static readonly symbolicNames: string[];
|
|
736
|
+
static readonly literalNames: (string | null)[];
|
|
737
|
+
static readonly symbolicNames: (string | null)[];
|
|
737
738
|
static readonly ruleNames: string[];
|
|
738
739
|
get grammarFileName(): string;
|
|
739
740
|
get literalNames(): (string | null)[];
|
|
@@ -936,6 +937,7 @@ export declare class FlinkSqlParser extends antlr.Parser {
|
|
|
936
937
|
reservedKeywordsUsedAsFuncName(): ReservedKeywordsUsedAsFuncNameContext;
|
|
937
938
|
nonReservedKeywords(): NonReservedKeywordsContext;
|
|
938
939
|
sempred(localContext: antlr.RuleContext | null, ruleIndex: number, predIndex: number): boolean;
|
|
940
|
+
private columnName_sempred;
|
|
939
941
|
private queryStatement_sempred;
|
|
940
942
|
private tableExpression_sempred;
|
|
941
943
|
private booleanExpression_sempred;
|
|
@@ -1294,7 +1296,6 @@ export declare class ColumnNameCreateContext extends antlr.ParserRuleContext {
|
|
|
1294
1296
|
export declare class ColumnNameContext extends antlr.ParserRuleContext {
|
|
1295
1297
|
constructor(parent: antlr.ParserRuleContext | null, invokingState: number);
|
|
1296
1298
|
uid(): UidContext | null;
|
|
1297
|
-
expression(): ExpressionContext | null;
|
|
1298
1299
|
get ruleIndex(): number;
|
|
1299
1300
|
enterRule(listener: FlinkSqlParserListener): void;
|
|
1300
1301
|
exitRule(listener: FlinkSqlParserListener): void;
|
|
@@ -1585,13 +1586,6 @@ export declare class ApplyTransformContext extends TransformContext {
|
|
|
1585
1586
|
exitRule(listener: FlinkSqlParserListener): void;
|
|
1586
1587
|
accept<Result>(visitor: FlinkSqlParserVisitor<Result>): Result | null;
|
|
1587
1588
|
}
|
|
1588
|
-
export declare class ColumnTransformContext extends TransformContext {
|
|
1589
|
-
constructor(ctx: TransformContext);
|
|
1590
|
-
qualifiedName(): QualifiedNameContext;
|
|
1591
|
-
enterRule(listener: FlinkSqlParserListener): void;
|
|
1592
|
-
exitRule(listener: FlinkSqlParserListener): void;
|
|
1593
|
-
accept<Result>(visitor: FlinkSqlParserVisitor<Result>): Result | null;
|
|
1594
|
-
}
|
|
1595
1589
|
export declare class TransformArgumentContext extends antlr.ParserRuleContext {
|
|
1596
1590
|
constructor(parent: antlr.ParserRuleContext | null, invokingState: number);
|
|
1597
1591
|
qualifiedName(): QualifiedNameContext | null;
|