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

Commit 5f0c5054 by Mick Hansen

Merge pull request #2588 from mbroadst/update-select-query

make abstract dialect more abstract
2 parents 6f7d6b8d 4037ed47
......@@ -732,9 +732,12 @@ module.exports = (function() {
, subJoinQueries = []
, mainTableAs = null;
if (!Array.isArray(tableName) && model) {
if (options.tableAs) {
mainTableAs = this.quoteTable(options.tableAs);
} else if (!Array.isArray(tableName) && model) {
options.tableAs = mainTableAs = this.quoteTable(model.name);
}
options.table = table = !Array.isArray(tableName) ? this.quoteTable(tableName) : tableName.map(function(t) {
if (Array.isArray(t)) {
return this.quoteTable(t[0], t[1]);
......@@ -753,7 +756,6 @@ module.exports = (function() {
});
}
// Escape attributes
mainAttributes = mainAttributes && mainAttributes.map(function(attr) {
var addTable = true;
......@@ -792,7 +794,6 @@ module.exports = (function() {
mainAttributes = [mainTableAs + '.*'];
}
if (options.include) {
var generateJoinQueries = function(include, parentTable) {
var table = include.model.getTableName()
......@@ -822,7 +823,6 @@ module.exports = (function() {
var attrAs = attr,
verbatim = false;
if (Array.isArray(attr) && attr.length === 2) {
if (attr[0]._isSequelizeMethod) {
if (attr[0] instanceof Utils.literal ||
......@@ -1035,23 +1035,26 @@ 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 && include.required) {
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.quoteTable(table, as),
'WHERE ' + joinOn,
'LIMIT 1',
')', 'IS NOT NULL'].join(' '));
var $query = self.selectQuery(include.model.getTableName(), {
tableAs: as,
attributes: [attrRight],
where: self.sequelize.asIs([joinOn]),
limit: 1
}, include.model);
options.where['__' + as] = self.sequelize.asIs([
'(',
$query.replace(/\;$/, ""),
')',
'IS NOT NULL'
].join(' '));
}
}
// Generate join SQL
joinQueryItem += joinType + self.quoteTable(table, as) + ' ON ' + joinOn;
}
if (include.subQuery && subQuery) {
joinQueries.subQuery.push(joinQueryItem);
} else {
......@@ -1158,9 +1161,8 @@ module.exports = (function() {
}
}
var limitOrder = this.addLimitAndOffset(options, query);
// Add LIMIT, OFFSET to sub or main query
var limitOrder = this.addLimitAndOffset(options, model);
if (limitOrder) {
if (subQuery) {
subQueryItems.push(limitOrder);
......@@ -1267,19 +1269,26 @@ module.exports = (function() {
return 'ROLLBACK;';
},
addLimitAndOffset: function(options, query) {
query = query || '';
/**
* Returns an SQL fragment for adding result constraints
*
* @param {Object} options An object with selectQuery options.
* @param {Object} options The model passed to the selectQuery.
* @return {String} The generated sql query.
*/
addLimitAndOffset: function(options, model) {
var fragment = '';
if (options.offset && !options.limit) {
query += ' LIMIT ' + options.offset + ', ' + 10000000000000;
fragment += ' LIMIT ' + options.offset + ', ' + 18440000000000000000;
} else if (options.limit) {
if (options.offset) {
query += ' LIMIT ' + options.offset + ', ' + options.limit;
fragment += ' LIMIT ' + options.offset + ', ' + options.limit;
} else {
query += ' LIMIT ' + options.limit;
fragment += ' LIMIT ' + options.limit;
}
}
return query;
return fragment;
},
handleSequelizeMethod: function (smth, tableName, factory, options, prepend) {
......
......@@ -327,20 +327,6 @@ module.exports = (function() {
return fields;
},
addLimitAndOffset: function(options, query) {
query = query || '';
if (options.offset && !options.limit) {
query += ' LIMIT ' + options.offset + ', ' + 18440000000000000000;
} else if (options.limit) {
if (options.offset) {
query += ' LIMIT ' + options.offset + ', ' + options.limit;
} else {
query += ' LIMIT ' + options.limit;
}
}
return query;
},
quoteIdentifier: function(identifier, force) {
if (identifier === '*') return identifier;
return Utils.addTicks(identifier, '`');
......
......@@ -433,17 +433,12 @@ module.exports = (function() {
});
},
addLimitAndOffset: function(options, query) {
query = query || '';
if (options.limit) {
query += ' LIMIT ' + options.limit;
}
if (options.offset) {
query += ' OFFSET ' + options.offset;
}
addLimitAndOffset: function(options, model) {
var fragment = '';
if (options.limit) fragment += ' LIMIT ' + options.limit;
if (options.offset) fragment += ' OFFSET ' + options.offset;
return query;
return fragment;
},
attributeToSQL: function(attribute, options) {
......
......@@ -142,18 +142,19 @@ module.exports = (function() {
}
},
addLimitAndOffset: function(options, query){
query = query || "";
addLimitAndOffset: function(options, model){
var fragment = '';
if (options.offset && !options.limit) {
query += " LIMIT " + options.offset + ", " + 10000000000000;
fragment += " LIMIT " + options.offset + ", " + 10000000000000;
} else if (options.limit) {
if (options.offset) {
query += " LIMIT " + options.offset + ", " + options.limit;
fragment += " LIMIT " + options.offset + ", " + options.limit;
} else {
query += " LIMIT " + options.limit;
fragment += " LIMIT " + options.limit;
}
}
return query;
return fragment;
},
addColumnQuery: function(table, key, dataType) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!