Skip to content

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

refactor: move utils

d-bytebased3d10f23e022
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.go
index d019ca2270..340083565b 100644
--- a/backend/api/v1/sql_service.go
+++ b/backend/api/v1/sql_service.go
@@ -1997,7 +1997,7 @@ func validateQueryRequest(instance *store.InstanceMessage, databaseName string,
return nil
}
- if !parser.ValidateSQLForEditor(instance.Engine, statement) {
+ if !base.ValidateSQLForEditor(instance.Engine, statement) {
return nonSelectSQLError.Err()
}
return nil
diff --git a/backend/plugin/db/util/driverutil.go b/backend/plugin/db/util/driverutil.go
index 203143ff7d..0063b5e928 100644
--- a/backend/plugin/db/util/driverutil.go
+++ b/backend/plugin/db/util/driverutil.go
@@ -21,6 +21,7 @@ import (
"github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/common/log"
"github.com/bytebase/bytebase/backend/plugin/db"
+ "github.com/bytebase/bytebase/backend/plugin/parser/base"
mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
parser "github.com/bytebase/bytebase/backend/plugin/parser/sql"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
@@ -143,7 +144,7 @@ func Query(ctx context.Context, dbType storepb.Engine, conn *sql.Conn, statement
if dbType == storepb.Engine_REDSHIFT && queryContext.ShareDB {
statement = strings.ReplaceAll(statement, fmt.Sprintf("%s.", queryContext.CurrentDatabase), "")
}
- fieldList, err := parser.ExtractSensitiveField(dbType, statement, queryContext.CurrentDatabase, queryContext.SensitiveSchemaInfo)
+ fieldList, err := base.ExtractSensitiveField(dbType, statement, queryContext.CurrentDatabase, queryContext.SensitiveSchemaInfo)
if err != nil {
return nil, errors.Wrapf(err, "failed to extract sensitive fields: %q", statement)
}
diff --git a/backend/plugin/parser/base/interface.go b/backend/plugin/parser/base/interface.go
index 6e43b576be..81294b68c5 100644
--- a/backend/plugin/parser/base/interface.go
+++ b/backend/plugin/parser/base/interface.go
@@ -10,8 +10,8 @@ import (
var (
mux sync.Mutex
- QueryValidators = make(map[storepb.Engine]ValidateSQLForEditorFunc)
- FieldMaskers = make(map[storepb.Engine]GetMaskedFieldsFunc)
+ queryValidators = make(map[storepb.Engine]ValidateSQLForEditorFunc)
+ fieldMaskers = make(map[storepb.Engine]GetMaskedFieldsFunc)
)
type ValidateSQLForEditorFunc func(string) bool
@@ -21,17 +21,36 @@ type GetMaskedFieldsFunc func(string, string, *db.SensitiveSchemaInfo) ([]db.Sen
func RegisterQueryValidator(engine storepb.Engine, f ValidateSQLForEditorFunc) {
mux.Lock()
defer mux.Unlock()
- if _, dup := QueryValidators[engine]; dup {
+ if _, dup := queryValidators[engine]; dup {
panic(fmt.Sprintf("Register called twice %s", engine))
}
- QueryValidators[engine] = f
+ queryValidators[engine] = f
}
func RegisterGetMaskedFieldsFunc(engine storepb.Engine, f GetMaskedFieldsFunc) {
mux.Lock()
defer mux.Unlock()
- if _, dup := FieldMaskers[engine]; dup {
+ if _, dup := fieldMaskers[engine]; dup {
panic(fmt.Sprintf("Register called twice %s", engine))
}
- FieldMaskers[engine] = f
+ fieldMaskers[engine] = f
+}
+
+func ExtractSensitiveField(engine storepb.Engine, statement string, currentDatabase string, schemaInfo *db.SensitiveSchemaInfo) ([]db.SensitiveField, error) {
+ if schemaInfo == nil {
+ return nil, nil
+ }
+
+ f := fieldMaskers[engine]
+ return f(statement, currentDatabase, schemaInfo)
+}
+
+// ValidateSQLForEditor validates the SQL statement for editor.
+// We support the following SQLs:
+// 1. EXPLAIN statement, except EXPLAIN ANALYZE
+// 2. SELECT statement
+// We also support CTE with SELECT statements, but not with DML statements.
+func ValidateSQLForEditor(engine storepb.Engine, statement string) bool {
+ f := queryValidators[engine]
+ return f(statement)
}
diff --git a/backend/plugin/parser/sql/utils.go b/backend/plugin/parser/sql/utils.go
index 9edc868bec..6485e7f3ff 100644
--- a/backend/plugin/parser/sql/utils.go
+++ b/backend/plugin/parser/sql/utils.go
@@ -5,7 +5,6 @@ import (
"github.com/pkg/errors"
- "github.com/bytebase/bytebase/backend/plugin/db"
"github.com/bytebase/bytebase/backend/plugin/parser/base"
mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"
pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"
@@ -108,22 +107,3 @@ func ExtractDatabaseList(engineType storepb.Engine, statement string, fallbackNo
return nil, errors.Errorf("engine type is not supported: %s", engineType)
}
}
-
-func ExtractSensitiveField(engine storepb.Engine, statement string, currentDatabase string, schemaInfo *db.SensitiveSchemaInfo) ([]db.SensitiveField, error) {
- if schemaInfo == nil {
- return nil, nil
- }
-
- f := base.FieldMaskers[engine]
- return f(statement, currentDatabase, schemaInfo)
-}
-
-// ValidateSQLForEditor validates the SQL statement for editor.
-// We support the following SQLs:
-// 1. EXPLAIN statement, except EXPLAIN ANALYZE
-// 2. SELECT statement
-// We also support CTE with SELECT statements, but not with DML statements.
-func ValidateSQLForEditor(engine storepb.Engine, statement string) bool {
- f := base.QueryValidators[engine]
- return f(statement)
-}