Passed
Push — master ( e4370e...efd9d8 )
by Sébastien
02:43 queued 12s
created

BulkInsertSqlCompiler::getBindings()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 22
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 22
ccs 12
cts 12
cp 1
rs 9.6111
cc 5
nc 5
nop 1
crap 5
1
<?php
2
3
namespace Bdf\Prime\Query\Custom\BulkInsert;
4
5
use Bdf\Prime\Query\CompilableClause;
6
use Bdf\Prime\Query\Compiler\AbstractCompiler;
7
use Bdf\Prime\Types\TypeInterface;
8
9
/**
10
 * Compiler for @see BulkInsertQuery
11
 *
12
 * The query will be compiled into a prepared statement
0 ignored issues
show
introduced by
Doc comment long description must end with a full stop
Loading history...
13
 */
14
class BulkInsertSqlCompiler extends AbstractCompiler
15
{
16
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $query should have a doc-comment as per coding-style.
Loading history...
17
     * {@inheritdoc}
18
     */
19 411
    protected function doCompileInsert(CompilableClause $query)
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before function; 0 found
Loading history...
20
    {
21 411
        $sql = $this->compileMode($query).' INTO '.$this->quoteIdentifier($query, $query->statements['table']);
22
23 411
        if (!isset($query->state()->compiledParts['columns'])) {
24 118
            $this->compileColumns($query);
25
        }
26
27 411
        $sql .= $query->state()->compiledParts['columns']['sql'].$this->compileValues($query);
28
29 411
        return $this->connection->prepare($sql);
0 ignored issues
show
Bug introduced by
The method prepare() does not exist on Bdf\Prime\Connection\ConnectionInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to Bdf\Prime\Connection\ConnectionInterface. ( Ignorable by Annotation )

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

29
        return $this->connection->/** @scrutinizer ignore-call */ prepare($sql);
Loading history...
30
    }
31
32
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $query should have a doc-comment as per coding-style.
Loading history...
33
     * {@inheritdoc}
34
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
35
    protected function doCompileUpdate(CompilableClause $query)
0 ignored issues
show
Coding Style introduced by
The method parameter $query is never used
Loading history...
36
    {
37
        throw new \BadMethodCallException();
38
    }
39
40
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $query should have a doc-comment as per coding-style.
Loading history...
41
     * {@inheritdoc}
42
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
43
    protected function doCompileDelete(CompilableClause $query)
0 ignored issues
show
Coding Style introduced by
The method parameter $query is never used
Loading history...
44
    {
45
        throw new \BadMethodCallException();
46
    }
47
48
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $query should have a doc-comment as per coding-style.
Loading history...
49
     * {@inheritdoc}
50
     */
0 ignored issues
show
Coding Style Documentation introduced by
Missing @throws tag in function comment
Loading history...
51
    protected function doCompileSelect(CompilableClause $query)
0 ignored issues
show
Coding Style introduced by
The method parameter $query is never used
Loading history...
52
    {
53
        throw new \BadMethodCallException();
54
    }
55
56
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $query should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $column should have a doc-comment as per coding-style.
Loading history...
57
     * {@inheritdoc}
58
     */
59 411
    public function quoteIdentifier(CompilableClause $query, $column)
60
    {
61 411
        if (!$query->isQuoteIdentifier()) {
62 410
            return $column;
63
        }
64
65 1
        return $this->platform()->grammar()->quoteIdentifier($column);
66
    }
67
68
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $query should have a doc-comment as per coding-style.
Loading history...
69
     * {@inheritdoc}
70
     */
71 422
    public function getBindings(CompilableClause $query)
72
    {
73 422
        if ($query->statements['bulk']) {
74 5
            $bindings = [];
75
76 5
            foreach ($query->statements['values'] as $values) {
77 5
                foreach ($query->state()->compiledParts['columns']['types'] as $field => $type) {
78 5
                    $bindings[] = $this->platform()->types()->toDatabase($values[$field] ?? null, $type);
0 ignored issues
show
Coding Style introduced by
Operation must be bracketed
Loading history...
79
                }
80
            }
81
82 5
            return $bindings;
83
        }
84
85 417
        $values = $query->statements['values'][0];
86 417
        $bindings = [];
87
88 417
        foreach ($query->state()->compiledParts['columns']['types'] as $field => $type) {
89 417
            $bindings[] = $this->platform()->types()->toDatabase($values[$field] ?? null, $type);
0 ignored issues
show
Coding Style introduced by
Operation must be bracketed
Loading history...
90
        }
91
92 417
        return $bindings;
93
    }
94
95
    /**
96
     * Compile the INSERT mode
97
     *
98
     * @param CompilableClause $query
99
     *
100
     * @return string
101
     */
102 411
    private function compileMode(CompilableClause $query)
0 ignored issues
show
Coding Style introduced by
Private method name "BulkInsertSqlCompiler::compileMode" must be prefixed with an underscore
Loading history...
103
    {
104 411
        switch ($query->statements['mode']) {
105 411
            case BulkInsertQuery::MODE_REPLACE:
106 3
                return 'REPLACE';
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
107
108 410
            case BulkInsertQuery::MODE_IGNORE:
109 7
                if ($this->platform()->grammar()->getName() === 'sqlite') {
110 7
                    return 'INSERT OR IGNORE';
111
                } else {
112
                    return 'INSERT IGNORE';
113
                }
114
                break;
0 ignored issues
show
introduced by
Case breaking statement indented incorrectly; expected 14 spaces, found 16
Loading history...
115
        }
116
117 408
        return 'INSERT';
118
    }
119
120
    /**
121
     * Compile columns, and resolve types
122
     *
123
     * @param CompilableClause $query
124
     */
125 118
    private function compileColumns(CompilableClause $query)
0 ignored issues
show
Coding Style introduced by
Private method name "BulkInsertSqlCompiler::compileColumns" must be prefixed with an underscore
Loading history...
126
    {
127 118
        $columns = [];
128 118
        $types = [];
129
130 118
        foreach ($query->statements['columns'] as $column) {
131 118
            if (!empty($column['type'])) {
132 1
                $types[$column['name']] = $column['type'];
133 1
                $type = null;
134
            } else {
135 117
                $types[$column['name']] = null;
136 117
                $type = true;
137
            }
138
139 118
            $columns[] = $this->quoteIdentifier($query, $query->preprocessor()->field($column['name'], $type));
140
141 118
            if ($type instanceof TypeInterface) {
142 118
                $types[$column['name']] = $type;
143
            }
144
        }
145
146 118
        $query->state()->compiledParts['columns'] = [
147 118
            'sql'    => '('.implode(', ', $columns).') ',
148 118
            'types'  => $types,
149 118
            'values' => '('.str_repeat('?, ', count($types) - 1).'?)',
0 ignored issues
show
Coding Style introduced by
Operation must be bracketed
Loading history...
150
        ];
151 118
    }
152
153
    /**
154
     * Compile values for bulk INSERT query
155
     *
156
     * @param CompilableClause $query
157
     *
158
     * @return string
159
     */
160 411
    private function compileValues(CompilableClause $query)
0 ignored issues
show
Coding Style introduced by
Private method name "BulkInsertSqlCompiler::compileValues" must be prefixed with an underscore
Loading history...
161
    {
162 411
        if (!$query->statements['bulk']) {
163 405
            return 'VALUES '.$query->state()->compiledParts['columns']['values'];
164
        }
165
166 6
        $values = $query->state()->compiledParts['columns']['values'];
167
168 6
        return 'VALUES '.str_repeat($values.', ', count($query->statements['values']) - 1).$values;
0 ignored issues
show
Coding Style introduced by
Operation must be bracketed
Loading history...
169
    }
170
}
171