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

Commit ffb5fcba by Mick Hansen

Comments

1 parent df521949
Showing with 20 additions and 6 deletions
...@@ -460,6 +460,7 @@ module.exports = (function() { ...@@ -460,6 +460,7 @@ module.exports = (function() {
- limit -> The maximum count you want to get. - limit -> The maximum count you want to get.
- offset -> An offset value to start from. Only useable with limit! - offset -> An offset value to start from. Only useable with limit!
*/ */
selectQuery: function(tableName, options, factory) { selectQuery: function(tableName, options, factory) {
options = options || {} options = options || {}
...@@ -476,10 +477,12 @@ module.exports = (function() { ...@@ -476,10 +477,12 @@ module.exports = (function() {
, subQueryAttributes = null , subQueryAttributes = null
, subJoinQueries = [] , subJoinQueries = []
// Escape table
options.table = table = !Array.isArray(tableName) ? this.quoteIdentifiers(tableName) : tableName.map(function(t) { options.table = table = !Array.isArray(tableName) ? this.quoteIdentifiers(tableName) : tableName.map(function(t) {
return this.quoteIdentifiers(t) return this.quoteIdentifiers(t)
}.bind(this)).join(", ") }.bind(this)).join(", ")
// Escape attributes
mainAttributes = mainAttributes && mainAttributes.map(function(attr){ mainAttributes = mainAttributes && mainAttributes.map(function(attr){
var addTable = true var addTable = true
...@@ -507,8 +510,11 @@ module.exports = (function() { ...@@ -507,8 +510,11 @@ module.exports = (function() {
return attr return attr
}.bind(this)) }.bind(this))
// If no attributes specified, use *
mainAttributes = mainAttributes || (options.include ? [options.table+'.*'] : ['*']) mainAttributes = mainAttributes || (options.include ? [options.table+'.*'] : ['*'])
// If subquery, we ad the mainAttributes to the subQuery and set the mainAttributes to select * from subquery
if (subQuery) { if (subQuery) {
subQueryAttributes = mainAttributes subQueryAttributes = mainAttributes
mainAttributes = [options.table+'.*'] mainAttributes = [options.table+'.*']
...@@ -596,6 +602,7 @@ module.exports = (function() { ...@@ -596,6 +602,7 @@ module.exports = (function() {
if (subQuery) { if (subQuery) {
if (!options.where) options.where = {} if (!options.where) options.where = {}
// Creating the as-is where for the subQuery, checks that the required association exists
var _where = "("; var _where = "(";
_where += "SELECT "+self.quoteIdentifier(identSource)+" FROM " + self.quoteIdentifier(throughTable) + " AS " + self.quoteIdentifier(throughAs); _where += "SELECT "+self.quoteIdentifier(identSource)+" FROM " + self.quoteIdentifier(throughTable) + " AS " + self.quoteIdentifier(throughAs);
_where += joinType + self.quoteIdentifier(table) + " AS " + self.quoteIdentifier(as) + " ON "+targetJoinOn; _where += joinType + self.quoteIdentifier(table) + " AS " + self.quoteIdentifier(as) + " ON "+targetJoinOn;
...@@ -626,6 +633,7 @@ module.exports = (function() { ...@@ -626,6 +633,7 @@ module.exports = (function() {
// If its a multi association we need to add a where query to the main where (executed in the subquery) // If its a multi association we need to add a where query to the main where (executed in the subquery)
if (subQuery && association.isMultiAssociation) { if (subQuery && association.isMultiAssociation) {
if (!options.where) options.where = {} if (!options.where) options.where = {}
// Creating the as-is where for the subQuery, checks that the required association exists
options.where["__"+as] = self.sequelize.asIs("(SELECT "+self.quoteIdentifier(attrRight)+" FROM " + self.quoteIdentifier(tableRight) + " WHERE " + where + " LIMIT 1) IS NOT NULL") options.where["__"+as] = self.sequelize.asIs("(SELECT "+self.quoteIdentifier(attrRight)+" FROM " + self.quoteIdentifier(tableRight) + " WHERE " + where + " LIMIT 1) IS NOT NULL")
} }
} }
...@@ -641,29 +649,31 @@ module.exports = (function() { ...@@ -641,29 +649,31 @@ module.exports = (function() {
return joinQueryItem return joinQueryItem
} }
// Loop through includes and generate subqueries
options.include.forEach(function(include) { options.include.forEach(function(include) {
var joinQueryItem = generateJoinQuery(include, tableName) var joinQueryItem = generateJoinQuery(include, tableName)
if (subQuery && (include.hasIncludeWhere || include.where)) { // If not many to many, and we're doing a subquery, add joinQuery to subQueries
if (include.association.isMultiAssociation) { if (!include.association.isMultiAssociation && (subQuery && (include.hasIncludeWhere || include.where))) {
mainJoinQueries.push(joinQueryItem) subJoinQueries.push(joinQueryItem)
} else {
subJoinQueries.push(joinQueryItem)
}
} else { } else {
mainJoinQueries.push(joinQueryItem) mainJoinQueries.push(joinQueryItem)
} }
}.bind(this)) }.bind(this))
} }
// If using subQuery select defined subQuery attributes and join subJoinQueries
if (subQuery) { if (subQuery) {
subQueryItems.push("SELECT " + subQueryAttributes.join(', ') + " FROM " + options.table) subQueryItems.push("SELECT " + subQueryAttributes.join(', ') + " FROM " + options.table)
subQueryItems.push(subJoinQueries.join('')) subQueryItems.push(subJoinQueries.join(''))
// Else do it the reguar way
} else { } else {
mainQueryItems.push("SELECT " + mainAttributes.join(', ') + " FROM " + options.table) mainQueryItems.push("SELECT " + mainAttributes.join(', ') + " FROM " + options.table)
mainQueryItems.push(mainJoinQueries.join('')) mainQueryItems.push(mainJoinQueries.join(''))
} }
// Add WHERE to sub or main query
if (options.hasOwnProperty('where')) { if (options.hasOwnProperty('where')) {
options.where = this.getWhereConditions(options.where, tableName, factory, options) options.where = this.getWhereConditions(options.where, tableName, factory, options)
if (subQuery) { if (subQuery) {
...@@ -673,6 +683,7 @@ module.exports = (function() { ...@@ -673,6 +683,7 @@ module.exports = (function() {
} }
} }
// Add GROUP BY to sub or main query
if (options.group) { if (options.group) {
options.group = Array.isArray(options.group) ? options.group.map(function (t) { return this.quote(t) }.bind(this)).join(', ') : options.group options.group = Array.isArray(options.group) ? options.group.map(function (t) { return this.quote(t) }.bind(this)).join(', ') : options.group
if (subQuery) { if (subQuery) {
...@@ -682,6 +693,7 @@ module.exports = (function() { ...@@ -682,6 +693,7 @@ module.exports = (function() {
} }
} }
// Add ORDER to sub or main query
if (options.order) { if (options.order) {
options.order = Array.isArray(options.order) ? options.order.map(function (t) { return this.quote(t) }.bind(this)).join(', ') : options.order options.order = Array.isArray(options.order) ? options.order.map(function (t) { return this.quote(t) }.bind(this)).join(', ') : options.order
...@@ -695,6 +707,7 @@ module.exports = (function() { ...@@ -695,6 +707,7 @@ module.exports = (function() {
var limitOrder = this.addLimitAndOffset(options, query) var limitOrder = this.addLimitAndOffset(options, query)
// Add LIMIT, OFFSET to sub or main query
if (limitOrder) { if (limitOrder) {
if (subQuery) { if (subQuery) {
subQueryItems.push(limitOrder) subQueryItems.push(limitOrder)
...@@ -703,6 +716,7 @@ module.exports = (function() { ...@@ -703,6 +716,7 @@ module.exports = (function() {
} }
} }
// If using subQuery, select attributes from wrapped subQuery and join out join tables
if (subQuery) { if (subQuery) {
query = "SELECT " + mainAttributes.join(', ') + " FROM (" query = "SELECT " + mainAttributes.join(', ') + " FROM ("
query += subQueryItems.join('') query += subQueryItems.join('')
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!