Passed
Push — dbal ( fb7505...dff49e )
by Greg
16:01
created

DB::primaryKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2023 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;
21
22
use Doctrine\DBAL\Configuration;
23
use Doctrine\DBAL\Connection;
24
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
25
use Doctrine\DBAL\Query\QueryBuilder;
26
use Doctrine\DBAL\Schema\Index;
0 ignored issues
show
Bug introduced by
The type Doctrine\DBAL\Schema\Index 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...
27
use Doctrine\DBAL\Types\AsciiStringType;
28
use Doctrine\DBAL\Types\FloatType;
29
use Doctrine\DBAL\Types\IntegerType;
30
use Doctrine\DBAL\Types\StringType;
31
use Doctrine\DBAL\Types\TextType;
32
use Fisharebest\Webtrees\DB\Column;
33
use Fisharebest\Webtrees\DB\PrefixedConnection;
34
use Fisharebest\Webtrees\DB\PrimaryKey;
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\DB\PrimaryKey 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...
35
use Illuminate\Database\DBAL\TimestampType;
36
use PDO;
37
38
use function str_starts_with;
39
40
/**
41
 * Static access to doctrine/dbal
42
 */
43
class DB
44
{
45
    private const ASCII_COLLATION = [
46
        'mysql'  => ['collation' => 'ascii_bin'],
47
        'pgsql'  => ['collation' => 'C'],
48
        'sqlite' => ['collation' => 'binary'],
49
        'sqlsrv' => ['collation' => 'Latin1_General_Bin'],
50
    ];
51
52
    private const UTF8_COLLATION = [
53
        'mysql'  => ['collation' => 'utf8mb4_unicode_ci'],
54
        'pgsql'  => ['collation' => 'und-x-icu'],
55
        'sqlite' => ['collation' => 'nocase'],
56
        'sqlsrv' => ['collation' => 'utf8_CI_AI'],
57
    ];
58
59
    private static Connection $connection;
60
61
    private static string $prefix;
62
63
    public static function connect(PDO $pdo, string $prefix): void
64
    {
65
        $configuration = new Configuration();
66
        $configuration->setSchemaAssetsFilter(static fn (string $name): bool => str_starts_with($name, $prefix));
67
68
        self::$connection = new PrefixedConnection(pdo: $pdo, configuration: $configuration);
69
        self::$prefix     = $prefix;
70
    }
71
72
    public static function connection(): Connection
73
    {
74
        return self::$connection;
75
    }
76
77
    public static function prefix(string $identifier = ''): string
78
    {
79
        return self::$prefix . $identifier;
80
    }
81
82
    public static function select(string ...$expressions): QueryBuilder
83
    {
84
        return self::$connection
85
            ->createQueryBuilder()
86
            ->select(...$expressions);
87
    }
88
89
    public static function selectDistinct(string ...$expressions): QueryBuilder
90
    {
91
        return self::$connection
92
            ->createQueryBuilder()
93
            ->select(...$expressions)
94
            ->distinct();
95
    }
96
97
    public static function expression(): ExpressionBuilder
98
    {
99
        return self::$connection->createExpressionBuilder();
100
    }
101
102
103
    public static function char(string $name, int $length): Column
104
    {
105
        return new Column(
106
            name: $name,
107
            type: new AsciiStringType(),
108
            options: ['length' => $length, 'fixed' => true, 'platformOptions'=> self::collateAscii()],
109
        );
110
    }
111
112
    public static function varchar(string $name, int $length): Column
113
    {
114
        return new Column(
115
            name: $name,
116
            type: new AsciiStringType(),
117
            options: ['length' => $length, 'platformOptions'=> self::collateAscii()],
118
        );
119
    }
120
121
    public static function nchar(string $name, int $length): Column
122
    {
123
        return new Column(
124
            name: $name,
125
            type: new StringType(),
126
            options: ['length' => $length, 'fixed' => true, 'platformOptions'=> self::collateUtf8()],
127
        );
128
    }
129
130
    public static function nvarchar(string $name, int $length): Column
131
    {
132
        return new Column(
133
            name: $name,
134
            type: new StringType(),
135
            options: ['length' => $length, 'platformOptions'=> self::collateUtf8()],
136
        );
137
    }
138
139
    public static function int(string $name): Column
140
    {
141
        return new Column(name: $name, type: new IntegerType());
142
    }
143
144
    public static function float(string $name): Column
145
    {
146
        return new Column(name: $name, type: new FloatType());
147
    }
148
149
    public static function text(string $name): Column
150
    {
151
        return new Column(name: $name, type: new TextType());
152
    }
153
154
    public static function timestamp(string $name, int $precision = 0): Column
155
    {
156
        return new Column(name: $name, type: new TimestampType(), options: ['precision' => $precision]);
157
    }
158
159
    public static function primaryKey(array $columns): Index
160
    {
161
        return new Index(name: 'primary', columns: $columns, isPrimary: true);
162
    }
163
164
    /**
165
     * Case sensitive collation for ascii data.
166
     *
167
     * @return array<string,string>
168
     */
169
    private static function collateAscii(): array
170
    {
171
        return self::ASCII_COLLATION[self::driverName()] ?? [];
172
    }
173
174
    /**
175
     * Case insensitve collation for utf8 data.
176
     *
177
     * @return array<string,string>
178
     */
179
    private static function collateUtf8(): array
180
    {
181
        return self::UTF8_COLLATION[self::driverName()] ?? [];
182
    }
183
184
    private static function driverName(): string
185
    {
186
        return self::connection()->getNativeConnection()->getAttribute(PDO::ATTR_DRIVER_NAME);
187
    }
188
}
189