Passed
Push — master ( f71b04...7dcb0c )
by P.R.
12:30
created

SelectRoutine   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 9

4 Methods

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