1 | <?php |
||||
2 | |||||
3 | declare(strict_types=1); |
||||
4 | |||||
5 | namespace LaravelFreelancerNL\Aranguent\Schema; |
||||
6 | |||||
7 | use ArangoClient\Exceptions\ArangoException; |
||||
8 | use ArangoClient\Schema\SchemaManager; |
||||
9 | use Closure; |
||||
10 | use Illuminate\Support\Facades\Log; |
||||
11 | use Illuminate\Support\Fluent; |
||||
12 | use LaravelFreelancerNL\Aranguent\Connection; |
||||
13 | use LaravelFreelancerNL\Aranguent\Exceptions\QueryException; |
||||
14 | use LaravelFreelancerNL\Aranguent\Schema\Concerns\HandlesAnalyzers; |
||||
15 | use LaravelFreelancerNL\Aranguent\Schema\Concerns\HandlesIndexes; |
||||
16 | use LaravelFreelancerNL\Aranguent\Schema\Concerns\HandlesIndexNaming; |
||||
17 | use LaravelFreelancerNL\Aranguent\Schema\Concerns\HandlesGraphs; |
||||
18 | use LaravelFreelancerNL\Aranguent\Schema\Concerns\HandlesViews; |
||||
19 | use LaravelFreelancerNL\Aranguent\Schema\Concerns\UsesBlueprints; |
||||
20 | |||||
21 | class Builder extends \Illuminate\Database\Schema\Builder |
||||
22 | { |
||||
23 | use HandlesAnalyzers; |
||||
24 | use HandlesIndexNaming; |
||||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||||
25 | use HandlesGraphs; |
||||
26 | use HandlesIndexes; |
||||
27 | use HandlesViews; |
||||
28 | use UsesBlueprints; |
||||
29 | |||||
30 | /** |
||||
31 | * The database connection instance. |
||||
32 | * |
||||
33 | * @var Connection |
||||
34 | */ |
||||
35 | protected $connection; |
||||
36 | |||||
37 | public SchemaManager $schemaManager; |
||||
38 | |||||
39 | /** |
||||
40 | * The schema grammar instance. |
||||
41 | * |
||||
42 | * @var Grammar |
||||
43 | */ |
||||
44 | public $grammar; |
||||
45 | |||||
46 | |||||
47 | /** |
||||
48 | * index prefixes? |
||||
49 | */ |
||||
50 | public ?bool $prefixIndexes; |
||||
51 | |||||
52 | /** |
||||
53 | * The table prefix. |
||||
54 | */ |
||||
55 | public string $prefix; |
||||
56 | |||||
57 | /** |
||||
58 | * Create a new database Schema manager. |
||||
59 | * |
||||
60 | * Builder constructor. |
||||
61 | */ |
||||
62 | 134 | public function __construct(Connection $connection) |
|||
63 | { |
||||
64 | 134 | $this->connection = $connection; |
|||
65 | |||||
66 | 134 | $this->grammar = $connection->getSchemaGrammar(); |
|||
67 | |||||
68 | 134 | $this->schemaManager = $connection->getArangoClient()->schema(); |
|||
69 | |||||
70 | 134 | $this->prefixIndexes = $this->connection->getConfig('prefix_indexes'); |
|||
71 | |||||
72 | 134 | $this->prefix = $this->prefixIndexes |
|||
73 | ? $this->connection->getConfig('prefix') |
||||
74 | 134 | : ''; |
|||
75 | |||||
76 | } |
||||
77 | |||||
78 | /** |
||||
79 | * Determine if the given table exists. |
||||
80 | * |
||||
81 | * @param string $table |
||||
82 | * @return bool |
||||
83 | */ |
||||
84 | 66 | public function hasTable($table) |
|||
85 | { |
||||
86 | 66 | return $this->handleExceptionsAsQueryExceptions(function () use ($table) { |
|||
87 | 66 | return $this->schemaManager->hasCollection($table); |
|||
88 | 66 | }); |
|||
89 | } |
||||
90 | |||||
91 | /** |
||||
92 | * @throws ArangoException |
||||
93 | */ |
||||
94 | 14 | public function dropIfExists($table): void |
|||
95 | { |
||||
96 | 14 | $tableExists = $this->hasTable($table); |
|||
97 | 14 | if ($tableExists) { |
|||
98 | 14 | $this->drop($table); |
|||
99 | } |
||||
100 | } |
||||
101 | |||||
102 | /** |
||||
103 | * Get all the tables for the database; excluding ArangoDB system collections |
||||
104 | * |
||||
105 | * @param string $name |
||||
106 | * @return array<mixed> |
||||
107 | * |
||||
108 | * @throws ArangoException |
||||
109 | */ |
||||
110 | 14 | public function getTable($name): array |
|||
111 | { |
||||
112 | 14 | return (array) $this->schemaManager->getCollectionStatistics($name); |
|||
113 | } |
||||
114 | |||||
115 | /** |
||||
116 | * Get all the tables for the database; including ArangoDB system tables |
||||
117 | * |
||||
118 | * @return array<mixed> |
||||
119 | * |
||||
120 | * @throws ArangoException |
||||
121 | */ |
||||
122 | 1 | public function getAllTables(): array |
|||
123 | { |
||||
124 | 1 | return $this->mapResultsToArray( |
|||
125 | 1 | $this->schemaManager->getCollections(false), |
|||
126 | 1 | ); |
|||
127 | } |
||||
128 | |||||
129 | /** |
||||
130 | * Get the tables that belong to the database. |
||||
131 | * |
||||
132 | * @param string|string[]|null $schema |
||||
133 | * @return array<mixed> |
||||
134 | * @throws ArangoException |
||||
135 | */ |
||||
136 | 55 | public function getTables($schema = null) |
|||
137 | { |
||||
138 | 55 | unset($schema); |
|||
139 | |||||
140 | 55 | return $this->mapResultsToArray( |
|||
141 | 55 | $this->schemaManager->getCollections(true), |
|||
142 | 55 | ); |
|||
143 | } |
||||
144 | |||||
145 | /** |
||||
146 | * Rename a table (collection). |
||||
147 | * |
||||
148 | * @param string $from |
||||
149 | * @param string $to |
||||
150 | * |
||||
151 | * @throws ArangoException |
||||
152 | */ |
||||
153 | 2 | public function rename($from, $to): bool |
|||
154 | { |
||||
155 | 2 | return (bool) $this->schemaManager->renameCollection($from, $to); |
|||
156 | } |
||||
157 | |||||
158 | /** |
||||
159 | * Drop a table (collection) from the schema. |
||||
160 | * |
||||
161 | * @throws ArangoException |
||||
162 | */ |
||||
163 | 23 | public function drop($table) |
|||
164 | { |
||||
165 | 23 | $this->schemaManager->deleteCollection($table); |
|||
166 | } |
||||
167 | |||||
168 | /** |
||||
169 | * Drop all tables (collections) from the schema. |
||||
170 | * |
||||
171 | * @throws ArangoException |
||||
172 | */ |
||||
173 | 45 | public function dropAllTables(): void |
|||
174 | { |
||||
175 | 45 | $collections = $this->getTables(true); |
|||
0 ignored issues
–
show
true of type true is incompatible with the type null|string|string[] expected by parameter $schema of LaravelFreelancerNL\Aran...ma\Builder::getTables() .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
176 | |||||
177 | 45 | foreach ($collections as $name) { |
|||
178 | 45 | $this->schemaManager->deleteCollection($name['name']); |
|||
179 | } |
||||
180 | } |
||||
181 | |||||
182 | /** |
||||
183 | * Determine if the given table has a given column. |
||||
184 | * |
||||
185 | * @param string $table |
||||
186 | * @param string|string[] $column |
||||
187 | * @return bool |
||||
188 | */ |
||||
189 | 7 | public function hasColumn($table, $column) |
|||
190 | { |
||||
191 | 7 | return $this->hasColumns($table, $column); |
|||
192 | } |
||||
193 | |||||
194 | /** |
||||
195 | * Determine if the given table has given columns. |
||||
196 | * |
||||
197 | * @param string $table |
||||
198 | * @param string|string[] $columns |
||||
199 | * @return array<mixed> |
||||
200 | */ |
||||
201 | 8 | public function getColumns($table) |
|||
202 | { |
||||
203 | 8 | $compilation = $this->grammar->compileColumns(null, $table); |
|||
204 | |||||
205 | 8 | $rawColumns = $this->connection->select($compilation['aqb'], $compilation['bindings']); |
|||
206 | |||||
207 | 8 | return $this->mapResultsToArray($rawColumns); |
|||
208 | } |
||||
209 | |||||
210 | /** |
||||
211 | * Determine if the given table has given columns. |
||||
212 | * |
||||
213 | * @param string $table |
||||
214 | * @param string|string[] $columns |
||||
215 | * @return bool |
||||
216 | */ |
||||
217 | 7 | public function hasColumns($table, $columns) |
|||
218 | { |
||||
219 | 7 | if (is_string($columns)) { |
|||
220 | 5 | $columns = [$columns]; |
|||
221 | } |
||||
222 | |||||
223 | 7 | $parameters = []; |
|||
224 | 7 | $parameters['name'] = 'hasColumn'; |
|||
225 | 7 | $parameters['handler'] = 'aql'; |
|||
226 | 7 | $parameters['columns'] = $columns; |
|||
227 | |||||
228 | 7 | $command = new Fluent($parameters); |
|||
229 | |||||
230 | 7 | $compilation = $this->grammar->compileHasColumn($table, $command); |
|||
231 | 7 | return $this->connection->select($compilation['aqb'])[0]; |
|||
232 | } |
||||
233 | |||||
234 | /** |
||||
235 | * Create a database in the schema. |
||||
236 | * |
||||
237 | * @param string $name |
||||
238 | * @return bool |
||||
239 | * |
||||
240 | * @throws ArangoException |
||||
241 | */ |
||||
242 | 4 | public function createDatabase($name) |
|||
243 | { |
||||
244 | 4 | return $this->schemaManager->createDatabase($name); |
|||
245 | } |
||||
246 | |||||
247 | /** |
||||
248 | * Drop a database from the schema if the database exists. |
||||
249 | * |
||||
250 | * @param string $name |
||||
251 | * @return bool |
||||
252 | * |
||||
253 | * @throws ArangoException |
||||
254 | */ |
||||
255 | 4 | public function dropDatabaseIfExists($name) |
|||
256 | { |
||||
257 | 4 | if ($this->schemaManager->hasDatabase($name)) { |
|||
258 | 2 | return $this->schemaManager->deleteDatabase($name); |
|||
259 | } |
||||
260 | |||||
261 | 2 | return true; |
|||
262 | } |
||||
263 | |||||
264 | /** |
||||
265 | * Get the database connection instance. |
||||
266 | * |
||||
267 | * @return Connection |
||||
268 | */ |
||||
269 | 1 | public function getConnection() |
|||
270 | { |
||||
271 | 1 | return $this->connection; |
|||
272 | } |
||||
273 | |||||
274 | /** |
||||
275 | * @throws QueryException |
||||
276 | */ |
||||
277 | 70 | protected function handleExceptionsAsQueryExceptions(Closure $callback): mixed |
|||
278 | { |
||||
279 | try { |
||||
280 | 70 | return $callback(); |
|||
281 | 1 | } catch (\Exception $e) { |
|||
282 | 1 | throw new QueryException($this->connection->getName(), $e->getMessage(), [], $e); |
|||
283 | } |
||||
284 | } |
||||
285 | |||||
286 | /** |
||||
287 | * Disable foreign key constraints during the execution of a callback. |
||||
288 | * |
||||
289 | * ArangoDB doesn't have foreign keys so this is just a dummy to keep things working |
||||
290 | * for functionality that expect this method. |
||||
291 | * |
||||
292 | * @param \Closure $callback |
||||
293 | * @return mixed |
||||
294 | */ |
||||
295 | 1 | public function withoutForeignKeyConstraints(Closure $callback) |
|||
296 | { |
||||
297 | 1 | return $callback(); |
|||
298 | } |
||||
299 | |||||
300 | /** |
||||
301 | * @param mixed[] $results |
||||
302 | * @return mixed[] |
||||
303 | */ |
||||
304 | 65 | protected function mapResultsToArray($results) |
|||
305 | { |
||||
306 | 65 | return array_map(function ($result) { return (array) $result; }, $results); |
|||
307 | } |
||||
308 | |||||
309 | /** |
||||
310 | * Silently catch the use of unsupported builder methods. |
||||
311 | */ |
||||
312 | 2 | public function __call($method, $parameters) |
|||
313 | { |
||||
314 | 2 | Log::warning("The ArangoDB driver's schema builder doesn't support method '$method'\n"); |
|||
315 | } |
||||
316 | } |
||||
317 |