From 8a0b528f2c3d094dc76dd1406a8f83335a3f6720 Mon Sep 17 00:00:00 2001 From: NeelParihar Date: Mon, 1 Nov 2021 14:55:14 +0530 Subject: [PATCH] fix: #35 bug in order_by function Signed-off-by: NeelParihar --- lib/query/woqlPrinter.js | 4 ++-- lib/query/woqlQuery.js | 22 +++++++++++++++++----- lib/woql.js | 4 ++-- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/lib/query/woqlPrinter.js b/lib/query/woqlPrinter.js index 2a905943..beddcb9c 100644 --- a/lib/query/woqlPrinter.js +++ b/lib/query/woqlPrinter.js @@ -220,11 +220,11 @@ WOQLPrinter.prototype.pvar = function(json) { //if (json['woql:variable_name'] && typeof json['woql:variable_name']['@value'] != 'undefined') { if(json['variable']){ let varname = json['variable'] - let order = json['order'] ? ` ${json['order']}` : '' + let order = json['order'] ? json['order'] : '' if (varname.indexOf(':') === -1) { varname = 'v:' + varname } - return `"${varname}${order}"` + return (order!=='' && order!=='asc') ? `["${varname}","${order}"]` : `"${varname}"`; }else if (json['node']){ return `"${json['node']}"` }else if (json['data'] ){ diff --git a/lib/query/woqlQuery.js b/lib/query/woqlQuery.js index a64cd0ac..a2c2ac81 100644 --- a/lib/query/woqlQuery.js +++ b/lib/query/woqlQuery.js @@ -886,16 +886,28 @@ WOQLQuery.prototype.order_by = function(...orderedVarlist) { : false for (var i = 0; i < orderedVarlist.length; i++) { - if (typeof orderedVarlist[i] == 'string'){ - let obj = { + let obj; + if (typeof orderedVarlist[i] == 'string' && orderedVarlist[i] !== ""){ + obj = { '@type': 'OrderTemplate', 'variable': this.rawVar(orderedVarlist[i]), 'order': "asc" } - this.cursor['ordering'].push(obj) - }else{ - this.cursor['ordering'].push(orderedVarlist[i]) + } else if(orderedVarlist[i].length === 2 && orderedVarlist[i][1] === 'asc'){ + obj = { + '@type': 'OrderTemplate', + 'variable': this.rawVar(orderedVarlist[i][0]), + 'order': "asc" + } + } else if(orderedVarlist[i].length === 2 && orderedVarlist[i][1] === 'desc'){ + obj = { + '@type': 'OrderTemplate', + 'variable': this.rawVar(orderedVarlist[i][0]), + 'order': "desc" + } } + + if(obj) this.cursor['ordering'].push(obj); } return this.addSubQuery(embedquery) } diff --git a/lib/woql.js b/lib/woql.js index f7029a7a..425305c0 100644 --- a/lib/woql.js +++ b/lib/woql.js @@ -871,10 +871,10 @@ WOQL.cast = WOQL.typecast /** * Orders the results of the contained subquery by a precedence list of variables - * @param {...string} varNames - A sequence of variables, by which to order the results, each optionally followed by either “asc” or “desc” to represent order + * @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 * @returns {WOQLQuery} A WOQLQuery which contains the ordering expression * @example - * WOQL.order_by("v:A", "v:B asc", "v:C desc").triple("v:A", "v:B", "v:C"); + * WOQL.order_by("v:A", ["v:B", "asc"], ["v:C", "desc"]).triple("v:A", "v:B", "v:C"); */ WOQL.order_by = function(...varNames) { return new WOQLQuery().order_by(...varNames)