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

View File

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

View File

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

View File

@ -792,6 +792,7 @@ var QueryBuilder = function(driver, adapter) {
/**
* Run the generated delete query
*
* @method delete
* @param {String} table - The table to insert into
* @param {Object} [where] - Where clause for delete statement
* @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
var pg = require(adapterName);
var connection = new pg.Client(config.conn);
connection.connect(function(err) {
if (err) {
throw new Error(err);
}
});
// Set up the query builder object
var nodeQuery = require('../../lib/node-query');
@ -20,14 +25,16 @@ var qb = nodeQuery('pg', connection, adapterName);
// Set up the test base
testBase._setUp(qb, function(test, err, result) {
if (err) {
throw new Error(err);
if (err != null) {
//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) {
test.ok(testBase.qb);
test.done();

View File

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