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