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

Commit 20bc8b0b by Sascha Depold

helper is now a class

1 parent dc5e2609
exports.Helper = function(Sequelize) { var injectSQL = function(instance) {
var Helper = { instance.SQL = {
log: function(obj) {
var sys = require("sys")
sys.log(sys.inspect(obj))
},
evaluateTemplate: function(template, replacements) {
var result = template
Helper.Hash.keys(replacements).forEach(function(key) {
result = result.replace("%{" + key + "}", replacements[key])
})
return result
},
SQL: {
isManyToManyAssociation: function(association) { isManyToManyAssociation: function(association) {
return (['hasMany', 'hasAndBelongsToMany'].indexOf(association.type) > -1) return (['hasMany', 'hasAndBelongsToMany'].indexOf(association.type) > -1)
}, },
...@@ -27,16 +13,16 @@ exports.Helper = function(Sequelize) { ...@@ -27,16 +13,16 @@ exports.Helper = function(Sequelize) {
asTableIdentifier: function(name) { asTableIdentifier: function(name) {
var _name = name[0].toLowerCase() + name.replace(/^./, "") var _name = name[0].toLowerCase() + name.replace(/^./, "")
return Helper.Inflection.singularize(_name) + "Id" return instance.Inflection.singularize(_name) + "Id"
}, },
addPrefix: function(prefix, string, singularize) { addPrefix: function(prefix, string, singularize) {
var _string = singularize ? Helper.Inflection.singularize(string) : Helper.Inflection.pluralize(string) var _string = singularize ? instance.Inflection.singularize(string) : instance.Inflection.pluralize(string)
return prefix + _string[0].toUpperCase() + _string.replace(/^./, "") return prefix + _string[0].toUpperCase() + _string.replace(/^./, "")
}, },
asTableName: function(name) { asTableName: function(name) {
return Helper.Inflection.pluralize(name) return instance.Inflection.pluralize(name)
}, },
asSqlDate: function(date) { asSqlDate: function(date) {
...@@ -54,9 +40,9 @@ exports.Helper = function(Sequelize) { ...@@ -54,9 +40,9 @@ exports.Helper = function(Sequelize) {
var actualValues = object.values, var actualValues = object.values,
result = [] result = []
Helper.Hash.forEach(actualValues, function(value, key) { instance.Hash.forEach(actualValues, function(value, key) {
var dataType = object.table.attributes[key] var dataType = object.table.attributes[key]
result.push(Helper.SQL.transformValueByDataType(value, dataType)) result.push(instance.SQL.transformValueByDataType(value, dataType))
}) })
return result return result
...@@ -64,39 +50,40 @@ exports.Helper = function(Sequelize) { ...@@ -64,39 +50,40 @@ exports.Helper = function(Sequelize) {
valuesForUpdate: function(object, options) { valuesForUpdate: function(object, options) {
var actualValues = object.values, var actualValues = object.values,
result = [] result = [],
self = instance
options = options || {} options = options || {}
Helper.Hash.forEach(actualValues, function(value, key) { instance.Hash.forEach(actualValues, function(value, key) {
var dataType = object.table.attributes[key] var dataType = object.table.attributes[key]
result.push([key, Helper.SQL.transformValueByDataType(value, dataType)].join(" = ")) result.push([key, self.SQL.transformValueByDataType(value, dataType)].join(" = "))
}) })
return result.join(options.seperator || ", ") return result.join(options.seperator || ", ")
}, },
fieldsForInsertQuery: function(object) { fieldsForInsertQuery: function(object) {
return Helper.Hash.keys(object.values).join(", ") return instance.Hash.keys(object.values).join(", ")
}, },
transformValueByDataType: function(value, attributeOptions) { transformValueByDataType: function(value, attributeOptions) {
var dataType = attributeOptions.type var dataType = attributeOptions.type
if((value == null)||(typeof value == 'undefined')||((dataType.indexOf(Sequelize.INTEGER) > -1) && isNaN(value))) if((value == null)||(typeof value == 'undefined')||((dataType.indexOf(instance.Sequelize.INTEGER) > -1) && isNaN(value)))
return "NULL" return "NULL"
if(dataType.indexOf(Sequelize.FLOAT) > -1) if(dataType.indexOf(instance.Sequelize.FLOAT) > -1)
return (typeof value == 'number') ? value : parseFloat(value.replace(",", ".")) return (typeof value == 'number') ? value : parseFloat(value.replace(",", "."))
if(dataType.indexOf(Sequelize.BOOLEAN) > -1) if(dataType.indexOf(instance.Sequelize.BOOLEAN) > -1)
return (value === true ? 1 : 0) return (value === true ? 1 : 0)
if(dataType.indexOf(Sequelize.INTEGER) > -1) if(dataType.indexOf(instance.Sequelize.INTEGER) > -1)
return value return value
if(dataType.indexOf(Sequelize.DATE) > -1) if(dataType.indexOf(instance.Sequelize.DATE) > -1)
return ("'" + Helper.SQL.asSqlDate(value) + "'") return ("'" + instance.SQL.asSqlDate(value) + "'")
return ("'" + value + "'") return ("'" + value + "'")
}, },
...@@ -106,26 +93,27 @@ exports.Helper = function(Sequelize) { ...@@ -106,26 +93,27 @@ exports.Helper = function(Sequelize) {
return ('id = ' + conditions) return ('id = ' + conditions)
else { else {
var result = [] var result = []
Helper.Hash.forEach(conditions, function(value, key) { instance.Hash.forEach(conditions, function(value, key) {
var _value = Helper.SQL.transformValueByDataType(value, attributes[key]) var _value = instance.SQL.transformValueByDataType(value, attributes[key])
if(_value == 'NULL') result.push(key + " IS NULL") if(_value == 'NULL') result.push(key + " IS NULL")
else result.push(key + "=" + _value) else result.push(key + "=" + _value)
}) })
return result.join(" AND ") return result.join(" AND ")
} }
} }
}, }
}
Hash: { var injectHash = function(instance) {
instance.Hash = {
forEach: function(object, func) { forEach: function(object, func) {
Helper.Hash.keys(object).forEach(function(key) { instance.Hash.keys(object).forEach(function(key) {
func(object[key], key, object) func(object[key], key, object)
}) })
}, },
map: function(object, func) { map: function(object, func) {
var result = [] var result = []
Helper.Hash.forEach(object, function(value, key, object) { instance.Hash.forEach(object, function(value, key, object) {
result.push(func(value, key, object)) result.push(func(value, key, object))
}) })
return result return result
...@@ -140,14 +128,14 @@ exports.Helper = function(Sequelize) { ...@@ -140,14 +128,14 @@ exports.Helper = function(Sequelize) {
values: function(object) { values: function(object) {
var result = [] var result = []
Helper.Hash.keys(object).forEach(function(key) { instance.Hash.keys(object).forEach(function(key) {
result.push(object[key]) result.push(object[key])
}) })
return result return result
}, },
merge: function(source, target, force) { merge: function(source, target, force) {
Helper.Hash.forEach(source, function(value, key) { instance.Hash.forEach(source, function(value, key) {
if(!target[key] || force) if(!target[key] || force)
target[key] = value target[key] = value
}) })
...@@ -155,14 +143,16 @@ exports.Helper = function(Sequelize) { ...@@ -155,14 +143,16 @@ exports.Helper = function(Sequelize) {
}, },
without: function(object, withoutKeys) { without: function(object, withoutKeys) {
var result = {} var result = {}
Helper.Hash.forEach(object, function(value, key) { instance.Hash.forEach(object, function(value, key) {
if(withoutKeys.indexOf(key) == -1) if(withoutKeys.indexOf(key) == -1)
result[key] = value result[key] = value
}) })
return result return result
} }
}, }
Array: { }
var injectArray = function(instance) {
instance.Array = {
map: function(array, func) { map: function(array, func) {
var result = [] var result = []
array.forEach(function(element) { array.forEach(function(element) {
...@@ -197,10 +187,34 @@ exports.Helper = function(Sequelize) { ...@@ -197,10 +187,34 @@ exports.Helper = function(Sequelize) {
arr2.forEach(function(e) { result.push(e) }) arr2.forEach(function(e) { result.push(e) })
return result return result
} }
}, }
}
var injectBasics = function(instance) {
instance.configure = function(options) {
this.options = options
}
Inflection: require(__dirname + "/../inflection/inflection") instance.log = function(obj) {
var sys = require("sys")
sys.log(sys.inspect(obj))
} }
return Helper instance.evaluateTemplate = function(template, replacements) {
var result = template
this.Hash.keys(replacements).forEach(function(key) {
result = result.replace("%{" + key + "}", replacements[key])
})
return result
}
}
var Helper = function(Sequelize) {
this.Sequelize = Sequelize
this.Inflection = require(__dirname + "/../inflection/inflection")
injectBasics(this)
injectSQL(this)
injectHash(this)
injectArray(this)
} }
exports.Helper = Helper
\ No newline at end of file
...@@ -12,7 +12,7 @@ var Sequelize = function(database, username, password, options) { ...@@ -12,7 +12,7 @@ var Sequelize = function(database, username, password, options) {
} }
var classMethods = { var classMethods = {
Helper: new require(__dirname + "/Helper").Helper(Sequelize), Helper: new (require(__dirname + "/Helper").Helper)(Sequelize),
STRING: 'VARCHAR(255)', STRING: 'VARCHAR(255)',
TEXT: 'TEXT', TEXT: 'TEXT',
......
...@@ -3,7 +3,6 @@ var Sequelize = require(__dirname + "/../../lib/sequelize/Sequelize").Sequelize, ...@@ -3,7 +3,6 @@ var Sequelize = require(__dirname + "/../../lib/sequelize/Sequelize").Sequelize,
Foo = s.define('Foo', { name: Sequelize.TEXT }), Foo = s.define('Foo', { name: Sequelize.TEXT }),
Bar = s.define('Bar', { nr: Sequelize.INTEGER }) Bar = s.define('Bar', { nr: Sequelize.INTEGER })
module.exports = { module.exports = {
'should have no fetchedAssociations first': function(assert, beforeExit) { 'should have no fetchedAssociations first': function(assert, beforeExit) {
var allowExit = false var allowExit = false
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!