Skip to content

Commit 8a3c12b

Browse files
Marshall variable args carefully in arithmetic expressions (terminusdb#297)
1 parent de9ac28 commit 8a3c12b

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

lib/query/woqlCore.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ WOQLQuery.prototype.wform = function (opts) {
260260
*/
261261
WOQLQuery.prototype.arop = function (arg) {
262262
if (typeof arg === 'object') {
263-
return arg.json ? arg.json() : arg;
263+
return this.jobj(this.cleanArithmeticValue(arg));
264264
}
265265
return this.cleanArithmeticValue(arg, 'xsd:decimal');
266266
};

test/woql.spec.js

+36-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const { expect } = require('chai');
22

33
const WOQL = require('../lib/woql');
4-
const { Var } = require('../lib/query/woqlDoc');
4+
const { Var, Vars } = require('../lib/query/woqlDoc');
55

66
const idGenJson = require('./woqlJson/woqlIdgenJson');
77
const woqlStarJson = require('./woqlJson/woqlStarJson');
@@ -461,4 +461,39 @@ describe('woql queries', () => {
461461
})
462462
});
463463

464+
it('check arithmetic var', () => {
465+
let v = Vars("a", "res");
466+
const wq = WOQL.eval(WOQL.times(v.a, 3),v.res).json();
467+
expect(wq).to.deep.eql({
468+
"@type": "Eval",
469+
"expression": {
470+
"@type": "Times",
471+
"left": {
472+
"@type": "ArithmeticValue",
473+
"variable": "a"
474+
},
475+
"right": {
476+
"@type": "ArithmeticValue",
477+
"data": {
478+
"@type": "xsd:decimal",
479+
"@value": 3
480+
}
481+
}
482+
},
483+
"result": {
484+
"@type": "ArithmeticValue",
485+
"variable": "res"
486+
}
487+
})
488+
});
489+
it('check deep arithmetic var', () => {
490+
let v = Vars("a", "res");
491+
const wq = WOQL.and(
492+
WOQL.eval(WOQL.times(3,4), v.a),
493+
WOQL.eval(WOQL.times(v.a, 3),v.res)
494+
).json();
495+
console.log(JSON.stringify(wq));
496+
expect(wq).to.deep.eql(
497+
{"@type":"And","and":[{"@type":"Eval","expression":{"@type":"Times","left":{"@type":"ArithmeticValue","data":{"@type":"xsd:decimal","@value":3}},"right":{"@type":"ArithmeticValue","data":{"@type":"xsd:decimal","@value":4}}},"result":{"@type":"ArithmeticValue","variable":"a"}},{"@type":"Eval","expression":{"@type":"Times","left":{"@type":"ArithmeticValue","variable":"a"},"right":{"@type":"ArithmeticValue","data":{"@type":"xsd:decimal","@value":3}}},"result":{"@type":"ArithmeticValue","variable":"res"}}]})
498+
});
464499
});

0 commit comments

Comments
 (0)