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

Commit a12b5954 by Mick Hansen

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

2 parents 13b9494b b1f666e9
......@@ -442,13 +442,16 @@ module.exports = (function() {
DAOFactory.prototype.findAll = function(options, queryOptions) {
var hasJoin = false
, tableNames = { }
tableNames[this.tableName] = true
options = optClone(options)
if (typeof options === 'object') {
if (options.hasOwnProperty('include') && options.include) {
hasJoin = true
validateIncludedElements.call(this, options)
validateIncludedElements.call(this, options, tableNames)
}
// whereCollection is used for non-primary key updates
......@@ -459,7 +462,8 @@ module.exports = (function() {
return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({
type: QueryTypes.SELECT,
hasJoin: hasJoin
hasJoin: hasJoin,
tableNames: Object.keys(tableNames)
}, queryOptions, { transaction: (options || {}).transaction }))
}
......@@ -498,6 +502,9 @@ module.exports = (function() {
var primaryKeys = this.primaryKeys
, keys = Object.keys(primaryKeys)
, keysLength = keys.length
, tableNames = { }
tableNames[this.tableName] = true
// options is not a hash but an id
if (typeof options === 'number') {
......@@ -534,7 +541,7 @@ module.exports = (function() {
if (options.hasOwnProperty('include') && options.include) {
hasJoin = true
validateIncludedElements.call(this, options)
validateIncludedElements.call(this, options, tableNames)
}
// whereCollection is used for non-primary key updates
......@@ -559,7 +566,8 @@ module.exports = (function() {
return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({
plain: true,
type: QueryTypes.SELECT,
hasJoin: hasJoin
hasJoin: hasJoin,
tableNames: Object.keys(tableNames)
}, queryOptions, { transaction: (options || {}).transaction }))
}
......@@ -1328,13 +1336,14 @@ module.exports = (function() {
}.bind(this))
}
var validateIncludedElements = function(options) {
var validateIncludedElements = function(options, tableNames) {
tableNames = tableNames || {}
options.includeNames = []
options.includeMap = {}
options.hasSingleAssociation = false
options.hasMultiAssociation = false
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.includeNames.push(include.as)
......@@ -1345,12 +1354,12 @@ module.exports = (function() {
options.hasIncludeWhere = options.hasIncludeWhere || include.hasIncludeWhere || !!include.where
options.hasIncludeRequired = options.hasIncludeRequired || include.hasIncludeRequired || !!include.required
return include
}.bind(this))
};
var validateIncludedElement = function(include, parent) {
var validateIncludedElement = function(include, parent, tableNames) {
if (include instanceof DAOFactory) {
include = { daoFactory: include }
}
......@@ -1370,6 +1379,8 @@ module.exports = (function() {
throw new Error('Include malformed. Expected attributes: daoFactory, as!')
}
tableNames[include.daoFactory.tableName] = true
if (include.hasOwnProperty('attributes')) {
var primaryKeys;
if (include.daoFactory.hasPrimaryKeys) {
......@@ -1389,7 +1400,7 @@ module.exports = (function() {
if (include._pseudo) return 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) {
include.association = association
include.as = association.as
......@@ -1409,6 +1420,7 @@ module.exports = (function() {
}
include.include.push(include.through)
tableNames[through.tableName] = true
}
if (include.required === undefined) {
......@@ -1417,7 +1429,7 @@ module.exports = (function() {
// Validate child includes
if (include.hasOwnProperty('include')) {
validateIncludedElements(include)
validateIncludedElements(include, tableNames)
}
return include
......
......@@ -35,7 +35,7 @@ module.exports = (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)({})
},
......@@ -44,7 +44,7 @@ module.exports = (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) {
......@@ -169,7 +169,7 @@ module.exports = (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) {
......
......@@ -55,21 +55,34 @@ module.exports = (function() {
}
}
if ((getDatabaseMethod.call(self) === '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 {
if ((getDatabaseMethod.call(self) === 'all')) {
var tableNames = []
if (self.options && self.options.tableNames) {
tableNames = self.options.tableNames
} else if (/FROM `(.*?)`/i.exec(self.sql)) {
tableNames.push(/FROM `(.*?)`/i.exec(self.sql)[1])
}
if (!tableNames.length) {
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 {
executeSql()
......
......@@ -5,12 +5,7 @@ var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/support')
, DataTypes = require(__dirname + "/../lib/data-types")
, dialect = Support.getTestDialect()
, config = require(__dirname + "/config/config")
, sinon = require('sinon')
, datetime = require('chai-datetime')
, _ = require('lodash')
, moment = require('moment')
, async = require('async')
chai.use(datetime)
......@@ -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 () {
......
......@@ -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!