Compare commits

..

10 Commits

Author SHA1 Message Date
yexinhao
b1c8a64804 feat: 更换npm名称与管理者 2024-09-26 18:01:47 +08:00
Kijin-Seija
fa4666273d 0.0.1-alpha.15 2024-06-19 20:12:27 +08:00
Kijin-Seija
9e0985b903 Merge branch 'master' of https://github.com/Kijin-Seija/dt-sql-parser-semantic-analyse-plugin 2024-06-19 14:09:54 +08:00
Kijin-Seija
f20b53d83f 0.0.1-alpha.11 2024-06-19 14:09:46 +08:00
Kijin-Seija
f48fabd74c
Update README.md 2024-06-05 16:01:24 +08:00
Kijin-Seija
3610d174d9 0.0.1-alpha.9 2024-05-28 11:33:18 +08:00
Kijin-Seija
77a29dfb40 Merge branch 'master' of https://github.com/Kijin-Seija/dt-sql-parser-semantic-analyse-plugin 2024-05-28 11:32:37 +08:00
Kijin-Seija
c889392d0d fix: sub entities should not be considered as nerestCaretEntities 2024-05-28 11:32:20 +08:00
Kijin-Seija
08ce5c0c41
Update README.md 2024-05-17 16:48:51 +08:00
Kijin-Seija
cd79c1305c
Update README.md 2024-05-16 09:51:17 +08:00
6 changed files with 138 additions and 19 deletions

View File

@ -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(),
...
]
})
```

View File

@ -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
View File

@ -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"

View File

@ -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"
}
}

View File

@ -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,
]
}

View File

@ -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
}