Skip to content

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

clickhouse driver: use pingcap parser for getting statements.

spinningbot75dce032b2b6
diff --git a/plugin/db/clickhouse/clickhouse.go b/plugin/db/clickhouse/clickhouse.go
index 34db9fd3ea..1fdd475a4f 100644
--- a/plugin/db/clickhouse/clickhouse.go
+++ b/plugin/db/clickhouse/clickhouse.go
@@ -14,6 +14,7 @@ import (
"github.com/bytebase/bytebase/common"
"github.com/bytebase/bytebase/plugin/db"
"github.com/bytebase/bytebase/plugin/db/util"
+ "github.com/pingcap/parser"
"go.uber.org/zap"
)
@@ -366,7 +367,16 @@ func (driver *Driver) Execute(ctx context.Context, statement string) error {
}
func (driver *Driver) ExecuteStatement(ctx context.Context, tx *sql.Tx, statement string) error {
- stmts := db.SplitStatements(statement)
+ p := parser.New()
+ root, _, err := p.Parse(statement, "", "")
+ if err != nil {
+ return err
+ }
+ var stmts []string
+ for _, node := range root {
+ stmts = append(stmts, node.Text())
+ }
+
for _, stmt := range stmts {
if _, err := tx.ExecContext(ctx, stmt); err != nil {
return err
diff --git a/plugin/db/driver.go b/plugin/db/driver.go
index e0f4f7f3cb..61c2aa8bb7 100644
--- a/plugin/db/driver.go
+++ b/plugin/db/driver.go
@@ -421,17 +421,3 @@ func (p *QueryParams) QueryString() string {
}
return ""
}
-
-// SplitStatements will split the sql statements into multiple statement parts.
-// TODO(spinningbot): making this more robust.
-func SplitStatements(stmt string) []string {
- stmts := strings.Split(stmt, ";")
- var ret []string
- for _, stmt := range stmts {
- stmt = strings.Trim(stmt, " \n")
- if len(stmt) > 0 {
- ret = append(ret, stmt)
- }
- }
- return ret
-}