sqlquerybuilder
Advanced tools
Comparing version 0.0.50 to 0.0.51
@@ -8,43 +8,63 @@ var _ = require('lodash'); | ||
if (orderByInput.indexOf('.DisplayName') !== -1) { | ||
orderByInput = orderByInput.replace(/\b.DisplayName/ig, "DisplayName"); | ||
} | ||
if (orderByInput.indexOf('.Value') !== -1) { | ||
orderByInput = orderByInput.replace(/\b.Value/ig, "Value"); | ||
} | ||
// TODO: This will break in scenarios with e.g. "order by SUBSTRING(x,0,10)" or "order by ISNULL(x,y)" | ||
var elements = orderByInput.split(','); | ||
orderByInput = _.reduce(elements, function(fullString, element, eIndex) { | ||
element = element.trim(); | ||
if (element.indexOf('.DisplayName') !== -1) { | ||
element = element.replace(/\b.DisplayName/ig, "DisplayName"); | ||
} | ||
if (element.indexOf('.Value') !== -1) { | ||
element = element.replace(/\b.Value/ig, "Value"); | ||
} | ||
var alias; | ||
if (orderByInput.indexOf('.') !== -1 && orderByInput.indexOf('(') !== 0) { | ||
var parts = orderByInput.split('.'); | ||
var table = parts[0]; | ||
if (self._options.enumerationSuffixes.indexOf(parts[1]) !== -1) { | ||
orderByInput = parts[0] + parts[1]; | ||
} else { | ||
if (self._options.entityToTableMap[table]) { | ||
orderByInput = self._options.entityToTableMap[table] + '.' + parts[1]; | ||
var spaceSplits = element.split(' '); | ||
var checkForAscDesc = spaceSplits.pop().toUpperCase(); | ||
if(checkForAscDesc == 'ASC' || checkForAscDesc == 'DESC') | ||
element = spaceSplits.join(' '); | ||
else | ||
checkForAscDesc = ""; | ||
var alias; | ||
if (element.indexOf('.') !== -1 && element.indexOf('(') !== 0) { | ||
var parts = element.split('.'); | ||
var table = parts[0]; | ||
if (self._options.enumerationSuffixes.indexOf(parts[1]) !== -1) { | ||
element = parts[0] + parts[1]; | ||
} else { | ||
alias = _.find(self._selectAliases, function(alias) { | ||
return alias.alias == orderByInput; | ||
}); | ||
if (self._options.entityToTableMap[table]) { | ||
element = self._options.entityToTableMap[table] + '.' + parts[1]; | ||
} else { | ||
alias = _.find(self._selectAliases, function(alias) { | ||
return alias.alias == element; | ||
}); | ||
// Commented out because ordering from grid was not working, all tests still pass. | ||
if (alias) | ||
orderByInput = alias.selectProp; | ||
else | ||
orderByInput = self._checkTableName(table) + '.' + parts[1]; | ||
if (alias) | ||
element = alias.selectProp; | ||
else | ||
element = self._checkTableName(table) + '.' + parts[1]; | ||
} | ||
} | ||
} else if(!skipAlias && element.indexOf('(') !== 0) { | ||
alias = _.find(self._selectAliases, function(alias) { | ||
return alias.alias == element; | ||
}); | ||
if (alias) | ||
element = alias.selectProp; | ||
else | ||
element = self._sqlObject.fromAlias + "." + element; | ||
} | ||
} else if(!skipAlias && orderByInput.indexOf('(') !== 0) { | ||
alias = _.find(self._selectAliases, function(alias) { | ||
return alias.alias == orderByInput; | ||
}); | ||
if (alias) | ||
orderByInput = alias.selectProp; | ||
else | ||
orderByInput = self._sqlObject.fromAlias + "." + orderByInput; | ||
} | ||
checkForAscDesc = checkForAscDesc ? checkForAscDesc : (direction ? direction.toUpperCase() : "ASC"); | ||
if (checkForAscDesc) | ||
element += " " + checkForAscDesc; | ||
if ((eIndex + 1) < elements.length) | ||
element += ", "; | ||
return fullString + element; | ||
},""); | ||
self._sqlObject.orderBy = orderByInput; | ||
self._sqlObject.direction = direction ? direction.toUpperCase() : "ASC"; | ||
@@ -51,0 +71,0 @@ return self; |
{ | ||
"name": "sqlquerybuilder", | ||
"version": "0.0.50", | ||
"version": "0.0.51", | ||
"description": "Highly opinionated Sql Server Query Writer, mostly for internal use.", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index", |
@@ -1,5 +0,1 @@ | ||
/** | ||
* Created by jordan.cotter on 9/12/2014. | ||
*/ | ||
var sqlQueryBuilder = require('../../../lib/index'), | ||
@@ -172,2 +168,15 @@ sqlBuilder = sqlQueryBuilder.queryBuilder; | ||
}); | ||
it('should work', function (done) { | ||
(sqlBuilder() | ||
.select("am.Id, am.MonthValue, ay.YearValue") | ||
.from("AccountingMonths am") | ||
.join("AccountingYears ay", "ay.Id", "am.AccountingYear_id") | ||
.where({"am.MonthValue": 1, "ay.YearValue": 2014}, ">=") | ||
.where({"am.StatusValue": 1}) | ||
.orderBy("ay.YearValue asc, am.MonthValue asc", "") | ||
.build()) | ||
.should.equal("SELECT am.Id, am.MonthValue, ay.YearValue FROM AccountingMonths am JOIN AccountingYears ay ON ay.Id = am.AccountingYear_id WHERE am.MonthValue >= 1 AND ay.YearValue >= 2014 AND am.StatusValue = 1 ORDER BY ay.YearValue ASC, am.MonthValue ASC "); | ||
done(); | ||
}); | ||
}); |
224417
4550