Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
49 / 49 |
|
100.00% |
11 / 11 |
CRAP | |
100.00% |
1 / 1 |
AlterSchema | |
100.00% |
49 / 49 |
|
100.00% |
11 / 11 |
24 | |
100.00% |
1 / 1 |
schema | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
renderSchema | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
charset | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
renderCharset | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
collate | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
renderCollate | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
upgrade | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
renderUpgrade | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
4 | |||
checkSpecifications | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
sql | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
4 | |||
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 AlterSchema. |
17 | * |
18 | * @see https://mariadb.com/kb/en/alter-database/ |
19 | * |
20 | * @package database |
21 | */ |
22 | class AlterSchema extends Statement |
23 | { |
24 | /** |
25 | * @param string $schemaName |
26 | * |
27 | * @return static |
28 | */ |
29 | public function schema(string $schemaName) : static |
30 | { |
31 | $this->sql['schema'] = $schemaName; |
32 | return $this; |
33 | } |
34 | |
35 | protected function renderSchema() : ?string |
36 | { |
37 | if ( ! isset($this->sql['schema'])) { |
38 | return null; |
39 | } |
40 | $schema = $this->sql['schema']; |
41 | if (isset($this->sql['upgrade'])) { |
42 | $schema = "#mysql50#{$schema}"; |
43 | } |
44 | return ' ' . $this->database->protectIdentifier($schema); |
45 | } |
46 | |
47 | /** |
48 | * @param string $charset |
49 | * |
50 | * @return static |
51 | */ |
52 | public function charset(string $charset) : static |
53 | { |
54 | $this->sql['charset'] = $charset; |
55 | return $this; |
56 | } |
57 | |
58 | protected function renderCharset() : ?string |
59 | { |
60 | if ( ! isset($this->sql['charset'])) { |
61 | return null; |
62 | } |
63 | $charset = $this->database->quote($this->sql['charset']); |
64 | return " CHARACTER SET = {$charset}"; |
65 | } |
66 | |
67 | /** |
68 | * @param string $collation |
69 | * |
70 | * @return static |
71 | */ |
72 | public function collate(string $collation) : static |
73 | { |
74 | $this->sql['collation'] = $collation; |
75 | return $this; |
76 | } |
77 | |
78 | protected function renderCollate() : ?string |
79 | { |
80 | if ( ! isset($this->sql['collation'])) { |
81 | return null; |
82 | } |
83 | $collation = $this->database->quote($this->sql['collation']); |
84 | return " COLLATE = {$collation}"; |
85 | } |
86 | |
87 | /** |
88 | * @return static |
89 | */ |
90 | public function upgrade() : static |
91 | { |
92 | $this->sql['upgrade'] = true; |
93 | return $this; |
94 | } |
95 | |
96 | protected function renderUpgrade() : ?string |
97 | { |
98 | if ( ! isset($this->sql['upgrade'])) { |
99 | return null; |
100 | } |
101 | if (isset($this->sql['charset']) || isset($this->sql['collation'])) { |
102 | throw new LogicException( |
103 | 'UPGRADE DATA DIRECTORY NAME can not be used with CHARACTER SET or COLLATE' |
104 | ); |
105 | } |
106 | return ' UPGRADE DATA DIRECTORY NAME'; |
107 | } |
108 | |
109 | protected function checkSpecifications() : void |
110 | { |
111 | if ( ! isset($this->sql['charset']) |
112 | && ! isset($this->sql['collation']) |
113 | && ! isset($this->sql['upgrade']) |
114 | ) { |
115 | throw new LogicException( |
116 | 'ALTER SCHEMA must have a specification' |
117 | ); |
118 | } |
119 | } |
120 | |
121 | public function sql() : string |
122 | { |
123 | $sql = 'ALTER SCHEMA'; |
124 | $sql .= $this->renderSchema() . \PHP_EOL; |
125 | $part = $this->renderCharset(); |
126 | if ($part) { |
127 | $sql .= $part . \PHP_EOL; |
128 | } |
129 | $part = $this->renderCollate(); |
130 | if ($part) { |
131 | $sql .= $part . \PHP_EOL; |
132 | } |
133 | $part = $this->renderUpgrade(); |
134 | if ($part) { |
135 | $sql .= $part . \PHP_EOL; |
136 | } |
137 | $this->checkSpecifications(); |
138 | return $sql; |
139 | } |
140 | |
141 | /** |
142 | * Runs the ALTER SCHEMA statement. |
143 | * |
144 | * @return int|string The number of affected rows |
145 | */ |
146 | public function run() : int|string |
147 | { |
148 | return $this->database->exec($this->sql()); |
149 | } |
150 | } |