|
1
|
|
|
<?php |
|
2
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
|
3
|
|
|
namespace SetBased\Audit\MySql\Sql; |
|
4
|
|
|
|
|
5
|
|
|
use SetBased\Audit\MySql\AuditDataLayer; |
|
6
|
|
|
use SetBased\Audit\MySql\Metadata\TableColumnsMetadata; |
|
7
|
|
|
use SetBased\Helper\CodeStore\MySqlCompoundSyntaxCodeStore; |
|
8
|
|
|
|
|
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 $auditSchemaName; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The name of the table. |
|
25
|
|
|
* |
|
26
|
|
|
* @var TableColumnsMetadata |
|
27
|
|
|
*/ |
|
28
|
|
|
private $columns; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The name of the data schema. |
|
32
|
|
|
* |
|
33
|
|
|
* @var string |
|
34
|
|
|
*/ |
|
35
|
|
|
private $dataSchemaName; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* The name of the table. |
|
39
|
|
|
* |
|
40
|
|
|
* @var string |
|
41
|
|
|
*/ |
|
42
|
|
|
private $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
|
13 |
|
public function __construct($dataSchemaName, |
|
55
|
|
|
$auditSchemaName, |
|
56
|
|
|
$tableName, |
|
57
|
|
|
$columns) |
|
58
|
|
|
{ |
|
59
|
13 |
|
$this->dataSchemaName = $dataSchemaName; |
|
60
|
13 |
|
$this->auditSchemaName = $auditSchemaName; |
|
61
|
13 |
|
$this->tableName = $tableName; |
|
62
|
13 |
|
$this->columns = $columns; |
|
63
|
13 |
|
} |
|
64
|
|
|
|
|
65
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
66
|
|
|
/** |
|
67
|
|
|
* Returns a SQL statement for creating the audit table. |
|
68
|
|
|
* |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
13 |
|
public function buildStatement() |
|
72
|
|
|
{ |
|
73
|
13 |
|
$code = new MySqlCompoundSyntaxCodeStore(); |
|
74
|
|
|
|
|
75
|
13 |
|
$code->append(sprintf('create table `%s`.`%s`', $this->auditSchemaName, $this->tableName)); |
|
76
|
|
|
|
|
77
|
|
|
// Create SQL for columns. |
|
78
|
13 |
|
$code->append('('); |
|
79
|
13 |
|
$code->append($this->getColumnDefinitions()); |
|
80
|
|
|
|
|
81
|
|
|
// Create SQL for table options. |
|
82
|
13 |
|
$tableOptions = AuditDataLayer::getTableOptions($this->dataSchemaName, $this->tableName); |
|
83
|
13 |
|
$code->append(sprintf(') engine=%s character set=%s collate=%s', |
|
84
|
13 |
|
$tableOptions['engine'], |
|
85
|
13 |
|
$tableOptions['character_set_name'], |
|
86
|
13 |
|
$tableOptions['table_collation'])); |
|
87
|
|
|
|
|
88
|
13 |
|
return $code->getCode(); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
92
|
|
|
/** |
|
93
|
|
|
* Returns an array with SQL code for column definitions. |
|
94
|
|
|
* |
|
95
|
|
|
* @return string[] |
|
96
|
|
|
*/ |
|
97
|
13 |
|
private function getColumnDefinitions() |
|
98
|
|
|
{ |
|
99
|
13 |
|
$lines = []; |
|
100
|
|
|
|
|
101
|
|
|
// Base format on column with longest name. |
|
102
|
13 |
|
$format = $this->getFormat(); |
|
103
|
|
|
|
|
104
|
13 |
|
$columns = $this->columns->getColumns(); |
|
105
|
13 |
|
foreach ($columns as $column) |
|
106
|
|
|
{ |
|
107
|
13 |
|
$line = sprintf($format, '`'.$column->getProperty('column_name').'`', $column->getProperty('column_type')); |
|
108
|
|
|
|
|
109
|
|
|
// Timestamps require special settings for null values. |
|
110
|
13 |
|
if ($column->getProperty('column_type')=='timestamp') |
|
111
|
|
|
{ |
|
112
|
2 |
|
$line .= ' null'; |
|
113
|
|
|
|
|
114
|
2 |
|
if ($column->getProperty('is_nullable')=='YES') |
|
115
|
|
|
{ |
|
116
|
|
|
$line .= ' default null'; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
// Set character set and collation. |
|
121
|
13 |
|
if ($column->getProperty('character_set_name')) |
|
122
|
|
|
{ |
|
123
|
7 |
|
$line .= ' character set '; |
|
124
|
7 |
|
$line .= $column->getProperty('character_set_name'); |
|
125
|
|
|
} |
|
126
|
13 |
|
if ($column->getProperty('collation_name')) |
|
127
|
|
|
{ |
|
128
|
7 |
|
$line .= ' collate '; |
|
129
|
7 |
|
$line .= $column->getProperty('collation_name'); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
// Set nullable. |
|
133
|
13 |
|
if ($column->getProperty('is_nullable')=='NO') |
|
134
|
|
|
{ |
|
135
|
6 |
|
$line .= ' not null'; |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
13 |
|
if (end($columns)!==$column) |
|
139
|
|
|
{ |
|
140
|
13 |
|
$line .= ','; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
13 |
|
$lines[] = $line; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
13 |
|
return $lines; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
150
|
|
|
/** |
|
151
|
|
|
* Returns the format specifier for printing column names and column types |
|
152
|
|
|
* |
|
153
|
|
|
* @return string |
|
154
|
|
|
*/ |
|
155
|
13 |
|
private function getFormat() |
|
156
|
|
|
{ |
|
157
|
13 |
|
$width = 0; |
|
158
|
13 |
|
foreach ($this->columns->getColumns() as $column) |
|
159
|
|
|
{ |
|
160
|
13 |
|
$width = max($width, mb_strlen($column->getProperty('column_name'))); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
13 |
|
return sprintf(' %%-%ds %%s', $width + 2); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
//-------------------------------------------------------------------------------------------------------------------- |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
//---------------------------------------------------------------------------------------------------------------------- |
|
170
|
|
|
|