Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
85.71% covered (success)
85.71%
6 / 7
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
DialectFactory
85.71% covered (success)
85.71%
6 / 7
50.00% covered (danger)
50.00%
1 / 2
7.14
0.00% covered (danger)
0.00%
0 / 1
 create
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 createForType
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
6
1<?php
2
3/**
4 * Factory for creating database-specific dialect instances.
5 *
6 * This Source Code Form is subject to the terms of the Mozilla Public License,
7 * v. 2.0. If a copy of the MPL was not distributed with this file, You can
8 * obtain one at https://mozilla.org/MPL/2.0/.
9 *
10 * @package   phpMyFAQ
11 * @author    Thorsten Rinne <thorsten@phpmyfaq.de>
12 * @copyright 2023-2026 phpMyFAQ Team
13 * @license   https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0
14 * @link      https://www.phpmyfaq.de
15 * @since     2026-01-25
16 */
17
18declare(strict_types=1);
19
20namespace phpMyFAQ\Setup\Migration\QueryBuilder;
21
22use phpMyFAQ\Database;
23use phpMyFAQ\Setup\Migration\QueryBuilder\Dialect\MysqlDialect;
24use phpMyFAQ\Setup\Migration\QueryBuilder\Dialect\PostgresDialect;
25use phpMyFAQ\Setup\Migration\QueryBuilder\Dialect\SqliteDialect;
26use phpMyFAQ\Setup\Migration\QueryBuilder\Dialect\SqlServerDialect;
27
28class DialectFactory
29{
30    /**
31     * Creates the appropriate dialect for the current database type.
32     */
33    public static function create(): DialectInterface
34    {
35        return self::createForType(Database::getType());
36    }
37
38    /**
39     * Creates a dialect for the specified database type.
40     */
41    public static function createForType(string $dbType): DialectInterface
42    {
43        return match ($dbType) {
44            'mysqli', 'pdo_mysql' => new MysqlDialect(),
45            'pgsql', 'pdo_pgsql' => new PostgresDialect(),
46            'sqlite3', 'pdo_sqlite' => new SqliteDialect(),
47            'sqlsrv', 'pdo_sqlsrv' => new SqlServerDialect(),
48            default => throw new \InvalidArgumentException("Unsupported database type: {$dbType}"),
49        };
50    }
51}