2016-09-26 09:28:47 +00:00
|
|
|
|
2022-05-28 19:27:36 +00:00
|
|
|
var Expr = require('./expr');
|
2016-09-26 09:28:47 +00:00
|
|
|
|
2015-01-23 13:09:30 +00:00
|
|
|
/**
|
|
|
|
* The equivalent of a SQL operation between exprs.
|
|
|
|
*
|
|
|
|
* @param {Array#Sql.Expr} expr Array with the exprs
|
|
|
|
* @param {Sql..Operation.Type} type The type of the operation
|
2022-05-26 06:08:31 +00:00
|
|
|
*/
|
2022-05-28 19:27:36 +00:00
|
|
|
var Operation = new Class();
|
2016-09-26 09:28:47 +00:00
|
|
|
module.exports = Operation;
|
|
|
|
|
2022-05-28 19:27:36 +00:00
|
|
|
var Type = {
|
2016-09-26 09:28:47 +00:00
|
|
|
EQUAL : 0
|
|
|
|
,LIKE : 1
|
|
|
|
,AND : 2
|
|
|
|
,OR : 3
|
|
|
|
,REGEXP : 4
|
|
|
|
};
|
|
|
|
|
2022-05-28 19:27:36 +00:00
|
|
|
var Operators = [
|
2016-09-26 09:28:47 +00:00
|
|
|
'='
|
|
|
|
,'LIKE'
|
|
|
|
,'AND'
|
|
|
|
,'OR'
|
|
|
|
,'REGEXP'
|
|
|
|
];
|
|
|
|
|
2022-05-28 19:27:36 +00:00
|
|
|
Operation.extend({
|
2016-09-26 09:28:47 +00:00
|
|
|
Type: Type
|
|
|
|
,Operators: Operators
|
2015-01-23 13:09:30 +00:00
|
|
|
});
|
|
|
|
|
2022-05-28 19:27:36 +00:00
|
|
|
Operation.implement({
|
2016-09-26 09:28:47 +00:00
|
|
|
Extends: Expr
|
2015-01-23 13:09:30 +00:00
|
|
|
,Tag: 'sql-operation'
|
2022-05-28 19:27:36 +00:00
|
|
|
,Properties: {
|
|
|
|
type: {
|
2016-09-26 09:28:47 +00:00
|
|
|
enumType: Type
|
2015-01-23 13:09:30 +00:00
|
|
|
,value: -1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-28 19:27:36 +00:00
|
|
|
,initialize: function(props) {
|
|
|
|
this.parent(props);
|
|
|
|
this.link({exprs: new Sql.List()}, {'changed': this.onListChange});
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
2015-11-09 08:14:33 +00:00
|
|
|
|
2022-05-28 19:27:36 +00:00
|
|
|
,appendChild: function(child) {
|
|
|
|
this.exprs.add(child);
|
2015-11-09 08:14:33 +00:00
|
|
|
}
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-05-28 19:27:36 +00:00
|
|
|
,onListChange: function() {
|
|
|
|
this.signalEmit('changed');
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-28 19:27:36 +00:00
|
|
|
,isReady: function() {
|
|
|
|
return this.exprs.isReady();
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
2022-05-28 19:27:36 +00:00
|
|
|
,render: function(batch) {
|
2015-01-23 13:09:30 +00:00
|
|
|
var sql = '(';
|
2016-09-26 09:28:47 +00:00
|
|
|
var operator = ' '+ Operators[this.type] +' ';
|
2022-05-28 19:27:36 +00:00
|
|
|
var e = this.exprs.getArray();
|
2015-01-23 13:09:30 +00:00
|
|
|
|
2022-05-28 19:27:36 +00:00
|
|
|
for (var i = 0; i < e.length; i++) {
|
2015-01-23 13:09:30 +00:00
|
|
|
if (i > 0)
|
|
|
|
sql += operator;
|
|
|
|
|
2022-05-28 19:27:36 +00:00
|
|
|
sql += e[i].render(batch);
|
2015-01-23 13:09:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sql += ')';
|
|
|
|
|
|
|
|
return sql;
|
|
|
|
}
|
|
|
|
});
|
2016-09-26 09:28:47 +00:00
|
|
|
|