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

Commit 4037ed47 by Matt Broadstone

make abstract dialect more abstract

This fixes two problems in particular:
  - there was no existing way to access information about the model
    you were generating a limit and offset for, so the model has been
    added to the options
  - included where generation was done from raw SQL, making it impossible
    to support other dialects that don't support the same LIMIT syntax
1 parent 6f7d6b8d
...@@ -732,9 +732,12 @@ module.exports = (function() { ...@@ -732,9 +732,12 @@ module.exports = (function() {
, subJoinQueries = [] , subJoinQueries = []
, mainTableAs = null; , 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.tableAs = mainTableAs = this.quoteTable(model.name);
} }
options.table = table = !Array.isArray(tableName) ? this.quoteTable(tableName) : tableName.map(function(t) { options.table = table = !Array.isArray(tableName) ? this.quoteTable(tableName) : tableName.map(function(t) {
if (Array.isArray(t)) { if (Array.isArray(t)) {
return this.quoteTable(t[0], t[1]); return this.quoteTable(t[0], t[1]);
...@@ -753,7 +756,6 @@ module.exports = (function() { ...@@ -753,7 +756,6 @@ module.exports = (function() {
}); });
} }
// Escape attributes // Escape attributes
mainAttributes = mainAttributes && mainAttributes.map(function(attr) { mainAttributes = mainAttributes && mainAttributes.map(function(attr) {
var addTable = true; var addTable = true;
...@@ -792,7 +794,6 @@ module.exports = (function() { ...@@ -792,7 +794,6 @@ module.exports = (function() {
mainAttributes = [mainTableAs + '.*']; mainAttributes = [mainTableAs + '.*'];
} }
if (options.include) { if (options.include) {
var generateJoinQueries = function(include, parentTable) { var generateJoinQueries = function(include, parentTable) {
var table = include.model.getTableName() var table = include.model.getTableName()
...@@ -822,7 +823,6 @@ module.exports = (function() { ...@@ -822,7 +823,6 @@ module.exports = (function() {
var attrAs = attr, var attrAs = attr,
verbatim = false; verbatim = false;
if (Array.isArray(attr) && attr.length === 2) { if (Array.isArray(attr) && attr.length === 2) {
if (attr[0]._isSequelizeMethod) { if (attr[0]._isSequelizeMethod) {
if (attr[0] instanceof Utils.literal || if (attr[0] instanceof Utils.literal ||
...@@ -1035,23 +1035,26 @@ module.exports = (function() { ...@@ -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 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 (subQuery && association.isMultiAssociation && include.required) {
if (!options.where) options.where = {}; if (!options.where) options.where = {};
// Creating the as-is where for the subQuery, checks that the required association exists // Creating the as-is where for the subQuery, checks that the required association exists
options.where['__' + as] = self.sequelize.asIs(['(', var $query = self.selectQuery(include.model.getTableName(), {
tableAs: as,
'SELECT ' + self.quoteIdentifier(attrRight), attributes: [attrRight],
'FROM ' + self.quoteTable(table, as), where: self.sequelize.asIs([joinOn]),
'WHERE ' + joinOn, limit: 1
'LIMIT 1', }, include.model);
')', 'IS NOT NULL'].join(' ')); options.where['__' + as] = self.sequelize.asIs([
'(',
$query.replace(/\;$/, ""),
')',
'IS NOT NULL'
].join(' '));
} }
} }
// Generate join SQL // Generate join SQL
joinQueryItem += joinType + self.quoteTable(table, as) + ' ON ' + joinOn; joinQueryItem += joinType + self.quoteTable(table, as) + ' ON ' + joinOn;
} }
if (include.subQuery && subQuery) { if (include.subQuery && subQuery) {
joinQueries.subQuery.push(joinQueryItem); joinQueries.subQuery.push(joinQueryItem);
} else { } else {
...@@ -1158,9 +1161,8 @@ module.exports = (function() { ...@@ -1158,9 +1161,8 @@ module.exports = (function() {
} }
} }
var limitOrder = this.addLimitAndOffset(options, query);
// Add LIMIT, OFFSET to sub or main query // Add LIMIT, OFFSET to sub or main query
var limitOrder = this.addLimitAndOffset(options, model);
if (limitOrder) { if (limitOrder) {
if (subQuery) { if (subQuery) {
subQueryItems.push(limitOrder); subQueryItems.push(limitOrder);
...@@ -1267,19 +1269,26 @@ module.exports = (function() { ...@@ -1267,19 +1269,26 @@ module.exports = (function() {
return 'ROLLBACK;'; 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) { if (options.offset && !options.limit) {
query += ' LIMIT ' + options.offset + ', ' + 10000000000000; fragment += ' LIMIT ' + options.offset + ', ' + 18440000000000000000;
} else if (options.limit) { } else if (options.limit) {
if (options.offset) { if (options.offset) {
query += ' LIMIT ' + options.offset + ', ' + options.limit; fragment += ' LIMIT ' + options.offset + ', ' + options.limit;
} else { } else {
query += ' LIMIT ' + options.limit; fragment += ' LIMIT ' + options.limit;
} }
} }
return query;
return fragment;
}, },
handleSequelizeMethod: function (smth, tableName, factory, options, prepend) { handleSequelizeMethod: function (smth, tableName, factory, options, prepend) {
......
...@@ -327,20 +327,6 @@ module.exports = (function() { ...@@ -327,20 +327,6 @@ module.exports = (function() {
return fields; 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) { quoteIdentifier: function(identifier, force) {
if (identifier === '*') return identifier; if (identifier === '*') return identifier;
return Utils.addTicks(identifier, '`'); return Utils.addTicks(identifier, '`');
......
...@@ -433,17 +433,12 @@ module.exports = (function() { ...@@ -433,17 +433,12 @@ module.exports = (function() {
}); });
}, },
addLimitAndOffset: function(options, query) { addLimitAndOffset: function(options, model) {
query = query || ''; var fragment = '';
if (options.limit) { if (options.limit) fragment += ' LIMIT ' + options.limit;
query += ' LIMIT ' + options.limit; if (options.offset) fragment += ' OFFSET ' + options.offset;
}
if (options.offset) {
query += ' OFFSET ' + options.offset;
}
return query; return fragment;
}, },
attributeToSQL: function(attribute, options) { attributeToSQL: function(attribute, options) {
......
...@@ -142,18 +142,19 @@ module.exports = (function() { ...@@ -142,18 +142,19 @@ module.exports = (function() {
} }
}, },
addLimitAndOffset: function(options, query){ addLimitAndOffset: function(options, model){
query = query || ""; var fragment = '';
if (options.offset && !options.limit) { if (options.offset && !options.limit) {
query += " LIMIT " + options.offset + ", " + 10000000000000; fragment += " LIMIT " + options.offset + ", " + 10000000000000;
} else if (options.limit) { } else if (options.limit) {
if (options.offset) { if (options.offset) {
query += " LIMIT " + options.offset + ", " + options.limit; fragment += " LIMIT " + options.offset + ", " + options.limit;
} else { } else {
query += " LIMIT " + options.limit; fragment += " LIMIT " + options.limit;
} }
} }
return query;
return fragment;
}, },
addColumnQuery: function(table, key, dataType) { 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!