Elgg  Version 4.3
ComparisonClause.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database\Clauses;
4 
7 use Elgg\Values;
8 
12 class ComparisonClause extends Clause {
13 
17  public $x;
18 
22  public $comparison;
23 
27  public $y;
28 
32  public $type;
33 
38 
48  public function __construct($x, $comparison, $y = null, $type = null, $case_sensitive = null) {
49  $this->x = $x;
50  $this->comparison = $comparison;
51  $this->y = $y;
52  $this->type = $type;
53  $this->case_sensitive = $case_sensitive;
54  }
55 
61  public function prepare(QueryBuilder $qb, $table_alias = null) {
62  $x = $this->x;
63  $y = $this->y;
66 
67  $compare_with = function ($func, $boolean = 'OR') use ($x, $y, $type, $case_sensitive, $qb) {
68  if (!isset($y)) {
69  return;
70  }
71 
72  $y = is_array($y) ? $y : [$y];
73  $parts = [];
74  foreach ($y as $val) {
75  $val = $qb->param($val, $type);
77  $val = "BINARY $val";
78  }
79  $parts[] = $qb->expr()->$func($x, $val);
80  }
81 
82  return $qb->merge($parts, $boolean);
83  };
84 
85  $match_expr = null;
86  $comparison = strtolower($this->comparison);
87  switch ($comparison) {
88  case '=' :
89  case 'eq' :
90  case 'in' :
91  if ($this->case_sensitive && $this->type == ELGG_VALUE_STRING) {
92  $x = "CAST($x as BINARY)";
93  }
94  if (is_array($y) || $comparison === 'in') {
95  if (!Values::isEmpty($y)) {
96  $param = isset($type) ? $qb->param($y, $type) : $y;
97  $match_expr = $qb->expr()->in($x, $param);
98  }
99  } else if (isset($y)) {
100  $param = isset($type) ? $qb->param($y, $type) : $y;
101  $match_expr = $qb->expr()->eq($x, $param);
102  }
103 
104  return $match_expr;
105 
106  case '!=' :
107  case '<>' :
108  case 'neq' :
109  case 'not in' :
110  if ($this->case_sensitive && $this->type == ELGG_VALUE_STRING) {
111  $x = "CAST($x as BINARY)";
112  }
113  if (is_array($y) || $comparison === 'not in') {
114  if (!empty($y)) {
115  $param = isset($type) ? $qb->param($y, $type) : $y;
116  $match_expr = $qb->expr()->notIn($x, $param);
117  }
118  } else if (isset($y)) {
119  $param = isset($type) ? $qb->param($y, $type) : $y;
120  $match_expr = $qb->expr()->neq($x, $param);
121  }
122 
123  return $match_expr;
124 
125  case 'like' :
126  return $compare_with('like');
127 
128  case 'not like' :
129  return $compare_with('notLike', 'AND');
130 
131  case '>':
132  case 'gt' :
133  return $compare_with('gt');
134 
135  case '<' :
136  case 'lt' :
137  return $compare_with('lt');
138 
139  case '>=' :
140  case 'gte' :
141  return $compare_with('gte');
142 
143  case '<=' :
144  case 'lte' :
145  return $compare_with('lte');
146 
147  case 'is null' :
148  return $qb->expr()->isNull($x);
149 
150  case 'is not null' :
151  return $qb->expr()->isNotNull($x);
152 
153  case 'exists' :
154  return "EXISTS ($y)";
155 
156  case 'not exists' :
157  return "NOT EXISTS ($y)";
158 
159  default :
160  throw new InvalidParameterException("'{$this->comparison}' is not a supported comparison operator");
161  }
162  }
163 }
Utility class for building composite comparison expression.
Saves user notification settings.
Interface that allows resolving statements and/or extending query builder.
Definition: Clause.php:14
Database abstraction query builder.
static isEmpty($value)
Check if a value isn&#39;t empty, but allow 0 and &#39;0&#39;.
Definition: Values.php:191
__construct($x, $comparison, $y=null, $type=null, $case_sensitive=null)
Constructor.
param($value, $type=null, $key=null)
Sets a new parameter assigning it a unique parameter key/name if none provided Returns the name of th...
prepare(QueryBuilder $qb, $table_alias=null)
{}
merge($parts=null, $boolean= 'AND')
Merges multiple composite expressions with a boolean.
const ELGG_VALUE_STRING
Definition: constants.php:127
$qb
Definition: queue.php:11