sqldb-ws/controllers/schema.go

56 lines
1.3 KiB
Go
Raw Normal View History

2021-10-29 23:37:32 +02:00
package controllers
import (
2023-04-24 16:53:10 +02:00
"net/http"
2021-10-29 23:37:32 +02:00
"os"
"forge.redroom.link/yves/sqldb"
beego "github.com/beego/beego/v2/server/web"
"github.com/rs/zerolog/log"
)
// Operations about schema
type SchemaController struct {
beego.Controller
}
2023-04-24 16:53:10 +02:00
// @Title GetTablesList
// @Description Get database tables list
2021-10-29 23:37:32 +02:00
// @Success 200 {string} success !
2023-04-24 16:53:10 +02:00
// @Failure 500 query error
2021-10-29 23:37:32 +02:00
// @router / [get]
2023-04-24 16:53:10 +02:00
func (s *SchemaController) GetTablesList() {
2021-10-29 23:37:32 +02:00
db := sqldb.Open(os.Getenv("driverdb"), os.Getenv("paramsdb"))
data, err := db.ListTables()
if err != nil {
log.Error().Msg(err.Error())
s.Data["json"] = map[string]string{"error": err.Error()}
2023-04-24 16:53:10 +02:00
s.Ctx.Output.SetStatus(http.StatusInternalServerError)
2021-10-29 23:37:32 +02:00
}
s.Data["json"] = data
s.ServeJSON()
db.Close()
}
// @Title GetSchema
2023-04-24 16:53:10 +02:00
// @Description Get table schema
2021-10-29 23:37:32 +02:00
// @Param table path string true "Name of the table"
// @Success 200 success !
2023-04-24 16:53:10 +02:00
// @Failure 204 no table
2021-10-29 23:37:32 +02:00
// @router /:table [get]
func (s *SchemaController) GetSchema() {
table := s.GetString(":table")
db := sqldb.Open(os.Getenv("driverdb"), os.Getenv("paramsdb"))
data, err := db.Table(table).GetSchema()
if err != nil {
log.Error().Msg(err.Error())
s.Data["json"] = map[string]string{"error": err.Error()}
2023-04-24 16:53:10 +02:00
s.Ctx.Output.SetStatus(http.StatusNoContent)
2021-10-29 23:37:32 +02:00
}
s.Data["json"] = data
s.ServeJSON()
db.Close()
}