Passed
Pull Request — next (#61)
by Bas
05:16 queued 02:40
created

Builder::createDatabase()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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\Database\Connection as IlluminateConnection;
11
use Illuminate\Support\Facades\Log;
12
use Illuminate\Support\Fluent;
13
use LaravelFreelancerNL\Aranguent\Connection;
14
use LaravelFreelancerNL\Aranguent\QueryException;
15
use LaravelFreelancerNL\Aranguent\Schema\Concerns\UsesBlueprints;
16
use stdClass;
17
18
class Builder extends \Illuminate\Database\Schema\Builder
19
{
20
    use UsesBlueprints;
21
22
    /**
23
     * The database connection instance.
24
     *
25
     * @var IlluminateConnection
26
     */
27
    protected $connection;
28
29
    protected SchemaManager $schemaManager;
30
31
    /**
32
     * The schema grammar instance.
33
     *
34
     * @var Grammar
35
     */
36
    protected $grammar;
37
38
    /**
39
     * Create a new database Schema manager.
40
     *
41
     * Builder constructor.
42
     *
43
     * @param Connection $connection
44
     */
45 28
    public function __construct(Connection $connection)
46
    {
47 28
        $this->connection = $connection;
48
49 28
        $this->grammar = $connection->getSchemaGrammar();
50
51 28
        $this->schemaManager = $connection->getArangoClient()->schema();
52 28
    }
53
54
    /**
55
     * Determine if the given table exists.
56
     *
57
     * @param string $table
58
     * @return bool
59
     */
60 4
    public function hasTable($table)
61
    {
62 4
        return $this->handleExceptionsAsQueryExceptions(function () use ($table) {
63 4
            return $this->schemaManager->hasCollection($table);
64 4
        });
65
    }
66
67
68
    /**
69
     * @throws ArangoException
70
     */
71
    public function dropIfExists($table): void
72
    {
73
        $tableExists = $this->hasTable($table);
74
        if ($tableExists) {
75
            $this->drop($table);
76
        }
77
    }
78
79
    /**
80
     * Get all the tables for the database; excluding ArangoDB system collections
81
     *
82
     * @return array<mixed>
83
     * @throws ArangoException
84
     */
85 2
    public function getAllTables(): array
86
    {
87 2
        return $this->schemaManager->getCollections(true);
88
    }
89
90
    /**
91
     * Rename a table (collection).
92
     *
93
     * @param string $from
94
     * @param string $to
95
     * @throws ArangoException
96
     */
97 1
    public function rename($from, $to): bool
98
    {
99 1
        return (bool) $this->schemaManager->renameCollection($from, $to);
100
    }
101
102
    /**
103
     * Drop a table (collection) from the schema.
104
     *
105
     * @throws ArangoException
106
     */
107
    public function drop($table)
108
    {
109
        $this->schemaManager->deleteCollection($table);
110
    }
111
112
    /**
113
     * Drop all tables (collections) from the schema.
114
     *
115
     * @throws ArangoException
116
     */
117 2
    public function dropAllTables(): void
118
    {
119 2
        $collections = $this->getAllTables();
120
121 2
        foreach ($collections as $name) {
122 2
            $this->schemaManager->deleteCollection($name->name);
123
        }
124 2
    }
125
126
    /**
127
     * Determine if the given table has a given column.
128
     *
129
     * @param  string  $table
130
     * @param  string  $column
131
     * @return bool
132
     */
133 1
    public function hasColumn($table, $column)
134
    {
135 1
        if (is_string($column)) {
0 ignored issues
show
introduced by
The condition is_string($column) is always true.
Loading history...
136 1
            $column = [$column];
137
        }
138
139 1
        return $this->hasColumns($table, $column);
140
    }
141
142
    /**
143
     * Determine if the given table has given columns.
144
     *
145
     * @param  string  $table
146
     * @param  array  $columns
147
     * @return bool
148
     */
149 1
    public function hasColumns($table, array $columns)
150
    {
151 1
        $parameters = [];
152 1
        $parameters['name'] = 'hasAttribute';
153 1
        $parameters['handler'] = 'aql';
154 1
        $parameters['columns'] = $columns;
155
156 1
        $command = new Fluent($parameters);
157 1
        $compilation = $this->grammar->compileHasColumn($table, $command);
158
159 1
        return $this->connection->statement($compilation['aql']);
160
    }
161
162
    /**
163
     * @param  array<mixed> $properties
164
     * @throws ArangoException
165
     */
166 8
    public function createView(string $name, array $properties, string $type = 'arangosearch')
167
    {
168 8
        $view = $properties;
169 8
        $view['name'] = $name;
170 8
        $view['type'] = $type;
171
172 8
        $this->schemaManager->createView($view);
173 8
    }
174
175
    /**
176
     * @throws ArangoException
177
     */
178 1
    public function getView(string $name): \stdClass
179
    {
180 1
        return $this->schemaManager->getView($name);
181
    }
182
183 2
    public function hasView(string $view): bool
184
    {
185 2
        return $this->handleExceptionsAsQueryExceptions(function () use ($view) {
186 2
            return $this->schemaManager->hasView($view);
187 2
        });
188
    }
189
190
    /**
191
     * @throws ArangoException
192
     */
193 1
    public function getAllViews(): array
194
    {
195 1
        return $this->schemaManager->getViews();
196
    }
197
198
    /**
199
     * @param  string  $name
200
     * @param  array  $properties
201
     * @throws ArangoException
202
     */
203 1
    public function editView(string $name, array $properties)
204
    {
205 1
        $this->schemaManager->updateView($name, $properties);
206 1
    }
207
208
    /**
209
     * @param  string  $from
210
     * @param  string  $to
211
     *
212
     * @throws ArangoException
213
     */
214 1
    public function renameView(string $from, string $to)
215
    {
216 1
        $this->schemaManager->renameView($from, $to);
217 1
    }
218
219
    /**
220
     * @param  string  $name
221
     * @throws ArangoException
222
     */
223 1
    public function dropView(string $name)
224
    {
225 1
        $this->schemaManager->deleteView($name);
226 1
    }
227
228
    /**
229
     * @param string $name
230
     * @return bool
231
     * @throws ArangoException
232
     */
233 2
    public function dropViewIfExists(string $name): bool
234
    {
235 2
        if ($this->hasView($name)) {
236 1
            $this->schemaManager->deleteView($name);
237
        }
238
239 2
        return true;
240
    }
241
242
    /**
243
     * Drop all views from the schema.
244
     *
245
     * @throws ArangoException
246
     */
247 1
    public function dropAllViews(): void
248
    {
249 1
        $views = $this->schemaManager->getViews();
250
251 1
        foreach ($views as $view) {
252 1
            $this->schemaManager->deleteView($view->name);
253
        }
254 1
    }
255
256
    /**
257
     * Create a database in the schema.
258
     *
259
     * @param string $name
260
     * @return bool
261
     *
262
     * @throws ArangoException
263
     */
264 2
    public function createDatabase($name)
265
    {
266 2
        return $this->schemaManager->createDatabase($name);
267
    }
268
269
    /**
270
     * Drop a database from the schema if the database exists.
271
     *
272
     * @param string $name
273
     * @return bool
274
     *
275
     * @throws ArangoException
276
     */
277 2
    public function dropDatabaseIfExists($name)
278
    {
279 2
        if ($this->schemaManager->hasDatabase($name)) {
280 1
            return $this->schemaManager->deleteDatabase($name);
281
        }
282
283 1
        return true;
284
    }
285
286
287
    /**
288
     * Get the database connection instance.
289
     *
290
     * @return Connection
291
     */
292 1
    public function getConnection()
293
    {
294 1
        return $this->connection;
295
    }
296
297
    /**
298
     * @throws QueryException
299
     */
300 6
    protected function handleExceptionsAsQueryExceptions(Closure $callback): mixed
301
    {
302
        try {
303 6
            return $callback();
304 1
        } catch (\Exception $e) {
305 1
            throw new QueryException($e->getMessage(), [], $e);
306
        }
307
    }
308
309
    /**
310
     * Silently catch the use of unsupported builder methods.
311
     */
312
    public function __call(string $method, mixed $args): void
313
    {
314
        Log::warning("The Aranguent Schema Builder doesn't support method '$method'\n");
315
    }
316
}
317