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