|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\orm\parser; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\utils\base\UArray; |
|
6
|
|
|
use Ubiquity\orm\OrmUtils; |
|
7
|
|
|
use Ubiquity\db\SqlUtils; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Represents a query condition. |
|
11
|
|
|
* |
|
12
|
|
|
* Ubiquity\orm\parser$ConditionParser |
|
13
|
|
|
* This class is part of Ubiquity |
|
14
|
|
|
* |
|
15
|
|
|
* @author jcheron <[email protected]> |
|
16
|
58 |
|
* @version 1.0.1 |
|
17
|
58 |
|
* |
|
18
|
58 |
|
*/ |
|
19
|
58 |
|
class ConditionParser { |
|
20
|
6 |
|
private $firstPart; |
|
21
|
|
|
private $condition; |
|
22
|
58 |
|
private $parts = [ ]; |
|
23
|
|
|
private $params; |
|
24
|
34 |
|
private $invertedParams = true; |
|
25
|
34 |
|
|
|
26
|
27 |
|
public function __construct($condition = null, $firstPart = null, $params = null) { |
|
27
|
|
|
$this->condition = $condition; |
|
28
|
11 |
|
$this->firstPart = $firstPart; |
|
29
|
7 |
|
if (is_array ( $params )) { |
|
30
|
7 |
|
$this->setParams ( $params ); |
|
31
|
7 |
|
} |
|
32
|
7 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function addKeyValues($keyValues, $classname, $separator = " AND ") { |
|
35
|
|
|
if (! is_array ( $keyValues )) { |
|
36
|
11 |
|
$this->condition = $this->parseKey ( $keyValues, $classname ); |
|
37
|
11 |
|
} else { |
|
38
|
11 |
|
if (! UArray::isAssociative ( $keyValues )) { |
|
39
|
11 |
|
if (isset ( $classname )) { |
|
40
|
|
|
$keys = OrmUtils::getKeyFields ( $classname ); |
|
41
|
|
|
if (is_array ( $keys )) { |
|
42
|
11 |
|
$keyValues = \array_combine ( $keys, $keyValues ); |
|
43
|
|
|
} |
|
44
|
34 |
|
} |
|
45
|
|
|
} |
|
46
|
49 |
|
$retArray = array (); |
|
47
|
49 |
|
foreach ( $keyValues as $key => $value ) { |
|
48
|
49 |
|
if ($this->addParams ( $value )) { |
|
49
|
|
|
$retArray [] = SqlUtils::$quote . $key . SqlUtils::$quote . " = ?"; |
|
50
|
13 |
|
} |
|
51
|
|
|
} |
|
52
|
|
|
$this->condition = implode ( $separator, $retArray ); |
|
53
|
38 |
|
} |
|
54
|
38 |
|
} |
|
55
|
38 |
|
|
|
56
|
38 |
|
public function setKeyValues($values) { |
|
57
|
|
|
if (! is_array ( $values )) { |
|
58
|
|
|
$this->params = [ $values => true ]; |
|
59
|
|
|
} else { |
|
60
|
|
|
$this->params = [ ]; |
|
61
|
23 |
|
foreach ( $values as $val ) { |
|
62
|
23 |
|
$this->addParams ( $val ); |
|
63
|
23 |
|
} |
|
64
|
23 |
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
23 |
|
private function addParams($value) { |
|
68
|
|
|
if (! isset ( $this->params [$value] )) { |
|
69
|
41 |
|
return $this->params [$value] = true; |
|
70
|
41 |
|
} |
|
71
|
23 |
|
return false; |
|
72
|
23 |
|
} |
|
73
|
23 |
|
|
|
74
|
23 |
|
public function addPart($condition, $value) { |
|
75
|
23 |
|
if ($this->addParams ( $value )) { |
|
76
|
|
|
$this->parts [] = $condition; |
|
77
|
23 |
|
return true; |
|
78
|
|
|
} |
|
79
|
36 |
|
return false; |
|
80
|
|
|
} |
|
81
|
41 |
|
|
|
82
|
|
|
public function addParts($condition, $values) { |
|
83
|
23 |
|
foreach ( $values as $value ) { |
|
84
|
23 |
|
if ($this->addParams ( $value )) { |
|
85
|
23 |
|
$this->parts [] = $condition; |
|
86
|
23 |
|
} |
|
87
|
23 |
|
} |
|
88
|
|
|
} |
|
89
|
23 |
|
|
|
90
|
|
|
public function compileParts($separator = " OR ") { |
|
91
|
|
|
if ($separator == " OR " && sizeof ( $this->parts ) > 3) { |
|
92
|
27 |
|
$parts = $this->refactorParts (); |
|
93
|
27 |
|
$conditions = [ ]; |
|
94
|
27 |
|
foreach ( $parts as $part => $values ) { |
|
95
|
14 |
|
$values [0] = "SELECT ? as _id"; |
|
96
|
14 |
|
$conditions [] = " INNER JOIN (" . implode ( " UNION ALL SELECT ", $values ) . ") as _tmp ON " . $part . "=_tmp._id"; |
|
97
|
|
|
} |
|
98
|
|
|
$this->condition = implode ( " ", $conditions ); |
|
99
|
27 |
|
} else { |
|
100
|
|
|
$this->condition = implode ( $separator, $this->parts ); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
private function refactorParts() { |
|
105
|
58 |
|
$result = [ ]; |
|
106
|
58 |
|
foreach ( $this->parts as $part ) { |
|
107
|
58 |
|
$part = str_replace ( "= ?", "", $part ); |
|
108
|
3 |
|
$result [$part] [] = '?'; |
|
109
|
3 |
|
} |
|
110
|
3 |
|
return $result; |
|
111
|
|
|
} |
|
112
|
3 |
|
|
|
113
|
|
|
private function parseKey($keyValues, $className) { |
|
114
|
|
|
$condition = $keyValues; |
|
115
|
|
|
if (strrpos ( $keyValues, "=" ) === false && strrpos ( $keyValues, ">" ) === false && strrpos ( $keyValues, "<" ) === false) { |
|
116
|
|
|
if ($this->addParams ( $keyValues )) { |
|
117
|
|
|
$condition = SqlUtils::$quote . OrmUtils::getFirstKey ( $className ) . SqlUtils::$quote . "= ?"; |
|
118
|
58 |
|
} |
|
119
|
58 |
|
} |
|
120
|
53 |
|
return $condition; |
|
121
|
49 |
|
} |
|
122
|
|
|
|
|
123
|
7 |
|
/** |
|
124
|
|
|
* |
|
125
|
36 |
|
* @return string |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getCondition() { |
|
128
|
|
|
if ($this->firstPart == null) |
|
129
|
|
|
return $this->condition; |
|
130
|
|
|
$ret = $this->firstPart; |
|
131
|
38 |
|
if (isset ( $this->condition )) { |
|
132
|
38 |
|
$ret .= " WHERE " . $this->condition; |
|
133
|
19 |
|
} |
|
134
|
19 |
|
return $ret; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
38 |
|
* |
|
139
|
|
|
* @return mixed |
|
140
|
|
|
*/ |
|
141
|
38 |
|
public function getParams() { |
|
142
|
38 |
|
if (is_array ( $this->params )) { |
|
143
|
19 |
|
if ($this->invertedParams) { |
|
144
|
38 |
|
return array_keys ( $this->params ); |
|
145
|
|
|
} |
|
146
|
|
|
return $this->params; |
|
147
|
|
|
} |
|
148
|
|
|
return; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* |
|
153
|
|
|
* @return mixed |
|
154
|
|
|
*/ |
|
155
|
|
|
public function hasParam($value) { |
|
156
|
|
|
if (is_array ( $this->params )) { |
|
157
|
|
|
if ($this->invertedParams) { |
|
158
|
7 |
|
return isset ( $this->params [$value] ); |
|
159
|
7 |
|
} |
|
160
|
7 |
|
return array_search ( $value, $this->params ) !== false; |
|
161
|
7 |
|
} |
|
162
|
|
|
return false; |
|
163
|
|
|
} |
|
164
|
35 |
|
|
|
165
|
35 |
|
public function countParts() { |
|
166
|
35 |
|
if (is_array ( $this->params )) |
|
167
|
35 |
|
return sizeof ( $this->params ); |
|
168
|
|
|
return 0; |
|
169
|
35 |
|
} |
|
170
|
35 |
|
|
|
171
|
|
|
/** |
|
172
|
11 |
|
* |
|
173
|
11 |
|
* @param string $condition |
|
174
|
11 |
|
*/ |
|
175
|
11 |
|
public function setCondition($condition) { |
|
176
|
|
|
$this->condition = $condition; |
|
177
|
|
|
return $this; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* |
|
182
|
|
|
* @param mixed $params |
|
183
|
|
|
*/ |
|
184
|
|
|
public function setParams($params) { |
|
185
|
|
|
$this->params = $params; |
|
186
|
|
|
$this->invertedParams = false; |
|
187
|
|
|
return $this; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
public function limitOne() { |
|
191
|
|
|
$limit = ""; |
|
192
|
|
|
if (\stripos ( $this->condition, " limit " ) === false) { |
|
193
|
|
|
$limit = " limit 1"; |
|
194
|
|
|
} |
|
195
|
|
|
$this->condition .= $limit; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
public static function simple($condition, $params) { |
|
199
|
|
|
$cParser = new ConditionParser ( $condition ); |
|
200
|
|
|
$cParser->addParams ( $params ); |
|
201
|
|
|
return $cParser; |
|
202
|
|
|
} |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
|