|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace SetBased\Audit\MySql\Sql; |
|
5
|
|
|
|
|
6
|
|
|
use SetBased\Audit\Metadata\TableColumnsMetadata; |
|
7
|
|
|
use SetBased\Audit\MySql\AuditDataLayer; |
|
8
|
|
|
use SetBased\Helper\CodeStore\MySqlCompoundSyntaxCodeStore; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class for creating SQL statements for creating audit tables. |
|
12
|
|
|
*/ |
|
13
|
|
|
class CreateAuditTable |
|
14
|
|
|
{ |
|
15
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
16
|
|
|
/** |
|
17
|
|
|
* The name of the audit schema. |
|
18
|
|
|
* |
|
19
|
|
|
* @var string |
|
20
|
|
|
*/ |
|
21
|
|
|
private string $auditSchemaName; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The name of the table. |
|
25
|
|
|
* |
|
26
|
|
|
* @var TableColumnsMetadata |
|
27
|
|
|
*/ |
|
28
|
|
|
private TableColumnsMetadata $columns; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The name of the data schema. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
private string $dataSchemaName; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The name of the table. |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
private string $tableName; |
|
43
|
|
|
|
|
44
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
45
|
|
|
/** |
|
46
|
|
|
* Object constructor. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $dataSchemaName The name of the data schema. |
|
49
|
|
|
* @param string $auditSchemaName The name of the audit schema. |
|
50
|
|
|
* @param string $tableName The name of the table. |
|
51
|
|
|
* @param TableColumnsMetadata $columns The metadata of the columns of the audit table (i.e. the audit |
|
52
|
|
|
* columns and columns of the data table). |
|
53
|
|
|
*/ |
|
54
|
27 |
|
public function __construct(string $dataSchemaName, |
|
55
|
|
|
string $auditSchemaName, |
|
56
|
|
|
string $tableName, |
|
57
|
|
|
TableColumnsMetadata $columns) |
|
58
|
|
|
{ |
|
59
|
27 |
|
$this->dataSchemaName = $dataSchemaName; |
|
60
|
27 |
|
$this->auditSchemaName = $auditSchemaName; |
|
61
|
27 |
|
$this->tableName = $tableName; |
|
62
|
27 |
|
$this->columns = $columns; |
|
63
|
27 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
66
|
|
|
/** |
|
67
|
|
|
* Returns a SQL statement for creating the audit table. |
|
68
|
|
|
* |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
27 |
|
public function buildStatement(): string |
|
72
|
|
|
{ |
|
73
|
27 |
|
$code = new MySqlCompoundSyntaxCodeStore(); |
|
74
|
|
|
|
|
75
|
27 |
|
$code->append(sprintf('create table `%s`.`%s`', $this->auditSchemaName, $this->tableName)); |
|
76
|
|
|
|
|
77
|
|
|
// Create SQL for columns. |
|
78
|
27 |
|
$code->append('('); |
|
79
|
27 |
|
$code->append($this->getColumnDefinitions()); |
|
80
|
|
|
|
|
81
|
|
|
// Create SQL for table options. |
|
82
|
27 |
|
$tableOptions = AuditDataLayer::$dl->getTableOptions($this->dataSchemaName, $this->tableName); |
|
83
|
27 |
|
$code->append(sprintf(') engine=%s character set=%s collate=%s', |
|
84
|
27 |
|
$tableOptions['engine'], |
|
85
|
27 |
|
$tableOptions['character_set_name'], |
|
86
|
27 |
|
$tableOptions['table_collation'])); |
|
87
|
|
|
|
|
88
|
27 |
|
return $code->getCode(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
92
|
|
|
/** |
|
93
|
|
|
* Returns an array with SQL code for column definitions. |
|
94
|
|
|
* |
|
95
|
|
|
* @return string[] |
|
96
|
|
|
*/ |
|
97
|
27 |
|
private function getColumnDefinitions(): array |
|
98
|
|
|
{ |
|
99
|
27 |
|
$lines = []; |
|
100
|
|
|
|
|
101
|
27 |
|
$columns = $this->columns->getColumns(); |
|
102
|
27 |
|
$maxLength = $this->columns->getLongestColumnNameLength(); |
|
103
|
27 |
|
foreach ($columns as $column) |
|
104
|
|
|
{ |
|
105
|
27 |
|
$name = $column->getName(); |
|
106
|
27 |
|
$filler = str_repeat(' ', $maxLength - mb_strlen($name) + 1); |
|
107
|
|
|
|
|
108
|
27 |
|
$line = sprintf(' `%s`%s%s', $name, $filler, $column->getColumnAuditDefinition()); |
|
109
|
|
|
|
|
110
|
27 |
|
if (end($columns)!==$column) |
|
111
|
|
|
{ |
|
112
|
27 |
|
$line .= ','; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
27 |
|
$lines[] = $line; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
27 |
|
return $lines; |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
|
125
|
|
|
|