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

Commit 485cf460 by Sascha Depold

allow charset definition + test on travis

1 parent b35685ce
...@@ -72,7 +72,7 @@ ModelDefinition.prototype.sync = function(options) { ...@@ -72,7 +72,7 @@ ModelDefinition.prototype.sync = function(options) {
var self = this var self = this
var doQuery = function() { var doQuery = function() {
self.query(QueryGenerator.createTableQuery(self.tableName, self.attributes)) self.query(QueryGenerator.createTableQuery(self.tableName, self.attributes, options))
.on('success', function() { self.emit('success', self) }) .on('success', function() { self.emit('success', self) })
.on('failure', function(err) { self.emit('failure', err) }) .on('failure', function(err) { self.emit('failure', err) })
} }
...@@ -109,7 +109,7 @@ ModelDefinition.prototype.count = function(options) { ...@@ -109,7 +109,7 @@ ModelDefinition.prototype.count = function(options) {
ModelDefinition.prototype.max = function(field, options) { ModelDefinition.prototype.max = function(field, options) {
var self = this var self = this
var emitter = new Utils.CustomEventEmitter(function() { var emitter = new Utils.CustomEventEmitter(function() {
self.query(QueryGenerator.maxQuery(self.tableName, field,options), self, {plain: true}).on('success', function(obj) { self.query(QueryGenerator.maxQuery(self.tableName, field,options), self, {plain: true}).on('success', function(obj) {
emitter.emit('success', obj['max']) emitter.emit('success', obj['max'])
...@@ -119,7 +119,7 @@ ModelDefinition.prototype.max = function(field, options) { ...@@ -119,7 +119,7 @@ ModelDefinition.prototype.max = function(field, options) {
} }
ModelDefinition.prototype.min = function(field, options) { ModelDefinition.prototype.min = function(field, options) {
var self = this var self = this
var emitter = new Utils.CustomEventEmitter(function() { var emitter = new Utils.CustomEventEmitter(function() {
self.query(QueryGenerator.minQuery(self.tableName, field,options), self, {plain: true}).on('success', function(obj) { self.query(QueryGenerator.minQuery(self.tableName, field,options), self, {plain: true}).on('success', function(obj) {
emitter.emit('success', obj['min']) emitter.emit('success', obj['min'])
......
...@@ -7,8 +7,8 @@ var QueryGenerator = module.exports = { ...@@ -7,8 +7,8 @@ var QueryGenerator = module.exports = {
*/ */
createTableQuery: function(tableName, attributes, options) { createTableQuery: function(tableName, attributes, options) {
options = options || {} options = options || {}
var query = "CREATE TABLE IF NOT EXISTS <%= table %> (<%= attributes%>);" var query = "CREATE TABLE IF NOT EXISTS <%= table %> (<%= attributes%>)"
, primaryKeys = [] , primaryKeys = []
, attrStr = Utils._.map(attributes, function(dataType, attr) { , attrStr = Utils._.map(attributes, function(dataType, attr) {
var dt = dataType var dt = dataType
...@@ -21,25 +21,28 @@ var QueryGenerator = module.exports = { ...@@ -21,25 +21,28 @@ var QueryGenerator = module.exports = {
}).join(", ") }).join(", ")
, values = {table: Utils.addTicks(tableName), attributes: attrStr} , values = {table: Utils.addTicks(tableName), attributes: attrStr}
, pkString = primaryKeys.map(function(pk) {return Utils.addTicks(pk)}).join(", ") , pkString = primaryKeys.map(function(pk) {return Utils.addTicks(pk)}).join(", ")
if (pkString.length > 0) values.attributes += ", PRIMARY KEY (" + pkString + ")" if (pkString.length > 0) values.attributes += ", PRIMARY KEY (" + pkString + ")"
if(options.charset) query += " DEFAULT CHARSET=" + options.charset
query += ";"
return Utils._.template(query)(values) return Utils._.template(query)(values)
}, },
dropTableQuery: function(tableName, options) { dropTableQuery: function(tableName, options) {
options = options || {} options = options || {}
var query = "DROP TABLE IF EXISTS <%= table %>;" var query = "DROP TABLE IF EXISTS <%= table %>;"
return Utils._.template(query)({table: Utils.addTicks(tableName)}) return Utils._.template(query)({table: Utils.addTicks(tableName)})
}, },
/* /*
Returns a query for selecting elements in the database <tableName>. Returns a query for selecting elements in the database <tableName>.
Options: Options:
- attributes -> An array of attributes (e.g. ['name', 'birthday']). Default: * - attributes -> An array of attributes (e.g. ['name', 'birthday']). Default: *
- where -> A hash with conditions (e.g. {name: 'foo'}) - where -> A hash with conditions (e.g. {name: 'foo'})
OR an ID as integer OR an ID as integer
OR a string with conditions (e.g. 'name="foo"'). OR a string with conditions (e.g. 'name="foo"').
If you use a string, you have to escape it on your own. If you use a string, you have to escape it on your own.
...@@ -53,9 +56,9 @@ var QueryGenerator = module.exports = { ...@@ -53,9 +56,9 @@ var QueryGenerator = module.exports = {
options.table = Utils.addTicks(tableName) options.table = Utils.addTicks(tableName)
options.attributes = options.attributes && options.attributes.map(function(attr){return Utils.addTicks(attr)}).join(", ") options.attributes = options.attributes && options.attributes.map(function(attr){return Utils.addTicks(attr)}).join(", ")
options.attributes = options.attributes || '*' options.attributes = options.attributes || '*'
var query = "SELECT <%= attributes %> FROM <%= table %>" var query = "SELECT <%= attributes %> FROM <%= table %>"
if(options.where) { if(options.where) {
options.where = QueryGenerator.getWhereConditions(options.where) options.where = QueryGenerator.getWhereConditions(options.where)
query += " WHERE <%= where %>" query += " WHERE <%= where %>"
...@@ -69,12 +72,12 @@ var QueryGenerator = module.exports = { ...@@ -69,12 +72,12 @@ var QueryGenerator = module.exports = {
if(options.offset) query += " LIMIT <%= offset %>, <%= limit %>" if(options.offset) query += " LIMIT <%= offset %>, <%= limit %>"
else query += " LIMIT <%= limit %>" else query += " LIMIT <%= limit %>"
} }
query += ";" query += ";"
return Utils._.template(query)(options) return Utils._.template(query)(options)
}, },
countQuery: function(tableName, options) { countQuery: function(tableName, options) {
return QueryGenerator.selectQuery(tableName, options).replace("*", "count(*)") return QueryGenerator.selectQuery(tableName, options).replace("*", "count(*)")
}, },
...@@ -89,7 +92,7 @@ var QueryGenerator = module.exports = { ...@@ -89,7 +92,7 @@ var QueryGenerator = module.exports = {
*/ */
insertQuery: function(tableName, attrValueHash) { insertQuery: function(tableName, attrValueHash) {
var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>);" var query = "INSERT INTO <%= table %> (<%= attributes %>) VALUES (<%= values %>);"
var replacements = { var replacements = {
table: Utils.addTicks(tableName), table: Utils.addTicks(tableName),
attributes: Utils._.keys(attrValueHash).map(function(attr){return Utils.addTicks(attr)}).join(","), attributes: Utils._.keys(attrValueHash).map(function(attr){return Utils.addTicks(attr)}).join(","),
...@@ -97,7 +100,7 @@ var QueryGenerator = module.exports = { ...@@ -97,7 +100,7 @@ var QueryGenerator = module.exports = {
return Utils.escape((value instanceof Date) ? Utils.toSqlDate(value) : value) return Utils.escape((value instanceof Date) ? Utils.toSqlDate(value) : value)
}).join(",") }).join(",")
} }
return Utils._.template(query)(replacements) return Utils._.template(query)(replacements)
}, },
...@@ -106,7 +109,7 @@ var QueryGenerator = module.exports = { ...@@ -106,7 +109,7 @@ var QueryGenerator = module.exports = {
Parameters: Parameters:
- tableName -> Name of the table - tableName -> Name of the table
- values -> A hash with attribute-value-pairs - values -> A hash with attribute-value-pairs
- where -> A hash with conditions (e.g. {name: 'foo'}) - where -> A hash with conditions (e.g. {name: 'foo'})
OR an ID as integer OR an ID as integer
OR a string with conditions (e.g. 'name="foo"'). OR a string with conditions (e.g. 'name="foo"').
If you use a string, you have to escape it on your own. If you use a string, you have to escape it on your own.
...@@ -120,15 +123,15 @@ var QueryGenerator = module.exports = { ...@@ -120,15 +123,15 @@ var QueryGenerator = module.exports = {
}).join(","), }).join(","),
where: QueryGenerator.getWhereConditions(where) where: QueryGenerator.getWhereConditions(where)
} }
return Utils._.template(query)(replacements) return Utils._.template(query)(replacements)
}, },
/* /*
Returns a deletion query. Returns a deletion query.
Parameters: Parameters:
- tableName -> Name of the table - tableName -> Name of the table
- where -> A hash with conditions (e.g. {name: 'foo'}) - where -> A hash with conditions (e.g. {name: 'foo'})
OR an ID as integer OR an ID as integer
OR a string with conditions (e.g. 'name="foo"'). OR a string with conditions (e.g. 'name="foo"').
If you use a string, you have to escape it on your own. If you use a string, you have to escape it on your own.
...@@ -138,23 +141,23 @@ var QueryGenerator = module.exports = { ...@@ -138,23 +141,23 @@ var QueryGenerator = module.exports = {
deleteQuery: function(tableName, where, options) { deleteQuery: function(tableName, where, options) {
options = options || {} options = options || {}
options.limit = options.limit || 1 options.limit = options.limit || 1
var query = "DELETE FROM <%= table %> WHERE <%= where %> LIMIT <%= limit %>" var query = "DELETE FROM <%= table %> WHERE <%= where %> LIMIT <%= limit %>"
var replacements = { var replacements = {
table: Utils.addTicks(tableName), table: Utils.addTicks(tableName),
where: QueryGenerator.getWhereConditions(where), where: QueryGenerator.getWhereConditions(where),
limit: Utils.escape(options.limit) limit: Utils.escape(options.limit)
} }
return Utils._.template(query)(replacements) return Utils._.template(query)(replacements)
}, },
/* /*
Takes something and transforms it into values of a where condition. Takes something and transforms it into values of a where condition.
*/ */
getWhereConditions: function(smth) { getWhereConditions: function(smth) {
var result = null var result = null
if(Utils.isHash(smth)) if(Utils.isHash(smth))
result = QueryGenerator.hashToWhereConditions(smth) result = QueryGenerator.hashToWhereConditions(smth)
else if(typeof smth == 'number') else if(typeof smth == 'number')
...@@ -166,7 +169,7 @@ var QueryGenerator = module.exports = { ...@@ -166,7 +169,7 @@ var QueryGenerator = module.exports = {
return result return result
}, },
/* /*
Takes a hash and transforms it into a mysql where condition: {key: value, key2: value2} ==> key=value AND key2=value2 Takes a hash and transforms it into a mysql where condition: {key: value, key2: value2} ==> key=value AND key2=value2
The values are transformed by the relevant datatype. The values are transformed by the relevant datatype.
...@@ -179,4 +182,4 @@ var QueryGenerator = module.exports = { ...@@ -179,4 +182,4 @@ var QueryGenerator = module.exports = {
return (_value == 'NULL') ? _key + " IS NULL" : [_key, _value].join("=") return (_value == 'NULL') ? _key + " IS NULL" : [_key, _value].join("=")
}).join(" AND ") }).join(" AND ")
} }
} }
\ No newline at end of file
...@@ -4,6 +4,6 @@ module.exports = { ...@@ -4,6 +4,6 @@ module.exports = {
database: 'sequelize_test', database: 'sequelize_test',
host: '127.0.0.1', host: '127.0.0.1',
rand: function() { rand: function() {
return parseInt(Math.random() * 99999) return parseInt(Math.random() * 999)
} }
} }
...@@ -21,7 +21,7 @@ module.exports = { ...@@ -21,7 +21,7 @@ module.exports = {
name: Sequelize.STRING, bio: Sequelize.TEXT name: Sequelize.STRING, bio: Sequelize.TEXT
}) })
User.sync({force: true}).on('success', function() { User.sync({force: true, charset: 'latin1'}).on('success', function() {
User.create({foo: '1', bar: '2', name: 'hallo', bio: 'welt'}).on('success', function(u) { User.create({foo: '1', bar: '2', name: 'hallo', bio: 'welt'}).on('success', function(u) {
assert.eql(u.equals(u), true) assert.eql(u.equals(u), true)
exit(function(){}) exit(function(){})
......
...@@ -4,6 +4,6 @@ module.exports = { ...@@ -4,6 +4,6 @@ module.exports = {
database: 'sequelize_test', database: 'sequelize_test',
host: '127.0.0.1', host: '127.0.0.1',
rand: function() { rand: function() {
return parseInt(Math.random() * 9999999) return parseInt(Math.random() * 999)
} }
} }
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!