Bring back /databases endpoint
This commit is contained in:
parent
14f4949efd
commit
5886f153f4
11
api.go
11
api.go
@ -34,6 +34,17 @@ func API_Home(c *gin.Context) {
|
||||
c.Data(200, "text/html; charset=utf-8", data)
|
||||
}
|
||||
|
||||
func API_GetDatabases(c *gin.Context) {
|
||||
names, err := dbClient.Databases()
|
||||
|
||||
if err != nil {
|
||||
c.JSON(400, NewError(err))
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(200, names)
|
||||
}
|
||||
|
||||
func API_RunQuery(c *gin.Context) {
|
||||
query := strings.TrimSpace(c.Request.FormValue("query"))
|
||||
|
||||
|
17
client.go
17
client.go
@ -10,6 +10,7 @@ import (
|
||||
|
||||
const (
|
||||
PG_INFO = "SELECT version(), user, current_database(), inet_client_addr(), inet_client_port(), inet_server_addr(), inet_server_port()"
|
||||
PG_DATABASES = "SELECT datname FROM pg_database WHERE datistemplate = false ORDER BY datname ASC;"
|
||||
PG_TABLES = "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public' ORDER BY table_schema,table_name;"
|
||||
PG_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';"
|
||||
PG_TABLE_INDEXES = "SELECT indexname, indexdef FROM pg_indexes WHERE tablename = '%s';"
|
||||
@ -47,6 +48,22 @@ func (client *Client) recordQuery(query string) {
|
||||
client.history = append(client.history, query)
|
||||
}
|
||||
|
||||
func (client *Client) Databases() ([]string, error) {
|
||||
res, err := client.Query(PG_DATABASES)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var tables []string
|
||||
|
||||
for _, row := range res.Rows {
|
||||
tables = append(tables, row[0].(string))
|
||||
}
|
||||
|
||||
return tables, nil
|
||||
}
|
||||
|
||||
func (client *Client) Tables() ([]string, error) {
|
||||
res, err := client.Query(PG_TABLES)
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user