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