|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* webtrees: online genealogy |
|
5
|
|
|
* Copyright (C) 2022 webtrees development team |
|
6
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
7
|
|
|
* it under the terms of the GNU General Public License as published by |
|
8
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
|
9
|
|
|
* (at your option) any later version. |
|
10
|
|
|
* This program is distributed in the hope that it will be useful, |
|
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13
|
|
|
* GNU General Public License for more details. |
|
14
|
|
|
* You should have received a copy of the GNU General Public License |
|
15
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
16
|
|
|
*/ |
|
17
|
|
|
|
|
18
|
|
|
declare(strict_types=1); |
|
19
|
|
|
|
|
20
|
|
|
namespace Fisharebest\Webtrees\DB\Drivers; |
|
21
|
|
|
|
|
22
|
|
|
use Fisharebest\Webtrees\DB\Schema\Schema; |
|
|
|
|
|
|
23
|
|
|
use Fisharebest\Webtrees\DB\Schema\Column; |
|
24
|
|
|
use Fisharebest\Webtrees\DB\Schema\Table; |
|
25
|
|
|
|
|
26
|
|
|
use function array_filter; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Driver for MySQL |
|
30
|
|
|
*/ |
|
31
|
|
|
class MySQLDriver extends AbstractDriver implements DriverInterface |
|
32
|
|
|
{ |
|
33
|
|
|
public function introspectSchema(string $schema_name = null): Schema |
|
34
|
|
|
{ |
|
35
|
|
|
$schema_name ?? $schema_name = $this->query('SELECT DATABASE() AS schema_name')[0]->schema_name; |
|
36
|
|
|
|
|
37
|
|
|
$pattern = $this->escapeLike($this->prefix) . '%'; |
|
38
|
|
|
|
|
39
|
|
|
$sql = |
|
40
|
|
|
'SELECT TABLE_NAME, ENGINE, AUTO_INCREMENT, TABLE_COLLATION' . |
|
41
|
|
|
' FROM INFORMATION_SCHEMA.TABLES' . |
|
42
|
|
|
' WHERE TABLE_TYPE = :table_type' . |
|
43
|
|
|
' AND TABLE_SCHEMA = :table_schema' . |
|
44
|
|
|
' AND TABLE_NAME LIKE :pattern' . |
|
45
|
|
|
' ORDER BY TABLE_NAME'; |
|
46
|
|
|
|
|
47
|
|
|
$table_rows = $this->query($sql, [ |
|
48
|
|
|
'table_type' => 'BASE TABLE', |
|
49
|
|
|
'pattern' => $pattern, |
|
50
|
|
|
'table_schema' => $schema_name, |
|
51
|
|
|
]); |
|
52
|
|
|
|
|
53
|
|
|
$sql = |
|
54
|
|
|
'SELECT TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION, COLUMN_DEFAULT, IS_NULLABLE, CHARACTER_MAXIMUM_LENGTH, CHARACTER_OCTET_LENGTH, NUMERIC_PRECISION, NUMERIC_SCALE, DATETIME_PRECISION, CHARACTER_SET_NAME, COLLATION_NAME, COLUMN_TYPE, COLUMN_KEY, EXTRA, COLUMN_COMMENT, GENERATION_EXPRESSION, SRS_ID' . |
|
55
|
|
|
' FROM INFORMATION_SCHEMA.COLUMNS' . |
|
56
|
|
|
' WHERE TABLE_SCHEMA = :table_schema' . |
|
57
|
|
|
' AND TABLE_NAME LIKE :pattern' . |
|
58
|
|
|
' ORDER BY ORDINAL_POSITION'; |
|
59
|
|
|
|
|
60
|
|
|
$column_rows = $this->query($sql, ['pattern' => $pattern, 'table_schema' => $schema_name]); |
|
61
|
|
|
|
|
62
|
|
|
$sql = |
|
63
|
|
|
'SELECT TABLE_NAME, CONSTRAINT_NAME, CONSTRAINT_TYPE' . |
|
64
|
|
|
' FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS' . |
|
65
|
|
|
' WHERE TABLE_SCHEMA = :table_schema' . |
|
66
|
|
|
' AND TABLE_NAME LIKE :pattern'; |
|
67
|
|
|
|
|
68
|
|
|
$table_constraints_rows = $this->query($sql, ['pattern' => $pattern, 'table_schema' => $schema_name]); |
|
69
|
|
|
|
|
70
|
|
|
$sql = |
|
71
|
|
|
'SELECT CONSTRAINT_NAME, TABLE_NAME, COLUMN_NAME, ORDINAL_POSITION, POSITION_IN_UNIQUE_CONSTRAINT,' . |
|
72
|
|
|
' REFERENCED_TABLE_NAME, REFERENCED_COLUMN_NAME' . |
|
73
|
|
|
' FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE' . |
|
74
|
|
|
' WHERE TABLE_SCHEMA = :table_schema' . |
|
75
|
|
|
' AND TABLE_NAME LIKE :pattern'; |
|
76
|
|
|
|
|
77
|
|
|
$key_column_usage_rows = $this->query($sql, ['pattern' => $pattern, 'table_schema' => $schema_name]); |
|
78
|
|
|
|
|
79
|
|
|
foreach ($table_rows as $table_row) { |
|
80
|
|
|
$table = new Table($table_row->TABLE_NAME); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
$this_column_rows = array_filter($column_rows, static fn (object $row): bool => $row->TABLE_NAME === $table_row->TABLE_NAME); |
|
83
|
|
|
|
|
84
|
|
|
foreach ($this_column_rows as $this_column_row) { |
|
85
|
|
|
$column = new Column($this_column_row->COLUMN_NAME, $this_column_row->COLUMN_TYPE, $this_column_row->COLUMN_DEFAULT); |
|
|
|
|
|
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
var_dump($pattern, $schema_name, $table_rows, $column_rows, $table_constraints_rows, $key_column_usage_rows);exit; |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: