hedera-web/js/sql/filter-item.js

78 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-09-26 09:28:47 +00:00
var Operation = require ('./operation');
2017-04-19 06:16:37 +00:00
var Value = require ('./value');
var Field = require ('./field');
2016-09-26 09:28:47 +00:00
/**
2017-04-19 06:16:37 +00:00
* Objects to be used as an operands of @Sql.Filter. It represents a two
* expressions basic operation composed by a table column, the operator and the
* value extracted from the rendering paramerers.
2016-12-20 09:32:17 +00:00
*/
2016-09-26 09:28:47 +00:00
module.exports = new Class
({
2016-09-26 09:28:47 +00:00
Extends: Operation
,Tag: 'sql-filter-item'
,Properties:
{
2017-04-19 06:16:37 +00:00
/**
* The column name.
*/
field:
2015-11-19 13:57:23 +00:00
{
2017-04-19 06:16:37 +00:00
type: String,
value: null
},
/**
* The source table name or its alias if it has been specified.
*/
target:
{
type: String,
value: null
},
/**
* The parameter name.
*/
param:
{
type: String,
value: null
}
}
2017-04-19 06:16:37 +00:00
/**
* Checks if parameter name haa been defined and if it has a value.
*/
,isReady: function (params)
{
return this.param != null && params != null && params[this.param] != null;
}
,render: function (params)
{
2017-11-13 16:36:30 +00:00
var newOp = new Operation ({type: this.type});
2017-04-19 06:16:37 +00:00
2017-11-13 16:36:30 +00:00
newOp.push (new Field ({
2017-04-19 06:16:37 +00:00
name: this.field,
target: this.target
2017-11-13 16:36:30 +00:00
}));
2017-04-19 06:16:37 +00:00
var value = params[this.param];
2017-11-13 16:36:30 +00:00
if (this.type === Operation.Type.LIKE && typeof value === 'string')
2017-04-19 06:16:37 +00:00
{
value = value.replace (/[\%\?]/g, this._escapeChar);
value = value.replace (/^|\s+|$/g, '%');
}
2017-11-13 16:36:30 +00:00
newOp.push (new Value ({value: value}));
2017-04-19 06:16:37 +00:00
2017-11-13 16:36:30 +00:00
return newOp.render (params);
2017-04-19 06:16:37 +00:00
}
,_escapeChar: function (char)
{
return '\\'+ char;
}
});