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

Commit 6307f1dc by Joel Trost Committed by Matt Broadstone

Fixes to where clause generation

1 parent 089cedc8
...@@ -391,10 +391,9 @@ module.exports = (function() { ...@@ -391,10 +391,9 @@ module.exports = (function() {
// Enter and change at your own peril -- Mick Hansen // Enter and change at your own peril -- Mick Hansen
//console.log('tablename', tableName); //console.log('tablename', tableName);
//console.log('options', options); //console.log('options', options);
//console.log('model', model); //console.log('model', model.name);
//console.log(options.where); //console.log(options.where);
options = options || {}; options = options || {};
var query = [ var query = [
SqlGenerator.getSelectorClause(model,options), SqlGenerator.getSelectorClause(model,options),
SqlGenerator.getFromClause(model.tableName, model.name) SqlGenerator.getFromClause(model.tableName, model.name)
...@@ -501,7 +500,9 @@ module.exports = (function() { ...@@ -501,7 +500,9 @@ module.exports = (function() {
getWhereConditions: function(where, tableName, model, options, prepend) { getWhereConditions: function(where, tableName, model, options, prepend) {
//console.log('where:', model); //console.log('where:', model);
console.log('logic', where); //console.log('logic', where);
//console.log('options', options);
if(where){ if(where){
return SqlGenerator.getWhereClause(where, tableName); return SqlGenerator.getWhereClause(where, tableName);
}else{ }else{
......
...@@ -486,8 +486,8 @@ module.exports = { ...@@ -486,8 +486,8 @@ module.exports = {
//we have joins //we have joins
//add join table //add join table
query.push(loadFieldsWithName(model.rawAttributes, model.name));
if(options.include){ if(options.include){
query.push(loadFieldsWithName(model.rawAttributes, model.name));
query.push(','); 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) {
...@@ -495,6 +495,8 @@ module.exports = { ...@@ -495,6 +495,8 @@ module.exports = {
, options.include[i].as)); , options.include[i].as));
} }
} }
}else{
query.push(loadFieldsWithName(model.rawAttributes));
} }
return query.join(' '); return query.join(' ');
}, },
...@@ -517,6 +519,7 @@ module.exports = { ...@@ -517,6 +519,7 @@ module.exports = {
} }
var associationType = include.association.associationType; var associationType = include.association.associationType;
var rootKey = primaryKey; var rootKey = primaryKey;
console.log(include.association);
var joinKey = quoteIdentifier(include.association.foreignKey); var joinKey = quoteIdentifier(include.association.foreignKey);
if(associationType === 'BelongsTo'){ if(associationType === 'BelongsTo'){
rootKey = quoteIdentifier(include.association.foreignKey); rootKey = quoteIdentifier(include.association.foreignKey);
...@@ -530,7 +533,6 @@ module.exports = { ...@@ -530,7 +533,6 @@ module.exports = {
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);
//console.log(include);
return [ return [
joinType, joinType,
quoteIdentifier(include.model.tableName), quoteIdentifier(include.model.tableName),
...@@ -541,6 +543,8 @@ module.exports = { ...@@ -541,6 +543,8 @@ module.exports = {
}, },
getWhereClause: function(where, tableName){ getWhereClause: function(where, tableName){
var query = ['WHERE']; var query = ['WHERE'];
//console.log(where);
//console.log(tableName);
for(var key in where){ for(var key in where){
if(tableName){ if(tableName){
query.push(quoteIdentifier(tableName) + '.' + quoteIdentifier(key)); query.push(quoteIdentifier(tableName) + '.' + quoteIdentifier(key));
......
...@@ -38,72 +38,72 @@ describe(Support.getTestDialectTeaser("Alias"), function() { ...@@ -38,72 +38,72 @@ describe(Support.getTestDialectTeaser("Alias"), function() {
}); });
}); });
// it('shouldnt touch the passed alias', function () { it('shouldnt touch the passed alias', function () {
// var User = this.sequelize.define('user', {}) var User = this.sequelize.define('user', {})
// , Task = this.sequelize.define('task', {}); , Task = this.sequelize.define('task', {});
// User.hasMany(Task, { as: 'ASSIGNMENTS', foreignKey: 'userId' }); User.hasMany(Task, { as: 'ASSIGNMENTS', foreignKey: 'userId' });
// Task.belongsTo(User, { as: 'OWNER', foreignKey: 'userId' }); Task.belongsTo(User, { as: 'OWNER', foreignKey: 'userId' });
// return this.sequelize.sync({ force: true }).then(function () { return this.sequelize.sync({ force: true }).then(function () {
// return User.create({ id: 1 }); return User.create({ id: 1 });
// }).then(function (user){ }).then(function (user){
// expect(user.getASSIGNMENTS).to.be.ok; expect(user.getASSIGNMENTS).to.be.ok;
// return Task.create({ id: 1, userId: 1 }); return Task.create({ id: 1, userId: 1 });
// }).then(function (task) { }).then(function (task) {
// expect(task.getOWNER).to.be.ok; expect(task.getOWNER).to.be.ok;
// return Promise.all([ return Promise.all([
// User.find({ where: { id: 1 }, include: [{model: Task, as: 'ASSIGNMENTS'}] }), User.find({ where: { id: 1 }, include: [{model: Task, as: 'ASSIGNMENTS'}] }),
// Task.find({ where: { id: 1 }, include: [{model: User, as: 'OWNER'}] }), Task.find({ where: { id: 1 }, include: [{model: User, as: 'OWNER'}] }),
// ]); ]);
// }).spread(function (user, task) { }).spread(function (user, task) {
// expect(user.ASSIGNMENTS).to.be.ok; expect(user.ASSIGNMENTS).to.be.ok;
// expect(task.OWNER).to.be.ok; expect(task.OWNER).to.be.ok;
// }); });
// }); });
// it('should allow me to pass my own plural and singular forms to hasMany', function () { it('should allow me to pass my own plural and singular forms to hasMany', function () {
// var User = this.sequelize.define('user', {}) var User = this.sequelize.define('user', {})
// , Task = this.sequelize.define('task', {}); , Task = this.sequelize.define('task', {});
// User.hasMany(Task, { as: { singular: 'task', plural: 'taskz'} }); User.hasMany(Task, { as: { singular: 'task', plural: 'taskz'} });
// return this.sequelize.sync({ force: true }).then(function () { return this.sequelize.sync({ force: true }).then(function () {
// return User.create({ id: 1 }); return User.create({ id: 1 });
// }).then(function (user) { }).then(function (user) {
// expect(user.getTaskz).to.be.ok; expect(user.getTaskz).to.be.ok;
// expect(user.addTask).to.be.ok; expect(user.addTask).to.be.ok;
// expect(user.addTaskz).to.be.ok; expect(user.addTaskz).to.be.ok;
// }).then(function () { }).then(function () {
// return User.find({ where: { id: 1 }, include: [{model: Task, as: 'taskz'}] }); return User.find({ where: { id: 1 }, include: [{model: Task, as: 'taskz'}] });
// }).then(function (user) { }).then(function (user) {
// expect(user.taskz).to.be.ok; expect(user.taskz).to.be.ok;
// }); });
// }); });
// it('should allow me to define plural and singular forms on the model', function () { it('should allow me to define plural and singular forms on the model', function () {
// var User = this.sequelize.define('user', {}) var User = this.sequelize.define('user', {})
// , Task = this.sequelize.define('task', {}, { , Task = this.sequelize.define('task', {}, {
// name: { name: {
// singular: 'assignment', singular: 'assignment',
// plural: 'assignments' plural: 'assignments'
// } }
// }); });
// User.hasMany(Task); User.hasMany(Task);
// return this.sequelize.sync({ force: true }).then(function () { return this.sequelize.sync({ force: true }).then(function () {
// return User.create({ id: 1 }); return User.create({ id: 1 });
// }).then(function (user) { }).then(function (user) {
// expect(user.getAssignments).to.be.ok; expect(user.getAssignments).to.be.ok;
// expect(user.addAssignment).to.be.ok; expect(user.addAssignment).to.be.ok;
// expect(user.addAssignments).to.be.ok; expect(user.addAssignments).to.be.ok;
// }).then(function () { }).then(function () {
// return User.find({ where: { id: 1 }, include: [Task] }); return User.find({ where: { id: 1 }, include: [Task] });
// }).then(function (user) { }).then(function (user) {
// expect(user.assignments).to.be.ok; expect(user.assignments).to.be.ok;
// }); });
// }); });
}); });
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!