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

Commit a12b5954 by Mick Hansen

Merge branch 'master' of github.com:sequelize/sequelize

2 parents 13b9494b b1f666e9
...@@ -442,13 +442,16 @@ module.exports = (function() { ...@@ -442,13 +442,16 @@ module.exports = (function() {
DAOFactory.prototype.findAll = function(options, queryOptions) { DAOFactory.prototype.findAll = function(options, queryOptions) {
var hasJoin = false var hasJoin = false
, tableNames = { }
tableNames[this.tableName] = true
options = optClone(options) options = optClone(options)
if (typeof options === 'object') { if (typeof options === 'object') {
if (options.hasOwnProperty('include') && options.include) { if (options.hasOwnProperty('include') && options.include) {
hasJoin = true hasJoin = true
validateIncludedElements.call(this, options) validateIncludedElements.call(this, options, tableNames)
} }
// whereCollection is used for non-primary key updates // whereCollection is used for non-primary key updates
...@@ -459,7 +462,8 @@ module.exports = (function() { ...@@ -459,7 +462,8 @@ module.exports = (function() {
return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({ return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({
type: QueryTypes.SELECT, type: QueryTypes.SELECT,
hasJoin: hasJoin hasJoin: hasJoin,
tableNames: Object.keys(tableNames)
}, queryOptions, { transaction: (options || {}).transaction })) }, queryOptions, { transaction: (options || {}).transaction }))
} }
...@@ -498,6 +502,9 @@ module.exports = (function() { ...@@ -498,6 +502,9 @@ module.exports = (function() {
var primaryKeys = this.primaryKeys var primaryKeys = this.primaryKeys
, keys = Object.keys(primaryKeys) , keys = Object.keys(primaryKeys)
, keysLength = keys.length , keysLength = keys.length
, tableNames = { }
tableNames[this.tableName] = true
// options is not a hash but an id // options is not a hash but an id
if (typeof options === 'number') { if (typeof options === 'number') {
...@@ -534,7 +541,7 @@ module.exports = (function() { ...@@ -534,7 +541,7 @@ module.exports = (function() {
if (options.hasOwnProperty('include') && options.include) { if (options.hasOwnProperty('include') && options.include) {
hasJoin = true hasJoin = true
validateIncludedElements.call(this, options) validateIncludedElements.call(this, options, tableNames)
} }
// whereCollection is used for non-primary key updates // whereCollection is used for non-primary key updates
...@@ -559,7 +566,8 @@ module.exports = (function() { ...@@ -559,7 +566,8 @@ module.exports = (function() {
return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({ return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({
plain: true, plain: true,
type: QueryTypes.SELECT, type: QueryTypes.SELECT,
hasJoin: hasJoin hasJoin: hasJoin,
tableNames: Object.keys(tableNames)
}, queryOptions, { transaction: (options || {}).transaction })) }, queryOptions, { transaction: (options || {}).transaction }))
} }
...@@ -1328,13 +1336,14 @@ module.exports = (function() { ...@@ -1328,13 +1336,14 @@ module.exports = (function() {
}.bind(this)) }.bind(this))
} }
var validateIncludedElements = function(options) { var validateIncludedElements = function(options, tableNames) {
tableNames = tableNames || {}
options.includeNames = [] options.includeNames = []
options.includeMap = {} options.includeMap = {}
options.hasSingleAssociation = false options.hasSingleAssociation = false
options.hasMultiAssociation = false options.hasMultiAssociation = false
options.include = options.include.map(function(include) { options.include = options.include.map(function(include) {
include = validateIncludedElement.call(this, include, options.daoFactory) include = validateIncludedElement.call(this, include, options.daoFactory, tableNames)
options.includeMap[include.as] = include options.includeMap[include.as] = include
options.includeNames.push(include.as) options.includeNames.push(include.as)
...@@ -1345,12 +1354,12 @@ module.exports = (function() { ...@@ -1345,12 +1354,12 @@ module.exports = (function() {
options.hasIncludeWhere = options.hasIncludeWhere || include.hasIncludeWhere || !!include.where options.hasIncludeWhere = options.hasIncludeWhere || include.hasIncludeWhere || !!include.where
options.hasIncludeRequired = options.hasIncludeRequired || include.hasIncludeRequired || !!include.required options.hasIncludeRequired = options.hasIncludeRequired || include.hasIncludeRequired || !!include.required
return include return include
}.bind(this)) }.bind(this))
}; };
var validateIncludedElement = function(include, parent) { var validateIncludedElement = function(include, parent, tableNames) {
if (include instanceof DAOFactory) { if (include instanceof DAOFactory) {
include = { daoFactory: include } include = { daoFactory: include }
} }
...@@ -1370,6 +1379,8 @@ module.exports = (function() { ...@@ -1370,6 +1379,8 @@ module.exports = (function() {
throw new Error('Include malformed. Expected attributes: daoFactory, as!') throw new Error('Include malformed. Expected attributes: daoFactory, as!')
} }
tableNames[include.daoFactory.tableName] = true
if (include.hasOwnProperty('attributes')) { if (include.hasOwnProperty('attributes')) {
var primaryKeys; var primaryKeys;
if (include.daoFactory.hasPrimaryKeys) { if (include.daoFactory.hasPrimaryKeys) {
...@@ -1389,7 +1400,7 @@ module.exports = (function() { ...@@ -1389,7 +1400,7 @@ module.exports = (function() {
if (include._pseudo) return include if (include._pseudo) return include
// check if the current daoFactory is actually associated with the passed daoFactory - or it's a pseudo include // check if the current daoFactory is actually associated with the passed daoFactory - or it's a pseudo include
var association = parent.getAssociation(include.daoFactory, include.as) var association = parent.getAssociation(include.daoFactory, include.as)
if (association) { if (association) {
include.association = association include.association = association
include.as = association.as include.as = association.as
...@@ -1409,6 +1420,7 @@ module.exports = (function() { ...@@ -1409,6 +1420,7 @@ module.exports = (function() {
} }
include.include.push(include.through) include.include.push(include.through)
tableNames[through.tableName] = true
} }
if (include.required === undefined) { if (include.required === undefined) {
...@@ -1417,7 +1429,7 @@ module.exports = (function() { ...@@ -1417,7 +1429,7 @@ module.exports = (function() {
// Validate child includes // Validate child includes
if (include.hasOwnProperty('include')) { if (include.hasOwnProperty('include')) {
validateIncludedElements(include) validateIncludedElements(include, tableNames)
} }
return include return include
......
...@@ -35,7 +35,7 @@ module.exports = (function() { ...@@ -35,7 +35,7 @@ module.exports = (function() {
}, },
createSchema: function() { createSchema: function() {
var query = "SELECT name FROM sqlite_master WHERE type='table' and name!='sqlite_sequence';" var query = "SELECT name FROM `sqlite_master` WHERE type='table' and name!='sqlite_sequence';"
return Utils._.template(query)({}) return Utils._.template(query)({})
}, },
...@@ -44,7 +44,7 @@ module.exports = (function() { ...@@ -44,7 +44,7 @@ module.exports = (function() {
}, },
showSchemasQuery: function() { showSchemasQuery: function() {
return "SELECT name FROM sqlite_master WHERE type='table' and name!='sqlite_sequence';" return "SELECT name FROM `sqlite_master` WHERE type='table' and name!='sqlite_sequence';"
}, },
createTableQuery: function(tableName, attributes, options) { createTableQuery: function(tableName, attributes, options) {
...@@ -169,7 +169,7 @@ module.exports = (function() { ...@@ -169,7 +169,7 @@ module.exports = (function() {
}, },
showTablesQuery: function() { showTablesQuery: function() {
return "SELECT name FROM sqlite_master WHERE type='table' and name!='sqlite_sequence';" return "SELECT name FROM `sqlite_master` WHERE type='table' and name!='sqlite_sequence';"
}, },
bulkInsertQuery: function(tableName, attrValueHashes, options) { bulkInsertQuery: function(tableName, attrValueHashes, options) {
......
...@@ -55,21 +55,34 @@ module.exports = (function() { ...@@ -55,21 +55,34 @@ module.exports = (function() {
} }
} }
if ((getDatabaseMethod.call(self) === 'all') && /select\s.*?\sfrom\s+([^ ;]+)/i.test(self.sql)) { if ((getDatabaseMethod.call(self) === 'all')) {
var tableName = RegExp.$1; var tableNames = []
if (self.options && self.options.tableNames) {
if (tableName !== 'sqlite_master') { tableNames = self.options.tableNames
// get the column types } else if (/FROM `(.*?)`/i.exec(self.sql)) {
self.database.all("PRAGMA table_info(" + tableName + ")", function(err, results) { tableNames.push(/FROM `(.*?)`/i.exec(self.sql)[1])
if (!err) { }
for (var i=0, l=results.length; i<l; i++) {
columnTypes[results[i].name] = results[i].type if (!tableNames.length) {
}
}
executeSql()
});
} else {
executeSql() executeSql()
} else {
var execute = Utils._.after(tableNames.length, executeSql)
tableNames.forEach(function (tableName) {
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[tableName + '.' + results[i].name] = columnTypes[results[i].name] = results[i].type
}
}
execute()
});
} else {
execute()
}
})
} }
} else { } else {
executeSql() executeSql()
......
...@@ -5,12 +5,7 @@ var chai = require('chai') ...@@ -5,12 +5,7 @@ var chai = require('chai')
, expect = chai.expect , expect = chai.expect
, Support = require(__dirname + '/support') , Support = require(__dirname + '/support')
, DataTypes = require(__dirname + "/../lib/data-types") , DataTypes = require(__dirname + "/../lib/data-types")
, dialect = Support.getTestDialect()
, config = require(__dirname + "/config/config")
, sinon = require('sinon')
, datetime = require('chai-datetime') , datetime = require('chai-datetime')
, _ = require('lodash')
, moment = require('moment')
, async = require('async') , async = require('async')
chai.use(datetime) chai.use(datetime)
...@@ -548,6 +543,38 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -548,6 +543,38 @@ describe(Support.getTestDialectTeaser("Include"), function () {
}) })
}) })
}) })
it('should support including date fields, with the correct timeszone', function (done) {
var User = this.sequelize.define('user', {
dateField: Sequelize.DATE
}, {timestamps: false})
, Group = this.sequelize.define('group', {
dateField: Sequelize.DATE
}, {timestamps: false})
User.hasMany(Group)
Group.hasMany(User)
this.sequelize.sync().success(function () {
User.create({ dateField: Date.UTC(2014, 1, 20) }).success(function (user) {
Group.create({ dateField: Date.UTC(2014, 1, 20) }).success(function (group) {
user.addGroup(group).success(function () {
User.find({
where: {
id: user.id
},
include: [Group]
}).success(function (user) {
expect(user.dateField.getTime()).to.equal(Date.UTC(2014, 1, 20))
expect(user.groups[0].dateField.getTime()).to.equal(Date.UTC(2014, 1, 20))
done()
})
})
})
})
})
})
}) })
describe('where', function () { describe('where', function () {
......
...@@ -1211,5 +1211,37 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -1211,5 +1211,37 @@ describe(Support.getTestDialectTeaser("Include"), function () {
}) })
}) })
}) })
it('should support including date fields, with the correct timeszone', function (done) {
var User = this.sequelize.define('user', {
dateField: Sequelize.DATE
}, {timestamps: false})
, Group = this.sequelize.define('group', {
dateField: Sequelize.DATE
}, {timestamps: false})
User.hasMany(Group)
Group.hasMany(User)
this.sequelize.sync().success(function () {
User.create({ dateField: Date.UTC(2014, 1, 20) }).success(function (user) {
Group.create({ dateField: Date.UTC(2014, 1, 20) }).success(function (group) {
user.addGroup(group).success(function () {
User.findAll({
where: {
id: user.id
},
include: [Group]
}).success(function (users) {
expect(users[0].dateField.getTime()).to.equal(Date.UTC(2014, 1, 20))
expect(users[0].groups[0].dateField.getTime()).to.equal(Date.UTC(2014, 1, 20))
done()
})
})
})
})
})
})
}) })
}) })
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!