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

Commit 64c9c793 by sdepold

extracted simplifyAttributes to be dialect specific

1 parent 9e97c40d
...@@ -22,8 +22,7 @@ module.exports = (function() { ...@@ -22,8 +22,7 @@ module.exports = (function() {
this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.target.tableName) + "Id", this.source.options.underscored) this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.target.tableName) + "Id", this.source.options.underscored)
newAttributes[this.identifier] = { type: DataTypes.INTEGER } newAttributes[this.identifier] = { type: DataTypes.INTEGER }
Utils._.extend(this.source.attributes, this.source.QueryGenerator.attributesToSQL(newAttributes))
Utils._.extend(this.source.attributes, Utils.simplifyAttributes(newAttributes))
return this return this
} }
......
...@@ -55,7 +55,7 @@ module.exports = (function() { ...@@ -55,7 +55,7 @@ module.exports = (function() {
} else { } else {
var newAttributes = {} var newAttributes = {}
newAttributes[this.identifier] = { type: DataTypes.INTEGER } newAttributes[this.identifier] = { type: DataTypes.INTEGER }
Utils._.extend(this.target.attributes, Utils.simplifyAttributes(newAttributes)) Utils._.extend(this.target.attributes, this.source.QueryGenerator.attributesToSQL(newAttributes))
} }
return this return this
......
...@@ -27,7 +27,7 @@ module.exports = (function() { ...@@ -27,7 +27,7 @@ module.exports = (function() {
this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.source.tableName) + "Id", this.options.underscored) this.identifier = this.options.foreignKey || Utils._.underscoredIf(Utils.singularize(this.source.tableName) + "Id", this.options.underscored)
newAttributes[this.identifier] = { type: DataTypes.INTEGER } newAttributes[this.identifier] = { type: DataTypes.INTEGER }
Utils._.extend(this.target.attributes, Utils.simplifyAttributes(newAttributes)) Utils._.extend(this.target.attributes, this.source.QueryGenerator.attributesToSQL(newAttributes))
return this return this
} }
......
...@@ -247,6 +247,32 @@ module.exports = (function() { ...@@ -247,6 +247,32 @@ module.exports = (function() {
return (_value == 'NULL') ? _key + " IS NULL" : [_key, _value].join("=") return (_value == 'NULL') ? _key + " IS NULL" : [_key, _value].join("=")
} }
}).join(" AND ") }).join(" AND ")
},
attributesToSQL: function(attributes) {
var result = {}
Utils._.map(attributes, function(dataType, name) {
if(Utils.isHash(dataType)) {
var template = "<%= type %>"
, replacements = { type: dataType.type }
if(dataType.hasOwnProperty('allowNull') && (!dataType.allowNull)) template += " NOT NULL"
if(dataType.autoIncrement) template +=" auto_increment"
if(dataType.defaultValue != undefined) {
template += " DEFAULT <%= defaultValue %>"
replacements.defaultValue = Utils.escape(dataType.defaultValue)
}
if(dataType.unique) template += " UNIQUE"
if(dataType.primaryKey) template += " PRIMARY KEY"
result[name] = Utils._.template(template)(replacements)
} else {
result[name] = dataType
}
})
return result
} }
} }
......
...@@ -18,21 +18,36 @@ module.exports = (function() { ...@@ -18,21 +18,36 @@ module.exports = (function() {
this.name = name this.name = name
this.tableName = this.options.freezeTableName ? name : Utils.pluralize(name) this.tableName = this.options.freezeTableName ? name : Utils.pluralize(name)
this.attributes = Utils.simplifyAttributes(attributes)
this.rawAttributes = attributes this.rawAttributes = attributes
this.modelManager = null // defined by model-manager during addModel this.modelManager = null // defined in init function
this.associations = {} this.associations = {}
// extract validation // extract validation
this.validate = this.options.validate || {} this.validate = this.options.validate || {}
}
ModelFactory.prototype.init = function(modelManager) {
this.modelManager = modelManager
addDefaultAttributes.call(this) addDefaultAttributes.call(this)
addOptionalClassMethods.call(this) addOptionalClassMethods.call(this)
findAutoIncrementField.call(this) findAutoIncrementField.call(this)
return this
} }
ModelFactory.prototype.__defineGetter__('QueryInterface', function() { Object.defineProperty(ModelFactory.prototype, 'attributes', {
return this.modelManager.sequelize.getQueryInterface() get: function() {
return this.QueryGenerator.attributesToSQL(this.rawAttributes)
}
})
Object.defineProperty(ModelFactory.prototype, 'QueryInterface', {
get: function() { return this.modelManager.sequelize.getQueryInterface() }
})
Object.defineProperty(ModelFactory.prototype, 'QueryGenerator', {
get: function() { return this.QueryInterface.QueryGenerator }
}) })
ModelFactory.prototype.sync = function(options) { ModelFactory.prototype.sync = function(options) {
...@@ -154,7 +169,6 @@ module.exports = (function() { ...@@ -154,7 +169,6 @@ module.exports = (function() {
ModelFactory.prototype.__defineGetter__('primaryKeys', function() { ModelFactory.prototype.__defineGetter__('primaryKeys', function() {
var result = {} var result = {}
Utils._.each(this.attributes, function(dataTypeString, attributeName) { Utils._.each(this.attributes, function(dataTypeString, attributeName) {
if((attributeName != 'id') && (dataTypeString.indexOf('PRIMARY KEY') > -1)) if((attributeName != 'id') && (dataTypeString.indexOf('PRIMARY KEY') > -1))
result[attributeName] = dataTypeString result[attributeName] = dataTypeString
...@@ -188,8 +202,15 @@ module.exports = (function() { ...@@ -188,8 +202,15 @@ module.exports = (function() {
} }
var addDefaultAttributes = function() { var addDefaultAttributes = function() {
var defaultAttributes = {id: {type: DataTypes.INTEGER, allowNull: false, primaryKey: true, autoIncrement: true}} var self = this
, self = this , defaultAttributes = {
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
}
}
if(this.hasPrimaryKeys) defaultAttributes = {} if(this.hasPrimaryKeys) defaultAttributes = {}
...@@ -201,8 +222,9 @@ module.exports = (function() { ...@@ -201,8 +222,9 @@ module.exports = (function() {
defaultAttributes[Utils._.underscoredIf('deletedAt', this.options.underscored)] = {type: DataTypes.DATE} defaultAttributes[Utils._.underscoredIf('deletedAt', this.options.underscored)] = {type: DataTypes.DATE}
} }
defaultAttributes = Utils.simplifyAttributes(defaultAttributes) Utils._.each(defaultAttributes, function(value, attr) {
Utils._.map(defaultAttributes, function(value, attr) { self.attributes[attr] = value }) self.rawAttributes[attr] = value
})
} }
var findAutoIncrementField = function() { var findAutoIncrementField = function() {
......
...@@ -5,7 +5,6 @@ module.exports = (function() { ...@@ -5,7 +5,6 @@ module.exports = (function() {
} }
ModelManager.prototype.addModel = function(model) { ModelManager.prototype.addModel = function(model) {
model.modelManager = this
this.models.push(model) this.models.push(model)
return model return model
......
...@@ -27,8 +27,16 @@ module.exports = (function() { ...@@ -27,8 +27,16 @@ module.exports = (function() {
destroy: 'DestroyQuery' destroy: 'DestroyQuery'
} }
Model.prototype.__defineGetter__('QueryInterface', function() { Object.defineProperty(Model.prototype, 'sequelize', {
return this.__definition.modelManager.sequelize.getQueryInterface() get: function(){ return this.__definition.modelManager.sequelize }
})
Object.defineProperty(Model.prototype, 'QueryInterface', {
get: function(){ return this.sequelize.getQueryInterface() }
})
Object.defineProperty(Model.prototype, 'QueryGenerator', {
get: function(){ return this.QueryInterface.QueryGenerator }
}) })
Model.prototype.save = function() { Model.prototype.save = function() {
......
...@@ -88,7 +88,7 @@ module.exports = (function() { ...@@ -88,7 +88,7 @@ module.exports = (function() {
else else
attributes[attributeName] = dataTypeOrOptions attributes[attributeName] = dataTypeOrOptions
var options = Utils.simplifyAttributes(attributes) var options = this.QueryGenerator.attributesToSQL(attributes)
, sql = this.QueryGenerator.addColumnQuery(tableName, options) , sql = this.QueryGenerator.addColumnQuery(tableName, options)
return queryAndEmit.call(this, sql, 'addColumn') return queryAndEmit.call(this, sql, 'addColumn')
...@@ -107,7 +107,7 @@ module.exports = (function() { ...@@ -107,7 +107,7 @@ module.exports = (function() {
else else
attributes[attributeName] = dataTypeOrOptions attributes[attributeName] = dataTypeOrOptions
var options = Utils.simplifyAttributes(attributes) var options = this.QueryGenerator.attributesToSQL(attributes)
, sql = this.QueryGenerator.changeColumnQuery(tableName, options) , sql = this.QueryGenerator.changeColumnQuery(tableName, options)
return queryAndEmit.call(this, sql, 'changeColumn') return queryAndEmit.call(this, sql, 'changeColumn')
...@@ -130,7 +130,7 @@ module.exports = (function() { ...@@ -130,7 +130,7 @@ module.exports = (function() {
var sql = self.QueryGenerator.renameColumnQuery(tableName, var sql = self.QueryGenerator.renameColumnQuery(tableName,
attrNameBefore, attrNameBefore,
Utils.simplifyAttributes(options) self.QueryGenerator.attributesToSQL(options)
) )
self.sequelize.query(sql).success(function() { self.sequelize.query(sql).success(function() {
......
...@@ -44,9 +44,11 @@ module.exports = (function() { ...@@ -44,9 +44,11 @@ module.exports = (function() {
if(this.options.define) if(this.options.define)
options = Sequelize.Utils.merge(options, this.options.define) options = Sequelize.Utils.merge(options, this.options.define)
var model = this.modelManager.addModel(new ModelFactory(modelName, attributes, options)) var factory = new ModelFactory(modelName, attributes, options)
return model this.modelManager.addModel(factory.init(this.modelManager))
return factory
} }
Sequelize.prototype.import = function(path) { Sequelize.prototype.import = function(path) {
......
...@@ -87,31 +87,6 @@ var Utils = module.exports = { ...@@ -87,31 +87,6 @@ var Utils = module.exports = {
return ("'" + value + "'") return ("'" + value + "'")
}, },
simplifyAttributes: function(attributes) {
var result = {}
Utils._.map(attributes, function(dataType, name) {
if(Utils.isHash(dataType)) {
var template = "<%= type %>"
, replacements = { type: dataType.type }
if(dataType.hasOwnProperty('allowNull') && (!dataType.allowNull)) template += " NOT NULL"
if(dataType.autoIncrement) template +=" auto_increment"
if(dataType.defaultValue != undefined) {
template += " DEFAULT <%= defaultValue %>"
replacements.defaultValue = Utils.escape(dataType.defaultValue)
}
if(dataType.unique) template += " UNIQUE"
if(dataType.primaryKey) template += " PRIMARY KEY"
result[name] = Utils._.template(template)(replacements)
} else {
result[name] = dataType
}
})
return result
},
toSqlDate: function(date) { toSqlDate: function(date) {
return [ return [
[ [
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!