Passed
Push — dbal ( 23f922...9cad68 )
by Greg
15:01
created

DB::varchar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 6
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\Driver as DBALDriver;
25
use Doctrine\DBAL\Query\Expression\ExpressionBuilder;
26
use Doctrine\DBAL\Query\QueryBuilder;
27
use DomainException;
28
use Fisharebest\Webtrees\DB\Column;
29
use Fisharebest\Webtrees\DB\ColumnType;
30
use Fisharebest\Webtrees\DB\Drivers\DriverInterface;
31
use Fisharebest\Webtrees\DB\Drivers\MySQLDriver;
32
use Fisharebest\Webtrees\DB\Drivers\PostgreSQLDriver;
33
use Fisharebest\Webtrees\DB\Drivers\SQLiteDriver;
34
use Fisharebest\Webtrees\DB\Drivers\SQLServerDriver;
35
use Fisharebest\Webtrees\DB\ForeignKey;
36
use Fisharebest\Webtrees\DB\Index;
37
use Fisharebest\Webtrees\DB\PrimaryKey;
38
use Fisharebest\Webtrees\DB\UniqueIndex;
39
use PDO;
40
41
use function str_starts_with;
42
43
/**
44
 * Static access to doctrine/dbal
45
 */
46
class DB
47
{
48
    private static Connection $connection;
49
50
    private static string $prefix;
51
52
    private static DBALDriver&DriverInterface $driver;
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected '&', expecting T_VARIABLE on line 52 at column 29
Loading history...
53
54
    public static function connect(PDO $pdo, string $prefix): void
55
    {
56
        $configuration = new Configuration();
57
        $configuration->setSchemaAssetsFilter(static fn(string $name): bool => str_starts_with($name, $prefix));
58
59
        $driver_name = $pdo->getAttribute(attribute: PDO::ATTR_DRIVER_NAME);
60
61
        static::$driver = match ($driver_name) {
62
            'mysql'    => new MySQLDriver(pdo: $pdo),
63
            'postgres' => new PostgreSQLDriver(pdo: $pdo),
64
            'sqlite'   => new SQLiteDriver(pdo: $pdo),
65
            'sqlsrv'   => new SQLServerDriver(pdo: $pdo),
66
            default    => throw new DomainException(message: 'No driver available for ' . $driver_name),
67
        };
68
69
        static::$connection = new Connection(params: [], driver: static::$driver, config: $configuration);
70
        static::$prefix     = $prefix;
71
    }
72
73
    public static function connection(): Connection
74
    {
75
        return self::$connection;
76
    }
77
78
    public static function prefix(string $identifier = ''): string
79
    {
80
        return self::$prefix . $identifier;
81
    }
82
83
    public static function select(string ...$expressions): QueryBuilder
84
    {
85
        return self::$connection
86
            ->createQueryBuilder()
87
            ->select(...$expressions);
88
    }
89
90
    public static function selectDistinct(string ...$expressions): QueryBuilder
91
    {
92
        return self::$connection
93
            ->createQueryBuilder()
94
            ->select(...$expressions)
95
            ->distinct();
96
    }
97
98
    public static function expression(): ExpressionBuilder
99
    {
100
        return self::$connection->createExpressionBuilder();
101
    }
102
103
104
    public static function char(string $name, int $length): Column
105
    {
106
        return new Column(
107
            name: $name,
108
            type: ColumnType::Char,
109
            length: $length,
110
            fixed: true,
111
            collation: DB::connection()->getDriver()->collationASCII(),
112
        );
113
    }
114
115
    public static function varchar(string $name, int $length): Column
116
    {
117
        return new Column(
118
            name: $name,
119
            type: ColumnType::Char,
120
            length: $length,
121
            collation: DB::connection()->getDriver()->collationASCII(),
122
        );
123
    }
124
125
    public static function nchar(string $name, int $length): Column
126
    {
127
        return new Column(
128
            name: $name,
129
            type: ColumnType::NChar,
130
            length: $length,
131
            fixed: true,
132
            collation: DB::connection()->getDriver()->collationUTF8(),
133
        );
134
    }
135
136
    public static function nvarchar(string $name, int $length): Column
137
    {
138
        return new Column(
139
            name: $name,
140
            type: ColumnType::NVarChar,
141
            length: $length,
142
            collation: DB::connection()->getDriver()->collationUTF8(),
143
        );
144
    }
145
146
    public static function integer(string $name): Column
147
    {
148
        return new Column(name: $name, type: ColumnType::Integer);
149
    }
150
151
    public static function float(string $name): Column
152
    {
153
        return new Column(name: $name, type: ColumnType::Float);
154
    }
155
156
    public static function text(string $name): Column
157
    {
158
        return new Column(
159
            name: $name,
160
            type: ColumnType::Text,
161
            collation: DB::connection()->getDriver()->collationUTF8(),
162
        );
163
    }
164
165
    public static function timestamp(string $name, int $precision = 0): Column
166
    {
167
        return new Column(name: $name, type: ColumnType::Timestamp, precision: $precision);
168
    }
169
170
    /**
171
     * @param array<array-key,string> $columns
172
     *
173
     * @return PrimaryKey
174
     */
175
    public static function primaryKey(array $columns): PrimaryKey
176
    {
177
        return new PrimaryKey(columns: $columns);
178
    }
179
180
    /**
181
     * @param array<array-key,string> $columns
182
     *
183
     * @return Index
184
     */
185
    public static function index(array $columns): Index
186
    {
187
        return new Index(columns: $columns);
188
    }
189
190
    /**
191
     * @param array<array-key,string> $columns
192
     *
193
     * @return UniqueIndex
194
     */
195
    public static function uniqueIndex(array $columns): UniqueIndex
196
    {
197
        return new UniqueIndex(columns: $columns);
198
    }
199
200
    /**
201
     * @param array<array-key,string> $local_columns
202
     * @param string                  $foreign_table
203
     * @param array<array-key,string> $foreign_columns
204
     *
205
     * @return ForeignKey
206
     */
207
    public static function foreignKey(array $local_columns, string $foreign_table, array $foreign_columns = null): ForeignKey
208
    {
209
        return new ForeignKey(
210
            local_columns: $local_columns,
211
            foreign_table: $foreign_table,
212
            foreign_columns: $foreign_columns ?? $local_columns,
213
        );
214
    }
215
216
    private static function driverName(): ?string
217
    {
218
        return self::connection()->getNativeConnection()->getAttribute(PDO::ATTR_DRIVER_NAME);
219
    }
220
}
221