Skip to content

Commit 3272f9b

Browse files
committed
support ARRAY[] in UPDATE set argument
1 parent 757ae87 commit 3272f9b

File tree

2 files changed

+41
-10
lines changed

2 files changed

+41
-10
lines changed

index.js

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,7 @@ function UPDATE(table) {
2929

3030
UPDATE.prototype = {
3131
SET: function(set) {
32-
// build set clause:
33-
var set_columns = Object.keys(set),
34-
set_clause = set_columns.map(function(c,i) {
35-
return c + ' = $' + (i+1);
36-
}),
37-
set_values = set_columns.map(function(c){ return set[c]; });
38-
this.text += ' SET ' + set_clause.join(', ');
39-
this.values.push.apply(this.values, set_values);
32+
this.text += ' SET ' + get_set_clause(set, this.values);
4033
return this;
4134
},
4235
WHERE: function(where,conjunction) {
@@ -176,16 +169,34 @@ DELETE.prototype = {
176169
}
177170
};
178171

172+
function get_set_placeholder(value, values) {
173+
if (Array.isArray(value)) {
174+
return 'ARRAY[' + value.map(function(item){
175+
return get_set_placeholder(item, values);
176+
}).join(', ') + ']'
177+
} else {
178+
values.push(value);
179+
return '$' + values.length;
180+
}
181+
}
182+
183+
// only used in UPDATE, but here for clarity/efficiency
184+
function get_set_clause(set, values) {
185+
return Object.keys(set).map(function(c,i){
186+
return c + ' = ' + get_set_placeholder(set[c], values);
187+
}).join(', ');
188+
}
189+
179190
// used in UPDATE, SELECT and DELETE
180191
// can handle where objects like so:
181192
// { foo: [1,2,3,4], bar: NOT_NULL, baz: null }
182193
function get_where_clause(where, values, conjunction){
183-
return Object.keys(where).map(function(c,i) {
194+
return Object.keys(where).map(function column_to_where(c,i) {
184195
var value = where[c];
185196
if (Array.isArray(value)) {
186197
return c + ' IN (' + value.map(function(v,i) {
187198
values.push(v);
188-
return '$'+values.length;
199+
return '$' + values.length;
189200
}).join(', ') + ')';
190201
} else if (value === null) {
191202
return c + ' IS NULL';

test/update.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,24 @@ describe('UPDATE', function(){
6868
sql.values.should.eql([1,2,3,4,5,6]);
6969
});
7070
});
71+
describe('#(table).SET(set_array).WHERE(where_array)', function(){
72+
it('should identify arrays in the set object and generate correct placeholders and flattened values', function(){
73+
var sql = UPDATE('foo').SET({ a: 1, b: [ 2, 3, 4 ], c: 5 }).WHERE({ d: 6, e: [ 7, 8 ] });
74+
sql.should.have.text;
75+
sql.should.have.values;
76+
sql.text.should.equal('UPDATE foo SET a = $1, b = ARRAY[$2, $3, $4], c = $5 WHERE d = $6 AND e IN ($7, $8)');
77+
sql.values.should.be.an.instanceOf(Array)
78+
sql.values.should.eql([1,2,3,4,5,6,7,8]);
79+
});
80+
});
81+
describe('#(table).SET(set_nested_array).WHERE(where_array)', function(){
82+
it('should identify nested arrays in the set object and generate correct placeholders and flattened values', function(){
83+
var sql = UPDATE('foo').SET({ a: 1, b: [ [ 2, 3 ], [ 4, 5 ] ], c: 6 }).WHERE({ d: 7, e: [ 8, 9 ] });
84+
sql.should.have.text;
85+
sql.should.have.values;
86+
sql.text.should.equal('UPDATE foo SET a = $1, b = ARRAY[ARRAY[$2, $3], ARRAY[$4, $5]], c = $6 WHERE d = $7 AND e IN ($8, $9)');
87+
sql.values.should.be.an.instanceOf(Array)
88+
sql.values.should.eql([1,2,3,4,5,6,7,8,9]);
89+
});
90+
});
7191
});

0 commit comments

Comments
 (0)