2016-09-26 09:28:47 +00:00
|
|
|
|
|
|
|
var Operation = require ('./operation');
|
2017-04-19 06:16:37 +00:00
|
|
|
var Value = require ('./value');
|
2016-09-26 09:28:47 +00:00
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
/**
|
2017-04-19 06:16:37 +00:00
|
|
|
* The equivalent of a SQL filter expression. It allows to automatically build
|
|
|
|
* SQL filters based on lot parameters.
|
2016-12-20 09:32:17 +00:00
|
|
|
*/
|
2016-09-26 09:28:47 +00:00
|
|
|
module.exports = new Class
|
2015-01-23 13:09:30 +00:00
|
|
|
({
|
2016-09-26 09:28:47 +00:00
|
|
|
Extends: Operation
|
2015-01-23 13:09:30 +00:00
|
|
|
,Tag: 'sql-filter'
|
|
|
|
|
2017-04-19 06:16:37 +00:00
|
|
|
/**
|
|
|
|
* Checks if any of filters childs are ready.
|
|
|
|
*/
|
|
|
|
,isReady: function (params)
|
2015-01-23 13:09:30 +00:00
|
|
|
{
|
2017-04-19 06:16:37 +00:00
|
|
|
var exprs = this.exprs.objects;
|
|
|
|
for (var i = exprs.length; i--;)
|
|
|
|
if (exprs[i].isReady (params))
|
2015-01-23 13:09:30 +00:00
|
|
|
return true;
|
2017-04-19 06:16:37 +00:00
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
return false;
|
|
|
|
}
|
2015-11-17 10:34:33 +00:00
|
|
|
|
2017-04-19 06:16:37 +00:00
|
|
|
/**
|
|
|
|
* Renders the filter as an SQL expression. If any of its childs isn't
|
|
|
|
* ready is ommitted from the expression. If all of its childs aren't ready
|
|
|
|
* renders the TRUE expression.
|
|
|
|
*/
|
|
|
|
,render: function (params)
|
2015-11-17 10:34:33 +00:00
|
|
|
{
|
2017-04-19 06:16:37 +00:00
|
|
|
var op = new Operation ({type: this.type});
|
|
|
|
var exprs = this.exprs.objects;
|
|
|
|
|
|
|
|
for (var i = 0; i < exprs.length; i++)
|
|
|
|
if (exprs[i].isReady (params))
|
|
|
|
op.exprs.add (exprs[i]);
|
2015-11-17 10:34:33 +00:00
|
|
|
|
2017-04-19 06:16:37 +00:00
|
|
|
if (op.exprs.objects.length == 0)
|
|
|
|
op = new Value ({value: true});
|
2015-11-17 10:34:33 +00:00
|
|
|
|
2017-04-19 06:16:37 +00:00
|
|
|
return op.render (params);
|
2015-11-17 10:34:33 +00:00
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
});
|