2017-06-24 20:06:28 +02:00
|
|
|
import { uuid } from 'lib/uuid.js';
|
|
|
|
import { promiseChain } from 'lib/promise-utils.js';
|
|
|
|
import { Logger } from 'lib/logger.js'
|
2017-06-27 01:20:01 +02:00
|
|
|
import { time } from 'lib/time-utils.js'
|
|
|
|
import { sprintf } from 'sprintf-js';
|
2017-05-07 23:02:17 +02:00
|
|
|
|
|
|
|
class Database {
|
|
|
|
|
2017-06-11 23:11:14 +02:00
|
|
|
constructor(driver) {
|
2017-05-11 22:14:01 +02:00
|
|
|
this.debugMode_ = false;
|
2017-06-11 23:11:14 +02:00
|
|
|
this.driver_ = driver;
|
2017-06-15 01:14:15 +02:00
|
|
|
this.inTransaction_ = false;
|
2017-06-23 23:32:24 +02:00
|
|
|
|
|
|
|
this.logger_ = new Logger();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converts the SQLite error to a regular JS error
|
|
|
|
// so that it prints a stacktrace when passed to
|
|
|
|
// console.error()
|
2017-07-04 20:09:47 +02:00
|
|
|
sqliteErrorToJsError(error, sql = null, params = null) {
|
2017-07-05 23:29:00 +02:00
|
|
|
return this.driver().sqliteErrorToJsError(error, sql, params);
|
2017-06-23 23:32:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
setLogger(l) {
|
|
|
|
this.logger_ = l;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger() {
|
|
|
|
return this.logger_;
|
2017-05-11 22:14:01 +02:00
|
|
|
}
|
2017-05-07 23:02:17 +02:00
|
|
|
|
2017-06-11 23:11:14 +02:00
|
|
|
driver() {
|
|
|
|
return this.driver_;
|
|
|
|
}
|
|
|
|
|
2017-07-04 20:09:47 +02:00
|
|
|
async open(options) {
|
|
|
|
await this.driver().open(options);
|
|
|
|
this.logger().info('Database was open successfully');
|
2017-06-11 23:11:14 +02:00
|
|
|
}
|
|
|
|
|
2017-06-25 14:49:46 +02:00
|
|
|
escapeField(field) {
|
2017-07-03 20:58:01 +02:00
|
|
|
if (field == '*') return '*';
|
2017-06-25 14:49:46 +02:00
|
|
|
return '`' + field + '`';
|
|
|
|
}
|
|
|
|
|
|
|
|
escapeFields(fields) {
|
2017-07-03 20:58:01 +02:00
|
|
|
if (fields == '*') return '*';
|
|
|
|
|
2017-06-25 14:49:46 +02:00
|
|
|
let output = [];
|
|
|
|
for (let i = 0; i < fields.length; i++) {
|
|
|
|
output.push(this.escapeField(fields[i]));
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2017-07-03 20:58:01 +02:00
|
|
|
async tryCall(callName, sql, params) {
|
2017-07-02 17:46:03 +02:00
|
|
|
if (typeof sql === 'object') {
|
|
|
|
params = sql.params;
|
|
|
|
sql = sql.sql;
|
|
|
|
}
|
|
|
|
|
2017-06-27 01:20:01 +02:00
|
|
|
let waitTime = 50;
|
|
|
|
let totalWaitTime = 0;
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
this.logQuery(sql, params);
|
2017-07-03 20:58:01 +02:00
|
|
|
let result = await this.driver()[callName](sql, params);
|
|
|
|
return result; // No exception was thrown
|
2017-06-27 01:20:01 +02:00
|
|
|
} catch (error) {
|
2017-07-03 20:58:01 +02:00
|
|
|
if (error && (error.code == 'SQLITE_IOERR' || error.code == 'SQLITE_BUSY')) {
|
|
|
|
if (totalWaitTime >= 20000) throw this.sqliteErrorToJsError(error, sql, params);
|
|
|
|
this.logger().warn(sprintf('Error %s: will retry in %s milliseconds', error.code, waitTime));
|
2017-06-27 01:20:01 +02:00
|
|
|
this.logger().warn('Error was: ' + error.toString());
|
|
|
|
await time.msleep(waitTime);
|
|
|
|
totalWaitTime += waitTime;
|
|
|
|
waitTime *= 1.5;
|
|
|
|
} else {
|
|
|
|
throw this.sqliteErrorToJsError(error, sql, params);
|
2017-07-03 20:58:01 +02:00
|
|
|
}
|
2017-06-27 01:20:01 +02:00
|
|
|
}
|
|
|
|
}
|
2017-06-11 23:11:14 +02:00
|
|
|
}
|
2017-05-07 23:02:17 +02:00
|
|
|
|
2017-07-03 20:58:01 +02:00
|
|
|
async selectOne(sql, params = null) {
|
|
|
|
return this.tryCall('selectOne', sql, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
async selectAll(sql, params = null) {
|
|
|
|
return this.tryCall('selectAll', sql, params);
|
|
|
|
}
|
|
|
|
|
|
|
|
async exec(sql, params = null) {
|
|
|
|
return this.tryCall('exec', sql, params);
|
|
|
|
}
|
|
|
|
|
2017-06-11 23:11:14 +02:00
|
|
|
transactionExecBatch(queries) {
|
2017-06-15 01:14:15 +02:00
|
|
|
if (queries.length <= 0) return Promise.resolve();
|
|
|
|
|
|
|
|
if (queries.length == 1) {
|
2017-06-25 01:19:11 +02:00
|
|
|
let q = this.wrapQuery(queries[0]);
|
|
|
|
return this.exec(q.sql, q.params);
|
2017-06-15 01:14:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// There can be only one transaction running at a time so queue
|
|
|
|
// any new transaction here.
|
|
|
|
if (this.inTransaction_) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let iid = setInterval(() => {
|
|
|
|
if (!this.inTransaction_) {
|
|
|
|
clearInterval(iid);
|
|
|
|
this.transactionExecBatch(queries).then(() => {
|
|
|
|
resolve();
|
|
|
|
}).catch((error) => {
|
|
|
|
reject(error);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}, 100);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.inTransaction_ = true;
|
|
|
|
|
2017-06-14 21:59:46 +02:00
|
|
|
queries.splice(0, 0, 'BEGIN TRANSACTION');
|
|
|
|
queries.push('COMMIT'); // Note: ROLLBACK is currently not supported
|
|
|
|
|
2017-06-11 23:11:14 +02:00
|
|
|
let chain = [];
|
|
|
|
for (let i = 0; i < queries.length; i++) {
|
|
|
|
let query = this.wrapQuery(queries[i]);
|
|
|
|
chain.push(() => {
|
|
|
|
return this.exec(query.sql, query.params);
|
|
|
|
});
|
|
|
|
}
|
2017-06-14 21:59:46 +02:00
|
|
|
|
2017-06-15 01:14:15 +02:00
|
|
|
return promiseChain(chain).then(() => {
|
|
|
|
this.inTransaction_ = false;
|
|
|
|
});
|
2017-05-12 22:17:23 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 00:16:50 +02:00
|
|
|
static enumId(type, s) {
|
2017-05-12 22:17:23 +02:00
|
|
|
if (type == 'settings') {
|
|
|
|
if (s == 'int') return 1;
|
|
|
|
if (s == 'string') return 2;
|
|
|
|
}
|
2017-05-20 00:16:50 +02:00
|
|
|
if (type == 'fieldType') {
|
2017-07-06 21:48:17 +02:00
|
|
|
if (s == 'INTEGER') s = 'INT';
|
2017-05-20 00:16:50 +02:00
|
|
|
return this['TYPE_' + s];
|
|
|
|
}
|
2017-05-12 22:17:23 +02:00
|
|
|
throw new Error('Unknown enum type or value: ' + type + ', ' + s);
|
2017-05-07 23:02:17 +02:00
|
|
|
}
|
|
|
|
|
2017-05-20 00:16:50 +02:00
|
|
|
static formatValue(type, value) {
|
|
|
|
if (value === null || value === undefined) return null;
|
|
|
|
if (type == this.TYPE_INT) return Number(value);
|
|
|
|
if (type == this.TYPE_TEXT) return value;
|
|
|
|
if (type == this.TYPE_NUMERIC) return Number(value);
|
|
|
|
throw new Error('Unknown type: ' + type);
|
|
|
|
}
|
|
|
|
|
2017-05-07 23:02:17 +02:00
|
|
|
sqlStringToLines(sql) {
|
|
|
|
let output = [];
|
|
|
|
let lines = sql.split("\n");
|
|
|
|
let statement = '';
|
|
|
|
for (var i = 0; i < lines.length; i++) {
|
|
|
|
var line = lines[i];
|
|
|
|
if (line == '') continue;
|
|
|
|
if (line.substr(0, 2) == "--") continue;
|
|
|
|
statement += line;
|
|
|
|
if (line[line.length - 1] == ';') {
|
|
|
|
output.push(statement);
|
|
|
|
statement = '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2017-05-11 22:14:01 +02:00
|
|
|
logQuery(sql, params = null) {
|
2017-06-25 13:39:42 +02:00
|
|
|
this.logger().debug(sql);
|
2017-06-25 14:49:46 +02:00
|
|
|
if (params !== null && params.length) this.logger().debug(JSON.stringify(params));
|
2017-05-18 22:31:40 +02:00
|
|
|
}
|
|
|
|
|
2017-05-11 22:14:01 +02:00
|
|
|
static insertQuery(tableName, data) {
|
2017-06-18 01:49:52 +02:00
|
|
|
if (!data || !Object.keys(data).length) throw new Error('Data is empty');
|
|
|
|
|
2017-05-10 21:51:43 +02:00
|
|
|
let keySql= '';
|
|
|
|
let valueSql = '';
|
2017-05-11 22:14:01 +02:00
|
|
|
let params = [];
|
2017-05-10 21:51:43 +02:00
|
|
|
for (let key in data) {
|
2017-05-11 22:14:01 +02:00
|
|
|
if (!data.hasOwnProperty(key)) continue;
|
2017-06-20 21:18:19 +02:00
|
|
|
if (key[key.length - 1] == '_') continue;
|
2017-05-10 21:51:43 +02:00
|
|
|
if (keySql != '') keySql += ', ';
|
|
|
|
if (valueSql != '') valueSql += ', ';
|
2017-05-11 22:14:01 +02:00
|
|
|
keySql += '`' + key + '`';
|
2017-05-10 21:51:43 +02:00
|
|
|
valueSql += '?';
|
2017-05-11 22:14:01 +02:00
|
|
|
params.push(data[key]);
|
2017-05-10 21:51:43 +02:00
|
|
|
}
|
2017-05-11 22:14:01 +02:00
|
|
|
return {
|
|
|
|
sql: 'INSERT INTO `' + tableName + '` (' + keySql + ') VALUES (' + valueSql + ')',
|
|
|
|
params: params,
|
|
|
|
};
|
2017-05-10 21:51:43 +02:00
|
|
|
}
|
|
|
|
|
2017-05-12 21:54:06 +02:00
|
|
|
static updateQuery(tableName, data, where) {
|
2017-06-18 01:49:52 +02:00
|
|
|
if (!data || !Object.keys(data).length) throw new Error('Data is empty');
|
|
|
|
|
2017-05-12 21:54:06 +02:00
|
|
|
let sql = '';
|
|
|
|
let params = [];
|
|
|
|
for (let key in data) {
|
|
|
|
if (!data.hasOwnProperty(key)) continue;
|
2017-06-20 21:18:19 +02:00
|
|
|
if (key[key.length - 1] == '_') continue;
|
2017-05-12 21:54:06 +02:00
|
|
|
if (sql != '') sql += ', ';
|
2017-05-20 00:16:50 +02:00
|
|
|
sql += '`' + key + '`=?';
|
2017-05-12 21:54:06 +02:00
|
|
|
params.push(data[key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof where != 'string') {
|
2017-07-13 20:47:31 +02:00
|
|
|
let s = [];
|
|
|
|
for (let n in where) {
|
|
|
|
if (!where.hasOwnProperty(n)) continue;
|
|
|
|
params.push(where[n]);
|
|
|
|
s.push('`' + n + '`=?');
|
|
|
|
}
|
|
|
|
where = s.join(' AND ');
|
|
|
|
// params.push(where.id);
|
|
|
|
// where = 'id=?';
|
2017-05-12 21:54:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
sql: 'UPDATE `' + tableName + '` SET ' + sql + ' WHERE ' + where,
|
|
|
|
params: params,
|
|
|
|
};
|
|
|
|
}
|
2017-06-14 21:59:46 +02:00
|
|
|
|
2017-06-11 23:11:14 +02:00
|
|
|
wrapQueries(queries) {
|
|
|
|
let output = [];
|
|
|
|
for (let i = 0; i < queries.length; i++) {
|
|
|
|
output.push(this.wrapQuery(queries[i]));
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
wrapQuery(sql, params = null) {
|
|
|
|
if (!sql) throw new Error('Cannot wrap empty string: ' + sql);
|
|
|
|
|
|
|
|
if (sql.constructor === Array) {
|
|
|
|
let output = {};
|
|
|
|
output.sql = sql[0];
|
|
|
|
output.params = sql.length >= 2 ? sql[1] : null;
|
|
|
|
return output;
|
|
|
|
} else if (typeof sql === 'string') {
|
|
|
|
return { sql: sql, params: params };
|
|
|
|
} else {
|
|
|
|
return sql; // Already wrapped
|
|
|
|
}
|
2017-05-12 22:17:23 +02:00
|
|
|
}
|
|
|
|
|
2017-05-07 23:02:17 +02:00
|
|
|
}
|
|
|
|
|
2017-06-06 22:58:19 +02:00
|
|
|
Database.TYPE_INT = 1;
|
|
|
|
Database.TYPE_TEXT = 2;
|
2017-06-15 20:18:48 +02:00
|
|
|
Database.TYPE_NUMERIC = 3;
|
2017-06-06 22:58:19 +02:00
|
|
|
|
2017-05-07 23:02:17 +02:00
|
|
|
export { Database };
|