Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
12 / 12
CRAP
100.00% covered (success)
100.00%
1 / 1
QueryBuilder
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
12 / 12
17
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 createTable
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 createTableIfNotExists
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 alterTable
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 dropTable
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 dropTableIfExists
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 createIndex
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 createIndexIfNotExists
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 dropIndex
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getDialect
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getTablePrefix
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 table
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * Main query builder providing access to table and alter table builders.
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;
23
24class QueryBuilder
25{
26    private DialectInterface $dialect;
27    private string $tablePrefix;
28
29    public function __construct(?DialectInterface $dialect = null)
30    {
31        $this->dialect = $dialect ?? DialectFactory::create();
32        $this->tablePrefix = Database::getTablePrefix();
33    }
34
35    /**
36     * Creates a new TableBuilder for CREATE TABLE statements.
37     */
38    public function createTable(string $tableName, bool $withPrefix = true): TableBuilder
39    {
40        $builder = new TableBuilder($this->dialect);
41        return $builder->table($tableName, $withPrefix);
42    }
43
44    /**
45     * Creates a new TableBuilder with IF NOT EXISTS.
46     */
47    public function createTableIfNotExists(string $tableName, bool $withPrefix = true): TableBuilder
48    {
49        $builder = new TableBuilder($this->dialect);
50        return $builder->table($tableName, $withPrefix)->ifNotExists();
51    }
52
53    /**
54     * Creates a new AlterTableBuilder for ALTER TABLE statements.
55     */
56    public function alterTable(string $tableName, bool $withPrefix = true): AlterTableBuilder
57    {
58        $builder = new AlterTableBuilder($this->dialect);
59        return $builder->table($tableName, $withPrefix);
60    }
61
62    /**
63     * Creates a DROP TABLE statement.
64     */
65    public function dropTable(string $tableName, bool $withPrefix = true): string
66    {
67        $fullName = $withPrefix ? $this->tablePrefix . $tableName : $tableName;
68        return "DROP TABLE {$fullName}";
69    }
70
71    /**
72     * Creates a DROP TABLE IF EXISTS statement.
73     */
74    public function dropTableIfExists(string $tableName, bool $withPrefix = true): string
75    {
76        $fullName = $withPrefix ? $this->tablePrefix . $tableName : $tableName;
77        return "DROP TABLE IF EXISTS {$fullName}";
78    }
79
80    /**
81     * Creates a CREATE INDEX statement.
82     *
83     * @param string|string[] $columns
84     */
85    public function createIndex(
86        string $indexName,
87        string $tableName,
88        string|array $columns,
89        bool $withPrefix = true,
90    ): string {
91        $fullTableName = $withPrefix ? $this->tablePrefix . $tableName : $tableName;
92        return $this->dialect->createIndex($indexName, $fullTableName, (array) $columns, false);
93    }
94
95    /**
96     * Creates a CREATE INDEX IF NOT EXISTS statement.
97     *
98     * @param string|string[] $columns
99     */
100    public function createIndexIfNotExists(
101        string $indexName,
102        string $tableName,
103        string|array $columns,
104        bool $withPrefix = true,
105    ): string {
106        $fullTableName = $withPrefix ? $this->tablePrefix . $tableName : $tableName;
107        return $this->dialect->createIndex($indexName, $fullTableName, (array) $columns, true);
108    }
109
110    /**
111     * Creates a DROP INDEX statement.
112     */
113    public function dropIndex(string $indexName, string $tableName, bool $withPrefix = true): string
114    {
115        $fullTableName = $withPrefix ? $this->tablePrefix . $tableName : $tableName;
116        return $this->dialect->dropIndex($indexName, $fullTableName);
117    }
118
119    /**
120     * Gets the current dialect.
121     */
122    public function getDialect(): DialectInterface
123    {
124        return $this->dialect;
125    }
126
127    /**
128     * Gets the table prefix.
129     */
130    public function getTablePrefix(): string
131    {
132        return $this->tablePrefix;
133    }
134
135    /**
136     * Returns a prefixed table name.
137     */
138    public function table(string $name): string
139    {
140        return $this->tablePrefix . $name;
141    }
142}