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

Commit b8d05967 by Nao Iizuka

decode DATETIME to Date object in sqlite

1 parent f680021a
Showing with 33 additions and 6 deletions
......@@ -20,16 +20,38 @@ module.exports = (function() {
if(this.options.logging !== false)
this.options.logging('Executing: ' + this.sql)
var columnTypes = {};
this.database.serialize(function() {
var executeSql = function() {
self.database[databaseMethod](self.sql, function(err, results) {
//allow clients to listen to sql to do their own logging or whatnot
self.emit('sql', self.sql)
this.columnTypes = columnTypes;
err ? onFailure.call(self, err) : onSuccess.call(self, results, this)
})
};
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) {
//allow clients to listen to sql to do their own logging or whatnot
self.emit('sql', self.sql)
err ? onFailure.call(self, err) : onSuccess.call(self, results, this)
})
if (databaseMethod === 'all' && /select\s.*?\sfrom\s+([^ ;]+)/i.test(self.sql)) {
var tableName = RegExp.$1;
if (tableName !== 'sqlite_master') {
// get the column types
self.database.all("PRAGMA table_info(" + tableName + ")", function(err, results) {
if (!err) {
for (var i=0, l=results.length; i<l; i++) {
columnTypes[results[i].name] = results[i].type;
}
}
executeSql();
});
} else {
executeSql();
}
} else {
executeSql();
}
})
return this
......@@ -57,6 +79,11 @@ module.exports = (function() {
result = results
} else {
result = results.map(function(result) {
for (var name in result) {
if (metaData.columnTypes[name] === 'DATETIME') {
result[name] = new Date(result[name]);
}
}
return self.callee.build(result, { isNewRecord: false })
})
}
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!