Passed
Pull Request — next (#137)
by Bas
04:08
created

Grammar::compileDropColumn()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 13
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 19
ccs 14
cts 14
cp 1
crap 2
rs 9.8333
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 7
    public function compileHasColumn($table, Fluent $command)
31
    {
32 7
        $attributes = $command->getAttributes();
33
34 7
        $aqb = new QueryBuilder();
35
36 7
        $filter = [];
37 7
        foreach ($attributes['columns'] as $column) {
38 7
            $filter[] = [$aqb->rawExpression('HAS(doc, \'' . $column . '\')')];
39
        }
40
41 7
        $command->aqb =
42 7
            $aqb->let(
43 7
                'columnFound',
44 7
                $aqb->first(
45 7
                    (new QueryBuilder())->for('doc', $table)
46 7
                        ->filter($filter)
47 7
                        ->limit(1)
48 7
                        ->return('true'),
49 7
                ),
50 7
            )->return($aqb->rawExpression('columnFound == true'))
51 7
                ->get();
52
53 7
        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 1
    public function compileRenameAttribute($table, Fluent $command)
64
    {
65 1
        $attributes = $command->getAttributes();
66
67 1
        $filter = [
68 1
            ['doc.' . $attributes['from'], '!=', null],
69 1
            ['doc.' . $attributes['to'], '==', null],
70 1
        ];
71
72 1
        $aqb = (new QueryBuilder())->for('doc', $table)
73 1
            ->filter($filter)
74 1
            ->update(
75 1
                'doc',
76 1
                [
77 1
                    $attributes['from'] => null,
78 1
                    $attributes['to'] => 'doc.' . $command->from,
79 1
                ],
80 1
                $table,
81 1
            )
82 1
            ->options(['keepNull' => false])
83 1
            ->get();
84
85 1
        $command->aqb = $aqb;
86
87 1
        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 2
    public function compileDropColumn($table, Fluent $command)
98
    {
99 2
        $filter = [];
100 2
        $attributes = $command->getAttributes();
101
102 2
        $data = [];
103 2
        foreach ($attributes['attributes'] as $attribute) {
104 2
            $filter[] = ['doc.' . $attribute, '!=', null, 'OR'];
105 2
            $data[$attribute] = null;
106
        }
107 2
        $aqb = (new QueryBuilder())->for('doc', $table)
108 2
            ->filter($filter)
109 2
            ->update('doc', $data, $table)
110 2
            ->options(['keepNull' => false])
111 2
            ->get();
112
113 2
        $command->aqb = $aqb;
114
115 2
        return $command;
116
    }
117
118
    /**
119
     * Prepare a bindVar for inclusion in an AQL query.
120
     */
121 1
    public function wrapBindVar(string $attribute): string
122
    {
123 1
        $attribute = trim($attribute);
124 1
        if (strpos($attribute, '@') === 0) {
125 1
            $attribute = "`$attribute`";
126
        }
127
128 1
        return $attribute;
129
    }
130
}
131