trilium/public/javascripts/dialogs/sql_console.js

68 lines
1.5 KiB
JavaScript
Raw Normal View History

2017-12-15 09:38:56 +08:00
"use strict";
const sqlConsole = (function() {
const dialogEl = $("#sql-console-dialog");
const queryEl = $('#sql-console-query');
const executeButton = $('#sql-console-execute');
const resultHeadEl = $('#sql-console-results thead');
const resultBodyEl = $('#sql-console-results tbody');
function showDialog() {
glob.activeDialog = dialogEl;
dialogEl.dialog({
modal: true,
width: $(window).width(),
height: $(window).height()
});
}
async function execute() {
const sqlQuery = queryEl.val();
const result = await server.post("sql/execute", {
2017-12-15 09:38:56 +08:00
query: sqlQuery
});
if (!result.success) {
showError(result.error);
return;
}
const rows = result.rows;
2017-12-15 09:38:56 +08:00
resultHeadEl.empty();
resultBodyEl.empty();
if (rows.length > 0) {
const result = rows[0];
2017-12-15 09:38:56 +08:00
const rowEl = $("<tr>");
for (const key in result) {
rowEl.append($("<th>").html(key));
}
resultHeadEl.append(rowEl);
}
for (const result of rows) {
2017-12-15 09:38:56 +08:00
const rowEl = $("<tr>");
for (const key in result) {
rowEl.append($("<td>").html(result[key]));
}
resultBodyEl.append(rowEl);
}
}
$(document).bind('keydown', 'alt+o', showDialog);
queryEl.bind('keydown', 'ctrl+return', execute);
2017-12-15 09:38:56 +08:00
executeButton.click(execute);
return {
showDialog
};
})();