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

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) { isManyToManyAssociation: function(association) {
var sys = require("sys") return (['hasMany', 'hasAndBelongsToMany'].indexOf(association.type) > -1)
sys.log(sys.inspect(obj))
}, },
evaluateTemplate: function(template, replacements) { manyToManyTableName: function(name1, name2) {
var result = template var _name1 = name1[0].toUpperCase() + name1.replace(/^./, "")
Helper.Hash.keys(replacements).forEach(function(key) { var _name2 = name2[0].toUpperCase() + name2.replace(/^./, "")
result = result.replace("%{" + key + "}", replacements[key])
return [_name1, _name2].sort().join("")
},
asTableIdentifier: function(name) {
var _name = name[0].toLowerCase() + name.replace(/^./, "")
return instance.Inflection.singularize(_name) + "Id"
},
addPrefix: function(prefix, string, singularize) {
var _string = singularize ? instance.Inflection.singularize(string) : instance.Inflection.pluralize(string)
return prefix + _string[0].toUpperCase() + _string.replace(/^./, "")
},
asTableName: function(name) {
return instance.Inflection.pluralize(name)
},
asSqlDate: function(date) {
return [
[
date.getFullYear(),
((date.getMonth() < 9 ? '0' : '') + (date.getMonth()+1)),
((date.getDate() < 10 ? '0' : '') + date.getDate())
].join("-"),
date.toLocaleTimeString()
].join(" ")
},
valuesForInsertQuery: function(object) {
var actualValues = object.values,
result = []
instance.Hash.forEach(actualValues, function(value, key) {
var dataType = object.table.attributes[key]
result.push(instance.SQL.transformValueByDataType(value, dataType))
}) })
return result return result
}, },
SQL: { valuesForUpdate: function(object, options) {
isManyToManyAssociation: function(association) { var actualValues = object.values,
return (['hasMany', 'hasAndBelongsToMany'].indexOf(association.type) > -1) result = [],
}, self = instance
manyToManyTableName: function(name1, name2) {
var _name1 = name1[0].toUpperCase() + name1.replace(/^./, "")
var _name2 = name2[0].toUpperCase() + name2.replace(/^./, "")
return [_name1, _name2].sort().join("")
},
asTableIdentifier: function(name) {
var _name = name[0].toLowerCase() + name.replace(/^./, "")
return Helper.Inflection.singularize(_name) + "Id"
},
addPrefix: function(prefix, string, singularize) {
var _string = singularize ? Helper.Inflection.singularize(string) : Helper.Inflection.pluralize(string)
return prefix + _string[0].toUpperCase() + _string.replace(/^./, "")
},
asTableName: function(name) {
return Helper.Inflection.pluralize(name)
},
asSqlDate: function(date) {
return [
[
date.getFullYear(),
((date.getMonth() < 9 ? '0' : '') + (date.getMonth()+1)),
((date.getDate() < 10 ? '0' : '') + date.getDate())
].join("-"),
date.toLocaleTimeString()
].join(" ")
},
valuesForInsertQuery: function(object) {
var actualValues = object.values,
result = []
Helper.Hash.forEach(actualValues, function(value, key) {
var dataType = object.table.attributes[key]
result.push(Helper.SQL.transformValueByDataType(value, dataType))
})
return result options = options || {}
},
valuesForUpdate: function(object, options) { instance.Hash.forEach(actualValues, function(value, key) {
var actualValues = object.values, var dataType = object.table.attributes[key]
result = [] result.push([key, self.SQL.transformValueByDataType(value, dataType)].join(" = "))
})
options = options || {}
Helper.Hash.forEach(actualValues, function(value, key) { return result.join(options.seperator || ", ")
var dataType = object.table.attributes[key] },
result.push([key, Helper.SQL.transformValueByDataType(value, dataType)].join(" = "))
})
return result.join(options.seperator || ", ") fieldsForInsertQuery: function(object) {
}, return instance.Hash.keys(object.values).join(", ")
fieldsForInsertQuery: function(object) {
return Helper.Hash.keys(object.values).join(", ")
},
transformValueByDataType: function(value, attributeOptions) {
var dataType = attributeOptions.type
if((value == null)||(typeof value == 'undefined')||((dataType.indexOf(Sequelize.INTEGER) > -1) && isNaN(value)))
return "NULL"
if(dataType.indexOf(Sequelize.FLOAT) > -1)
return (typeof value == 'number') ? value : parseFloat(value.replace(",", "."))
if(dataType.indexOf(Sequelize.BOOLEAN) > -1)
return (value === true ? 1 : 0)
if(dataType.indexOf(Sequelize.INTEGER) > -1)
return value
if(dataType.indexOf(Sequelize.DATE) > -1)
return ("'" + Helper.SQL.asSqlDate(value) + "'")
return ("'" + value + "'")
},
hashToWhereConditions: function(conditions, attributes) {
if(typeof conditions == 'number')
return ('id = ' + conditions)
else {
var result = []
Helper.Hash.forEach(conditions, function(value, key) {
var _value = Helper.SQL.transformValueByDataType(value, attributes[key])
if(_value == 'NULL') result.push(key + " IS NULL")
else result.push(key + "=" + _value)
})
return result.join(" AND ")
}
}
}, },
Hash: { transformValueByDataType: function(value, attributeOptions) {
forEach: function(object, func) { var dataType = attributeOptions.type
Helper.Hash.keys(object).forEach(function(key) {
func(object[key], key, object)
})
},
map: function(object, func) { if((value == null)||(typeof value == 'undefined')||((dataType.indexOf(instance.Sequelize.INTEGER) > -1) && isNaN(value)))
var result = [] return "NULL"
Helper.Hash.forEach(object, function(value, key, object) {
result.push(func(value, key, object))
})
return result
},
keys: function(object) { if(dataType.indexOf(instance.Sequelize.FLOAT) > -1)
var results = [] return (typeof value == 'number') ? value : parseFloat(value.replace(",", "."))
for (var property in object)
results.push(property)
return results
},
values: function(object) { if(dataType.indexOf(instance.Sequelize.BOOLEAN) > -1)
var result = [] return (value === true ? 1 : 0)
Helper.Hash.keys(object).forEach(function(key) {
result.push(object[key])
})
return result
},
merge: function(source, target, force) { if(dataType.indexOf(instance.Sequelize.INTEGER) > -1)
Helper.Hash.forEach(source, function(value, key) { return value
if(!target[key] || force)
target[key] = value if(dataType.indexOf(instance.Sequelize.DATE) > -1)
}) return ("'" + instance.SQL.asSqlDate(value) + "'")
return target
}, return ("'" + value + "'")
without: function(object, withoutKeys) {
var result = {}
Helper.Hash.forEach(object, function(value, key) {
if(withoutKeys.indexOf(key) == -1)
result[key] = value
})
return result
}
}, },
Array: {
map: function(array, func) { hashToWhereConditions: function(conditions, attributes) {
var result = [] if(typeof conditions == 'number')
array.forEach(function(element) { return ('id = ' + conditions)
result.push(func(element)) else {
})
return result
},
reject: function(array, func) {
var result = []
array.forEach(function(element) {
if(!func(element)) result.push(element)
})
return result
},
select: function(array, func) {
var result = []
array.forEach(function(element) {
if(func(element)) result.push(element)
})
return result
},
without: function(array, withouts) {
var result = [] var result = []
array.forEach(function(e) { instance.Hash.forEach(conditions, function(value, key) {
if(withouts.indexOf(e) == -1) result.push(e) var _value = instance.SQL.transformValueByDataType(value, attributes[key])
if(_value == 'NULL') result.push(key + " IS NULL")
else result.push(key + "=" + _value)
}) })
return result return result.join(" AND ")
},
join: function(arr1, arr2) {
var result = []
arr1.forEach(function(e) { result.push(e) })
arr2.forEach(function(e) { result.push(e) })
return result
} }
}
}
}
var injectHash = function(instance) {
instance.Hash = {
forEach: function(object, func) {
instance.Hash.keys(object).forEach(function(key) {
func(object[key], key, object)
})
},
map: function(object, func) {
var result = []
instance.Hash.forEach(object, function(value, key, object) {
result.push(func(value, key, object))
})
return result
},
keys: function(object) {
var results = []
for (var property in object)
results.push(property)
return results
},
values: function(object) {
var result = []
instance.Hash.keys(object).forEach(function(key) {
result.push(object[key])
})
return result
},
merge: function(source, target, force) {
instance.Hash.forEach(source, function(value, key) {
if(!target[key] || force)
target[key] = value
})
return target
},
without: function(object, withoutKeys) {
var result = {}
instance.Hash.forEach(object, function(value, key) {
if(withoutKeys.indexOf(key) == -1)
result[key] = value
})
return result
}
}
}
var injectArray = function(instance) {
instance.Array = {
map: function(array, func) {
var result = []
array.forEach(function(element) {
result.push(func(element))
})
return result
},
reject: function(array, func) {
var result = []
array.forEach(function(element) {
if(!func(element)) result.push(element)
})
return result
},
select: function(array, func) {
var result = []
array.forEach(function(element) {
if(func(element)) result.push(element)
})
return result
}, },
without: function(array, withouts) {
var result = []
array.forEach(function(e) {
if(withouts.indexOf(e) == -1) result.push(e)
})
return result
},
join: function(arr1, arr2) {
var result = []
arr1.forEach(function(e) { result.push(e) })
arr2.forEach(function(e) { result.push(e) })
return result
}
}
}
var injectBasics = function(instance) {
instance.configure = function(options) {
this.options = options
}
instance.log = function(obj) {
var sys = require("sys")
sys.log(sys.inspect(obj))
}
Inflection: require(__dirname + "/../inflection/inflection") instance.evaluateTemplate = function(template, replacements) {
var result = template
this.Hash.keys(replacements).forEach(function(key) {
result = result.replace("%{" + key + "}", replacements[key])
})
return result
} }
}
return Helper var Helper = function(Sequelize) {
} this.Sequelize = Sequelize
\ No newline at end of file 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!