Skip to content

Commit

Permalink
(IMPROVE) Implement optimistic locking for updatePartialSql
Browse files Browse the repository at this point in the history
  • Loading branch information
fadrizul committed Jan 12, 2015
1 parent 0d55632 commit 5f566e6
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lib/driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,25 @@ var Driver = Class.extend({
}
}

if (model.columns["lockVersion"]) {
columnNamesSql.push(this.escapeColumnName('lock_version') + ' = lock_version + 1');
}

if (model.columns["updatedAt"]) {
columnNamesSql.push(this.escapeColumnName('updated_at') + ' = NOW()');
}

if (columnNamesSql.length === 0) {
throw new Error("No columns to update.");
}

var idColumndName = model.getIdPropertyName();
values.push(id);

var sql = util.format('UPDATE %s SET %s WHERE %s = %s', model.tableName, columnNamesSql.join(','), idColumndName, this.getValuesSubstitutionString(valueSubstitutionIndex++));
var defaultUpdateQuery = 'UPDATE %s SET %s WHERE %s = %s';
var sql = util.format(defaultUpdateQuery, model.tableName, columnNamesSql.join(','), idColumndName, this.getValuesSubstitutionString(valueSubstitutionIndex++));
var results = { sql: sql, values: values };
//console.log(results);

return results;
},

Expand Down

0 comments on commit 5f566e6

Please sign in to comment.