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

Commit 8ea65b1f by Joel Trost Committed by Matt Broadstone

Queries correct attributes specified

1 parent 11b72c6f
......@@ -435,10 +435,6 @@ module.exports = (function() {
selectQuery: function(tableName, options, model) {
// Enter and change at your own peril -- Mick Hansen
//console.log('tablename', tableName);
//console.log('options', options);
//console.log('model', model.name);
//console.log(options.where);
options = options || {};
var query =[];
if(options.attributes[0][0].fn === 'COUNT'){
......
......@@ -151,13 +151,30 @@ function loadFields(attributes){
}
return attrStr;
}
function processField(attribute, tableName){
var sql = '';
if(tableName){
sql += quoteIdentifier(tableName) + '.';
}
if(attribute[0] !== attribute[1]){
return sql + quoteIdentifier(attribute[0]) + ' AS ' + quoteIdentifier(attribute[1]);
}else{
return sql + quoteIdentifier(attribute[0]);
}
}
function loadFieldsWithName(attributes, tableName){
var attrStr = [];
for (var attr in attributes) {
if(tableName){
attrStr.push(quoteIdentifier(tableName) + "." + quoteIdentifier(attr));
}else{
attrStr.push(quoteIdentifier(attr));
if(Array.isArray(attributes[0])){
for (var i = 0; i < attributes.length; i++) {
attrStr.push(processField(attributes[i], tableName));
}
}else{
for (var attr in attributes) {
if(tableName){
attrStr.push(quoteIdentifier(tableName) + "." + quoteIdentifier(attr));
}else{
attrStr.push(quoteIdentifier(attr));
}
}
}
return attrStr.join(',');
......@@ -600,7 +617,7 @@ module.exports = {
}
//add join table
if(options.include){
query.push(loadFieldsWithName(model.rawAttributes, model.name));
query.push(loadFieldsWithName(options.attributes, model.name));
query.push(',');
for(var i = 0; i < options.include.length; i++){
if(options.include[i].as) {
......@@ -609,7 +626,8 @@ module.exports = {
}
}
}else{
query.push(loadFieldsWithName(model.rawAttributes));
query.push(loadFieldsWithName(options.attributes));
//query.push(loadFieldsWithName(model.rawAttributes));
}
return query.join(' ');
},
......
......@@ -6,6 +6,7 @@ var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + "/../../lib/data-types")
, dialect = Support.getTestDialect()
, config = require(__dirname + "/../config/config")
, datetime = require('chai-datetime')
, promised = require("chai-as-promised")
......@@ -170,7 +171,7 @@ describe(Support.getTestDialectTeaser("Model"), function () {
})
})
it("should make aliased attributes available", function(done) {
it.only("should make aliased attributes available", function(done) {
this.User.find({
where: { id: 1 },
attributes: ['id', ['username', 'name']]
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!