1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LaravelFreelancerNL\Aranguent\Schema; |
6
|
|
|
|
7
|
|
|
use Illuminate\Database\Schema\Grammars\Grammar as IlluminateGrammar; |
8
|
|
|
use Illuminate\Support\Fluent; |
9
|
|
|
use LaravelFreelancerNL\FluentAQL\QueryBuilder; |
10
|
|
|
|
11
|
|
|
class Grammar extends IlluminateGrammar |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* ArangoDB handles transactions itself. However most schema actions |
15
|
|
|
* are not done through queries but rather through commands. |
16
|
|
|
* So we just run schema actions sequentially. |
17
|
|
|
* |
18
|
|
|
* @var bool |
19
|
|
|
*/ |
20
|
|
|
protected $transactions = false; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Compile AQL to check if an attribute is in use within a document in the collection. |
24
|
|
|
* If multiple attributes are set then all must be set in one document. |
25
|
|
|
* |
26
|
|
|
* @param string $table |
27
|
|
|
* @return Fluent |
28
|
|
|
* @throws BindException |
29
|
|
|
*/ |
30
|
|
|
public function compileHasColumn($table, Fluent $command) |
31
|
|
|
{ |
32
|
|
|
$attributes = $command->getAttributes(); |
33
|
|
|
|
34
|
|
|
$aqb = new QueryBuilder(); |
35
|
|
|
|
36
|
|
|
$filter = []; |
37
|
|
|
foreach ($attributes['columns'] as $column) { |
38
|
|
|
$filter[] = [$aqb->rawExpression('HAS(doc, \'' . $column . '\')')]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
$command->aqb = |
42
|
|
|
$aqb->let( |
43
|
|
|
'columnFound', |
44
|
|
|
$aqb->first( |
45
|
|
|
(new QueryBuilder())->for('doc', $table) |
46
|
|
|
->filter($filter) |
47
|
|
|
->limit(1) |
48
|
|
|
->return('true') |
49
|
|
|
) |
50
|
|
|
)->return($aqb->rawExpression('columnFound == true')) |
51
|
|
|
->get(); |
52
|
|
|
|
53
|
|
|
return $command; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Compile AQL to rename an attribute, if the new name isn't already in use. |
58
|
|
|
* |
59
|
|
|
* @param string $table |
60
|
|
|
* @param Fluent $command |
61
|
|
|
* @return Fluent |
62
|
|
|
*/ |
63
|
|
|
public function compileRenameAttribute($table, Fluent $command) |
64
|
|
|
{ |
65
|
|
|
$attributes = $command->getAttributes(); |
66
|
|
|
|
67
|
|
|
$filter = [ |
68
|
|
|
['doc.' . $attributes['from'], '!=', null], |
69
|
|
|
['doc.' . $attributes['to'], '==', null], |
70
|
|
|
]; |
71
|
|
|
|
72
|
|
|
$aqb = (new QueryBuilder())->for('doc', $table) |
73
|
|
|
->filter($filter) |
74
|
|
|
->update( |
75
|
|
|
'doc', |
76
|
|
|
[ |
77
|
|
|
$attributes['from'] => null, |
78
|
|
|
$attributes['to'] => 'doc.' . $command->from, |
79
|
|
|
], |
80
|
|
|
$table |
81
|
|
|
) |
82
|
|
|
->options(['keepNull' => false]) |
83
|
|
|
->get(); |
84
|
|
|
|
85
|
|
|
$command->aqb = $aqb; |
86
|
|
|
|
87
|
|
|
return $command; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Compile AQL to drop one or more attributes. |
92
|
|
|
* |
93
|
|
|
* @param string $table |
94
|
|
|
* @param Fluent $command |
95
|
|
|
* @return Fluent |
96
|
|
|
*/ |
97
|
|
|
public function compileDropColumn($table, Fluent $command) |
98
|
|
|
{ |
99
|
|
|
$filter = []; |
100
|
|
|
$attributes = $command->getAttributes(); |
101
|
|
|
|
102
|
|
|
$data = []; |
103
|
|
|
foreach ($attributes['attributes'] as $attribute) { |
104
|
|
|
$filter[] = ['doc.' . $attribute, '!=', null, 'OR']; |
105
|
|
|
$data[$attribute] = null; |
106
|
|
|
} |
107
|
|
|
$aqb = (new QueryBuilder())->for('doc', $table) |
108
|
|
|
->filter($filter) |
109
|
|
|
->update('doc', $data, $table) |
110
|
|
|
->options(['keepNull' => false]) |
111
|
|
|
->get(); |
112
|
|
|
|
113
|
|
|
$command->aqb = $aqb; |
114
|
|
|
|
115
|
|
|
return $command; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Prepare a bindVar for inclusion in an AQL query. |
120
|
|
|
*/ |
121
|
|
|
public function wrapBindVar(string $attribute): string |
122
|
|
|
{ |
123
|
|
|
$attribute = trim($attribute); |
124
|
|
|
if (strpos($attribute, '@') === 0) { |
125
|
|
|
$attribute = "`$attribute`"; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
return $attribute; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|