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

Commit a8f043b8 by sdepold

Merge branch 'fix-sqlite-date' of https://github.com/iizukanao/sequelize into ii…

…zukanao-fix-sqlite-date
2 parents c0fd2af4 b8d05967
...@@ -31,16 +31,38 @@ module.exports = (function() { ...@@ -31,16 +31,38 @@ module.exports = (function() {
this.options.logging('Executing: ' + this.sql) this.options.logging('Executing: ' + this.sql)
} }
var columnTypes = {};
this.database.serialize(function() { 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) var isInsertCommand = (self.sql.toLowerCase().indexOf('insert') == 0)
, isUpdateCommand = (self.sql.toLowerCase().indexOf('update') == 0) , isUpdateCommand = (self.sql.toLowerCase().indexOf('update') == 0)
, databaseMethod = (isInsertCommand || isUpdateCommand) ? 'run' : 'all' , databaseMethod = (isInsertCommand || isUpdateCommand) ? 'run' : 'all'
if (databaseMethod === 'all' && /select\s.*?\sfrom\s+([^ ;]+)/i.test(self.sql)) {
self.database[databaseMethod](self.sql, function(err, results) { var tableName = RegExp.$1;
//allow clients to listen to sql to do their own logging or whatnot if (tableName !== 'sqlite_master') {
self.emit('sql', self.sql) // get the column types
err ? onFailure.call(self, err) : onSuccess.call(self, results, this) 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 return this
...@@ -68,6 +90,11 @@ module.exports = (function() { ...@@ -68,6 +90,11 @@ module.exports = (function() {
result = results result = results
} else { } else {
result = results.map(function(result) { 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 }) return self.callee.build(result, { isNewRecord: false })
}) })
} }
......
...@@ -20,7 +20,10 @@ describe('DAO', function() { ...@@ -20,7 +20,10 @@ describe('DAO', function() {
var setup = function() { var setup = function() {
Helpers.async(function(done) { Helpers.async(function(done) {
User = sequelize.define('User', { username: Sequelize.STRING }) User = sequelize.define('User', {
username: Sequelize.STRING,
birthDate: Sequelize.DATE
})
User.sync({ force: true }).success(done) User.sync({ force: true }).success(done)
}) })
} }
...@@ -332,7 +335,10 @@ describe('DAO', function() { ...@@ -332,7 +335,10 @@ describe('DAO', function() {
describe('save', function() { describe('save', function() {
it("stores an entry in the database", function() { it("stores an entry in the database", function() {
var username = 'user' var username = 'user'
, user = User.build({ username: username }) , user = User.build({
username: username,
birthDate: new Date(1984, 8, 23)
})
Helpers.async(function(done) { Helpers.async(function(done) {
User.all().success(function(users) { User.all().success(function(users) {
...@@ -349,6 +355,8 @@ describe('DAO', function() { ...@@ -349,6 +355,8 @@ describe('DAO', function() {
User.all().success(function(users) { User.all().success(function(users) {
expect(users.length).toEqual(1) expect(users.length).toEqual(1)
expect(users[0].username).toEqual(username) expect(users[0].username).toEqual(username)
expect(users[0].birthDate instanceof Date).toBe(true)
expect(users[0].birthDate.getTime()).toEqual(new Date(1984, 8, 23).getTime())
done() done()
}) })
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!