Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
14 / 14 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
DropSchema | |
100.00% |
14 / 14 |
|
100.00% |
6 / 6 |
8 | |
100.00% |
1 / 1 |
ifExists | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
renderIfExists | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
schema | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
renderSchema | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
sql | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
run | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php declare(strict_types=1); |
2 | /* |
3 | * This file is part of Aplus Framework Database Library. |
4 | * |
5 | * (c) Natan Felles <natanfelles@gmail.com> |
6 | * |
7 | * For the full copyright and license information, please view the LICENSE |
8 | * file that was distributed with this source code. |
9 | */ |
10 | namespace Framework\Database\Definition; |
11 | |
12 | use Framework\Database\Statement; |
13 | use LogicException; |
14 | |
15 | /** |
16 | * Class DropSchema. |
17 | * |
18 | * @see https://mariadb.com/kb/en/drop-database/ |
19 | * |
20 | * @package database |
21 | */ |
22 | class DropSchema extends Statement |
23 | { |
24 | /** |
25 | * @return static |
26 | */ |
27 | public function ifExists() : static |
28 | { |
29 | $this->sql['if_exists'] = true; |
30 | return $this; |
31 | } |
32 | |
33 | protected function renderIfExists() : ?string |
34 | { |
35 | if ( ! isset($this->sql['if_exists'])) { |
36 | return null; |
37 | } |
38 | return ' IF EXISTS'; |
39 | } |
40 | |
41 | /** |
42 | * @param string $schemaName |
43 | * |
44 | * @return static |
45 | */ |
46 | public function schema(string $schemaName) : static |
47 | { |
48 | $this->sql['schema'] = $schemaName; |
49 | return $this; |
50 | } |
51 | |
52 | protected function renderSchema() : string |
53 | { |
54 | if (isset($this->sql['schema'])) { |
55 | return ' ' . $this->database->protectIdentifier($this->sql['schema']); |
56 | } |
57 | throw new LogicException('SCHEMA name must be set'); |
58 | } |
59 | |
60 | public function sql() : string |
61 | { |
62 | $sql = 'DROP SCHEMA' . $this->renderIfExists(); |
63 | $sql .= $this->renderSchema() . \PHP_EOL; |
64 | return $sql; |
65 | } |
66 | |
67 | /** |
68 | * Runs the CREATE SCHEMA statement. |
69 | * |
70 | * @return int|string The number of affected rows |
71 | */ |
72 | public function run() : int|string |
73 | { |
74 | return $this->database->exec($this->sql()); |
75 | } |
76 | } |