mirror of
https://github.com/knex/knex.git
synced 2025-11-02 10:49:39 +00:00
Allow for returning in update queries, #132
This commit is contained in:
parent
10e3d398f4
commit
06c38bcc2d
@ -38,7 +38,20 @@ exports.grammar = _.defaults({
|
||||
}
|
||||
sql += "(" + this.columnize(columns) + ") values " + paramBlocks.join(', ');
|
||||
}
|
||||
sql += this.compileReturning(qb);
|
||||
return sql;
|
||||
},
|
||||
|
||||
// Compiles an `update` query, allowing for a return value.
|
||||
compileUpdate: function(qb) {
|
||||
var sql = baseGrammar.compileUpdate.apply(this, arguments);
|
||||
sql += this.compileReturning(qb);
|
||||
return sql;
|
||||
},
|
||||
|
||||
// Adds the returning value to the statement.
|
||||
compileReturning: function(qb) {
|
||||
var sql = '';
|
||||
if (qb.flags.returning) {
|
||||
if (_.isArray(qb.flags.returning)) {
|
||||
sql += ' returning ' + this.wrapArray(qb.flags.returning);
|
||||
@ -51,9 +64,9 @@ exports.grammar = _.defaults({
|
||||
|
||||
// Ensures the response is returned in the same format as other clients.
|
||||
handleResponse: function(builder, response) {
|
||||
var returning = builder.flags.returning;
|
||||
if (response.command === 'SELECT') return response.rows;
|
||||
if (response.command === 'INSERT') {
|
||||
var returning = builder.flags.returning;
|
||||
if (response.command === 'INSERT' || (response.command === 'UPDATE' && returning)) {
|
||||
return _.map(response.rows, function(row) {
|
||||
if (returning === '*' || _.isArray(returning)) return row;
|
||||
return row[returning];
|
||||
|
||||
@ -404,7 +404,8 @@ _.extend(Builder.prototype, Common, {
|
||||
},
|
||||
|
||||
// Sets the values for an `update` query.
|
||||
update: function(values) {
|
||||
update: function(values, returning) {
|
||||
if (returning) this.returning(returning);
|
||||
var obj = Helpers.sortObject(values);
|
||||
var bindings = [];
|
||||
for (var i = 0, l = obj.length; i < l; i++) {
|
||||
|
||||
@ -50,6 +50,16 @@ module.exports = function(knex) {
|
||||
|
||||
});
|
||||
|
||||
it('should allow returning for updates in postgresql', function() {
|
||||
|
||||
return knex('accounts').logMe().where('id', 1).update({
|
||||
first_name: 'UpdatedUser',
|
||||
last_name: 'UpdatedTest',
|
||||
email:'test100@example.com'
|
||||
}, '*');
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
};
|
||||
@ -15,5 +15,32 @@ module.exports = {
|
||||
sql: 'update "accounts" set "email" = ?, "first_name" = ?, "last_name" = ? where "id" = ?',
|
||||
result: 1
|
||||
}
|
||||
},
|
||||
'should allow returning for updates in postgresql': {
|
||||
mysql: {
|
||||
bindings: ['test100@example.com','UpdatedUser','UpdatedTest',1],
|
||||
sql: 'update `accounts` set `email` = ?, `first_name` = ?, `last_name` = ? where `id` = ?',
|
||||
result: 1
|
||||
},
|
||||
postgresql: {
|
||||
bindings: ['test100@example.com','UpdatedUser','UpdatedTest',1],
|
||||
sql: 'update "accounts" set "email" = ?, "first_name" = ?, "last_name" = ? where "id" = ? returning *',
|
||||
result: [{
|
||||
id: '1',
|
||||
first_name: 'UpdatedUser',
|
||||
last_name: 'UpdatedTest',
|
||||
email: 'test100@example.com',
|
||||
logins: 1,
|
||||
about: 'Lorem ipsum Dolore labore incididunt enim.',
|
||||
created_at: new Date(),
|
||||
updated_at: new Date(),
|
||||
phone: null
|
||||
}]
|
||||
},
|
||||
sqlite3: {
|
||||
bindings: ['test100@example.com','UpdatedUser','UpdatedTest',1],
|
||||
sql: 'update "accounts" set "email" = ?, "first_name" = ?, "last_name" = ? where "id" = ?',
|
||||
result: 1
|
||||
}
|
||||
}
|
||||
};
|
||||
Loading…
x
Reference in New Issue
Block a user