Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor: migrate PostgreSQL resource change extraction to ANTLR parser (#17915)

h3n4l219a9eb4e248
diff --git a/backend/plugin/parser/pg/resource_change.go b/backend/plugin/parser/pg/resource_change.go
index c82b5955f5..d483be0c8f 100644
--- a/backend/plugin/parser/pg/resource_change.go
+++ b/backend/plugin/parser/pg/resource_change.go
@@ -11,7 +11,6 @@ import (
"github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
- "github.com/bytebase/bytebase/backend/plugin/parser/pg/legacy/ast"
"github.com/bytebase/bytebase/backend/store/model"
)
@@ -21,420 +20,862 @@ func init() {
}
func extractChangedResources(database string, _ string, dbSchema *model.DatabaseSchema, asts any, statement string) (*base.ChangeSummary, error) {
- // Check if this is ANTLR ParseResult
- if parseResult, ok := asts.(*ParseResult); ok {
- return extractChangedResourcesANTLR(database, "", dbSchema, parseResult, statement)
+ parseResult, ok := asts.(*ParseResult)
+ if !ok {
+ return nil, errors.Errorf("invalid ast type %T, expected *ParseResult", asts)
}
- // Fall back to legacy AST
- nodes, ok := asts.([]ast.Node)
- if !ok {
- return nil, errors.Errorf("invalid ast type %T", asts)
+ if parseResult == nil || parseResult.Tree == nil {
+ return nil, errors.New("parse result or tree is nil")
}
changedResources := model.NewChangedResources(dbSchema)
- dmlCount := 0
- insertCount := 0
- var sampleDMLs []string
searchPath := dbSchema.GetDatabaseMetadata().GetSearchPath()
if len(searchPath) == 0 {
searchPath = []string{"public"} // default search path for PostgreSQL
}
- for _, node := range nodes {
- if n, ok := node.(*ast.VariableSetStmt); ok {
- if strings.EqualFold(n.Name, "search_path") {
- var err error
- searchPath, err = getSearchPathFromSQL(n.Text())
- if err != nil {
- return nil, errors.Wrapf(err, "failed to get search path from statement %q", n.Text())
- }
- if len(searchPath) == 0 {
- searchPath = []string{"public"} // default search path for PostgreSQL
- }
- }
- }
- // schema is "public" by default.
- collectResourceChanges(database, searchPath, node, statement, changedResources, dbSchema.GetDatabaseMetadata())
-
- switch node := node.(type) {
- case *ast.InsertStmt, *ast.UpdateStmt, *ast.DeleteStmt:
- if node, ok := node.(*ast.InsertStmt); ok && len(node.ValueList) > 0 {
- insertCount += len(node.ValueList)
- continue
- }
- dmlCount++
- if len(sampleDMLs) < common.MaximumLintExplainSize {
- sampleDMLs = append(sampleDMLs, node.Text())
- }
- }
+ listener := &changedResourcesListener{
+ database: database,
+ searchPath: searchPath,
+ changedResources: changedResources,
+ databaseMetadata: dbSchema.GetDatabaseMetadata(),
+ statement: statement,
+ tokenStream: parseResult.Tokens,
+ dmlCount: 0,
+ insertCount: 0,
+ sampleDMLs: []string{},
}
+ antlr.ParseTreeWalkerDefault.Walk(listener, parseResult.Tree)
+
return &base.ChangeSummary{
ChangedResources: changedResources,
- DMLCount: dmlCount,
- SampleDMLS: sampleDMLs,
- InsertCount: insertCount,
+ DMLCount: listener.dmlCount,
+ SampleDMLS: listener.sampleDMLs,
+ InsertCount: listener.insertCount,
}, nil
}
-func collectResourceChanges(database string, searchPath []string, node ast.Node, statement string, changedResources *model.ChangedResources, databaseMetadata *model.DatabaseMetadata) {
- switch node := node.(type) {
- case *ast.CreateTableStmt:
- if node.Name.Type == ast.TableTypeBaseTable {
- d, s, table := node.Name.Database, node.Name.Schema, node.Name.Name
- if d == "" {
- d = database
- }
- if s == "" {
- s = searchPath[0] // default schema for PostgreSQL
- }
- changedResources.AddTable(
- d,
- s,
- &storepb.ChangedResourceTable{
- Name: table,
- Ranges: []*storepb.Range{base.NewRange(statement, node.Text())},
- },
- false,
- )
+type changedResourcesListener struct {
+ *parser.BasePostgreSQLParserListener
+
+ database string
+ searchPath []string
+ changedResources *model.ChangedResources
+ databaseMetadata *model.DatabaseMetadata
+ statement string
+ tokenStream antlr.TokenStream
+
+ // DML statistics
+ dmlCount int
+ insertCount int
+ sampleDMLs []string
+}
+
+// EnterVariablesetstmt handles SET search_path statements
+func (l *changedResourcesListener) EnterVariablesetstmt(ctx *parser.VariablesetstmtContext) {
+ setRest := ctx.Set_rest()
+ if setRest == nil {
+ return
+ }
+ setRestMore := setRest.Set_rest_more()
+ if setRestMore == nil {
+ return
+ }
+ genericSet := setRestMore.Generic_set()
+ if genericSet == nil {
+ return
+ }
+ varName := genericSet.Var_name()
+ if varName == nil {
+ return
+ }
+ if len(varName.AllColid()) != 1 {
+ return
+ }
+ colid := varName.Colid(0)
+ if colid == nil {
+ return
+ }
+ name := colid.GetText()
+ if !strings.EqualFold(name, "search_path") {
+ return
+ }
+
+ // Extract search path values
+ varList := genericSet.Var_list()
+ if varList == nil {
+ return
+ }
+ values := varList.AllVar_value()
+ if len(values) == 0 {
+ return
+ }
+
+ var newSearchPath []string
+ for _, value := range values {
+ text := value.GetText()
+ // Remove quotes if present
+ text = strings.Trim(text, "'\"")
+ if text != "" {
+ newSearchPath = append(newSearchPath, text)
}
- case *ast.DropTableStmt:
- for _, table := range node.TableList {
- if table.Type == ast.TableTypeView {
- d, s, v := table.Database, table.Schema, table.Name
- if d == "" {
- d = database
- }
- if s == "" {
- schemaName, _ := databaseMetadata.SearchObject(searchPath, v)
- if schemaName == "" {
- s = searchPath[0] // default schema for PostgreSQL
- } else {
- s = schemaName
- }
- }
- changedResources.AddView(
- d,
- s,
- &storepb.ChangedResourceView{
- Name: v,
- Ranges: []*storepb.Range{base.NewRange(statement, node.Text())},
- },
- )
+ }
+
+ if len(newSearchPath) > 0 {
+ l.searchPath = newSearchPath
+ }
+}
+
+// EnterCreatestmt handles CREATE TABLE statements
+func (l *changedResourcesListener) EnterCreatestmt(ctx *parser.CreatestmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ // Get qualified name
+ qualifiedName := ctx.Qualified_name(0)
+ if qualifiedName == nil {
+ return
+ }
+
+ db, schema, table := l.extractQualifiedName(qualifiedName)
+ if db == "" {
+ db = l.database
+ }
+ if schema == "" {
+ schema = l.searchPath[0]
+ }
+
+ l.changedResources.AddTable(
+ db,
+ schema,
+ &storepb.ChangedResourceTable{
+ Name: table,
+ Ranges: []*storepb.Range{l.getRange(ctx)},
+ },
+ false,
+ )
+}
+
+// EnterDropstmt handles DROP TABLE/VIEW/INDEX statements
+func (l *changedResourcesListener) EnterDropstmt(ctx *parser.DropstmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ // Check object type
+ objectTypeAnyName := ctx.Object_type_any_name()
+ if objectTypeAnyName == nil {
+ return
+ }
+
+ // Handle DROP INDEX separately
+ if objectTypeAnyName.INDEX() != nil {
+ l.handleDropIndex(ctx)
+ return
+ }
+
+ // Handle DROP TABLE or DROP VIEW
+ if objectTypeAnyName.TABLE() == nil && objectTypeAnyName.VIEW() == nil {
+ return
+ }
+
+ // Get the list of tables/views to drop
+ anyNameList := ctx.Any_name_list()
+ if anyNameList == nil {
+ return
+ }
+
+ isView := objectTypeAnyName.VIEW() != nil
+
+ for _, anyName := range anyNameList.AllAny_name() {
+ db, schema, name := l.extractAnyName(anyName)
+ if db == "" {
+ db = l.database
+ }
+ if schema == "" {
+ schemaName, _ := l.databaseMetadata.SearchObject(l.searchPath, name)
+ if schemaName == "" {
+ schema = l.searchPath[0]
} else {
- d, s, table := table.Database, table.Schema, table.Name
- if d == "" {
- d = database
- }
- if s == "" {
- schemaName, _ := databaseMetadata.SearchObject(searchPath, table)
- if schemaName == "" {
- s = searchPath[0] // default schema for PostgreSQL
- } else {
- s = schemaName
- }
- }
- changedResources.AddTable(
- d,
- s,
- &storepb.ChangedResourceTable{
- Name: table,
- Ranges: []*storepb.Range{base.NewRange(statement, node.Text())},
- },
- true,
- )
+ schema = schemaName
}
}
- case *ast.AlterTableStmt:
- if node.Table.Type == ast.TableTypeBaseTable {
- d, s, table := node.Table.Database, node.Table.Schema, node.Table.Name
- if d == "" {
- d = database
- }
- if s == "" {
- schemaName, _ := databaseMetadata.SearchObject(searchPath, table)
- if schemaName == "" {
- s = searchPath[0] // default schema for PostgreSQL
- } else {
- s = schemaName
- }
- }
- changedResources.AddTable(
- d,
- s,
+
+ if isView {
+ // For views, add to views
+ l.changedResources.AddView(
+ db,
+ schema,
+ &storepb.ChangedResourceView{
+ Name: name,
+ Ranges: []*storepb.Range{l.getRange(ctx)},
+ },
+ )
+ } else {
+ // For tables
+ l.changedResources.AddTable(
+ db,
+ schema,
&storepb.ChangedResourceTable{
- Name: table,
- Ranges: []*storepb.Range{base.NewRange(statement, node.Text())},
+ Name: name,
+ Ranges: []*storepb.Range{l.getRange(ctx)},
},
true,
)
+ }
+ }
+}
+
+// EnterAltertablestmt handles ALTER TABLE statements
+func (l *changedResourcesListener) EnterAltertablestmt(ctx *parser.AltertablestmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ // Skip if this is ALTER VIEW
+ if ctx.VIEW() != nil {
+ return
+ }
+
+ // Get table name
+ relationExpr := ctx.Relation_expr()
+ if relationExpr == nil {
+ return
+ }
+
+ db, schema, table := l.extractRelationExpr(relationExpr)
+ if db == "" {
+ db = l.database
+ }
+ if schema == "" {
+ schemaName, _ := l.databaseMetadata.SearchObject(l.searchPath, table)
+ if schemaName == "" {
+ schema = l.searchPath[0]
+ } else {
+ schema = schemaName
+ }
+ }
+
+ l.changedResources.AddTable(
+ db,
+ schema,
+ &storepb.ChangedResourceTable{
+ Name: table,
+ Ranges: []*storepb.Range{l.getRange(ctx)},
+ },
+ true,
+ )
+}
+
+// EnterRenamestmt handles ALTER TABLE...RENAME TO statements
+func (l *changedResourcesListener) EnterRenamestmt(ctx *parser.RenamestmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ // Check if this is a table rename (ALTER TABLE t1 RENAME TO t2)
+ relationExpr := ctx.Relation_expr()
+ if relationExpr == nil {
+ return
+ }
+
+ // Get the new table name
+ allNames := ctx.AllName()
+ if len(allNames) == 0 {
+ return
+ }
+ newTableName := allNames[0].GetText()
+
+ // Extract old table name from relation_expr
+ db, schema, oldTableName := l.extractRelationExpr(relationExpr)
+ if db == "" {
+ db = l.database
+ }
+ if schema == "" {
+ schemaName, _ := l.databaseMetadata.SearchObject(l.searchPath, oldTableName)
+ if schemaName == "" {
+ schema = l.searchPath[0]
+ } else {
+ schema = schemaName
+ }
+ }
+
+ // Add the old table name (being renamed from)
+ l.changedResources.AddTable(
+ db,
+ schema,
+ &storepb.ChangedResourceTable{
+ Name: oldTableName,
+ Ranges: []*storepb.Range{l.getRange(ctx)},
+ },
+ true,
+ )
+
+ // Add the new table name (being renamed to)
+ l.changedResources.AddTable(
+ db,
+ schema,
+ &storepb.ChangedResourceTable{
+ Name: newTableName,
+ Ranges: []*storepb.Range{l.getRange(ctx)},
+ },
+ false,
+ )
+}
+
+// EnterIndexstmt handles CREATE INDEX statements
+func (l *changedResourcesListener) EnterIndexstmt(ctx *parser.IndexstmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ // Get table name
+ relationExpr := ctx.Relation_expr()
+ if relationExpr == nil {
+ return
+ }
+
+ db, schema, table := l.extractRelationExpr(relationExpr)
+ if db == "" {
+ db = l.database
+ }
+ if schema == "" {
+ schemaName, _ := l.databaseMetadata.SearchObject(l.searchPath, table)
+ if schemaName == "" {
+ schema = l.searchPath[0]
+ } else {
+ schema = schemaName
+ }
+ }
+
+ l.changedResources.AddTable(
+ db,
+ schema,
+ &storepb.ChangedResourceTable{
+ Name: table,
+ Ranges: []*storepb.Range{l.getRange(ctx)},
+ },
+ false,
+ )
+}
+
+// handleDropIndex handles DROP INDEX statements
+func (l *changedResourcesListener) handleDropIndex(ctx *parser.DropstmtContext) {
+ // Get index names
+ anyNameList := ctx.Any_name_list()
+ if anyNameList == nil {
+ return
+ }
+
+ for _, anyName := range anyNameList.AllAny_name() {
+ db, schema, indexName := l.extractAnyName(anyName)
+ if db == "" {
+ db = l.database
+ }
- for _, item := range node.AlterItemList {
- if v, ok := item.(*ast.RenameTableStmt); ok {
- d, s, table := node.Table.Database, node.Table.Schema, v.NewName
- if d == "" {
- d = database
- }
- if s == "" {
- schemaName, _ := databaseMetadata.SearchObject(searchPath, table)
- if schemaName == "" {
- s = searchPath[0] // default schema for PostgreSQL
- } else {
- s = schemaName
- }
- }
- changedResources.AddTable(
- d,
- s,
+ // If schema is specified, try to find the table
+ if schema != "" {
+ // Search for the index in metadata to find its table
+ schemaName, indexMetadata := l.databaseMetadata.SearchIndex([]string{schema}, indexName)
+ if indexMetadata != nil && schemaName != "" {
+ tableProto := indexMetadata.GetTableProto()
+ if tableProto != nil {
+ l.changedResources.AddTable(
+ db,
+ schemaName,
&storepb.ChangedResourceTable{
- Name: table,
- Ranges: []*storepb.Range{base.NewRange(statement, node.Text())},
+ Name: tableProto.GetName(),
+ Ranges: []*storepb.Range{l.getRange(ctx)},
},
false,
)
- // Only one rename table statement is allowed in a single alter table statement.
- break
}
}
- }
- case *ast.CreateIndexStmt:
- d, s, table := node.Index.Table.Database, node.Index.Table.Schema, node.Index.Table.Name
- if d == "" {
- d = database
- }
- if s == "" {
- schemaName, _ := databaseMetadata.SearchObject(searchPath, table)
- if schemaName == "" {
- s = searchPath[0] // default schema for PostgreSQL
- } else {
- s = schemaName
- }
- }
- changedResources.AddTable(
- d,
- s,
- &storepb.ChangedResourceTable{
- Name: table,
- Ranges: []*storepb.Range{base.NewRange(statement, node.Text())},
- },
- false,
- )
- case *ast.DropIndexStmt:
- for _, index := range node.IndexList {
- if index.Table != nil {
- d, s, table := index.Table.Database, index.Table.Schema, index.Table.Name
- if d == "" {
- d = database
- }
- if s == "" {
- schemaName, _ := databaseMetadata.SearchObject(searchPath, table)
- if schemaName == "" {
- s = searchPath[0] // default schema for PostgreSQL
- } else {
- s = schemaName
- }
- }
- changedResources.AddTable(
- d,
- s,
- &storepb.ChangedResourceTable{
- Name: table,
- Ranges: []*storepb.Range{base.NewRange(statement, node.Text())},
- },
- false,
- )
- } else {
- schema, indexMetadata := databaseMetadata.SearchIndex(searchPath, index.Name)
- if indexMetadata == nil {
- continue
- }
- tableMetadata := indexMetadata.GetTableProto()
- if tableMetadata == nil {
- continue
+ } else {
+ // Search in search_path
+ schemaName, indexMetadata := l.databaseMetadata.SearchIndex(l.searchPath, indexName)
+ if indexMetadata != nil && schemaName != "" {
+ tableProto := indexMetadata.GetTableProto()
+ if tableProto != nil {
+ l.changedResources.AddTable(
+ db,
+ schemaName,
+ &storepb.ChangedResourceTable{
+ Name: tableProto.GetName(),
+ Ranges: []*storepb.Range{l.getRange(ctx)},
+ },
+ false,
+ )
}
- changedResources.AddTable(
- database,
- schema,
- &storepb.ChangedResourceTable{
- Name: tableMetadata.GetName(),
- Ranges: []*storepb.Range{base.NewRange(statement, node.Text())},
- },
- false,
- )
}
}
- case *ast.CreateViewStmt:
- d, s, view := node.Name.Database, node.Name.Schema, node.Name.Name
- if d == "" {
- d = database
+ }
+}
+
+// EnterViewstmt handles CREATE VIEW statements
+func (l *changedResourcesListener) EnterViewstmt(ctx *parser.ViewstmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ // Get view name
+ qualifiedName := ctx.Qualified_name()
+ if qualifiedName == nil {
+ return
+ }
+
+ db, schema, view := l.extractQualifiedName(qualifiedName)
+ if db == "" {
+ db = l.database
+ }
+ if schema == "" {
+ schema = l.searchPath[0]
+ }
+
+ l.changedResources.AddView(
+ db,
+ schema,
+ &storepb.ChangedResourceView{
+ Name: view,
+ Ranges: []*storepb.Range{l.getRange(ctx)},
+ },
+ )
+}
+
+// EnterCreatefunctionstmt handles CREATE FUNCTION statements
+func (l *changedResourcesListener) EnterCreatefunctionstmt(ctx *parser.CreatefunctionstmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ // Get function name
+ funcName := ctx.Func_name()
+ if funcName == nil {
+ return
+ }
+
+ db, schema, function := l.extractFuncName(funcName)
+ if db == "" {
+ db = l.database
+ }
+ if schema == "" {
+ schema = l.searchPath[0]
+ }
+
+ l.changedResources.AddFunction(
+ db,
+ schema,
+ &storepb.ChangedResourceFunction{
+ Name: function,
+ Ranges: []*storepb.Range{l.getRange(ctx)},
+ },
+ )
+}
+
+// EnterRemovefuncstmt handles DROP FUNCTION statements
+func (l *changedResourcesListener) EnterRemovefuncstmt(ctx *parser.RemovefuncstmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
+ }
+
+ // Get function names
+ funcTableList := ctx.Function_with_argtypes_list()
+ if funcTableList == nil {
+ return
+ }
+
+ for _, funcWithArgs := range funcTableList.AllFunction_with_argtypes() {
+ funcName := funcWithArgs.Func_name()
+ if funcName == nil {
+ continue
}
- if s == "" {
- s = searchPath[0] // default schema for PostgreSQL
+
+ db, schema, function := l.extractFuncName(funcName)
+ if db == "" {
+ db = l.database
}
- changedResources.AddView(
- d,
- s,
- &storepb.ChangedResourceView{
- Name: view,
- Ranges: []*storepb.Range{base.NewRange(statement, node.Text())},
- },
- )
- case *ast.CreateFunctionStmt:
- s, f := node.Function.Schema, node.Function.Name
- if s == "" {
- s = searchPath[0] // default schema for PostgreSQL
+ if schema == "" {
+ schema = l.searchPath[0]
}
- changedResources.AddFunction(
- database,
- s,
+
+ l.changedResources.AddFunction(
+ db,
+ schema,
&storepb.ChangedResourceFunction{
- Name: f,
- Ranges: []*storepb.Range{base.NewRange(statement, node.Text())},
- },
- )
- case *ast.DropFunctionStmt:
- for _, ref := range node.FunctionList {
- s, f := ref.Schema, ref.Name
- if s == "" {
- schemaName, _ := databaseMetadata.SearchObject(searchPath, f)
- if schemaName == "" {
- s = searchPath[0] // default schema for PostgreSQL
- } else {
- s = schemaName
- }
- }
- changedResources.AddFunction(
- database,
- s,
- &storepb.ChangedResourceFunction{
- Name: f,
- Ranges: []*storepb.Range{base.NewRange(statement, node.Text())},
- },
- )
- }
- case *ast.InsertStmt:
- s := node.Table.Schema
- if s == "" {
- schemaName, _ := databaseMetadata.SearchObject(searchPath, node.Table.Name)
- if schemaName == "" {
- s = searchPath[0] // default schema for PostgreSQL
- } else {
- s = schemaName
- }
- }
- changedResources.AddTable(
- database,
- s,
- &storepb.ChangedResourceTable{
- Name: node.Table.Name,
- Ranges: []*storepb.Range{base.NewRange(statement, node.Text())},
- },
- false, // no need to add all table rows as affected rows for DML
- )
- case *ast.UpdateStmt:
- s := node.Table.Schema
- if s == "" {
- schemaName, _ := databaseMetadata.SearchObject(searchPath, node.Table.Name)
- if schemaName == "" {
- s = searchPath[0] // default schema for PostgreSQL
- } else {
- s = schemaName
- }
- }
- changedResources.AddTable(
- database,
- s,
- &storepb.ChangedResourceTable{
- Name: node.Table.Name,
- Ranges: []*storepb.Range{base.NewRange(statement, node.Text())},
- },
- false, // no need to add all table rows as affected rows for DML
- )
- case *ast.DeleteStmt:
- s := node.Table.Schema
- if s == "" {
- schemaName, _ := databaseMetadata.SearchObject(searchPath, node.Table.Name)
- if schemaName == "" {
- s = searchPath[0] // default schema for PostgreSQL
- } else {
- s = schemaName
- }
- }
- changedResources.AddTable(
- database,
- s,
- &storepb.ChangedResourceTable{
- Name: node.Table.Name,
- Ranges: []*storepb.Range{base.NewRange(statement, node.Text())},
+ Name: function,
+ Ranges: []*storepb.Range{l.getRange(ctx)},
},
- false, // no need to add all table rows as affected rows for DML
)
}
}
-func getSearchPathFromSQL(statement string) ([]string, error) {
- parseResult, err := ParsePostgreSQL(statement)
- if err != nil {
- return nil, errors.Wrapf(err, "failed to parse statement %q", statement)
+// EnterInsertstmt handles INSERT statements
+func (l *changedResourcesListener) EnterInsertstmt(ctx *parser.InsertstmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
+ return
}
- if parseResult == nil {
- return nil, errors.Errorf("parse result is nil for statement %q", statement)
+ // Get table name
+ insertTarget := ctx.Insert_target()
+ if insertTarget == nil {
+ return
+ }
+ qualifiedName := insertTarget.Qualified_name()
+ if qualifiedName == nil {
+ return
}
- visitor := &searchPathVisitor{}
- antlr.ParseTreeWalkerDefault.Walk(visitor, parseResult.Tree)
- return visitor.searchPath, nil
-}
+ db, schema, table := l.extractQualifiedName(qualifiedName)
+ if db == "" {
+ db = l.database
+ }
+ if schema == "" {
+ schemaName, _ := l.databaseMetadata.SearchObject(l.searchPath, table)
+ if schemaName == "" {
+ schema = l.searchPath[0]
+ } else {
+ schema = schemaName
+ }
+ }
-type searchPathVisitor struct {
- *parser.BasePostgreSQLParserListener
- searchPath []string
+ l.changedResources.AddTable(
+ db,
+ schema,
+ &storepb.ChangedResourceTable{
+ Name: table,
+ Ranges: []*storepb.Range{l.getRange(ctx)},
+ },
+ false,
+ )
+
+ // Count insert rows
+ insertRest := ctx.Insert_rest()
+ if insertRest != nil {
+ selectStmt := insertRest.Selectstmt()
+ if selectStmt != nil {
+ // Try to count VALUES clauses
+ rowCount := l.countInsertRows(selectStmt)
+ if rowCount > 0 {
+ l.insertCount += rowCount
+ }
+ }
+ }
}
-func (v *searchPathVisitor) EnterVariablesetstmt(ctx *parser.VariablesetstmtContext) {
- setRest := ctx.Set_rest()
- if setRest == nil {
+// EnterUpdatestmt handles UPDATE statements
+func (l *changedResourcesListener) EnterUpdatestmt(ctx *parser.UpdatestmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
return
}
- setRestMore := setRest.Set_rest_more()
- if setRestMore == nil {
+
+ // Get table name
+ relationExprOptAlias := ctx.Relation_expr_opt_alias()
+ if relationExprOptAlias == nil {
return
}
- genericSet := setRestMore.Generic_set()
- if genericSet == nil {
+ relationExpr := relationExprOptAlias.Relation_expr()
+ if relationExpr == nil {
return
}
- varName := genericSet.Var_name()
- if varName == nil {
+
+ db, schema, table := l.extractRelationExpr(relationExpr)
+ if db == "" {
+ db = l.database
+ }
+ if schema == "" {
+ schemaName, _ := l.databaseMetadata.SearchObject(l.searchPath, table)
+ if schemaName == "" {
+ schema = l.searchPath[0]
+ } else {
+ schema = schemaName
+ }
+ }
+
+ l.changedResources.AddTable(
+ db,
+ schema,
+ &storepb.ChangedResourceTable{
+ Name: table,
+ Ranges: []*storepb.Range{l.getRange(ctx)},
+ },
+ false,
+ )
+
+ // Count as DML
+ l.dmlCount++
+ if len(l.sampleDMLs) < common.MaximumLintExplainSize {
+ // Use getStatementText to preserve whitespace and include semicolon
+ l.sampleDMLs = append(l.sampleDMLs, l.getStatementText(ctx))
+ }
+}
+
+// EnterDeletestmt handles DELETE statements
+func (l *changedResourcesListener) EnterDeletestmt(ctx *parser.DeletestmtContext) {
+ if !isTopLevel(ctx.GetParent()) {
return
}
- if len(varName.AllColid()) != 1 {
+
+ // Get table name
+ relationExprOptAlias := ctx.Relation_expr_opt_alias()
+ if relationExprOptAlias == nil {
return
}
- name := NormalizePostgreSQLColid(varName.Colid(0))
- if !strings.EqualFold(name, "search_path") {
+ relationExpr := relationExprOptAlias.Relation_expr()
+ if relationExpr == nil {
return
}
- var searchPath []string
- for _, value := range genericSet.Var_list().AllVar_value() {
- valueText := value.GetText()
- if strings.HasPrefix(valueText, "\"") && strings.HasSuffix(valueText, "\"") {
- // Remove the quotes from the schema name.
- valueText = strings.Trim(valueText, "\"")
- } else if strings.HasPrefix(valueText, "'") && strings.HasSuffix(valueText, "'") {
- // Remove the quotes from the schema name.
- valueText = strings.Trim(valueText, "'")
+
+ db, schema, table := l.extractRelationExpr(relationExpr)
+ if db == "" {
+ db = l.database
+ }
+ if schema == "" {
+ schemaName, _ := l.databaseMetadata.SearchObject(l.searchPath, table)
+ if schemaName == "" {
+ schema = l.searchPath[0]
} else {
- // For non-quoted schema names, we just return the lower string for PostgreSQL.
- valueText = strings.ToLower(valueText)
+ schema = schemaName
}
- path := strings.TrimSpace(valueText)
- if model.IsSystemPath(path) {
- continue
+ }
+
+ l.changedResources.AddTable(
+ db,
+ schema,
+ &storepb.ChangedResourceTable{
+ Name: table,
+ Ranges: []*storepb.Range{l.getRange(ctx)},
+ },
+ false,
+ )
+
+ // Count as DML
+ l.dmlCount++
+ if len(l.sampleDMLs) < common.MaximumLintExplainSize {
+ // Use getStatementText to preserve whitespace and include semicolon
+ l.sampleDMLs = append(l.sampleDMLs, l.getStatementText(ctx))
+ }
+}
+
+// Helper functions
+
+func (*changedResourcesListener) extractQualifiedName(ctx parser.IQualified_nameContext) (string, string, string) {
+ if ctx == nil {
+ return "", "", ""
+ }
+
+ colid := ctx.Colid()
+ if colid == nil {
+ return "", "", ""
+ }
+
+ name := colid.GetText()
+
+ // Check for indirection (schema.table or db.schema.table)
+ indirection := ctx.Indirection()
+ if indirection == nil {
+ return "", "", name
+ }
+
+ indirectionEls := indirection.AllIndirection_el()
+ if len(indirectionEls) == 0 {
+ return "", "", name
+ }
+
+ // If there's one indirection, it's schema.table
+ if len(indirectionEls) == 1 {
+ attrName := indirectionEls[0].Attr_name()
+ if attrName != nil {
+ return "", name, attrName.GetText()
+ }
+ }
+
+ // If there are two indirections, it's db.schema.table
+ if len(indirectionEls) == 2 {
+ schema := indirectionEls[0].Attr_name()
+ table := indirectionEls[1].Attr_name()
+ if schema != nil && table != nil {
+ return name, schema.GetText(), table.GetText()
+ }
+ }
+
+ return "", "", name
+}
+
+func (*changedResourcesListener) extractAnyName(ctx parser.IAny_nameContext) (string, string, string) {
+ if ctx == nil {
+ return "", "", ""
+ }
+
+ // Collect all parts: first from Colid(), then from Attrs()
+ var parts []string
+ if ctx.Colid() != nil {
+ parts = append(parts, NormalizePostgreSQLColid(ctx.Colid()))
+ }
+
+ if ctx.Attrs() != nil {
+ for _, attr := range ctx.Attrs().AllAttr_name() {
+ // Use GetText() and normalize it - PostgreSQL identifiers are case-insensitive by default
+ text := attr.GetText()
+ // Remove quotes if present
+ if (strings.HasPrefix(text, "\"") && strings.HasSuffix(text, "\"")) || (strings.HasPrefix(text, "'") && strings.HasSuffix(text, "'")) {
+ text = text[1 : len(text)-1]
+ } else {
+ text = strings.ToLower(text)
+ }
+ parts = append(parts, text)
+ }
+ }
+
+ if len(parts) == 0 {
+ return "", "", ""
+ }
+
+ // Simple name
+ if len(parts) == 1 {
+ return "", "", parts[0]
+ }
+
+ // schema.name
+ if len(parts) == 2 {
+ return "", parts[0], parts[1]
+ }
+
+ // db.schema.name
+ if len(parts) >= 3 {
+ return parts[0], parts[1], parts[2]
+ }
+
+ return "", "", ""
+}
+
+func (l *changedResourcesListener) extractRelationExpr(ctx parser.IRelation_exprContext) (string, string, string) {
+ if ctx == nil {
+ return "", "", ""
+ }
+
+ qualifiedName := ctx.Qualified_name()
+ if qualifiedName == nil {
+ return "", "", ""
+ }
+
+ return l.extractQualifiedName(qualifiedName)
+}
+
+func (*changedResourcesListener) extractFuncName(ctx parser.IFunc_nameContext) (string, string, string) {
+ if ctx == nil {
+ return "", "", ""
+ }
+
+ typeFunc := ctx.Type_function_name()
+ if typeFunc != nil {
+ // Simple function name
+ return "", "", typeFunc.GetText()
+ }
+
+ colid := ctx.Colid()
+ if colid == nil {
+ return "", "", ""
+ }
+
+ name := colid.GetText()
+
+ // Check for indirection
+ indirection := ctx.Indirection()
+ if indirection == nil {
+ return "", "", name
+ }
+
+ indirectionEls := indirection.AllIndirection_el()
+ if len(indirectionEls) == 0 {
+ return "", "", name
+ }
+
+ // schema.function
+ if len(indirectionEls) == 1 {
+ attrName := indirectionEls[0].Attr_name()
+ if attrName != nil {
+ return "", name, attrName.GetText()
+ }
+ }
+
+ // db.schema.function
+ if len(indirectionEls) >= 2 {
+ schema := indirectionEls[0].Attr_name()
+ function := indirectionEls[1].Attr_name()
+ if schema != nil && function != nil {
+ return name, schema.GetText(), function.GetText()
+ }
+ }
+
+ return "", "", name
+}
+
+func (l *changedResourcesListener) getRange(ctx antlr.ParserRuleContext) *storepb.Range {
+ // Use GetTextFromInterval to get the actual text with whitespace
+ start := ctx.GetStart().GetTokenIndex()
+ stop := ctx.GetStop().GetTokenIndex()
+
+ // Check if the next token is a semicolon and include it
+ tokens, ok := l.tokenStream.(*antlr.CommonTokenStream)
+ if ok {
+ nextToken := tokens.Get(stop + 1)
+ if nextToken != nil && nextToken.GetText() == ";" {
+ // Include the semicolon
+ text := l.tokenStream.GetTextFromInterval(antlr.NewInterval(start, stop+1))
+ return base.NewRange(l.statement, text)
}
- searchPath = append(searchPath, path)
}
- v.searchPath = searchPath
+
+ text := l.tokenStream.GetTextFromInterval(antlr.NewInterval(start, stop))
+ return base.NewRange(l.statement, text)
+}
+
+// getStatementText returns the text of a statement, including trailing semicolon if present
+func (l *changedResourcesListener) getStatementText(ctx antlr.ParserRuleContext) string {
+ start := ctx.GetStart().GetTokenIndex()
+ stop := ctx.GetStop().GetTokenIndex()
+
+ // Check if the next token is a semicolon
+ tokens, ok := l.tokenStream.(*antlr.CommonTokenStream)
+ if ok {
+ nextToken := tokens.Get(stop + 1)
+ if nextToken != nil && nextToken.GetText() == ";" {
+ // Include the semicolon
+ return l.tokenStream.GetTextFromInterval(antlr.NewInterval(start, stop+1))
+ }
+ }
+
+ // No semicolon or can't access token stream
+ return l.tokenStream.GetTextFromInterval(antlr.NewInterval(start, stop))
+}
+
+func (*changedResourcesListener) countInsertRows(ctx parser.ISelectstmtContext) int {
+ if ctx == nil {
+ return 0
+ }
+
+ // Count VALUES clauses
+ counter := &valuesRowCounter{count: 0}
+ antlr.ParseTreeWalkerDefault.Walk(counter, ctx)
+ return counter.count
+}
+
+// valuesRowCounter counts the number of rows in VALUES clauses
+type valuesRowCounter struct {
+ *parser.BasePostgreSQLParserListener
+ count int
+}
+
+func (v *valuesRowCounter) EnterValues_clause(ctx *parser.Values_clauseContext) {
+ // Count the number of expr_list (rows) in the VALUES clause
+ if ctx == nil {
+ return
+ }
+
+ // Each expr_list represents one row
+ exprLists := ctx.AllExpr_list()
+ v.count += len(exprLists)
}
diff --git a/backend/plugin/parser/pg/resource_change_antlr.go b/backend/plugin/parser/pg/resource_change_antlr.go
deleted file mode 100644
index 88f4aad531..0000000000
--- a/backend/plugin/parser/pg/resource_change_antlr.go
+++ /dev/null
@@ -1,818 +0,0 @@
-package pg
-
-import (
- "strings"
-
- "github.com/antlr4-go/antlr/v4"
- "github.com/pkg/errors"
-
- parser "github.com/bytebase/parser/postgresql"
-
- "github.com/bytebase/bytebase/backend/common"
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/parser/base"
- "github.com/bytebase/bytebase/backend/store/model"
-)
-
-func extractChangedResourcesANTLR(database string, _ string, dbSchema *model.DatabaseSchema, parseResult *ParseResult, statement string) (*base.ChangeSummary, error) {
- if parseResult == nil || parseResult.Tree == nil {
- return nil, errors.New("parse result or tree is nil")
- }
-
- changedResources := model.NewChangedResources(dbSchema)
- searchPath := dbSchema.GetDatabaseMetadata().GetSearchPath()
- if len(searchPath) == 0 {
- searchPath = []string{"public"} // default search path for PostgreSQL
- }
-
- listener := &changedResourcesListener{
- database: database,
- searchPath: searchPath,
- changedResources: changedResources,
- databaseMetadata: dbSchema.GetDatabaseMetadata(),
- statement: statement,
- dmlCount: 0,
- insertCount: 0,
- sampleDMLs: []string{},
- }
-
- antlr.ParseTreeWalkerDefault.Walk(listener, parseResult.Tree)
-
- return &base.ChangeSummary{
- ChangedResources: changedResources,
- DMLCount: listener.dmlCount,
- SampleDMLS: listener.sampleDMLs,
- InsertCount: listener.insertCount,
- }, nil
-}
-
-type changedResourcesListener struct {
- *parser.BasePostgreSQLParserListener
-
- database string
- searchPath []string
- changedResources *model.ChangedResources
- databaseMetadata *model.DatabaseMetadata
- statement string
-
- // DML statistics
- dmlCount int
- insertCount int
- sampleDMLs []string
-}
-
-// EnterVariablesetstmt handles SET search_path statements
-func (l *changedResourcesListener) EnterVariablesetstmt(ctx *parser.VariablesetstmtContext) {
- setRest := ctx.Set_rest()
- if setRest == nil {
- return
- }
- setRestMore := setRest.Set_rest_more()
- if setRestMore == nil {
- return
- }
- genericSet := setRestMore.Generic_set()
- if genericSet == nil {
- return
- }
- varName := genericSet.Var_name()
- if varName == nil {
- return
- }
- if len(varName.AllColid()) != 1 {
- return
- }
- colid := varName.Colid(0)
- if colid == nil {
- return
- }
- name := colid.GetText()
- if !strings.EqualFold(name, "search_path") {
- return
- }
-
- // Extract search path values
- varList := genericSet.Var_list()
- if varList == nil {
- return
- }
- values := varList.AllVar_value()
- if len(values) == 0 {
- return
- }
-
- var newSearchPath []string
- for _, value := range values {
- text := value.GetText()
- // Remove quotes if present
- text = strings.Trim(text, "'\"")
- if text != "" {
- newSearchPath = append(newSearchPath, text)
- }
- }
-
- if len(newSearchPath) > 0 {
- l.searchPath = newSearchPath
- }
-}
-
-// EnterCreatestmt handles CREATE TABLE statements
-func (l *changedResourcesListener) EnterCreatestmt(ctx *parser.CreatestmtContext) {
- if !isTopLevel(ctx.GetParent()) {
- return
- }
-
- // Get qualified name
- qualifiedName := ctx.Qualified_name(0)
- if qualifiedName == nil {
- return
- }
-
- db, schema, table := l.extractQualifiedName(qualifiedName)
- if db == "" {
- db = l.database
- }
- if schema == "" {
- schema = l.searchPath[0]
- }
-
- l.changedResources.AddTable(
- db,
- schema,
- &storepb.ChangedResourceTable{
- Name: table,
- Ranges: []*storepb.Range{l.getRange(ctx)},
- },
- false,
- )
-}
-
-// EnterDropstmt handles DROP TABLE/VIEW/INDEX statements
-func (l *changedResourcesListener) EnterDropstmt(ctx *parser.DropstmtContext) {
- if !isTopLevel(ctx.GetParent()) {
- return
- }
-
- // Check object type
- objectTypeAnyName := ctx.Object_type_any_name()
- if objectTypeAnyName == nil {
- return
- }
-
- // Handle DROP INDEX separately
- if objectTypeAnyName.INDEX() != nil {
- l.handleDropIndex(ctx)
- return
- }
-
- // Handle DROP TABLE or DROP VIEW
- if objectTypeAnyName.TABLE() == nil && objectTypeAnyName.VIEW() == nil {
- return
- }
-
- // Get the list of tables/views to drop
- anyNameList := ctx.Any_name_list()
- if anyNameList == nil {
- return
- }
-
- isView := objectTypeAnyName.VIEW() != nil
-
- for _, anyName := range anyNameList.AllAny_name() {
- db, schema, name := l.extractAnyName(anyName)
- if db == "" {
- db = l.database
- }
- if schema == "" {
- schemaName, _ := l.databaseMetadata.SearchObject(l.searchPath, name)
- if schemaName == "" {
- schema = l.searchPath[0]
- } else {
- schema = schemaName
- }
- }
-
- if isView {
- // For views, add to views
- l.changedResources.AddView(
- db,
- schema,
- &storepb.ChangedResourceView{
- Name: name,
- Ranges: []*storepb.Range{l.getRange(ctx)},
- },
- )
- } else {
- // For tables
- l.changedResources.AddTable(
- db,
- schema,
- &storepb.ChangedResourceTable{
- Name: name,
- Ranges: []*storepb.Range{l.getRange(ctx)},
- },
- true,
- )
- }
- }
-}
-
-// EnterAltertablestmt handles ALTER TABLE statements
-func (l *changedResourcesListener) EnterAltertablestmt(ctx *parser.AltertablestmtContext) {
- if !isTopLevel(ctx.GetParent()) {
- return
- }
-
- // Skip if this is ALTER VIEW
- if ctx.VIEW() != nil {
- return
- }
-
- // Get table name
- relationExpr := ctx.Relation_expr()
- if relationExpr == nil {
- return
- }
-
- db, schema, table := l.extractRelationExpr(relationExpr)
- if db == "" {
- db = l.database
- }
- if schema == "" {
- schemaName, _ := l.databaseMetadata.SearchObject(l.searchPath, table)
- if schemaName == "" {
- schema = l.searchPath[0]
- } else {
- schema = schemaName
- }
- }
-
- l.changedResources.AddTable(
- db,
- schema,
- &storepb.ChangedResourceTable{
- Name: table,
- Ranges: []*storepb.Range{l.getRange(ctx)},
- },
- true,
- )
-}
-
-// EnterIndexstmt handles CREATE INDEX statements
-func (l *changedResourcesListener) EnterIndexstmt(ctx *parser.IndexstmtContext) {
- if !isTopLevel(ctx.GetParent()) {
- return
- }
-
- // Get table name
- relationExpr := ctx.Relation_expr()
- if relationExpr == nil {
- return
- }
-
- db, schema, table := l.extractRelationExpr(relationExpr)
- if db == "" {
- db = l.database
- }
- if schema == "" {
- schemaName, _ := l.databaseMetadata.SearchObject(l.searchPath, table)
- if schemaName == "" {
- schema = l.searchPath[0]
- } else {
- schema = schemaName
- }
- }
-
- l.changedResources.AddTable(
- db,
- schema,
- &storepb.ChangedResourceTable{
- Name: table,
- Ranges: []*storepb.Range{l.getRange(ctx)},
- },
- false,
- )
-}
-
-// EnterDropstmt_INDEX handles DROP INDEX statements
-// This is handled in EnterDropstmt but we need special logic for indexes
-func (l *changedResourcesListener) handleDropIndex(ctx *parser.DropstmtContext) {
- // Get index names
- anyNameList := ctx.Any_name_list()
- if anyNameList == nil {
- return
- }
-
- for _, anyName := range anyNameList.AllAny_name() {
- db, schema, indexName := l.extractAnyName(anyName)
- if db == "" {
- db = l.database
- }
-
- // If schema is specified, try to find the table
- if schema != "" {
- // Search for the index in metadata to find its table
- schemaName, indexMetadata := l.databaseMetadata.SearchIndex([]string{schema}, indexName)
- if indexMetadata != nil && schemaName != "" {
- tableProto := indexMetadata.GetTableProto()
- if tableProto != nil {
- l.changedResources.AddTable(
- db,
- schemaName,
- &storepb.ChangedResourceTable{
- Name: tableProto.GetName(),
- Ranges: []*storepb.Range{l.getRange(ctx)},
- },
- false,
- )
- }
- }
- } else {
- // Search in search_path
- schemaName, indexMetadata := l.databaseMetadata.SearchIndex(l.searchPath, indexName)
- if indexMetadata != nil && schemaName != "" {
- tableProto := indexMetadata.GetTableProto()
- if tableProto != nil {
- l.changedResources.AddTable(
- db,
- schemaName,
- &storepb.ChangedResourceTable{
- Name: tableProto.GetName(),
- Ranges: []*storepb.Range{l.getRange(ctx)},
- },
- false,
- )
- }
- }
- }
- }
-}
-
-// EnterViewstmt handles CREATE VIEW statements
-func (l *changedResourcesListener) EnterViewstmt(ctx *parser.ViewstmtContext) {
- if !isTopLevel(ctx.GetParent()) {
- return
- }
-
- // Get view name
- qualifiedName := ctx.Qualified_name()
- if qualifiedName == nil {
- return
- }
-
- db, schema, view := l.extractQualifiedName(qualifiedName)
- if db == "" {
- db = l.database
- }
- if schema == "" {
- schema = l.searchPath[0]
- }
-
- l.changedResources.AddView(
- db,
- schema,
- &storepb.ChangedResourceView{
- Name: view,
- Ranges: []*storepb.Range{l.getRange(ctx)},
- },
- )
-}
-
-// EnterCreatefunctionstmt handles CREATE FUNCTION statements
-func (l *changedResourcesListener) EnterCreatefunctionstmt(ctx *parser.CreatefunctionstmtContext) {
- if !isTopLevel(ctx.GetParent()) {
- return
- }
-
- // Get function name
- funcName := ctx.Func_name()
- if funcName == nil {
- return
- }
-
- db, schema, function := l.extractFuncName(funcName)
- if db == "" {
- db = l.database
- }
- if schema == "" {
- schema = l.searchPath[0]
- }
-
- l.changedResources.AddFunction(
- db,
- schema,
- &storepb.ChangedResourceFunction{
- Name: function,
- Ranges: []*storepb.Range{l.getRange(ctx)},
- },
- )
-}
-
-// EnterRemovefuncstmt handles DROP FUNCTION statements
-func (l *changedResourcesListener) EnterRemovefuncstmt(ctx *parser.RemovefuncstmtContext) {
- if !isTopLevel(ctx.GetParent()) {
- return
- }
-
- // Get function names
- funcTableList := ctx.Function_with_argtypes_list()
- if funcTableList == nil {
- return
- }
-
- for _, funcWithArgs := range funcTableList.AllFunction_with_argtypes() {
- funcName := funcWithArgs.Func_name()
- if funcName == nil {
- continue
- }
-
- db, schema, function := l.extractFuncName(funcName)
- if db == "" {
- db = l.database
- }
- if schema == "" {
- schema = l.searchPath[0]
- }
-
- l.changedResources.AddFunction(
- db,
- schema,
- &storepb.ChangedResourceFunction{
- Name: function,
- Ranges: []*storepb.Range{l.getRange(ctx)},
- },
- )
- }
-}
-
-// EnterCommentstmt handles COMMENT ON TABLE statements
-func (l *changedResourcesListener) EnterCommentstmt(ctx *parser.CommentstmtContext) {
- if !isTopLevel(ctx.GetParent()) {
- return
- }
-
- // Check if this is COMMENT ON TABLE
- objectTypeAnyName := ctx.Object_type_any_name()
- if objectTypeAnyName == nil || objectTypeAnyName.TABLE() == nil {
- return
- }
-
- anyName := ctx.Any_name()
- if anyName == nil {
- return
- }
-
- db, schema, table := l.extractAnyName(anyName)
- if db == "" {
- db = l.database
- }
- if schema == "" {
- schemaName, _ := l.databaseMetadata.SearchObject(l.searchPath, table)
- if schemaName == "" {
- schema = l.searchPath[0]
- } else {
- schema = schemaName
- }
- }
-
- l.changedResources.AddTable(
- db,
- schema,
- &storepb.ChangedResourceTable{
- Name: table,
- Ranges: []*storepb.Range{l.getRange(ctx)},
- },
- true,
- )
-}
-
-// EnterInsertstmt handles INSERT statements
-func (l *changedResourcesListener) EnterInsertstmt(ctx *parser.InsertstmtContext) {
- if !isTopLevel(ctx.GetParent()) {
- return
- }
-
- // Get table name
- insertTarget := ctx.Insert_target()
- if insertTarget == nil {
- return
- }
- qualifiedName := insertTarget.Qualified_name()
- if qualifiedName == nil {
- return
- }
-
- db, schema, table := l.extractQualifiedName(qualifiedName)
- if db == "" {
- db = l.database
- }
- if schema == "" {
- schemaName, _ := l.databaseMetadata.SearchObject(l.searchPath, table)
- if schemaName == "" {
- schema = l.searchPath[0]
- } else {
- schema = schemaName
- }
- }
-
- l.changedResources.AddTable(
- db,
- schema,
- &storepb.ChangedResourceTable{
- Name: table,
- Ranges: []*storepb.Range{l.getRange(ctx)},
- },
- false,
- )
-
- // Count insert rows
- insertRest := ctx.Insert_rest()
- if insertRest != nil {
- selectStmt := insertRest.Selectstmt()
- if selectStmt != nil {
- // Try to count VALUES clauses
- rowCount := l.countInsertRows(selectStmt)
- if rowCount > 0 {
- l.insertCount += rowCount
- }
- }
- }
-}
-
-// EnterUpdatestmt handles UPDATE statements
-func (l *changedResourcesListener) EnterUpdatestmt(ctx *parser.UpdatestmtContext) {
- if !isTopLevel(ctx.GetParent()) {
- return
- }
-
- // Get table name
- relationExprOptAlias := ctx.Relation_expr_opt_alias()
- if relationExprOptAlias == nil {
- return
- }
- relationExpr := relationExprOptAlias.Relation_expr()
- if relationExpr == nil {
- return
- }
-
- db, schema, table := l.extractRelationExpr(relationExpr)
- if db == "" {
- db = l.database
- }
- if schema == "" {
- schemaName, _ := l.databaseMetadata.SearchObject(l.searchPath, table)
- if schemaName == "" {
- schema = l.searchPath[0]
- } else {
- schema = schemaName
- }
- }
-
- l.changedResources.AddTable(
- db,
- schema,
- &storepb.ChangedResourceTable{
- Name: table,
- Ranges: []*storepb.Range{l.getRange(ctx)},
- },
- false,
- )
-
- // Count as DML
- l.dmlCount++
- if len(l.sampleDMLs) < common.MaximumLintExplainSize {
- l.sampleDMLs = append(l.sampleDMLs, ctx.GetText())
- }
-}
-
-// EnterDeletestmt handles DELETE statements
-func (l *changedResourcesListener) EnterDeletestmt(ctx *parser.DeletestmtContext) {
- if !isTopLevel(ctx.GetParent()) {
- return
- }
-
- // Get table name
- relationExprOptAlias := ctx.Relation_expr_opt_alias()
- if relationExprOptAlias == nil {
- return
- }
- relationExpr := relationExprOptAlias.Relation_expr()
- if relationExpr == nil {
- return
- }
-
- db, schema, table := l.extractRelationExpr(relationExpr)
- if db == "" {
- db = l.database
- }
- if schema == "" {
- schemaName, _ := l.databaseMetadata.SearchObject(l.searchPath, table)
- if schemaName == "" {
- schema = l.searchPath[0]
- } else {
- schema = schemaName
- }
- }
-
- l.changedResources.AddTable(
- db,
- schema,
- &storepb.ChangedResourceTable{
- Name: table,
- Ranges: []*storepb.Range{l.getRange(ctx)},
- },
- false,
- )
-
- // Count as DML
- l.dmlCount++
- if len(l.sampleDMLs) < common.MaximumLintExplainSize {
- l.sampleDMLs = append(l.sampleDMLs, ctx.GetText())
- }
-}
-
-// Helper functions
-
-func (*changedResourcesListener) extractQualifiedName(ctx parser.IQualified_nameContext) (string, string, string) {
- if ctx == nil {
- return "", "", ""
- }
-
- colid := ctx.Colid()
- if colid == nil {
- return "", "", ""
- }
-
- name := colid.GetText()
-
- // Check for indirection (schema.table or db.schema.table)
- indirection := ctx.Indirection()
- if indirection == nil {
- return "", "", name
- }
-
- indirectionEls := indirection.AllIndirection_el()
- if len(indirectionEls) == 0 {
- return "", "", name
- }
-
- // If there's one indirection, it's schema.table
- if len(indirectionEls) == 1 {
- attrName := indirectionEls[0].Attr_name()
- if attrName != nil {
- return "", name, attrName.GetText()
- }
- }
-
- // If there are two indirections, it's db.schema.table
- if len(indirectionEls) == 2 {
- schema := indirectionEls[0].Attr_name()
- table := indirectionEls[1].Attr_name()
- if schema != nil && table != nil {
- return name, schema.GetText(), table.GetText()
- }
- }
-
- return "", "", name
-}
-
-func (*changedResourcesListener) extractAnyName(ctx parser.IAny_nameContext) (string, string, string) {
- if ctx == nil {
- return "", "", ""
- }
-
- // Collect all parts: first from Colid(), then from Attrs()
- var parts []string
- if ctx.Colid() != nil {
- parts = append(parts, NormalizePostgreSQLColid(ctx.Colid()))
- }
-
- if ctx.Attrs() != nil {
- for _, attr := range ctx.Attrs().AllAttr_name() {
- // Use GetText() and normalize it - PostgreSQL identifiers are case-insensitive by default
- text := attr.GetText()
- // Remove quotes if present
- if (strings.HasPrefix(text, "\"") && strings.HasSuffix(text, "\"")) || (strings.HasPrefix(text, "'") && strings.HasSuffix(text, "'")) {
- text = text[1 : len(text)-1]
- } else {
- text = strings.ToLower(text)
- }
- parts = append(parts, text)
- }
- }
-
- if len(parts) == 0 {
- return "", "", ""
- }
-
- // Simple name
- if len(parts) == 1 {
- return "", "", parts[0]
- }
-
- // schema.name
- if len(parts) == 2 {
- return "", parts[0], parts[1]
- }
-
- // db.schema.name
- if len(parts) >= 3 {
- return parts[0], parts[1], parts[2]
- }
-
- return "", "", ""
-}
-
-func (l *changedResourcesListener) extractRelationExpr(ctx parser.IRelation_exprContext) (string, string, string) {
- if ctx == nil {
- return "", "", ""
- }
-
- qualifiedName := ctx.Qualified_name()
- if qualifiedName == nil {
- return "", "", ""
- }
-
- return l.extractQualifiedName(qualifiedName)
-}
-
-func (*changedResourcesListener) extractFuncName(ctx parser.IFunc_nameContext) (string, string, string) {
- if ctx == nil {
- return "", "", ""
- }
-
- typeFunc := ctx.Type_function_name()
- if typeFunc != nil {
- // Simple function name
- return "", "", typeFunc.GetText()
- }
-
- colid := ctx.Colid()
- if colid == nil {
- return "", "", ""
- }
-
- name := colid.GetText()
-
- // Check for indirection
- indirection := ctx.Indirection()
- if indirection == nil {
- return "", "", name
- }
-
- indirectionEls := indirection.AllIndirection_el()
- if len(indirectionEls) == 0 {
- return "", "", name
- }
-
- // schema.function
- if len(indirectionEls) == 1 {
- attrName := indirectionEls[0].Attr_name()
- if attrName != nil {
- return "", name, attrName.GetText()
- }
- }
-
- // db.schema.function
- if len(indirectionEls) >= 2 {
- schema := indirectionEls[0].Attr_name()
- function := indirectionEls[1].Attr_name()
- if schema != nil && function != nil {
- return name, schema.GetText(), function.GetText()
- }
- }
-
- return "", "", name
-}
-
-func (l *changedResourcesListener) getRange(ctx antlr.ParserRuleContext) *storepb.Range {
- return base.NewRange(l.statement, ctx.GetText())
-}
-
-func (*changedResourcesListener) countInsertRows(ctx parser.ISelectstmtContext) int {
- if ctx == nil {
- return 0
- }
-
- // Count VALUES clauses
- counter := &valuesRowCounter{count: 0}
- antlr.ParseTreeWalkerDefault.Walk(counter, ctx)
- return counter.count
-}
-
-// valuesRowCounter counts the number of rows in VALUES clauses
-type valuesRowCounter struct {
- *parser.BasePostgreSQLParserListener
- count int
-}
-
-func (v *valuesRowCounter) EnterValues_clause(ctx *parser.Values_clauseContext) {
- // Count the number of expr_list (rows) in the VALUES clause
- if ctx == nil {
- return
- }
-
- // Each expr_list represents one row
- exprLists := ctx.AllExpr_list()
- v.count += len(exprLists)
-}
diff --git a/backend/plugin/parser/pg/resource_change_test.go b/backend/plugin/parser/pg/resource_change_test.go
index 0351f9425c..f3530c7b84 100644
--- a/backend/plugin/parser/pg/resource_change_test.go
+++ b/backend/plugin/parser/pg/resource_change_test.go
@@ -7,7 +7,7 @@ import (
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
- pgrawparser "github.com/bytebase/bytebase/backend/plugin/parser/pg/legacy"
+
"github.com/bytebase/bytebase/backend/store/model"
)
@@ -65,9 +65,9 @@ func TestExtractChangedResources(t *testing.T) {
InsertCount: 2,
}
- nodes, err := pgrawparser.Parse(pgrawparser.ParseContext{}, statement)
+ parseResult, err := ParsePostgreSQL(statement)
require.NoError(t, err)
- got, err := extractChangedResources("db", "public", dbSchema, nodes, statement)
+ got, err := extractChangedResources("db", "public", dbSchema, parseResult, statement)
require.NoError(t, err)
require.Equal(t, want, got)
}