123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145 |
- package sql_executor
- import (
- "git.sxidc.com/go-framework/baize/framework/binding"
- "git.sxidc.com/go-framework/baize/framework/core/api"
- "git.sxidc.com/go-framework/baize/framework/core/api/request"
- "git.sxidc.com/go-framework/baize/framework/core/api/response"
- "git.sxidc.com/go-framework/baize/framework/core/application"
- "git.sxidc.com/go-framework/baize/framework/core/domain"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
- "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
- "git.sxidc.com/go-tools/utils/strutils"
- )
- // Simple Bind参数
- type Simple struct {
- // schema
- Schema string
- }
- func (simple *Simple) bind(binder *binding.Binder) {
- sqlExecuteLogTableName := domain.TableName(simple.Schema, &SqlExecuteLog{})
- binding.PostBind(binder, &binding.SimpleBindItem[map[string]any]{
- Path: "/sql/execute",
- SendResponseFunc: response.SendMapResponse,
- RequestParams: &ExecuteSqlJsonBody{},
- ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (map[string]any, error) {
- errResponse := map[string]any{
- "result": make([]sql.Result, 0),
- }
- dbExecutor := i.DBExecutor()
- jsonBody, err := request.ToConcrete[*ExecuteSqlJsonBody](params)
- if err != nil {
- return errResponse, err
- }
- var results []sql.Result
- err = database.Transaction(dbExecutor, func(tx database.Executor) error {
- innerResults, err := database.ExecuteRawSql(tx, jsonBody.Sql, nil)
- if err != nil {
- return err
- }
- sqlExecuteLog := &SqlExecuteLog{
- Sql: jsonBody.Sql,
- ExecutorID: jsonBody.ExecutorID,
- ExecutorName: jsonBody.ExecutorName,
- }
- err = sqlExecuteLog.ForCreate()
- if err != nil {
- return err
- }
- err = database.InsertEntity(tx, sqlExecuteLogTableName, sqlExecuteLog)
- if err != nil {
- return err
- }
- results = innerResults
- return nil
- })
- if err != nil {
- return errResponse, err
- }
- return map[string]any{
- "result": results,
- }, nil
- },
- })
- binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[SqlExecuteLogInfo]]{
- Path: "/sql/execute/log",
- SendResponseFunc: response.SendInfosResponse[SqlExecuteLogInfo],
- RequestParams: &QuerySqlExecuteLogQueryParams{},
- ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[SqlExecuteLogInfo], error) {
- errResponse := response.InfosData[SqlExecuteLogInfo]{
- Infos: make([]SqlExecuteLogInfo, 0),
- }
- dbExecutor := i.DBExecutor()
- queryParams, err := request.ToConcrete[*QuerySqlExecuteLogQueryParams](params)
- if err != nil {
- return errResponse, err
- }
- conditions := sql.NewConditions()
- if strutils.IsStringNotEmpty(queryParams.Sql) {
- conditions.Like(ColumnSql, "%"+queryParams.Sql+"%")
- }
- if strutils.IsStringNotEmpty(queryParams.ExecutorID) {
- conditions.Like(ColumnExecutorID, queryParams.ExecutorID)
- }
- if strutils.IsStringNotEmpty(queryParams.ExecutorName) {
- conditions.Like(ColumnExecutorName, "%"+queryParams.ExecutorName+"%")
- }
- if strutils.IsStringNotEmpty(queryParams.StartExecuteTime) {
- conditions.GreaterThanAndEqual(ColumnExecutedTime, queryParams.StartExecuteTime)
- }
- if strutils.IsStringNotEmpty(queryParams.EndExecuteTime) {
- conditions.LessThanAndEqual(ColumnExecutedTime, queryParams.EndExecuteTime)
- }
- results, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
- TableName: sqlExecuteLogTableName,
- Conditions: conditions,
- PageNo: queryParams.PageNo,
- PageSize: queryParams.PageSize,
- })
- if err != nil {
- return errResponse, nil
- }
- infos := make([]SqlExecuteLogInfo, 0)
- err = sql.ParseSqlResult(results, &infos)
- if err != nil {
- return errResponse, nil
- }
- return response.InfosData[SqlExecuteLogInfo]{
- Infos: infos,
- TotalCount: totalCount,
- PageNo: queryParams.PageNo,
- }, nil
- },
- })
- }
- func Bind(app *application.App, simple *Simple) {
- binder := binding.NewBinder(app.Api().ChooseRouter(api.RouterPrefix, ""), app.Infrastructure())
- simple.bind(binder)
- }
|