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