table.php
5.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
<?php
namespace Yoast\WP\Lib\Migrations;
use Exception;
/**
* Yoast migrations table class.
*/
class Table {
/**
* The adapter.
*
* @var Adapter
*/
private $adapter;
/**
* The name
*
* @var string
*/
private $name;
/**
* The options
*
* @var array
*/
private $options;
/**
* The SQL representation of this table.
*
* @var string
*/
private $sql = '';
/**
* Whether or not the table has been initialized.
*
* @var bool
*/
private $initialized = false;
/**
* The columns
*
* @var Column[]
*/
private $columns = [];
/**
* The primary keys.
*
* @var string[]
*/
private $primary_keys = [];
/**
* Whether or not to auto generate the id.
*
* @var bool
*/
private $auto_generate_id = true;
/**
* Creates an instance of the Adapter.
*
* @param Adapter $adapter The current adapter.
* @param string $name The table name.
* @param array $options The options.
*
* @throws Exception If invalid arguments are passed.
*/
public function __construct( $adapter, $name, $options = [] ) {
// Sanity checks.
if ( ! $adapter instanceof Adapter ) {
throw new Exception( 'Invalid MySQL Adapter instance.' );
}
if ( ! $name ) {
throw new Exception( "Invalid 'name' parameter" );
}
$this->adapter = $adapter;
$this->name = $name;
$this->options = $options;
$this->init_sql( $name, $options );
if ( \array_key_exists( 'id', $options ) ) {
if ( \is_bool( $options['id'] ) && $options['id'] === false ) {
$this->auto_generate_id = false;
}
// If its a string then we want to auto-generate an integer-based
// primary key with this name.
if ( \is_string( $options['id'] ) ) {
$this->auto_generate_id = true;
$this->primary_keys[] = $options['id'];
}
}
}
/**
* Create a column
*
* @param string $column_name The column name.
* @param string $type The column type.
* @param array $options The options.
*/
public function column( $column_name, $type, $options = [] ) {
// If there is already a column by the same name then silently fail and continue.
foreach ( $this->columns as $column ) {
if ( $column->name === $column_name ) {
return;
}
}
$column_options = [];
if ( \array_key_exists( 'primary_key', $options ) ) {
if ( $options['primary_key'] ) {
$this->primary_keys[] = $column_name;
}
}
if ( \array_key_exists( 'auto_increment', $options ) ) {
if ( $options['auto_increment'] ) {
$column_options['auto_increment'] = true;
}
}
$column_options = \array_merge( $column_options, $options );
$column = new Column( $this->adapter, $column_name, $type, $column_options );
$this->columns[] = $column;
}
/**
* Shortcut to create timestamps columns (default created_at, updated_at)
*
* @param string $created_column_name Created at column name.
* @param string $updated_column_name Updated at column name.
*/
public function timestamps( $created_column_name = 'created_at', $updated_column_name = 'updated_at' ) {
$this->column( $created_column_name, 'datetime' );
$this->column(
$updated_column_name,
'timestamp',
[
'null' => false,
'default' => 'CURRENT_TIMESTAMP',
'extra' => 'ON UPDATE CURRENT_TIMESTAMP',
]
);
}
/**
* Get all primary keys
*
* @return string
*/
private function keys() {
if ( \count( $this->primary_keys ) > 0 ) {
$lead = ' PRIMARY KEY (';
$quoted = [];
foreach ( $this->primary_keys as $key ) {
$quoted[] = \sprintf( '%s', $this->adapter->identifier( $key ) );
}
$primary_key_sql = ",\n" . $lead . \implode( ',', $quoted ) . ')';
return $primary_key_sql;
}
return '';
}
/**
* Table definition
*
* @param bool $wants_sql Whether or not to return SQL or execute the query. Defaults to false.
*
* @return bool|string
*
* @throws Exception If the table definition has not been intialized.
*/
public function finish( $wants_sql = false ) {
if ( ! $this->initialized ) {
throw new Exception( \sprintf( "Table Definition: '%s' has not been initialized", $this->name ) );
}
$opt_str = '';
if ( \is_array( $this->options ) && \array_key_exists( 'options', $this->options ) ) {
$opt_str = $this->options['options'];
}
else {
if ( isset( $this->adapter->db_info['charset'] ) ) {
$opt_str = ' DEFAULT CHARSET=' . $this->adapter->db_info['charset'];
}
else {
$opt_str = ' DEFAULT CHARSET=utf8';
}
}
$close_sql = \sprintf( ') %s;', $opt_str );
$create_table_sql = $this->sql;
if ( $this->auto_generate_id === true ) {
$this->primary_keys[] = 'id';
$primary_id = new Column(
$this->adapter,
'id',
'integer',
[
'unsigned' => true,
'null' => false,
'auto_increment' => true,
]
);
$create_table_sql .= $primary_id->to_sql() . ",\n";
}
$create_table_sql .= $this->columns_to_str();
$create_table_sql .= $this->keys() . $close_sql;
if ( $wants_sql ) {
return $create_table_sql;
}
return $this->adapter->execute_ddl( $create_table_sql );
}
/**
* Get SQL for all columns.
*
* @return string The SQL.
*/
private function columns_to_str() {
$fields = [];
$len = \count( $this->columns );
for ( $i = 0; $i < $len; $i++ ) {
$c = $this->columns[ $i ];
$fields[] = $c->__toString();
}
return \implode( ",\n", $fields );
}
/**
* Init create sql statement.
*
* @param string $name The name.
* @param array $options The options.
*/
private function init_sql( $name, $options ) {
// Are we forcing table creation? If so, drop it first.
if ( \array_key_exists( 'force', $options ) && $options['force'] === true ) {
$this->adapter->drop_table( $name );
}
$temp = '';
if ( \array_key_exists( 'temporary', $options ) ) {
$temp = ' TEMPORARY';
}
$create_sql = \sprintf( 'CREATE%s TABLE ', $temp );
$create_sql .= \sprintf( "%s (\n", $this->adapter->identifier( $name ) );
$this->sql .= $create_sql;
$this->initialized = true;
}
}