Elgg
Version master
Main Page
Related Pages
Namespaces
Classes
Files
Examples
File List
File Members
engine
classes
Elgg
Database
Clauses
ComparisonClause.php
Go to the documentation of this file.
1
<?php
2
3
namespace
Elgg\Database\Clauses
;
4
5
use
Elgg\Database\QueryBuilder
;
6
use
Elgg\Exceptions\DomainException
;
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
37
public
$case_sensitive
;
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
;
64
$type
=
$this->type
;
65
$case_sensitive
=
$this->case_sensitive
;
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
);
76
if
(
$case_sensitive
&&
$type
===
ELGG_VALUE_STRING
) {
77
$val =
"BINARY {$val}"
;
78
}
79
80
$parts[] = $qb->expr()->$func(
$x
, $val);
81
}
82
83
return
$qb->
merge
($parts, $boolean);
84
};
85
86
$match_expr = null;
87
$comparison
= strtolower($this->comparison);
88
switch
(
$comparison
) {
89
case
'='
:
90
case
'eq'
:
91
case
'in'
:
92
if
($this->case_sensitive && $this->type ==
ELGG_VALUE_STRING
) {
93
$x
=
"CAST($x as BINARY)"
;
94
}
95
96
if
(is_array(
$y
) ||
$comparison
===
'in'
) {
97
if
(!
Values::isEmpty
(
$y
)) {
98
$param = isset(
$type
) ? $qb->
param
(
$y
,
$type
) :
$y
;
99
$match_expr = $qb->expr()->in(
$x
, $param);
100
}
101
}
elseif
(isset(
$y
)) {
102
$param = isset(
$type
) ? $qb->
param
(
$y
,
$type
) :
$y
;
103
$match_expr = $qb->expr()->eq(
$x
, $param);
104
}
105
return
$match_expr;
106
107
case
'!='
:
108
case
'<>'
:
109
case
'neq'
:
110
case
'not in'
:
111
if
($this->case_sensitive && $this->type ==
ELGG_VALUE_STRING
) {
112
$x
=
"CAST($x as BINARY)"
;
113
}
114
115
if
(is_array(
$y
) ||
$comparison
===
'not in'
) {
116
if
(!empty(
$y
)) {
117
$param = isset(
$type
) ? $qb->
param
(
$y
,
$type
) :
$y
;
118
$match_expr = $qb->expr()->notIn(
$x
, $param);
119
}
120
}
elseif
(isset(
$y
)) {
121
$param = isset(
$type
) ? $qb->
param
(
$y
,
$type
) :
$y
;
122
$match_expr = $qb->expr()->neq(
$x
, $param);
123
}
124
return
$match_expr;
125
126
case
'like'
:
127
return
$compare_with(
'like'
);
128
129
case
'not like'
:
130
return
$compare_with(
'notLike'
,
'AND'
);
131
132
case
'>'
:
133
case
'gt'
:
134
return
$compare_with(
'gt'
);
135
136
case
'<'
:
137
case
'lt'
:
138
return
$compare_with(
'lt'
);
139
140
case
'>='
:
141
case
'gte'
:
142
return
$compare_with(
'gte'
);
143
144
case
'<='
:
145
case
'lte'
:
146
return
$compare_with(
'lte'
);
147
148
case
'is null'
:
149
return
$qb->expr()->isNull(
$x
);
150
151
case
'is not null'
:
152
return
$qb->expr()->isNotNull(
$x
);
153
154
case
'exists'
:
155
return
"EXISTS ($y)"
;
156
157
case
'not exists'
:
158
return
"NOT EXISTS ($y)"
;
159
160
default
:
161
throw
new
DomainException
(
"'{$this->comparison}' is not a supported comparison operator"
);
162
}
163
}
164
}
Elgg\Database\Clauses\ComparisonClause
Utility class for building composite comparison expression.
Definition:
ComparisonClause.php:12
Values
Saves user notification settings.
Elgg\Database\Clauses\Clause
Interface that allows resolving statements and/or extending query builder.
Definition:
Clause.php:14
QueryBuilder
Elgg\Exceptions\DomainException
Exception thrown if a value does not adhere to a defined valid data domain.
Definition:
DomainException.php:11
Elgg\Database\QueryBuilder
Database abstraction query builder.
Definition:
QueryBuilder.php:18
Elgg\Values\isEmpty
static isEmpty($value)
Check if a value isn't empty, but allow 0 and '0'.
Definition:
Values.php:192
Elgg\Database\Clauses\ComparisonClause\$case_sensitive
$case_sensitive
Definition:
ComparisonClause.php:37
elseif
if($item instanceof\ElggEntity) elseif($item instanceof\ElggRiverItem) elseif($item instanceof\ElggRelationship) elseif(is_callable([$item, 'getType']))
Definition:
item.php:48
Elgg\Database\Clauses\ComparisonClause\$type
$type
Definition:
ComparisonClause.php:32
Elgg\Database\Clauses\ComparisonClause\$comparison
$comparison
Definition:
ComparisonClause.php:22
Elgg\Database\Clauses\ComparisonClause\__construct
__construct($x, $comparison, $y=null, $type=null, $case_sensitive=null)
Constructor.
Definition:
ComparisonClause.php:48
DomainException
Elgg\Database\QueryBuilder\param
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...
Definition:
QueryBuilder.php:125
Elgg\Database\Clauses\ComparisonClause\prepare
prepare(QueryBuilder $qb, $table_alias=null)
{}
Definition:
ComparisonClause.php:61
Elgg\Database\Clauses\ComparisonClause\$x
$x
Definition:
ComparisonClause.php:17
Elgg\Database\QueryBuilder\merge
merge($parts=null, $boolean= 'AND')
Merges multiple composite expressions with a boolean.
Definition:
QueryBuilder.php:285
ELGG_VALUE_STRING
const ELGG_VALUE_STRING
Definition:
constants.php:112
Elgg\Database\Clauses
Definition:
AccessWhereClause.php:3
Elgg\Database\Clauses\ComparisonClause\$y
$y
Definition:
ComparisonClause.php:27
$qb
$qb
Definition:
queue.php:11
Generated on Sat Dec 2 2023 00:01:57 for Elgg by
1.8.11