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