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