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 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); |
|
|
|
|
63
|
|
|
// returning-clause |
64
|
|
|
$returning = implode(', ', $query->returning); |
|
|
|
|
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); |
|
|
|
|
92
|
|
|
// returning-clause |
93
|
|
|
$returning = implode(', ', $query->returning); |
|
|
|
|
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); |
|
|
|
|
122
|
|
|
$keyClause = "USE KEYS {$key}"; |
123
|
|
|
} |
124
|
|
|
// returning-clause |
125
|
|
|
$returning = implode(', ', $query->returning); |
|
|
|
|
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
|
|
|
|
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: