| @@ 178-256 (lines=79) @@ | ||
| 175 | * @param string $operation |
|
| 176 | * @param string $concatenation |
|
| 177 | */ |
|
| 178 | public function buildWhere($where = '', $value = '', $operation = '=', $concatenation = 'AND') { |
|
| 179 | if (!is_array($where)) { |
|
| 180 | if (empty($operation)) { |
|
| 181 | $operation = '='; |
|
| 182 | } |
|
| 183 | ||
| 184 | if ($concatenation === false) { |
|
| 185 | $concatenation = 'AND'; |
|
| 186 | } elseif ($concatenation === true) { |
|
| 187 | $concatenation = ''; |
|
| 188 | } |
|
| 189 | ||
| 190 | if ($this->whereString == '') { |
|
| 191 | $this->whereString = ' WHERE '; |
|
| 192 | } |
|
| 193 | ||
| 194 | if (stristr($operation, 'IN') || stristr($operation, 'NOT IN')) { |
|
| 195 | if (is_array($value)) { |
|
| 196 | $newValue = ''; |
|
| 197 | foreach ($value as $item) { |
|
| 198 | if ($newValue) { |
|
| 199 | $newValue .= ','; |
|
| 200 | } |
|
| 201 | if (is_string($item)) { |
|
| 202 | $newValue .= '"' . $item . '"'; |
|
| 203 | } else { |
|
| 204 | $newValue .= $item; |
|
| 205 | } |
|
| 206 | } |
|
| 207 | $value = '(' . $newValue . ')'; |
|
| 208 | } elseif (!preg_match('!\(!', $value) && !preg_match('![^0-9,\.\(\) ]!', $value)) { |
|
| 209 | $value = "({$value})"; |
|
| 210 | } elseif (preg_match('!\(!', $value) && preg_match('![^0-9,\.\(\) ]!', $value)) { |
|
| 211 | $value = "\"{$value}\""; |
|
| 212 | } |
|
| 213 | } elseif (!in_array($value, array('CURRENT_TIMESTAMP', 'NULL'))) { |
|
| 214 | $this->params[] = $value; |
|
| 215 | $value = "?"; |
|
| 216 | } |
|
| 217 | ||
| 218 | if (substr($this->whereString, -1, 1) == '(' || substr($this->whereString, -2, 2) == 'E ') { |
|
| 219 | $this->whereString .= " {$where} {$operation} {$value} "; |
|
| 220 | } else { |
|
| 221 | $this->whereString .= "{$concatenation} {$where} {$operation} {$value} "; |
|
| 222 | } |
|
| 223 | } else { |
|
| 224 | $i = -1; |
|
| 225 | while (isset($where[++$i])) { |
|
| 226 | $item = $where[$i]; |
|
| 227 | if (isset($where[$i + 1]) && !isset($where[$i - 1]) && is_array($where[$i])) { |
|
| 228 | if ($this->whereString != null && substr($this->whereString, -1, 1) != '(' && $this->whereString != 'WHERE ') { |
|
| 229 | if (!isset($item[3])) { |
|
| 230 | $concatenation = 'AND'; |
|
| 231 | } else { |
|
| 232 | $concatenation = $item[3]; |
|
| 233 | } |
|
| 234 | ||
| 235 | $this->whereString .= "{$concatenation} "; |
|
| 236 | } |
|
| 237 | ||
| 238 | if ($this->whereString != '') { |
|
| 239 | $this->whereString .= '('; |
|
| 240 | } else { |
|
| 241 | $this->whereString = 'WHERE ('; |
|
| 242 | } |
|
| 243 | } |
|
| 244 | ||
| 245 | if (!is_array($item)) { |
|
| 246 | call_user_func_array(array($this, 'buildWhere'), $where); |
|
| 247 | break; |
|
| 248 | } else { |
|
| 249 | $this->buildWhere($item); |
|
| 250 | } |
|
| 251 | if (!isset($where[$i + 1]) && isset($where[$i - 1])) { |
|
| 252 | $this->whereString .= ') '; |
|
| 253 | } |
|
| 254 | } |
|
| 255 | } |
|
| 256 | } |
|
| 257 | ||
| 258 | /** |
|
| 259 | * Build having string |
|
| @@ 266-342 (lines=77) @@ | ||
| 263 | * @param string $operation |
|
| 264 | * @param string $concatenation |
|
| 265 | */ |
|
| 266 | public function buildHaving($where = '', $value = '', $operation = '=', $concatenation = 'AND') { |
|
| 267 | if (!is_array($where)) { |
|
| 268 | if (empty($operation)) { |
|
| 269 | $operation = '='; |
|
| 270 | } |
|
| 271 | if ($concatenation === false) { |
|
| 272 | $concatenation = 'AND'; |
|
| 273 | } elseif ($concatenation === true) { |
|
| 274 | $concatenation = ''; |
|
| 275 | } |
|
| 276 | ||
| 277 | if ($this->havingString == '') { |
|
| 278 | $this->havingString = ' HAVING '; |
|
| 279 | } |
|
| 280 | ||
| 281 | if (stristr($operation, 'IN') || stristr($operation, 'NOT IN')) { |
|
| 282 | if (is_array($value)) { |
|
| 283 | $newValue = ''; |
|
| 284 | foreach ($value as $item) { |
|
| 285 | if ($newValue) { |
|
| 286 | $newValue .= ','; |
|
| 287 | } |
|
| 288 | if (is_string($item)) { |
|
| 289 | $newValue .= '"' . $item . '"'; |
|
| 290 | } else { |
|
| 291 | $newValue .= $item; |
|
| 292 | } |
|
| 293 | } |
|
| 294 | $value = '(' . $newValue . ')'; |
|
| 295 | } elseif (!preg_match('!\(!', $value) && !preg_match('![^0-9,\.\(\) ]!', $value)) { |
|
| 296 | $value = "({$value})"; |
|
| 297 | } elseif (preg_match('!\(!', $value) && preg_match('![^0-9,\.\(\) ]!', $value)) { |
|
| 298 | $value = "\"{$value}\""; |
|
| 299 | } |
|
| 300 | } elseif (!in_array($value, array('CURRENT_TIMESTAMP', 'NULL'))) { |
|
| 301 | $this->params[] = $value; |
|
| 302 | $value = "?"; |
|
| 303 | } |
|
| 304 | if (substr($this->havingString, -1, 1) == '(' || substr($this->havingString, -2, 2) == 'G ') { |
|
| 305 | $this->havingString .= " {$where} {$operation} {$value} "; |
|
| 306 | } else { |
|
| 307 | $this->havingString .= "{$concatenation} {$where} {$operation} {$value} "; |
|
| 308 | } |
|
| 309 | } else { |
|
| 310 | $i = -1; |
|
| 311 | while (isset($where[++$i])) { |
|
| 312 | $item = $where[$i]; |
|
| 313 | if (isset($where[$i + 1]) && !isset($where[$i - 1]) && is_array($where[$i])) { |
|
| 314 | if ($this->havingString != null && substr($this->havingString, -1, 1) != '(' && $this->havingString != 'HAVING ') { |
|
| 315 | if (!isset($item[3])) { |
|
| 316 | $concatenation = 'AND'; |
|
| 317 | } else { |
|
| 318 | $concatenation = $item[3]; |
|
| 319 | } |
|
| 320 | ||
| 321 | $this->havingString .= "{$concatenation} "; |
|
| 322 | } |
|
| 323 | ||
| 324 | if ($this->havingString != '') { |
|
| 325 | $this->havingString .= '('; |
|
| 326 | } else { |
|
| 327 | $this->havingString = 'HAVING ('; |
|
| 328 | } |
|
| 329 | } |
|
| 330 | ||
| 331 | if (!is_array($item)) { |
|
| 332 | call_user_func_array(array($this, 'buildHaving'), $where); |
|
| 333 | break; |
|
| 334 | } else { |
|
| 335 | $this->buildHaving($item); |
|
| 336 | } |
|
| 337 | if (!isset($where[$i + 1]) && isset($where[$i - 1])) { |
|
| 338 | $this->havingString .= ') '; |
|
| 339 | } |
|
| 340 | } |
|
| 341 | } |
|
| 342 | } |
|
| 343 | ||
| 344 | public function buildQuery() { |
|
| 345 | $query = $this->operation; |
|