Skip to content

Commit ce99e07

Browse files
authored
Merge pull request #178 from DoubleLoong/main
add interface
2 parents 5707457 + e670552 commit ce99e07

File tree

4 files changed

+77
-10
lines changed

4 files changed

+77
-10
lines changed

controllers/tp_data_services_config.go

+28
Original file line numberDiff line numberDiff line change
@@ -163,3 +163,31 @@ func (c *TpDataServicesConfigController) GetData() {
163163
utils.SuccessWithMessage(400, rsp_err.Error(), (*context2.Context)(c.Ctx))
164164
}
165165
}
166+
167+
//表名查询
168+
func (c *TpDataServicesConfigController) TpTable() {
169+
170+
var tpdataservicesconfig services.TpDataServicesConfig
171+
d, rsp_err := tpdataservicesconfig.GetTableNames()
172+
if rsp_err == nil {
173+
utils.SuccessWithDetailed(200, "success", d, map[string]string{}, (*context2.Context)(c.Ctx))
174+
} else {
175+
utils.SuccessWithMessage(400, rsp_err.Error(), (*context2.Context)(c.Ctx))
176+
}
177+
}
178+
179+
//表字段查询
180+
func (c *TpDataServicesConfigController) TpTableField() {
181+
reqData := valid.GetTpTableFieldValidate{}
182+
if err := valid.ParseAndValidate(&c.Ctx.Input.RequestBody, &reqData); err != nil {
183+
utils.SuccessWithMessage(1000, err.Error(), (*context2.Context)(c.Ctx))
184+
return
185+
}
186+
var tpdataservicesconfig services.TpDataServicesConfig
187+
d, rsp_err := tpdataservicesconfig.GetTableField(reqData.TableName)
188+
if rsp_err == nil {
189+
utils.SuccessWithDetailed(200, "success", d, map[string]string{}, (*context2.Context)(c.Ctx))
190+
} else {
191+
utils.SuccessWithMessage(400, rsp_err.Error(), (*context2.Context)(c.Ctx))
192+
}
193+
}

routers/router.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -440,12 +440,13 @@ func init() {
440440

441441
web.NSRouter("/notification/history/list", &controllers.TpNotification{}, "*:HistoryList"),
442442
//数据服务配置
443-
//本地可视化插件
444443
web.NSRouter("/tp_data_services_config/list", &controllers.TpDataServicesConfigController{}, "*:List"),
445444
web.NSRouter("/tp_data_services_config/add", &controllers.TpDataServicesConfigController{}, "*:Add"),
446445
web.NSRouter("/tp_data_services_config/edit", &controllers.TpDataServicesConfigController{}, "*:Edit"),
447446
web.NSRouter("/tp_data_services_config/del", &controllers.TpDataServicesConfigController{}, "*:Del"),
448447
web.NSRouter("/tp_data_services_config/quize", &controllers.TpDataServicesConfigController{}, "*:Quize"),
448+
web.NSRouter("/tp_data_services_config/tptable", &controllers.TpDataServicesConfigController{}, "*:TpTable"),
449+
web.NSRouter("/tp_data_services_config/tptablefield", &controllers.TpDataServicesConfigController{}, "*:TpTableField"),
449450

450451
// openapi
451452
web.NSRouter("/openapi/auth/list", &controllers.OpenApiController{}, "*:List"),

services/tp_data_services_config.go

+43-9
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ type TpDataServicesConfig struct {
2323
TimeField []string
2424
}
2525

26+
//表面结构体
27+
type TableInfo struct {
28+
TableName string `json:"table_name" alias:"名称" gorm:"column:table_name"`
29+
Comment string `json:"comment" alias:"描述" gorm:"column:table_description"`
30+
}
31+
32+
//获取一条数详情
2633
func (*TpDataServicesConfig) GetTpDataServicesConfigDetail(id string) models.TpDataServicesConfig {
2734
var TpDataServicesConfig models.TpDataServicesConfig
2835
psql.Mydb.First(&TpDataServicesConfig, "id = ? ", id)
@@ -37,15 +44,6 @@ func (*TpDataServicesConfig) GetTpDataServicesConfigList(PaginationValidate vali
3744
if PaginationValidate.Name != "" {
3845
db.Where("name like ?", "%"+PaginationValidate.Name+"%")
3946
}
40-
// if PaginationValidate.AppKey != "" {
41-
// db.Where("app_key like ?", "%"+PaginationValidate.AppKey+"%")
42-
// }
43-
// if PaginationValidate.SignatureMode != "" {
44-
// db.Where("SignatureMode = ?", PaginationValidate.SignatureMode)
45-
// }
46-
// if PaginationValidate.IpWhitelist != "" {
47-
// db.Where("ip_whitelist like ?", "%"+PaginationValidate.IpWhitelist+"%")
48-
// }
4947
if PaginationValidate.EnableFlag != "" {
5048
db.Where("enable_flag = ?", PaginationValidate.EnableFlag)
5149
}
@@ -207,3 +205,39 @@ func (*TpDataServicesConfig) GetDataByAppkey(reqData valid.GetDataPaginationVali
207205
}
208206
return result, nil
209207
}
208+
209+
func (*TpDataServicesConfig) GetTableNames() ([]TableInfo, error) {
210+
var tableNames []TableInfo
211+
sql := `SELECT table_name, obj_description (c.oid) AS table_description
212+
FROM information_schema.tables t
213+
LEFT JOIN pg_class c ON t.table_name = c.relname
214+
WHERE table_type = 'BASE TABLE' AND table_schema = 'public'`
215+
result := psql.Mydb.Raw(sql).Scan(&tableNames)
216+
if result.Error != nil {
217+
logs.Error(result.Error, gorm.ErrRecordNotFound)
218+
return tableNames, result.Error
219+
}
220+
return tableNames, nil
221+
222+
}
223+
func (*TpDataServicesConfig) GetTableField(table string) ([]map[string]interface{}, error) {
224+
sql := `SELECT
225+
a.attname AS field,
226+
format_type ( a.atttypid,a.atttypmod ) AS TYPE,
227+
col_description(a.attrelid, a.attnum) AS comment
228+
FROM
229+
pg_class as c,
230+
pg_attribute as a
231+
WHERE
232+
c.relname = 'asset'
233+
AND a.attnum > 0
234+
AND a.attrelid = c.oid`
235+
var result []map[string]interface{}
236+
db := psql.Mydb.Raw(sql).Scan(&result)
237+
if db.Error != nil {
238+
logs.Error(db.Error, gorm.ErrRecordNotFound)
239+
return result, db.Error
240+
241+
}
242+
return result, nil
243+
}

validate/tp_data_services_config.go

+4
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,7 @@ type TpDataServicesConfigIdValidate struct {
6868
type TpDataServicesConfigQuizeValidate struct {
6969
DataSql string `json:"data_sql" alias:"Sql" valid:"Required"`
7070
}
71+
72+
type GetTpTableFieldValidate struct {
73+
TableName string `json:"table_name" alias:"表名" `
74+
}

0 commit comments

Comments
 (0)