Skip to content

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

chore(pg parser): split multi-sql and set text for the root node (#1808)

rebeliceac7d45a2c8d2
diff --git a/plugin/parser/engine/pg/convert.go b/plugin/parser/engine/pg/convert.go
index 882f77c7dd..beab069a9a 100644
--- a/plugin/parser/engine/pg/convert.go
+++ b/plugin/parser/engine/pg/convert.go
@@ -10,7 +10,12 @@ import (
)
// convert converts the pg_query.Node to ast.Node.
-func convert(node *pgquery.Node) (ast.Node, error) {
+func convert(node *pgquery.Node, text string) (res ast.Node, err error) {
+ defer func() {
+ if res != nil {
+ res.SetText(text)
+ }
+ }()
switch in := node.Node.(type) {
case *pgquery.Node_AlterTableStmt:
alterTable := &ast.AlterTableStmt{
diff --git a/plugin/parser/engine/pg/parser.go b/plugin/parser/engine/pg/parser.go
index 369c03a982..634c92bb9d 100644
--- a/plugin/parser/engine/pg/parser.go
+++ b/plugin/parser/engine/pg/parser.go
@@ -4,6 +4,8 @@
package pg
import (
+ "fmt"
+
"github.com/bytebase/bytebase/plugin/parser"
"github.com/bytebase/bytebase/plugin/parser/ast"
pgquery "github.com/pganalyze/pg_query_go/v2"
@@ -28,10 +30,18 @@ func (p *PostgreSQLParser) Parse(ctx parser.Context, statement string) ([]ast.No
return nil, err
}
+ textList, err := parser.SplitMultiSQL(parser.Postgres, statement)
+ if err != nil {
+ return nil, err
+ }
+ if len(res.Stmts) != len(textList) {
+ return nil, fmt.Errorf("split multi-SQL failed: the length should be %d, but get %d. stmt: \"%s\"", len(res.Stmts), len(textList), statement)
+ }
+
var nodeList []ast.Node
- for _, stmt := range res.Stmts {
- node, err := convert(stmt.Stmt)
+ for i, stmt := range res.Stmts {
+ node, err := convert(stmt.Stmt, textList[i])
if err != nil {
return nil, err
}