|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace SetBased\Audit\MySql\Sql; |
|
5
|
|
|
|
|
6
|
|
|
use SetBased\Audit\Metadata\TableColumnsMetadata; |
|
7
|
|
|
use SetBased\Helper\CodeStore\MySqlCompoundSyntaxCodeStore; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Class for creating SQL statements for adding new columns to an audit table. |
|
11
|
|
|
*/ |
|
12
|
|
|
class AlterAuditTableAddColumns |
|
13
|
|
|
{ |
|
14
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
15
|
|
|
/** |
|
16
|
|
|
* The name of the audit schema. |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private string $auditSchemaName; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The array of new columns for adding to table. |
|
24
|
|
|
* |
|
25
|
|
|
* @var TableColumnsMetadata |
|
26
|
|
|
*/ |
|
27
|
|
|
private TableColumnsMetadata $columns; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* The name of the audit table. |
|
31
|
|
|
* |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
private string $tableName; |
|
35
|
|
|
|
|
36
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
37
|
|
|
/** |
|
38
|
|
|
* Object constructor. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $auditSchemaName The name of the audit schema. |
|
41
|
|
|
* @param string $tableName The name of the table. |
|
42
|
|
|
* @param TableColumnsMetadata $columns The metadata of the new columns of the audit table (i.e. the audit |
|
43
|
|
|
* columns and columns of the data table). |
|
44
|
|
|
*/ |
|
45
|
3 |
|
public function __construct(string $auditSchemaName, string $tableName, TableColumnsMetadata $columns) |
|
46
|
|
|
{ |
|
47
|
3 |
|
$this->auditSchemaName = $auditSchemaName; |
|
48
|
3 |
|
$this->tableName = $tableName; |
|
49
|
3 |
|
$this->columns = $columns; |
|
50
|
3 |
|
} |
|
51
|
|
|
|
|
52
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
53
|
|
|
/** |
|
54
|
|
|
* Returns a SQL statement for adding new columns to the audit table. |
|
55
|
|
|
* |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
3 |
|
public function buildStatement(): string |
|
59
|
|
|
{ |
|
60
|
3 |
|
$code = new MySqlCompoundSyntaxCodeStore(); |
|
61
|
|
|
|
|
62
|
3 |
|
$code->append(sprintf('alter table `%s`.`%s`', $this->auditSchemaName, $this->tableName)); |
|
63
|
3 |
|
foreach ($this->columns->getColumns() as $column) |
|
64
|
|
|
{ |
|
65
|
3 |
|
$code->append(sprintf(' add `%s` %s', $column->getName(), $column->getColumnAuditDefinition()), false); |
|
66
|
3 |
|
$after = $column->getProperty('after'); |
|
67
|
3 |
|
if (isset($after)) |
|
68
|
|
|
{ |
|
69
|
3 |
|
$code->appendToLastLine(sprintf(' after `%s`', $after)); |
|
70
|
|
|
} |
|
71
|
|
|
else |
|
72
|
|
|
{ |
|
73
|
|
|
$code->appendToLastLine(' first'); |
|
74
|
|
|
} |
|
75
|
3 |
|
$columns = $this->columns->getColumns(); |
|
76
|
3 |
|
if (end($columns)!==$column) |
|
77
|
|
|
{ |
|
78
|
|
|
$code->appendToLastLine(','); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
3 |
|
return $code->getCode(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
|
89
|
|
|
|