0.0.1-alpha.5

- entity添加belongsToEntity属性
- 修改entity和stmt的关联逻辑,不再取栈顶元素,而是搜索匹配ruleIndex的元素.这样可以在匹配多个规则链时都关联
This commit is contained in:
Kijin-Seija 2024-04-10 18:40:49 +08:00
parent 9995067bbe
commit e7729dae33
4 changed files with 28 additions and 11 deletions

View File

@ -1,6 +1,6 @@
{
"name": "dt-sql-parser-semantic-analyse-plugin",
"version": "0.0.1-alpha.4",
"version": "0.0.1-alpha.5",
"description": "an dt-sql-parser plugin with semantic result",
"type": "module",
"files": [

View File

@ -1,6 +1,7 @@
import { PostgreSQLParser } from 'dt-sql-parser/dist/lib/pgsql/PostgreSQLParser'
export const defaultStmts = [
'stmt',
// select statement
'selectstmt'
]
@ -21,6 +22,16 @@ export const defaultEntities = [
]
export const defaultRules: Record<string, number[]> = {
// 通用的简单column规则不带.运算符的column)
common_column_simple: [
PostgreSQLParser.RULE_stmt,
PostgreSQLParser.RULE_column_name
],
// 通用的复合column规则带.运算符的column)
common_column_ref: [
PostgreSQLParser.RULE_stmt,
PostgreSQLParser.RULE_columnref
],
select_target_column_simple: [
PostgreSQLParser.RULE_selectstmt,
PostgreSQLParser.RULE_select_clause,

View File

@ -58,26 +58,31 @@ export class SQLVisitor extends AbstractParseTreeVisitor<void> implements Postgr
(this as any)[visitorName] = (ctx: ParserRuleContext) => {
const chain = this.getNodeChain(ctx)
for (const rule of rules) {
if (this.matchRules(chain, this.rules.get(rule))) {
const ruleChain = this.rules.get(rule)
if (!ruleChain) continue
if (this.matchRules(chain, ruleChain)) {
const ruleChainBegin = ruleChain[0]
const beginStmt = this.stmtStack.find(stmt => stmt.type === ruleChainBegin)
const beginEntity = this.entityStack.find(entity => entity.type === ruleChainBegin)
const result: Entity = {
rule,
text: ctx.text,
type: ctx.ruleIndex,
caret: withCaret(ctx),
belongsToStmt: this.stmtStack[this.stmtStack.length - 1],
belongsToStmt: beginStmt || null,
belongsToEntity: beginEntity || null,
relatedEntities: {}
}
if (this.entityStack[this.entityStack.length - 1]) {
if (!this.entityStack[this.entityStack.length - 1].relatedEntities[rule]) this.entityStack[this.entityStack.length - 1].relatedEntities[rule] = []
this.entityStack[this.entityStack.length - 1].relatedEntities[rule].push(result)
} else {
if (!this.stmtStack[this.stmtStack.length - 1].relatedEntities[rule]) this.stmtStack[this.stmtStack.length - 1].relatedEntities[rule] = []
this.stmtStack[this.stmtStack.length - 1].relatedEntities[rule].push(result)
if (beginEntity) {
if (!beginEntity.relatedEntities[rule]) beginEntity.relatedEntities[rule] = []
beginEntity.relatedEntities[rule].push(result)
} else if (beginStmt) {
if (!beginStmt.relatedEntities[rule]) beginStmt.relatedEntities[rule] = []
beginStmt.relatedEntities[rule].push(result)
}
if (withCaret(ctx)) this.result.nerestCaretEntityList.push(result)
this.entityStack.push(result)
isHitRule = true
break
}
}
this.visitChildren(ctx)

View File

@ -52,7 +52,8 @@ export interface Entity {
text: string
type: number
caret: boolean
belongsToStmt: Stmt
belongsToStmt: Stmt | null
belongsToEntity: Entity | null
relatedEntities: Record<string, Entity[]>
}