143 lines
3.8 KiB
JavaScript
Raw Normal View History

2018-03-27 22:42:46 -04:00
import libraryLoader from '../services/library_loader.js';
import server from '../services/server.js';
2019-10-20 10:00:18 +02:00
import toastService from "../services/toast.js";
import utils from "../services/utils.js";
2018-02-10 09:14:18 -05:00
const $dialog = $("#sql-console-dialog");
const $query = $('#sql-console-query');
const $executeButton = $('#sql-console-execute');
2019-12-09 21:31:38 +01:00
const $tableSchemas = $("#sql-console-table-schemas");
const $resultContainer = $("#result-container");
2018-02-10 09:14:18 -05:00
let codeEditor;
2018-02-10 09:14:18 -05:00
$dialog.on("shown.bs.modal", e => initEditor());
export async function showDialog() {
utils.closeActiveDialog();
glob.activeDialog = $dialog;
2019-12-09 21:31:38 +01:00
await showTableSchemas();
2019-02-13 23:27:00 +01:00
$dialog.modal();
}
2018-02-10 09:14:18 -05:00
async function initEditor() {
if (!codeEditor) {
2018-03-27 22:42:46 -04:00
await libraryLoader.requireLibrary(libraryLoader.CODE_MIRROR);
CodeMirror.keyMap.default["Shift-Tab"] = "indentLess";
CodeMirror.keyMap.default["Tab"] = "indentMore";
// removing Escape binding so that Escape will propagate to the dialog (which will close on escape)
delete CodeMirror.keyMap.basic["Esc"];
CodeMirror.modeURL = 'libraries/codemirror/mode/%N/%N.js';
codeEditor = CodeMirror($query[0], {
value: "",
viewportMargin: Infinity,
indentUnit: 4,
highlightSelectionMatches: {showToken: /\w/, annotateScrollbar: false}
});
codeEditor.setOption("mode", "text/x-sqlite");
CodeMirror.autoLoadMode(codeEditor, "sql");
codeEditor.setValue(`SELECT title, isProtected, type, mime FROM notes WHERE noteId = 'root';
---
SELECT noteId, parentNoteId, notePosition, prefix FROM branches WHERE branchId = 'root';`);
2017-12-14 20:38:56 -05:00
}
codeEditor.focus();
}
2019-08-25 21:55:36 +02:00
async function execute() {
// execute the selected text or the whole content if there's no selection
let sqlQuery = codeEditor.getSelection();
if (!sqlQuery) {
sqlQuery = codeEditor.getValue();
}
2017-12-14 20:38:56 -05:00
const result = await server.post("sql/execute", {
query: sqlQuery
});
if (!result.success) {
2019-10-20 10:00:18 +02:00
toastService.showError(result.error);
return;
}
else {
2019-10-20 10:00:18 +02:00
toastService.showMessage("Query was executed successfully.");
}
const results = result.results;
$resultContainer.empty();
for (const rows of results) {
if (rows.length === 0) {
continue;
}
2017-12-14 20:38:56 -05:00
const $table = $('<table class="table table-striped">');
$resultContainer.append($table);
2017-12-14 20:38:56 -05:00
const result = rows[0];
const $row = $("<tr>");
2017-12-14 20:38:56 -05:00
for (const key in result) {
$row.append($("<th>").html(key));
2017-12-14 20:38:56 -05:00
}
$table.append($row);
2017-12-14 20:38:56 -05:00
for (const result of rows) {
const $row = $("<tr>");
2017-12-14 20:38:56 -05:00
for (const key in result) {
$row.append($("<td>").html(result[key]));
}
$table.append($row);
}
2017-12-14 20:38:56 -05:00
}
}
2017-12-14 20:38:56 -05:00
2019-12-09 21:31:38 +01:00
async function showTableSchemas() {
2019-02-13 23:27:00 +01:00
const tables = await server.get('sql/schema');
2019-12-09 21:31:38 +01:00
$tableSchemas.empty();
2019-02-13 23:27:00 +01:00
for (const table of tables) {
const $tableLink = $('<button class="btn">').text(table.name);
2019-12-09 21:31:38 +01:00
const $columns = $("<ul>");
2019-02-13 23:27:00 +01:00
for (const column of table.columns) {
$columns.append(
2019-12-09 21:31:38 +01:00
$("<li>")
.append($("<span>").text(column.name))
.append($("<span>").text(column.type))
2019-02-13 23:27:00 +01:00
);
}
2019-12-09 21:31:38 +01:00
$tableSchemas.append($tableLink).append(" ");
2019-02-13 23:27:00 +01:00
$tableLink
2019-12-09 21:31:38 +01:00
.tooltip({
html: true,
placement: 'bottom',
boundary: 'window',
title: $columns[0].outerHTML
})
2019-11-09 17:39:48 +01:00
.on('click', () => codeEditor.setValue("SELECT * FROM " + table.name + " LIMIT 100"));
2019-02-13 23:27:00 +01:00
}
}
utils.bindElShortcut($query, 'ctrl+return', execute);
2019-11-09 17:39:48 +01:00
$executeButton.on('click', execute);