From ae60ab82cf1ab1323d280ace20194078cfe8d7d1 Mon Sep 17 00:00:00 2001 From: luowei Date: Fri, 10 Jun 2022 15:34:26 +0800 Subject: [PATCH 1/4] add number field to distinguish each sql in audit-plan-report --- sqle/api/controller/v2/audit_plan.go | 1 + sqle/model/audit_plan_list_test.go | 6 +++--- sqle/model/audit_plan_report.go | 1 + sqle/model/audit_plan_report_list.go | 3 ++- sqle/server/auditplan/task.go | 3 ++- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/sqle/api/controller/v2/audit_plan.go b/sqle/api/controller/v2/audit_plan.go index 37458b7cdf..a14061505f 100644 --- a/sqle/api/controller/v2/audit_plan.go +++ b/sqle/api/controller/v2/audit_plan.go @@ -152,6 +152,7 @@ func GetAuditPlanReportSQLs(c echo.Context) error { auditPlanReportSQLsResV2[i] = AuditPlanReportSQLResV2{ SQL: auditPlanReportSQL.SQL, AuditResult: auditPlanReportSQL.AuditResult, + Number: auditPlanReportSQL.Number, } } return c.JSON(http.StatusOK, &GetAuditPlanReportSQLsResV2{ diff --git a/sqle/model/audit_plan_list_test.go b/sqle/model/audit_plan_list_test.go index 63b8eb5395..ac4a2a0d4d 100644 --- a/sqle/model/audit_plan_list_test.go +++ b/sqle/model/audit_plan_list_test.go @@ -149,10 +149,10 @@ func TestStorage_GetAuditPlanReportSQLsByReq(t *testing.T) { assert.NoError(t, err) defer mockDB.Close() InitMockStorage(mockDB) - mock.ExpectPrepare(fmt.Sprintf(`SELECT report_sqls.sql, report_sqls.audit_result %v LIMIT ? OFFSET ?`, tableAndRowOfSQL)). + mock.ExpectPrepare(fmt.Sprintf(`SELECT report_sqls.sql, report_sqls.audit_result, report_sqls.number %v LIMIT ? OFFSET ?`, tableAndRowOfSQL)). ExpectQuery().WithArgs(1, 100, 10).WillReturnRows(sqlmock.NewRows([]string{ - "sql", "audit_result", - }).AddRow("select * from t1 where id = 1", "FAKE AUDIT RESULT")) + "sql", "audit_result", "number", + }).AddRow("select * from t1 where id = 1", "FAKE AUDIT RESULT", "1")) mock.ExpectPrepare(fmt.Sprintf(`SELECT COUNT(*) %v`, tableAndRowOfSQL)). ExpectQuery().WithArgs(1).WillReturnRows(sqlmock.NewRows([]string{"COUNT(*)"}).AddRow("2")) diff --git a/sqle/model/audit_plan_report.go b/sqle/model/audit_plan_report.go index 6de4b1ba4a..f6ee08a201 100644 --- a/sqle/model/audit_plan_report.go +++ b/sqle/model/audit_plan_report.go @@ -19,6 +19,7 @@ type AuditPlanReportSQLV2 struct { Model AuditPlanReportID uint `json:"audit_plan_report_id" gorm:"index"` SQL string `json:"sql" gorm:"type:text;not null"` + Number uint `json:"number"` AuditResult string `json:"audit_result" gorm:"type:text"` AuditPlanReport *AuditPlanReportV2 `gorm:"foreignkey:AuditPlanReportID"` diff --git a/sqle/model/audit_plan_report_list.go b/sqle/model/audit_plan_report_list.go index 37bd975dac..32b527e730 100644 --- a/sqle/model/audit_plan_report_list.go +++ b/sqle/model/audit_plan_report_list.go @@ -58,10 +58,11 @@ func (s *Storage) GetAuditPlanReportsByReq(data map[string]interface{}) ( type AuditPlanReportSQLListDetail struct { SQL string `json:"sql"` AuditResult string `json:"audit_result"` + Number uint `json:"number"` } var auditPlanReportSQLQueryTpl = ` -SELECT report_sqls.sql, report_sqls.audit_result +SELECT report_sqls.sql, report_sqls.audit_result, report_sqls.number {{- template "body" . -}} diff --git a/sqle/server/auditplan/task.go b/sqle/server/auditplan/task.go index 85ddcde735..d9873951d3 100644 --- a/sqle/server/auditplan/task.go +++ b/sqle/server/auditplan/task.go @@ -109,9 +109,10 @@ func (at *baseTask) audit(task *model.Task) (*model.AuditPlanReportV2, error) { Score: task.Score, AuditLevel: task.AuditLevel, } - for _, executeSQL := range task.ExecuteSQLs { + for i, executeSQL := range task.ExecuteSQLs { auditPlanReport.AuditPlanReportSQLs = append(auditPlanReport.AuditPlanReportSQLs, &model.AuditPlanReportSQLV2{ SQL: executeSQL.Content, + Number: uint(i + 1), AuditResult: executeSQL.AuditResult, }) } From 38ad749216d45259156efe530cb4c46b69d5a0c7 Mon Sep 17 00:00:00 2001 From: taolx0 Date: Mon, 13 Jun 2022 09:59:36 +0800 Subject: [PATCH 2/4] explain sql api interface --- sqle/api/app.go | 1 + sqle/api/controller/v1/sql_query.go | 2 +- sqle/api/controller/v1/sql_query_ce.go | 4 ++++ 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/sqle/api/app.go b/sqle/api/app.go index c25f737300..c3a34401b4 100644 --- a/sqle/api/app.go +++ b/sqle/api/app.go @@ -233,6 +233,7 @@ func StartApi(net *gracenet.Net, exitChan chan struct{}, config config.SqleConfi v1Router.POST("/sql_query/prepare/:instance_name/", v1.PrepareSQLQuery) v1Router.GET("/sql_query/history/:instance_name/", v1.GetSQLQueryHistory) v1Router.GET("/sql_query/results/:query_id/", v1.GetSQLResult) + v1Router.GET("/sql_query/explain/:instance_name/", v1.GetSQLExplain) // UI e.File("/", "ui/index.html") diff --git a/sqle/api/controller/v1/sql_query.go b/sqle/api/controller/v1/sql_query.go index 7c3e21cdb5..92b0ba2ff3 100644 --- a/sqle/api/controller/v1/sql_query.go +++ b/sqle/api/controller/v1/sql_query.go @@ -131,5 +131,5 @@ type GetSQLExplainResV1 struct { // @Success 200 {object} v1.GetSQLExplainResV1 // @router /v1/sql_query/explain/{instance_name}/ [get] func GetSQLExplain(c echo.Context) error { - return nil + return getSQLExplain(c) } diff --git a/sqle/api/controller/v1/sql_query_ce.go b/sqle/api/controller/v1/sql_query_ce.go index 2bf03ae860..b3a599fe1a 100644 --- a/sqle/api/controller/v1/sql_query_ce.go +++ b/sqle/api/controller/v1/sql_query_ce.go @@ -24,3 +24,7 @@ func getSQLResult(c echo.Context) error { func getSQLQueryHistory(c echo.Context) error { return controller.JSONBaseErrorReq(c, errCommunityEditionDoesNotSupportSqlQuery) } + +func getSQLExplain(c echo.Context) error { + return controller.JSONBaseErrorReq(c, errCommunityEditionDoesNotSupportSqlQuery) +} From 69e783c8e4a3d7cd5d63aaebabb7c40a2fbcc9df Mon Sep 17 00:00:00 2001 From: taolx0 Date: Mon, 13 Jun 2022 16:11:33 +0800 Subject: [PATCH 3/4] get table metadata interface from table name --- sqle/api/app.go | 2 ++ sqle/api/controller/v1/instance.go | 2 +- sqle/api/controller/v1/instance_ce.go | 5 +++++ 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/sqle/api/app.go b/sqle/api/app.go index c25f737300..c2633dddad 100644 --- a/sqle/api/app.go +++ b/sqle/api/app.go @@ -171,6 +171,8 @@ func StartApi(net *gracenet.Net, exitChan chan struct{}, config config.SqleConfi v1Router.GET("/instances/:instance_name/rules", v1.GetInstanceRules) v1Router.GET("/instances/:instance_name/workflow_template", v1.GetInstanceWorkflowTemplate) v1Router.GET("/instances/:instance_name/schemas/:schema_name/tables", v1.ListTableBySchema) + v1Router.GET("/instances/:instance_name/schemas/:schema_name/tables/:table_name/metadata", v1.GetTableMetadata) + // rule template v1Router.GET("/rule_templates", v1.GetRuleTemplates) v1Router.GET("/rule_template_tips", v1.GetRuleTemplateTips) diff --git a/sqle/api/controller/v1/instance.go b/sqle/api/controller/v1/instance.go index feaf596abf..1ec5d8deec 100644 --- a/sqle/api/controller/v1/instance.go +++ b/sqle/api/controller/v1/instance.go @@ -1103,5 +1103,5 @@ type GetTableMetadataResV1 struct { // @Success 200 {object} v1.GetTableMetadataResV1 // @router /v1/instances/{instance_name}/schemas/{schema_name}/tables/{table_name}/metadata [get] func GetTableMetadata(c echo.Context) error { - return nil + return getTableMetadata(c) } diff --git a/sqle/api/controller/v1/instance_ce.go b/sqle/api/controller/v1/instance_ce.go index ea5ac5b27c..85e862ba76 100644 --- a/sqle/api/controller/v1/instance_ce.go +++ b/sqle/api/controller/v1/instance_ce.go @@ -14,6 +14,7 @@ import ( ) var errCommunityEditionDoesNotSupportListTables = errors.New(errors.EnterpriseEditionFeatures, e.New("community edition does not support list tables")) +var errCommunityEditionDoesNotSupportGetTablesMetadata = errors.New(errors.EnterpriseEditionFeatures, e.New("community edition does not support get table metadata")) func getInstanceTips(c echo.Context) error { req := new(InstanceTipReqV1) @@ -54,3 +55,7 @@ func getInstanceTips(c echo.Context) error { func listTableBySchema(c echo.Context) error { return controller.JSONBaseErrorReq(c, errCommunityEditionDoesNotSupportListTables) } + +func getTableMetadata(c echo.Context) error { + return controller.JSONBaseErrorReq(c, errCommunityEditionDoesNotSupportGetTablesMetadata) +} From cf66ad480629b52f4f318e993bb25c69e2266bf6 Mon Sep 17 00:00:00 2001 From: luowei Date: Tue, 14 Jun 2022 10:59:28 +0800 Subject: [PATCH 4/4] add field to identify table metadata --- sqle/api/controller/v1/instance.go | 1 + sqle/docs/docs.go | 3 +++ sqle/docs/swagger.json | 3 +++ sqle/docs/swagger.yaml | 2 ++ sqle/driver/driver_analysis.go | 2 ++ 5 files changed, 11 insertions(+) diff --git a/sqle/api/controller/v1/instance.go b/sqle/api/controller/v1/instance.go index feaf596abf..ac5b7d8672 100644 --- a/sqle/api/controller/v1/instance.go +++ b/sqle/api/controller/v1/instance.go @@ -1081,6 +1081,7 @@ type TableIndexes struct { type TableMeta struct { Name string `json:"name"` + Schema string `json:"schema"` Columns TableColumns `json:"columns"` Indexes TableIndexes `json:"indexes"` CreateTableSQL string `json:"create_table_sql"` diff --git a/sqle/docs/docs.go b/sqle/docs/docs.go index f7a174a7a7..aaa1f8122d 100644 --- a/sqle/docs/docs.go +++ b/sqle/docs/docs.go @@ -7185,6 +7185,9 @@ var doc = `{ }, "name": { "type": "string" + }, + "schema": { + "type": "string" } } }, diff --git a/sqle/docs/swagger.json b/sqle/docs/swagger.json index c5fbd7b61e..c0d9147ee2 100644 --- a/sqle/docs/swagger.json +++ b/sqle/docs/swagger.json @@ -7169,6 +7169,9 @@ }, "name": { "type": "string" + }, + "schema": { + "type": "string" } } }, diff --git a/sqle/docs/swagger.yaml b/sqle/docs/swagger.yaml index b8c4e4751e..2fd28765b2 100644 --- a/sqle/docs/swagger.yaml +++ b/sqle/docs/swagger.yaml @@ -1799,6 +1799,8 @@ definitions: type: object name: type: string + schema: + type: string type: object v1.TableMetaItemHeadResV1: properties: diff --git a/sqle/driver/driver_analysis.go b/sqle/driver/driver_analysis.go index a7d3ccc8ad..4fd46aacd5 100644 --- a/sqle/driver/driver_analysis.go +++ b/sqle/driver/driver_analysis.go @@ -66,6 +66,8 @@ type IndexesInfo struct { } type TableMetaItem struct { + Name string + Schema string ColumnsInfo ColumnsInfo IndexesInfo IndexesInfo CreateTableSQL string