MySqlCrudWorker::generateRoutine()   A
last analyzed

Complexity

Conditions 5
Paths 5

Size

Total Lines 34
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 22
nc 5
nop 3
dl 0
loc 34
ccs 0
cts 18
cp 0
crap 30
rs 9.2568
c 1
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace SetBased\Stratum\MySql\Backend;
5
6
use SetBased\Exception\FallenException;
7
use SetBased\Stratum\Backend\CrudWorker;
8
use SetBased\Stratum\MySql\Crud\Helper\DeleteRoutine;
9
use SetBased\Stratum\MySql\Crud\Helper\InsertRoutine;
10
use SetBased\Stratum\MySql\Crud\Helper\SelectRoutine;
11
use SetBased\Stratum\MySql\Crud\Helper\UpdateRoutine;
12
13
/**
14
 * Creates stored procedures for CRUD operations.
15
 */
16
class MySqlCrudWorker extends MySqlWorker implements CrudWorker
17
{
18
  //--------------------------------------------------------------------------------------------------------------------
19
  /**
20
   * Generates the code for a stored routine.
21
   *
22
   * @param string $tableName   The name of the table.
23
   * @param string $operation   The operation {insert|update|delete|select}.
24
   * @param string $routineName The name of the generated routine.
25
   */
26
  public function generateRoutine(string $tableName, string $operation, string $routineName): string
27
  {
28
    $schemaName = $this->settings->manString('database.database');
29
30
    $this->connect();
31
32
    $tableColumns  = $this->dl->tableColumns($schemaName, $tableName);
33
    $primaryKey    = $this->dl->tablePrimaryKey($schemaName, $tableName);
34
    $uniqueIndexes = $this->dl->tableUniqueIndexes($schemaName, $tableName);
35
36
    switch ($operation)
37
    {
38
      case 'update':
39
        $routine = new UpdateRoutine($tableName, $routineName, $tableColumns, $primaryKey, $uniqueIndexes);
40
        break;
41
42
      case 'delete':
43
        $routine = new DeleteRoutine($tableName, $routineName, $tableColumns, $primaryKey, $uniqueIndexes);
44
        break;
45
46
      case 'select':
47
        $routine = new SelectRoutine($tableName, $routineName, $tableColumns, $primaryKey, $uniqueIndexes);
48
        break;
49
50
      case 'insert':
51
        $routine = new InsertRoutine($tableName, $routineName, $tableColumns, $primaryKey, $uniqueIndexes);
52
        break;
53
54
      default:
55
        throw new FallenException('operation', $operation);
56
    }
57
    $this->disconnect();
58
59
    return $routine->getCode();
60
  }
61
62
  //--------------------------------------------------------------------------------------------------------------------
63
  /**
64
   * Returns a list of all supported operations by the worker.
65
   */
66
  public function operations(): array
67
  {
68
    return ['insert', 'update', 'delete', 'select'];
69
  }
70
71
  //--------------------------------------------------------------------------------------------------------------------
72
  /**
73
   * Returns a list of all tables in de database of the backend.
74
   */
75
  public function tables(): array
76
  {
77
    $this->connect();
78
    $schema = $this->settings->manString('database.database');
79
    $rows   = $this->dl->allTablesNames($schema);
80
    $this->disconnect();
81
82
    $tables = [];
83
    foreach ($rows as $row)
84
    {
85
      $tables[] = $row['table_name'];
86
    }
87
88
    return $tables;
89
  }
90
91
  //--------------------------------------------------------------------------------------------------------------------
92
}
93
94
//----------------------------------------------------------------------------------------------------------------------
95