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

Commit 29e5b1f0 by Jan Aagaard Meier

Merge pull request #630 from Obbedire/master

Issue #629: Attribute ambiguity issue when eagerly loading.
2 parents 52fbf2e1 7683f133
...@@ -193,7 +193,7 @@ module.exports = (function() { ...@@ -193,7 +193,7 @@ module.exports = (function() {
for (var attributeName in attributes) { for (var attributeName in attributes) {
attrString.push(Utils._.template('<%= before %> TO <%= after %>')({ attrString.push(Utils._.template('<%= before %> TO <%= after %>')({
before: QueryGenerator.addQuotes(attrBefore), before: QueryGenerator.addQuotes(attrBefore),
after: QueryGenerator.addQuotes(attributeName), after: QueryGenerator.addQuotes(attributeName)
})) }))
} }
...@@ -259,7 +259,7 @@ module.exports = (function() { ...@@ -259,7 +259,7 @@ module.exports = (function() {
} }
if(options.hasOwnProperty('where')) { if(options.hasOwnProperty('where')) {
options.where = QueryGenerator.getWhereConditions(options.where) options.where = QueryGenerator.getWhereConditions(options.where, tableName)
query += " WHERE <%= where %>" query += " WHERE <%= where %>"
} }
...@@ -474,14 +474,16 @@ module.exports = (function() { ...@@ -474,14 +474,16 @@ module.exports = (function() {
}) })
}, },
getWhereConditions: function(smth) { getWhereConditions: function(smth, tableName) {
var result = null var result = null
if (Utils.isHash(smth)) { if (Utils.isHash(smth)) {
smth = Utils.prependTableNameToHash(tableName, smth)
result = QueryGenerator.hashToWhereConditions(smth) result = QueryGenerator.hashToWhereConditions(smth)
} }
else if (typeof smth === "number") { else if (typeof smth === "number") {
result = '\"id\"' + "=" + QueryGenerator.pgEscape(smth) smth = Utils.prependTableNameToHash(tableName, { id: smth })
result = QueryGenerator.hashToWhereConditions(smth)
} }
else if (typeof smth === "string") { else if (typeof smth === "string") {
result = smth result = smth
......
...@@ -121,16 +121,16 @@ describe('QueryGenerator', function() { ...@@ -121,16 +121,16 @@ describe('QueryGenerator', function() {
expectation: "SELECT \"id\", \"name\" FROM \"myTable\";" expectation: "SELECT \"id\", \"name\" FROM \"myTable\";"
}, { }, {
arguments: ['myTable', {where: {id: 2}}], arguments: ['myTable', {where: {id: 2}}],
expectation: "SELECT * FROM \"myTable\" WHERE \"id\"=2;" expectation: "SELECT * FROM \"myTable\" WHERE \"myTable\".\"id\"=2;"
}, { }, {
arguments: ['myTable', {where: {name: 'foo'}}], arguments: ['myTable', {where: {name: 'foo'}}],
expectation: "SELECT * FROM \"myTable\" WHERE \"name\"='foo';" expectation: "SELECT * FROM \"myTable\" WHERE \"myTable\".\"name\"='foo';"
}, { }, {
arguments: ['myTable', {where: {name: "foo';DROP TABLE myTable;"}}], arguments: ['myTable', {where: {name: "foo';DROP TABLE myTable;"}}],
expectation: "SELECT * FROM \"myTable\" WHERE \"name\"='foo'';DROP TABLE myTable;';" expectation: "SELECT * FROM \"myTable\" WHERE \"myTable\".\"name\"='foo'';DROP TABLE myTable;';"
}, { }, {
arguments: ['myTable', {where: 2}], arguments: ['myTable', {where: 2}],
expectation: "SELECT * FROM \"myTable\" WHERE \"id\"=2;" expectation: "SELECT * FROM \"myTable\" WHERE \"myTable\".\"id\"=2;"
}, { }, {
arguments: ['foo', { attributes: [['count(*)', 'count']] }], arguments: ['foo', { attributes: [['count(*)', 'count']] }],
expectation: 'SELECT count(*) as \"count\" FROM \"foo\";' expectation: 'SELECT count(*) as \"count\" FROM \"foo\";'
...@@ -164,7 +164,7 @@ describe('QueryGenerator', function() { ...@@ -164,7 +164,7 @@ describe('QueryGenerator', function() {
expectation: "SELECT * FROM \"mySchema\".\"myTable\";" expectation: "SELECT * FROM \"mySchema\".\"myTable\";"
}, { }, {
arguments: ['mySchema.myTable', {where: {name: "foo';DROP TABLE mySchema.myTable;"}}], arguments: ['mySchema.myTable', {where: {name: "foo';DROP TABLE mySchema.myTable;"}}],
expectation: "SELECT * FROM \"mySchema\".\"myTable\" WHERE \"name\"='foo'';DROP TABLE mySchema.myTable;';" expectation: "SELECT * FROM \"mySchema\".\"myTable\" WHERE \"mySchema\".\"myTable\".\"name\"='foo'';DROP TABLE mySchema.myTable;';"
} }
], ],
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!