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

InsertMultipleWrapper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 40
dl 0
loc 110
rs 10
c 1
b 0
f 0
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A enhancePhpDocBlockParameters() 0 8 1
A generateMethodBodyWithLobReturnData() 0 2 1
A getDocBlockReturnType() 0 3 1
A generateMethodBodyWithoutLob() 0 45 4
A getReturnTypeDeclaration() 0 3 1
A getWrapperArgs() 0 3 1
A generateMethodBodyWithLobFetchData() 0 2 1
1
<?php
2
declare(strict_types=1);
3
4
namespace SetBased\Stratum\MySql\Wrapper;
5
6
use SetBased\Exception\LogicException;
7
use SetBased\Stratum\Common\Wrapper\Helper\WrapperContext;
8
use SetBased\Stratum\MySql\Exception\MySqlQueryErrorException;
9
10
/**
11
 * Class for generating a wrapper method for a stored procedure that prepares a table to be used with a multiple insert
12
 * SQL statement.
13
 */
14
class InsertMultipleWrapper extends MysqlWrapper
15
{
16
  //--------------------------------------------------------------------------------------------------------------------
17
  /**
18
   * @inheritdoc
19
   */
20
  protected function enhancePhpDocBlockParameters(array &$parameters): void
21
  {
22
    $parameter = ['php_name'       => '$rows',
23
                  'description'    => 'The rows that must be inserted.',
24
                  'php_type'       => 'array[]',
25
                  'dtd_identifier' => null];
26
27
    $parameters = array_merge([$parameter], $parameters);
28
  }
29
30
  //--------------------------------------------------------------------------------------------------------------------
31
  /**
32
   * @inheritdoc
33
   */
34
  protected function generateMethodBodyWithLobFetchData(WrapperContext $context): void
35
  {
36
    // Nothing to do.
37
  }
38
39
  //--------------------------------------------------------------------------------------------------------------------
40
  /**
41
   * @inheritdoc
42
   */
43
  protected function generateMethodBodyWithLobReturnData(WrapperContext $context): void
44
  {
45
    // Nothing to do.
46
  }
47
48
  //--------------------------------------------------------------------------------------------------------------------
49
  /**
50
   * @inheritdoc
51
   */
52
  protected function generateMethodBodyWithoutLob(WrapperContext $context): void
53
  {
54
    $this->throws(MySqlQueryErrorException::class);
55
56
    $tableName = $context->phpStratumMetadata['designation']['table_name'];
57
    $keys      = $context->phpStratumMetadata['designation']['keys'];
58
    $columns   = $context->phpStratumMetadata['insert_multiple_table_columns'];
59
    $n1        = sizeof($keys);
60
    $n2        = sizeof($columns);
61
    if ($n1!==$n2)
62
    {
63
      throw new LogicException("Number of fields %d and number of columns %d don't match.", $n1, $n2);
64
    }
65
66
    $context->codeStore->append(sprintf("\$this->realQuery('call %s(%s)');",
67
                                        $context->phpStratumMetadata['routine_name'],
68
                                        $this->getRoutineArgs($context)));
69
70
    $columnNames = [];
71
    $values      = [];
72
    foreach ($keys as $i => $key)
73
    {
74
      if ($key!='_')
75
      {
76
        $columnNames[] = '`'.$columns[$i]['column_name'].'`';
77
        $values[]      = $context->dataType->escapePhpExpression($columns[$i], '$row[\''.$key.'\']');
78
      }
79
    }
80
81
    $context->codeStore->append('if (is_array($rows) && !empty($rows))');
82
    $context->codeStore->append('{');
83
    $context->codeStore->append(sprintf('$sql = "INSERT INTO `%s`(%s)".PHP_EOL;',
84
                                        $tableName,
85
                                        implode(', ', $columnNames)));
86
    $context->codeStore->append('$first = true;');
87
    $context->codeStore->append('foreach($rows as $row)');
88
    $context->codeStore->append('{');
89
90
    $context->codeStore->append(sprintf("\$sql .= ((\$first) ? 'values' : ',     ').'(%s)'.PHP_EOL;",
91
                                        implode(', ', $values)));
92
93
    $context->codeStore->append('$first = false;');
94
    $context->codeStore->append('}');
95
    $context->codeStore->append('$this->realQuery($sql);');
96
    $context->codeStore->append('}');
97
  }
98
99
  //--------------------------------------------------------------------------------------------------------------------
100
  /**
101
   * @inheritdoc
102
   */
103
  protected function getDocBlockReturnType(WrapperContext $context): string
104
  {
105
    return 'void';
106
  }
107
108
  //--------------------------------------------------------------------------------------------------------------------
109
  /**
110
   * @inheritdoc
111
   */
112
  protected function getReturnTypeDeclaration(WrapperContext $context): string
113
  {
114
    return ': void';
115
  }
116
117
  //--------------------------------------------------------------------------------------------------------------------
118
  /**
119
   * @inheritdoc
120
   */
121
  protected function getWrapperArgs(WrapperContext $context): string
122
  {
123
    return '?array $rows';
124
  }
125
126
  //--------------------------------------------------------------------------------------------------------------------
127
}
128
129
//----------------------------------------------------------------------------------------------------------------------
130