api.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package sql_executor
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/binding"
  4. "git.sxidc.com/go-framework/baize/framework/core/api"
  5. "git.sxidc.com/go-framework/baize/framework/core/api/request"
  6. "git.sxidc.com/go-framework/baize/framework/core/api/response"
  7. "git.sxidc.com/go-framework/baize/framework/core/application"
  8. "git.sxidc.com/go-framework/baize/framework/core/domain"
  9. "git.sxidc.com/go-framework/baize/framework/core/infrastructure"
  10. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database"
  11. "git.sxidc.com/go-framework/baize/framework/core/infrastructure/database/sql"
  12. "git.sxidc.com/go-tools/utils/strutils"
  13. )
  14. // Simple Bind参数
  15. type Simple struct {
  16. // schema
  17. Schema string
  18. }
  19. func (simple *Simple) bind(binder *binding.Binder) {
  20. sqlExecuteLogTableName := domain.TableName(simple.Schema, &SqlExecuteLog{})
  21. binding.PostBind(binder, &binding.SimpleBindItem[map[string]any]{
  22. Path: "/sql/execute",
  23. SendResponseFunc: response.SendMapResponse,
  24. RequestParams: &ExecuteSqlJsonBody{},
  25. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (map[string]any, error) {
  26. errResponse := map[string]any{
  27. "result": make([]sql.Result, 0),
  28. }
  29. dbExecutor := i.DBExecutor()
  30. jsonBody, err := request.ToConcrete[*ExecuteSqlJsonBody](params)
  31. if err != nil {
  32. return errResponse, err
  33. }
  34. var results []sql.Result
  35. err = database.Transaction(dbExecutor, func(tx database.Executor) error {
  36. innerResults, err := database.ExecuteRawSql(tx, jsonBody.Sql, nil)
  37. if err != nil {
  38. return err
  39. }
  40. sqlExecuteLog := &SqlExecuteLog{
  41. Sql: jsonBody.Sql,
  42. ExecutorID: jsonBody.ExecutorID,
  43. ExecutorName: jsonBody.ExecutorName,
  44. }
  45. err = domain.CheckFieldsForCreate(sqlExecuteLog, sqlExecuteLog.GetFieldMap())
  46. if err != nil {
  47. return err
  48. }
  49. err = database.InsertEntity(tx, sqlExecuteLogTableName, sqlExecuteLog)
  50. if err != nil {
  51. return err
  52. }
  53. results = innerResults
  54. return nil
  55. })
  56. if err != nil {
  57. return errResponse, err
  58. }
  59. return map[string]any{
  60. "result": results,
  61. }, nil
  62. },
  63. })
  64. binding.GetBind(binder, &binding.SimpleBindItem[response.InfosData[SqlExecuteLogInfo]]{
  65. Path: "/sql/execute/log",
  66. SendResponseFunc: response.SendInfosResponse[SqlExecuteLogInfo],
  67. RequestParams: &QuerySqlExecuteLogQueryParams{},
  68. ServiceFunc: func(c *api.Context, params request.Params, objects []domain.Object, i *infrastructure.Infrastructure) (response.InfosData[SqlExecuteLogInfo], error) {
  69. errResponse := response.InfosData[SqlExecuteLogInfo]{
  70. Infos: make([]SqlExecuteLogInfo, 0),
  71. }
  72. dbExecutor := i.DBExecutor()
  73. queryParams, err := request.ToConcrete[*QuerySqlExecuteLogQueryParams](params)
  74. if err != nil {
  75. return errResponse, err
  76. }
  77. conditions := sql.NewConditions()
  78. if strutils.IsStringNotEmpty(queryParams.Sql) {
  79. conditions.Like(ColumnSql, "%"+queryParams.Sql+"%")
  80. }
  81. if strutils.IsStringNotEmpty(queryParams.ExecutorID) {
  82. conditions.Like(ColumnExecutorID, queryParams.ExecutorID)
  83. }
  84. if strutils.IsStringNotEmpty(queryParams.ExecutorName) {
  85. conditions.Like(ColumnExecutorName, "%"+queryParams.ExecutorName+"%")
  86. }
  87. if strutils.IsStringNotEmpty(queryParams.StartExecuteTime) {
  88. conditions.GreaterThanAndEqual(ColumnExecutedTime, queryParams.StartExecuteTime)
  89. }
  90. if strutils.IsStringNotEmpty(queryParams.EndExecuteTime) {
  91. conditions.LessThanAndEqual(ColumnExecutedTime, queryParams.EndExecuteTime)
  92. }
  93. results, totalCount, err := database.Query(dbExecutor, &sql.QueryExecuteParams{
  94. TableName: sqlExecuteLogTableName,
  95. Conditions: conditions,
  96. PageNo: queryParams.PageNo,
  97. PageSize: queryParams.PageSize,
  98. })
  99. if err != nil {
  100. return errResponse, nil
  101. }
  102. infos := make([]SqlExecuteLogInfo, 0)
  103. err = sql.ParseSqlResult(results, &infos)
  104. if err != nil {
  105. return errResponse, nil
  106. }
  107. return response.InfosData[SqlExecuteLogInfo]{
  108. Infos: infos,
  109. TotalCount: totalCount,
  110. PageNo: queryParams.PageNo,
  111. }, nil
  112. },
  113. })
  114. }
  115. func Bind(app *application.App, simple *Simple) {
  116. binder := binding.NewBinder(app.Api().ChooseRouter(api.RouterPrefix, ""), app.Infrastructure())
  117. simple.bind(binder)
  118. }