不要怂,就是干,撸起袖子干!

Commit 054c7569 by Jan Aagaard Meier

based omitNull on master

1 parent 31f6bd8e
......@@ -129,6 +129,16 @@ module.exports = (function() {
insertQuery: function(tableName, attrValueHash) {
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>);"
if(this.options.omitNull) {
_attrValueHash = {}
Utils._.each(attrValueHash, function(val, key) {
if(val !== null && val !== undefined) {
_attrValueHash[key] = val;
}
})
attrValueHash = _attrValueHash
}
var replacements = {
table: Utils.addTicks(tableName),
attributes: Utils._.keys(attrValueHash).map(function(attr){return Utils.addTicks(attr)}).join(","),
......@@ -142,6 +152,17 @@ module.exports = (function() {
updateQuery: function(tableName, values, where) {
var query = "UPDATE <%= table %> SET <%= values %> WHERE <%= where %>"
if(this.options.omitNull) {
_attrValueHash = {}
Utils._.each(values, function(val, key) {
if(val !== null && val !== undefined) {
_attrValueHash[key] = val;
}
})
values = _attrValueHash
}
var replacements = {
table: Utils.addTicks(tableName),
values: Utils._.map(values, function(value, key){
......
......@@ -53,6 +53,8 @@ function pgDataTypeMapping(tableName, attr, dataType) {
module.exports = (function() {
var QueryGenerator = {
options: {},
createTableQuery: function(tableName, attributes, options) {
options = Utils._.extend({
}, options || {})
......@@ -191,6 +193,16 @@ module.exports = (function() {
insertQuery: function(tableName, attrValueHash) {
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>) RETURNING *;"
if(this.options.omitNull) {
_attrValueHash = {}
Utils._.each(attrValueHash, function(val, key) {
if(val !== null && val !== undefined) {
_attrValueHash[key] = val;
}
})
attrValueHash = _attrValueHash
}
returning = []
Utils._.forEach(attrValueHash, function(value, key, hash) {
if (tables[tableName] && tables[tableName][key]) {
......@@ -216,6 +228,17 @@ module.exports = (function() {
updateQuery: function(tableName, values, where) {
var query = "UPDATE <%= table %> SET <%= values %> WHERE <%= where %>"
if(this.options.omitNull) {
_attrValueHash = {}
Utils._.each(values, function(val, key) {
if(val !== null && val !== undefined) {
_attrValueHash[key] = val;
}
})
values = _attrValueHash
}
var replacements = {
table: addQuotes(tableName),
values: Utils._.map(values, function(value, key){
......
......@@ -15,6 +15,8 @@ var escape = function(str) {
module.exports = (function() {
var QueryGenerator = {
options: {},
createTableQuery: function(tableName, attributes, options) {
options = options || {}
......@@ -44,6 +46,16 @@ module.exports = (function() {
insertQuery: function(tableName, attrValueHash) {
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>);";
if(this.options.omitNull) {
_attrValueHash = {}
Utils._.each(attrValueHash, function(val, key) {
if(val !== null && val !== undefined) {
_attrValueHash[key] = val;
}
})
attrValueHash = _attrValueHash
}
var replacements = {
table: Utils.addTicks(tableName),
attributes: Utils._.keys(attrValueHash).map(function(attr){return Utils.addTicks(attr)}).join(","),
......@@ -57,6 +69,16 @@ module.exports = (function() {
updateQuery: function(tableName, values, where) {
var query = "UPDATE <%= table %> SET <%= values %> WHERE <%= where %>"
if(this.options.omitNull) {
_attrValueHash = {}
Utils._.each(values, function(val, key) {
if(val !== null && val !== undefined) {
_attrValueHash[key] = val;
}
})
values = _attrValueHash
}
var replacements = {
table: Utils.addTicks(tableName),
values: Utils._.map(values, function(value, key){
......
......@@ -95,11 +95,15 @@ describe('QueryGenerator', function() {
}, {
arguments: ['myTable', {name: 'foo', foo: 1, nullValue: null}],
expectation: "INSERT INTO `myTable` (`name`,`foo`,`nullValue`) VALUES ('foo',1,NULL);",
context: {options: {ignoreNull: false}}
context: {options: {omitNull: false}}
}, {
arguments: ['myTable', {name: 'foo', foo: 1, nullValue: null}],
expectation: "INSERT INTO `myTable` (`name`,`foo`) VALUES ('foo',1);",
context: {options: {ignoreNull: true}}
context: {options: {omitNull: true}}
}, {
arguments: ['myTable', {name: 'foo', foo: 1, nullValue: undefined}],
expectation: "INSERT INTO `myTable` (`name`,`foo`) VALUES ('foo',1);",
context: {options: {omitNull: true}}
}
],
......@@ -122,11 +126,11 @@ describe('QueryGenerator', function() {
}, {
arguments: ['myTable', {bar: 2, nullValue: null}, {name: 'foo'}],
expectation: "UPDATE `myTable` SET `bar`=2,`nullValue`=NULL WHERE `name`='foo'",
context: {options: {ignoreNull: false}}
context: {options: {omitNull: false}}
}, {
arguments: ['myTable', {bar: 2, nullValue: null}, {name: 'foo'}],
expectation: "UPDATE `myTable` SET `bar`=2 WHERE `name`='foo'",
context: {options: {ignoreNull: true}}
context: {options: {omitNull: true}}
}
],
......
......@@ -85,6 +85,21 @@ describe('QueryGenerator', function() {
}, {
arguments: ['myTable', {name: 'foo', foo: 1}],
expectation: "INSERT INTO \"myTable\" (\"name\",\"foo\") VALUES ('foo',1) RETURNING *;"
}, {
arguments: ['myTable', {name: 'foo', nullValue: null}],
expectation: "INSERT INTO \"myTable\" (\"name\",\"nullValue\") VALUES ('foo',NULL) RETURNING *;"
}, {
arguments: ['myTable', {name: 'foo', nullValue: null}],
expectation: "INSERT INTO \"myTable\" (\"name\",\"nullValue\") VALUES ('foo',NULL) RETURNING *;",
context: {options: {omitNull: false}}
}, {
arguments: ['myTable', {name: 'foo', nullValue: null}],
expectation: "INSERT INTO \"myTable\" (\"name\") VALUES ('foo') RETURNING *;",
context: {options: {omitNull: true}}
}, {
arguments: ['myTable', {name: 'foo', nullValue: undefined}],
expectation: "INSERT INTO \"myTable\" (\"name\") VALUES ('foo') RETURNING *;",
context: {options: {omitNull: true}}
}
],
......@@ -101,7 +116,22 @@ describe('QueryGenerator', function() {
}, {
arguments: ['myTable', {name: "foo';DROP TABLE myTable;"}, {name: 'foo'}],
expectation: "UPDATE \"myTable\" SET \"name\"='foo\\';DROP TABLE myTable;' WHERE \"name\"='foo'"
}
}, {
arguments: ['myTable', {bar: 2, nullValue: null}, {name: 'foo'}],
expectation: "UPDATE \"myTable\" SET \"bar\"=2,\"nullValue\"=NULL WHERE \"name\"='foo'"
}, {
arguments: ['myTable', {bar: 2, nullValue: null}, {name: 'foo'}],
expectation: "UPDATE \"myTable\" SET \"bar\"=2,\"nullValue\"=NULL WHERE \"name\"='foo'",
context: {options: {omitNull: false}}
}, {
arguments: ['myTable', {bar: 2, nullValue: null}, {name: 'foo'}],
expectation: "UPDATE \"myTable\" SET \"bar\"=2 WHERE \"name\"='foo'",
context: {options: {omitNull: true}}
}, {
arguments: ['myTable', {bar: 2, nullValue: undefined}, {name: 'foo'}],
expectation: "UPDATE \"myTable\" SET \"bar\"=2 WHERE \"name\"='foo'",
context: {options: {omitNull: true}}
},
],
deleteQuery: [
......@@ -174,7 +204,9 @@ describe('QueryGenerator', function() {
tests.forEach(function(test) {
var title = test.title || 'correctly returns ' + test.expectation + ' for ' + util.inspect(test.arguments)
it(title, function() {
var conditions = QueryGenerator[suiteTitle].apply(null, test.arguments)
// Options would normally be set by the query interface that instantiates the query-generator, but here we specify it explicitly
var context = test.context || {options: {}};
var conditions = QueryGenerator[suiteTitle].apply(context, test.arguments)
expect(conditions).toEqual(test.expectation)
})
})
......
......@@ -28,6 +28,21 @@ describe('QueryGenerator', function() {
}, {
arguments: ['myTable', { name: "foo", value: false }],
expectation: "INSERT INTO `myTable` (`name`,`value`) VALUES ('foo',0);"
}, {
arguments: ['myTable', {name: 'foo', foo: 1, nullValue: null}],
expectation: "INSERT INTO `myTable` (`name`,`foo`,`nullValue`) VALUES ('foo',1,NULL);"
}, {
arguments: ['myTable', {name: 'foo', foo: 1, nullValue: null}],
expectation: "INSERT INTO `myTable` (`name`,`foo`,`nullValue`) VALUES ('foo',1,NULL);",
context: {options: {omitNull: false}}
}, {
arguments: ['myTable', {name: 'foo', foo: 1, nullValue: null}],
expectation: "INSERT INTO `myTable` (`name`,`foo`) VALUES ('foo',1);",
context: {options: {omitNull: true}}
}, {
arguments: ['myTable', {name: 'foo', foo: 1, nullValue: undefined}],
expectation: "INSERT INTO `myTable` (`name`,`foo`) VALUES ('foo',1);",
context: {options: {omitNull: true}}
}
],
......@@ -50,6 +65,17 @@ describe('QueryGenerator', function() {
}, {
arguments: ['myTable', { flag: false }, { id: 2 }],
expectation: "UPDATE `myTable` SET `flag`=0 WHERE `id`=2"
}, {
arguments: ['myTable', {bar: 2, nullValue: null}, {name: 'foo'}],
expectation: "UPDATE `myTable` SET `bar`=2,`nullValue`=NULL WHERE `name`='foo'"
}, {
arguments: ['myTable', {bar: 2, nullValue: null}, {name: 'foo'}],
expectation: "UPDATE `myTable` SET `bar`=2,`nullValue`=NULL WHERE `name`='foo'",
context: {options: {omitNull: false}}
}, {
arguments: ['myTable', {bar: 2, nullValue: null}, {name: 'foo'}],
expectation: "UPDATE `myTable` SET `bar`=2 WHERE `name`='foo'",
context: {options: {omitNull: true}}
}
]
};
......@@ -59,7 +85,10 @@ describe('QueryGenerator', function() {
tests.forEach(function(test) {
var title = test.title || 'correctly returns ' + test.expectation + ' for ' + util.inspect(test.arguments)
it(title, function() {
var conditions = QueryGenerator[suiteTitle].apply(null, test.arguments)
// Options would normally be set by the query interface that instantiates the query-generator, but here we specify it explicitly
var context = test.context || {options: {}};
var conditions = QueryGenerator[suiteTitle].apply(context, test.arguments)
expect(conditions).toEqual(test.expectation)
})
})
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!