UpdateRoutine   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 89
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
B generateBody() 0 55 6
A generateRoutineDeclaration() 0 3 1
A generateDocBlock() 0 3 1
A generateSqlDataAndDesignationType() 0 4 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SetBased\Stratum\MySql\Crud\Helper;
5
6
/**
7
 * Generates the code for a stored routine that updates a row.
8
 */
9
class UpdateRoutine extends BaseRoutine
10
{
11
  //--------------------------------------------------------------------------------------------------------------------
12
  /**
13
   * @inheritdoc
14
   */
15
  protected function generateBody(): void
16
  {
17
    $this->codeStore->append(sprintf('update %s', $this->tableName));
18
    $this->codeStore->append('set');
19
20
    $offset  = mb_strlen($this->codeStore->getLastLine());
21
    $columns = $this->tableColumnsWithoutAutoIncrement();
22
    $width   = $this->maxColumnNameLength($columns);
23
24
    $first = true;
25
    foreach ($columns as $column)
26
    {
27
      if ($first)
28
      {
29
        $format = sprintf("%%%ds %%-%ds = p_%%s", $offset, $width);
30
        $this->codeStore->appendToLastLine(sprintf($format, '', $column['column_name'], $column['column_name']));
31
32
        $first = false;
33
      }
34
      else
35
      {
36
        $format = sprintf("%%-%ds %%-%ds = p_%%s", $offset + 3, $width);
37
        $this->codeStore->append(sprintf($format, ',', $column['column_name'], $column['column_name']));
38
      }
39
    }
40
41
    $this->codeStore->append('where');
42
43
    $columns = $this->keyColumns();
44
    $width   = $this->maxColumnNameLength($columns);
45
46
    $first = true;
47
    foreach ($columns as $column)
48
    {
49
      if ($first)
50
      {
51
        $format = sprintf("%%%ds %%-%ds = p_%%s", 1, $width);
52
        $line   = sprintf($format, '', $column['column_name'], $column['column_name']);
53
        $this->codeStore->appendToLastLine($line);
54
55
        $first = false;
56
      }
57
      else
58
      {
59
        $format = sprintf("and%%%ds %%-%ds = p_%%s", 3, $width);
60
        $this->codeStore->append(sprintf($format, '', $column['column_name'], $column['column_name']));
61
      }
62
    }
63
64
    if (empty($this->uniqueIndexes))
65
    {
66
      $this->codeStore->append('limit 0,1');
67
    }
68
69
    $this->codeStore->append(';');
70
  }
71
72
  //--------------------------------------------------------------------------------------------------------------------
73
  /**
74
   * @inheritDoc
75
   */
76
  protected function generateDocBlock(): void
77
  {
78
    $this->generateDocBlockAllColumnsWithKeyList();
79
  }
80
81
  //--------------------------------------------------------------------------------------------------------------------
82
  /**
83
   * Generates the function name and parameters of the stored routine.
84
   */
85
  protected function generateRoutineDeclaration(): void
86
  {
87
    $this->generateRoutineDeclarationWithKey();
88
  }
89
90
  //--------------------------------------------------------------------------------------------------------------------
91
  /**
92
   * @inheritDoc
93
   */
94
  protected function generateSqlDataAndDesignationType(): void
95
  {
96
    $this->codeStore->append('modifies sql data');
97
    $this->codeStore->append('-- type: none');
98
  }
99
100
  //--------------------------------------------------------------------------------------------------------------------
101
}
102
103
//----------------------------------------------------------------------------------------------------------------------
104