From f0552ab72d6828c79c6c39a2e3a93d86366480b4 Mon Sep 17 00:00:00 2001 From: Dan Sosedoff Date: Mon, 30 Mar 2015 23:58:04 -0500 Subject: [PATCH] Add /api/schemas endpoint to list all database schemas --- api.go | 12 ++++++++++++ client.go | 4 ++++ statements.go | 2 ++ 3 files changed, 18 insertions(+) diff --git a/api.go b/api.go index 142a7e5..4bb29b4 100644 --- a/api.go +++ b/api.go @@ -55,6 +55,7 @@ func setupRoutes(router *gin.Engine) { api.GET("/databases", API_GetDatabases) api.GET("/connection", API_ConnectionInfo) api.GET("/activity", API_Activity) + api.GET("/schemas", API_GetSchemas) api.GET("/tables", API_GetTables) api.GET("/tables/:table", API_GetTable) api.GET("/tables/:table/rows", API_GetTableRows) @@ -189,6 +190,17 @@ func API_ExplainQuery(c *gin.Context) { API_HandleQuery(fmt.Sprintf("EXPLAIN ANALYZE %s", query), c) } +func API_GetSchemas(c *gin.Context) { + names, err := dbClient.Schemas() + + if err != nil { + c.JSON(400, NewError(err)) + return + } + + c.JSON(200, names) +} + func API_GetTables(c *gin.Context) { names, err := dbClient.Tables() diff --git a/client.go b/client.go index c786056..be40c8f 100644 --- a/client.go +++ b/client.go @@ -87,6 +87,10 @@ func (client *Client) Databases() ([]string, error) { return client.fetchRows(PG_DATABASES) } +func (client *Client) Schemas() ([]string, error) { + return client.fetchRows(PG_SCHEMAS) +} + func (client *Client) Tables() ([]string, error) { return client.fetchRows(PG_TABLES) } diff --git a/statements.go b/statements.go index f4ef943..277f9b9 100644 --- a/statements.go +++ b/statements.go @@ -3,6 +3,8 @@ package main const ( PG_DATABASES = `SELECT datname FROM pg_database WHERE NOT datistemplate ORDER BY datname ASC` + PG_SCHEMAS = `SELECT schema_name FROM information_schema.schemata ORDER BY schema_name ASC` + PG_INFO = `SELECT session_user , current_user