1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
6
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
7
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
8
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
9
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
10
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
11
|
|
|
* THE SOFTWARE. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Ytake\LaravelCouchbase\Query; |
15
|
|
|
|
16
|
|
|
use Illuminate\Database\Query\Builder; |
|
|
|
|
17
|
|
|
use Ytake\LaravelCouchbase\Query\Builder as QueryBuilder; |
18
|
|
|
use Illuminate\Database\Query\Grammars\Grammar as IlluminateGrammar; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class Grammar. |
22
|
|
|
* |
23
|
|
|
* @author Yuuki Takezawa<[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class Grammar extends IlluminateGrammar |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* {@inheritdoc} |
29
|
|
|
*/ |
30
|
20 |
|
protected function wrapValue($value) |
31
|
|
|
{ |
32
|
20 |
|
if ($value === '*') { |
33
|
10 |
|
return $value; |
34
|
|
|
} |
35
|
|
|
|
36
|
20 |
|
return $value; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param mixed $value |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
14 |
|
protected function wrapKey($value) |
45
|
|
|
{ |
46
|
14 |
|
if (is_null($value)) { |
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
|
50
|
14 |
|
return '"' . str_replace('"', '""', $value) . '"'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
* |
56
|
|
|
* notice: supported set query only |
57
|
|
|
*/ |
58
|
2 |
|
public function compileUpdate(Builder $query, $values) |
59
|
|
|
{ |
60
|
|
|
// keyspace-ref: |
61
|
2 |
|
$table = $this->wrapTable($query->from); |
62
|
|
|
// use-keys-clause: |
63
|
2 |
|
$keyClause = $this->wrapKey($query->key); |
|
|
|
|
64
|
|
|
// returning-clause |
65
|
2 |
|
$returning = implode(', ', $query->returning); |
|
|
|
|
66
|
|
|
|
67
|
2 |
|
$columns = []; |
68
|
|
|
|
69
|
2 |
|
foreach ($values as $key => $value) { |
70
|
2 |
|
$columns[] = $this->wrap($key) . ' = ' . $this->parameter($value); |
71
|
|
|
} |
72
|
|
|
|
73
|
2 |
|
$columns = implode(', ', $columns); |
74
|
|
|
|
75
|
2 |
|
$joins = ''; |
76
|
2 |
|
if (isset($query->joins)) { |
77
|
|
|
$joins = ' ' . $this->compileJoins($query, $query->joins); |
78
|
|
|
} |
79
|
2 |
|
$where = $this->compileWheres($query); |
80
|
|
|
|
81
|
2 |
|
return trim("update {$table} USE KEYS {$keyClause} {$joins} set $columns $where RETURNING {$returning}"); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
10 |
|
public function compileInsert(Builder $query, array $values) |
88
|
|
|
{ |
89
|
|
|
// keyspace-ref: |
90
|
10 |
|
$table = $this->wrapTable($query->from); |
91
|
|
|
// use-keys-clause: |
92
|
10 |
|
$keyClause = $this->wrapKey($query->key); |
|
|
|
|
93
|
|
|
// returning-clause |
94
|
10 |
|
$returning = implode(', ', $query->returning); |
|
|
|
|
95
|
|
|
|
96
|
10 |
|
if (!is_array(reset($values))) { |
97
|
|
|
$values = [$values]; |
98
|
|
|
} |
99
|
10 |
|
$parameters = []; |
100
|
|
|
|
101
|
10 |
|
foreach ($values as $record) { |
102
|
10 |
|
$parameters[] = '(' . $this->parameterize($record) . ')'; |
103
|
|
|
} |
104
|
10 |
|
$parameters = (!$keyClause) ? implode(', ', $parameters) : "({$keyClause}, \$parameters)"; |
105
|
10 |
|
$keyValue = (!$keyClause) ? null : '(KEY, VALUE)'; |
106
|
|
|
|
107
|
10 |
|
return "insert into {$table} {$keyValue} values $parameters RETURNING {$returning}"; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
* |
113
|
|
|
* @see http://developer.couchbase.com/documentation/server/4.1/n1ql/n1ql-language-reference/delete.html |
114
|
|
|
*/ |
115
|
8 |
|
public function compileDelete(Builder $query) |
116
|
|
|
{ |
117
|
|
|
// keyspace-ref: |
118
|
8 |
|
$table = $this->wrapTable($query->from); |
119
|
|
|
// use-keys-clause: |
120
|
8 |
|
$keyClause = null; |
121
|
8 |
|
if ($query->key) { |
122
|
4 |
|
$key = $this->wrapKey($query->key); |
|
|
|
|
123
|
4 |
|
$keyClause = "USE KEYS {$key}"; |
124
|
|
|
} |
125
|
|
|
// returning-clause |
126
|
8 |
|
$returning = implode(', ', $query->returning); |
|
|
|
|
127
|
8 |
|
$where = is_array($query->wheres) ? $this->compileWheres($query) : ''; |
128
|
|
|
|
129
|
8 |
|
return trim("delete from {$table} {$keyClause} {$where} RETURNING {$returning}"); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @param QueryBuilder $query |
134
|
|
|
* @param array $values |
135
|
|
|
* |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
2 |
|
public function compileUpsert(QueryBuilder $query, array $values): string |
139
|
|
|
{ |
140
|
|
|
// keyspace-ref: |
141
|
2 |
|
$table = $this->wrapTable($query->from); |
142
|
|
|
// use-keys-clause: |
143
|
2 |
|
$keyClause = $this->wrapKey($query->key); |
144
|
|
|
// returning-clause |
145
|
2 |
|
$returning = implode(', ', $query->returning); |
146
|
|
|
|
147
|
2 |
|
if (!is_array(reset($values))) { |
148
|
|
|
$values = [$values]; |
149
|
|
|
} |
150
|
2 |
|
$parameters = []; |
151
|
|
|
|
152
|
2 |
|
foreach ($values as $record) { |
153
|
2 |
|
$parameters[] = '(' . $this->parameterize($record) . ')'; |
154
|
|
|
} |
155
|
2 |
|
$parameters = (!$keyClause) ? implode(', ', $parameters) : "({$keyClause}, \$parameters)"; |
156
|
2 |
|
$keyValue = (!$keyClause) ? null : '(KEY, VALUE)'; |
157
|
|
|
|
158
|
2 |
|
return "UPSERT INTO {$table} {$keyValue} VALUES $parameters RETURNING {$returning}"; |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/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 beforeOtherDir/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: