2017-12-15 09:38:56 +08:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
const sql = require('../../services/sql');
|
|
|
|
|
2019-02-10 17:38:18 +08:00
|
|
|
async function getSchema() {
|
|
|
|
const tableNames = await sql.getColumn(`SELECT name FROM sqlite_master WHERE type='table' AND name NOT LIKE 'sqlite_%' ORDER BY name`);
|
|
|
|
const tables = [];
|
|
|
|
|
|
|
|
for (const tableName of tableNames) {
|
|
|
|
tables.push({
|
|
|
|
name: tableName,
|
|
|
|
columns: await sql.getRows(`PRAGMA table_info(${tableName})`)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return tables;
|
|
|
|
}
|
|
|
|
|
2018-03-31 05:07:41 +08:00
|
|
|
async function execute(req) {
|
2019-12-08 18:20:44 +08:00
|
|
|
const queries = req.body.query.split("\n---");
|
2017-12-15 09:38:56 +08:00
|
|
|
|
2017-12-20 11:33:44 +08:00
|
|
|
try {
|
2019-12-08 18:20:44 +08:00
|
|
|
const results = [];
|
|
|
|
|
|
|
|
for (const query of queries) {
|
|
|
|
results.push(await sql.getRows(query));
|
|
|
|
}
|
|
|
|
|
2018-03-31 05:07:41 +08:00
|
|
|
return {
|
2017-12-20 11:33:44 +08:00
|
|
|
success: true,
|
2019-12-08 18:20:44 +08:00
|
|
|
results
|
2018-03-31 05:07:41 +08:00
|
|
|
};
|
2017-12-20 11:33:44 +08:00
|
|
|
}
|
|
|
|
catch (e) {
|
2018-03-31 05:07:41 +08:00
|
|
|
return {
|
2017-12-20 11:33:44 +08:00
|
|
|
success: false,
|
|
|
|
error: e.message
|
2018-03-31 05:07:41 +08:00
|
|
|
};
|
2017-12-20 11:33:44 +08:00
|
|
|
}
|
2018-03-31 05:07:41 +08:00
|
|
|
}
|
2017-12-15 09:38:56 +08:00
|
|
|
|
2018-03-31 05:07:41 +08:00
|
|
|
module.exports = {
|
2019-02-10 17:38:18 +08:00
|
|
|
getSchema,
|
2018-03-31 05:07:41 +08:00
|
|
|
execute
|
|
|
|
};
|