Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
44 / 44 |
|
100.00% |
12 / 12 |
CRAP | |
100.00% |
1 / 1 |
DropTable | |
100.00% |
44 / 44 |
|
100.00% |
12 / 12 |
21 | |
100.00% |
1 / 1 |
temporary | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
renderTemporary | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
ifExists | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
renderIfExists | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
commentToSave | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
renderCommentToSave | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
table | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
renderTables | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
wait | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
renderWait | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
sql | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
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 InvalidArgumentException; |
14 | use LogicException; |
15 | |
16 | /** |
17 | * Class DropTable. |
18 | * |
19 | * @see https://mariadb.com/kb/en/drop-table/ |
20 | * |
21 | * @package database |
22 | */ |
23 | class DropTable extends Statement |
24 | { |
25 | /** |
26 | * @return static |
27 | */ |
28 | public function temporary() : static |
29 | { |
30 | $this->sql['temporary'] = true; |
31 | return $this; |
32 | } |
33 | |
34 | protected function renderTemporary() : ?string |
35 | { |
36 | if ( ! isset($this->sql['temporary'])) { |
37 | return null; |
38 | } |
39 | return ' TEMPORARY'; |
40 | } |
41 | |
42 | /** |
43 | * @return static |
44 | */ |
45 | public function ifExists() : static |
46 | { |
47 | $this->sql['if_exists'] = true; |
48 | return $this; |
49 | } |
50 | |
51 | protected function renderIfExists() : ?string |
52 | { |
53 | if ( ! isset($this->sql['if_exists'])) { |
54 | return null; |
55 | } |
56 | return ' IF EXISTS'; |
57 | } |
58 | |
59 | /** |
60 | * @param string $comment |
61 | * |
62 | * @return static |
63 | */ |
64 | public function commentToSave(string $comment) : static |
65 | { |
66 | $this->sql['comment'] = $comment; |
67 | return $this; |
68 | } |
69 | |
70 | protected function renderCommentToSave() : ?string |
71 | { |
72 | if ( ! isset($this->sql['comment'])) { |
73 | return null; |
74 | } |
75 | $comment = \strtr($this->sql['comment'], ['*/' => '* /']); |
76 | return " /* {$comment} */"; |
77 | } |
78 | |
79 | /** |
80 | * @param string $table |
81 | * @param string ...$tables |
82 | * |
83 | * @return static |
84 | */ |
85 | public function table(string $table, string ...$tables) : static |
86 | { |
87 | $this->sql['tables'] = $tables ? \array_merge([$table], $tables) : [$table]; |
88 | return $this; |
89 | } |
90 | |
91 | protected function renderTables() : string |
92 | { |
93 | if ( ! isset($this->sql['tables'])) { |
94 | throw new LogicException('Table names can not be empty'); |
95 | } |
96 | $tables = $this->sql['tables']; |
97 | foreach ($tables as &$table) { |
98 | $table = $this->database->protectIdentifier($table); |
99 | } |
100 | unset($table); |
101 | $tables = \implode(', ', $tables); |
102 | return " {$tables}"; |
103 | } |
104 | |
105 | /** |
106 | * @param int $seconds |
107 | * |
108 | * @return static |
109 | */ |
110 | public function wait(int $seconds) : static |
111 | { |
112 | $this->sql['wait'] = $seconds; |
113 | return $this; |
114 | } |
115 | |
116 | public function renderWait() : ?string |
117 | { |
118 | if ( ! isset($this->sql['wait'])) { |
119 | return null; |
120 | } |
121 | if ($this->sql['wait'] < 0) { |
122 | throw new InvalidArgumentException( |
123 | "Invalid WAIT value: {$this->sql['wait']}" |
124 | ); |
125 | } |
126 | return " WAIT {$this->sql['wait']}"; |
127 | } |
128 | |
129 | public function sql() : string |
130 | { |
131 | $sql = 'DROP' . $this->renderTemporary(); |
132 | $sql .= ' TABLE' . $this->renderIfExists(); |
133 | $sql .= $this->renderCommentToSave(); |
134 | $sql .= $this->renderTables() . \PHP_EOL; |
135 | $part = $this->renderWait(); |
136 | if ($part) { |
137 | $sql .= $part . \PHP_EOL; |
138 | } |
139 | return $sql; |
140 | } |
141 | |
142 | /** |
143 | * Runs the DROP TABLE statement. |
144 | * |
145 | * @return int|string The number of affected rows |
146 | */ |
147 | public function run() : int|string |
148 | { |
149 | return $this->database->exec($this->sql()); |
150 | } |
151 | } |