2014-10-19 09:06:35 -06:00
|
|
|
var editor;
|
2014-11-01 20:06:15 -05:00
|
|
|
var connected = false;
|
2014-12-06 20:04:32 -06:00
|
|
|
var bookmarks = {};
|
2014-10-19 09:06:35 -06:00
|
|
|
|
2014-10-16 16:58:32 -05:00
|
|
|
function apiCall(method, path, params, cb) {
|
2014-10-13 13:55:19 -05:00
|
|
|
$.ajax({
|
2015-04-09 08:49:17 +02:00
|
|
|
url: "/api" + path,
|
2014-10-16 16:58:32 -05:00
|
|
|
method: method,
|
2014-10-13 13:55:19 -05:00
|
|
|
cache: false,
|
2014-10-16 16:58:32 -05:00
|
|
|
data: params,
|
2014-10-13 13:55:19 -05:00
|
|
|
success: function(data) {
|
|
|
|
cb(data);
|
|
|
|
},
|
|
|
|
error: function(xhr, status, data) {
|
|
|
|
cb(jQuery.parseJSON(xhr.responseText));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-01-03 20:32:06 -06:00
|
|
|
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); }
|
2015-11-13 22:34:53 +09:00
|
|
|
function getSequences(cb) { apiCall("get", "/sequences", {}, cb); }
|
2014-10-16 16:58:32 -05:00
|
|
|
|
2015-08-15 20:53:51 -05:00
|
|
|
function encodeQuery(query) {
|
|
|
|
return window.btoa(query);
|
|
|
|
}
|
|
|
|
|
2014-10-16 16:58:32 -05:00
|
|
|
function executeQuery(query, cb) {
|
|
|
|
apiCall("post", "/query", { query: query }, cb);
|
|
|
|
}
|
|
|
|
|
|
|
|
function explainQuery(query, cb) {
|
|
|
|
apiCall("post", "/explain", { query: query }, cb);
|
|
|
|
}
|
|
|
|
|
2014-10-13 13:55:19 -05:00
|
|
|
function loadTables() {
|
2014-11-01 15:44:24 -05:00
|
|
|
$("#tables li").remove();
|
|
|
|
|
2014-10-13 13:55:19 -05:00
|
|
|
getTables(function(data) {
|
2015-11-13 22:34:53 +09:00
|
|
|
data.forEach(function(item) {
|
|
|
|
$("<li><span><i class='fa fa-table'></i> " + item + " </span></li>").appendTo("#tables");
|
2015-11-13 22:26:05 +09:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadSequences() {
|
|
|
|
$("#sequences li").remove();
|
|
|
|
|
|
|
|
getSequences(function(data) {
|
2015-11-13 22:34:53 +09:00
|
|
|
data.forEach(function(item) {
|
2015-11-13 22:26:05 +09:00
|
|
|
$("<li><span><i class='fa fa-chevron-right'></i> " + item + " </span></li>").appendTo("#sequences");
|
2014-10-13 13:55:19 -05:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-13 19:49:43 -05:00
|
|
|
function escapeHtml(str) {
|
|
|
|
if (str != null || str != undefined) {
|
|
|
|
return jQuery("<div/>").text(str).html();
|
|
|
|
}
|
|
|
|
|
|
|
|
return "<span class='null'>null</span>";
|
|
|
|
}
|
|
|
|
|
2014-11-03 18:37:51 -06:00
|
|
|
function unescapeHtml(str){
|
|
|
|
var e = document.createElement("div");
|
|
|
|
e.innerHTML = str;
|
|
|
|
return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
|
|
|
|
}
|
|
|
|
|
2014-12-01 20:51:20 -06:00
|
|
|
function getCurrentTable() {
|
2014-12-01 20:52:46 -06:00
|
|
|
return $("#tables").attr("data-current") || "";
|
2014-12-01 20:51:20 -06:00
|
|
|
}
|
|
|
|
|
2014-10-18 07:57:55 -07:00
|
|
|
function resetTable() {
|
|
|
|
$("#results").
|
|
|
|
attr("data-mode", "").
|
|
|
|
text("").
|
2014-10-25 15:18:30 -05:00
|
|
|
removeClass("empty").
|
2014-11-03 17:55:43 -06:00
|
|
|
removeClass("no-crop");
|
2014-10-18 07:57:55 -07:00
|
|
|
}
|
|
|
|
|
2015-05-19 12:24:52 -05:00
|
|
|
function performTableAction(table, action) {
|
|
|
|
if (action == "truncate" || action == "delete") {
|
|
|
|
var message = "Are you sure you want to " + action + " table " + table + " ?";
|
|
|
|
if (!confirm(message)) return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(action) {
|
|
|
|
case "truncate":
|
|
|
|
executeQuery("TRUNCATE TABLE " + table, function(data) {
|
|
|
|
if (data.error) alert(data.error);
|
|
|
|
resetTable();
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "delete":
|
|
|
|
executeQuery("DROP TABLE " + table, function(data) {
|
|
|
|
if (data.error) alert(data.error);
|
|
|
|
loadTables();
|
2015-11-13 22:34:53 +09:00
|
|
|
loadSequences();
|
2015-05-19 12:24:52 -05:00
|
|
|
resetTable();
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
case "export":
|
|
|
|
var filename = table + ".csv"
|
|
|
|
var query = window.encodeURI("SELECT * FROM " + table);
|
|
|
|
var url = "http://" + window.location.host + "/api/query?format=csv&filename=" + filename + "&query=" + query;
|
|
|
|
var win = window.open(url, "_blank");
|
|
|
|
win.focus();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-09 08:59:10 +02:00
|
|
|
function sortArrow(direction) {
|
2015-05-19 12:24:52 -05:00
|
|
|
switch (direction) {
|
|
|
|
case "ASC":
|
|
|
|
return "▲";
|
|
|
|
case "DESC":
|
|
|
|
return "▼";
|
|
|
|
default:
|
|
|
|
return "";
|
|
|
|
}
|
2015-04-09 08:59:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function buildTable(results, sortColumn, sortOrder) {
|
2014-10-18 07:57:55 -07:00
|
|
|
resetTable();
|
2014-10-13 13:55:19 -05: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-25 15:18:30 -05:00
|
|
|
var rows = "";
|
2014-10-13 13:55:19 -05:00
|
|
|
|
|
|
|
results.columns.forEach(function(col) {
|
2015-04-28 10:01:34 -05:00
|
|
|
if (col === sortColumn) {
|
|
|
|
cols += "<th class='active' data='" + col + "'" + "data-sort-order=" + sortOrder + ">" + col + " " + sortArrow(sortOrder) + "</th>";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
cols += "<th data='" + col + "'>" + col + "</th>";
|
|
|
|
}
|
2014-10-13 13:55:19 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
results.rows.forEach(function(row) {
|
|
|
|
var r = "";
|
2014-10-13 19:49:43 -05:00
|
|
|
for (i in row) { r += "<td><div>" + escapeHtml(row[i]) + "</div></td>"; }
|
2014-10-13 13:55:19 -05: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) {
|
2015-01-04 18:11:13 -06:00
|
|
|
rows.unshift([parseInt(i) + 1, data[i].query, data[i].timestamp]);
|
2014-10-13 13:55:19 -05:00
|
|
|
}
|
|
|
|
|
2015-01-04 18:11:13 -06:00
|
|
|
buildTable({ columns: ["id", "query", "timestamp"], rows: rows });
|
2015-04-09 08:49:17 +02:00
|
|
|
|
|
|
|
setCurrentTab("table_history");
|
2014-10-13 13:55:19 -05:00
|
|
|
$("#input").hide();
|
|
|
|
$("#output").addClass("full");
|
2014-11-03 17:55:43 -06:00
|
|
|
$("#results").addClass("no-crop");
|
2014-10-13 13:55:19 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function showTableIndexes() {
|
2014-12-01 20:51:20 -06:00
|
|
|
var name = getCurrentTable();
|
2014-10-13 13:55:19 -05:00
|
|
|
|
|
|
|
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-11-20 19:32:54 -06:00
|
|
|
$("#results").addClass("no-crop");
|
2014-10-13 13:55:19 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-17 20:33:33 -07:00
|
|
|
function showTableInfo() {
|
2014-12-01 20:51:20 -06:00
|
|
|
var name = getCurrentTable();
|
2014-10-17 20:33:33 -07:00
|
|
|
|
|
|
|
if (name.length == 0) {
|
|
|
|
alert("Please select a table!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
apiCall("get", "/tables/" + name + "/info", {}, function(data) {
|
2014-10-26 17:16:35 -05: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-17 20:33:33 -07:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-04-09 08:59:10 +02:00
|
|
|
function showTableContent(sortColumn, sortOrder) {
|
2014-12-01 20:51:20 -06:00
|
|
|
var name = getCurrentTable();
|
2014-10-13 13:55:19 -05:00
|
|
|
|
|
|
|
if (name.length == 0) {
|
|
|
|
alert("Please select a table!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-04-09 08:59:10 +02:00
|
|
|
getTableRows(name, { limit: 100, sort_column: sortColumn, sort_order: sortOrder }, function(data) {
|
|
|
|
buildTable(data, sortColumn, sortOrder);
|
2014-10-13 13:55:19 -05:00
|
|
|
setCurrentTab("table_content");
|
|
|
|
|
2014-10-18 07:57:55 -07:00
|
|
|
$("#results").attr("data-mode", "browse");
|
2014-10-13 13:55:19 -05:00
|
|
|
$("#input").hide();
|
|
|
|
$("#output").addClass("full");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function showTableStructure() {
|
2014-12-01 20:51:20 -06:00
|
|
|
var name = getCurrentTable();
|
2014-10-13 13:55:19 -05:00
|
|
|
|
|
|
|
if (name.length == 0) {
|
|
|
|
alert("Please select a table!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
getTableStructure(name, function(data) {
|
|
|
|
setCurrentTab("table_structure");
|
|
|
|
buildTable(data);
|
2015-03-09 14:03:54 -05:00
|
|
|
|
|
|
|
$("#input").hide();
|
|
|
|
$("#output").addClass("full");
|
|
|
|
$("#results").addClass("no-crop");
|
2014-10-13 13:55:19 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-28 11:28:44 -05:00
|
|
|
function showQueryPanel() {
|
|
|
|
setCurrentTab("table_query");
|
|
|
|
editor.focus();
|
|
|
|
|
|
|
|
$("#input").show();
|
|
|
|
$("#output").removeClass("full");
|
|
|
|
}
|
|
|
|
|
2014-10-31 22:15:56 -05:00
|
|
|
function showConnectionPanel() {
|
|
|
|
setCurrentTab("table_connection");
|
|
|
|
|
2014-12-02 21:20:04 -06:00
|
|
|
apiCall("get", "/connection", {}, function(data) {
|
2014-10-31 22:15:56 -05:00
|
|
|
var rows = [];
|
|
|
|
|
|
|
|
for(key in data) {
|
|
|
|
rows.push([key, data[key]]);
|
|
|
|
}
|
|
|
|
|
|
|
|
buildTable({
|
|
|
|
columns: ["attribute", "value"],
|
|
|
|
rows: rows
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#input").hide();
|
|
|
|
$("#output").addClass("full");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:14:11 -05:00
|
|
|
function showActivityPanel() {
|
|
|
|
setCurrentTab("table_activity");
|
|
|
|
|
|
|
|
apiCall("get", "/activity", {}, function(data) {
|
|
|
|
buildTable(data);
|
|
|
|
$("#input").hide();
|
|
|
|
$("#output").addClass("full");
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-13 13:55:19 -05:00
|
|
|
function runQuery() {
|
|
|
|
setCurrentTab("table_query");
|
|
|
|
|
2014-10-28 22:16:35 +05:30
|
|
|
$("#run, #explain, #csv").prop("disabled", true);
|
2014-10-13 20:12:13 -05:00
|
|
|
$("#query_progress").show();
|
|
|
|
|
2015-05-18 14:58:03 -06:00
|
|
|
var query = $.trim(editor.getSelectedText() || editor.getValue());
|
2014-10-14 21:32:46 -05:00
|
|
|
|
|
|
|
if (query.length == 0) {
|
2014-10-28 22:16:35 +05:30
|
|
|
$("#run, #explain, #csv").prop("disabled", false);
|
2014-10-16 16:58:32 -05:00
|
|
|
$("#query_progress").hide();
|
2014-10-14 21:32:46 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
executeQuery(query, function(data) {
|
2014-10-13 13:55:19 -05:00
|
|
|
buildTable(data);
|
2014-10-13 20:12:13 -05:00
|
|
|
|
2014-10-28 22:16:35 +05:30
|
|
|
$("#run, #explain, #csv").prop("disabled", false);
|
2014-10-14 21:32:46 -05:00
|
|
|
$("#query_progress").hide();
|
|
|
|
$("#input").show();
|
|
|
|
$("#output").removeClass("full");
|
2014-11-04 10:07:22 -06:00
|
|
|
|
|
|
|
if (query.toLowerCase().indexOf("explain") != -1) {
|
|
|
|
$("#results").addClass("no-crop");
|
|
|
|
}
|
2014-12-01 19:28:24 -06:00
|
|
|
|
|
|
|
var re = /(create|drop) table/i;
|
|
|
|
|
|
|
|
// Refresh tables list if table was added or removed
|
|
|
|
if (query.match(re)) {
|
|
|
|
loadTables();
|
2015-11-13 22:34:53 +09:00
|
|
|
loadSequences();
|
2014-12-01 19:28:24 -06:00
|
|
|
}
|
2014-10-14 21:32:46 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function runExplain() {
|
|
|
|
setCurrentTab("table_query");
|
|
|
|
|
2014-10-28 22:16:35 +05:30
|
|
|
$("#run, #explain, #csv").prop("disabled", true);
|
2014-10-14 21:32:46 -05:00
|
|
|
$("#query_progress").show();
|
|
|
|
|
|
|
|
var query = $.trim(editor.getValue());
|
|
|
|
|
|
|
|
if (query.length == 0) {
|
2014-10-28 22:16:35 +05:30
|
|
|
$("#run, #explain, #csv").prop("disabled", false);
|
2014-10-16 16:58:32 -05:00
|
|
|
$("#query_progress").hide();
|
2014-10-14 21:32:46 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-10-16 16:58:32 -05:00
|
|
|
explainQuery(query, function(data) {
|
2014-10-14 21:32:46 -05:00
|
|
|
buildTable(data);
|
|
|
|
|
2014-10-28 22:16:35 +05:30
|
|
|
$("#run, #explain, #csv").prop("disabled", false);
|
2014-10-13 20:12:13 -05:00
|
|
|
$("#query_progress").hide();
|
2014-10-13 13:55:19 -05:00
|
|
|
$("#input").show();
|
|
|
|
$("#output").removeClass("full");
|
2014-11-03 17:55:43 -06:00
|
|
|
$("#results").addClass("no-crop");
|
2014-10-13 13:55:19 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-18 08:07:19 -07:00
|
|
|
function exportToCSV() {
|
|
|
|
var query = $.trim(editor.getValue());
|
|
|
|
|
|
|
|
if (query.length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2014-10-21 07:40:45 -07:00
|
|
|
|
2015-08-15 20:53:51 -05:00
|
|
|
var url = "http://" + window.location.host + "/api/query?format=csv&query=" + encodeQuery(query);
|
2014-10-21 07:40:45 -07:00
|
|
|
var win = window.open(url, '_blank');
|
2014-10-25 13:03:27 -05:00
|
|
|
|
2014-10-29 18:35:13 -05:00
|
|
|
setCurrentTab("table_query");
|
2014-10-21 07:40:45 -07:00
|
|
|
win.focus();
|
2014-10-18 08:07:19 -07:00
|
|
|
}
|
|
|
|
|
2014-10-19 09:06:35 -06:00
|
|
|
function initEditor() {
|
2015-08-02 19:21:42 -05:00
|
|
|
var writeQueryTimeout = null;
|
2014-10-13 13:55:19 -05:00
|
|
|
editor = ace.edit("custom_query");
|
2014-10-19 09:06:35 -06:00
|
|
|
|
2014-10-13 13:55:19 -05:00
|
|
|
editor.getSession().setMode("ace/mode/pgsql");
|
|
|
|
editor.getSession().setTabSize(2);
|
|
|
|
editor.getSession().setUseSoftTabs(true);
|
2015-07-30 22:29:08 -05:00
|
|
|
|
2014-10-30 10:42:05 +05:30
|
|
|
editor.commands.addCommands([{
|
2014-10-29 18:53:20 -05:00
|
|
|
name: "run_query",
|
|
|
|
bindKey: {
|
|
|
|
win: "Ctrl-Enter",
|
|
|
|
mac: "Command-Enter"
|
|
|
|
},
|
|
|
|
exec: function(editor) {
|
|
|
|
runQuery();
|
|
|
|
}
|
2014-10-30 10:42:05 +05:30
|
|
|
}, {
|
|
|
|
name: "explain_query",
|
|
|
|
bindKey: {
|
|
|
|
win: "Ctrl-E",
|
|
|
|
mac: "Command-E"
|
|
|
|
},
|
|
|
|
exec: function(editor) {
|
|
|
|
runExplain();
|
|
|
|
}
|
|
|
|
}]);
|
2015-07-30 22:29:08 -05:00
|
|
|
|
2015-08-02 19:21:42 -05:00
|
|
|
editor.on("change", function() {
|
|
|
|
if (writeQueryTimeout) {
|
|
|
|
clearTimeout(writeQueryTimeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
writeQueryTimeout = setTimeout(function() {
|
|
|
|
localStorage.setItem("pgweb_query", editor.getValue());
|
|
|
|
}, 1000);
|
|
|
|
});
|
|
|
|
|
2015-07-30 22:29:08 -05:00
|
|
|
var query = localStorage.getItem("pgweb_query");
|
|
|
|
if (query && query.length > 0) {
|
|
|
|
editor.setValue(query);
|
|
|
|
editor.clearSelection();
|
|
|
|
}
|
2014-10-19 09:06:35 -06:00
|
|
|
}
|
|
|
|
|
2014-10-30 18:44:49 -05:00
|
|
|
function addShortcutTooltips() {
|
|
|
|
if (navigator.userAgent.indexOf("OS X") > 0) {
|
|
|
|
$("#run").attr("title", "Shortcut: ⌘+Enter");
|
|
|
|
$("#explain").attr("title", "Shortcut: ⌘+E");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$("#run").attr("title", "Shortcut: Ctrl+Enter");
|
|
|
|
$("#explain").attr("title", "Shortcut: Ctrl+E");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-01 20:06:15 -05:00
|
|
|
function showConnectionSettings() {
|
2014-12-06 20:04:32 -06:00
|
|
|
getBookmarks(function(data) {
|
2014-12-31 18:20:40 -06:00
|
|
|
// Do not add any bookmarks if we've got an error
|
|
|
|
if (data.error) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-12-06 20:04:32 -06:00
|
|
|
if (Object.keys(data).length > 0) {
|
|
|
|
// Set bookmarks in global var
|
|
|
|
bookmarks = data;
|
|
|
|
|
|
|
|
// Remove all existing bookmark options
|
|
|
|
$("#connection_bookmarks").html("");
|
|
|
|
|
|
|
|
// Add blank option
|
|
|
|
$("<option value=''></option>").appendTo("#connection_bookmarks");
|
|
|
|
|
|
|
|
// Add all available bookmarks
|
|
|
|
for (key in data) {
|
|
|
|
$("<option value='" + key + "''>" + key + "</option>").appendTo("#connection_bookmarks");
|
|
|
|
}
|
|
|
|
|
|
|
|
$(".bookmarks").show();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
$(".bookmarks").hide();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-11-01 20:06:15 -05:00
|
|
|
$("#connection_window").show();
|
|
|
|
}
|
|
|
|
|
2014-11-06 11:25:53 -06:00
|
|
|
function getConnectionString() {
|
2014-11-20 21:15:01 -06:00
|
|
|
var url = $.trim($("#connection_url").val());
|
|
|
|
var mode = $(".connection-group-switch button.active").attr("data");
|
|
|
|
var ssl = $("#connection_ssl").val();
|
2014-11-06 11:25:53 -06:00
|
|
|
|
2014-11-20 21:15:01 -06:00
|
|
|
if (mode == "standard") {
|
2014-11-06 11:25:53 -06:00
|
|
|
var host = $("#pg_host").val();
|
|
|
|
var port = $("#pg_port").val();
|
|
|
|
var user = $("#pg_user").val();
|
2015-08-15 21:11:09 -05:00
|
|
|
var pass = encodeURIComponent($("#pg_password").val());
|
2014-11-06 11:25:53 -06:00
|
|
|
var db = $("#pg_db").val();
|
|
|
|
|
|
|
|
if (port.length == 0) {
|
|
|
|
port = "5432";
|
|
|
|
}
|
|
|
|
|
2015-04-30 11:51:02 -05:00
|
|
|
url = "postgres://" + user + ":" + pass + "@" + host + ":" + port + "/" + db + "?sslmode=" + ssl;
|
2014-11-06 11:25:53 -06:00
|
|
|
}
|
2014-11-20 21:15:01 -06:00
|
|
|
else {
|
2014-11-21 20:04:02 -06:00
|
|
|
var local = url.indexOf("localhost") != -1 || url.indexOf("127.0.0.1") != -1;
|
|
|
|
|
|
|
|
if (local && url.indexOf("sslmode") == -1) {
|
2014-11-20 21:15:01 -06:00
|
|
|
url += "?sslmode=" + ssl;
|
|
|
|
}
|
2014-11-06 11:25:53 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
2014-10-19 09:06:35 -06:00
|
|
|
$(document).ready(function() {
|
2014-10-31 22:15:56 -05: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(); });
|
|
|
|
$("#table_query").on("click", function() { showQueryPanel(); });
|
|
|
|
$("#table_connection").on("click", function() { showConnectionPanel(); });
|
2015-03-21 19:14:11 -05:00
|
|
|
$("#table_activity").on("click", function() { showActivityPanel(); });
|
2014-10-13 13:55:19 -05:00
|
|
|
|
|
|
|
$("#run").on("click", function() {
|
|
|
|
runQuery();
|
|
|
|
});
|
|
|
|
|
2014-10-14 21:32:46 -05:00
|
|
|
$("#explain").on("click", function() {
|
|
|
|
runExplain();
|
|
|
|
});
|
|
|
|
|
2014-10-18 08:07:19 -07:00
|
|
|
$("#csv").on("click", function() {
|
|
|
|
exportToCSV();
|
|
|
|
});
|
|
|
|
|
2014-10-13 13:55:19 -05:00
|
|
|
$("#results").on("click", "tr", function() {
|
|
|
|
$("#results tr.selected").removeClass();
|
|
|
|
$(this).addClass("selected");
|
|
|
|
});
|
|
|
|
|
2015-04-09 08:59:10 +02:00
|
|
|
$("#results").on("click", "th", function(e) {
|
2015-04-28 10:01:34 -05:00
|
|
|
var sortColumn = this.attributes['data'].value;
|
|
|
|
var contentTab = $('#table_content').hasClass('selected');
|
2015-04-09 08:59:10 +02:00
|
|
|
|
2015-04-28 10:01:34 -05:00
|
|
|
if (!contentTab) {
|
|
|
|
return;
|
|
|
|
}
|
2015-04-09 08:59:10 +02:00
|
|
|
|
2015-04-28 10:01:34 -05:00
|
|
|
if (this.dataset.sortOrder === "ASC") {
|
|
|
|
this.dataset.sortOrder = "DESC"
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.dataset.sortOrder = "ASC"
|
|
|
|
}
|
2015-04-09 08:59:10 +02:00
|
|
|
|
2015-04-28 10:01:34 -05:00
|
|
|
showTableContent(sortColumn, this.dataset.sortOrder);
|
2015-04-09 08:59:10 +02:00
|
|
|
});
|
|
|
|
|
2014-11-03 18:37:51 -06:00
|
|
|
$("#results").on("dblclick", "td > div", function() {
|
|
|
|
if ($(this).has("textarea").length > 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var value = unescapeHtml($(this).html());
|
2014-11-03 20:18:09 -06:00
|
|
|
if (!value) { return; }
|
2014-11-03 18:37:51 -06:00
|
|
|
|
2014-11-03 20:18:09 -06:00
|
|
|
var textarea = $("<textarea />").
|
|
|
|
text(value).
|
|
|
|
addClass("form-control").
|
|
|
|
css("width", $(this).css("width"));
|
2014-11-03 18:37:51 -06:00
|
|
|
|
|
|
|
if (value.split("\n").length >= 3) {
|
|
|
|
textarea.css("height", "200px");
|
|
|
|
}
|
|
|
|
|
|
|
|
$(this).html(textarea).css("max-height", "200px");
|
|
|
|
});
|
|
|
|
|
2014-10-13 13:55:19 -05:00
|
|
|
$("#tables").on("click", "li", function() {
|
|
|
|
$("#tables li.selected").removeClass("selected");
|
2015-11-13 22:34:53 +09:00
|
|
|
$("#sequences li.selected").removeClass("selected");
|
2014-10-13 13:55:19 -05:00
|
|
|
$(this).addClass("selected");
|
2014-12-01 20:51:20 -06:00
|
|
|
$("#tables").attr("data-current", $.trim($(this).text()));
|
|
|
|
|
2014-10-13 13:55:19 -05:00
|
|
|
showTableContent();
|
2014-10-26 17:16:35 -05:00
|
|
|
showTableInfo();
|
2014-10-13 13:55:19 -05:00
|
|
|
});
|
|
|
|
|
2015-05-19 12:24:52 -05:00
|
|
|
$("#tables").contextmenu({
|
|
|
|
target: "#tables_context_menu",
|
|
|
|
scopes: "li",
|
|
|
|
onItem: function(context, e) {
|
|
|
|
var table = $.trim($(context[0]).text());
|
|
|
|
var action = $(e.target).data("action");
|
|
|
|
performTableAction(table, action);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-11-13 22:26:05 +09:00
|
|
|
$("#sequences").on("click", "li", function() {
|
|
|
|
$("#tables li.selected").removeClass("selected");
|
2015-11-13 22:34:53 +09:00
|
|
|
$("#sequences li.selected").removeClass("selected");
|
|
|
|
|
2015-11-13 22:26:05 +09:00
|
|
|
$(this).addClass("selected");
|
|
|
|
$("#tables").attr("data-current", $.trim($(this).text()));
|
|
|
|
|
|
|
|
showTableContent();
|
|
|
|
$(".table-information ul").hide();
|
|
|
|
});
|
|
|
|
|
2015-01-03 20:20:18 -06:00
|
|
|
$("#refresh_tables").on("click", function() {
|
|
|
|
loadTables();
|
2015-11-13 22:34:53 +09:00
|
|
|
loadSequences();
|
2015-01-03 20:20:18 -06:00
|
|
|
});
|
|
|
|
|
2014-11-01 20:06:15 -05:00
|
|
|
$("#edit_connection").on("click", function() {
|
|
|
|
if (connected) {
|
|
|
|
$("#close_connection_window").show();
|
|
|
|
}
|
|
|
|
|
|
|
|
showConnectionSettings();
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#close_connection_window").on("click", function() {
|
|
|
|
$("#connection_window").hide();
|
2014-11-03 20:12:41 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
$("#connection_url").on("change", function() {
|
2014-11-06 11:25:53 -06:00
|
|
|
if ($(this).val().indexOf("localhost") != -1) {
|
|
|
|
$("#connection_ssl").val("disable");
|
|
|
|
}
|
|
|
|
});
|
2014-11-03 20:12:41 -06:00
|
|
|
|
2014-11-06 11:25:53 -06:00
|
|
|
$("#pg_host").on("change", function() {
|
2014-11-23 14:48:59 -06:00
|
|
|
var value = $(this).val();
|
|
|
|
|
|
|
|
if (value.indexOf("localhost") != -1 || value.indexOf("127.0.0.1") != -1) {
|
2014-11-03 20:12:41 -06:00
|
|
|
$("#connection_ssl").val("disable");
|
|
|
|
}
|
|
|
|
});
|
2014-11-01 20:06:15 -05:00
|
|
|
|
2014-11-06 16:05:49 -06:00
|
|
|
$(".connection-group-switch button").on("click", function() {
|
|
|
|
$(".connection-group-switch button").removeClass("active");
|
|
|
|
$(this).addClass("active");
|
|
|
|
|
|
|
|
switch($(this).attr("data")) {
|
|
|
|
case "scheme":
|
|
|
|
$(".connection-scheme-group").show();
|
|
|
|
$(".connection-standard-group").hide();
|
|
|
|
return;
|
|
|
|
case "standard":
|
|
|
|
$(".connection-scheme-group").hide();
|
|
|
|
$(".connection-standard-group").show();
|
|
|
|
$(".connection-ssh-group").hide();
|
|
|
|
return;
|
|
|
|
case "ssh":
|
|
|
|
$(".connection-scheme-group").hide();
|
|
|
|
$(".connection-standard-group").show();
|
|
|
|
$(".connection-ssh-group").show();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-12-06 20:04:32 -06:00
|
|
|
$("#connection_bookmarks").on("change", function(e) {
|
|
|
|
var name = $.trim($(this).val());
|
|
|
|
|
|
|
|
if (name == "") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
item = bookmarks[name];
|
|
|
|
|
|
|
|
// Check if bookmark only has url set
|
|
|
|
if (item.url != "") {
|
|
|
|
$("#connection_url").val(item.url);
|
|
|
|
$("#connection_scheme").click();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fill in bookmarked connection settings
|
|
|
|
$("#pg_host").val(item.host);
|
|
|
|
$("#pg_port").val(item.port);
|
|
|
|
$("#pg_user").val(item.user);
|
|
|
|
$("#pg_password").val(item.password);
|
|
|
|
$("#pg_db").val(item.database);
|
|
|
|
$("#connection_ssl").val(item.ssl);
|
|
|
|
});
|
|
|
|
|
2014-11-01 15:44:24 -05:00
|
|
|
$("#connection_form").on("submit", function(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
var button = $(this).children("button");
|
2014-11-06 11:25:53 -06:00
|
|
|
var url = getConnectionString();
|
2015-04-09 08:49:17 +02:00
|
|
|
|
2014-11-01 15:44:24 -05:00
|
|
|
if (url.length == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$("#connection_error").hide();
|
|
|
|
button.prop("disabled", true).text("Please wait...");
|
|
|
|
|
|
|
|
apiCall("post", "/connect", { url: url }, function(resp) {
|
|
|
|
button.prop("disabled", false).text("Connect");
|
|
|
|
|
|
|
|
if (resp.error) {
|
2014-11-01 20:06:15 -05:00
|
|
|
connected = false;
|
2014-11-01 15:44:24 -05:00
|
|
|
$("#connection_error").text(resp.error).show();
|
|
|
|
}
|
|
|
|
else {
|
2014-11-01 20:06:15 -05:00
|
|
|
connected = true;
|
2014-11-01 15:44:24 -05:00
|
|
|
loadTables();
|
2015-11-13 22:34:53 +09:00
|
|
|
loadSequences();
|
2015-01-03 20:55:22 -06:00
|
|
|
|
|
|
|
$("#connection_window").hide();
|
|
|
|
$("#current_database").text(resp.current_database);
|
2014-11-01 15:44:24 -05:00
|
|
|
$("#main").show();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
initEditor();
|
|
|
|
addShortcutTooltips();
|
2015-04-09 08:49:17 +02:00
|
|
|
|
2014-12-02 21:20:04 -06:00
|
|
|
apiCall("get", "/connection", {}, function(resp) {
|
2014-11-01 15:44:24 -05:00
|
|
|
if (resp.error) {
|
2014-11-01 20:06:15 -05:00
|
|
|
connected = false;
|
|
|
|
showConnectionSettings();
|
2014-11-01 15:44:24 -05:00
|
|
|
}
|
|
|
|
else {
|
2014-11-01 20:06:15 -05:00
|
|
|
connected = true;
|
2014-11-01 15:44:24 -05:00
|
|
|
loadTables();
|
2015-11-13 22:34:53 +09:00
|
|
|
loadSequences();
|
2015-01-03 20:55:22 -06:00
|
|
|
|
|
|
|
$("#current_database").text(resp.current_database);
|
2014-11-01 15:44:24 -05:00
|
|
|
$("#main").show();
|
|
|
|
}
|
|
|
|
});
|
2015-04-09 08:49:17 +02:00
|
|
|
});
|