2014-10-31 11:57:44 -04:00
|
|
|
'use strict';
|
|
|
|
|
2016-11-18 21:59:22 -05:00
|
|
|
const Adapter = require('../../Adapter');
|
|
|
|
const Result = require('../../Result');
|
2016-11-22 16:03:46 -05:00
|
|
|
const Helpers = require('../../Helpers');
|
2016-03-11 10:41:04 -05:00
|
|
|
const dbliteAdapter = require('dblite');
|
|
|
|
|
2016-11-18 21:59:22 -05:00
|
|
|
class SqliteDblite extends Adapter {
|
2016-09-14 16:50:32 -04:00
|
|
|
constructor (config) {
|
2016-11-22 16:03:46 -05:00
|
|
|
let file = (Helpers.isString(config)) ? config : config.file;
|
2016-03-11 16:32:38 -05:00
|
|
|
|
2016-11-18 21:59:22 -05:00
|
|
|
const instance = new Promise((resolve, reject) => {
|
|
|
|
let conn = dbliteAdapter(file);
|
|
|
|
|
2016-11-10 22:10:45 -05:00
|
|
|
// Stop the stupid 'bye bye' message being output
|
|
|
|
conn.on('close', () => {});
|
|
|
|
|
2016-11-18 21:59:22 -05:00
|
|
|
conn.on('error', err => {
|
|
|
|
reject(err);
|
2016-11-10 22:10:45 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
// Make sure to actually pass on the connection!
|
2016-11-18 21:59:22 -05:00
|
|
|
return resolve(conn);
|
|
|
|
});
|
2016-11-10 22:10:45 -05:00
|
|
|
|
|
|
|
super(instance);
|
2016-03-11 10:41:04 -05:00
|
|
|
}
|
2015-12-03 20:43:42 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Run the sql query as a prepared statement
|
|
|
|
*
|
|
|
|
* @param {String} sql - The sql with placeholders
|
|
|
|
* @param {Array} params - The values to insert into the query
|
2016-11-10 22:10:45 -05:00
|
|
|
* @return {Promise} - Returns a promise if no callback is provided
|
2015-12-03 20:43:42 -05:00
|
|
|
*/
|
2016-11-10 22:10:45 -05:00
|
|
|
execute (sql, params) {
|
2016-11-18 21:59:22 -05:00
|
|
|
return this.instance.then(conn => new Promise((resolve, reject) => {
|
|
|
|
return conn.query(sql, params, (err, rows) => {
|
2016-11-10 22:10:45 -05:00
|
|
|
if (err) {
|
2016-11-18 21:59:22 -05:00
|
|
|
return reject(err);
|
2016-11-10 22:10:45 -05:00
|
|
|
}
|
2016-11-18 21:59:22 -05:00
|
|
|
return resolve(this.transformResult(rows));
|
2016-03-15 15:37:24 -04:00
|
|
|
});
|
2016-11-18 21:59:22 -05:00
|
|
|
}));
|
2015-12-02 13:01:31 -05:00
|
|
|
}
|
2015-12-08 10:06:29 -05:00
|
|
|
|
|
|
|
/**
|
2016-11-18 21:59:22 -05:00
|
|
|
* Transform the adapter's result into a standard format
|
|
|
|
*
|
|
|
|
* @param {*} originalResult - the original result object from the driver
|
|
|
|
* @return {Result} - the new result object
|
|
|
|
*/
|
|
|
|
transformResult (originalResult) {
|
|
|
|
return new Result(originalResult);
|
|
|
|
}
|
2016-01-26 19:29:12 -05:00
|
|
|
|
2016-11-18 21:59:22 -05:00
|
|
|
/**
|
|
|
|
* Close the current database connection
|
|
|
|
*
|
2015-12-08 10:06:29 -05:00
|
|
|
* @return {void}
|
|
|
|
*/
|
2016-09-14 16:50:32 -04:00
|
|
|
close () {
|
2016-11-10 22:10:45 -05:00
|
|
|
this.instance.then(conn => conn.close());
|
2015-12-08 10:06:29 -05:00
|
|
|
}
|
2016-03-11 10:41:04 -05:00
|
|
|
}
|
|
|
|
|
2016-11-18 21:59:22 -05:00
|
|
|
module.exports = SqliteDblite;
|