Skip to content

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

feat: implement parse mybatis mapper file api (#10796)

h3n4lccf1a673dea8
diff --git a/backend/api/auth/config.go b/backend/api/auth/config.go
index 6923cb613d..7f4521d2b8 100644
--- a/backend/api/auth/config.go
+++ b/backend/api/auth/config.go
@@ -16,6 +16,7 @@ var authenticationAllowlistMethods = map[string]bool{
v1pb.AuthService_Logout_FullMethodName: true,
v1pb.AuthService_CreateUser_FullMethodName: true,
v1pb.IdentityProviderService_ListIdentityProviders_FullMethodName: true,
+ v1pb.SQLService_ParseMyBatisMapper_FullMethodName: true,
}
// IsAuthenticationAllowed returns whether the method is exempted from authentication.
diff --git a/backend/api/gitops/webhook.go b/backend/api/gitops/webhook.go
index 4326cb2d1c..2f99bc9017 100644
--- a/backend/api/gitops/webhook.go
+++ b/backend/api/gitops/webhook.go
@@ -2482,7 +2482,7 @@ func extractMybatisMapperSQL(mapperContent string, engineType storepb.Engine) (s
var placeholder string
switch engineType {
case storepb.Engine_MYSQL:
- placeholder = "?"
+ placeholder = "@1"
case storepb.Engine_POSTGRES:
placeholder = "$1"
default:
diff --git a/backend/api/v1/acl_iam.go b/backend/api/v1/acl_iam.go
index 892b270352..6a364b4319 100644
--- a/backend/api/v1/acl_iam.go
+++ b/backend/api/v1/acl_iam.go
@@ -73,6 +73,7 @@ func isSkippedMethod(fullMethod string) bool {
v1pb.SQLService_Export_FullMethodName,
v1pb.SQLService_DifferPreview_FullMethodName,
v1pb.SQLService_Check_FullMethodName,
+ v1pb.SQLService_ParseMyBatisMapper_FullMethodName,
v1pb.SQLService_Pretty_FullMethodName,
v1pb.SQLService_StringifyMetadata_FullMethodName,
v1pb.SubscriptionService_GetSubscription_FullMethodName,
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.go
index 48556d1f83..dd851fb59c 100644
--- a/backend/api/v1/sql_service.go
+++ b/backend/api/v1/sql_service.go
@@ -27,6 +27,8 @@ import (
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/structpb"
+ mapperparser "github.com/bytebase/bytebase/backend/plugin/parser/mybatis/mapper"
+
"github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/common/log"
"github.com/bytebase/bytebase/backend/component/activity"
@@ -1024,6 +1026,40 @@ func (s *SQLService) Query(ctx context.Context, request *v1pb.QueryRequest) (*v1
return response, nil
}
+// ParseMyBatisMapper parses a MyBatis mapper XML file and returns the multi-SQL statements.
+func (*SQLService) ParseMyBatisMapper(_ context.Context, request *v1pb.ParseMyBatisMapperRequest) (*v1pb.ParseMyBatisMapperResponse, error) {
+ content := string(request.Content)
+
+ parser := mapperparser.NewParser(content)
+ node, err := parser.Parse()
+ if err != nil {
+ return nil, status.Errorf(codes.InvalidArgument, "failed to parse mybatis mapper: %v", err)
+ }
+
+ var stringsBuilder strings.Builder
+ if err := node.RestoreSQL(parser.NewRestoreContext().WithRestoreDataNodePlaceholder("@1"), &stringsBuilder); err != nil {
+ return nil, status.Errorf(codes.Internal, "failed to restore mybatis mapper: %v", err)
+ }
+
+ statement := stringsBuilder.String()
+ singleSQLs, err := base.SplitMultiSQL(storepb.Engine_POSTGRES, statement)
+ if err != nil {
+ return nil, status.Errorf(codes.InvalidArgument, "failed to split mybatis mapper: %v", err)
+ }
+
+ var results []string
+ for _, sql := range singleSQLs {
+ if sql.Empty {
+ continue
+ }
+ results = append(results, sql.Text)
+ }
+
+ return &v1pb.ParseMyBatisMapperResponse{
+ Statements: results,
+ }, nil
+}
+
// postQuery does the following:
// 1. Check index hit Explain statements
// 2. Update SQL query activity