Completed
Pull Request — master (#34)
by yuuki
06:28
created

Grammar::compileDelete()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 9
nc 4
nop 1
dl 0
loc 16
ccs 0
cts 9
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Ytake\LaravelCouchbase\Query\Builder.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

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