Completed
Push — develop ( 0e6100...5d337c )
by yuuki
02:33 queued 31s
created

Grammar::wrapValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4286
cc 2
eloc 4
nc 2
nop 1
crap 2
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
namespace Ytake\LaravelCouchbase\Query;
13
14
use Illuminate\Database\Query\Builder;
15
use Illuminate\Database\Query\Grammars\Grammar as IlluminateGrammar;
16
use Ytake\LaravelCouchbase\Database\QueryBuilder;
17
18
/**
19
 * Class Grammar.
20
 */
21
class Grammar extends IlluminateGrammar
22
{
23
    /**
24
     * {@inheritdoc}
25
     */
26 5
    protected function wrapValue($value)
27
    {
28 5
        if ($value === '*') {
29 3
            return $value;
30
        }
31
32 5
        return $value;
33
    }
34
35
    /**
36
     * @param $value
37
     *
38
     * @return string
39
     */
40 5
    protected function wrapKey($value)
41
    {
42 5
        if (is_null($value)) {
43
            return;
44
        }
45
46 5
        return '"'.str_replace('"', '""', $value).'"';
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     *
52
     * notice: supported set query only
53
     */
54 1
    public function compileUpdate(Builder $query, $values)
55
    {
56
        // keyspace-ref:
57 1
        $table = $this->wrapTable($query->from);
58
        // use-keys-clause:
59 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...
60
        // returning-clause
61 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...
62
63 1
        $columns = [];
64
65 1
        foreach ($values as $key => $value) {
66 1
            $columns[] = $this->wrap($key).' = '.$this->parameter($value);
67 1
        }
68
69 1
        $columns = implode(', ', $columns);
70
71 1
        $joins = '';
72 1
        if (isset($query->joins)) {
73
            $joins = ' '.$this->compileJoins($query, $query->joins);
74
        }
75 1
        $where = $this->compileWheres($query);
76
77 1
        return trim("update {$table} USE KEYS {$keyClause} {$joins} set $columns $where RETURNING {$returning}");
78
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 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...
84
    {
85
        // keyspace-ref:
86 4
        $table = $this->wrapTable($query->from);
87
        // use-keys-clause:
88 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...
89
        // returning-clause
90 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...
91
92 4
        if (!is_array(reset($values))) {
93
            $values = [$values];
94
        }
95 4
        $parameters = [];
96
97 4
        foreach ($values as $record) {
98 4
            $parameters[] = '('.$this->parameterize($record).')';
99 4
        }
100 4
        $parameters = (!$keyClause) ? implode(', ', $parameters) : "({$keyClause}, \$parameters)";
101 4
        $keyValue = (!$keyClause) ? null : '(KEY, VALUE)';
102
103 4
        return "insert into {$table} {$keyValue} values $parameters RETURNING {$returning}";
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     *
109
     * @see http://developer.couchbase.com/documentation/server/4.1/n1ql/n1ql-language-reference/delete.html
110
     */
111 5
    public function compileDelete(Builder $query)
112
    {
113
        // keyspace-ref:
114 5
        $table = $this->wrapTable($query->from);
115
        // use-keys-clause:
116 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...
117
        // returning-clause
118 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...
119 5
        $where = is_array($query->wheres) ? $this->compileWheres($query) : '';
120
121 5
        return trim("delete from {$table} USE KEYS {$keyClause} {$where} RETURNING {$returning}");
122
    }
123
124
    /**
125
     * @param QueryBuilder $query
126
     * @param array        $values
127
     *
128
     * @return string
129
     */
130 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...
131
    {
132
        // keyspace-ref:
133 1
        $table = $this->wrapTable($query->from);
134
        // use-keys-clause:
135 1
        $keyClause = $this->wrapKey($query->key);
136
        // returning-clause
137 1
        $returning = implode(', ', $query->returning);
138
139 1
        if (!is_array(reset($values))) {
140
            $values = [$values];
141
        }
142 1
        $parameters = [];
143
144 1
        foreach ($values as $record) {
145 1
            $parameters[] = '('.$this->parameterize($record).')';
146 1
        }
147 1
        $parameters = (!$keyClause) ? implode(', ', $parameters) : "({$keyClause}, \$parameters)";
148 1
        $keyValue = (!$keyClause) ? null : '(KEY, VALUE)';
149
150 1
        return "UPSERT INTO {$table} {$keyValue} VALUES $parameters RETURNING {$returning}";
151
    }
152
}
153