Passed
Push — dbal ( fbb214...fb7505 )
by Greg
06:03
created

DB::int()   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
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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\Types\AsciiStringType;
27
use Doctrine\DBAL\Types\FloatType;
28
use Doctrine\DBAL\Types\IntegerType;
29
use Doctrine\DBAL\Types\StringType;
30
use Doctrine\DBAL\Types\TextType;
31
use Fisharebest\Webtrees\DB\Column;
32
use Fisharebest\Webtrees\DB\PrefixedConnection;
33
use Illuminate\Database\DBAL\TimestampType;
34
use PDO;
35
36
use function str_starts_with;
37
38
/**
39
 * Static access to doctrine/dbal
40
 */
41
class DB
42
{
43
    private static Connection $connection;
44
45
    private static string $prefix;
46
47
    public static function connect(PDO $pdo, string $prefix): void
48
    {
49
        $configuration = new Configuration();
50
        $configuration->setSchemaAssetsFilter(static fn (string $name): bool => str_starts_with($name, $prefix));
51
52
        self::$connection = new PrefixedConnection(pdo: $pdo, configuration: $configuration);
53
        self::$prefix     = $prefix;
54
    }
55
56
    public static function connection(): Connection
57
    {
58
        return self::$connection;
59
    }
60
61
    public static function prefix(string $identifier = ''): string
62
    {
63
        return self::$prefix . $identifier;
64
    }
65
66
    public static function select(string ...$expressions): QueryBuilder
67
    {
68
        return self::$connection
69
            ->createQueryBuilder()
70
            ->select(...$expressions);
71
    }
72
73
    public static function selectDistinct(string ...$expressions): QueryBuilder
74
    {
75
        return self::$connection
76
            ->createQueryBuilder()
77
            ->select(...$expressions)
78
            ->distinct();
79
    }
80
81
    public static function expression(): ExpressionBuilder
82
    {
83
        return self::$connection->createExpressionBuilder();
84
    }
85
86
87
    public static function char(string $name, int $length): Column
88
    {
89
        return new Column(
90
            name: $name,
91
            type: new AsciiStringType(),
92
            options: ['length' => $length, 'fixed' => true, 'platformOptions'=> self::collateAscii()],
93
        );
94
    }
95
96
    public static function varchar(string $name, int $length): Column
97
    {
98
        return new Column(
99
            name: $name,
100
            type: new AsciiStringType(),
101
            options: ['length' => $length, 'platformOptions'=> self::collateAscii()],
102
        );
103
    }
104
105
    public static function nchar(string $name, int $length): Column
106
    {
107
        return new Column(
108
            name: $name,
109
            type: new StringType(),
110
            options: ['length' => $length, 'fixed' => true, 'platformOptions'=> self::collateUtf8()],
111
        );
112
    }
113
114
    public static function nvarchar(string $name, int $length): Column
115
    {
116
        return new Column(
117
            name: $name,
118
            type: new StringType(),
119
            options: ['length' => $length, 'platformOptions'=> self::collateUtf8()],
120
        );
121
    }
122
123
    public static function int(string $name): Column
124
    {
125
        return new Column(name: $name, type: new IntegerType());
126
    }
127
128
    public static function float(string $name): Column
129
    {
130
        return new Column(name: $name, type: new FloatType());
131
    }
132
133
    public static function text(string $name): Column
134
    {
135
        return new Column(name: $name, type: new TextType());
136
    }
137
138
    public static function timestamp(string $name, int $precision = 0): Column
139
    {
140
        return new Column(name: $name, type: new TimestampType(), options: ['precision' => $precision]);
141
    }
142
143
    private static function collateAscii(): array
144
    {
145
        return match(self::connection()->getNativeConnection()->getAttribute(PDO::ATTR_DRIVER_NAME)) {
146
            'mysql' => ['charset' => 'ascii', 'collation' => 'ascii_bin'],
147
            default => [],
148
        };
149
    }
150
151
    private static function collateUtf8(): array
152
    {
153
        return match(self::connection()->getNativeConnection()->getAttribute(PDO::ATTR_DRIVER_NAME)) {
154
            'mysql' => ['charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci'],
155
            default => [],
156
        };
157
    }
158
}
159