Fix inserting, updating, and deleting methods

This commit is contained in:
Timothy Warren 2014-10-27 13:36:10 -04:00
parent 81c7526bc6
commit 83af1d3946
5 changed files with 79 additions and 13 deletions

View File

@ -10,6 +10,7 @@ var helpers = require('./helpers');
var d = { var d = {
identifierChar: '"', identifierChar: '"',
tablePrefix: null, tablePrefix: null,
hasTruncate: true,
/** /**
* Low level function for naive quoting of strings * Low level function for naive quoting of strings
@ -139,6 +140,22 @@ var d = {
} }
return raw; return raw;
},
/**
* SQL to truncate the passed table
*
* @param {String} table
* @return {String} - sql
*/
truncate: function(table) {
var sql = (d.hasTruncate)
? 'TRUNCATE '
: 'DELETE FROM ';
sql += d.quoteTable(table);
return sql;
} }
}; };

View File

@ -25,6 +25,9 @@ var QueryBuilder = function(driver, adapter) {
var parser = require('./query-parser')(driver); var parser = require('./query-parser')(driver);
this.driver = driver;
this.adapter = adapter;
/** /**
* "Private" methods * "Private" methods
* *
@ -776,13 +779,14 @@ var QueryBuilder = function(driver, adapter) {
* @param {Function} callback - Callback for handling response from the database * @param {Function} callback - Callback for handling response from the database
* @return void * @return void
*/ */
this.update = function(table, data, callback) { this.update = function(/*table, data, callback*/) {
if (data) { var args = getArgs('table:string, [data]:object, callback:function', arguments);
this.set(data); if (args.data) {
this.set(args.data);
} }
// Run the query // Run the query
_p.run('update', table, callback); _p.run('update', args.table, args.callback);
}; };
/** /**
@ -792,9 +796,16 @@ var QueryBuilder = function(driver, adapter) {
* @param {Function} callback - Callback for handling response from the database * @param {Function} callback - Callback for handling response from the database
* @return void * @return void
*/ */
this['delete'] = function (table, callback) { this['delete'] = function (/*table, [where], callback*/) {
var args = getArgs('table:string, [where], callback:function', arguments);
if (args.where)
{
this.where(args.where);
}
// Run the query // Run the query
_p.run('delete', table, callback); _p.run('delete', args.table, args.callback);
}; };
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------

View File

@ -19,7 +19,8 @@ var qb = nodeQuery('mysql', connection, adapterName);
// Set up the test base // Set up the test base
testBase._setUp(qb, function(test, err, rows) { testBase._setUp(qb, function(test, err, rows) {
if (err) { if (err != null) {
test.done();
throw new Error(err); throw new Error(err);
} }

View File

@ -19,7 +19,8 @@ var qb = nodeQuery('mysql', connection, adapterName);
// Set up the test base // Set up the test base
testBase._setUp(qb, function(test, err, rows) { testBase._setUp(qb, function(test, err, rows) {
if (err) { if (err != null) {
test.done();
throw new Error(err); throw new Error(err);
} }

View File

@ -19,6 +19,16 @@ module.exports = (function() {
* Generic query builder tests * Generic query builder tests
*/ */
base.tests = { base.tests = {
setUp: function(callback) {
var sql = base.qb.driver.truncate('create_test');
base.qb.adapter.execute(sql, function(err, result) {
if (err) {
throw new Error(err);
}
callback();
});
},
// ! Get tests // ! Get tests
'Get tests' : { 'Get tests' : {
'Get with function': function(test) { 'Get with function': function(test) {
@ -240,7 +250,7 @@ module.exports = (function() {
} }
}, },
'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)
@ -252,13 +262,39 @@ module.exports = (function() {
key: 1, key: 1,
val: 2 val: 2
}, base.testCallback.bind(null, test)); }, base.testCallback.bind(null, test));
}*/ },
'Test Update': function(test) {
base.qb.where('id', 7)
.update('create_test', {
id: 7,
key: 'gogle',
val: 'non-word'
}, base.testCallback.bind(null, test));
},
'Test set Array Update': function(test) {
var object = {
id: 22,
key: 'gogle',
val: 'non-word'
};
base.qb.set(object)
.where('id', 22)
.update('create_test', base.testCallback.bind(null, 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));
},
'Test delete': function(test) {
base.qb.delete('create_test', {id: 5}, base.testCallback.bind(null, test));
}
}, },
'Compiled query tests' : { 'Compiled query tests' : {
},
'Error tests' : {
} }
}; };