2017-12-14 20:38:56 -05:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const sql = require('../../services/sql');
|
2021-06-06 22:15:51 +02:00
|
|
|
const becca = require("../../becca/becca");
|
2022-12-09 16:13:22 +01:00
|
|
|
const NotFoundError = require("../../errors/not_found_error");
|
2017-12-14 20:38:56 -05:00
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function getSchema() {
|
|
|
|
const tableNames = sql.getColumn(`SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name`);
|
2019-02-10 10:38:18 +01:00
|
|
|
const tables = [];
|
|
|
|
|
|
|
|
for (const tableName of tableNames) {
|
|
|
|
tables.push({
|
|
|
|
name: tableName,
|
2020-06-20 12:31:38 +02:00
|
|
|
columns: sql.getRows(`PRAGMA table_info(${tableName})`)
|
2019-02-10 10:38:18 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return tables;
|
|
|
|
}
|
|
|
|
|
2020-06-20 12:31:38 +02:00
|
|
|
function execute(req) {
|
2021-05-02 11:23:58 +02:00
|
|
|
const note = becca.getNote(req.params.noteId);
|
2020-12-29 22:27:31 +01:00
|
|
|
|
|
|
|
if (!note) {
|
2022-12-09 16:04:13 +01:00
|
|
|
throw new NotFoundError(`Note '${req.params.noteId}' was not found.`);
|
2020-12-29 22:27:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const queries = note.getContent().split("\n---");
|
2017-12-14 20:38:56 -05:00
|
|
|
|
2017-12-19 22:33:44 -05:00
|
|
|
try {
|
2019-12-08 11:20:44 +01:00
|
|
|
const results = [];
|
|
|
|
|
2020-08-28 22:04:35 +02:00
|
|
|
for (let query of queries) {
|
|
|
|
query = query.trim();
|
|
|
|
|
|
|
|
if (!query) {
|
2019-12-09 21:31:38 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2023-06-12 23:18:57 +02:00
|
|
|
if (query.toLowerCase().startsWith('select') || query.toLowerCase().startsWith('with')) {
|
2020-08-28 22:04:35 +02:00
|
|
|
results.push(sql.getRows(query));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
results.push(sql.execute(query));
|
|
|
|
}
|
2019-12-08 11:20:44 +01:00
|
|
|
}
|
|
|
|
|
2018-03-30 17:07:41 -04:00
|
|
|
return {
|
2017-12-19 22:33:44 -05:00
|
|
|
success: true,
|
2019-12-08 11:20:44 +01:00
|
|
|
results
|
2018-03-30 17:07:41 -04:00
|
|
|
};
|
2017-12-19 22:33:44 -05:00
|
|
|
}
|
|
|
|
catch (e) {
|
2018-03-30 17:07:41 -04:00
|
|
|
return {
|
2017-12-19 22:33:44 -05:00
|
|
|
success: false,
|
|
|
|
error: e.message
|
2018-03-30 17:07:41 -04:00
|
|
|
};
|
2017-12-19 22:33:44 -05:00
|
|
|
}
|
2018-03-30 17:07:41 -04:00
|
|
|
}
|
2017-12-14 20:38:56 -05:00
|
|
|
|
2018-03-30 17:07:41 -04:00
|
|
|
module.exports = {
|
2019-02-10 10:38:18 +01:00
|
|
|
getSchema,
|
2018-03-30 17:07:41 -04:00
|
|
|
execute
|
2020-06-20 12:31:38 +02:00
|
|
|
};
|