1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Platine Database |
5
|
|
|
* |
6
|
|
|
* Platine Database is the abstraction layer using PDO with support of query and schema builder |
7
|
|
|
* |
8
|
|
|
* This content is released under the MIT License (MIT) |
9
|
|
|
* |
10
|
|
|
* Copyright (c) 2020 Platine Database |
11
|
|
|
* |
12
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
13
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
14
|
|
|
* in the Software without restriction, including without limitation the rights |
15
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
16
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
17
|
|
|
* furnished to do so, subject to the following conditions: |
18
|
|
|
* |
19
|
|
|
* The above copyright notice and this permission notice shall be included in all |
20
|
|
|
* copies or substantial portions of the Software. |
21
|
|
|
* |
22
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
23
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
24
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
25
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
26
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
27
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
28
|
|
|
* SOFTWARE. |
29
|
|
|
*/ |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @file WhereStatement.php |
33
|
|
|
* |
34
|
|
|
* The class for where query statement |
35
|
|
|
* |
36
|
|
|
* @package Platine\Database\Query |
37
|
|
|
* @author Platine Developers Team |
38
|
|
|
* @copyright Copyright (c) 2020 |
39
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
40
|
|
|
* @link https://www.platine-php.com |
41
|
|
|
* @version 1.0.0 |
42
|
|
|
* @filesource |
43
|
|
|
*/ |
44
|
|
|
|
45
|
|
|
declare(strict_types=1); |
46
|
|
|
|
47
|
|
|
namespace Platine\Database\Query; |
48
|
|
|
|
49
|
|
|
use Closure; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @class WhereStatement |
53
|
|
|
* @package Platine\Database\Query |
54
|
|
|
*/ |
55
|
|
|
class WhereStatement |
56
|
|
|
{ |
57
|
|
|
/** |
58
|
|
|
* The Query statement instance |
59
|
|
|
* @var QueryStatement |
60
|
|
|
*/ |
61
|
|
|
protected QueryStatement $queryStatement; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* The Where instance |
65
|
|
|
* @var Where |
66
|
|
|
*/ |
67
|
|
|
protected Where $where; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* WhereStatement constructor. |
71
|
|
|
* @param QueryStatement|null $queryStatement |
72
|
|
|
*/ |
73
|
|
|
public function __construct(?QueryStatement $queryStatement = null) |
74
|
|
|
{ |
75
|
|
|
$this->queryStatement = $queryStatement ?? new QueryStatement(); |
76
|
|
|
$this->where = new Where($this, $this->queryStatement); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return QueryStatement |
81
|
|
|
*/ |
82
|
|
|
public function getQueryStatement(): QueryStatement |
83
|
|
|
{ |
84
|
|
|
return $this->queryStatement; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string|Expression|Closure $column |
89
|
|
|
* @param bool $isExpression |
90
|
|
|
* |
91
|
|
|
* @return WhereStatement|Where|Select|Delete|Update |
92
|
|
|
*/ |
93
|
|
|
public function where( |
94
|
|
|
string|Expression|Closure $column, |
95
|
|
|
bool $isExpression = false |
96
|
|
|
): WhereStatement|Where|Select|Delete|Update { |
97
|
|
|
return $this->addWhereCondition($column, 'AND', $isExpression); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string|Expression|Closure $column |
102
|
|
|
* @param bool $isExpression |
103
|
|
|
* |
104
|
|
|
* @return WhereStatement|Where|Select|Delete|Update |
105
|
|
|
*/ |
106
|
|
|
public function orWhere( |
107
|
|
|
string|Expression|Closure $column, |
108
|
|
|
bool $isExpression = false |
109
|
|
|
): WhereStatement|Where|Select|Delete|Update { |
110
|
|
|
return $this->addWhereCondition($column, 'OR', $isExpression); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param Closure $select |
115
|
|
|
* @return WhereStatement|Select|Delete|Update |
116
|
|
|
*/ |
117
|
|
|
public function whereExists(Closure $select): WhereStatement|Select|Delete|Update |
118
|
|
|
{ |
119
|
|
|
return $this->addWhereExistsCondition($select, 'AND', false); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param Closure $select |
124
|
|
|
* @return WhereStatement|Select|Delete|Update |
125
|
|
|
*/ |
126
|
|
|
public function orWhereExists(Closure $select): WhereStatement|Select|Delete|Update |
127
|
|
|
{ |
128
|
|
|
return $this->addWhereExistsCondition($select, 'OR', false); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param Closure $select |
133
|
|
|
* @return WhereStatement|Select|Delete|Update |
134
|
|
|
*/ |
135
|
|
|
public function whereNotExists(Closure $select): WhereStatement|Select|Delete|Update |
136
|
|
|
{ |
137
|
|
|
return $this->addWhereExistsCondition($select, 'AND', true); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param Closure $select |
142
|
|
|
* @return WhereStatement|Select|Delete|Update |
143
|
|
|
*/ |
144
|
|
|
public function orWhereNotExists(Closure $select): WhereStatement|Select|Delete|Update |
145
|
|
|
{ |
146
|
|
|
return $this->addWhereExistsCondition($select, 'OR', true); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @inheritDoc |
151
|
|
|
*/ |
152
|
|
|
public function __clone(): void |
153
|
|
|
{ |
154
|
|
|
$this->queryStatement = clone $this->queryStatement; |
155
|
|
|
$this->where = new Where($this, $this->queryStatement); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param mixed $column |
160
|
|
|
* @param string $separator |
161
|
|
|
* @param bool $isExpression |
162
|
|
|
* |
163
|
|
|
* @return WhereStatement|Where |
164
|
|
|
*/ |
165
|
|
|
protected function addWhereCondition( |
166
|
|
|
mixed $column, |
167
|
|
|
string $separator = 'AND', |
168
|
|
|
bool $isExpression = false |
169
|
|
|
): WhereStatement|Where { |
170
|
|
|
if (($column instanceof Closure) && $isExpression === false) { |
171
|
|
|
$this->queryStatement->addWhereGroup($column, $separator); |
172
|
|
|
|
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
return $this->where->init($column, $separator); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* @param Closure $select |
182
|
|
|
* @param string $separator |
183
|
|
|
* @param bool $not |
184
|
|
|
* @return $this |
185
|
|
|
*/ |
186
|
|
|
protected function addWhereExistsCondition( |
187
|
|
|
Closure $select, |
188
|
|
|
string $separator = 'AND', |
189
|
|
|
bool $not = false |
190
|
|
|
): self { |
191
|
|
|
$this->queryStatement->addWhereExists($select, $separator, $not); |
192
|
|
|
|
193
|
|
|
return $this; |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|