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

Commit 5992de73 by Sascha Depold

added test for destruction of keys with dots when doing raw queries

1 parent a377f267
...@@ -216,13 +216,13 @@ ...@@ -216,13 +216,13 @@
}, },
{ {
"file": "lib/dialects/abstract/query.js", "file": "lib/dialects/abstract/query.js",
"line": 333, "line": 345,
"description": "The function takes the result of the query execution and groups\nthe associated data by the callee.\n\nExample:\n groupDataByCalleeFactory([\n {\n callee: { some: 'data', id: 1 },\n association: { foo: 'bar', id: 1 }\n }, {\n callee: { some: 'data', id: 1 },\n association: { foo: 'bar', id: 2 }\n }, {\n callee: { some: 'data', id: 1 },\n association: { foo: 'bar', id: 3 }\n }\n ])\n\nResult:\n Something like this:\n\n [\n {\n callee: { some: 'data', id: 1 },\n association: [\n { foo: 'bar', id: 1 },\n { foo: 'bar', id: 2 },\n { foo: 'bar', id: 3 }\n ]\n }\n ]", "description": "The function takes the result of the query execution and groups\nthe associated data by the callee.\n\nExample:\n groupDataByCalleeFactory([\n {\n callee: { some: 'data', id: 1 },\n association: { foo: 'bar', id: 1 }\n }, {\n callee: { some: 'data', id: 1 },\n association: { foo: 'bar', id: 2 }\n }, {\n callee: { some: 'data', id: 1 },\n association: { foo: 'bar', id: 3 }\n }\n ])\n\nResult:\n Something like this:\n\n [\n {\n callee: { some: 'data', id: 1 },\n association: [\n { foo: 'bar', id: 1 },\n { foo: 'bar', id: 2 },\n { foo: 'bar', id: 3 }\n ]\n }\n ]",
"class": "QueryInterface" "class": "QueryInterface"
}, },
{ {
"file": "lib/dialects/abstract/query.js", "file": "lib/dialects/abstract/query.js",
"line": 393, "line": 405,
"description": "This function will prepare the result of select queries with joins.", "description": "This function will prepare the result of select queries with joins.",
"params": [ "params": [
{ {
...@@ -407,11 +407,11 @@ ...@@ -407,11 +407,11 @@
}, },
{ {
"message": "Missing item type\nThe function takes the result of the query execution and groups\nthe associated data by the callee.\n\nExample:\n groupDataByCalleeFactory([\n {\n callee: { some: 'data', id: 1 },\n association: { foo: 'bar', id: 1 }\n }, {\n callee: { some: 'data', id: 1 },\n association: { foo: 'bar', id: 2 }\n }, {\n callee: { some: 'data', id: 1 },\n association: { foo: 'bar', id: 3 }\n }\n ])\n\nResult:\n Something like this:\n\n [\n {\n callee: { some: 'data', id: 1 },\n association: [\n { foo: 'bar', id: 1 },\n { foo: 'bar', id: 2 },\n { foo: 'bar', id: 3 }\n ]\n }\n ]", "message": "Missing item type\nThe function takes the result of the query execution and groups\nthe associated data by the callee.\n\nExample:\n groupDataByCalleeFactory([\n {\n callee: { some: 'data', id: 1 },\n association: { foo: 'bar', id: 1 }\n }, {\n callee: { some: 'data', id: 1 },\n association: { foo: 'bar', id: 2 }\n }, {\n callee: { some: 'data', id: 1 },\n association: { foo: 'bar', id: 3 }\n }\n ])\n\nResult:\n Something like this:\n\n [\n {\n callee: { some: 'data', id: 1 },\n association: [\n { foo: 'bar', id: 1 },\n { foo: 'bar', id: 2 },\n { foo: 'bar', id: 3 }\n ]\n }\n ]",
"line": " lib/dialects/abstract/query.js:333" "line": " lib/dialects/abstract/query.js:345"
}, },
{ {
"message": "Missing item type\nThis function will prepare the result of select queries with joins.", "message": "Missing item type\nThis function will prepare the result of select queries with joins.",
"line": " lib/dialects/abstract/query.js:393" "line": " lib/dialects/abstract/query.js:405"
}, },
{ {
"message": "Missing item type\nSearch for an instance.", "message": "Missing item type\nSearch for an instance.",
......
...@@ -334,10 +334,22 @@ module.exports = (function() { ...@@ -334,10 +334,22 @@ module.exports = (function() {
} }
var handleSelectQuery = function(results) { var handleSelectQuery = function(results) {
var result = null, self = this; var result = null
if (this.options.raw) { if (this.options.raw) {
result = results.map(Dot.transform) result = results.map(function(result) {
var o = {}
for (var key in result) {
if (result.hasOwnProperty(key)) {
o[key] = result[key]
}
}
return o
})
result = result.map(Dot.transform)
} else if (this.options.hasJoin === true) { } else if (this.options.hasJoin === true) {
result = prepareJoinData.call(this, results) result = prepareJoinData.call(this, results)
result = groupDataByCalleeFactory.call(this, result).map(function(result) { result = groupDataByCalleeFactory.call(this, result).map(function(result) {
......
...@@ -233,7 +233,10 @@ module.exports = (function() { ...@@ -233,7 +233,10 @@ module.exports = (function() {
} }
Sequelize.prototype.query = function(sql, callee, options) { Sequelize.prototype.query = function(sql, callee, options) {
if (arguments.length === 3) { if (arguments.length === 4) {
values.unshift(sql)
sql = Utils.format(values)
} else if (arguments.length === 3) {
options = options options = options
} else if (arguments.length === 2) { } else if (arguments.length === 2) {
options = {} options = {}
......
...@@ -242,10 +242,22 @@ module.exports = (function() { ...@@ -242,10 +242,22 @@ module.exports = (function() {
} }
var handleSelectQuery = function(results) { var handleSelectQuery = function(results) {
var result = null, self = this; var result = null
if (this.options.raw) { if (this.options.raw) {
result = results.map(Dot.transform) result = results.map(function(result) {
var o = {}
for (var key in result) {
if (result.hasOwnProperty(key)) {
o[key] = result[key]
}
}
return o
})
result = result.map(Dot.transform)
} else if (this.options.hasJoin === true) { } else if (this.options.hasJoin === true) {
result = prepareJoinData.call(this, results) result = prepareJoinData.call(this, results)
result = groupDataByCalleeFactory.call(this, result).map(function(result) { result = groupDataByCalleeFactory.call(this, result).map(function(result) {
......
...@@ -36,11 +36,13 @@ var Utils = module.exports = { ...@@ -36,11 +36,13 @@ var Utils = module.exports = {
util.inherits(_class, require('events').EventEmitter) util.inherits(_class, require('events').EventEmitter)
}, },
TICK_CHAR: '`', TICK_CHAR: '`',
addTicks: function(s) { addTicks: function(s, tickChar) {
return Utils.TICK_CHAR + Utils.removeTicks(s) + Utils.TICK_CHAR tickChar = tickChar || Utils.TICK_CHAR
return tickChar + Utils.removeTicks(s, tickChar) + tickChar
}, },
removeTicks: function(s) { removeTicks: function(s, tickChar) {
return s.replace(new RegExp(Utils.TICK_CHAR, 'g'), "") tickChar = tickChar || Utils.TICK_CHAR
return s.replace(new RegExp(tickChar, 'g'), "")
}, },
escape: function(s) { escape: function(s) {
return SqlString.escape(s, true, "local").replace(/\\"/g, '"') return SqlString.escape(s, true, "local").replace(/\\"/g, '"')
......
...@@ -107,21 +107,24 @@ describe(Helpers.getTestDialectTeaser("Sequelize"), function() { ...@@ -107,21 +107,24 @@ describe(Helpers.getTestDialectTeaser("Sequelize"), function() {
}.bind(this)) }.bind(this))
}) })
if (dialect == 'mysql') if (dialect == 'mysql') {
it('executes stored procedures', function(done) { it('executes stored procedures', function(done) {
this.sequelize.query(this.insertQuery).success(function() { this.sequelize.query(this.insertQuery).success(function() {
this.sequelize.query('DROP PROCEDURE IF EXISTS foo').success(function() { this.sequelize.query('DROP PROCEDURE IF EXISTS foo').success(function() {
this.sequelize.query( this.sequelize.query(
"CREATE PROCEDURE foo()\nSELECT * FROM " + this.User.tableName + ";" "CREATE PROCEDURE foo()\nSELECT * FROM " + this.User.tableName + ";"
).success(function() { ).success(function() {
this.sequelize.query('CALL foo()').success(function(users) { this.sequelize.query('CALL foo()').success(function(users) {
expect(users.map(function(u){ return u.username })).toEqual(['john']) expect(users.map(function(u){ return u.username })).toEqual(['john'])
done() done()
}) })
}.bind(this))
}.bind(this)) }.bind(this))
}.bind(this)) }.bind(this))
}.bind(this)) })
}) } else {
console.log('FIXME: I want to be supported in this dialect as well :-(')
}
it('uses the passed DAOFactory', function(done) { it('uses the passed DAOFactory', function(done) {
this.sequelize.query(this.insertQuery).success(function() { this.sequelize.query(this.insertQuery).success(function() {
...@@ -131,6 +134,16 @@ describe(Helpers.getTestDialectTeaser("Sequelize"), function() { ...@@ -131,6 +134,16 @@ describe(Helpers.getTestDialectTeaser("Sequelize"), function() {
}.bind(this)) }.bind(this))
}.bind(this)) }.bind(this))
}) })
it('destructs dot separated attributes when doing a raw query', function(done) {
var tickChar = (dialect === 'postgres') ? '"' : '`'
, sql = "select 1 as " + Utils.addTicks('foo.bar.baz', tickChar)
this.sequelize.query(sql, null, { raw: true }).success(function(result) {
expect(result).toEqual([ { foo: { bar: { baz: 1 } } } ])
done()
})
})
}) })
describe('define', function() { describe('define', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!