Completed
Push — devops/catch-breaking-changes-... ( 88c9a6 )
by Bas
28s queued 18s
created

Grammar::compileDropAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

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