DeleteRoutine   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A generateBody() 0 31 4
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 deletes a row.
8
 */
9
class DeleteRoutine extends BaseRoutine
10
{
11
  //--------------------------------------------------------------------------------------------------------------------
12
  /**
13
   * @inheritDoc
14
   */
15
  protected function generateBody(): void
16
  {
17
    $this->codeStore->append(sprintf('delete from %s', $this->tableName));
18
    $this->codeStore->append('where');
19
20
    $columns = $this->keyColumns();
21
    $width   = $this->maxColumnNameLength($columns);
22
23
    $first = true;
24
    foreach ($columns as $column)
25
    {
26
      if ($first)
27
      {
28
        $format = sprintf("%%%ds %%-%ds = p_%%s", 1, $width);
29
        $this->codeStore->appendToLastLine(sprintf($format, '', $column['column_name'], $column['column_name']));
30
31
        $first = false;
32
      }
33
      else
34
      {
35
        $format = sprintf("and%%%ds %%-%ds = p_%%s", 3, $width);
36
        $this->codeStore->append(sprintf($format, '', $column['column_name'], $column['column_name']));
37
      }
38
    }
39
40
    if (empty($this->uniqueIndexes))
41
    {
42
      $this->codeStore->append('limit 0,1');
43
    }
44
45
    $this->codeStore->append(';');
46
  }
47
48
  //--------------------------------------------------------------------------------------------------------------------
49
  /**
50
   * @inheritDoc
51
   */
52
  protected function generateDocBlock(): void
53
  {
54
    $this->generateDocBlockWithKey();
55
  }
56
57
  //--------------------------------------------------------------------------------------------------------------------
58
  /**
59
   * Generates the function name and parameters of the stored routine.
60
   */
61
  protected function generateRoutineDeclaration(): void
62
  {
63
    $this->generateRoutineDeclarationWithKey();
64
  }
65
66
  //--------------------------------------------------------------------------------------------------------------------
67
  /**
68
   * @inheritDoc
69
   */
70
  protected function generateSqlDataAndDesignationType(): void
71
  {
72
    $this->codeStore->append('modifies sql data');
73
    $this->codeStore->append('-- type: none');
74
  }
75
}
76
//----------------------------------------------------------------------------------------------------------------------
77