-
Notifications
You must be signed in to change notification settings - Fork 0
/
Schema.php
821 lines (719 loc) · 22.6 KB
/
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
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
<?php
/**
* @package vdm/data
*
* @created 4th September, 2022
* @author Llewellyn van der Merwe <https://dev.vdm.io>
* @git VDM Data Library <https://git.vdm.dev/joomla/vdm-data>
* @copyright Copyright (C) 2015 Vast Development Method. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
namespace VastDevelopmentMethod\Joomla\Abstraction;
use Joomla\CMS\Factory;
use Joomla\CMS\Version;
use VastDevelopmentMethod\Joomla\Interfaces\Tableinterface as Table;
use VastDevelopmentMethod\Joomla\Interfaces\SchemaInterface;
/**
* Schema Checking
*
* @since 3.2.1
*/
abstract class Schema implements SchemaInterface
{
/**
* The Table Class.
*
* @var Table
* @since 3.2.1
*/
protected Table $table;
/**
* The Database Class
*
* @since 3.2.1
*/
protected $db;
/**
* The local tables
*
* @var array
* @since 3.2.1
*/
private array $tables;
/**
* The component table prefix
*
* @var string
* @since 3.2.1
*/
private string $prefix;
/**
* The field unique keys
*
* @var array
* @since 3.2.1
*/
private array $uniqueKeys;
/**
* The field keys
*
* @var array
* @since 3.2.1
*/
private array $keys;
/**
* The current table columns
*
* @var array
* @since 3.2.1
*/
private array $columns;
/**
* The success messages of the action
*
* @var array
* @since 3.2.1
*/
private array $success;
/**
* Current Joomla Version We are IN
*
* @var int
* @since 3.2.1
**/
protected $currentVersion;
/**
* Constructor.
*
* @param Table $table The Table Class.
*
* @since 3.2.1
* @throws \Exception If the database fails
*/
public function __construct(Table $table)
{
$this->table = $table;
try {
// set the database object
$this->db = Factory::getDbo();
// get current component tables
$this->tables = $this->db->getTableList();
// set the component table
$this->prefix = $this->db->getPrefix() . $this->getCode();
// set the current version
$this->currentVersion = Version::MAJOR_VERSION;
} catch (\Exception $e) {
throw new \Exception("Error: failed to initialize schema class due to a database error.");
}
}
/**
* Check and update database schema for missing fields or tables.
*
* @return array The array of successful updates/actions, if empty no update/action was taken.
* @since 3.2.1
* @throws \Exception If there is an error during the update process.
*/
public function update(): array
{
try {
$this->success = [
"Success: scan of the component tables started."
];
foreach ($this->table->tables() as $table)
{
$this->uniqueKeys = [];
$this->keys = [];
if ($this->tableExists($table))
{
$this->updateSchema($table);
}
else
{
$this->createTable($table);
}
}
} catch (\Exception $e) {
throw new \Exception("Error: updating database schema. " . $e->getMessage());
}
if (count($this->success) == 1)
{
$this->success[] = "Success: scan of the component tables completed with no update needed.";
}
else
{
$this->success[] = "Success: scan of the component tables completed.";
}
return $this->success;
}
/**
* Get the targeted component code
*
* @return string
* @since 3.2.1
*/
abstract protected function getCode(): string;
/**
* Check if a table exists in the database.
*
* @param string $table The name of the table to check.
*
* @return bool True if table exists, False otherwise.
* @since 3.2.1
*/
protected function tableExists(string $table): bool
{
return in_array($this->getTable($table), $this->tables);
}
/**
* Update the schema of an existing table.
*
* @param string $table The table to update.
*
* @return void
* @since 3.2.1
* @throws \Exception If there is an error while updating the schema.
*/
public function updateSchema(string $table): void
{
try {
$existingColumns = $this->getExistingColumns($table);
$expectedColumns = $this->table->fields($table, true);
$missingColumns = array_diff($expectedColumns, $existingColumns);
if (!empty($missingColumns))
{
$this->addMissingColumns($table, $missingColumns);
}
$this->checkColumnsDataType($table, $expectedColumns);
} catch (\Exception $e) {
throw new \Exception("Error: updating schema for $table table. " . $e->getMessage());
}
if (!empty($missingColumns))
{
$column_s = (count($missingColumns) == 1) ? 'column' : 'columns';
$missingColumns = implode(', ', $missingColumns);
$this->success[] = "Success: added missing ($missingColumns) $column_s to $table table.";
}
}
/**
* Create a table with all necessary fields.
*
* @param string $table The name of the table to create.
*
* @return void
* @since 3.2.1
* @throws \Exception If there is an error creating the table.
*/
public function createTable(string $table): void
{
try {
$columns = [];
$fields = $this->table->fields($table, true);
$createTable = 'CREATE TABLE IF NOT EXISTS ' . $this->db->quoteName($this->getTable($table));
foreach ($fields as $field)
{
if (($def = $this->getColumnDefinition($table, $field)) !== null)
{
$columns[] = $def;
}
}
$columnDefinitions = implode(', ', $columns);
$keys = $this->getTableKeys();
$createTableSql = "$createTable ($columnDefinitions, $keys)";
$this->db->setQuery($createTableSql);
$this->db->execute();
} catch (\Exception $e) {
throw new \Exception("Error: failed to create missing $table table. " . $e->getMessage());
}
$this->success[] = "Success: created missing $table table.";
}
/**
* Fetch existing columns from a database table.
*
* @param string $table The name of the table.
*
* @return array An array of column names.
* @since 3.2.1
*/
protected function getExistingColumns(string $table): array
{
$this->columns = $this->db->getTableColumns($this->getTable($table), false);
return array_keys($this->columns);
}
/**
* Add missing columns to a table.
*
* @param string $table The table to update.
* @param array $columns List of missing columns/fields.
*
* @return void
* @since 3.2.1
* @throws \Exception If there is an error adding columns.
*/
protected function addMissingColumns(string $table, array $columns): void
{
try {
$query = $this->db->getQuery(true);
$alterTable = 'ALTER TABLE ' . $this->db->quoteName($this->getTable($table)) . ' ';
// Start an ALTER TABLE query
$alterQueries = [];
foreach ($columns as $column)
{
if (($def = $this->getColumnDefinition($table, $column)) !== null)
{
$alterQueries[] = " ADD " . $def;
}
}
$this->db->setQuery($alterTable . implode(', ', $alterQueries));
$this->db->execute();
} catch (\Exception $e) {
$column_s = (count($columns) == 1) ? 'column' : 'columns';
$columns = implode(', ', $columns);
throw new \Exception("Error: failed to add ($columns) $column_s to $table table. " . $e->getMessage());
}
}
/**
* Validate and update the data type of existing fields/columns
*
* @param string $table The table to update.
* @param array $columns List of columns/fields to check.
*
* @return void
* @since 3.2.1
*/
protected function checkColumnsDataType(string $table, array $columns): void
{
$requireUpdate = [];
foreach ($columns as $column)
{
$current = $this->columns[$column] ?? null;
if ($current === null || ($expected = $this->table->get($table, $column, 'db')) === null)
{
continue;
}
// check if the data type and size match
if ($this->isDataTypeChangeSignificant($current->Type, $expected['type']))
{
$requireUpdate[$column] = [
'column' => $column,
'current' => $current->Type,
'expected' => $expected['type']
];
}
// check if update of default values is needed
elseif ($this->checkDefault($table, $column))
{
$requireUpdate[$column] = [
'column' => $column,
'current' => $current->Type,
'expected' => $expected['type']
];
}
// check if update of null is needed
elseif ($this->checkNull($table, $column))
{
$requireUpdate[$column] = [
'column' => $column,
'current' => $current->Type,
'expected' => $expected['type']
];
}
}
if (!empty($requireUpdate))
{
$this->updateColumnsDataType($table, $requireUpdate);
}
}
/**
* Generates a SQL snippet for defining a table column, incorporating column type,
* default value, nullability, and auto-increment properties.
*
* @param string $table The table name to be used.
* @param string $field The field name in the table to generate SQL for.
*
* @return string|null The SQL snippet for the column definition.
* @since 3.2.1
* @throws \Exception If the schema details cannot be retrieved or the SQL statement cannot be constructed properly.
*/
protected function getColumnDefinition(string $table, string $field): ?string
{
try {
// Retrieve the database schema details for the specified table and field
if (($db = $this->table->get($table, $field, 'db')) === null)
{
return null;
}
// Prepare the column name
$column_name = $this->db->quoteName($field);
$db['name'] = $field;
// Prepare the type and default value SQL statement
$type = $db['type'] ?? 'TEXT';
$db_default = isset($db['default']) ? $db['default'] : null;
$default = $this->getDefaultValue($type, $db_default);
// Prepare the null switch, and auto increment statement
$null_switch = !empty($db['null_switch']) ? ' ' . $db['null_switch'] : '';
// Prepare the auto increment statement
$auto_increment = !empty($db['auto_increment']) ? " AUTO_INCREMENT" : '';
// If there's a default value, the column should not be nullable
if ($default !== '')
{
$null_switch = '';
}
$this->setKeys($db);
// Assemble the SQL snippet for the column definition
return "{$column_name} {$type}{$null_switch}{$auto_increment}{$default}";
} catch (\Exception $e) {
throw new \Exception("Error: failed to generate column definition for ($table.$field). " . $e->getMessage());
}
}
/**
* Check and Update the default values if needed, including existing data adjustments
*
* @param string $table The table to update.
* @param string $column The column/field to check.
*
* @return bool
* @since 3.2.1
*/
protected function checkDefault(string $table, string $column): bool
{
// Retrieve the expected column configuration
$expected = $this->table->get($table, $column, 'db');
// Skip updates if the column is auto_increment
if (isset($expected['auto_increment']) && $expected['auto_increment'] === true)
{
return false;
}
// Retrieve the current column configuration
$current = $this->columns[$column];
// Determine the new default value based on the expected settings
$type = $expected['type'] ?? 'TEXT';
$db_default = isset($expected['default']) ? $expected['default'] : null;
$newDefault = $this->getDefaultValue($type, $db_default, true);
// First, adjust existing rows to conform to the new default if necessary
if (is_numeric($newDefault) && $this->adjustExistingDefaults($table, $column, $current->Default, $newDefault))
{
$this->success[] = "Success: updated the ($column) defaults in $table table.";
return true;
}
if (is_string($expected['default']) && strtoupper($expected['default']) === 'EMPTY' &&
is_string($current->Default) && strpos($current->Default, 'EMPTY') !== false)
{
return true; // little fix
}
return false;
}
/**
* Check and Update the null value if needed, including existing data adjustments
*
* @param string $table The table to update.
* @param string $column The column/field to check.
*
* @return bool
* @since 3.2.2
*/
protected function checkNull(string $table, string $column): bool
{
// Retrieve the expected column configuration
$expected = $this->table->get($table, $column, 'db');
// Skip updates if the null_switch is not set
if (!isset($expected['null_switch']))
{
return false;
}
// Retrieve the current column configuration
$current = $this->columns[$column];
// Skip updates if the Null is not set
if (!isset($current->Null))
{
return false;
}
// set the expected NULL
$expected_null = 'NO';
if ($expected['null_switch'] === "NULL")
{
$expected_null = 'YES';
}
// set the current NULL
$current_null = $current->Null;
// Prepare the type and default value SQL statement
$type = $expected['type'] ?? 'TEXT';
$db_default = isset($expected['default']) ? $expected['default'] : null;
$default = $this->getDefaultValue($type, $db_default, true);
// check the null values if they match
if ($current_null !== $expected_null && $current_null === 'NO' && empty($default))
{
$this->success[] = "Success: updated the ($column) null state in $table table.";
return true;
}
return false;
}
/**
* Update the data type of the given fields.
*
* @param string $table The table to update.
* @param array $columns List of columns/fields that must be updated.
*
* @return void
* @since 3.2.1
*/
protected function updateColumnsDataType(string $table, array $columns): void
{
$alterTable = 'ALTER TABLE ' . $this->db->quoteName($this->getTable($table));
foreach ($columns as $column => $types)
{
if (($def = $this->getColumnDefinition($table, $column)) === null)
{
continue;
}
$dbField = $this->db->quoteName($column);
$alterQuery = "$alterTable CHANGE $dbField ". $def;
if ($this->updateColumnDataType($alterQuery, $table, $column))
{
$current = $types['current'] ?? 'error';
$expected = $types['expected'] ?? 'error';
$this->success[] = "Success: updated ($column) column datatype $current to $expected in $table table.";
}
}
}
/**
* Add the component name to get the full table name.
*
* @param string $table The table name.
*
* @return void
* @since 3.2.1
*/
protected function getTable(string $table): string
{
return $this->prefix . '_' . $table;
}
/**
* Determines if the change in data type between two definitions is significant.
*
* This function checks if there's a significant difference between the current
* data type and the expected data type that would require updating the database schema.
* It ignores display width for numeric types where MySQL considers these attributes
* irrelevant for storage but considers size and other modifiers for types like VARCHAR.
*
* @param string $currentType The current data type from the database schema.
* @param string $expectedType The expected data type to validate against.
*
* @return bool Returns true if the data type change is significant, otherwise false.
* @since 3.2.1
*/
protected function isDataTypeChangeSignificant(string $currentType, string $expectedType): bool
{
// Normalize both input types to lowercase for case-insensitive comparison
$currentType = strtolower($currentType);
$expectedType = strtolower($expectedType);
// Regex to extract the base data type and numeric parameters with named groups
$typePattern = '/^(?<datatype>\w+)(\((?<params>\d+(,\d+)?)\))?/';
// Match types and parameters
preg_match($typePattern, $currentType, $currentMatches);
preg_match($typePattern, $expectedType, $expectedMatches);
// Compare base types
if ($currentMatches['datatype'] !== $expectedMatches['datatype'])
{
return true; // Base types differ
}
// Define types where size and other modifiers are irrelevant
$sizeIrrelevantTypes = [
'int', 'tinyint', 'smallint', 'mediumint', 'bigint',
'float', 'double', 'decimal', 'numeric' // Numeric types where display width is irrelevant
];
// If the type is not in the size irrelevant list, compare full definitions
if (!in_array($currentMatches['datatype'], $sizeIrrelevantTypes))
{
return $currentType !== $expectedType; // Use full definition for types where size matters
}
// For size irrelevant types, only compare base type, ignoring size and unsigned
$currentBaseType = preg_replace('/\(\d+(,\d+)?\)|unsigned/', '', $currentType);
$expectedBaseType = preg_replace('/\(\d+(,\d+)?\)|unsigned/', '', $expectedType);
// Perform a final comparison for numeric types ignoring size
return $currentBaseType !== $expectedBaseType;
}
/**
* Updates existing rows in a column to a new default value
*
* @param string $table The table to update.
* @param string $column The column to update.
* @param mixed $currentDefault Current default value.
* @param mixed $newDefault The new default value to be set.
*
* @return void
* @since 3.2.1
* @throws \Exception If there is an error updating column defaults.
*/
protected function adjustExistingDefaults(string $table, string $column, $currentDefault, $newDefault): bool
{
// Determine if adjustment is needed based on new and current defaults
if ($newDefault !== $currentDefault)
{
try {
// Format the new default for SQL use
$sqlDefault = $this->db->quote($newDefault);
$updateTable = 'UPDATE ' . $this->db->quoteName($this->getTable($table));
$dbField = $this->db->quoteName($column);
// Update SQL to set new default on existing rows where the default is currently the old default
$sql = $updateTable . " SET $dbField = $sqlDefault WHERE $dbField IS NULL OR $dbField = ''";
// Execute the update
$this->db->setQuery($sql);
return $this->db->execute();
} catch (\Exception $e) {
throw new \Exception("Error: failed to update ($column) column defaults in $table table. " . $e->getMessage());
}
}
return false;
}
/**
* Update the data type of the given field.
*
* @param string $updateString The SQL command to update the column data type
* @param string $table The table to update.
* @param string $field Column/field that must be updated.
*
* @return bool true on succes
* @since 3.2.1
* @throws \Exception If there is an error adding columns.
*/
protected function updateColumnDataType(string $updateString, string $table, string $field): bool
{
try {
$this->db->setQuery($updateString);
return $this->db->execute();
} catch (\Exception $e) {
throw new \Exception("Error: failed to update the datatype of ($field) column in $table table. " . $e->getMessage());
}
}
/**
* Key all needed keys for this table
*
* @return string of keys
* @since 3.2.1
*/
protected function getTableKeys(): string
{
$keys = [];
$keys[] = 'PRIMARY KEY (`id`)'; // TODO (we may want this to be dynamicly set)
if (!empty($this->uniqueKeys))
{
$keys[] = implode(', ', $this->uniqueKeys);
}
if (!empty($this->keys))
{
$keys[] = implode(', ', $this->keys);
}
return implode(', ', $keys);
}
/**
* Function to set the view keys
*
* @param string $column The field column database array values
*
* @return void
* @since 3.2.1
*/
protected function setKeys(array $column): void
{
$this->setUniqueKey($column);
$this->setKey($column);
}
/**
* Function to set the unique key
*
* @param string $column The field column database array values
*
* @return void
* @since 3.2.1
*/
protected function setUniqueKey(array $column): void
{
if (isset($column['unique_key']) && $column['unique_key'])
{
$key = $column['unique_key_name'] ?? $column['name'];
$this->uniqueKeys[] = "UNIQUE KEY `idx_" . $key . "` (`" . $column['name'] . "`)";
}
}
/**
* Function to set the key
*
* @param string $column The field column database array values
*
* @return void
* @since 3.2.1
*/
protected function setKey(array $column): void
{
if (isset($column['key']) && $column['key'])
{
$key = $column['key_name'] ?? $column['name'];
$this->keys[] = "KEY `idx_" . $key . "` (`" . $column['name'] . "`)";
}
}
/**
* Adjusts the default value SQL fragment for a database field based on its type and specific rules.
*
* If the field is of type DATETIME and the Joomla version is not 3, it sets the default to CURRENT_TIMESTAMP
* if not explicitly specified otherwise. For all other types it handles defaults by either leaving them unset or applying
* the provided default, properly quoted for SQL safety. When a 'EMPTY' default is specified, it returns no default at all. (:)
*
* @param string $type The type of the database field (e.g., 'DATETIME').
* @param string|null $defaultValue Optional default value for the field, null if not provided.
* @param bool $pure Optional to add the 'DEFAULT' string or not.
*
* @return string The SQL fragment to set the default value for a field.
* @since 3.2.1
*/
protected function getDefaultValue(string $type, ?string $defaultValue, bool $pure = false): string
{
if ($defaultValue === null || strtoupper($defaultValue) === 'EMPTY')
{
return '';
}
// Set default for DATETIME fields in Joomla versions above 3
if (strtoupper($type) === 'DATETIME' && $this->currentVersion != 3)
{
return $pure ? "CURRENT_TIMESTAMP" : " DEFAULT CURRENT_TIMESTAMP";
}
// Apply and quote the default value
$sql_default = $this->quote($defaultValue);
return $pure ? $defaultValue : " DEFAULT $sql_default";
}
/**
* Set a value based on data type
*
* @param mixed $value The value to set
*
* @return mixed
* @since 3.2.0
**/
protected function quote($value)
{
if ($value === null) // hmm the null does pose an issue (will keep an eye on this)
{
return 'NULL';
}
if (is_numeric($value))
{
if (filter_var($value, FILTER_VALIDATE_INT))
{
return (int) $value;
}
elseif (filter_var($value, FILTER_VALIDATE_FLOAT))
{
return (float) $value;
}
}
elseif (is_bool($value)) // not sure if this will work well (but its correct)
{
return $value ? 'TRUE' : 'FALSE';
}
// For date and datetime values
elseif ($value instanceof \DateTime)
{
return $this->db->quote($value->format('Y-m-d H:i:s'));
}
// For other data types, just escape it
return $this->db->quote($value);
}
}