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

Commit 7968f093 by Sascha Depold

sqlite query is almost eager-loading compatible

1 parent e6aefa7d
Showing with 16 additions and 16 deletions
...@@ -16,6 +16,10 @@ module.exports = (function() { ...@@ -16,6 +16,10 @@ module.exports = (function() {
} }
Utils.inherit(Query, AbstractQuery) Utils.inherit(Query, AbstractQuery)
Query.prototype.getInsertIdField = function() {
return 'lastID'
}
Query.prototype.run = function(sql) { Query.prototype.run = function(sql) {
var self = this var self = this
...@@ -75,34 +79,30 @@ module.exports = (function() { ...@@ -75,34 +79,30 @@ module.exports = (function() {
, self = this , self = this
// add the inserted row id to the instance // add the inserted row id to the instance
if (this.callee && (this.sql.indexOf('INSERT INTO') == 0) && metaData.hasOwnProperty('lastID')) { if (this.send('isInsertQuery', results, metaData)) {
var autoIncrementField = this.callee.__factory.autoIncrementField this.send('handleInsertQuery', results, metaData)
this.callee[autoIncrementField] = metaData.lastID
} }
if (this.sql.indexOf('sqlite_master') != -1) { if (this.sql.indexOf('sqlite_master') != -1) {
result = results.map(function(resultSet){ return resultSet.name }) result = results.map(function(resultSet) { return resultSet.name })
} else if (this.sql.indexOf('SELECT') == 0) { } else if (this.send('isSelectQuery')) {
// transform results into real model instances // we need to convert the timestamps into actual date objects
// return the first real model instance if options.plain is set (e.g. Model.find)
if(this.options.raw) { if(!this.options.raw) {
result = results results = results.map(function(result) {
} else {
result = results.map(function(result) {
for (var name in result) { for (var name in result) {
if (metaData.columnTypes[name] === 'DATETIME') { if (result.hasOwnProperty(name) && (metaData.columnTypes[name] === 'DATETIME')) {
result[name] = new Date(result[name]); result[name] = new Date(result[name]);
} }
} }
return self.callee.build(result, { isNewRecord: false }) return result
}) })
} }
if(this.options.plain) result = this.send('handleSelectQuery', results)
result = (result.length == 0) ? null : result[0] } else if (this.send('isShowOrDescribeQuery')) {
} else if((this.sql.indexOf('SHOW') == 0) || (this.sql.indexOf('DESCRIBE') == 0))
result = results result = results
}
this.emit('success', result) this.emit('success', result)
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!