Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
38 / 38 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
| SqlOperation | |
100.00% |
38 / 38 |
|
100.00% |
6 / 6 |
15 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getType | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getDescription | |
100.00% |
26 / 26 |
|
100.00% |
1 / 1 |
9 | |||
| getQuery | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| execute | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| toArray | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * SQL query operation for migrations. |
| 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 | |
| 18 | declare(strict_types=1); |
| 19 | |
| 20 | namespace phpMyFAQ\Setup\Migration\Operations; |
| 21 | |
| 22 | use phpMyFAQ\Configuration; |
| 23 | |
| 24 | readonly class SqlOperation implements OperationInterface |
| 25 | { |
| 26 | public function __construct( |
| 27 | private Configuration $configuration, |
| 28 | private string $query, |
| 29 | private string $description = '', |
| 30 | ) { |
| 31 | } |
| 32 | |
| 33 | public function getType(): string |
| 34 | { |
| 35 | return 'sql'; |
| 36 | } |
| 37 | |
| 38 | public function getDescription(): string |
| 39 | { |
| 40 | if ($this->description !== '') { |
| 41 | return $this->description; |
| 42 | } |
| 43 | |
| 44 | // Generate description from query |
| 45 | $query = trim($this->query); |
| 46 | $matches = []; |
| 47 | if (stripos(haystack: $query, needle: 'CREATE TABLE') === 0) { |
| 48 | preg_match('/CREATE TABLE\s+(?:IF NOT EXISTS\s+)?(\S+)/i', $query, $matches); |
| 49 | return sprintf('Create table %s', $matches[1] ?? 'unknown'); |
| 50 | } |
| 51 | if (stripos(haystack: $query, needle: 'ALTER TABLE') === 0) { |
| 52 | preg_match('/ALTER TABLE\s+(\S+)/i', $query, $matches); |
| 53 | return sprintf('Alter table %s', $matches[1] ?? 'unknown'); |
| 54 | } |
| 55 | if (stripos(haystack: $query, needle: 'DROP TABLE') === 0) { |
| 56 | preg_match('/DROP TABLE\s+(?:IF EXISTS\s+)?(\S+)/i', $query, $matches); |
| 57 | return sprintf('Drop table %s', $matches[1] ?? 'unknown'); |
| 58 | } |
| 59 | if (stripos(haystack: $query, needle: 'CREATE INDEX') === 0) { |
| 60 | preg_match('/CREATE INDEX\s+(?:IF NOT EXISTS\s+)?(\S+)/i', $query, $matches); |
| 61 | return sprintf('Create index %s', $matches[1] ?? 'unknown'); |
| 62 | } |
| 63 | if (stripos(haystack: $query, needle: 'INSERT INTO') === 0) { |
| 64 | preg_match('/INSERT INTO\s+(\S+)/i', $query, $matches); |
| 65 | return sprintf('Insert into %s', $matches[1] ?? 'unknown'); |
| 66 | } |
| 67 | if (stripos(haystack: $query, needle: 'UPDATE') === 0) { |
| 68 | preg_match('/UPDATE\s+(\S+)/i', $query, $matches); |
| 69 | return sprintf('Update %s', $matches[1] ?? 'unknown'); |
| 70 | } |
| 71 | if (stripos(haystack: $query, needle: 'DELETE FROM') === 0) { |
| 72 | preg_match('/DELETE FROM\s+(\S+)/i', $query, $matches); |
| 73 | return sprintf('Delete from %s', $matches[1] ?? 'unknown'); |
| 74 | } |
| 75 | |
| 76 | return 'Execute SQL query'; |
| 77 | } |
| 78 | |
| 79 | public function getQuery(): string |
| 80 | { |
| 81 | return $this->query; |
| 82 | } |
| 83 | |
| 84 | public function execute(): bool |
| 85 | { |
| 86 | try { |
| 87 | $result = $this->configuration->getDb()->query($this->query); |
| 88 | return (bool) $result; |
| 89 | } catch (\Throwable) { |
| 90 | return false; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | public function toArray(): array |
| 95 | { |
| 96 | return [ |
| 97 | 'type' => $this->getType(), |
| 98 | 'description' => $this->getDescription(), |
| 99 | 'query' => $this->query, |
| 100 | ]; |
| 101 | } |
| 102 | } |