Skip to content

Commit 4b33e89

Browse files
Merge pull request #61 from terminusdb/fixOrderBy
fix: #35 bug in order_by function
2 parents 2982361 + 8a0b528 commit 4b33e89

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

lib/query/woqlPrinter.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,11 @@ WOQLPrinter.prototype.pvar = function(json) {
220220
//if (json['woql:variable_name'] && typeof json['woql:variable_name']['@value'] != 'undefined') {
221221
if(json['variable']){
222222
let varname = json['variable']
223-
let order = json['order'] ? ` ${json['order']}` : ''
223+
let order = json['order'] ? json['order'] : ''
224224
if (varname.indexOf(':') === -1) {
225225
varname = 'v:' + varname
226226
}
227-
return `"${varname}${order}"`
227+
return (order!=='' && order!=='asc') ? `["${varname}","${order}"]` : `"${varname}"`;
228228
}else if (json['node']){
229229
return `"${json['node']}"`
230230
}else if (json['data'] ){

lib/query/woqlQuery.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -886,16 +886,28 @@ WOQLQuery.prototype.order_by = function(...orderedVarlist) {
886886
: false
887887

888888
for (var i = 0; i < orderedVarlist.length; i++) {
889-
if (typeof orderedVarlist[i] == 'string'){
890-
let obj = {
889+
let obj;
890+
if (typeof orderedVarlist[i] == 'string' && orderedVarlist[i] !== ""){
891+
obj = {
891892
'@type': 'OrderTemplate',
892893
'variable': this.rawVar(orderedVarlist[i]),
893894
'order': "asc"
894895
}
895-
this.cursor['ordering'].push(obj)
896-
}else{
897-
this.cursor['ordering'].push(orderedVarlist[i])
896+
} else if(orderedVarlist[i].length === 2 && orderedVarlist[i][1] === 'asc'){
897+
obj = {
898+
'@type': 'OrderTemplate',
899+
'variable': this.rawVar(orderedVarlist[i][0]),
900+
'order': "asc"
901+
}
902+
} else if(orderedVarlist[i].length === 2 && orderedVarlist[i][1] === 'desc'){
903+
obj = {
904+
'@type': 'OrderTemplate',
905+
'variable': this.rawVar(orderedVarlist[i][0]),
906+
'order': "desc"
907+
}
898908
}
909+
910+
if(obj) this.cursor['ordering'].push(obj);
899911
}
900912
return this.addSubQuery(embedquery)
901913
}

lib/woql.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -871,10 +871,10 @@ WOQL.cast = WOQL.typecast
871871

872872
/**
873873
* Orders the results of the contained subquery by a precedence list of variables
874-
* @param {...string} varNames - A sequence of variables, by which to order the results, each optionally followed by either “asc” or “desc” to represent order
874+
* @param {...string} varNames - A sequence of variables, by which to order the results, each optionally followed by either “asc” or “desc” to represent order as a list, by default it will sort the variable in ascending order
875875
* @returns {WOQLQuery} A WOQLQuery which contains the ordering expression
876876
* @example
877-
* WOQL.order_by("v:A", "v:B asc", "v:C desc").triple("v:A", "v:B", "v:C");
877+
* WOQL.order_by("v:A", ["v:B", "asc"], ["v:C", "desc"]).triple("v:A", "v:B", "v:C");
878878
*/
879879
WOQL.order_by = function(...varNames) {
880880
return new WOQLQuery().order_by(...varNames)

0 commit comments

Comments
 (0)