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