FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
database/src/Condition.php at master · selective-php/database · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Nov 26, 2022. It is now read-only.
selective-php
/
database
Public archive
Notifications
You must be signed in to change notification settings
Fork
4
Star
23
Code
Pull requests
0
Actions
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
database
/
src
/
Condition.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
291 lines (260 loc) · 8.19 KB
Breadcrumbs
database
/
src
/
Condition.php
Copy path
File metadata and controls
291 lines (260 loc) · 8.19 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
namespace
Selective
\
Database
;
use
Closure
;
/**
* Condition.
*/
final
class
Condition
{
/**
* @var Quoter
*/
private
Quoter
$
quoter
;
/**
* PDO Connection.
*
* @var QueryInterface
*/
private
QueryInterface
$
query
;
/**
* Where clause.
*
* @var array
*/
private
array
$
where
= [];
/**
* Having clause.
*
* @var array
*/
private
array
$
having
= [];
/**
* Constructor.
*
* @param Connection $connection The connection
* @param QueryInterface $query The query
*/
public
function
__construct
(
Connection
$
connection
,
QueryInterface
$
query
)
{
$
this
->
quoter
=
$
connection
->
getQuoter
();
$
this
->
query
=
$
query
;
}
/**
* Get sql.
*
* @param array $sql The sql
*
* @return array The sql
*/
public
function
getWhereSql
(
array
$
sql
):
array
{
return
$
this
->
getConditionSql
(
$
sql
,
$
this
->
where
,
'
WHERE
'
);
}
/**
* Get sql.
*
* @param array $sql The sql
* @param array $where The where
* @param string $conditionType The condition type
*
* @return array The sql
*/
public
function
getConditionSql
(
array
$
sql
,
array
$
where
,
string
$
conditionType
):
array
{
if
(
empty
(
$
where
)) {
return
$
sql
;
}
foreach
(
$
where
as
$
index
=>
$
item
) {
if
(
$
item
instanceof
RawExp) {
if
(
$
index
===
0
) {
$
sql
[] =
$
conditionType
.
'
'
.
$
item
->
getValue
();
continue
;
}
$
sql
[] =
$
item
->
getValue
();
continue
;
}
[
$
type
,
$
conditions
] =
$
item
;
if
(!
$
index
) {
$
whereType
=
$
conditionType
;
}
else
{
$
whereType
=
strtoupper
(
$
type
);
}
if
(
$
conditions
[
0
]
instanceof
RawExp) {
$
sql
[] =
$
whereType
.
'
'
.
$
conditions
[
0
]->
getValue
();
continue
;
}
[
$
leftField
,
$
operator
,
$
rightField
] =
$
conditions
;
$
leftField
=
$
this
->
quoter
->
quoteName
(
$
leftField
);
[
$
rightField
,
$
operator
] =
$
this
->
getRightFieldValue
(
$
rightField
,
$
operator
);
$
sql
[] =
sprintf
(
'
%s %s %s %s
'
,
$
whereType
,
$
leftField
,
$
operator
,
$
rightField
);
}
return
$
sql
;
}
/**
* Comparison Functions and Operators.
*
* https://dev.mysql.com/doc/refman/5.7/en/comparison-operators.html
*
* @param string|array $rightField The right field
* @param string|mixed $comparison The comparison
*
* @return array The value
*/
private
function
getRightFieldValue
(
$
rightField
,
$
comparison
):
array
{
if
(
$
comparison
===
'
in
'
||
$
comparison
===
'
not in
'
) {
$
rightField
=
'
(
'
.
implode
(
'
,
'
,
$
this
->
quoter
->
quoteArray
((
array
)
$
rightField
)) .
'
)
'
;
}
elseif
(
$
comparison
===
'
greatest
'
||
$
comparison
===
'
least
'
||
$
comparison
===
'
coalesce
'
||
$
comparison
===
'
interval
'
||
$
comparison
===
'
strcmp
'
) {
$
comparison
=
'
=
'
.
$
comparison
;
$
rightField
=
'
(
'
.
implode
(
'
,
'
,
$
this
->
quoter
->
quoteArray
((
array
)
$
rightField
)) .
'
)
'
;
}
elseif
(
$
comparison
===
'
=
'
&&
$
rightField
===
null
) {
$
comparison
=
'
IS
'
;
$
rightField
=
$
this
->
quoter
->
quoteValue
(
$
rightField
);
}
elseif
((
$
comparison
===
'
<>
'
||
$
comparison
===
'
!=
'
) &&
$
rightField
===
null
) {
$
comparison
=
'
IS NOT
'
;
$
rightField
=
$
this
->
quoter
->
quoteValue
(
$
rightField
);
}
elseif
(
$
comparison
===
'
between
'
||
$
comparison
===
'
not between
'
) {
/** @var array $rightField */
$
between1
=
$
this
->
quoter
->
quoteValue
(
$
rightField
[
0
]);
$
between2
=
$
this
->
quoter
->
quoteValue
(
$
rightField
[
1
]);
$
rightField
=
sprintf
(
'
%s AND %s
'
,
$
between1
,
$
between2
);
}
elseif
(
$
rightField
instanceof
RawExp) {
$
rightField
=
$
rightField
->
getValue
();
}
else
{
$
rightField
=
$
this
->
quoter
->
quoteValue
(
$
rightField
);
}
// @phpstan-ignore-next-line
return
[
$
rightField
,
strtoupper
(
$
comparison
)];
}
/**
* Get sql.
*
* @param array $sql The sql
*
* @return array The result
*/
public
function
getHavingSql
(
array
$
sql
):
array
{
return
$
this
->
getConditionSql
(
$
sql
,
$
this
->
having
,
'
HAVING
'
);
}
/**
* Where AND condition.
*
* @param array $conditions The conditions (field, comparison, value)
* or (field, comparison, new RawExp('table.field'))
* or new RawExp('...')
*
* @return self
*/
public
function
where
(
array
$
conditions
):
self
{
if
(
$
conditions
[
0
]
instanceof
Closure) {
$
this
->
addClauseCondClosure
(
'
where
'
,
'
AND
'
,
$
conditions
[
0
]);
return
$
this
;
}
$
this
->
where
[] = [
'
and
'
,
$
conditions
];
return
$
this
;
}
/**
* Adds to a clause through a closure, enclosing within parentheses.
*
* @param string $clause The clause to work with, typically 'where' or 'having'
* @param string $andor Add the condition using this operator, typically 'AND' or 'OR'
* @param callable $closure The closure that adds to the clause
*
* @return void
*/
private
function
addClauseCondClosure
(
string
$
clause
,
string
$
andor
,
callable
$
closure
):
void
{
// Retain the prior set of conditions, and temporarily reset the clause
// for the closure to work with (otherwise there will be an extraneous
// opening AND/OR keyword)
/** @var array $set */
$
set
=
$
this
->
$
clause
;
$
this
->
$
clause
= [];
// invoke the closure, which will re-populate the $this->$clause
$
closure
(
$
this
->
query
);
// are there new clause elements?
if
(
empty
(
$
this
->
$
clause
)) {
// no: restore the old ones, and done
$
this
->
$
clause
=
$
set
;
return
;
}
// Append an opening parenthesis to the prior set of conditions,
// with AND/OR as needed ...
if
(!
empty
(
$
set
)) {
$
set
[] =
new
RawExp
(
strtoupper
(
$
andor
) .
'
(
'
);
}
else
{
$
set
[] =
new
RawExp
(
'
(
'
);
}
// Append the new conditions to the set, with indenting
$
sql
= [];
$
sql
=
$
this
->
getConditionSql
(
$
sql
,
$
this
->
$
clause
,
''
);
foreach
(
$
sql
as
$
cond
) {
$
set
[] =
new
RawExp
(
$
cond
);
}
$
set
[] =
new
RawExp
(
'
)
'
);
// ... then put the full set of conditions back into $this->$clause
$
this
->
$
clause
=
$
set
;
}
/**
* Where OR condition.
*
* @param array $conditions The conditions (field, comparison, value)
* or (field, comparison, new RawExp('table.field'))
* or new RawExp('...')
*
* @return self
*/
public
function
orWhere
(
array
$
conditions
):
self
{
if
(
$
conditions
[
0
]
instanceof
Closure) {
$
this
->
addClauseCondClosure
(
'
where
'
,
'
OR
'
,
$
conditions
[
0
]);
return
$
this
;
}
$
this
->
where
[] = [
'
or
'
,
$
conditions
];
return
$
this
;
}
/**
* Add AND having condition.
*
* @param array $conditions The conditions (field, comparison, value)
* or (field, comparison, new RawExp('table.field'))
* or new RawExp('...')
*
* @return self
*/
public
function
having
(
array
$
conditions
):
self
{
if
(
$
conditions
[
0
]
instanceof
Closure) {
$
this
->
addClauseCondClosure
(
'
having
'
,
'
AND
'
,
$
conditions
[
0
]);
return
$
this
;
}
$
this
->
having
[] = [
'
and
'
,
$
conditions
];
return
$
this
;
}
/**
* Add OR having condition.
*
* @param array $conditions The conditions (field, comparison, value)
* or (field, comparison, new RawExp('table.field'))
* or new RawExp('...')
*
* @return self
*/
public
function
orHaving
(
array
$
conditions
):
self
{
if
(
$
conditions
[
0
]
instanceof
Closure) {
$
this
->
addClauseCondClosure
(
'
having
'
,
'
OR
'
,
$
conditions
[
0
]);
return
$
this
;
}
$
this
->
having
[] = [
'
or
'
,
$
conditions
];
return
$
this
;
}
}
Back
|
FazBrowse Home
|
New Git URL