| Total Complexity | 135 |
| Total Lines | 1258 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Tools often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Tools, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class Tools extends AbstractionController |
||
| 23 | { |
||
| 24 | use \Drone\Error\ErrorTrait; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var integer |
||
| 28 | */ |
||
| 29 | private $identity; |
||
|
|
|||
| 30 | |||
| 31 | /** |
||
| 32 | * @var EntityAdapter |
||
| 33 | */ |
||
| 34 | private $usersEntity; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var EntityAdapter |
||
| 38 | */ |
||
| 39 | private $identifiersEntity; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var EntityAdapter |
||
| 43 | */ |
||
| 44 | private $connectionTypesEntity; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var EntityAdapter |
||
| 48 | */ |
||
| 49 | private $connectionFieldsEntity; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var EntityAdapter |
||
| 53 | */ |
||
| 54 | private $userConnectionEntity; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var EntityAdapter |
||
| 58 | */ |
||
| 59 | private $userConnectionDetailsEntity; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @return integer |
||
| 63 | */ |
||
| 64 | private function getIdentity() |
||
| 65 | { |
||
| 66 | $config = include 'module/Auth/config/user.config.php'; |
||
| 67 | $method = $config["authentication"]["method"]; |
||
| 68 | $key = $config["authentication"]["key"]; |
||
| 69 | |||
| 70 | switch ($method) |
||
| 71 | { |
||
| 72 | case '_COOKIE': |
||
| 73 | |||
| 74 | $user = $this->getUsersEntity()->select([ |
||
| 75 | "USERNAME" => $_COOKIE[$key] |
||
| 76 | ]); |
||
| 77 | |||
| 78 | break; |
||
| 79 | |||
| 80 | case '_SESSION': |
||
| 81 | |||
| 82 | $user = $this->getUsersEntity()->select([ |
||
| 83 | "USERNAME" => $_SESSION[$key] |
||
| 84 | ]); |
||
| 85 | |||
| 86 | break; |
||
| 87 | } |
||
| 88 | |||
| 89 | $user = array_shift($user); |
||
| 90 | |||
| 91 | return $user->USER_ID; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @return UsersEntity |
||
| 96 | */ |
||
| 97 | private function getUsersEntity() |
||
| 98 | { |
||
| 99 | if (!is_null($this->usersEntity)) |
||
| 100 | return $this->usersEntity; |
||
| 101 | |||
| 102 | $this->usersEntity = new EntityAdapter(new TableGateway(new UserModel())); |
||
| 103 | |||
| 104 | return $this->usersEntity; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @return UsersEntity |
||
| 109 | */ |
||
| 110 | private function getIdentifiersEntity() |
||
| 111 | { |
||
| 112 | if (!is_null($this->identifiersEntity)) |
||
| 113 | return $this->identifiersEntity; |
||
| 114 | |||
| 115 | $this->identifiersEntity = new EntityAdapter(new TableGateway(new Identifiers())); |
||
| 116 | |||
| 117 | return $this->identifiersEntity; |
||
| 118 | } |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @return EntityAdapter |
||
| 122 | */ |
||
| 123 | private function getConnectionTypesEntity() |
||
| 131 | } |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return EntityAdapter |
||
| 135 | */ |
||
| 136 | private function getConnectionFieldsEntity() |
||
| 137 | { |
||
| 138 | if (!is_null($this->connectionFieldsEntity)) |
||
| 139 | return $this->connectionFieldsEntity; |
||
| 140 | |||
| 141 | $this->connectionFieldsEntity = new EntityAdapter(new TableGateway(new ConnectionTypeField())); |
||
| 142 | |||
| 143 | return $this->connectionFieldsEntity; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return EntityAdapter |
||
| 148 | */ |
||
| 149 | private function getUserConnectionEntity() |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @return EntityAdapter |
||
| 161 | */ |
||
| 162 | private function getUserConnectionDetailsEntity() |
||
| 163 | { |
||
| 164 | if (!is_null($this->userConnectionDetailsEntity)) |
||
| 165 | return $this->userConnectionDetailsEntity; |
||
| 166 | |||
| 167 | $this->userConnectionDetailsEntity = new EntityAdapter(new TableGateway(new UserConnectionDetails())); |
||
| 168 | |||
| 169 | return $this->userConnectionDetailsEntity; |
||
| 170 | } |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Tests a connection |
||
| 174 | * |
||
| 175 | * @return array |
||
| 176 | */ |
||
| 177 | public function testConnection() |
||
| 178 | { |
||
| 179 | clearstatcache(); |
||
| 180 | session_write_close(); |
||
| 181 | |||
| 182 | # data to send |
||
| 183 | $data = []; |
||
| 184 | |||
| 185 | # environment settings |
||
| 186 | $post = $this->getPost(); # catch $_POST |
||
| 187 | $this->setTerminal(true); # set terminal |
||
| 188 | |||
| 189 | # TRY-CATCH-BLOCK |
||
| 190 | try { |
||
| 191 | |||
| 192 | # STANDARD VALIDATIONS [check method] |
||
| 193 | if (!$this->isPost()) |
||
| 194 | { |
||
| 195 | $http = new Http(); |
||
| 196 | $http->writeStatus($http::HTTP_METHOD_NOT_ALLOWED); |
||
| 197 | |||
| 198 | die('Error ' . $http::HTTP_METHOD_NOT_ALLOWED .' (' . $http->getStatusText($http::HTTP_METHOD_NOT_ALLOWED) . ')!!'); |
||
| 199 | } |
||
| 200 | |||
| 201 | $idenfiers = $this->getIdentifiersEntity()->select([]); |
||
| 202 | $dbconfig = []; |
||
| 203 | |||
| 204 | if (array_key_exists('conn_id', $post)) |
||
| 205 | { |
||
| 206 | # STANDARD VALIDATIONS [check needed arguments] |
||
| 207 | $needles = ['conn_id']; |
||
| 208 | |||
| 209 | array_walk($needles, function(&$item) use ($post) { |
||
| 210 | if (!array_key_exists($item, $post)) |
||
| 211 | { |
||
| 212 | $http = new Http(); |
||
| 213 | $http->writeStatus($http::HTTP_BAD_REQUEST); |
||
| 214 | |||
| 215 | die('Error ' . $http::HTTP_BAD_REQUEST .' (' . $http->getStatusText($http::HTTP_BAD_REQUEST) . ')!!'); |
||
| 216 | } |
||
| 217 | }); |
||
| 218 | |||
| 219 | $id = $post["conn_id"]; |
||
| 220 | |||
| 221 | $details = $this->getUserConnectionDetailsEntity()->select([ |
||
| 222 | "USER_CONN_ID" => $id |
||
| 223 | ]); |
||
| 224 | |||
| 225 | foreach ($details as $field) |
||
| 226 | { |
||
| 227 | foreach ($idenfiers as $identifier) |
||
| 228 | { |
||
| 229 | if ($field->CONN_IDENTI_ID == $identifier->CONN_IDENTI_ID) |
||
| 230 | $dbconfig[$identifier->CONN_IDENTI_NAME] = $field->FIELD_VALUE; |
||
| 231 | } |
||
| 232 | } |
||
| 233 | } |
||
| 234 | else |
||
| 235 | { |
||
| 236 | # STANDARD VALIDATIONS [check needed arguments] |
||
| 237 | $needles = ['type', 'aliasname']; |
||
| 238 | |||
| 239 | array_walk($needles, function(&$item) use ($post) { |
||
| 240 | if (!array_key_exists($item, $post)) |
||
| 241 | { |
||
| 242 | $http = new Http(); |
||
| 243 | $http->writeStatus($http::HTTP_BAD_REQUEST); |
||
| 244 | |||
| 245 | die('Error ' . $http::HTTP_BAD_REQUEST .' (' . $http->getStatusText($http::HTTP_BAD_REQUEST) . ')!!'); |
||
| 246 | } |
||
| 247 | }); |
||
| 248 | |||
| 249 | $components = [ |
||
| 250 | "attributes" => [ |
||
| 251 | "type" => [ |
||
| 252 | "required" => true, |
||
| 253 | ], |
||
| 254 | "aliasname" => [ |
||
| 255 | "required" => true, |
||
| 256 | ] |
||
| 257 | ], |
||
| 258 | ]; |
||
| 259 | |||
| 260 | $options = [ |
||
| 261 | "type" => [ |
||
| 262 | "label" => "Value of connection parameter" |
||
| 263 | ], |
||
| 264 | "aliasname" => [ |
||
| 265 | "label" => "Type of connection parameter" |
||
| 266 | ] |
||
| 267 | ]; |
||
| 268 | |||
| 269 | $form = new Form($components); |
||
| 270 | $form->fill($post); |
||
| 271 | |||
| 272 | $validator = new FormValidator($form, $options); |
||
| 273 | $validator->validate(); |
||
| 274 | |||
| 275 | $data["validator"] = $validator; |
||
| 276 | |||
| 277 | # STANDARD VALIDATIONS [check argument constraints] |
||
| 278 | if (!$validator->isValid()) |
||
| 279 | { |
||
| 280 | $data["messages"] = $validator->getMessages(); |
||
| 281 | throw new \Drone\Exception\Exception("Form validation errors"); |
||
| 282 | } |
||
| 283 | |||
| 284 | $id = 0; |
||
| 285 | |||
| 286 | foreach ($post['field'][$post["type"]] as $field_number => $field_value) |
||
| 287 | { |
||
| 288 | foreach ($idenfiers as $identifier) |
||
| 289 | { |
||
| 290 | if ($field_number == $identifier->CONN_IDENTI_ID) |
||
| 291 | $dbconfig[$identifier->CONN_IDENTI_NAME] = $field_value; |
||
| 292 | } |
||
| 293 | } |
||
| 294 | } |
||
| 295 | |||
| 296 | try |
||
| 297 | { |
||
| 298 | $entity = new EntityMd([]); |
||
| 299 | $entity->setConnectionIdentifier("CONN" . $id); |
||
| 300 | |||
| 301 | $driverAdapter = new \Drone\Db\Driver\DriverAdapter($dbconfig, false); |
||
| 302 | $driverAdapter->getDb()->connect(); |
||
| 303 | } |
||
| 304 | catch (\Exception $e) |
||
| 305 | { |
||
| 306 | # SUCCESS-MESSAGE |
||
| 307 | $data["process"] = "error"; |
||
| 308 | $data["message"] = $e->getMessage(); |
||
| 309 | |||
| 310 | return $data; |
||
| 311 | } |
||
| 312 | |||
| 313 | # SUCCESS-MESSAGE |
||
| 314 | $data["process"] = "success"; |
||
| 315 | } |
||
| 316 | catch (\Drone\Exception\Exception $e) |
||
| 317 | { |
||
| 318 | # ERROR-MESSAGE |
||
| 319 | $data["process"] = "warning"; |
||
| 320 | $data["message"] = $e->getMessage(); |
||
| 321 | } |
||
| 322 | catch (\Exception $e) |
||
| 323 | { |
||
| 324 | $file = str_replace('\\', '', __CLASS__); |
||
| 325 | $storage = new \Drone\Exception\Storage("cache/$file.json"); |
||
| 326 | |||
| 327 | # stores the error code |
||
| 328 | if (($errorCode = $storage->store($e)) === false) |
||
| 329 | { |
||
| 330 | $errors = $storage->getErrors(); |
||
| 331 | |||
| 332 | # if error storing is not possible, handle it (internal app error) |
||
| 333 | $this->handleErrors($errors, __METHOD__); |
||
| 334 | } |
||
| 335 | |||
| 336 | $data["code"] = $errorCode; |
||
| 337 | $data["message"] = $e->getMessage(); |
||
| 338 | |||
| 339 | $config = include 'config/application.config.php'; |
||
| 340 | $data["dev_mode"] = $config["environment"]["dev_mode"]; |
||
| 341 | |||
| 342 | # redirect view |
||
| 343 | $this->setMethod('error'); |
||
| 344 | |||
| 345 | return $data; |
||
| 346 | } |
||
| 347 | |||
| 348 | return $data; |
||
| 349 | } |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Puts a worksheet |
||
| 353 | * |
||
| 354 | * @return array |
||
| 355 | */ |
||
| 356 | public function worksheet() |
||
| 377 | } |
||
| 378 | |||
| 379 | /** |
||
| 380 | * Executes a statement |
||
| 381 | * |
||
| 382 | * @return array |
||
| 383 | */ |
||
| 384 | public function execute() |
||
| 385 | { |
||
| 386 | clearstatcache(); |
||
| 387 | session_write_close(); |
||
| 388 | |||
| 389 | # data to send |
||
| 390 | $data = []; |
||
| 391 | |||
| 392 | # environment settings |
||
| 393 | $post = $this->getPost(); # catch $_POST |
||
| 394 | $this->setTerminal(true); # set terminal |
||
| 395 | |||
| 396 | # TRY-CATCH-BLOCK |
||
| 397 | try { |
||
| 398 | |||
| 399 | # STANDARD VALIDATIONS [check method] |
||
| 400 | if (!$this->isPost()) |
||
| 401 | { |
||
| 402 | $http = new Http(); |
||
| 403 | $http->writeStatus($http::HTTP_METHOD_NOT_ALLOWED); |
||
| 404 | |||
| 405 | die('Error ' . $http::HTTP_METHOD_NOT_ALLOWED .' (' . $http->getStatusText($http::HTTP_METHOD_NOT_ALLOWED) . ')!!'); |
||
| 406 | } |
||
| 407 | |||
| 408 | # STANDARD VALIDATIONS [check needed arguments] |
||
| 409 | $needles = ['conn', 'worksheet']; |
||
| 410 | |||
| 411 | array_walk($needles, function(&$item) use ($post) { |
||
| 412 | if (!array_key_exists($item, $post)) |
||
| 413 | { |
||
| 414 | $http = new Http(); |
||
| 415 | $http->writeStatus($http::HTTP_BAD_REQUEST); |
||
| 416 | |||
| 417 | die('Error ' . $http::HTTP_BAD_REQUEST .' (' . $http->getStatusText($http::HTTP_BAD_REQUEST) . ')!!'); |
||
| 418 | } |
||
| 419 | }); |
||
| 420 | |||
| 421 | $data["worksheet"] = $post["worksheet"]; |
||
| 422 | |||
| 423 | $id = $post["conn"]; |
||
| 424 | $data["conn"] = $id; |
||
| 425 | |||
| 426 | $connection = $this->getUserConnectionEntity()->select([ |
||
| 427 | "USER_CONN_ID" => $id |
||
| 428 | ]); |
||
| 429 | |||
| 430 | if (!count($connection)) |
||
| 431 | throw new \Exception("The Connection does not exists"); |
||
| 432 | |||
| 433 | $connection = array_shift($connection); |
||
| 434 | |||
| 435 | if ($connection->STATE == 'I') |
||
| 436 | throw new \Drone\Exception\Exception("This connection was deleted", 300); |
||
| 437 | |||
| 438 | $details = $this->getUserConnectionDetailsEntity()->select([ |
||
| 439 | "USER_CONN_ID" => $id |
||
| 440 | ]); |
||
| 441 | |||
| 442 | $idenfiers = $this->getIdentifiersEntity()->select([]); |
||
| 443 | |||
| 444 | $dbconfig = []; |
||
| 445 | |||
| 446 | foreach ($details as $field) |
||
| 447 | { |
||
| 448 | foreach ($idenfiers as $identifier) |
||
| 449 | { |
||
| 450 | if ($field->CONN_IDENTI_ID == $identifier->CONN_IDENTI_ID) |
||
| 451 | $dbconfig[$identifier->CONN_IDENTI_NAME] = $field->FIELD_VALUE; |
||
| 452 | } |
||
| 453 | } |
||
| 454 | |||
| 455 | /* identifies if sql is base64 encoded */ |
||
| 456 | if (array_key_exists('base64', $post)) |
||
| 457 | { |
||
| 458 | if ((bool) $post["base64"]) |
||
| 459 | $post["sql"] = base64_decode($post["sql"]); |
||
| 460 | } |
||
| 461 | |||
| 462 | $data["sql"] = base64_encode($post["sql"]); |
||
| 463 | |||
| 464 | $sql_text = $post["sql"]; |
||
| 465 | |||
| 466 | /* |
||
| 467 | * SQL parsing |
||
| 468 | */ |
||
| 469 | $sql_text = trim($sql_text); |
||
| 470 | |||
| 471 | if (empty($sql_text)) |
||
| 472 | throw new \Drone\Exception\Exception("Empty statement"); |
||
| 473 | |||
| 474 | $pos = strpos($sql_text, ';'); |
||
| 475 | |||
| 476 | if ($pos !== false) |
||
| 477 | { |
||
| 478 | $end_stament = strstr($sql_text, ';'); |
||
| 479 | |||
| 480 | if ($end_stament == ';') |
||
| 481 | $sql_text = strstr($sql_text, ';', true); |
||
| 482 | } |
||
| 483 | |||
| 484 | # clean comments and other characters |
||
| 485 | |||
| 486 | // (/**/) |
||
| 487 | $clean_code = preg_replace('/(\s)*\/\*([^*]|[\r\n]|(\*+([^*\/]|[\r\n])))*\*+\//', '', $sql_text); |
||
| 488 | |||
| 489 | // (--) |
||
| 490 | $clean_code = preg_replace('/(\s)*--.*\n/', "", $clean_code); |
||
| 491 | |||
| 492 | # clean other characters starting senteces |
||
| 493 | $clean_code = preg_replace('/^[\n\t\s]*/', "", $clean_code); |
||
| 494 | |||
| 495 | # indicates if SQL is a selection statement |
||
| 496 | $isSelectStm = $data["selectStm"] = (preg_match('/^SELECT/i', $clean_code)); |
||
| 497 | |||
| 498 | # indicates if SQL is a show statement |
||
| 499 | $isShowStm = $data["showStm"] = (preg_match('/^SHOW/i', $clean_code)); |
||
| 500 | |||
| 501 | # detect selection |
||
| 502 | if ($isSelectStm || $isShowStm) |
||
| 503 | { |
||
| 504 | $step = 10; |
||
| 505 | |||
| 506 | $row_start = 0; |
||
| 507 | $row_end = $step; |
||
| 508 | |||
| 509 | if (array_key_exists('row_start', $post) && array_key_exists('row_end', $post)) |
||
| 510 | { |
||
| 511 | $components = [ |
||
| 512 | "attributes" => [ |
||
| 513 | "row_start" => [ |
||
| 514 | "required" => true, |
||
| 515 | "type" => "number", |
||
| 516 | "min" => 0 |
||
| 517 | ], |
||
| 518 | "row_end" => [ |
||
| 519 | "required" => true, |
||
| 520 | "type" => "number", |
||
| 521 | "min" => 0 |
||
| 522 | ], |
||
| 523 | ], |
||
| 524 | ]; |
||
| 525 | |||
| 526 | $options = [ |
||
| 527 | "row_start" => [ |
||
| 528 | "label" => "Start row", |
||
| 529 | ], |
||
| 530 | "row_end" => [ |
||
| 531 | "label" => "End row", |
||
| 532 | ], |
||
| 533 | ]; |
||
| 534 | |||
| 535 | $form = new Form($components); |
||
| 536 | $form->fill($post); |
||
| 537 | |||
| 538 | $validator = new FormValidator($form, $options); |
||
| 539 | $validator->validate(); |
||
| 540 | |||
| 541 | # STANDARD VALIDATIONS [check argument constraints] |
||
| 542 | if (!$validator->isValid()) |
||
| 543 | { |
||
| 544 | $http = new Http(); |
||
| 545 | $http->writeStatus($http::HTTP_BAD_REQUEST); |
||
| 546 | |||
| 547 | die('Error ' . $http::HTTP_BAD_REQUEST .' (' . $http->getStatusText($http::HTTP_BAD_REQUEST) . ')!!'); |
||
| 548 | } |
||
| 549 | |||
| 550 | $row_start = $post["row_start"] + $step; |
||
| 551 | $row_end = $post["row_end"] + $step; |
||
| 552 | } |
||
| 553 | |||
| 554 | switch (strtolower($dbconfig["driver"])) |
||
| 555 | { |
||
| 556 | case 'mysqli': |
||
| 557 | |||
| 558 | # show statement cannot be a subquery |
||
| 559 | if (!$isShowStm) |
||
| 560 | $sql_text = "SELECT (@ROW_NUM:=@ROW_NUM + 1) AS ROW_NUM, V.* FROM ( |
||
| 561 | " . $sql_text . " |
||
| 562 | ) V LIMIT $row_start, $step"; |
||
| 563 | break; |
||
| 564 | |||
| 565 | case 'oci8': |
||
| 566 | |||
| 567 | $start = $row_start + 1; |
||
| 568 | |||
| 569 | $sql_text = "SELECT * FROM ( |
||
| 570 | SELECT ROWNUM ROW_NUM, V.* FROM (" . $sql_text . ") V |
||
| 571 | ) VV WHERE VV.ROW_NUM BETWEEN $start AND $row_end"; |
||
| 572 | break; |
||
| 573 | |||
| 574 | case 'sqlsrv': |
||
| 575 | |||
| 576 | $start = $row_start + 1; |
||
| 577 | |||
| 578 | $sql_text = "SELECT VV.* |
||
| 579 | FROM ( |
||
| 580 | SELECT ROW_NUMBER() OVER(ORDER BY ( |
||
| 581 | SELECT TOP 1 NAME |
||
| 582 | FROM SYS.DM_EXEC_DESCRIBE_FIRST_RESULT_SET('$sql_text', NULL, 0)) |
||
| 583 | ) AS ROW_NUM, V.* |
||
| 584 | FROM ( $sql_text ) V |
||
| 585 | ) VV |
||
| 586 | WHERE VV.ROW_NUM BETWEEN $start AND $row_end"; |
||
| 587 | break; |
||
| 588 | |||
| 589 | default: |
||
| 590 | # code... |
||
| 591 | break; |
||
| 592 | } |
||
| 593 | |||
| 594 | $data["row_start"] = $row_start; |
||
| 595 | $data["row_end"] = $row_end; |
||
| 596 | } |
||
| 597 | |||
| 598 | try { |
||
| 599 | |||
| 600 | $connError = false; |
||
| 601 | |||
| 602 | $entity = new EntityMd([]); |
||
| 603 | $entity->setConnectionIdentifier("CONN" . $id); |
||
| 604 | |||
| 605 | $driverAdapter = new \Drone\Db\Driver\DriverAdapter($dbconfig, false); |
||
| 606 | |||
| 607 | # start time to compute execution |
||
| 608 | $startTime = microtime(true); |
||
| 609 | |||
| 610 | $driverAdapter->getDb()->connect(); |
||
| 611 | |||
| 612 | $auth = $driverAdapter; |
||
| 613 | |||
| 614 | $data["results"] = $auth->getDb()->execute($sql_text); |
||
| 615 | } |
||
| 616 | # encapsulate real connection error! |
||
| 617 | catch (\Drone\Db\Driver\Exception\ConnectionException $e) |
||
| 618 | { |
||
| 619 | $connError = true; |
||
| 620 | |||
| 621 | $file = str_replace('\\', '', __CLASS__); |
||
| 622 | $storage = new \Drone\Exception\Storage("cache/$file.json"); |
||
| 623 | |||
| 624 | if (($errorCode = $storage->store($e)) === false) |
||
| 625 | { |
||
| 626 | $errors = $storage->getErrors(); |
||
| 627 | $this->handleErrors($errors, __METHOD__); |
||
| 628 | } |
||
| 629 | |||
| 630 | $data["code"] = $errorCode; |
||
| 631 | $data["message"] = "Could not connect to database!"; |
||
| 632 | |||
| 633 | # to identify development mode |
||
| 634 | $config = include 'config/application.config.php'; |
||
| 635 | $data["dev_mode"] = $config["environment"]["dev_mode"]; |
||
| 636 | |||
| 637 | # redirect view |
||
| 638 | $this->setMethod('error'); |
||
| 639 | } |
||
| 640 | catch (\Exception $e) |
||
| 641 | { |
||
| 642 | # SUCCESS-MESSAGE |
||
| 643 | $data["process"] = "error"; |
||
| 644 | $data["message"] = $e->getMessage(); |
||
| 645 | |||
| 646 | return $data; |
||
| 647 | } |
||
| 648 | |||
| 649 | # end time to compute execution |
||
| 650 | $endTime = microtime(true); |
||
| 651 | $elapsed_time = $endTime - $startTime; |
||
| 652 | |||
| 653 | $data["time"] = round($elapsed_time, 4); |
||
| 654 | |||
| 655 | if (!$connError) |
||
| 656 | { |
||
| 657 | $data["num_rows"] = $auth->getDb()->getNumRows(); |
||
| 658 | $data["num_fields"] = $auth->getDb()->getNumFields(); |
||
| 659 | $data["rows_affected"] = $auth->getDb()->getRowsAffected(); |
||
| 660 | |||
| 661 | # cumulative results |
||
| 662 | if ($isSelectStm && array_key_exists('num_rows', $post) && array_key_exists('time', $post)) |
||
| 663 | { |
||
| 664 | $data["num_rows"] += $post["num_rows"]; |
||
| 665 | $data["time"] += $post["time"]; |
||
| 666 | } |
||
| 667 | |||
| 668 | $data["data"] = []; |
||
| 669 | |||
| 670 | # redirect view |
||
| 671 | if ($isSelectStm || $isShowStm) |
||
| 672 | { |
||
| 673 | $rows = $auth->getDb()->getArrayResult(); |
||
| 674 | |||
| 675 | $k = 0; |
||
| 676 | |||
| 677 | # columns with errors in a select statement |
||
| 678 | $column_errors = []; |
||
| 679 | |||
| 680 | # data parsing |
||
| 681 | foreach ($rows as $key => $row) |
||
| 682 | { |
||
| 683 | $k++; |
||
| 684 | |||
| 685 | $data["data"][$key] = []; |
||
| 686 | |||
| 687 | if ($isShowStm) |
||
| 688 | { |
||
| 689 | $data["data"][$key]["ROW_NUM"] = $k; |
||
| 690 | $data["data"][$key][0] = $k; |
||
| 691 | } |
||
| 692 | |||
| 693 | foreach ($row as $column => $value) |
||
| 694 | { |
||
| 695 | if ($isShowStm) |
||
| 696 | $column++; |
||
| 697 | |||
| 698 | if (gettype($value) == 'object') |
||
| 699 | { |
||
| 700 | if (get_class($value) == 'OCI-Lob') |
||
| 701 | { |
||
| 702 | if (($val = @$value->load()) === false) |
||
| 703 | { |
||
| 704 | $val = null; # only for default, this value is not used |
||
| 705 | $column_errors[] = $column; |
||
| 706 | } |
||
| 707 | |||
| 708 | $data["data"][$key][$column] = $val; |
||
| 709 | } |
||
| 710 | else |
||
| 711 | $data["data"][$key][$column] = $value; |
||
| 712 | } |
||
| 713 | else { |
||
| 714 | $data["data"][$key][$column] = $value; |
||
| 715 | } |
||
| 716 | } |
||
| 717 | } |
||
| 718 | |||
| 719 | $data["column_errors"] = $column_errors; |
||
| 720 | |||
| 721 | if ($row_start > 1) |
||
| 722 | $this->setMethod('nextResults'); |
||
| 723 | } |
||
| 724 | |||
| 725 | if (array_key_exists('id', $post)) |
||
| 726 | $data["id"] = $post["id"]; |
||
| 727 | |||
| 728 | # SUCCESS-MESSAGE |
||
| 729 | $data["process"] = "success"; |
||
| 730 | } |
||
| 731 | } |
||
| 732 | catch (\Drone\Exception\Exception $e) |
||
| 733 | { |
||
| 734 | # ERROR-MESSAGE |
||
| 735 | $data["process"] = "warning"; |
||
| 736 | $data["message"] = $e->getMessage(); |
||
| 737 | } |
||
| 738 | catch (\Exception $e) |
||
| 739 | { |
||
| 740 | $file = str_replace('\\', '', __CLASS__); |
||
| 741 | $storage = new \Drone\Exception\Storage("cache/$file.json"); |
||
| 742 | |||
| 743 | # stores the error code |
||
| 744 | if (($errorCode = $storage->store($e)) === false) |
||
| 745 | { |
||
| 746 | $errors = $storage->getErrors(); |
||
| 747 | |||
| 748 | # if error storing is not possible, handle it (internal app error) |
||
| 749 | $this->handleErrors($errors, __METHOD__); |
||
| 750 | } |
||
| 751 | |||
| 752 | $data["code"] = $errorCode; |
||
| 753 | $data["message"] = $e->getMessage(); |
||
| 754 | |||
| 755 | $config = include 'config/application.config.php'; |
||
| 756 | $data["dev_mode"] = $config["environment"]["dev_mode"]; |
||
| 757 | |||
| 758 | # redirect view |
||
| 759 | $this->setMethod('error'); |
||
| 760 | |||
| 761 | return $data; |
||
| 762 | } |
||
| 763 | |||
| 764 | return $data; |
||
| 765 | } |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Exports a statement |
||
| 769 | * |
||
| 770 | * @return array |
||
| 771 | */ |
||
| 772 | public function export() |
||
| 1250 | } |
||
| 1251 | |||
| 1252 | private function handleErrors(Array $errors, $method) |
||
| 1280 | } |
||
| 1281 | } |
||
| 1282 | } |