Passed
Push — next ( 0a206d...d05cf7 )
by Bas
03:59
created

Grammar::compileDropAttribute()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 3.0026

Importance

Changes 0
Metric Value
cc 3
eloc 14
nc 4
nop 2
dl 0
loc 21
ccs 14
cts 15
cp 0.9333
crap 3.0026
rs 9.7998
c 0
b 0
f 0
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\QueryBuilder;
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
     *
24
     * @param string $collection
25
     * @param Fluent $command
26
     *
27
     * @return Fluent
28
     */
29 2
    public function compileHasAttribute($collection, Fluent $command)
30
    {
31 2
        if (is_string($command->attribute)) {
32
            $command->attribute = [$command->attribute];
33
        }
34
35 2
        $filter = [];
36 2
        foreach ($command->attribute as $attribute) {
37 2
            $filter[] = ['doc.' . $attribute, '!=', null];
38
        }
39
40 2
        $aqb = (new QueryBuilder())->for('doc', $collection)
41 2
            ->filter($filter)
42 2
            ->limit(1)
43 2
            ->return('true')
44 2
            ->get();
45
46 2
        $command->aqb = $aqb;
47
48 2
        return $command;
49
    }
50
51
    /**
52
     * Compile AQL to rename an attribute, if the new name isn't already in use.
53
     *
54
     * @param string $collection
55
     * @param Fluent $command
56
     *
57
     * @return Fluent
58
     */
59 1
    public function compileRenameAttribute($collection, Fluent $command)
60
    {
61 1
        $filter = [
62 1
            ['doc.' . $command->from, '!=', null],
63 1
            ['doc.' . $command->to, '==', null],
64
        ];
65
66 1
        $aqb = (new QueryBuilder())->for('doc', $collection)
67 1
            ->filter($filter)
68 1
            ->update(
69 1
                'doc',
70
                [
71 1
                    $command->from => null,
72 1
                    $command->to   => 'doc.' . $command->from,
73
                ],
74
                $collection
75
            )
76 1
            ->options(['keepNull' => true])
77 1
            ->get();
78
79 1
        $command->aqb = $aqb;
80
81 1
        return $command;
82
    }
83
84
    /**
85
     * Compile AQL to drop one or more attributes.
86
     *
87
     * @param string $collection
88
     * @param Fluent $command
89
     *
90
     * @return Fluent
91
     */
92 1
    public function compileDropAttribute($collection, Fluent $command)
93
    {
94 1
        $filter = [];
95 1
        if (is_string($command->attributes)) {
0 ignored issues
show
introduced by
The condition is_string($command->attributes) is always false.
Loading history...
96
            $command->attributes = [$command->attributes];
97
        }
98
99 1
        $data = [];
100 1
        foreach ($command->attributes as $attribute) {
101 1
            $filter[] = ['doc.' . $attribute, '!=', null, 'OR'];
102 1
            $data[$attribute] = null;
103
        }
104 1
        $aqb = (new QueryBuilder())->for('doc', $collection)
105 1
            ->filter($filter)
106 1
            ->update('doc', $data, $collection)
107 1
            ->options(['keepNull' => false])
108 1
            ->get();
109
110 1
        $command->aqb = $aqb;
111
112 1
        return $command;
113
    }
114
115
    /**
116
     * Prepare a bindVar for inclusion in an AQL query.
117
     *
118
     * @param $attribute
119
     *
120
     * @return string
121
     */
122 1
    public function wrapBindVar($attribute)
123
    {
124 1
        $attribute = trim($attribute);
125 1
        if (strpos($attribute, '@') === 0) {
126 1
            $attribute = "`$attribute`";
127
        }
128
129 1
        return $attribute;
130
    }
131
}
132