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

Commit 198f3501 by Joel Trost Committed by Matt Broadstone

Alias Test passes

1 parent 9f6c5f04
var common;
common.quoteTable: function(param, as) {
var table = '';
if (as === true) {
as = param.as || param.name || param;
}
if (_.isObject(param)) {
if (common._dialect.supports.schemas) {
if (param.schema) {
table += common.quoteIdentifier(param.schema) + '.';
}
table += common.quoteIdentifier(param.tableName);
} else {
if (param.schema) {
table += param.schema + param.delimiter;
}
table += param.tableName;
table = common.quoteIdentifier(table);
}
} else {
table = common.quoteIdentifier(param);
}
if (as) {
table += ' AS ' + common.quoteIdentifier(as);
}
return table;
};
/*
Escape an identifier (e.g. a table or attribute name)
*/
/* istanbul ignore next */
common.quoteIdentifier: function(identifier, force) {
throwMethodUndefined('quoteIdentifier');
},
/*
Split an identifier into .-separated tokens and quote each part
*/
common.quoteIdentifiers: function(identifiers, force) {
if (identifiers.indexOf('.') !== -1) {
identifiers = identifiers.split('.');
return common.quoteIdentifier(identifiers.slice(0, identifiers.length - 1).join('.')) + '.' + common.quoteIdentifier(identifiers[identifiers.length - 1]);
} else {
return common.quoteIdentifier(identifiers);
}
},
module.exports = function (queryGenerator) {
common = queryGenerator;
};
...@@ -23,6 +23,7 @@ module.exports = (function() { ...@@ -23,6 +23,7 @@ module.exports = (function() {
Utils.inherit(Query, AbstractQuery); Utils.inherit(Query, AbstractQuery);
Query.prototype.run = function(sql) { Query.prototype.run = function(sql) {
console.log(sql);
var self = this; var self = this;
this.sql = sql; this.sql = sql;
......
...@@ -459,16 +459,15 @@ module.exports = { ...@@ -459,16 +459,15 @@ module.exports = {
//we have joins //we have joins
//add join table //add join table
if(options.include){
query.push(loadFieldsWithName(model.rawAttributes, model.name)); query.push(loadFieldsWithName(model.rawAttributes, model.name));
if(options.include){
query.push(',');
for(var i = 0; i < options.include.length; i++){ for(var i = 0; i < options.include.length; i++){
if(options.include[i].as) { if(options.include[i].as) {
query.push(joinFields(options.include[i].model.rawAttributes query.push(joinFields(options.include[i].model.rawAttributes
, options.include[i].as)); , options.include[i].as));
} }
} }
}else {
query.push(loadFields(model.rawAttributes));
} }
return query.join(' '); return query.join(' ');
}, },
...@@ -489,8 +488,18 @@ module.exports = { ...@@ -489,8 +488,18 @@ module.exports = {
break; break;
} }
} }
var associationType = include.association.associationType;
var rootKey = primaryKey;
var joinKey = quoteIdentifier(include.association.foreignKey);
if(associationType === 'BelongsTo'){
rootKey = quoteIdentifier(include.association.foreignKey);
joinKey = primaryKey;
}
var joinType = include.required ? 'INNER JOIN' : 'LEFT OUTER JOIN'; var joinType = include.required ? 'INNER JOIN' : 'LEFT OUTER JOIN';
//var key, joinKey;
//console.log(include.association.associationType);
//if(include.association)
var joinTable = include.as ? include.as : include.model.name; var joinTable = include.as ? include.as : include.model.name;
joinTable = quoteIdentifier(joinTable); joinTable = quoteIdentifier(joinTable);
var tableName = quoteIdentifier(model.name); var tableName = quoteIdentifier(model.name);
...@@ -499,14 +508,19 @@ module.exports = { ...@@ -499,14 +508,19 @@ module.exports = {
joinType, joinType,
quoteIdentifier(include.model.tableName), quoteIdentifier(include.model.tableName),
'AS', joinTable, 'AS', joinTable,
'ON', tableName + '.' + primaryKey, 'ON', tableName + '.' + rootKey,
'=', joinTable + '.' + quoteIdentifier(include.association.foreignKey) '=', joinTable + '.' + joinKey
].join(' '); ].join(' ');
}, },
getWhereClause: function(tableName, model, options){ getWhereClause: function(where, tableName){
//console.log('my model:', model); var query = ['WHERE'];
for(var key in where){
query.push(quoteIdentifier(tableName) + '.' + quoteIdentifier(key));
query.push('=');
query.push(where[key]);
} }
}; return query.join(' ');
}};
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!