trilium/src/services/sql.js

208 lines
4.5 KiB
JavaScript
Raw Normal View History

2017-10-22 09:10:33 +08:00
"use strict";
2017-10-25 10:17:48 +08:00
const log = require('./log');
const cls = require('./cls');
let dbConnection;
function setDbConnection(connection) {
dbConnection = connection;
2017-12-04 11:29:23 +08:00
}
async function insert(table_name, rec, replace = false) {
2017-10-26 10:39:21 +08:00
const keys = Object.keys(rec);
if (keys.length === 0) {
log.error("Can't insert empty object into table " + table_name);
return;
}
const columns = keys.join(", ");
const questionMarks = keys.map(p => "?").join(", ");
const query = "INSERT " + (replace ? "OR REPLACE" : "") + " INTO " + table_name + "(" + columns + ") VALUES (" + questionMarks + ")";
const res = await execute(query, Object.values(rec));
return res.lastID;
}
async function replace(table_name, rec) {
return await insert(table_name, rec, true);
2017-10-30 10:22:30 +08:00
}
async function beginTransaction() {
2018-04-03 09:34:28 +08:00
return await execute("BEGIN");
}
async function commit() {
2018-04-03 09:34:28 +08:00
return await execute("COMMIT");
}
async function rollback() {
2018-04-03 09:34:28 +08:00
return await execute("ROLLBACK");
2017-10-26 10:39:21 +08:00
}
async function getRow(query, params = []) {
return await wrap(async db => db.get(query, ...params));
}
async function getRowOrNull(query, params = []) {
2018-04-03 09:34:28 +08:00
const all = await getRows(query, params);
2017-10-30 10:22:30 +08:00
return all.length > 0 ? all[0] : null;
}
async function getValue(query, params = []) {
const row = await getRowOrNull(query, params);
2017-10-30 10:22:30 +08:00
if (!row) {
return null;
}
return row[Object.keys(row)[0]];
}
const PARAM_LIMIT = 900; // actual limit is 999
// this is to overcome 999 limit of number of query parameters
async function getManyRows(query, params) {
let results = [];
while (params.length > 0) {
const curParams = params.slice(0, Math.max(params.length, PARAM_LIMIT));
params = params.slice(curParams.length);
let i = 1;
const questionMarks = curParams.map(() => "?" + i++).join(",");
const curQuery = query.replace(/\?\?\?/g, questionMarks);
results = results.concat(await getRows(curQuery, curParams));
}
return results;
}
async function getRows(query, params = []) {
return await wrap(async db => db.all(query, ...params));
}
2017-11-03 10:55:22 +08:00
async function getMap(query, params = []) {
const map = {};
const results = await getRows(query, params);
2017-11-03 10:55:22 +08:00
for (const row of results) {
const keys = Object.keys(row);
map[row[keys[0]]] = row[keys[1]];
}
return map;
}
async function getColumn(query, params = []) {
2017-10-25 11:14:26 +08:00
const list = [];
const result = await getRows(query, params);
2017-10-25 11:14:26 +08:00
2017-12-15 11:16:26 +08:00
if (result.length === 0) {
return list;
}
const key = Object.keys(result[0])[0];
2017-10-25 11:14:26 +08:00
for (const row of result) {
list.push(row[key]);
}
return list;
}
async function execute(query, params = []) {
return await wrap(async db => db.run(query, ...params));
}
async function executeScript(query) {
return await wrap(async db => db.exec(query));
2017-10-16 05:31:49 +08:00
}
async function wrap(func) {
2017-11-22 11:11:27 +08:00
const thisError = new Error();
try {
return await func(dbConnection);
}
catch (e) {
log.error("Error executing query. Inner exception: " + e.stack + thisError.stack);
thisError.message = e.stack;
throw thisError;
}
}
2017-11-29 07:33:23 +08:00
let transactionActive = false;
let transactionPromise = null;
async function transactional(func) {
if (cls.namespace.get('isInTransaction')) {
return await func();
}
2017-11-29 07:33:23 +08:00
while (transactionActive) {
await transactionPromise;
}
2018-02-25 11:44:45 +08:00
let ret = null;
const error = new Error(); // to capture correct stack trace in case of exception
2017-11-29 07:33:23 +08:00
transactionActive = true;
transactionPromise = new Promise(async (resolve, reject) => {
try {
cls.namespace.set('isInTransaction', true);
await beginTransaction();
2018-02-25 11:44:45 +08:00
ret = await func();
await commit();
2017-11-29 07:33:23 +08:00
transactionActive = false;
resolve();
}
catch (e) {
if (transactionActive) {
log.error("Error executing transaction, executing rollback. Inner exception: " + e.stack + error.stack);
await rollback();
transactionActive = false;
}
reject(e);
}
finally {
cls.namespace.set('isInTransaction', false);
}
});
2017-11-29 07:33:23 +08:00
if (transactionActive) {
await transactionPromise;
}
2018-02-25 11:44:45 +08:00
return ret;
}
module.exports = {
setDbConnection,
insert,
2017-10-30 10:22:30 +08:00
replace,
getValue,
getRow,
getRowOrNull,
getRows,
getManyRows,
2017-11-03 10:55:22 +08:00
getMap,
getColumn,
execute,
2017-10-16 05:31:49 +08:00
executeScript,
transactional
};