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
|
|
|
/** |
16
|
|
|
* Class Builder |
17
|
|
|
*/ |
18
|
|
|
class Builder extends \Illuminate\Database\Query\Builder |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* The database connection instance. |
22
|
|
|
* |
23
|
|
|
* @var \Ytake\LaravelCouchbase\Database\CouchbaseConnection |
24
|
|
|
*/ |
25
|
|
|
protected $connection; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* The database query grammar instance. |
29
|
|
|
* |
30
|
|
|
* @var \Ytake\LaravelCouchbase\Query\Grammar |
31
|
|
|
*/ |
32
|
|
|
protected $grammar; |
33
|
|
|
|
34
|
|
|
/** @var string use-keys-clause */ |
35
|
|
|
public $key; |
36
|
|
|
|
37
|
|
|
/** @var string[] returning-clause */ |
38
|
|
|
public $returning = ['*']; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param $key |
42
|
|
|
* |
43
|
|
|
* @return $this |
44
|
|
|
*/ |
45
|
6 |
|
public function key($key) |
46
|
|
|
{ |
47
|
6 |
|
$this->key = $key; |
48
|
|
|
|
49
|
6 |
|
return $this; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param array $column |
54
|
|
|
* |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
3 |
|
public function returning(array $column = ['*']) |
58
|
|
|
{ |
59
|
3 |
|
$this->returning = $column; |
60
|
|
|
|
61
|
3 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Insert a new record into the database. |
66
|
|
|
* |
67
|
|
|
* @param array $values |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
4 |
View Code Duplication |
public function insert(array $values) |
|
|
|
|
72
|
|
|
{ |
73
|
4 |
|
if (empty($values)) { |
74
|
|
|
return true; |
75
|
|
|
} |
76
|
4 |
|
$values = $this->detectValues($values); |
77
|
4 |
|
$bindings = []; |
78
|
4 |
|
foreach ($values as $record) { |
79
|
4 |
|
foreach ($record as $key => $value) { |
80
|
4 |
|
$bindings[$key] = $value; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
4 |
|
$sql = $this->grammar->compileInsert($this, $values); |
85
|
|
|
|
86
|
4 |
|
return $this->connection->insert($sql, $bindings); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* supported N1QL upsert query. |
91
|
|
|
* |
92
|
|
|
* @param array $values |
93
|
|
|
* |
94
|
|
|
* @return bool|mixed |
95
|
|
|
*/ |
96
|
1 |
View Code Duplication |
public function upsert(array $values) |
|
|
|
|
97
|
|
|
{ |
98
|
1 |
|
if (empty($values)) { |
99
|
|
|
return true; |
100
|
|
|
} |
101
|
1 |
|
$values = $this->detectValues($values); |
102
|
1 |
|
$bindings = []; |
103
|
1 |
|
foreach ($values as $record) { |
104
|
1 |
|
foreach ($record as $key => $value) { |
105
|
1 |
|
$bindings[$key] = $value; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
1 |
|
$sql = $this->grammar->compileUpsert($this, $values); |
110
|
|
|
|
111
|
1 |
|
return $this->connection->upsert($sql, $bindings); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param string|int|array $values |
116
|
|
|
* |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
5 |
|
protected function detectValues($values) |
120
|
|
|
{ |
121
|
5 |
|
if (!is_array(reset($values))) { |
122
|
5 |
|
$values = [$values]; |
123
|
|
|
} else { |
124
|
|
|
foreach ($values as $key => $value) { |
125
|
|
|
ksort($value); |
126
|
|
|
$values[$key] = $value; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
130
|
5 |
|
return $values; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
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.