Bring back /databases endpoint

This commit is contained in:
Dan Sosedoff 2014-10-15 16:05:23 -05:00
parent 14f4949efd
commit 5886f153f4
3 changed files with 29 additions and 0 deletions

11
api.go
View File

@ -34,6 +34,17 @@ func API_Home(c *gin.Context) {
c.Data(200, "text/html; charset=utf-8", data) 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) { func API_RunQuery(c *gin.Context) {
query := strings.TrimSpace(c.Request.FormValue("query")) query := strings.TrimSpace(c.Request.FormValue("query"))

View File

@ -10,6 +10,7 @@ import (
const ( const (
PG_INFO = "SELECT version(), user, current_database(), inet_client_addr(), inet_client_port(), inet_server_addr(), inet_server_port()" 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_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_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';" 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) 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) { func (client *Client) Tables() ([]string, error) {
res, err := client.Query(PG_TABLES) res, err := client.Query(PG_TABLES)

View File

@ -85,6 +85,7 @@ func main() {
router := gin.Default() router := gin.Default()
router.GET("/", API_Home) router.GET("/", API_Home)
router.GET("/databases", API_GetDatabases)
router.GET("/info", API_Info) router.GET("/info", API_Info)
router.GET("/tables", API_GetTables) router.GET("/tables", API_GetTables)
router.GET("/tables/:table", API_GetTable) router.GET("/tables/:table", API_GetTable)