Completed
Push — master ( 0835cd...0d9cbc )
by yuuki
7s
created

Grammar::compileInsert()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 22
Code Lines 12

Duplication

Lines 22
Ratio 100 %

Code Coverage

Tests 11
CRAP Score 5.0144

Importance

Changes 4
Bugs 0 Features 2
Metric Value
cc 5
eloc 12
c 4
b 0
f 2
nc 16
nop 2
dl 22
loc 22
ccs 11
cts 12
cp 0.9167
crap 5.0144
rs 8.6737
1
<?php
2
3
/**
4
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
5
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
6
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
7
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
8
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
9
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
10
 * THE SOFTWARE.
11
 */
12
13
namespace Ytake\LaravelCouchbase\Query;
14
15
use Illuminate\Database\Query\Builder;
16
use Illuminate\Database\Query\Grammars\Grammar as IlluminateGrammar;
17
use Ytake\LaravelCouchbase\Database\QueryBuilder;
18
19
/**
20
 * Class Grammar.
21
 */
22
class Grammar extends IlluminateGrammar
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27 5
    protected function wrapValue($value)
28
    {
29 5
        if ($value === '*') {
30 3
            return $value;
31
        }
32
33 5
        return $value;
34
    }
35
36
    /**
37
     * @param $value
38
     *
39
     * @return string
40
     */
41 5
    protected function wrapKey($value)
42
    {
43 5
        if (is_null($value)) {
44
            return;
45
        }
46
47 5
        return '"'.str_replace('"', '""', $value).'"';
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     *
53
     * notice: supported set query only
54
     */
55 1
    public function compileUpdate(Builder $query, $values)
56
    {
57
        // keyspace-ref:
58 1
        $table = $this->wrapTable($query->from);
59
        // use-keys-clause:
60 1
        $keyClause = $this->wrapKey($query->key);
0 ignored issues
show
Bug introduced by
The property key does not seem to exist in Illuminate\Database\Query\Builder.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
61
        // returning-clause
62 1
        $returning = implode(', ', $query->returning);
0 ignored issues
show
Bug introduced by
The property returning does not seem to exist in Illuminate\Database\Query\Builder.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
63
64 1
        $columns = [];
65
66 1
        foreach ($values as $key => $value) {
67 1
            $columns[] = $this->wrap($key).' = '.$this->parameter($value);
68
        }
69
70 1
        $columns = implode(', ', $columns);
71
72 1
        $joins = '';
73 1
        if (isset($query->joins)) {
74
            $joins = ' '.$this->compileJoins($query, $query->joins);
75
        }
76 1
        $where = $this->compileWheres($query);
77
78 1
        return trim("update {$table} USE KEYS {$keyClause} {$joins} set $columns $where RETURNING {$returning}");
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 4 View Code Duplication
    public function compileInsert(Builder $query, array $values)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
    {
86
        // keyspace-ref:
87 4
        $table = $this->wrapTable($query->from);
88
        // use-keys-clause:
89 4
        $keyClause = $this->wrapKey($query->key);
0 ignored issues
show
Bug introduced by
The property key does not seem to exist in Illuminate\Database\Query\Builder.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
90
        // returning-clause
91 4
        $returning = implode(', ', $query->returning);
0 ignored issues
show
Bug introduced by
The property returning does not seem to exist in Illuminate\Database\Query\Builder.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
92
93 4
        if (!is_array(reset($values))) {
94
            $values = [$values];
95
        }
96 4
        $parameters = [];
97
98 4
        foreach ($values as $record) {
99 4
            $parameters[] = '('.$this->parameterize($record).')';
100
        }
101 4
        $parameters = (!$keyClause) ? implode(', ', $parameters) : "({$keyClause}, \$parameters)";
102 4
        $keyValue = (!$keyClause) ? null : '(KEY, VALUE)';
103
104 4
        return "insert into {$table} {$keyValue} values $parameters RETURNING {$returning}";
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     *
110
     * @see http://developer.couchbase.com/documentation/server/4.1/n1ql/n1ql-language-reference/delete.html
111
     */
112 5
    public function compileDelete(Builder $query)
113
    {
114
        // keyspace-ref:
115 5
        $table = $this->wrapTable($query->from);
116
        // use-keys-clause:
117 5
        $keyClause = $this->wrapKey($query->key);
0 ignored issues
show
Bug introduced by
The property key does not seem to exist in Illuminate\Database\Query\Builder.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
118
        // returning-clause
119 5
        $returning = implode(', ', $query->returning);
0 ignored issues
show
Bug introduced by
The property returning does not seem to exist in Illuminate\Database\Query\Builder.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
120 5
        $where = is_array($query->wheres) ? $this->compileWheres($query) : '';
121
122 5
        return trim("delete from {$table} USE KEYS {$keyClause} {$where} RETURNING {$returning}");
123
    }
124
125
    /**
126
     * @param QueryBuilder $query
127
     * @param array        $values
128
     *
129
     * @return string
130
     */
131 1 View Code Duplication
    public function compileUpsert(QueryBuilder $query, array $values)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
132
    {
133
        // keyspace-ref:
134 1
        $table = $this->wrapTable($query->from);
135
        // use-keys-clause:
136 1
        $keyClause = $this->wrapKey($query->key);
137
        // returning-clause
138 1
        $returning = implode(', ', $query->returning);
139
140 1
        if (!is_array(reset($values))) {
141
            $values = [$values];
142
        }
143 1
        $parameters = [];
144
145 1
        foreach ($values as $record) {
146 1
            $parameters[] = '('.$this->parameterize($record).')';
147
        }
148 1
        $parameters = (!$keyClause) ? implode(', ', $parameters) : "({$keyClause}, \$parameters)";
149 1
        $keyValue = (!$keyClause) ? null : '(KEY, VALUE)';
150
151 1
        return "UPSERT INTO {$table} {$keyValue} VALUES $parameters RETURNING {$returning}";
152
    }
153
}
154