Compare commits
10 Commits
a63b373bae
...
b1c8a64804
Author | SHA1 | Date | |
---|---|---|---|
|
b1c8a64804 | ||
|
fa4666273d | ||
|
9e0985b903 | ||
|
f20b53d83f | ||
|
f48fabd74c | ||
|
3610d174d9 | ||
|
77a29dfb40 | ||
|
c889392d0d | ||
|
08ce5c0c41 | ||
|
cd79c1305c |
10
README.md
10
README.md
@ -55,7 +55,7 @@ console.log(result)
|
||||
|
||||
**Notice: A rule must start with a/an statement/entity and stop with an entity. You should add a node keywords(keyword is in your parser with format: `RULE_[keyword]`) into stmts/entities before using it.**
|
||||
|
||||
## Rule Chain Opeator
|
||||
## Rule Chain Operator
|
||||
|
||||
You can set a negative number whose abs equals to a ruleIndex. That means exclude this rule.
|
||||
|
||||
@ -75,18 +75,20 @@ select_target_alias: [
|
||||
|
||||
Some node names in dt-sql-parser code are different from their antlr4's definition.
|
||||
|
||||
You can find possible alias in https://github.com/DTStack/dt-sql-parser/blob/main/src/grammar/postgresql/PostgreSqlParser.g4, then add it into `alias` option.
|
||||
|
||||
Example:
|
||||
|
||||
![alt text](./assets/alias-example.png)
|
||||
|
||||
You can find possible alias in https://github.com/DTStack/dt-sql-parser/blob/main/src/grammar/postgresql/PostgreSqlParser.g4, then add it into `alias` option.
|
||||
|
||||
## Add a preprocessor
|
||||
|
||||
```typescript
|
||||
const myPlugin = new DtSqlParserSemAnalysePlugin({
|
||||
preprocessor: [
|
||||
(sql) => sql.toUpperCase()
|
||||
(sql) => sql.toUpperCase(),
|
||||
...
|
||||
]
|
||||
})
|
||||
```
|
||||
|
||||
|
@ -12,7 +12,7 @@
|
||||
<script type="module">
|
||||
const { DtSqlParserSemAnalysePlugin } = await import(/* @vite-ignore */import.meta.env.VITE_ENTRY_PATH)
|
||||
const myPlugin = new DtSqlParserSemAnalysePlugin()
|
||||
const sql = 'SELECT a.| AS c'
|
||||
const sql = 'CREATE TABLE "public"."h1" (c1 int8 not null, c2 int8, c3 int8) with (appendonly = true, orientation = horc) TABLESPACE horc_default DISTRIBUTED BY (|)'
|
||||
const caretColumn = sql.indexOf('|') + 1
|
||||
const result = myPlugin.parse(sql.replace('|', ''), { lineNumber: 1, columnNumber: caretColumn })
|
||||
console.log(result)
|
||||
|
13
package-lock.json
generated
13
package-lock.json
generated
@ -1,14 +1,14 @@
|
||||
{
|
||||
"name": "dt-sql-parser-semantic-analyse-plugin",
|
||||
"version": "0.0.1-alpha.7",
|
||||
"version": "0.0.1-alpha.14",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "dt-sql-parser-semantic-analyse-plugin",
|
||||
"version": "0.0.1-alpha.6",
|
||||
"version": "0.0.1-alpha.14",
|
||||
"dependencies": {
|
||||
"dt-sql-parser": "^4.0.0-beta.4.12"
|
||||
"dt-sql-parser": "npm:dt-sql-parser-oushudb@^4.0.2-5"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@eslint/eslintrc": "^3.0.2",
|
||||
@ -1904,9 +1904,10 @@
|
||||
}
|
||||
},
|
||||
"node_modules/dt-sql-parser": {
|
||||
"version": "4.0.0-beta.4.12",
|
||||
"resolved": "http://npm.oushu.com:14837/dt-sql-parser/-/dt-sql-parser-4.0.0-beta.4.12.tgz",
|
||||
"integrity": "sha512-kfLRecn+dfdZjrKt3Ovm52ryoh9UGF+dCjNzV2pk8XI5kgF7lgQJhPZdRi+2yn1AU/BWQ1/0E6LnvFYbZ7Wr9Q==",
|
||||
"name": "dt-sql-parser-oushudb",
|
||||
"version": "4.0.2-5",
|
||||
"resolved": "http://npm.oushu.com:14837/dt-sql-parser-oushudb/-/dt-sql-parser-oushudb-4.0.2-5.tgz",
|
||||
"integrity": "sha512-fHu00/+Mk+QtCrS24Uc1vYV3xsy8BxyLyD0Oh4/TJwhBq7U+ZWunjHzcQBMccPyb8JnTYcG/HZ/f5EZ2GqQG4Q==",
|
||||
"dependencies": {
|
||||
"antlr4-c3": "3.3.7",
|
||||
"antlr4ng": "2.0.11"
|
||||
|
11
package.json
11
package.json
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "dt-sql-parser-semantic-analyse-plugin",
|
||||
"version": "0.0.1-alpha.8",
|
||||
"name": "lava-dt-sql-parser-semantic-analyse-plugin",
|
||||
"version": "0.0.1-alpha.16",
|
||||
"description": "an dt-sql-parser plugin with semantic result",
|
||||
"type": "module",
|
||||
"files": [
|
||||
@ -37,6 +37,11 @@
|
||||
"vite-plugin-node-polyfills": "^0.21.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"dt-sql-parser": "^4.0.0-beta.4.12"
|
||||
"dt-sql-parser": "npm:lava-oushudb-dt-sql-parser@^4.0.2-10"
|
||||
},
|
||||
"git repository": "https://git.yevpt.com/oushu/lava-dt-sql-parser-semantic-analyse-plugin",
|
||||
"repository": "https://git.yevpt.com/oushu/lava-dt-sql-parser-semantic-analyse-plugin",
|
||||
"volta": {
|
||||
"node": "14.21.3"
|
||||
}
|
||||
}
|
||||
|
@ -2,25 +2,104 @@ import { PostgreSqlParser } from 'dt-sql-parser/dist/lib/postgresql/PostgreSqlPa
|
||||
|
||||
export const defaultAlias = {
|
||||
selectstmt: 'selectStatement',
|
||||
target_el: 'target_label'
|
||||
insertstmt: 'insertStatement',
|
||||
target_el: 'target_label',
|
||||
table_name: 'tableName',
|
||||
view_name: 'viewName',
|
||||
column_name: 'columnName',
|
||||
schema_name: 'schemaName'
|
||||
}
|
||||
|
||||
export const defaultStmts = [
|
||||
'simple_select',
|
||||
'selectstmt',
|
||||
'insertstmt',
|
||||
'updatestmt',
|
||||
'deletestmt',
|
||||
'altertablestmt',
|
||||
'alterfunctionstmt',
|
||||
'dropstmt',
|
||||
'createfunctionstmt'
|
||||
]
|
||||
|
||||
export const defaultEntities = [
|
||||
'target_el',
|
||||
'colid',
|
||||
'attr_name',
|
||||
'collabel'
|
||||
'collabel',
|
||||
'table_name',
|
||||
'view_name',
|
||||
'function_with_argtypes',
|
||||
'column_name',
|
||||
'schema_name',
|
||||
'anysconst'
|
||||
]
|
||||
|
||||
export const defaultRules: Record<string, number[]> = {
|
||||
select_target: [
|
||||
PostgreSqlParser.RULE_simple_select,
|
||||
PostgreSqlParser.RULE_selectstmt,
|
||||
PostgreSqlParser.RULE_target_el,
|
||||
],
|
||||
select_from_table: [
|
||||
PostgreSqlParser.RULE_selectstmt,
|
||||
PostgreSqlParser.RULE_from_clause,
|
||||
PostgreSqlParser.RULE_table_name
|
||||
],
|
||||
insert_target_table: [
|
||||
PostgreSqlParser.RULE_insertstmt,
|
||||
PostgreSqlParser.RULE_insert_target,
|
||||
PostgreSqlParser.RULE_table_name
|
||||
],
|
||||
update_relation_table: [
|
||||
PostgreSqlParser.RULE_updatestmt,
|
||||
PostgreSqlParser.RULE_relation_expr,
|
||||
PostgreSqlParser.RULE_table_name
|
||||
],
|
||||
delete_relation_table: [
|
||||
PostgreSqlParser.RULE_deletestmt,
|
||||
PostgreSqlParser.RULE_relation_expr,
|
||||
PostgreSqlParser.RULE_table_name
|
||||
],
|
||||
alter_table: [
|
||||
PostgreSqlParser.RULE_altertablestmt,
|
||||
PostgreSqlParser.RULE_relation_expr,
|
||||
PostgreSqlParser.RULE_table_name
|
||||
],
|
||||
alter_view: [
|
||||
PostgreSqlParser.RULE_altertablestmt,
|
||||
PostgreSqlParser.RULE_view_name
|
||||
],
|
||||
alter_function: [
|
||||
PostgreSqlParser.RULE_alterfunctionstmt,
|
||||
PostgreSqlParser.RULE_alterFunctionTypeClause,
|
||||
PostgreSqlParser.RULE_function_with_argtypes
|
||||
],
|
||||
alter_table_drop_column: [
|
||||
PostgreSqlParser.RULE_altertablestmt,
|
||||
PostgreSqlParser.RULE_alter_table_cmds,
|
||||
PostgreSqlParser.RULE_column_name
|
||||
],
|
||||
drop_table: [
|
||||
PostgreSqlParser.RULE_dropstmt,
|
||||
PostgreSqlParser.RULE_table_name_list,
|
||||
PostgreSqlParser.RULE_table_name
|
||||
],
|
||||
drop_view: [
|
||||
PostgreSqlParser.RULE_dropstmt,
|
||||
PostgreSqlParser.RULE_view_nameList,
|
||||
PostgreSqlParser.RULE_view_name
|
||||
],
|
||||
drop_schema: [
|
||||
PostgreSqlParser.RULE_dropstmt,
|
||||
PostgreSqlParser.RULE_schema_name_list,
|
||||
PostgreSqlParser.RULE_schema_name
|
||||
],
|
||||
create_function_sub_content: [
|
||||
PostgreSqlParser.RULE_createfunctionstmt,
|
||||
PostgreSqlParser.RULE_createfunc_opt_list,
|
||||
PostgreSqlParser.RULE_colid,
|
||||
PostgreSqlParser.RULE_anysconst,
|
||||
],
|
||||
// sub entities
|
||||
select_target_colid: [
|
||||
PostgreSqlParser.RULE_target_el,
|
||||
PostgreSqlParser.RULE_function_name,
|
||||
@ -35,5 +114,37 @@ export const defaultRules: Record<string, number[]> = {
|
||||
PostgreSqlParser.RULE_target_el,
|
||||
-PostgreSqlParser.RULE_attr_name,
|
||||
PostgreSqlParser.RULE_collabel
|
||||
],
|
||||
table_name_colid: [
|
||||
PostgreSqlParser.RULE_table_name,
|
||||
PostgreSqlParser.RULE_colid
|
||||
],
|
||||
table_name_attr: [
|
||||
PostgreSqlParser.RULE_table_name,
|
||||
PostgreSqlParser.RULE_attr_name,
|
||||
],
|
||||
view_name_colid: [
|
||||
PostgreSqlParser.RULE_view_name,
|
||||
PostgreSqlParser.RULE_colid
|
||||
],
|
||||
view_name_attr: [
|
||||
PostgreSqlParser.RULE_view_name,
|
||||
PostgreSqlParser.RULE_attr_name,
|
||||
],
|
||||
function_colid: [
|
||||
PostgreSqlParser.RULE_function_with_argtypes,
|
||||
PostgreSqlParser.RULE_colid
|
||||
],
|
||||
function_attr: [
|
||||
PostgreSqlParser.RULE_function_with_argtypes,
|
||||
PostgreSqlParser.RULE_attr_name,
|
||||
],
|
||||
column_name_colid: [
|
||||
PostgreSqlParser.RULE_column_name,
|
||||
PostgreSqlParser.RULE_colid
|
||||
],
|
||||
column_name_attr: [
|
||||
PostgreSqlParser.RULE_column_name,
|
||||
PostgreSqlParser.RULE_attr_name,
|
||||
]
|
||||
}
|
||||
|
@ -85,7 +85,7 @@ export class SQLVisitor extends AbstractParseTreeVisitor<void> implements Postgr
|
||||
if (!beginStmt.relatedEntities[rule]) beginStmt.relatedEntities[rule] = []
|
||||
beginStmt.relatedEntities[rule].push(result)
|
||||
}
|
||||
if (withCaret(ctx)) this.result.nerestCaretEntityList.push(result)
|
||||
if (beginStmt && withCaret(ctx)) this.result.nerestCaretEntityList.push(result)
|
||||
this.entityStack.push(result)
|
||||
isHitRule = true
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user