1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LaravelFreelancerNL\Aranguent\Query\Concerns; |
6
|
|
|
|
7
|
|
|
use Illuminate\Database\Query\Expression; |
8
|
|
|
use Illuminate\Support\Arr; |
9
|
|
|
use InvalidArgumentException; |
10
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Grammar; |
11
|
|
|
use LaravelFreelancerNL\FluentAQL\Exceptions\BindException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @method applyBeforeQueryCallbacks() |
15
|
|
|
*/ |
16
|
|
|
trait BuildsUpdates |
17
|
|
|
{ |
18
|
|
|
protected function prepareValuesForUpdate(array $values) |
19
|
|
|
{ |
20
|
|
|
foreach($values as $key => $value) { |
21
|
|
|
if ($value instanceof Expression) { |
22
|
|
|
$values[$key] = $value->getValue($this->grammar); |
23
|
|
|
|
24
|
|
|
continue; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
if (is_array($value)) { |
28
|
|
|
$values[$key] = $this->prepareValuesForUpdate($value); |
29
|
|
|
continue; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$values[$key] = $this->bindValue($value, 'update'); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
return $values; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Update records in the database. |
40
|
|
|
* |
41
|
|
|
* @param array $values |
42
|
|
|
* @return int |
43
|
|
|
*/ |
44
|
|
|
public function update(array $values) |
45
|
|
|
{ |
46
|
|
|
assert($this->grammar instanceof Grammar); |
47
|
|
|
|
48
|
|
|
$this->applyBeforeQueryCallbacks(); |
49
|
|
|
|
50
|
|
|
$values = Arr::undot($this->grammar->convertJsonFields($values)); |
51
|
|
|
|
52
|
|
|
$values = $this->prepareValuesForUpdate($values); |
53
|
|
|
|
54
|
|
|
$aql = $this->grammar->compileUpdate($this, $values); |
|
|
|
|
55
|
|
|
|
56
|
|
|
return $this->connection->update($aql, $this->getBindings()); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Insert or update a record matching the attributes, and fill it with values. |
61
|
|
|
* |
62
|
|
|
* @param array $attributes |
63
|
|
|
* @param array $values |
64
|
|
|
* @return bool |
65
|
|
|
* @throws BindException |
66
|
|
|
*/ |
67
|
|
|
public function updateOrInsert(array $attributes, array $values = []) |
68
|
|
|
{ |
69
|
|
|
if (!$this->where($attributes)->exists()) { |
|
|
|
|
70
|
|
|
$this->bindings['where'] = []; |
|
|
|
|
71
|
|
|
return $this->insert(array_merge($attributes, $values)); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (empty($values)) { |
75
|
|
|
return true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return (bool) $this->limit(1)->update($values); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Increment the given column's values by the given amounts. |
83
|
|
|
* |
84
|
|
|
* @param array<string, float|int|numeric-string> $columns |
|
|
|
|
85
|
|
|
* @param array<string, mixed> $extra |
86
|
|
|
* @return int |
87
|
|
|
* |
88
|
|
|
* @throws \InvalidArgumentException |
89
|
|
|
*/ |
90
|
|
|
public function incrementEach(array $columns, array $extra = []) |
91
|
|
|
{ |
92
|
|
|
foreach ($columns as $column => $amount) { |
93
|
|
|
if (!is_numeric($amount)) { |
94
|
|
|
throw new InvalidArgumentException("Non-numeric value passed as increment amount for column: '$column'."); |
95
|
|
|
} elseif (!is_string($column)) { |
96
|
|
|
throw new InvalidArgumentException('Non-associative array passed to incrementEach method.'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$columns[$column] = new Expression($this->getTableAlias($this->from) . '.' . $column . ' + ' . $amount); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this->update(array_merge($columns, $extra)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Decrement the given column's values by the given amounts. |
107
|
|
|
* |
108
|
|
|
* @param array<string, float|int|numeric-string> $columns |
|
|
|
|
109
|
|
|
* @param array<string, mixed> $extra |
110
|
|
|
* @return int |
111
|
|
|
* |
112
|
|
|
* @throws \InvalidArgumentException |
113
|
|
|
*/ |
114
|
|
|
public function decrementEach(array $columns, array $extra = []) |
115
|
|
|
{ |
116
|
|
|
foreach ($columns as $column => $amount) { |
117
|
|
|
if (!is_numeric($amount)) { |
118
|
|
|
throw new InvalidArgumentException("Non-numeric value passed as decrement amount for column: '$column'."); |
119
|
|
|
} elseif (!is_string($column)) { |
120
|
|
|
throw new InvalidArgumentException('Non-associative array passed to decrementEach method.'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$columns[$column] = new Expression($this->getTableAlias($this->from) . '.' . $column . ' - ' . $amount); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $this->update(array_merge($columns, $extra)); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Insert new records or update the existing ones. |
131
|
|
|
* |
132
|
|
|
* @param array $values |
133
|
|
|
* @param array|string $uniqueBy |
134
|
|
|
* @param array|null $update |
135
|
|
|
* @return int |
136
|
|
|
* @throws BindException |
137
|
|
|
*/ |
138
|
|
|
public function upsert(array $values, $uniqueBy, $update = null) |
139
|
|
|
{ |
140
|
|
|
assert($this->grammar instanceof Grammar); |
141
|
|
|
|
142
|
|
|
if (empty($values)) { |
143
|
|
|
return 0; |
144
|
|
|
} elseif ($update === []) { |
145
|
|
|
return (int) $this->insert($values); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if (!is_array(reset($values))) { |
149
|
|
|
$values = [$values]; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
foreach($values as $key => $value) { |
153
|
|
|
$values[$key] = $this->grammar->convertJsonFields($value); |
154
|
|
|
$values[$key] = $this->convertIdToKey($values[$key]); |
|
|
|
|
155
|
|
|
$values[$key] = Arr::undot($values[$key]); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
foreach($values as $key => $value) { |
159
|
|
|
foreach ($value as $dataKey => $data) { |
160
|
|
|
$values[$key][$dataKey] = $this->bindValue($data, 'upsert'); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
$uniqueBy = $this->grammar->convertJsonFields($uniqueBy); |
165
|
|
|
|
166
|
|
|
if (is_null($update)) { |
167
|
|
|
$update = array_keys(reset($values)); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
foreach ($update as $key => $value) { |
171
|
|
|
$update[$key] = $this->convertIdToKey($value); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$update = $this->grammar->convertJsonFields($update); |
175
|
|
|
|
176
|
|
|
$this->applyBeforeQueryCallbacks(); |
177
|
|
|
|
178
|
|
|
$bindings = $this->bindings['upsert']; |
179
|
|
|
|
180
|
|
|
$aql = $this->grammar->compileUpsert($this, $values, (array) $uniqueBy, $update); |
|
|
|
|
181
|
|
|
|
182
|
|
|
return $this->connection->affectingStatement( |
183
|
|
|
$aql, |
184
|
|
|
$bindings |
185
|
|
|
); |
186
|
|
|
} |
187
|
|
|
} |
188
|
|
|
|