|
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 inserts a row. |
|
8
|
|
|
*/ |
|
9
|
|
|
class InsertRoutine extends BaseRoutine |
|
10
|
|
|
{ |
|
11
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
12
|
|
|
/** |
|
13
|
|
|
* @inheritdoc |
|
14
|
|
|
*/ |
|
15
|
|
|
protected function generateBody(): void |
|
16
|
|
|
{ |
|
17
|
|
|
$this->codeStore->append(sprintf('insert into %s(', $this->tableName)); |
|
18
|
|
|
|
|
19
|
|
|
$offset = mb_strlen($this->codeStore->getLastLine()); |
|
20
|
|
|
$columns = $this->tableColumnsWithoutAutoIncrement(); |
|
21
|
|
|
$padding = $this->maxColumnNameLength($columns); |
|
22
|
|
|
|
|
23
|
|
|
$first = true; |
|
24
|
|
|
foreach ($columns as $column) |
|
25
|
|
|
{ |
|
26
|
|
|
if ($first) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->codeStore->appendToLastLine(sprintf(' %s', $column['column_name'])); |
|
29
|
|
|
|
|
30
|
|
|
$first = false; |
|
31
|
|
|
} |
|
32
|
|
|
else |
|
33
|
|
|
{ |
|
34
|
|
|
$format = sprintf('%%-%ds %%s', $offset); |
|
35
|
|
|
$this->codeStore->append(sprintf($format, ',', $column['column_name'])); |
|
36
|
|
|
|
|
37
|
|
|
if ($column===end($this->tableColumns)) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->codeStore->appendToLastLine(' )'); |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$this->codeStore->append('values('); |
|
45
|
|
|
$offset = mb_strlen($this->codeStore->getLastLine()); |
|
46
|
|
|
|
|
47
|
|
|
$first = true; |
|
48
|
|
|
foreach ($columns as $column) |
|
49
|
|
|
{ |
|
50
|
|
|
if ($first) |
|
51
|
|
|
{ |
|
52
|
|
|
$this->codeStore->appendToLastLine(sprintf(' p_%s', $column['column_name'])); |
|
53
|
|
|
|
|
54
|
|
|
$first = false; |
|
55
|
|
|
} |
|
56
|
|
|
else |
|
57
|
|
|
{ |
|
58
|
|
|
$format = sprintf('%%-%ds p_%%-%ds', $offset, $padding); |
|
59
|
|
|
$this->codeStore->append(sprintf($format, ',', $column['column_name'])); |
|
60
|
|
|
|
|
61
|
|
|
if ($column===end($this->tableColumns)) |
|
62
|
|
|
{ |
|
63
|
|
|
$this->codeStore->appendToLastLine(' )'); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
$this->codeStore->append(';'); |
|
68
|
|
|
|
|
69
|
|
|
if ($this->checkAutoIncrement($this->tableColumns)) |
|
70
|
|
|
{ |
|
71
|
|
|
$this->codeStore->append(''); |
|
72
|
|
|
$this->codeStore->append('select last_insert_id();'); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
77
|
|
|
/** |
|
78
|
|
|
* @inheritDoc |
|
79
|
|
|
*/ |
|
80
|
|
|
protected function generateDocBlock(): void |
|
81
|
|
|
{ |
|
82
|
|
|
$this->generateDocBlockAllColumnsWithoutAutoIncrement(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
86
|
|
|
/** |
|
87
|
|
|
* Generates the function name and parameters of the stored routine. |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function generateRoutineDeclaration(): void |
|
90
|
|
|
{ |
|
91
|
|
|
$this->generateRoutineDeclarationAllColumnsWithoutAutoIncrement(); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
95
|
|
|
/** |
|
96
|
|
|
* @inheritDoc |
|
97
|
|
|
*/ |
|
98
|
|
|
protected function generateSqlDataAndDesignationType(): void |
|
99
|
|
|
{ |
|
100
|
|
|
$this->codeStore->append('modifies sql data'); |
|
101
|
|
|
|
|
102
|
|
|
if ($this->checkAutoIncrement($this->tableColumns)) |
|
103
|
|
|
{ |
|
104
|
|
|
$this->codeStore->append('-- type: singleton1'); |
|
105
|
|
|
$this->codeStore->append('-- return: int'); |
|
106
|
|
|
} |
|
107
|
|
|
else |
|
108
|
|
|
{ |
|
109
|
|
|
$this->codeStore->append('-- type: none'); |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
117
|
|
|
|