2020-12-29 22:27:31 +01:00
|
|
|
import TabAwareWidget from "./tab_aware_widget.js";
|
|
|
|
|
|
|
|
const TPL = `
|
|
|
|
<div class="sql-result-widget">
|
|
|
|
<style>
|
|
|
|
.sql-result-widget {
|
|
|
|
padding: 15px;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
|
|
|
|
<div class="sql-console-result-container"></div>
|
|
|
|
</div>`;
|
|
|
|
|
|
|
|
export default class SqlResultWidget extends TabAwareWidget {
|
|
|
|
isEnabled() {
|
|
|
|
return this.note
|
|
|
|
&& this.note.mime === 'text/x-sqlite;schema=trilium'
|
|
|
|
&& super.isEnabled();
|
|
|
|
}
|
|
|
|
|
|
|
|
doRender() {
|
|
|
|
this.$widget = $(TPL);
|
|
|
|
this.overflowing();
|
|
|
|
|
|
|
|
this.$sqlConsoleResultContainer = this.$widget.find('.sql-console-result-container');
|
|
|
|
}
|
|
|
|
|
2021-05-22 12:26:45 +02:00
|
|
|
async sqlQueryResultsEvent({ntxId, results}) {
|
|
|
|
if (!this.isTab(ntxId)) {
|
2021-02-15 20:44:55 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-12-29 22:27:31 +01:00
|
|
|
this.$sqlConsoleResultContainer.empty();
|
|
|
|
|
|
|
|
for (const rows of results) {
|
2021-02-15 20:44:55 +01:00
|
|
|
if (!rows.length) {
|
2020-12-29 22:27:31 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const $table = $('<table class="table table-striped">');
|
|
|
|
this.$sqlConsoleResultContainer.append($table);
|
|
|
|
|
|
|
|
const result = rows[0];
|
|
|
|
const $row = $("<tr>");
|
|
|
|
|
|
|
|
for (const key in result) {
|
|
|
|
$row.append($("<th>").html(key));
|
|
|
|
}
|
|
|
|
|
|
|
|
$table.append($row);
|
|
|
|
|
|
|
|
for (const result of rows) {
|
|
|
|
const $row = $("<tr>");
|
|
|
|
|
|
|
|
for (const key in result) {
|
|
|
|
$row.append($("<td>").html(result[key]));
|
|
|
|
}
|
|
|
|
|
|
|
|
$table.append($row);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|