Passed
Pull Request — master (#13)
by Vincent
07:15
created

BulkInsertQuery::bulk()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
dl 0
loc 6
c 1
b 0
f 0
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace Bdf\Prime\Query\Custom\BulkInsert;
4
5
use Bdf\Prime\Connection\ConnectionInterface;
6
use Bdf\Prime\Query\CommandInterface;
7
use Bdf\Prime\Query\CompilableClause;
8
use Bdf\Prime\Query\Compiler\CompilerInterface;
9
use Bdf\Prime\Query\Compiler\CompilerState;
10
use Bdf\Prime\Query\Compiler\Preprocessor\DefaultPreprocessor;
11
use Bdf\Prime\Query\Compiler\Preprocessor\PreprocessorInterface;
12
use Bdf\Prime\Query\Contract\Cachable;
13
use Bdf\Prime\Query\Contract\Compilable;
14
use Bdf\Prime\Query\Contract\Query\InsertQueryInterface;
15
use Bdf\Prime\Query\Contract\WriteOperation;
16
use Bdf\Prime\Query\Extension\CachableTrait;
17
18
/**
19
 * Handle optimised insert query
20
 *
21
 * <code>
22
 * // Simple insert
23
 * $insert
24
 *     ->into('perforn')
25
 *     ->values([
26
 *         'first_name' => 'John',
27
 *         'last_name'  => 'Doe'
28
 *     ])
29
 *     ->execute()
30
 * ;
31
 *
32
 * // Bulk insert
33
 * $insert
34
 *     ->bulk()
35
 *     ->values([
36
 *         'first_name' => 'Alan',
37
 *         'last_name'  => 'Smith'
38
 *     ])
39
 *     ->values([
40
 *         'first_name' => 'Mickey',
41
 *         'last_name'  => 'Mouse'
42
 *     ])
43
 *     ->execute()
44
 * ;
45
 * </code>
46
 */
47
class BulkInsertQuery extends CompilableClause implements CommandInterface, Compilable, Cachable, InsertQueryInterface
48
{
49
    use CachableTrait;
50
51
    /**
52
     * The DBAL Connection.
53
     *
54
     * @var ConnectionInterface
55
     */
56
    protected $connection;
57
58
    /**
59
     * The SQL compiler
60
     *
61
     * @var CompilerInterface
62
     */
63
    protected $compiler;
64
65
66
    /**
67
     * BulkInsertQuery constructor.
68
     *
69
     * @param ConnectionInterface $connection
70
     * @param PreprocessorInterface|null $preprocessor
71
     */
72 119
    public function __construct(ConnectionInterface $connection, PreprocessorInterface $preprocessor = null)
0 ignored issues
show
Coding Style introduced by
Expected 1 blank line before function; 2 found
Loading history...
73
    {
74 119
        parent::__construct($preprocessor ?: new DefaultPreprocessor(), new CompilerState());
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
75
76 119
        $this->on($connection);
77
78 119
        $this->statements = [
79 119
            'table'   => null,
80
            'columns' => [],
81
            'values'  => [],
82 119
            'mode'    => self::MODE_INSERT,
83
            'bulk'    => false
0 ignored issues
show
introduced by
A comma should follow the last multiline array item. Found: false
Loading history...
84
        ];
85 119
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90
    public function compiler()
91
    {
92
        return $this->compiler;
93
    }
94
95
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $compiler should have a doc-comment as per coding-style.
Loading history...
96
     * {@inheritdoc}
97
     */
98
    public function setCompiler(CompilerInterface $compiler)
99
    {
100
        $this->compiler = $compiler;
101
102
        return $this;
103
    }
104
105
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $connection should have a doc-comment as per coding-style.
Loading history...
106
     * {@inheritdoc}
107
     */
108 119
    public function on(ConnectionInterface $connection)
109
    {
110 119
        $this->connection = $connection;
111 119
        $this->compiler = $connection->factory()->compiler(static::class);
112
113 119
        return $this;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119
    public function connection()
120
    {
121
        return $this->connection;
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127
    #[WriteOperation]
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
Coding Style introduced by
Perl-style comments are not allowed; use "// Comment" instead
Loading history...
Coding Style introduced by
Perl-style comments are not allowed. Use "// Comment." or "/* comment */" instead.
Loading history...
128 413
    public function execute($columns = null)
0 ignored issues
show
Coding Style introduced by
The method parameter $columns is never used
Loading history...
Coding Style introduced by
You must use "/**" style comments for a function comment
Loading history...
129
    {
130 413
        $nb = $this->connection->execute($this)->count();
131
132 411
        if ($nb > 0) {
133 411
            $this->clearCacheOnWrite();
134
        }
135
136 411
        return $nb;
137
    }
138
139
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $table should have a doc-comment as per coding-style.
Loading history...
140
     * {@inheritdoc}
141
     */
142 118
    public function into($table)
143
    {
144 118
        $this->compilerState->invalidate('table');
145 118
        $this->compilerState->invalidate('columns');
146
147
        // Reset columns when changing table
148 118
        $this->statements['columns'] = [];
149 118
        $this->statements['table'] = $table;
150
151 118
        return $this;
152
    }
153
154
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $from should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $alias should have a doc-comment as per coding-style.
Loading history...
155
     * {@inheritdoc}
156
     *
157
     * Alias of into for compatibility purpose with interface
0 ignored issues
show
introduced by
Doc comment long description must end with a full stop
Loading history...
158
     *
159
     * @see BulkInsertQuery::into()
160
     */
161 68
    public function from($from, $alias = null)
0 ignored issues
show
Coding Style introduced by
The method parameter $alias is never used
Loading history...
162
    {
163 68
        return $this->into($from);
164
    }
165
166
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $columns should have a doc-comment as per coding-style.
Loading history...
167
     * {@inheritdoc}
168
     */
169 118
    public function columns(array $columns)
170
    {
171 118
        $this->compilerState->invalidate('columns');
172
173 118
        $this->statements['columns'] = [];
174
175 118
        foreach ($columns as $name => $type) {
176 118
            if (is_int($name)) {
177 117
                $this->statements['columns'][] = [
178 117
                    'name' => $type,
179
                    'type' => null
0 ignored issues
show
introduced by
A comma should follow the last multiline array item. Found: null
Loading history...
180
                ];
181
            } else {
182 1
                $this->statements['columns'][] = [
183 1
                    'name' => $name,
184 118
                    'type' => $type
0 ignored issues
show
introduced by
A comma should follow the last multiline array item. Found: $type
Loading history...
185
                ];
186
            }
187
        }
188
189 118
        return $this;
190
    }
191
192
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $data should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $replace should have a doc-comment as per coding-style.
Loading history...
193
     * {@inheritdoc}
194
     */
195 427
    public function values(array $data, $replace = false)
196
    {
197 427
        if (empty($this->statements['columns'])) {
198 27
            $this->columns(array_keys($data));
199
        }
200
201 427
        if ($this->statements['bulk']) {
202 6
            $this->compilerState->invalidate();
203
        }
204
205 427
        if (!$this->statements['bulk'] || $replace) {
206 421
            $this->statements['values'] = [$data];
207
        } else {
208 6
            $this->statements['values'][] = $data;
209
        }
210
211 427
        return $this;
212
    }
213
214
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $mode should have a doc-comment as per coding-style.
Loading history...
215
     * {@inheritdoc}
216
     */
217 410
    public function mode($mode)
218
    {
219 410
        if ($mode !== $this->statements['mode']) {
220 11
            $this->compilerState->invalidate('mode');
221 11
            $this->statements['mode'] = $mode;
222
        }
223
224 410
        return $this;
225
    }
226
227
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $flag should have a doc-comment as per coding-style.
Loading history...
228
     * {@inheritdoc}
229
     */
230 378
    public function ignore($flag = true)
231
    {
232 378
        return $this->mode($flag ? self::MODE_IGNORE : self::MODE_INSERT);
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
233
    }
234
235
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $flag should have a doc-comment as per coding-style.
Loading history...
236
     * {@inheritdoc}
237
     */
238 2
    public function replace($flag = true)
239
    {
240 2
        return $this->mode($flag ? self::MODE_REPLACE : self::MODE_INSERT);
0 ignored issues
show
Coding Style introduced by
Inline shorthand IF statement requires brackets around comparison
Loading history...
241
    }
242
243
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $flag should have a doc-comment as per coding-style.
Loading history...
244
     * {@inheritdoc}
245
     */
246 8
    public function bulk($flag = true)
247
    {
248 8
        $this->compilerState->invalidate();
249 8
        $this->statements['bulk'] = $flag;
250
251 8
        return $this;
252
    }
253
254
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $forceRecompile should have a doc-comment as per coding-style.
Loading history...
255
     * {@inheritdoc}
256
     */
257 415
    public function compile($forceRecompile = false)
258
    {
259 415
        if (!$forceRecompile && $this->state()->compiled) {
260 348
            return $this->state()->compiled;
261
        }
262
263 399
        return $this->state()->compiled = $this->compiler->compileInsert($this);
0 ignored issues
show
Coding Style introduced by
Assignments must be the first block of code on a line
Loading history...
264
    }
265
266
    /**
267
     * {@inheritdoc}
268
     */
269 413
    public function getBindings()
270
    {
271 413
        return $this->compiler->getBindings($this);
272
    }
273
274
    /**
275
     * {@inheritdoc}
276
     */
277
    public function type()
278
    {
279
        return self::TYPE_INSERT;
280
    }
281
282
    /**
283
     * Get cache namespace
284
     *
285
     * @return string
286
     */
287 2
    protected function cacheNamespace()
288
    {
289 2
        return $this->connection->getName().':'.$this->statements['table'];
290
    }
291
}
292