2016-11-10 20:15:16 -05:00
|
|
|
'use strict';
|
|
|
|
|
2016-11-18 21:59:22 -05:00
|
|
|
const Adapter = require('../../Adapter');
|
|
|
|
const Result = require('../../Result');
|
2016-11-10 20:15:16 -05:00
|
|
|
const fb = require('node-firebird');
|
|
|
|
|
|
|
|
class Firebird extends Adapter {
|
2016-11-10 22:10:45 -05:00
|
|
|
constructor (config) {
|
2016-11-10 20:15:16 -05:00
|
|
|
super({});
|
2016-11-10 22:10:45 -05:00
|
|
|
this.instance = new Promise((resolve, reject) => {
|
|
|
|
fb.attach(config, (err, instance) => {
|
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
2016-11-18 21:59:22 -05:00
|
|
|
return resolve(instance);
|
2016-11-10 22:10:45 -05:00
|
|
|
});
|
2016-11-10 20:15:16 -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
|
2016-11-10 20:15:16 -05:00
|
|
|
*/
|
2016-11-10 22:10:45 -05:00
|
|
|
execute (sql, params) {
|
|
|
|
return this.instance.then(conn => {
|
|
|
|
return new Promise((resolve, reject) => {
|
2016-11-14 20:23:27 -05:00
|
|
|
conn.query(sql, params, (err, result) => {
|
2016-11-10 22:10:45 -05:00
|
|
|
if (err) {
|
|
|
|
return reject(err);
|
|
|
|
}
|
|
|
|
|
2016-11-18 21:59:22 -05:00
|
|
|
return resolve(this.transformResult(result));
|
|
|
|
});
|
2016-11-10 22:10:45 -05:00
|
|
|
});
|
|
|
|
});
|
2016-11-10 20:15:16 -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-11-10 20:15:16 -05:00
|
|
|
/**
|
|
|
|
* Close the current database connection
|
|
|
|
* @return {void}
|
|
|
|
*/
|
2016-11-10 22:10:45 -05:00
|
|
|
close () {
|
|
|
|
this.instance.then(conn => conn.detach());
|
2016-11-10 20:15:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-18 21:59:22 -05:00
|
|
|
module.exports = Firebird;
|