3 * Visitor to convert a livestatus filter to a search query
5 class LSFilterQueryBuilderVisitor
implements LivestatusFilterVisitor
{
7 * Visit an and node, to build the subquery
9 public function visit_and( LivestatusFilterAnd
$filt, $prio ) {
10 $subfilters = $filt->get_sub_filters();
11 $subqueries = array();
13 if( count( $subfilters ) == 1) {
14 return $subfilters[0]->visit($this, $prio);
15 } else if( count( $subfilters ) == 0 ) {
19 foreach( $subfilters as $sub_filt ) {
20 $subqueries[] = $sub_filt->visit($this, 2);
22 $query = implode(' and ', $subqueries);
24 if( $prio > 2 ) $query = "($query)";
29 * Visit an or node, to build the subquery
31 public function visit_or( LivestatusFilterOr
$filt, $prio ) {
32 $subfilters = $filt->get_sub_filters();
33 $subqueries = array();
35 if( count( $subfilters ) == 1) {
36 return $subfilters[0]->visit($this, $prio);
37 } else if( count( $subfilters ) == 0 ) {
41 foreach( $subfilters as $sub_filt ) {
42 $subqueries[] = $sub_filt->visit($this, 2);
44 $query = implode(' or ', $subqueries);
46 if( $prio > 1 ) $query = "($query)";
51 * Visit an match node, to build the subquery
53 public function visit_match( LivestatusFilterMatch
$filt, $prio ) {
54 $op = $filt->get_op();
55 $field = $filt->get_field();
56 $value = $filt->get_value();
58 if( !is_numeric($value) ) {
59 $value = '"'.addslashes($value).'"';
62 /* Special case for groups */
63 $field_parts = explode('.', $field);
64 if( end($field_parts) == 'groups' ) {
66 array_pop($field_parts);
67 $field = implode('.', $field_parts);
72 return $field.$op.$value;
77 * Visit an not node, to build the subquery
79 public function visit_not( LivestatusFilterNot
$filt, $prio ) {
80 $query = 'not '.$filt->get_filter()->visit($this, 3);
81 if( $prio >= 3 ) $query = "($query)";