fix: sub entities should not be considered as nerestCaretEntities

This commit is contained in:
Kijin-Seija 2024-05-28 11:32:20 +08:00
parent 68d04deafc
commit c889392d0d
3 changed files with 21 additions and 6 deletions

View File

@ -12,7 +12,7 @@
<script type="module"> <script type="module">
const { DtSqlParserSemAnalysePlugin } = await import(/* @vite-ignore */import.meta.env.VITE_ENTRY_PATH) const { DtSqlParserSemAnalysePlugin } = await import(/* @vite-ignore */import.meta.env.VITE_ENTRY_PATH)
const myPlugin = new DtSqlParserSemAnalysePlugin() const myPlugin = new DtSqlParserSemAnalysePlugin()
const sql = 'SELECT a.| AS c' const sql = 'INSERT INTO |'
const caretColumn = sql.indexOf('|') + 1 const caretColumn = sql.indexOf('|') + 1
const result = myPlugin.parse(sql.replace('|', ''), { lineNumber: 1, columnNumber: caretColumn }) const result = myPlugin.parse(sql.replace('|', ''), { lineNumber: 1, columnNumber: caretColumn })
console.log(result) console.log(result)

View File

@ -2,23 +2,25 @@ import { PostgreSqlParser } from 'dt-sql-parser/dist/lib/postgresql/PostgreSqlPa
export const defaultAlias = { export const defaultAlias = {
selectstmt: 'selectStatement', selectstmt: 'selectStatement',
target_el: 'target_label' target_el: 'target_label',
table_name: 'tableName'
} }
export const defaultStmts = [ export const defaultStmts = [
'simple_select', 'selectstmt',
] ]
export const defaultEntities = [ export const defaultEntities = [
'target_el', 'target_el',
'colid', 'colid',
'attr_name', 'attr_name',
'collabel' 'collabel',
'table_name'
] ]
export const defaultRules: Record<string, number[]> = { export const defaultRules: Record<string, number[]> = {
select_target: [ select_target: [
PostgreSqlParser.RULE_simple_select, PostgreSqlParser.RULE_selectstmt,
PostgreSqlParser.RULE_target_el, PostgreSqlParser.RULE_target_el,
], ],
select_target_colid: [ select_target_colid: [
@ -35,5 +37,18 @@ export const defaultRules: Record<string, number[]> = {
PostgreSqlParser.RULE_target_el, PostgreSqlParser.RULE_target_el,
-PostgreSqlParser.RULE_attr_name, -PostgreSqlParser.RULE_attr_name,
PostgreSqlParser.RULE_collabel PostgreSqlParser.RULE_collabel
],
select_from_table: [
PostgreSqlParser.RULE_selectstmt,
PostgreSqlParser.RULE_from_clause,
PostgreSqlParser.RULE_table_name
],
table_name_colid: [
PostgreSqlParser.RULE_table_name,
PostgreSqlParser.RULE_colid
],
table_name_attr: [
PostgreSqlParser.RULE_table_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] = [] if (!beginStmt.relatedEntities[rule]) beginStmt.relatedEntities[rule] = []
beginStmt.relatedEntities[rule].push(result) 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) this.entityStack.push(result)
isHitRule = true isHitRule = true
} }