node-query/lib/adapters/pg.js

36 lines
967 B
JavaScript
Raw Normal View History

2014-10-23 10:53:16 -04:00
'use strict';
2014-10-27 15:46:54 -04:00
var adapter = require('../adapter'),
getArgs = require('getargs');
2014-10-23 10:53:16 -04:00
2014-10-27 15:46:54 -04:00
/** @module adapters/pg */
2014-10-23 10:53:16 -04:00
var Pg = function(instance) {
// That 'new' keyword is annoying
if ( ! (this instanceof Pg)) return new Pg(instance);
/**
* 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
* @param {Function} callback - Callback to run when a response is recieved
* @return void
*/
2014-10-27 15:46:54 -04:00
adapter.execute = function(/*sql, params, callback*/) {
var args = getArgs('sql:string, [params]:array, callback:function', arguments);
// Replace question marks with numbered placeholders, because this adapter is different...
var count = 0;
args.sql = args.sql.replace(/\?/g, function(match, offset, string) {
count++;
return '$' + count;
});
instance.query(args.sql, args.params, args.callback);
2014-10-23 10:53:16 -04:00
};
return adapter;
}
module.exports = Pg;