Passed
Push — dbal ( 2391b4...52c0e6 )
by Greg
08:07
created

MySQLDriver   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 60
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A introspectSchema() 0 58 3
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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Fisharebest\Webtrees\DB\Drivers\Schema. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are 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.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/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:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
Bug introduced by
The type Fisharebest\Webtrees\DB\Schema\Schema was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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);
0 ignored issues
show
Unused Code introduced by
The assignment to $table is dead and can be removed.
Loading history...
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);
0 ignored issues
show
Unused Code introduced by
The assignment to $column is dead and can be removed.
Loading history...
86
            }
87
88
        }
89
90
        var_dump($pattern, $schema_name, $table_rows, $column_rows, $table_constraints_rows, $key_column_usage_rows);exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
Security Debugging Code introduced by
var_dump($pattern, $sche...$key_column_usage_rows) looks like debug code. Are you sure you do not want to remove it?
Loading history...
91
    }
92
}
93