sqlquerybuilder
Advanced tools
Comparing version 0.0.23 to 0.0.24
module.exports = function cudMethods(self) { | ||
if (typeof String.prototype.endsWith !== 'function') { | ||
String.prototype.endsWith = function (suffix) { | ||
return this.indexOf(suffix, this.length - suffix.length) !== -1; | ||
}; | ||
} | ||
self.delete = function remove(table, alias) { | ||
@@ -31,2 +37,4 @@ self._sqlObject.delete = 1; | ||
var colNames = [], vals = []; | ||
if (valuesInput instanceof Array) { | ||
@@ -36,2 +44,3 @@ self._handleArrayOfObjects(valuesInput); | ||
var objs = Object.keys(valuesInput); | ||
for (var i = 0; i < objs.length; i++) { | ||
@@ -41,16 +50,20 @@ if (typeof(valuesInput[objs[i]]) == 'object') { | ||
} | ||
self._sqlObject.columns += objs[i] + ((i == objs.length - 1) ? ")" : ", " ); | ||
if (valuesInput[objs[i]] !== undefined) { | ||
if (typeof (valuesInput[objs[i]]) == 'string' && valuesInput[objs[i]].indexOf("()") == -1 && valuesInput[objs[i]].charAt(0) != '\'') { //makes sure it's a string and not a function | ||
self._sqlObject.values += "'" + valuesInput[objs[i]].replace(/'/g, "''") + "'"; //makes it a string | ||
colNames.push(objs[i]); | ||
if (typeof (valuesInput[objs[i]]) == 'string' && valuesInput[objs[i]].indexOf("()") == -1 && valuesInput[objs[i]].charAt(0) != '\'') { //makes sure it's a string and not a function | ||
vals.push("'" + valuesInput[objs[i]].replace(/'/g, "''") + "'"); //makes it a string | ||
} | ||
else if (typeof(valuesInput[objs[i]]) == 'boolean') { | ||
vals.push(valuesInput[objs[i]] ? 1 : 0); | ||
} | ||
else { | ||
vals.push(valuesInput[objs[i]]); | ||
} | ||
} | ||
else if (typeof(valuesInput[objs[i]]) == 'boolean') { | ||
self._sqlObject.values += (valuesInput[objs[i]] ? 1 : 0); | ||
} | ||
else { | ||
self._sqlObject.values += valuesInput[objs[i]]; | ||
} | ||
} | ||
self._sqlObject.values += ((i == objs.length - 1) ? ")" : ", " ); | ||
} | ||
self._sqlObject.columns += colNames.join(', ') + ')'; | ||
self._sqlObject.values += vals.join(', ') + ')'; | ||
} | ||
@@ -57,0 +70,0 @@ else { |
{ | ||
"name": "sqlquerybuilder", | ||
"version": "0.0.23", | ||
"version": "0.0.24", | ||
"description": "Highly opinionated Sql Server Query Writer, mostly for internal use.", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index", |
@@ -118,2 +118,12 @@ /** | ||
}); | ||
it('Should not try and put column value pair when value is undefined', function (done) { | ||
var obj = {Id: 'NewId()', IsActive: true, Date: 'GetDate()', Thing: {}, Text: "test", Thing2: undefined}; | ||
(sqlBuilder() | ||
.insertInto("aTable") | ||
.values(obj) | ||
.build()) | ||
.should.equal("INSERT INTO aTable (Id, IsActive, Date, Text) VALUES(NewId(), 1, GetDate(), 'test') "); | ||
done(); | ||
}); | ||
}); |
178466
3801