mirror of
https://github.com/sosedoff/pgweb.git
synced 2024-12-15 03:36:33 +03:00
Merge pull request #104 from brianlow/constraints
Show table constraints
This commit is contained in:
commit
bc1f876fb5
@ -179,6 +179,11 @@ func GetTableIndexes(c *gin.Context) {
|
||||
serveResult(res, err, c)
|
||||
}
|
||||
|
||||
func GetTableConstraints(c *gin.Context) {
|
||||
res, err := DbClient.TableConstraints(c.Params.ByName("table"))
|
||||
serveResult(res, err, c)
|
||||
}
|
||||
|
||||
func HandleQuery(query string, c *gin.Context) {
|
||||
rawQuery, err := base64.StdEncoding.DecodeString(query)
|
||||
if err == nil {
|
||||
|
@ -33,6 +33,7 @@ func SetupRoutes(router *gin.Engine) {
|
||||
api.GET("/tables/:table/rows", GetTableRows)
|
||||
api.GET("/tables/:table/info", GetTableInfo)
|
||||
api.GET("/tables/:table/indexes", GetTableIndexes)
|
||||
api.GET("/tables/:table/constraints", GetTableConstraints)
|
||||
api.GET("/query", RunQuery)
|
||||
api.POST("/query", RunQuery)
|
||||
api.GET("/explain", ExplainQuery)
|
||||
|
@ -136,6 +136,16 @@ func (client *Client) TableIndexes(table string) (*Result, error) {
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (client *Client) TableConstraints(table string) (*Result, error) {
|
||||
res, err := client.query(statements.PG_TABLE_CONSTRAINTS, table)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return res, err
|
||||
}
|
||||
|
||||
func (client *Client) Sequences() ([]string, error) {
|
||||
return client.fetchRows(statements.PG_SEQUENCES)
|
||||
}
|
||||
|
@ -188,6 +188,14 @@ func test_TableIndexes(t *testing.T) {
|
||||
assert.Equal(t, 2, len(res.Rows))
|
||||
}
|
||||
|
||||
func test_TableConstraints(t *testing.T) {
|
||||
res, err := testClient.TableConstraints("editions")
|
||||
|
||||
assert.Equal(t, nil, err)
|
||||
assert.Equal(t, 1, len(res.Columns))
|
||||
assert.Equal(t, 2, len(res.Rows))
|
||||
}
|
||||
|
||||
func test_Sequences(t *testing.T) {
|
||||
res, err := testClient.Sequences()
|
||||
|
||||
@ -271,6 +279,7 @@ func TestAll(t *testing.T) {
|
||||
test_TableRows(t)
|
||||
test_TableInfo(t)
|
||||
test_TableIndexes(t)
|
||||
test_TableConstraints(t)
|
||||
test_Sequences(t)
|
||||
test_Query(t)
|
||||
test_QueryError(t)
|
||||
|
File diff suppressed because one or more lines are too long
@ -18,6 +18,16 @@ const (
|
||||
|
||||
PG_TABLE_INDEXES = `SELECT indexname, indexdef FROM pg_indexes WHERE tablename = $1`
|
||||
|
||||
PG_TABLE_CONSTRAINTS = `SELECT
|
||||
pg_get_constraintdef(c.oid, true) as condef
|
||||
FROM pg_constraint c
|
||||
JOIN pg_namespace n ON n.oid = c.connamespace
|
||||
JOIN pg_class cl ON cl.oid = c.conrelid
|
||||
WHERE n.nspname = 'public'
|
||||
AND relname = $1
|
||||
ORDER BY contype desc`
|
||||
|
||||
|
||||
PG_TABLE_INFO = `SELECT
|
||||
pg_size_pretty(pg_table_size($1)) AS data_size
|
||||
, pg_size_pretty(pg_indexes_size($1)) AS index_size
|
||||
|
@ -22,6 +22,7 @@
|
||||
<li id="table_content">Rows</li>
|
||||
<li id="table_structure">Structure</li>
|
||||
<li id="table_indexes">Indexes</li>
|
||||
<li id="table_constraints">Constraints</li>
|
||||
<li id="table_query" class="selected">SQL Query</li>
|
||||
<li id="table_history">History</li>
|
||||
<li id="table_activity">Activity</li>
|
||||
|
@ -17,13 +17,14 @@ function apiCall(method, path, params, cb) {
|
||||
});
|
||||
}
|
||||
|
||||
function getTables(cb) { apiCall("get", "/tables", {}, cb); }
|
||||
function getTableRows(table, opts, cb) { apiCall("get", "/tables/" + table + "/rows", opts, cb); }
|
||||
function getTableStructure(table, cb) { apiCall("get", "/tables/" + table, {}, cb); }
|
||||
function getTableIndexes(table, cb) { apiCall("get", "/tables/" + table + "/indexes", {}, cb); }
|
||||
function getHistory(cb) { apiCall("get", "/history", {}, cb); }
|
||||
function getBookmarks(cb) { apiCall("get", "/bookmarks", {}, cb); }
|
||||
function getSequences(cb) { apiCall("get", "/sequences", {}, cb); }
|
||||
function getTables(cb) { apiCall("get", "/tables", {}, cb); }
|
||||
function getTableRows(table, opts, cb) { apiCall("get", "/tables/" + table + "/rows", opts, cb); }
|
||||
function getTableStructure(table, cb) { apiCall("get", "/tables/" + table, {}, cb); }
|
||||
function getTableIndexes(table, cb) { apiCall("get", "/tables/" + table + "/indexes", {}, cb); }
|
||||
function getTableConstraints(table, cb) { apiCall("get", "/tables/" + table + "/constraints", {}, cb); }
|
||||
function getHistory(cb) { apiCall("get", "/history", {}, cb); }
|
||||
function getBookmarks(cb) { apiCall("get", "/bookmarks", {}, cb); }
|
||||
function getSequences(cb) { apiCall("get", "/sequences", {}, cb); }
|
||||
|
||||
function encodeQuery(query) {
|
||||
return window.btoa(query);
|
||||
@ -201,6 +202,24 @@ function showTableIndexes() {
|
||||
});
|
||||
}
|
||||
|
||||
function showTableConstraints() {
|
||||
var name = getCurrentTable();
|
||||
|
||||
if (name.length == 0) {
|
||||
alert("Please select a table!");
|
||||
return;
|
||||
}
|
||||
|
||||
getTableConstraints(name, function(data) {
|
||||
setCurrentTab("table_constraints");
|
||||
buildTable(data);
|
||||
|
||||
$("#input").hide();
|
||||
$("#output").addClass("full");
|
||||
$("#results").addClass("no-crop");
|
||||
});
|
||||
}
|
||||
|
||||
function showTableInfo() {
|
||||
var name = getCurrentTable();
|
||||
|
||||
@ -486,13 +505,14 @@ function getConnectionString() {
|
||||
}
|
||||
|
||||
$(document).ready(function() {
|
||||
$("#table_content").on("click", function() { showTableContent(); });
|
||||
$("#table_structure").on("click", function() { showTableStructure(); });
|
||||
$("#table_indexes").on("click", function() { showTableIndexes(); });
|
||||
$("#table_history").on("click", function() { showQueryHistory(); });
|
||||
$("#table_query").on("click", function() { showQueryPanel(); });
|
||||
$("#table_connection").on("click", function() { showConnectionPanel(); });
|
||||
$("#table_activity").on("click", function() { showActivityPanel(); });
|
||||
$("#table_content").on("click", function() { showTableContent(); });
|
||||
$("#table_structure").on("click", function() { showTableStructure(); });
|
||||
$("#table_indexes").on("click", function() { showTableIndexes(); });
|
||||
$("#table_constraints").on("click", function() { showTableConstraints(); });
|
||||
$("#table_history").on("click", function() { showQueryHistory(); });
|
||||
$("#table_query").on("click", function() { showQueryPanel(); });
|
||||
$("#table_connection").on("click", function() { showConnectionPanel(); });
|
||||
$("#table_activity").on("click", function() { showActivityPanel(); });
|
||||
|
||||
$("#run").on("click", function() {
|
||||
runQuery();
|
||||
|
Loading…
Reference in New Issue
Block a user