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

Commit 6436c021 by sdepold

fixed insertion id in sqlite

1 parent 10f291b4
Showing with 11 additions and 5 deletions
......@@ -23,8 +23,12 @@ module.exports = (function() {
console.log('Executing: ' + this.sql)
this.database.serialize(function() {
self.database.all(self.sql, function(err, results) {
err ? onFailure.call(self, err) : onSuccess.call(self, results)
var isInsertCommand = (self.sql.toLowerCase().indexOf('insert') == 0)
, isUpdateCommand = (self.sql.toLowerCase().indexOf('update') == 0)
, databaseMethod = (isInsertCommand || isUpdateCommand) ? 'run' : 'all'
self.database[databaseMethod](self.sql, function(err, results) {
err ? onFailure.call(self, err) : onSuccess.call(self, results, this)
})
})
......@@ -43,13 +47,15 @@ module.exports = (function() {
//private
var onSuccess = function(results) {
var onSuccess = function(results, metaData) {
var result = this.callee
, self = this
// add the inserted row id to the instance
if (this.callee && (this.sql.indexOf('INSERT INTO') == 0) && (results.hasOwnProperty('insertId')))
this.callee[this.callee.__definition.autoIncrementField] = results.insertId
if (this.callee && (this.sql.indexOf('INSERT INTO') == 0) && metaData.hasOwnProperty('lastID')) {
var autoIncrementField = this.callee.__definition.autoIncrementField
this.callee[autoIncrementField] = metaData.lastID
}
if (this.sql.indexOf('sqlite_master') != -1) {
result = results.map(function(resultSet){ return resultSet.name })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!