diff --git a/api.go b/api.go index f49405e..272b967 100644 --- a/api.go +++ b/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")) diff --git a/client.go b/client.go index ef73c18..6b89775 100644 --- a/client.go +++ b/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) diff --git a/main.go b/main.go index 926631d..cda6472 100644 --- a/main.go +++ b/main.go @@ -85,6 +85,7 @@ func main() { router := gin.Default() router.GET("/", API_Home) + router.GET("/databases", API_GetDatabases) router.GET("/info", API_Info) router.GET("/tables", API_GetTables) router.GET("/tables/:table", API_GetTable)