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

Commit ffb5fcba by Mick Hansen

Comments

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