Files
go-gin-api/internal/api/tool/func_tables.go
T
新亮 4c37a7e6b5 feature(1.2.8): swagger 接口文档新增 Security
- 将 middleware 命名为 interceptor
- 将 deploy 命名为 deployments
- 移除 pkg/errno
- 使用 proposal 目录
- 优化代码
2021-11-28 13:25:27 +08:00

77 lines
1.8 KiB
Go

package tool
import (
"fmt"
"net/http"
"github.com/xinliangnote/go-gin-api/internal/code"
"github.com/xinliangnote/go-gin-api/internal/pkg/core"
)
type tablesRequest struct {
DbName string `form:"db_name"` // 数据库名称
}
type tablesResponse struct {
List []tableData `json:"list"` // 数据表列表
}
type tableData struct {
Name string `json:"table_name"` // 数据表名称
Comment string `json:"table_comment"` // 数据表备注
}
// Tables 查询 Table
// @Summary 查询 Table
// @Description 查询 Table
// @Tags API.tool
// @Accept application/x-www-form-urlencoded
// @Produce json
// @Param db_name formData string true "数据库名称"
// @Success 200 {object} tablesResponse
// @Failure 400 {object} code.Failure
// @Router /api/tool/data/tables [post]
// @Security LoginToken
func (h *handler) Tables() core.HandlerFunc {
return func(c core.Context) {
req := new(tablesRequest)
res := new(tablesResponse)
if err := c.ShouldBindForm(req); err != nil {
c.AbortWithError(core.Error(
http.StatusBadRequest,
code.ParamBindError,
code.Text(code.ParamBindError)).WithError(err),
)
return
}
sqlTables := fmt.Sprintf("SELECT `table_name`,`table_comment` FROM `information_schema`.`tables` WHERE `table_schema`= '%s'", req.DbName)
// TODO 后期支持查询多个数据库
rows, err := h.db.GetDbR().Raw(sqlTables).Rows()
if err != nil {
c.AbortWithError(core.Error(
http.StatusBadRequest,
code.MySQLExecError,
code.Text(code.MySQLExecError)).WithError(err),
)
return
}
defer rows.Close()
for rows.Next() {
var info tableData
err = rows.Scan(&info.Name, &info.Comment)
if err != nil {
fmt.Printf("execute query tables action error,had ignored, detail is [%v]\n", err.Error())
continue
}
res.List = append(res.List, info)
}
c.Payload(res)
}
}