MySqlGrammar   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 39
ccs 11
cts 11
cp 1
rs 10
wmc 3

2 Methods

Rating   Name   Duplication   Size   Complexity  
A compileUpsert() 0 11 2
A compileInsertIgnore() 0 5 1
1
<?php
2
3
namespace Staudenmeir\LaravelUpsert\Query\Grammars;
4
5
use Illuminate\Database\Query\Builder;
6
use Illuminate\Database\Query\Grammars\MySqlGrammar as Base;
7
use Illuminate\Support\Str;
8
9
class MySqlGrammar extends Base
10
{
11
    use CompilesUpsertQueries;
12
13
    /**
14
     * Compile an "upsert" statement into SQL.
15
     *
16
     * @param \Illuminate\Database\Query\Builder $query
17
     * @param array $values
18
     * @param array $target
19
     * @param array $update
20
     * @return string
21
     */
22 8
    public function compileUpsert(Builder $query, array $values, array $target, array $update)
23
    {
24 8
        $sql = $this->compileInsert($query, $values).' on duplicate key update ';
25
26 8
        $columns = collect($update)->map(function ($value, $key) {
27 8
            return is_numeric($key)
28 7
                ? $this->wrap($value).' = values('.$this->wrap($value).')'
29 8
                : $this->wrap($key).' = '.$this->parameter($value);
30 8
        })->implode(', ');
31
32 8
        return $sql.$columns;
33
    }
34
35
    /**
36
     * Compile an "insert ignore" statement into SQL.
37
     *
38
     * @param \Illuminate\Database\Query\Builder $query
39
     * @param array $values
40
     * @param array $target
41
     * @return string
42
     */
43 4
    public function compileInsertIgnore(Builder $query, array $values, array $target)
0 ignored issues
show
Unused Code introduced by
The parameter $target is not used and could be removed. ( Ignorable by Annotation )

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

43
    public function compileInsertIgnore(Builder $query, array $values, /** @scrutinizer ignore-unused */ array $target)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44
    {
45 4
        $sql = $this->compileInsert($query, $values);
46
47 4
        return Str::replaceFirst('insert', 'insert ignore', $sql);
48
    }
49
}
50