Add endpoint to fetch table indexes
This commit is contained in:
parent
aca2b473b6
commit
d786919a36
13
api.go
13
api.go
@ -34,7 +34,7 @@ func API_GetTables(c *gin.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func API_GetTable(c *gin.Context) {
|
func API_GetTable(c *gin.Context) {
|
||||||
res, err := dbClient.Query(fmt.Sprintf(SQL_TABLE_SCHEMA, c.Params.ByName("name")))
|
res, err := dbClient.Query(fmt.Sprintf(SQL_TABLE_SCHEMA, c.Params.ByName("table")))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.JSON(400, NewError(err))
|
c.JSON(400, NewError(err))
|
||||||
@ -59,6 +59,17 @@ func API_Info(c *gin.Context) {
|
|||||||
c.JSON(200, res.Format()[0])
|
c.JSON(200, res.Format()[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func API_TableIndexes(c *gin.Context) {
|
||||||
|
res, err := dbClient.TableIndexes(c.Params.ByName("table"))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(400, NewError(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
c.JSON(200, res)
|
||||||
|
}
|
||||||
|
|
||||||
func API_HandleQuery(query string, c *gin.Context) {
|
func API_HandleQuery(query string, c *gin.Context) {
|
||||||
result, err := dbClient.Query(query)
|
result, err := dbClient.Query(query)
|
||||||
|
|
||||||
|
17
client.go
17
client.go
@ -9,9 +9,10 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
SQL_INFO = "SELECT version(), user, current_database(), inet_client_addr(), inet_client_port(), inet_server_addr(), inet_server_port()"
|
SQL_INFO = "SELECT version(), user, current_database(), inet_client_addr(), inet_client_port(), inet_server_addr(), inet_server_port()"
|
||||||
SQL_TABLES = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' ORDER BY table_schema,table_name;"
|
SQL_TABLES = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' ORDER BY table_schema,table_name;"
|
||||||
SQL_TABLE_SCHEMA = "SELECT column_name, data_type, is_nullable, character_maximum_length, character_set_catalog, column_default FROM information_schema.columns where table_name = '%s';"
|
SQL_TABLE_SCHEMA = "SELECT column_name, data_type, is_nullable, character_maximum_length, character_set_catalog, column_default FROM information_schema.columns where table_name = '%s';"
|
||||||
|
SQL_TABLE_INDEXES = "SELECT indexname, indexdef FROM pg_indexes WHERE tablename = '%s';"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Client struct {
|
type Client struct {
|
||||||
@ -58,6 +59,16 @@ func (client *Client) Tables() ([]string, error) {
|
|||||||
return tables, nil
|
return tables, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (client *Client) TableIndexes(table string) (*Result, error) {
|
||||||
|
res, err := client.Query(fmt.Sprintf(SQL_TABLE_INDEXES, table))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return res, err
|
||||||
|
}
|
||||||
|
|
||||||
func (client *Client) Query(query string) (*Result, error) {
|
func (client *Client) Query(query string) (*Result, error) {
|
||||||
rows, err := client.db.Queryx(query)
|
rows, err := client.db.Queryx(query)
|
||||||
|
|
||||||
|
3
main.go
3
main.go
@ -68,7 +68,8 @@ func main() {
|
|||||||
|
|
||||||
router.GET("/info", API_Info)
|
router.GET("/info", API_Info)
|
||||||
router.GET("/tables", API_GetTables)
|
router.GET("/tables", API_GetTables)
|
||||||
router.GET("/tables/:name", API_GetTable)
|
router.GET("/tables/:table", API_GetTable)
|
||||||
|
router.GET("/tables/:table/indexes", API_TableIndexes)
|
||||||
router.GET("/query", API_RunQuery)
|
router.GET("/query", API_RunQuery)
|
||||||
router.POST("/query", API_RunQuery)
|
router.POST("/query", API_RunQuery)
|
||||||
router.GET("/history", API_History)
|
router.GET("/history", API_History)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user