2014-10-19 19:06:35 +04:00
|
|
|
var editor;
|
|
|
|
|
2014-10-17 01:58:32 +04:00
|
|
|
function apiCall(method, path, params, cb) {
|
2014-10-13 22:55:19 +04:00
|
|
|
$.ajax({
|
2014-10-17 01:58:32 +04:00
|
|
|
url: path,
|
|
|
|
method: method,
|
2014-10-13 22:55:19 +04:00
|
|
|
cache: false,
|
2014-10-17 01:58:32 +04:00
|
|
|
data: params,
|
2014-10-13 22:55:19 +04:00
|
|
|
success: function(data) {
|
|
|
|
cb(data);
|
|
|
|
},
|
|
|
|
error: function(xhr, status, data) {
|
|
|
|
cb(jQuery.parseJSON(xhr.responseText));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-17 01:58:32 +04:00
|
|
|
function getTables(cb) { apiCall("get", "/tables", {}, 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 executeQuery(query, cb) {
|
|
|
|
apiCall("post", "/query", { query: query }, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
function explainQuery(query, cb) {
|
|
|
|
apiCall("post", "/explain", { query: query }, cb);
|
|
|
|
}
|
|
|
|
|
2014-10-13 22:55:19 +04:00
|
|
|
function loadTables() {
|
|
|
|
getTables(function(data) {
|
|
|
|
data.forEach(function(item) {
|
|
|
|
$("<li>" + item + "</li>").appendTo("#tables");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-14 04:49:43 +04:00
|
|
|
function escapeHtml(str) {
|
|
|
|
if (str != null || str != undefined) {
|
|
|
|
return jQuery("<div/>").text(str).html();
|
|
|
|
}
|
|
|
|
|
|
|
|
return "<span class='null'>null</span>";
|
|
|
|
}
|
|
|
|
|
2014-10-18 18:57:55 +04:00
|
|
|
function resetTable() {
|
|
|
|
$("#results").
|
|
|
|
attr("data-mode", "").
|
|
|
|
text("").
|
2014-10-26 00:18:30 +04:00
|
|
|
removeClass("empty").
|
|
|
|
removeClass("history");
|
2014-10-18 18:57:55 +04:00
|
|
|
}
|
|
|
|
|
2014-10-13 22:55:19 +04:00
|
|
|
function buildTable(results) {
|
2014-10-18 18:57:55 +04:00
|
|
|
resetTable();
|
2014-10-13 22:55:19 +04:00
|
|
|
|
|
|
|
if (results.error) {
|
|
|
|
$("<tr><td>ERROR: " + results.error + "</tr></tr>").appendTo("#results");
|
|
|
|
$("#results").addClass("empty");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!results.rows) {
|
|
|
|
$("<tr><td>No records found</tr></tr>").appendTo("#results");
|
|
|
|
$("#results").addClass("empty");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var cols = "";
|
2014-10-26 00:18:30 +04:00
|
|
|
var rows = "";
|
2014-10-13 22:55:19 +04:00
|
|
|
|
|
|
|
results.columns.forEach(function(col) {
|
2014-10-18 18:55:55 +04:00
|
|
|
cols += "<th data='" + col + "'>" + col + "</th>";
|
2014-10-13 22:55:19 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
results.rows.forEach(function(row) {
|
|
|
|
var r = "";
|
2014-10-14 04:49:43 +04:00
|
|
|
for (i in row) { r += "<td><div>" + escapeHtml(row[i]) + "</div></td>"; }
|
2014-10-13 22:55:19 +04:00
|
|
|
rows += "<tr>" + r + "</tr>";
|
|
|
|
});
|
|
|
|
|
|
|
|
$("<thead>" + cols + "</thead><tbody>" + rows + "</tobdy>").appendTo("#results");
|
|
|
|
}
|
|
|
|
|
|
|
|
function setCurrentTab(id) {
|
|
|
|
$("#nav ul li.selected").removeClass("selected");
|
|
|
|
$("#" + id).addClass("selected");
|
|
|
|
}
|
|
|
|
|
|
|
|
function showQueryHistory() {
|
|
|
|
getHistory(function(data) {
|
|
|
|
var rows = [];
|
|
|
|
|
|
|
|
for(i in data) {
|
|
|
|
rows.unshift([parseInt(i) + 1, data[i]]);
|
|
|
|
}
|
|
|
|
|
|
|
|
buildTable({ columns: ["id", "query"], rows: rows });
|
|
|
|
|
|
|
|
setCurrentTab("table_history");
|
|
|
|
$("#input").hide();
|
|
|
|
$("#output").addClass("full");
|
2014-10-26 00:18:30 +04:00
|
|
|
$("#results").addClass("history");
|
2014-10-13 22:55:19 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function showTableIndexes() {
|
|
|
|
var name = $("#tables li.selected").text();
|
|
|
|
|
|
|
|
if (name.length == 0) {
|
|
|
|
alert("Please select a table!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
getTableIndexes(name, function(data) {
|
|
|
|
setCurrentTab("table_indexes");
|
|
|
|
buildTable(data);
|
|
|
|
|
|
|
|
$("#input").hide();
|
|
|
|
$("#output").addClass("full");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-18 07:33:33 +04:00
|
|
|
function showTableInfo() {
|
|
|
|
var name = $("#tables li.selected").text();
|
|
|
|
|
|
|
|
if (name.length == 0) {
|
|
|
|
alert("Please select a table!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
apiCall("get", "/tables/" + name + "/info", {}, function(data) {
|
2014-10-27 01:16:35 +03:00
|
|
|
$(".table-information ul").show();
|
|
|
|
$("#table_total_size").text(data.total_size);
|
|
|
|
$("#table_data_size").text(data.data_size);
|
|
|
|
$("#table_index_size").text(data.index_size);
|
|
|
|
$("#table_rows_count").text(data.rows_count);
|
|
|
|
$("#table_encoding").text("Unknown");
|
2014-10-18 07:33:33 +04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-13 22:55:19 +04:00
|
|
|
function showTableContent() {
|
|
|
|
var name = $("#tables li.selected").text();
|
|
|
|
|
|
|
|
if (name.length == 0) {
|
|
|
|
alert("Please select a table!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-28 05:04:42 +03:00
|
|
|
var query = "SELECT * FROM \"" + name + "\" LIMIT 100;";
|
2014-10-13 22:55:19 +04:00
|
|
|
|
|
|
|
executeQuery(query, function(data) {
|
|
|
|
buildTable(data);
|
|
|
|
setCurrentTab("table_content");
|
|
|
|
|
2014-10-18 18:57:55 +04:00
|
|
|
$("#results").attr("data-mode", "browse");
|
2014-10-13 22:55:19 +04:00
|
|
|
$("#input").hide();
|
|
|
|
$("#output").addClass("full");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function showTableStructure() {
|
|
|
|
var name = $("#tables li.selected").text();
|
|
|
|
|
|
|
|
if (name.length == 0) {
|
|
|
|
alert("Please select a table!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
getTableStructure(name, function(data) {
|
|
|
|
setCurrentTab("table_structure");
|
|
|
|
buildTable(data);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-28 19:28:44 +03:00
|
|
|
function showQueryPanel() {
|
|
|
|
setCurrentTab("table_query");
|
|
|
|
editor.focus();
|
|
|
|
|
|
|
|
$("#input").show();
|
|
|
|
$("#output").removeClass("full");
|
|
|
|
}
|
|
|
|
|
2014-10-13 22:55:19 +04:00
|
|
|
function runQuery() {
|
|
|
|
setCurrentTab("table_query");
|
|
|
|
|
2014-10-28 19:46:35 +03:00
|
|
|
$("#run, #explain, #csv").prop("disabled", true);
|
2014-10-14 05:12:13 +04:00
|
|
|
$("#query_progress").show();
|
|
|
|
|
2014-10-15 06:32:46 +04:00
|
|
|
var query = $.trim(editor.getValue());
|
|
|
|
|
|
|
|
if (query.length == 0) {
|
2014-10-28 19:46:35 +03:00
|
|
|
$("#run, #explain, #csv").prop("disabled", false);
|
2014-10-17 01:58:32 +04:00
|
|
|
$("#query_progress").hide();
|
2014-10-15 06:32:46 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
executeQuery(query, function(data) {
|
2014-10-13 22:55:19 +04:00
|
|
|
buildTable(data);
|
2014-10-14 05:12:13 +04:00
|
|
|
|
2014-10-28 19:46:35 +03:00
|
|
|
$("#run, #explain, #csv").prop("disabled", false);
|
2014-10-15 06:32:46 +04:00
|
|
|
$("#query_progress").hide();
|
|
|
|
$("#input").show();
|
|
|
|
$("#output").removeClass("full");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function runExplain() {
|
|
|
|
setCurrentTab("table_query");
|
|
|
|
|
2014-10-28 19:46:35 +03:00
|
|
|
$("#run, #explain, #csv").prop("disabled", true);
|
2014-10-15 06:32:46 +04:00
|
|
|
$("#query_progress").show();
|
|
|
|
|
|
|
|
var query = $.trim(editor.getValue());
|
|
|
|
|
|
|
|
if (query.length == 0) {
|
2014-10-28 19:46:35 +03:00
|
|
|
$("#run, #explain, #csv").prop("disabled", false);
|
2014-10-17 01:58:32 +04:00
|
|
|
$("#query_progress").hide();
|
2014-10-15 06:32:46 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-17 01:58:32 +04:00
|
|
|
explainQuery(query, function(data) {
|
2014-10-15 06:32:46 +04:00
|
|
|
buildTable(data);
|
|
|
|
|
2014-10-28 19:46:35 +03:00
|
|
|
$("#run, #explain, #csv").prop("disabled", false);
|
2014-10-14 05:12:13 +04:00
|
|
|
$("#query_progress").hide();
|
2014-10-13 22:55:19 +04:00
|
|
|
$("#input").show();
|
|
|
|
$("#output").removeClass("full");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-18 19:07:19 +04:00
|
|
|
function exportToCSV() {
|
|
|
|
var query = $.trim(editor.getValue());
|
|
|
|
|
|
|
|
if (query.length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2014-10-21 18:40:45 +04:00
|
|
|
|
2014-10-30 02:35:13 +03:00
|
|
|
// Replace line breaks with spaces and properly encode query
|
|
|
|
query = window.encodeURI(query.replace(/\n/g, " "));
|
2014-10-29 20:48:11 +03:00
|
|
|
|
2014-10-25 22:03:27 +04:00
|
|
|
var url = "http://" + window.location.host + "/query?format=csv&query=" + query;
|
2014-10-21 18:40:45 +04:00
|
|
|
var win = window.open(url, '_blank');
|
2014-10-25 22:03:27 +04:00
|
|
|
|
2014-10-30 02:35:13 +03:00
|
|
|
setCurrentTab("table_query");
|
2014-10-21 18:40:45 +04:00
|
|
|
win.focus();
|
2014-10-18 19:07:19 +04:00
|
|
|
}
|
|
|
|
|
2014-10-19 19:06:35 +04:00
|
|
|
function initEditor() {
|
2014-10-13 22:55:19 +04:00
|
|
|
editor = ace.edit("custom_query");
|
2014-10-19 19:06:35 +04:00
|
|
|
|
2014-10-13 22:55:19 +04:00
|
|
|
editor.getSession().setMode("ace/mode/pgsql");
|
|
|
|
editor.getSession().setTabSize(2);
|
|
|
|
editor.getSession().setUseSoftTabs(true);
|
2014-10-30 02:53:20 +03:00
|
|
|
|
|
|
|
editor.commands.addCommand({
|
|
|
|
name: "run_query",
|
|
|
|
bindKey: {
|
|
|
|
win: "Ctrl-Enter",
|
|
|
|
mac: "Command-Enter"
|
|
|
|
},
|
|
|
|
exec: function(editor) {
|
|
|
|
runQuery();
|
|
|
|
}
|
|
|
|
});
|
2014-10-19 19:06:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
$(document).ready(function() {
|
|
|
|
initEditor();
|
2014-10-13 22:55:19 +04:00
|
|
|
|
2014-10-17 01:47:37 +04:00
|
|
|
$("#table_content").on("click", function() { showTableContent(); });
|
|
|
|
$("#table_structure").on("click", function() { showTableStructure(); });
|
|
|
|
$("#table_indexes").on("click", function() { showTableIndexes(); });
|
|
|
|
$("#table_history").on("click", function() { showQueryHistory(); });
|
2014-10-28 19:28:44 +03:00
|
|
|
$("#table_query").on("click", function() { showQueryPanel(); });
|
2014-10-13 22:55:19 +04:00
|
|
|
|
|
|
|
$("#run").on("click", function() {
|
|
|
|
runQuery();
|
|
|
|
});
|
|
|
|
|
2014-10-15 06:32:46 +04:00
|
|
|
$("#explain").on("click", function() {
|
|
|
|
runExplain();
|
|
|
|
});
|
|
|
|
|
2014-10-18 19:07:19 +04:00
|
|
|
$("#csv").on("click", function() {
|
|
|
|
exportToCSV();
|
|
|
|
});
|
|
|
|
|
2014-10-13 22:55:19 +04:00
|
|
|
$("#results").on("click", "tr", function() {
|
|
|
|
$("#results tr.selected").removeClass();
|
|
|
|
$(this).addClass("selected");
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#tables").on("click", "li", function() {
|
|
|
|
$("#tables li.selected").removeClass("selected");
|
|
|
|
$(this).addClass("selected");
|
|
|
|
showTableContent();
|
2014-10-27 01:16:35 +03:00
|
|
|
showTableInfo();
|
2014-10-13 22:55:19 +04:00
|
|
|
});
|
|
|
|
|
|
|
|
loadTables();
|
|
|
|
});
|