|
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
|
|
|
|