Passed
Push — master ( e0c5e8...7926db )
by P.R.
04:00
created

BulkInsertWrapper::enhancePhpDocBlockParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types=1);
3
4
namespace SetBased\Stratum\MySql\Wrapper;
5
6
use SetBased\Exception\LogicException;
7
use SetBased\Stratum\MySql\Exception\MySqlQueryErrorException;
8
use SetBased\Stratum\MySql\Helper\DataTypeHelper;
9
10
/**
11
 * Class for generating a wrapper method for a stored procedure that prepares a table to be used with a bulk SQL
12
 * statement.
13
 */
14
class BulkInsertWrapper extends Wrapper
15
{
16
  //--------------------------------------------------------------------------------------------------------------------
17
  /**
18
   * @inheritdoc
19
   */
20 1
  protected function enhancePhpDocBlockParameters(array &$parameters): void
21
  {
22 1
    $parameter = ['php_name'             => '$rows',
23
                  'description'          => 'The rows that must inserted.',
24
                  'php_type'             => 'array[]',
25
                  'data_type_descriptor' => null];
26
27 1
    $parameters = array_merge([$parameter], $parameters);
28 1
  }
29
30
  //--------------------------------------------------------------------------------------------------------------------
31
  /**
32
   * @inheritdoc
33
   */
34 1
  protected function getDocBlockReturnType(): string
35
  {
36 1
    return 'void';
37
  }
38
39
  //--------------------------------------------------------------------------------------------------------------------
40
  /**
41
   * @inheritdoc
42
   */
43 1
  protected function getReturnTypeDeclaration(): string
44
  {
45 1
    return ': void';
46
  }
47
48
  //--------------------------------------------------------------------------------------------------------------------
49
  /**
50
   * @inheritdoc
51
   */
52 1
  protected function getWrapperArgs(): string
53
  {
54 1
    return '?array $rows';
55
  }
56
57
  //--------------------------------------------------------------------------------------------------------------------
58
  /**
59
   * @inheritdoc
60
   */
61 1
  protected function writeResultHandler(): void
62
  {
63 1
    $this->throws(MySqlQueryErrorException::class);
64
65
    // Validate number of column names and number of column types are equal.
66 1
    $n1 = sizeof($this->routine['bulk_insert_keys']);
67 1
    $n2 = sizeof($this->routine['bulk_insert_columns']);
68 1
    if ($n1!=$n2)
69
    {
70
      throw new LogicException("Number of fields %d and number of columns %d don't match.", $n1, $n2);
71
    }
72
73 1
    $routine_args = $this->getRoutineArgs();
74 1
    $this->codeStore->append('$this->realQuery(\'call '.$this->routine['routine_name'].'('.$routine_args.')\');');
75
76 1
    $columns = '';
77 1
    $fields  = '';
78 1
    foreach ($this->routine['bulk_insert_keys'] as $i => $key)
79
    {
80 1
      if ($key!='_')
81
      {
82 1
        if ($columns) $columns .= ',';
83 1
        $columns .= '`'.$this->routine['bulk_insert_columns'][$i]['column_name'].'`';
84
85 1
        if ($fields) $fields .= ',';
86 1
        $fields .= DataTypeHelper::escapePhpExpression($this->routine['bulk_insert_columns'][$i], '$row[\''.$key.'\']');
87
      }
88
    }
89
90 1
    $this->codeStore->append('if (is_array($rows) && !empty($rows))');
91 1
    $this->codeStore->append('{');
92 1
    $this->codeStore->append('$sql = "INSERT INTO `'.$this->routine['bulk_insert_table_name'].'`('.$columns.')".PHP_EOL;');
93 1
    $this->codeStore->append('$first = true;');
94 1
    $this->codeStore->append('foreach($rows as $row)');
95 1
    $this->codeStore->append('{');
96
97 1
    $this->codeStore->append('$sql .= (($first) ? \'values\' : \',     \').\'('.$fields.')\'.PHP_EOL;');
98
99 1
    $this->codeStore->append('$first = false;');
100 1
    $this->codeStore->append('}');
101 1
    $this->codeStore->append('$this->realQuery($sql);');
102 1
    $this->codeStore->append('}');
103 1
  }
104
105
  //--------------------------------------------------------------------------------------------------------------------
106
  /**
107
   * @inheritdoc
108
   */
109
  protected function writeRoutineFunctionLobFetchData(): void
110
  {
111
    // Nothing to do.
112
  }
113
114
  //--------------------------------------------------------------------------------------------------------------------
115
  /**
116
   * @inheritdoc
117
   */
118
  protected function writeRoutineFunctionLobReturnData(): void
119
  {
120
    // Nothing to do.
121
  }
122
123
  //--------------------------------------------------------------------------------------------------------------------
124
}
125
126
//----------------------------------------------------------------------------------------------------------------------
127