Failed Conditions
Push — refactor/improve-static-analys... ( ba8000...c6edde )
by Bas
14:06
created

CompilesInserts   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 117
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 43
c 1
b 0
f 0
dl 0
loc 117
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A compileInsertOrIgnore() 0 17 2
A compileInsertGetId() 0 18 2
A compileInsert() 0 14 2
A compileInsertUsing() 0 30 5
1
<?php
2
3
namespace LaravelFreelancerNL\Aranguent\Query\Concerns;
4
5
use Illuminate\Database\Query\Builder as IlluminateQueryBuilder;
6
use LaravelFreelancerNL\Aranguent\Query\Builder;
7
use LaravelFreelancerNL\FluentAQL\Exceptions\BindException as BindException;
8
9
trait CompilesInserts
10
{
11
    /**
12
     * Compile an insert statement into AQL.
13
     *
14
     * @param IlluminateQueryBuilder $query
15
     * @param array   $values
16
     *
17
     * @throws BindException
18
     *
19
     * @return string
20
     */
21
    public function compileInsert(Builder|IlluminateQueryBuilder $query, array $values, string $bindVar = null)
22
    {
23
        $table = $this->prefixTable($query->from);
0 ignored issues
show
Bug introduced by
It seems like prefixTable() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

23
        /** @scrutinizer ignore-call */ 
24
        $table = $this->prefixTable($query->from);
Loading history...
24
25
        if (empty($values)) {
26
            $aql = /** @lang AQL */ 'INSERT {} INTO ' . $table . ' RETURN NEW._key';
27
28
            return $aql;
29
        }
30
31
        return /** @lang AQL */ 'LET values = ' . $bindVar
32
            . ' FOR value IN values'
33
            . ' INSERT value INTO ' . $table
34
            . ' RETURN NEW._key';
35
    }
36
37
    /**
38
     * Compile an insert and get ID statement into SQL.
39
     *
40
     * @param array<mixed> $values
41
     */
42
    public function compileInsertGetId(IlluminateQueryBuilder $query, $values, $sequence = '_key', string $bindVar = null)
43
    {
44
        $table = $this->prefixTable($query->from);
45
46
        $sequence = $this->convertIdToKey($sequence);
0 ignored issues
show
Bug introduced by
It seems like convertIdToKey() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        /** @scrutinizer ignore-call */ 
47
        $sequence = $this->convertIdToKey($sequence);
Loading history...
47
48
        if (empty($values)) {
49
            $aql = /** @lang AQL */ 'INSERT {} INTO ' . $table . ' RETURN NEW.' . $sequence;
50
51
            return $aql;
52
        }
53
54
        $aql = /** @lang AQL */ 'LET values = ' . $bindVar
55
            . ' FOR value IN values'
56
            . ' INSERT value INTO ' . $table
57
            . ' RETURN NEW.' . $sequence;
58
59
        return $aql;
60
    }
61
62
    /**
63
     * Compile an insert statement into AQL.
64
     *
65
     * @param IlluminateQueryBuilder $query
66
     * @param array<mixed> $values
67
     * @return string
68
     */
69
    public function compileInsertOrIgnore(IlluminateQueryBuilder $query, array $values, string $bindVar = null)
70
    {
71
        $table = $this->prefixTable($query->from);
72
73
        if (empty($values)) {
74
            $aql = /** @lang AQL */ "INSERT {} INTO $table RETURN NEW._key";
75
76
            return $aql;
77
        }
78
79
        $aql = /** @lang AQL */ "LET values = $bindVar "
80
            . "FOR value IN values "
81
            . "INSERT value INTO $table "
82
            . "OPTIONS { ignoreErrors: true } "
83
            . "RETURN NEW._key";
84
85
        return $aql;
86
    }
87
88
    /**
89
     * Compile an insert statement using a subquery into SQL.
90
     *
91
     * @param  \Illuminate\Database\Query\Builder  $query
92
     * @param  array  $columns
93
     * @param  string  $sql
94
     * @return string
95
     */
96
    public function compileInsertUsing(IlluminateQueryBuilder $query, array $columns, string $sql)
97
    {
98
        $table = $this->wrapTable($query->from);
0 ignored issues
show
Bug introduced by
It seems like wrapTable() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
        /** @scrutinizer ignore-call */ 
99
        $table = $this->wrapTable($query->from);
Loading history...
99
100
        $insertDoc = '';
101
        if (empty($columns) || $columns === ['*']) {
102
            $insertDoc = 'docDoc';
103
        }
104
105
106
        if ($insertDoc === '') {
107
            $insertValues = [];
108
            foreach($columns as $column) {
109
                $insertValues[$column] = $this->normalizeColumnReferences($query, $column, 'docs');
0 ignored issues
show
Bug introduced by
It seems like normalizeColumnReferences() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
                /** @scrutinizer ignore-call */ 
110
                $insertValues[$column] = $this->normalizeColumnReferences($query, $column, 'docs');
Loading history...
110
            }
111
            $insertDoc = $this->generateAqlObject($insertValues);
0 ignored issues
show
Bug introduced by
It seems like generateAqlObject() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
            /** @scrutinizer ignore-call */ 
112
            $insertDoc = $this->generateAqlObject($insertValues);
Loading history...
112
        }
113
114
115
        //        if ($insertDoc !== '') {
116
        //            $insertValues;
117
        //        }
118
119
        $aql = /** @lang AQL */ 'LET docs = ' . $sql
120
            . ' FOR docDoc IN docs'
121
            . ' INSERT ' . $insertDoc . ' INTO ' . $table
122
            . ' RETURN NEW._key';
123
124
125
        return $aql;
126
    }
127
128
129
}
130