2017-12-14 20:38:56 -05:00
|
|
|
"use strict";
|
|
|
|
|
2024-07-18 21:35:17 +03:00
|
|
|
import sql from "../../services/sql.js";
|
|
|
|
import becca from "../../becca/becca.js";
|
2024-04-06 23:12:22 +03:00
|
|
|
import { Request } from 'express';
|
2024-07-18 21:35:17 +03:00
|
|
|
import ValidationError from "../../errors/validation_error.js";
|
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;
|
|
|
|
}
|
|
|
|
|
2024-04-06 23:12:22 +03:00
|
|
|
function execute(req: Request) {
|
2023-05-08 00:02:08 +02:00
|
|
|
const note = becca.getNoteOrThrow(req.params.noteId);
|
2020-12-29 22:27:31 +01:00
|
|
|
|
2024-04-06 23:12:22 +03:00
|
|
|
const content = note.getContent();
|
|
|
|
if (typeof content !== "string") {
|
|
|
|
throw new ValidationError("Invalid note type.");
|
|
|
|
}
|
|
|
|
|
|
|
|
const queries = content.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();
|
|
|
|
|
2023-06-28 02:31:17 +08:00
|
|
|
while (query.startsWith('-- ')) {
|
2023-06-17 17:06:17 +10:00
|
|
|
// Query starts with one or more SQL comments, discard these before we execute.
|
2023-06-23 09:06:14 +10:00
|
|
|
const pivot = query.indexOf('\n');
|
|
|
|
query = pivot > 0 ? query.substr(pivot + 1).trim() : "";
|
|
|
|
}
|
|
|
|
|
2020-08-28 22:04:35 +02:00
|
|
|
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
|
|
|
}
|
2024-04-06 23:12:22 +03:00
|
|
|
catch (e: any) {
|
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
|
|
|
|
2024-07-18 21:47:30 +03:00
|
|
|
export default {
|
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
|
|
|
};
|