use a different query to fetch materialized view structure

This commit is contained in:
Dan Sosedoff 2016-01-17 15:00:33 -06:00
parent 84f1bd95e7
commit 3167d96cfc
6 changed files with 75 additions and 41 deletions

View File

@ -151,7 +151,15 @@ func GetSchemas(c *gin.Context) {
} }
func GetTable(c *gin.Context) { func GetTable(c *gin.Context) {
res, err := DB(c).Table(c.Params.ByName("table")) var res *client.Result
var err error
if c.Request.FormValue("type") == "materialized_view" {
res, err = DB(c).MaterializedView(c.Params.ByName("table"))
} else {
res, err = DB(c).Table(c.Params.ByName("table"))
}
serveResult(res, err, c) serveResult(res, err, c)
} }

View File

@ -107,6 +107,10 @@ func (client *Client) Table(table string) (*Result, error) {
return client.query(statements.PG_TABLE_SCHEMA, schema, table) return client.query(statements.PG_TABLE_SCHEMA, schema, table)
} }
func (client *Client) MaterializedView(name string) (*Result, error) {
return client.query(statements.PG_MATERIALIZED_VIEW_SCHEMA, name)
}
func (client *Client) TableRows(table string, opts RowsOptions) (*Result, error) { func (client *Client) TableRows(table string, opts RowsOptions) (*Result, error) {
schema, table := getSchemaAndTable(table) schema, table := getSchemaAndTable(table)
sql := fmt.Sprintf(`SELECT * FROM "%s"."%s"`, schema, table) sql := fmt.Sprintf(`SELECT * FROM "%s"."%s"`, schema, table)

View File

@ -25,10 +25,10 @@ type Result struct {
} }
type Objects struct { type Objects struct {
Tables []string `json:"tables"` Tables []string `json:"table"`
Views []string `json:"views"` Views []string `json:"view"`
MaterializedViews []string `json:"materialized_views"` MaterializedViews []string `json:"materialized_view"`
Sequences []string `json:"sequences"` Sequences []string `json:"sequence"`
} }
// Due to big int number limitations in javascript, numbers should be encoded // Due to big int number limitations in javascript, numbers should be encoded

File diff suppressed because one or more lines are too long

View File

@ -92,6 +92,23 @@ WHERE
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
PG_MATERIALIZED_VIEW_SCHEMA = `
SELECT
attname as column_name,
atttypid::regtype AS data_type,
(case when attnotnull IS TRUE then 'NO' else 'YES' end) as is_nullable,
null as character_maximum_length,
null as character_set_catalog,
null as column_default
FROM
pg_attribute
WHERE
attrelid = $1::regclass AND
attnum > 0 AND
NOT attisdropped`
// ---------------------------------------------------------------------------
PG_ACTIVITY = ` PG_ACTIVITY = `
SELECT SELECT
datname, datname,

View File

@ -2,7 +2,7 @@ var editor = null;
var connected = false; var connected = false;
var bookmarks = {}; var bookmarks = {};
var default_rows_limit = 100; var default_rows_limit = 100;
var currentTable = null; var currentObject = null;
var filterOptions = { var filterOptions = {
"equal": "= 'DATA'", "equal": "= 'DATA'",
@ -79,7 +79,7 @@ function apiCall(method, path, params, cb) {
function getObjects(cb) { apiCall("get", "/objects", {}, cb); } function getObjects(cb) { apiCall("get", "/objects", {}, cb); }
function getTables(cb) { apiCall("get", "/tables", {}, cb); } function getTables(cb) { apiCall("get", "/tables", {}, cb); }
function getTableRows(table, opts, cb) { apiCall("get", "/tables/" + table + "/rows", opts, cb); } function getTableRows(table, opts, cb) { apiCall("get", "/tables/" + table + "/rows", opts, cb); }
function getTableStructure(table, cb) { apiCall("get", "/tables/" + table, {}, cb); } function getTableStructure(table, opts, cb) { apiCall("get", "/tables/" + table, opts, cb); }
function getTableIndexes(table, cb) { apiCall("get", "/tables/" + table + "/indexes", {}, cb); } function getTableIndexes(table, cb) { apiCall("get", "/tables/" + table + "/indexes", {}, cb); }
function getTableConstraints(table, cb) { apiCall("get", "/tables/" + table + "/constraints", {}, cb); } function getTableConstraints(table, cb) { apiCall("get", "/tables/" + table + "/constraints", {}, cb); }
function getHistory(cb) { apiCall("get", "/history", {}, cb); } function getHistory(cb) { apiCall("get", "/history", {}, cb); }
@ -95,17 +95,17 @@ function buildSchemaSection(name, objects) {
var section = ""; var section = "";
var titles = { var titles = {
"tables": "Tables", "table": "Tables",
"views": "Views", "view": "Views",
"materialized_views": "Materialized Views", "materialized_view": "Materialized Views",
"sequences": "Sequences" "sequence": "Sequences"
}; };
var icons = { var icons = {
"tables": '<i class="fa fa-table"></i>', "table": '<i class="fa fa-table"></i>',
"views": '<i class="fa fa-table"></i>', "view": '<i class="fa fa-table"></i>',
"materialized_views": '<i class="fa fa-table"></i>', "materialized_view": '<i class="fa fa-table"></i>',
"sequences": '<i class="fa fa-circle-o"></i>' "sequence": '<i class="fa fa-circle-o"></i>'
}; };
var klass = ""; var klass = "";
@ -115,11 +115,11 @@ function buildSchemaSection(name, objects) {
section += "<div class='schema-name'><i class='fa fa-folder-o'></i><i class='fa fa-folder-open-o'></i> " + name + "</div>"; section += "<div class='schema-name'><i class='fa fa-folder-o'></i><i class='fa fa-folder-open-o'></i> " + name + "</div>";
section += "<div class='schema-container'>"; section += "<div class='schema-container'>";
for (group of ["tables", "views", "materialized_views", "sequences"]) { for (group of ["table", "view", "materialized_view", "sequence"]) {
if (objects[group].length == 0) continue; if (objects[group].length == 0) continue;
group_klass = ""; group_klass = "";
if (name == "public" && group == "tables") group_klass = "expanded"; if (name == "public" && group == "table") group_klass = "expanded";
section += "<div class='schema-group " + group_klass + "'>"; section += "<div class='schema-group " + group_klass + "'>";
section += "<div class='schema-group-title'><i class='fa fa-chevron-right'></i><i class='fa fa-chevron-down'></i> " + titles[group] + " (" + objects[group].length + ")</div>"; section += "<div class='schema-group-title'><i class='fa fa-chevron-right'></i><i class='fa fa-chevron-down'></i> " + titles[group] + " (" + objects[group].length + ")</div>";
@ -167,8 +167,8 @@ function unescapeHtml(str){
return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue; return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
} }
function getCurrentTable() { function getCurrentObject() {
return currentTable; return currentObject || { name: "", type: "" };
} }
function resetTable() { function resetTable() {
@ -280,7 +280,7 @@ function showQueryHistory() {
} }
function showTableIndexes() { function showTableIndexes() {
var name = getCurrentTable(); var name = getCurrentObject().name;
if (name.length == 0) { if (name.length == 0) {
alert("Please select a table!"); alert("Please select a table!");
@ -298,7 +298,7 @@ function showTableIndexes() {
} }
function showTableConstraints() { function showTableConstraints() {
var name = getCurrentTable(); var name = getCurrentObject().name;
if (name.length == 0) { if (name.length == 0) {
alert("Please select a table!"); alert("Please select a table!");
@ -316,7 +316,7 @@ function showTableConstraints() {
} }
function showTableInfo() { function showTableInfo() {
var name = getCurrentTable(); var name = getCurrentObject().name;
if (name.length == 0) { if (name.length == 0) {
alert("Please select a table!"); alert("Please select a table!");
@ -332,7 +332,7 @@ function showTableInfo() {
$("#table_encoding").text("Unknown"); $("#table_encoding").text("Unknown");
}); });
buildTableFilters(name); buildTableFilters(name, getCurrentObject().type);
} }
function updatePaginator(pagination) { function updatePaginator(pagination) {
@ -367,7 +367,7 @@ function updatePaginator(pagination) {
} }
function showTableContent(sortColumn, sortOrder) { function showTableContent(sortColumn, sortOrder) {
var name = getCurrentTable(); var name = getCurrentObject().name;
if (name.length == 0) { if (name.length == 0) {
alert("Please select a table!"); alert("Please select a table!");
@ -409,7 +409,7 @@ function showTableContent(sortColumn, sortOrder) {
} }
function showTableStructure() { function showTableStructure() {
var name = getCurrentTable(); var name = getCurrentObject().name;
if (name.length == 0) { if (name.length == 0) {
alert("Please select a table!"); alert("Please select a table!");
@ -421,7 +421,9 @@ function showTableStructure() {
$("#input").hide(); $("#input").hide();
$("#body").prop("class", "full"); $("#body").prop("class", "full");
getTableStructure(name, function(data) { console.log(getCurrentObject());
getTableStructure(name, { type: getCurrentObject().type }, function(data) {
buildTable(data); buildTable(data);
$("#results").addClass("no-crop"); $("#results").addClass("no-crop");
}); });
@ -539,8 +541,8 @@ function exportTo(format) {
win.focus(); win.focus();
} }
function buildTableFilters(name) { function buildTableFilters(name, type) {
getTableStructure(name, function(data) { getTableStructure(name, { type: type }, function(data) {
if (data.rows.length == 0) { if (data.rows.length == 0) {
$("#pagination .filters").hide(); $("#pagination .filters").hide();
} }
@ -733,7 +735,10 @@ $(document).ready(function() {
}); });
$("#objects").on("click", "li", function(e) { $("#objects").on("click", "li", function(e) {
currentTable = $(this).data("id"); currentObject = {
name: $(this).data("id"),
type: $(this).data("type")
};
$("#objects li").removeClass("active"); $("#objects li").removeClass("active");
$(this).addClass("active"); $(this).addClass("active");