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", "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", "description": "an dt-sql-parser plugin with semantic result",
"type": "module", "type": "module",
"files": [ "files": [

View File

@ -1,6 +1,7 @@
import { PostgreSQLParser } from 'dt-sql-parser/dist/lib/pgsql/PostgreSQLParser' import { PostgreSQLParser } from 'dt-sql-parser/dist/lib/pgsql/PostgreSQLParser'
export const defaultStmts = [ export const defaultStmts = [
'stmt',
// select statement // select statement
'selectstmt' 'selectstmt'
] ]
@ -21,6 +22,16 @@ export const defaultEntities = [
] ]
export const defaultRules: Record<string, number[]> = { 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: [ select_target_column_simple: [
PostgreSQLParser.RULE_selectstmt, PostgreSQLParser.RULE_selectstmt,
PostgreSQLParser.RULE_select_clause, PostgreSQLParser.RULE_select_clause,

View File

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

View File

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