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

Commit 8ea65b1f by Joel Trost Committed by Matt Broadstone

Queries correct attributes specified

1 parent 11b72c6f
...@@ -435,10 +435,6 @@ module.exports = (function() { ...@@ -435,10 +435,6 @@ module.exports = (function() {
selectQuery: function(tableName, options, model) { selectQuery: function(tableName, options, model) {
// Enter and change at your own peril -- Mick Hansen // 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 || {}; options = options || {};
var query =[]; var query =[];
if(options.attributes[0][0].fn === 'COUNT'){ if(options.attributes[0][0].fn === 'COUNT'){
......
...@@ -151,13 +151,30 @@ function loadFields(attributes){ ...@@ -151,13 +151,30 @@ function loadFields(attributes){
} }
return attrStr; 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){ function loadFieldsWithName(attributes, tableName){
var attrStr = []; var attrStr = [];
for (var attr in attributes) { if(Array.isArray(attributes[0])){
if(tableName){ for (var i = 0; i < attributes.length; i++) {
attrStr.push(quoteIdentifier(tableName) + "." + quoteIdentifier(attr)); attrStr.push(processField(attributes[i], tableName));
}else{ }
attrStr.push(quoteIdentifier(attr)); }else{
for (var attr in attributes) {
if(tableName){
attrStr.push(quoteIdentifier(tableName) + "." + quoteIdentifier(attr));
}else{
attrStr.push(quoteIdentifier(attr));
}
} }
} }
return attrStr.join(','); return attrStr.join(',');
...@@ -600,7 +617,7 @@ module.exports = { ...@@ -600,7 +617,7 @@ module.exports = {
} }
//add join table //add join table
if(options.include){ if(options.include){
query.push(loadFieldsWithName(model.rawAttributes, model.name)); query.push(loadFieldsWithName(options.attributes, 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) {
...@@ -609,7 +626,8 @@ module.exports = { ...@@ -609,7 +626,8 @@ module.exports = {
} }
} }
}else{ }else{
query.push(loadFieldsWithName(model.rawAttributes)); query.push(loadFieldsWithName(options.attributes));
//query.push(loadFieldsWithName(model.rawAttributes));
} }
return query.join(' '); return query.join(' ');
}, },
......
...@@ -6,6 +6,7 @@ var chai = require('chai') ...@@ -6,6 +6,7 @@ var chai = require('chai')
, expect = chai.expect , expect = chai.expect
, Support = require(__dirname + '/../support') , Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + "/../../lib/data-types") , DataTypes = require(__dirname + "/../../lib/data-types")
, dialect = Support.getTestDialect()
, config = require(__dirname + "/../config/config") , config = require(__dirname + "/../config/config")
, datetime = require('chai-datetime') , datetime = require('chai-datetime')
, promised = require("chai-as-promised") , promised = require("chai-as-promised")
...@@ -170,7 +171,7 @@ describe(Support.getTestDialectTeaser("Model"), function () { ...@@ -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({ this.User.find({
where: { id: 1 }, where: { id: 1 },
attributes: ['id', ['username', 'name']] 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!