Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions sqle/api/controller/v1/sql_audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func DirectAudit(c echo.Context) error {

var task *model.Task
if instance != nil && schemaName != "" {
task, err = server.DirectAuditByInstance(l, sql, schemaName, instance)
task, err = server.DirectAuditByInstance(l, sql, schemaName, instance, "")
} else {
task, err = server.AuditSQLByDBType(l, sql, req.InstanceType, projectUid, "")
}
Expand Down Expand Up @@ -226,7 +226,7 @@ func DirectAuditFiles(c echo.Context) error {

var task *model.Task
if instance != nil && schemaName != "" {
task, err = server.DirectAuditByInstance(l, sqls, schemaName, instance)
task, err = server.DirectAuditByInstance(l, sqls, schemaName, instance, "")
} else {
task, err = server.AuditSQLByDBType(l, sqls, req.InstanceType, projectUid, "")
}
Expand Down
31 changes: 25 additions & 6 deletions sqle/api/controller/v2/sql_audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ import (
type DirectAuditReqV2 struct {
InstanceType string `json:"instance_type" form:"instance_type" example:"MySQL" valid:"required"`
// 调用方不应该关心SQL是否被完美的拆分成独立的条目, 拆分SQL由SQLE实现
SQLContent string `json:"sql_content" form:"sql_content" example:"select * from t1; select * from t2;" valid:"required"`
SQLType string `json:"sql_type" form:"sql_type" example:"sql" enums:"sql,mybatis," valid:"omitempty,oneof=sql mybatis"`
ProjectId string `json:"project_id" form:"project_id" example:"700300" valid:"required"`
RuleTemplateName string `json:"rule_template_name" form:"rule_template_name" example:"default" valid:"required"`
SQLContent string `json:"sql_content" form:"sql_content" example:"select * from t1; select * from t2;" valid:"required"`
SQLType string `json:"sql_type" form:"sql_type" example:"sql" enums:"sql,mybatis," valid:"omitempty,oneof=sql mybatis"`
ProjectId string `json:"project_id" form:"project_id" example:"700300" valid:"required"`
RuleTemplateName string `json:"rule_template_name" form:"rule_template_name" example:"default" valid:"required"`
InstanceName *string `json:"instance_name" form:"instance_name" example:"instance1"`
SchemaName *string `json:"schema_name" form:"schema_name" example:"schema1"`
}

type AuditResDataV2 struct {
Expand Down Expand Up @@ -72,7 +74,24 @@ func DirectAudit(c echo.Context) error {

l := log.NewEntry().WithField(c.Path(), "direct audit failed")

task, err := server.AuditSQLByDBType(l, sql, req.InstanceType, req.ProjectId, req.RuleTemplateName)
var instance *model.Instance
if req.ProjectId != "" && req.InstanceName != nil {
var exist bool
instance, exist, err = dms.GetInstanceInProjectByName(c.Request().Context(), req.ProjectId, *req.InstanceName)
if err != nil {
return controller.JSONBaseErrorReq(c, err)
}
if !exist {
return controller.JSONBaseErrorReq(c, v1.ErrInstanceNotExist)
}
}

var task *model.Task
if instance != nil && req.SchemaName != nil {
task, err = server.DirectAuditByInstance(l, sql, *req.SchemaName, instance, req.RuleTemplateName)
} else {
task, err = server.AuditSQLByDBType(l, sql, req.InstanceType, req.ProjectId, req.RuleTemplateName)
}
if err != nil {
l.Errorf("audit sqls failed: %v", err)
return controller.JSONBaseErrorReq(c, v1.ErrDirectAudit)
Expand Down Expand Up @@ -180,7 +199,7 @@ func DirectAuditFiles(c echo.Context) error {

var task *model.Task
if instance != nil && schemaName != "" {
task, err = server.DirectAuditByInstance(l, sqls, schemaName, instance)
task, err = server.DirectAuditByInstance(l, sqls, schemaName, instance, "")
} else {
task, err = server.AuditSQLByDBType(l, sqls, req.InstanceType, projectUid, "")
}
Expand Down
8 changes: 8 additions & 0 deletions sqle/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22620,6 +22620,10 @@ var doc = `{
"v2.DirectAuditReqV2": {
"type": "object",
"properties": {
"instance_name": {
"type": "string",
"example": "instance1"
},
"instance_type": {
"type": "string",
"example": "MySQL"
Expand All @@ -22632,6 +22636,10 @@ var doc = `{
"type": "string",
"example": "default"
},
"schema_name": {
"type": "string",
"example": "schema1"
},
"sql_content": {
"description": "调用方不应该关心SQL是否被完美的拆分成独立的条目, 拆分SQL由SQLE实现",
"type": "string",
Expand Down
8 changes: 8 additions & 0 deletions sqle/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -22604,6 +22604,10 @@
"v2.DirectAuditReqV2": {
"type": "object",
"properties": {
"instance_name": {
"type": "string",
"example": "instance1"
},
"instance_type": {
"type": "string",
"example": "MySQL"
Expand All @@ -22616,6 +22620,10 @@
"type": "string",
"example": "default"
},
"schema_name": {
"type": "string",
"example": "schema1"
},
"sql_content": {
"description": "调用方不应该关心SQL是否被完美的拆分成独立的条目, 拆分SQL由SQLE实现",
"type": "string",
Expand Down
6 changes: 6 additions & 0 deletions sqle/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6505,6 +6505,9 @@ definitions:
type: object
v2.DirectAuditReqV2:
properties:
instance_name:
example: instance1
type: string
instance_type:
example: MySQL
type: string
Expand All @@ -6514,6 +6517,9 @@ definitions:
rule_template_name:
example: default
type: string
schema_name:
example: schema1
type: string
sql_content:
description: 调用方不应该关心SQL是否被完美的拆分成独立的条目, 拆分SQL由SQLE实现
example: select * from t1; select * from t2;
Expand Down
4 changes: 2 additions & 2 deletions sqle/server/audit.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ func HookAudit(l *logrus.Entry, task *model.Task, hook AuditHook, projectId *mod

const AuditSchema = "AuditSchema"

func DirectAuditByInstance(l *logrus.Entry, sql, schemaName string, instance *model.Instance) (*model.Task, error) {
func DirectAuditByInstance(l *logrus.Entry, sql, schemaName string, instance *model.Instance, ruleTemplateName string) (*model.Task, error) {
st := model.GetStorage()
rules, customRules, err := st.GetAllRulesByTmpNameAndProjectIdInstanceDBType("", "", instance, instance.DbType)
rules, customRules, err := st.GetAllRulesByTmpNameAndProjectIdInstanceDBType(ruleTemplateName, instance.ProjectId, instance, instance.DbType)
if err != nil {
return nil, err
}
Expand Down
Loading