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