Fix pg adapter

This commit is contained in:
Timothy Warren 2014-10-27 15:46:54 -04:00
parent 2f5a927e99
commit 64b480c8e1
6 changed files with 71 additions and 50 deletions

View File

@ -1,8 +1,9 @@
'use strict'; 'use strict';
var adapter = require('../adapter.js'); var adapter = require('../adapter'),
getArgs = require('getargs');
/** @module adapters/mysql2 */ /** @module adapters/pg */
var Pg = function(instance) { var Pg = function(instance) {
// That 'new' keyword is annoying // That 'new' keyword is annoying
@ -16,8 +17,17 @@ var Pg = function(instance) {
* @param {Function} callback - Callback to run when a response is recieved * @param {Function} callback - Callback to run when a response is recieved
* @return void * @return void
*/ */
adapter.execute = function(sql, params, callback) { adapter.execute = function(/*sql, params, callback*/) {
instance.query.apply(instance, arguments); 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);
}; };
return adapter; return adapter;

View File

@ -6,7 +6,8 @@
* @module drivers/mysql * @module drivers/mysql
*/ */
module.exports = (function() { module.exports = (function() {
delete require.cache[require.resolve('../driver')];
var driver = require('../driver');
var driver = require('../driver'), var driver = require('../driver'),
helpers = require('../helpers'); helpers = require('../helpers');

View File

@ -6,6 +6,7 @@
* @module drivers/pg * @module drivers/pg
*/ */
module.exports = (function() { module.exports = (function() {
delete require.cache[require.resolve('../driver')];
var driver = require('../driver'); var driver = require('../driver');
return driver; return driver;

View File

@ -792,6 +792,7 @@ var QueryBuilder = function(driver, adapter) {
/** /**
* Run the generated delete query * Run the generated delete query
* *
* @method delete
* @param {String} table - The table to insert into * @param {String} table - The table to insert into
* @param {Object} [where] - Where clause for delete statement * @param {Object} [where] - Where clause for delete statement
* @param {Function} callback - Callback for handling response from the database * @param {Function} callback - Callback for handling response from the database

View File

@ -12,6 +12,11 @@ var config = require('../config.json')[adapterName];
// Set up the connection // Set up the connection
var pg = require(adapterName); var pg = require(adapterName);
var connection = new pg.Client(config.conn); var connection = new pg.Client(config.conn);
connection.connect(function(err) {
if (err) {
throw new Error(err);
}
});
// Set up the query builder object // Set up the query builder object
var nodeQuery = require('../../lib/node-query'); var nodeQuery = require('../../lib/node-query');
@ -20,14 +25,16 @@ var qb = nodeQuery('pg', connection, adapterName);
// Set up the test base // Set up the test base
testBase._setUp(qb, function(test, err, result) { testBase._setUp(qb, function(test, err, result) {
if (err) { if (err != null) {
throw new Error(err); //throw new Error(err);
console.error('SQL syntax error', err);
} }
test.ok.call(test, result); test.ok(result, 'Valid result for generated query');
test.done();
}); });
// Export the final test object
tests["pg adapter with query builder"] = function(test) { tests["pg adapter with query builder"] = function(test) {
test.ok(testBase.qb); test.ok(testBase.qb);
test.done(); test.done();

View File

@ -35,20 +35,20 @@ module.exports = (function() {
base.qb.select('id, COUNT(id) as count') base.qb.select('id, COUNT(id) as count')
.from('create_test') .from('create_test')
.groupBy('id') .groupBy('id')
.get(base.testCallback.bind(null, test)); .get(base.testCallback.bind(this, test));
}, },
'Basic select all get': function(test) { 'Basic select all get': function(test) {
base.qb.get('create_test', base.testCallback.bind(null, test)); base.qb.get('create_test', base.testCallback.bind(this, test));
}, },
'Basic select all with from': function(test) { 'Basic select all with from': function(test) {
base.qb.from('create_test') base.qb.from('create_test')
.get(base.testCallback.bind(null, test)); .get(base.testCallback.bind(this, test));
}, },
'Get with limit': function(test) { 'Get with limit': function(test) {
base.qb.get('create_test', 2, base.testCallback.bind(null, test)); base.qb.get('create_test', 2, base.testCallback.bind(this, test));
}, },
'Get with limit and offset': function(test) { 'Get with limit and offset': function(test) {
base.qb.get('create_test', 2, 1, base.testCallback.bind(null, test)); base.qb.get('create_test', 2, 1, base.testCallback.bind(this, test));
}, },
'Test get with having': function(test) { 'Test get with having': function(test) {
base.qb.select('id') base.qb.select('id')
@ -56,7 +56,7 @@ module.exports = (function() {
.groupBy('id') .groupBy('id')
.having({'id >':1}) .having({'id >':1})
.having('id !=', 3) .having('id !=', 3)
.get(base.testCallback.bind(null, test)); .get(base.testCallback.bind(this, test));
}, },
"Test get with 'orHaving'": function(test) { "Test get with 'orHaving'": function(test) {
base.qb.select('id') base.qb.select('id')
@ -64,7 +64,7 @@ module.exports = (function() {
.groupBy('id') .groupBy('id')
.having({'id >':1}) .having({'id >':1})
.orHaving('id !=', 3) .orHaving('id !=', 3)
.get(base.testCallback.bind(null, test)); .get(base.testCallback.bind(this, test));
} }
}, },
// ! Select tests // ! Select tests
@ -73,34 +73,34 @@ module.exports = (function() {
base.qb.select(['id', 'key as k', 'val']) base.qb.select(['id', 'key as k', 'val'])
.where('id >', 1) .where('id >', 1)
.where('id <', 900) .where('id <', 900)
.get('create_test', 2, 1, base.testCallback.bind(null, test)); .get('create_test', 2, 1, base.testCallback.bind(this, test));
}, },
'Select where get 2': function(test) { 'Select where get 2': function(test) {
base.qb.select('id, key as k, val') base.qb.select('id, key as k, val')
.where('id !=', 1) .where('id !=', 1)
.get('create_test', 2, 1, base.testCallback.bind(null, test)); .get('create_test', 2, 1, base.testCallback.bind(this, test));
}, },
'Multi Order By': function(test) { 'Multi Order By': function(test) {
base.qb.from('create_test') base.qb.from('create_test')
.orderBy('id, key') .orderBy('id, key')
.get(base.testCallback.bind(null, test)); .get(base.testCallback.bind(this, test));
}, },
'Select get': function(test) { 'Select get': function(test) {
base.qb.select('id, key as k, val') base.qb.select('id, key as k, val')
.get('create_test', 2, 1, base.testCallback.bind(null, test)); .get('create_test', 2, 1, base.testCallback.bind(this, test));
}, },
'Select from get': function(test) { 'Select from get': function(test) {
base.qb.select('id, key as k, val') base.qb.select('id, key as k, val')
.from('create_test ct') .from('create_test ct')
.where('id >', 1) .where('id >', 1)
.get(base.testCallback.bind(null, test)); .get(base.testCallback.bind(this, test));
}, },
'Select from limit get': function(test) { 'Select from limit get': function(test) {
base.qb.select('id, key as k, val') base.qb.select('id, key as k, val')
.from('create_test ct') .from('create_test ct')
.where('id >', 1) .where('id >', 1)
.limit(3) .limit(3)
.get(base.testCallback.bind(null, test)); .get(base.testCallback.bind(this, test));
} }
}, },
// ! Grouping tests // ! Grouping tests
@ -113,7 +113,7 @@ module.exports = (function() {
.where('id <', 900) .where('id <', 900)
.groupEnd() .groupEnd()
.limit(2, 1) .limit(2, 1)
.get(base.testCallback.bind(null, test)); .get(base.testCallback.bind(this, test));
}, },
'Using or grouping method': function(test) { 'Using or grouping method': function(test) {
base.qb.select('id, key as k, val') base.qb.select('id, key as k, val')
@ -126,7 +126,7 @@ module.exports = (function() {
.where('id', 0) .where('id', 0)
.groupEnd() .groupEnd()
.limit(2, 1) .limit(2, 1)
.get(base.testCallback.bind(null, test)); .get(base.testCallback.bind(this, test));
}, },
'Using or not grouping method': function(test) { 'Using or not grouping method': function(test) {
base.qb.select('id, key as k, val') base.qb.select('id, key as k, val')
@ -139,7 +139,7 @@ module.exports = (function() {
.where('id', 0) .where('id', 0)
.groupEnd() .groupEnd()
.limit(2, 1) .limit(2, 1)
.get(base.testCallback.bind(null, test)); .get(base.testCallback.bind(this, test));
} }
}, },
// ! Where in tests // ! Where in tests
@ -147,28 +147,28 @@ module.exports = (function() {
'Where in': function(test) { 'Where in': function(test) {
base.qb.from('create_test') base.qb.from('create_test')
.whereIn('id', [0, 6, 56, 563, 341]) .whereIn('id', [0, 6, 56, 563, 341])
.get(base.testCallback.bind(null, test)); .get(base.testCallback.bind(this, test));
}, },
'Or Where in': function(test) { 'Or Where in': function(test) {
base.qb.from('create_test') base.qb.from('create_test')
.where('key', 'false') .where('key', 'false')
.orWhereIn('id', [0, 6, 56, 563, 341]) .orWhereIn('id', [0, 6, 56, 563, 341])
.get(base.testCallback.bind(null, test)); .get(base.testCallback.bind(this, test));
}, },
'Where Not in': function(test) { 'Where Not in': function(test) {
base.qb.from('create_test') base.qb.from('create_test')
.where('key', 'false') .where('key', 'false')
.whereNotIn('id', [0, 6, 56, 563, 341]) .whereNotIn('id', [0, 6, 56, 563, 341])
.get(base.testCallback.bind(null, test)); .get(base.testCallback.bind(this, test));
}, },
'Or Where Not in': function(test) { 'Or Where Not in': function(test) {
base.qb.from('create_test') base.qb.from('create_test')
.where('key', 'false') .where('key', 'false')
.orWhereNotIn('id', [0, 6, 56, 563, 341]) .orWhereNotIn('id', [0, 6, 56, 563, 341])
.get(base.testCallback.bind(null, test)); .get(base.testCallback.bind(this, test));
} }
}, },
// ! Query modifier testss // ! Query modifier tests
'Query modifier tests': { 'Query modifier tests': {
'Order By': function(test) { 'Order By': function(test) {
base.qb.select('id, key as k, val') base.qb.select('id, key as k, val')
@ -178,19 +178,18 @@ module.exports = (function() {
.orderBy('id', 'DESC') .orderBy('id', 'DESC')
.orderBy('k', "ASC") .orderBy('k', "ASC")
.limit(5, 2) .limit(5, 2)
.get(base.testCallback.bind(null, test)); .get(base.testCallback.bind(this, test));
}, },
'Group by': function(test) { 'Group by': function(test) {
base.qb.select('id, key as k, val') base.qb.select('id, key as k, val')
.from('create_test') .from('create_test')
.where('id >', 0) .where('id >', 0)
.where('id <', 9000) .where('id <', 9000)
.groupBy('k') .groupBy('id')
.groupBy(['id', 'val'])
.orderBy('id', 'DESC') .orderBy('id', 'DESC')
.orderBy('k', "ASC") .orderBy('k', "ASC")
.limit(5, 2) .limit(5, 2)
.get(base.testCallback.bind(null, test)); .get(base.testCallback.bind(this, test));
}, },
'Or Where': function(test) { 'Or Where': function(test) {
base.qb.select('id, key as k, val') base.qb.select('id, key as k, val')
@ -198,70 +197,71 @@ module.exports = (function() {
.where(' id ', 1) .where(' id ', 1)
.orWhere('key >', 0) .orWhere('key >', 0)
.limit(2, 1) .limit(2, 1)
.get(base.testCallback.bind(null, test)); .get(base.testCallback.bind(this, test));
}, },
'Like' : function(test) { 'Like' : function(test) {
base.qb.from('create_test') base.qb.from('create_test')
.like('key', 'og') .like('key', 'og')
.get(base.testCallback.bind(null, test)); .get(base.testCallback.bind(this, test));
}, },
'Or Like': function(test) { 'Or Like': function(test) {
base.qb.from('create_test') base.qb.from('create_test')
.like('key', 'og') .like('key', 'og')
.orLike('key', 'val') .orLike('key', 'val')
.get(base.testCallback.bind(null, test)); .get(base.testCallback.bind(this, test));
}, },
'Not Like': function(test) { 'Not Like': function(test) {
base.qb.from('create_test') base.qb.from('create_test')
.like('key', 'og', 'before') .like('key', 'og', 'before')
.notLike('key', 'val') .notLike('key', 'val')
.get(base.testCallback.bind(null, test)); .get(base.testCallback.bind(this, test));
}, },
'Or Not Like': function(test) { 'Or Not Like': function(test) {
base.qb.from('create_test') base.qb.from('create_test')
.like('key', 'og', 'before') .like('key', 'og', 'before')
.orNotLike('key', 'val') .orNotLike('key', 'val')
.get(base.testCallback.bind(null, test)); .get(base.testCallback.bind(this, test));
}, },
'Like Before': function(test) { 'Like Before': function(test) {
base.qb.from('create_test') base.qb.from('create_test')
.like('key', 'og', 'before') .like('key', 'og', 'before')
.get(base.testCallback.bind(null, test)); .get(base.testCallback.bind(this, test));
}, },
'Like After': function(test) { 'Like After': function(test) {
base.qb.from('create_test') base.qb.from('create_test')
.like('key', 'og', 'after') .like('key', 'og', 'after')
.get(base.testCallback.bind(null, test)); .get(base.testCallback.bind(this, test));
}, },
'Basic Join': function(test) { 'Basic Join': function(test) {
base.qb.from('create_test ct') base.qb.from('create_test ct')
.join('create_join cj', 'cj.id=ct.id') .join('create_join cj', 'cj.id=ct.id')
.get(base.testCallback.bind(null, test)); .get(base.testCallback.bind(this, test));
}, },
'Left Join': function(test) { 'Left Join': function(test) {
base.qb.from('create_test ct') base.qb.from('create_test ct')
.join('create_join cj', 'cj.id=ct.id', 'left') .join('create_join cj', 'cj.id=ct.id', 'left')
.get(base.testCallback.bind(null, test)); .get(base.testCallback.bind(this, test));
}, },
'InnerJoin': function(test) { 'InnerJoin': function(test) {
base.qb.from('create_test ct') base.qb.from('create_test ct')
.join('create_join cj', 'cj.id=ct.id', 'inner') .join('create_join cj', 'cj.id=ct.id', 'inner')
.get(base.testCallback.bind(null, test)); .get(base.testCallback.bind(this, test));
} }
}, },
// ! DB Update test
'DB update tests' : { 'DB update tests' : {
'Test Insert': function(test) { 'Test Insert': function(test) {
base.qb.set('id', 98) base.qb.set('id', 98)
.set('key', 84) .set('key', 84)
.set('val', 120) .set('val', 120)
.insert('create_test', base.testCallback.bind(null, test)); .insert('create_test', base.testCallback.bind(this, test));
}, },
'Test Insert Object': function(test) { 'Test Insert Object': function(test) {
base.qb.insert('create_test', { base.qb.insert('create_test', {
id: 587, id: 587,
key: 1, key: 1,
val: 2 val: 2
}, base.testCallback.bind(null, test)); }, base.testCallback.bind(this, test));
}, },
'Test Update': function(test) { 'Test Update': function(test) {
base.qb.where('id', 7) base.qb.where('id', 7)
@ -269,7 +269,7 @@ module.exports = (function() {
id: 7, id: 7,
key: 'gogle', key: 'gogle',
val: 'non-word' val: 'non-word'
}, base.testCallback.bind(null, test)); }, base.testCallback.bind(this, test));
}, },
'Test set Array Update': function(test) { 'Test set Array Update': function(test) {
var object = { var object = {
@ -280,19 +280,20 @@ module.exports = (function() {
base.qb.set(object) base.qb.set(object)
.where('id', 22) .where('id', 22)
.update('create_test', base.testCallback.bind(null, test)); .update('create_test', base.testCallback.bind(this, test));
}, },
'Test where set update': function(test) { 'Test where set update': function(test) {
base.qb.where('id', 36) base.qb.where('id', 36)
.set('id', 36) .set('id', 36)
.set('key', 'gogle') .set('key', 'gogle')
.set('val', 'non-word') .set('val', 'non-word')
.update('create_test', base.testCallback.bind(null, test)); .update('create_test', base.testCallback.bind(this, test));
}, },
'Test delete': function(test) { 'Test delete': function(test) {
base.qb.delete('create_test', {id: 5}, base.testCallback.bind(null, test)); base.qb.delete('create_test', {id: 5}, base.testCallback.bind(this, test));
} }
}, },
// ! Compiled query tests
'Compiled query tests' : { 'Compiled query tests' : {
} }