|
539 | 539 | * @param Response $response
|
540 | 540 | */
|
541 | 541 | $this->on('cradlephp-cradle-website-sql-populate', function ($request, $response) {
|
542 |
| - |
| 542 | + //load up the database |
| 543 | + $pdo = $this->package('global')->service('sql-main'); |
| 544 | + //Storm ORM |
| 545 | + $database = SqlFactory::load($pdo); |
| 546 | + |
| 547 | + //setting up combining variables |
| 548 | + $combineFlag = 0; |
| 549 | + $combinedQuery = ""; |
| 550 | + |
| 551 | + //Going through each file in fixtures |
| 552 | + foreach (scandir(__DIR__ . '/fixtures') as $file) { |
| 553 | + //if it's not an sql file |
| 554 | + if(substr($file, -4) !== '.sql') { |
| 555 | + //skip |
| 556 | + continue; |
| 557 | + } |
| 558 | + //put all lines of the file into an array |
| 559 | + $sql = file(sprintf('%s/fixtures/%s', __DIR__, $file), FILE_IGNORE_NEW_LINES); |
| 560 | + |
| 561 | + //to check for the final iteration for the array |
| 562 | + $len = count($sql); |
| 563 | + |
| 564 | + //working on every line that was read. i is used as a key. query is used as the value |
| 565 | + foreach($sql as $i=>$query) { |
| 566 | + //skips everything that is either empty or contains a comment |
| 567 | + if ($query == "" || preg_match_all("/(^--.*)/m", $query)) { |
| 568 | + $combineFlag = 0; |
| 569 | + //if combinedQuery is not empty, work on it. Empty after. If it is, don't work on it. |
| 570 | + if ($combinedQuery != ""){ |
| 571 | + //combinedQuery is not empty means that it just finished combining. |
| 572 | + try { |
| 573 | + $test = $database->query($combinedQuery); |
| 574 | + } catch (\Exception $e){ |
| 575 | + continue; |
| 576 | + } |
| 577 | + |
| 578 | + $combinedQuery = ""; |
| 579 | + } |
| 580 | + continue; |
| 581 | + } else if ($query != "" && $combineFlag == 1) { |
| 582 | + $combinedQuery = $combinedQuery . " " . $query; |
| 583 | + if ($i == $len - 1){ |
| 584 | + //last combinedQuery in the file gets worked on here. This is the very last line if it's combined. |
| 585 | + try { |
| 586 | + $test = $database->query($combinedQuery); |
| 587 | + } catch (\Exception $e){ |
| 588 | + continue; |
| 589 | + } |
| 590 | + } |
| 591 | + continue; |
| 592 | + } |
| 593 | + |
| 594 | + if (preg_match_all("/(.*);/m", $query)) { |
| 595 | + if ($combineFlag == 1){ //it's the end of the query since we see ; Time to execute |
| 596 | + try { |
| 597 | + $test = $database->query($combinedQuery); |
| 598 | + $combineFlag = 0; |
| 599 | + } catch (\Exception $e){ |
| 600 | + continue; |
| 601 | + } |
| 602 | + } else { //It's already complete even without the combine. |
| 603 | + try { |
| 604 | + $test = $database->query($query); |
| 605 | + } catch (\Exception $e){ |
| 606 | + continue; |
| 607 | + } |
| 608 | + } |
| 609 | + } else { //we have to combine since we don't see a ; |
| 610 | + $combineFlag = 1; //becomes true to combine |
| 611 | + $combinedQuery = $query; //make it the start |
| 612 | + continue; |
| 613 | + } |
| 614 | + |
| 615 | + } |
| 616 | + //reset values for the next |
| 617 | + $combineFlag = 0; |
| 618 | + $combinedQuery = ""; |
| 619 | + } |
543 | 620 | });
|
0 commit comments