2021-10-01 19:35:27 +01:00
|
|
|
const shim = require('./shim').default;
|
2017-06-11 22:11:14 +01:00
|
|
|
const Promise = require('promise');
|
|
|
|
|
|
|
|
class DatabaseDriverNode {
|
|
|
|
open(options) {
|
|
|
|
return new Promise((resolve, reject) => {
|
2021-10-01 19:35:27 +01:00
|
|
|
const sqlite3 = shim.nodeSqlite().verbose();
|
|
|
|
|
2020-05-21 09:14:33 +01:00
|
|
|
this.db_ = new sqlite3.Database(options.name, sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, error => {
|
2017-06-11 22:11:14 +01:00
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-05 22:29:00 +01:00
|
|
|
sqliteErrorToJsError(error, sql = null, params = null) {
|
2020-03-13 23:46:14 +00:00
|
|
|
const msg = [error.toString()];
|
2017-07-05 22:29:00 +01:00
|
|
|
if (sql) msg.push(sql);
|
|
|
|
if (params) msg.push(params);
|
2020-03-13 23:46:14 +00:00
|
|
|
const output = new Error(msg.join(': '));
|
2017-07-05 22:29:00 +01:00
|
|
|
if (error.code) output.code = error.code;
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2017-06-11 22:11:14 +01:00
|
|
|
selectOne(sql, params = null) {
|
|
|
|
if (!params) params = {};
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.db_.get(sql, params, (error, row) => {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve(row);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-09-06 17:37:00 +05:30
|
|
|
loadExtension(path) {
|
2020-09-23 12:14:17 +01:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.db_.loadExtension(path, (error) => {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
} else {
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
2020-09-06 17:37:00 +05:30
|
|
|
}
|
|
|
|
|
2017-06-11 22:11:14 +01:00
|
|
|
selectAll(sql, params = null) {
|
|
|
|
if (!params) params = {};
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.db_.all(sql, params, (error, row) => {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve(row);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
exec(sql, params = null) {
|
|
|
|
if (!params) params = {};
|
|
|
|
return new Promise((resolve, reject) => {
|
2020-05-21 09:14:33 +01:00
|
|
|
this.db_.run(sql, params, error => {
|
2017-06-11 22:11:14 +01:00
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-11-27 22:50:46 +00:00
|
|
|
lastInsertId() {
|
|
|
|
throw new Error('NOT IMPLEMENTED');
|
|
|
|
}
|
2017-06-11 22:11:14 +01:00
|
|
|
}
|
|
|
|
|
2019-07-29 15:43:53 +02:00
|
|
|
module.exports = { DatabaseDriverNode };
|