Elgg  Version 3.0
ComparisonClause.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Database\Clauses;
4 
6 use Elgg\Values;
7 
11 class ComparisonClause implements Clause {
12 
16  public $x;
17 
21  public $comparison;
22 
26  public $y;
27 
31  public $type;
32 
37 
47  public function __construct($x, $comparison, $y = null, $type = null, $case_sensitive = null) {
48  $this->x = $x;
49  $this->comparison = $comparison;
50  $this->y = $y;
51  $this->type = $type;
52  $this->case_sensitive = $case_sensitive;
53  }
54 
60  public function prepare(QueryBuilder $qb, $table_alias = null) {
61  $x = $this->x;
62  $y = $this->y;
65 
66  switch ($type) {
70  break;
71 
72  case ELGG_VALUE_GUID :
75  break;
76 
77  case ELGG_VALUE_ID :
80  break;
81  }
82 
83  if (is_array($y) && count($y) === 1) {
84  $y = array_shift($y);
85  }
86 
87  $match_expr = null;
88 
89  $comparison = strtolower($this->comparison);
90 
91 
92  $compare_with = function ($func, $boolean = 'OR') use ($x, $y, $type, $case_sensitive, $qb) {
93  if (!isset($y)) {
94  return;
95  }
96 
97  $parts = [];
98  foreach ((array) $y as $val) {
99  $val = $qb->param($val, $type);
101  $val = "BINARY $val";
102  }
103  $parts[] = $qb->expr()->$func($x, $val);
104  }
105 
106  return $qb->merge($parts, $boolean);
107  };
108 
109  switch (strtolower($this->comparison)) {
110  case '=' :
111  case 'eq' :
112  case 'in' :
113  if ($this->case_sensitive && $this->type == ELGG_VALUE_STRING) {
114  $x = "CAST($x as BINARY)";
115  }
116  if (is_array($y) || $comparison === 'in') {
117  if (!empty($y)) {
118  $param = isset($type) ? $qb->param($y, $type) : $y;
119  $match_expr = $qb->expr()->in($x, $param);
120  }
121  } else if (isset($y)) {
122  $param = isset($type) ? $qb->param($y, $type) : $y;
123  $match_expr = $qb->expr()->eq($x, $param);
124  }
125 
126  return $match_expr;
127 
128  case '!=' :
129  case '<>' :
130  case 'neq' :
131  case 'not in' :
132  if ($this->case_sensitive && $this->type == ELGG_VALUE_STRING) {
133  $x = "CAST($x as BINARY)";
134  }
135  if (is_array($y) || $comparison === 'not in') {
136  if (!empty($y)) {
137  $param = isset($type) ? $qb->param($y, $type) : $y;
138  $match_expr = $qb->expr()->notIn($x, $param);
139  }
140  } else if (isset($y)) {
141  $param = isset($type) ? $qb->param($y, $type) : $y;
142  $match_expr = $qb->expr()->neq($x, $param);
143  }
144 
145  return $match_expr;
146 
147  case 'like' :
148  return $compare_with('like');
149 
150  case 'not like' :
151  return $compare_with('notLike', 'AND');
152 
153  case '>':
154  case 'gt' :
155  return $compare_with('gt');
156 
157  case '<' :
158  case 'lt' :
159  return $compare_with('lt');
160 
161  case '>=' :
162  case 'gte' :
163  return $compare_with('gte');
164 
165  case '<=' :
166  case 'lte' :
167  return $compare_with('lte');
168 
169  case 'is null' :
170  return $qb->expr()->isNull($x);
171 
172  case 'is not null' :
173  return $qb->expr()->isNotNull($x);
174 
175  case 'exists' :
176  return "EXISTS ($y)";
177 
178  case 'not exists' :
179  return "NOT EXISTS ($y)";
180 
181  default :
182  throw new \InvalidParameterException("'{$this->comparison}' is not a supported comparison operator");
183  }
184  }
185 }
Utility class for building composite comparison expression.
static normalizeGuids(...$args)
Flatten an array of data into an array of GUIDs.
Definition: Values.php:139
const ELGG_VALUE_INTEGER
Value types.
Definition: constants.php:138
const ELGG_VALUE_GUID
Definition: constants.php:140
Database abstraction query builder.
const ELGG_VALUE_ID
Definition: constants.php:141
__construct($x, $comparison, $y=null, $type=null, $case_sensitive=null)
Constructor.
static normalizeTimestamp($time)
Returns timestamp value of the time representation.
Definition: Values.php:64
static normalizeIds(...$args)
Prepare IDs.
Definition: Values.php:103
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...
const ELGG_VALUE_TIMESTAMP
Definition: constants.php:142
prepare(QueryBuilder $qb, $table_alias=null)
{Build an expression and/or apply it to an instance of query builder.Query builder Table aliasComposi...
Interface that allows resolving statements and/or extending query builder.
Definition: Clause.php:11
merge($parts=null, $boolean= 'AND')
Merges multiple composite expressions with a boolean.
const ELGG_VALUE_STRING
Definition: constants.php:139