mirror of
https://github.com/knex/knex.git
synced 2025-11-15 01:24:25 +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.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 (qb.flags.returning) {
|
||||||
if (_.isArray(qb.flags.returning)) {
|
if (_.isArray(qb.flags.returning)) {
|
||||||
sql += ' returning ' + this.wrapArray(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.
|
// Ensures the response is returned in the same format as other clients.
|
||||||
handleResponse: function(builder, response) {
|
handleResponse: function(builder, response) {
|
||||||
|
var returning = builder.flags.returning;
|
||||||
if (response.command === 'SELECT') return response.rows;
|
if (response.command === 'SELECT') return response.rows;
|
||||||
if (response.command === 'INSERT') {
|
if (response.command === 'INSERT' || (response.command === 'UPDATE' && returning)) {
|
||||||
var returning = builder.flags.returning;
|
|
||||||
return _.map(response.rows, function(row) {
|
return _.map(response.rows, function(row) {
|
||||||
if (returning === '*' || _.isArray(returning)) return row;
|
if (returning === '*' || _.isArray(returning)) return row;
|
||||||
return row[returning];
|
return row[returning];
|
||||||
|
|||||||
@ -404,7 +404,8 @@ _.extend(Builder.prototype, Common, {
|
|||||||
},
|
},
|
||||||
|
|
||||||
// Sets the values for an `update` query.
|
// 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 obj = Helpers.sortObject(values);
|
||||||
var bindings = [];
|
var bindings = [];
|
||||||
for (var i = 0, l = obj.length; i < l; i++) {
|
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" = ?',
|
sql: 'update "accounts" set "email" = ?, "first_name" = ?, "last_name" = ? where "id" = ?',
|
||||||
result: 1
|
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