Passed
Push — 2.1 ( 469da2...39d703 )
by Greg
16:30 queued 08:04
created

DB::concat()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2025 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 Closure;
23
use Illuminate\Database\Capsule\Manager;
24
use Illuminate\Database\Query\Builder;
25
use Illuminate\Database\Query\Expression;
26
use PDO;
27
use RuntimeException;
28
29
/**
30
 * Database abstraction
31
 */
32
class DB extends Manager
33
{
34
    // Supported drivers
35
    public const MYSQL      = 'mysql';
36
    public const POSTGRES   = 'pgsql';
37
    public const SQLITE     = 'sqlite';
38
    public const SQL_SERVER = 'sqlsrv';
39
40
    private const COLLATION_ASCII = [
41
        self::MYSQL      => 'ascii_bin',
42
        self::POSTGRES   => 'C',
43
        self::SQLITE     => 'BINARY',
44
        self::SQL_SERVER => 'Latin1_General_Bin',
45
    ];
46
47
    private const COLLATION_UTF8 = [
48
        self::MYSQL      => 'utf8mb4_unicode_ci',
49
        self::POSTGRES   => 'und-x-icu',
50
        self::SQLITE     => 'NOCASE',
51
        self::SQL_SERVER => 'utf8_CI_AI',
52
    ];
53
54
    private const REGEX_OPERATOR = [
55
        self::MYSQL      => 'REGEXP',
56
        self::POSTGRES   => '~',
57
        self::SQLITE     => 'REGEXP',
58
        self::SQL_SERVER => 'REGEXP',
59
    ];
60
61
    private const DRIVER_INITIALIZATION = [
62
        self::MYSQL      => "SET NAMES utf8mb4, sql_mode := 'ANSI,STRICT_ALL_TABLES', TIME_ZONE := '+00:00', SQL_BIG_SELECTS := 1, GROUP_CONCAT_MAX_LEN := 1048576",
63
        self::POSTGRES   => '',
64
        self::SQLITE     => 'PRAGMA foreign_keys = ON',
65
        self::SQL_SERVER => 'SET language us_english', // For timestamp columns
66
    ];
67
68
    public static function driverName(): string
69
    {
70
        return self::pdo()->getAttribute(PDO::ATTR_DRIVER_NAME);
71
    }
72
73
    /**
74
     * @return int|false
75
     */
76
    public static function exec(string $sql)
77
    {
78
        return self::pdo()->exec($sql);
79
    }
80
81
    public static function lastInsertId(): int
82
    {
83
        $return = self::pdo()->lastInsertId();
84
85
        if ($return === false) {
86
            throw new RuntimeException('Unable to retrieve last insert ID');
87
        }
88
89
        // All IDs are integers in our schema.
90
        return (int) $return;
91
    }
92
93
    private static function pdo(): PDO
94
    {
95
        return parent::connection()->getPdo();
96
    }
97
98
    public static function prefix(string $identifier): string
99
    {
100
        return parent::connection()->getTablePrefix() . $identifier;
101
    }
102
103
    /**
104
     * SQL-Server needs to be told that we are going to insert into an identity column.
105
     *
106
     * @param Closure(): void $callback
107
     */
108
    public static function identityInsert(string $table, Closure $callback): void
109
    {
110
        if (self::driverName() === self::SQL_SERVER) {
111
            self::exec('SET IDENTITY_INSERT [' . self::prefix($table) . '] ON');
112
        }
113
114
        $callback();
115
116
        if (self::driverName() === self::SQL_SERVER) {
117
            self::exec('SET IDENTITY_INSERT [' . self::prefix($table) . '] OFF');
118
        }
119
    }
120
121
    public static function rollBack(): void
122
    {
123
        parent::connection()->rollBack();
124
    }
125
126
    /**
127
     * @internal
128
     *
129
     * @param list<string> $expressions
0 ignored issues
show
Bug introduced by
The type Fisharebest\Webtrees\list 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...
130
     */
131
    public static function concat(array $expressions): string
132
    {
133
        if (self::driverName() === self::SQL_SERVER) {
134
            return 'CONCAT(' . implode(', ', $expressions) . ')';
135
        }
136
137
        // ANSI standard.  MySQL uses this with ANSI mode
138
        return '(' . implode(' || ', $expressions) . ')';
139
    }
140
141
    /**
142
     * @internal
143
     */
144
    public static function iLike(): string
145
    {
146
        if (self::driverName() === self::POSTGRES) {
147
            return 'ILIKE';
148
        }
149
150
        return 'LIKE';
151
    }
152
153
    /**
154
     * @internal
155
     */
156
    public static function groupConcat(string $column): string
157
    {
158
        switch (self::driverName()) {
159
            case self::POSTGRES:
160
            case self::SQL_SERVER:
161
                return 'STRING_AGG(' . $column . ", ',')";
162
163
            case self::MYSQL:
164
            case self::SQLITE:
165
            default:
166
                return 'GROUP_CONCAT(' . $column . ')';
167
        }
168
    }
169
170
    /**
171
     * @return Expression
172
     */
173
    public static function binaryColumn(string $column, ?string $alias = null): Expression
174
    {
175
        if (self::driverName() === self::MYSQL) {
176
            $sql = 'CAST(' . $column . ' AS binary)';
177
        } else {
178
            $sql = $column;
179
        }
180
181
        if ($alias !== null) {
182
            $sql .= ' AS ' . $alias;
183
        }
184
185
        return new Expression($sql);
186
    }
187
188
    public static function regexOperator(): string
189
    {
190
        return self::REGEX_OPERATOR[self::driverName()];
191
    }
192
193
    /**
194
     * PHPSTAN can't detect the magic methods in the parent class.
195
     */
196
    public static function query(): Builder
197
    {
198
        return parent::connection()->query();
199
    }
200
}
201