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

You need to sign in or sign up before continuing.
Commit 64c9c793 by sdepold

extracted simplifyAttributes to be dialect specific

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