javascript - How to test a MySQL syntax using Math.js parser -


i allow user specify mysql calculated field in web form field might read like:

(bldgfuelcostelectricity * assetmotorsize) * if(bldgfield1>bldgfield2,bldgfield1-bldgfield2,0) * 24 

at run time substitute other table columns camelcase parameters, before let user save expression part of report template, test function validity using math.js doesn't if() construct. math.js allow function name overrides, cannot make work particular case of if because that's reserved word.

how can test mysql function valid using math.js when mysql has functions have javascript equivalents?

you need careful testing mysql syntax parser of math.js, syntaxes differ. have define syntax support , explicitely test this.

what can math.js parse expressions expression tree , anaylise them. traversing on tree, can validate syntax. example:

var node = math.parse('3 * x + 2'); node.traverse(function (node, path, parent) {   switch (node.type) {     case 'operatornode': console.log(node.type, node.op);    break;     case 'constantnode': console.log(node.type, node.value); break;     case 'symbolnode':   console.log(node.type, node.name);  break;     default:             console.log(node.type);   } }); // outputs: //   operatornode + //   operatornode * //   constantnode 3 //   symbolnode x //   constantnode 2 

so in case, can parse expression , analyze returned tree:

var node = math.parse('(bldgfuelcostelectricity * assetmotorsize) * if(bldgfield1>bldgfield2,bldgfield1-bldgfield2,0) * 24'); // node.traverse(...) 

see docs more information: http://mathjs.org/docs/expressions/expression_trees.html


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -