-
Notifications
You must be signed in to change notification settings - Fork 11
/
class-ct-database-schema.php
496 lines (415 loc) · 17.3 KB
/
class-ct-database-schema.php
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
<?php
/**
* Database Table Schema class
*
* @author GamiPress <contact@gamipress.com>, Ruben Garcia <rubengcdev@gamil.com>
*
* @since 1.0.0
*/
// Exit if accessed directly
defined( 'ABSPATH' ) || exit;
if ( ! class_exists( 'CT_DataBase_Schema' ) ) :
class CT_DataBase_Schema {
/**
* @var array Fields schema definition
*/
public $fields = array();
/**
* @var array Keys schema definition
*/
public $keys = array();
public $primary_key = '';
public function __construct( $schema ) {
$this->keys = array();
if( gettype( $schema ) === 'string' ) {
$schema = $this->string_schema_to_array( $schema );
}
if( is_array( $schema ) ) {
foreach( $schema as $field_id => $field_args ) {
$field_args = wp_parse_args( $field_args, array(
'type' => '',
'length' => 0,
'decimals' => 0, // numeric fields
'format' => '', // time fields
'options' => array(), // ENUM and SET types
'nullable' => false,
'unsigned' => null, // numeric field
'zerofill' => null, // numeric field
'binary' => null, // text fields
'charset' => false, // text fields
'collate' => false, // text fields
'default' => false,
'auto_increment' => false,
'unique' => false,
'primary_key' => false,
'key' => false,
) );
$this->fields[$field_id] = $field_args;
}
}
}
public function __toString() {
$fields_def = array();
foreach( $this->fields as $field_id => $field_args ) {
// Turn field array to schema
$fields_def[] = $this->field_array_to_schema( $field_id, $field_args );
}
// Setup PRIMARY KEY definition
$sql = implode( ', ', $fields_def ) . ', '
. 'PRIMARY KEY (`' . $this->primary_key . '`)'; // Add two spaces to avoid issues
// Setup KEY definitions
if( ! empty( $this->keys ) ) {
$sql .= ', ' . implode( ', ', $this->keys );
}
return $sql;
}
/**
* Convert a field definition into a schema string
*
* @param string $field_id
* @param array $field_args
*
* @return string A schema string like: field type(length,decimals) UNSIGNED NOT NULL
*/
public function field_array_to_schema( $field_id, $field_args ) {
$schema = '';
// Field name
$schema .= '`' . $field_id . '` ';
// Type definition
$schema .= $field_args['type'];
// Type definition args
switch( strtoupper( $field_args['type'] ) ) {
case 'ENUM':
case 'SET':
if( is_array( $field_args['options'] ) ) {
$schema .= '(' . implode( ',', $field_args['options'] ) . ')';
} else {
$schema .= '(' . $field_args['options'] . ')';
}
break;
case 'REAL':
case 'DOUBLE':
case 'FLOAT':
case 'DECIMAL':
case 'NUMERIC':
if( $field_args['length'] !== 0 ) {
$schema .= '(' . $field_args['length'] . ',' . $field_args['decimals'] . ')';
}
break;
case 'TIME':
case 'TIMESTAMP':
case 'DATETIME':
if( $field_args['format'] !== '' ) {
$schema .= '(' . $field_args['format'] . ')';
}
break;
default:
if( $field_args['length'] !== 0 ) {
$schema .= '(' . $field_args['length'] . ')';
}
break;
}
$schema .= ' ';
// Type specific definitions
switch( strtoupper( $field_args['type'] ) ) {
case 'TINYINT':
case 'SMALLINT':
case 'MEDIUMINT':
case 'INT':
case 'INTEGER':
case 'BIGINT':
case 'REAL':
case 'DOUBLE':
case 'FLOAT':
case 'DECIMAL':
case 'NUMERIC':
// UNSIGNED definition
if( $field_args['unsigned'] !== null ) {
if( $field_args['unsigned'] ) {
$schema .= 'UNSIGNED ';
} else {
$schema .= 'SIGNED ';
}
}
// ZEROFILL definition
if( $field_args['zerofill'] !== null && $field_args['zerofill'] ) {
$schema .= 'ZEROFILL ';
}
break;
case 'CHAR':
case 'VARCHAR':
case 'TINYTEXT':
case 'TEXT':
case 'MEDIUMTEXT':
case 'LONGTEXT':
case 'ENUM':
case 'SET':
// BINARY definition
if( $field_args['binary'] !== null && $field_args['binary']) {
$schema .= 'BINARY ';
}
// CHARACTER SET definition
if( $field_args['charset'] !== false ) {
$schema .= 'CHARACTER SET ' . $field_args['charset'] . ' ';
}
// COLLATE definition
if( $field_args['collate'] !== false ) {
$schema .= 'COLLATE ' . $field_args['collate'] . ' ';
}
break;
}
// NULL definition
if( $field_args['nullable'] ) {
$schema .= 'NULL ';
} else {
$schema .= 'NOT NULL ';
}
// DEFAULT definition
if( $field_args['default'] !== false ) {
if( gettype( $field_args['default'] ) === 'string' ) {
$field_args['default'] = "'" . $field_args['default'] . "'";
}
if( $field_args['default'] === null ) {
$field_args['default'] = 'NULL';
}
$schema .= 'DEFAULT ' . $field_args['default'] . ' ';
}
// UNIQUE definition
if( $field_args['unique'] ) {
$schema .= 'UNIQUE ';
}
// AUTO_INCREMENT definition
if( $field_args['auto_increment'] ) {
$schema .= 'AUTO_INCREMENT ';
}
// PRIMARY KEY definition
if( $field_args['primary_key'] ) {
$this->primary_key = $field_id;
}
// KEY definition
if( $field_args['key'] ) {
/*
* Indexes have a maximum size of 767 bytes. WordPress 4.2 was moved to utf8mb4, which uses 4 bytes per character.
* This means that an index which used to have room for floor(767/3) = 255 characters, now only has room for floor(767/4) = 191 characters.
*/
$max_index_length = 191;
$length = min( $field_args['length'], $max_index_length );
// Ensure that length has a value
if( $length === 0 ) {
$length = $max_index_length;
}
if( $this->is_numeric( $field_args['type'] ) || strtoupper( $field_args['type'] ) === 'DATETIME' ) {
$this->keys[] = 'KEY `' . $field_id . '`(`' . $field_id . '`)';
} else {
$this->keys[] = 'KEY `' . $field_id . '`(`' . $field_id . '`(' . $length . '))';
}
}
return $schema;
}
/**
* Convert a schema string into an array of fields definitions
*
* @param string $schema
*
* @return array
*/
public function string_schema_to_array( $schema ) {
$schema_array = explode( ',', trim( $schema ) );
$new_schema = array();
foreach( $schema_array as $field_def ) {
$field_id = '';
$field_args = array();
$field_def_parts = explode( ' ', trim( $field_def ) );
foreach( $field_def_parts as $index => $field_def_part ) {
if( $index === 0 ) {
// Field id at index 0
if( $field_def_part !== 'PRIMARY' && $field_def_part !== 'KEY' ) {
$field_id = $field_def_part;
continue;
}
// PRIMARY KEY at index 0
if( $field_def_part === 'PRIMARY' ) {
if( isset( $field_def_parts[$index+1] ) && strtoupper( $field_def_parts[$index+1] ) === 'KEY' && isset( $field_def_parts[$index+2] ) ) {
$primary_key_field = str_replace( array( '(', ')' ), '', $field_def_parts[$index+2] );
if( isset( $this->fields[$primary_key_field] ) ) {
$this->fields[$primary_key_field]['primary_key'] = true;
continue;
}
}
}
}
// NOT NULL definition
if( strtoupper( $field_def_part ) === 'NOT' ) {
if( isset( $field_def_parts[$index+1] ) && strtoupper( $field_def_parts[$index+1] ) === 'NULL' ) {
$field_args['nullable'] = false;
continue;
}
}
// NULL definition
if( strtoupper( $field_def_part ) === 'NULL' ) {
if( isset( $field_def_parts[$index-1] ) && strtoupper( $field_def_parts[$index-1] ) !== 'NOT' ) {
$field_args['nullable'] = true;
continue;
}
}
// UNSIGNED definition
if( strtoupper( $field_def_part ) === 'UNSIGNED' ) {
$field_args['unsigned'] = true;
continue;
}
// SIGNED definition
if( strtoupper( $field_def_part ) === 'SIGNED' ) {
$field_args['unsigned'] = false;
continue;
}
// ZEROFILL definition
if( strtoupper( $field_def_part ) === 'ZEROFILL' ) {
$field_args['zerofill'] = true;
continue;
}
// BINARY definition
if( strtoupper( $field_def_part ) === 'BINARY' ) {
$field_args['binary'] = true;
continue;
}
// CHARACTER SET definition
if( strtoupper( $field_def_part ) === 'CHARACTER' ) {
if( isset( $field_def_parts[$index+1] ) && strtoupper( $field_def_parts[$index+1] ) === 'SET' ) {
$field_args['charset'] = $field_def_parts[$index+2];
continue;
}
}
// COLLATE definition
if( strtoupper( $field_def_part ) === 'COLLATE' ) {
if( isset( $field_def_parts[$index+1] ) ) {
$field_args['collate'] = $field_def_parts[$index+1];
continue;
}
}
// DEFAULT definition
if( strtoupper( $field_def_part ) === 'DEFAULT' ) {
if( isset( $field_def_parts[$index+1] ) ) {
$field_args['default'] = $field_def_parts[$index+1];
continue;
}
}
// UNIQUE definition
if( strtoupper( $field_def_part ) === 'UNIQUE' ) {
$field_args['unique'] = true;
continue;
}
// AUTO_INCREMENT definition
if( strtoupper( $field_def_part ) === 'AUTO_INCREMENT' ) {
$field_args['auto_increment'] = true;
continue;
}
// PRIMARY KEY definition
if( strtoupper( $field_def_part ) === 'PRIMARY' ) {
if( isset( $field_def_parts[$index+1] ) && strtoupper( $field_def_parts[$index+1] ) === 'KEY' ) {
$field_args['primary_key'] = true;
continue;
}
}
$type_parts = explode( '(', $field_def_part );
// Possible field type
if( in_array( strtoupper( $field_def_part ), $this->allowed_field_types() ) ) {
$field_args['type'] = strtoupper( $field_def_part );
continue;
} else if( isset( $type_parts[0] ) && in_array( strtoupper( $type_parts[0] ), $this->allowed_field_types() ) ) {
$field_args['type'] = strtoupper( $type_parts[0] );
if( isset( $type_parts[1] ) ) {
$type_def = explode( ',', str_replace(')', '', $type_parts[1] ) );
switch( $field_args['type'] ) {
case 'ENUM':
case 'SET':
$field_args['options'] = $type_def;
break;
case 'REAL':
case 'DOUBLE':
case 'FLOAT':
case 'DECIMAL':
case 'NUMERIC':
$field_args['length'] = $type_def[0];
if( isset( $type_def[1] ) ) {
$field_args['decimals'] = $type_def[1];
}
break;
case 'TIME':
case 'TIMESTAMP':
case 'DATETIME':
$field_args['format'] = $type_def[0];
break;
default:
$field_args['length'] = $type_def[0];
break;
}
}
}
}
if( ! empty( $field_id ) && ! empty( $field_args ) ) {
$new_schema[$field_id] = $field_args;
}
}
return $new_schema;
}
/**
* Get the list of allowed types
*
* @return array
*/
public function allowed_field_types() {
return array(
'BIT',
'TINYINT',
'SMALLINT',
'MEDIUMINT',
'INT',
'INTEGER',
'BIGINT',
'REAL',
'DOUBLE',
'FLOAT',
'DECIMAL',
'NUMERIC',
'DATE',
'TIME',
'TIMESTAMP',
'DATETIME',
'YEAR',
'CHAR',
'VARCHAR',
'BINARY',
'VARBINARY',
'TINYBLOB',
'BLOB',
'MEDIUMBLOB',
'LONGBLOB',
'TINYTEXT',
'TEXT',
'JSON'
);
}
/**
* Check if given type is numeric
*
* @param string $type
*
* @return bool
*/
public function is_numeric( $type ) {
return in_array( strtoupper( $type ), array(
'TINYINT',
'SMALLINT',
'MEDIUMINT',
'INT',
'INTEGER',
'BIGINT',
'REAL',
'DOUBLE',
'FLOAT',
'DECIMAL',
'NUMERIC',
) );
}
}
endif;