1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LaravelFreelancerNL\Aranguent\Eloquent; |
6
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Builder as IlluminateEloquentBuilder; |
8
|
|
|
use Illuminate\Support\Arr; |
9
|
|
|
use LaravelFreelancerNL\Aranguent\Eloquent\Concerns\QueriesAranguentRelationships; |
10
|
|
|
use LaravelFreelancerNL\Aranguent\Exceptions\UniqueConstraintViolationException; |
11
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Builder as QueryBuilder; |
12
|
|
|
|
13
|
|
|
class Builder extends IlluminateEloquentBuilder |
14
|
|
|
{ |
15
|
|
|
use QueriesAranguentRelationships; |
|
|
|
|
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* The base query builder instance. |
19
|
|
|
* |
20
|
|
|
* @var QueryBuilder |
21
|
|
|
*/ |
22
|
|
|
protected $query; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Attempt to create the record. If a unique constraint violation occurs, attempt to find the matching record. |
26
|
|
|
* |
27
|
|
|
* @param mixed[] $attributes |
28
|
|
|
* @param mixed[] $values |
29
|
|
|
* @return \Illuminate\Database\Eloquent\Model|static |
30
|
|
|
*/ |
31
|
9 |
|
public function createOrFirst(array $attributes = [], array $values = []) |
32
|
|
|
{ |
33
|
|
|
try { |
34
|
9 |
|
return $this->withSavepointIfNeeded(fn() => $this->create(array_merge($attributes, $values))); |
35
|
2 |
|
} catch (UniqueConstraintViolationException $e) { |
36
|
2 |
|
return $this->useWritePdo()->where($attributes)->first() ?? throw $e; |
|
|
|
|
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Insert a record in the database. |
42
|
|
|
* |
43
|
|
|
* |
44
|
|
|
* @param array<mixed> $values |
45
|
|
|
* @return bool |
46
|
|
|
*/ |
47
|
6 |
|
public function insert(array $values) |
48
|
|
|
{ |
49
|
|
|
// Since every insert gets treated like a batch insert, we will make sure the |
50
|
|
|
// bindings are structured in a way that is convenient when building these |
51
|
|
|
// inserts statements by verifying these elements are actually an array. |
52
|
6 |
|
if (empty($values)) { |
53
|
|
|
return true; |
54
|
|
|
} |
55
|
|
|
|
56
|
6 |
|
if (Arr::isAssoc($values)) { |
57
|
6 |
|
$values = [$values]; |
58
|
|
|
} |
59
|
6 |
|
if (!Arr::isAssoc($values)) { |
60
|
|
|
// Here, we will sort the insert keys for every record so that each insert is |
61
|
|
|
// in the same order for the record. We need to make sure this is the case |
62
|
|
|
// so there are not any errors or problems when inserting these records. |
63
|
6 |
|
foreach ($values as $key => $value) { |
64
|
6 |
|
ksort($value); |
65
|
|
|
|
66
|
6 |
|
$values[$key] = $value; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
//Set timestamps |
71
|
6 |
|
foreach ($values as $key => $value) { |
72
|
6 |
|
$values[$key] = $this->updateTimestamps($value); |
73
|
|
|
} |
74
|
|
|
|
75
|
6 |
|
return $this->toBase()->insert($values); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Add the "updated at" column to an array of values. |
80
|
|
|
* |
81
|
|
|
* @param array<string, string> $values |
82
|
|
|
* @return array<string, string> |
83
|
|
|
*/ |
84
|
6 |
|
protected function updateTimestamps(array $values) |
85
|
|
|
{ |
86
|
|
|
if ( |
87
|
6 |
|
!$this->model->usesTimestamps() || |
88
|
2 |
|
is_null($this->model->getUpdatedAtColumn()) || |
89
|
6 |
|
is_null($this->model->getCreatedAtColumn()) |
90
|
|
|
) { |
91
|
4 |
|
return $values; |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
$timestamp = $this->model->freshTimestampString(); |
95
|
2 |
|
$updatedAtColumn = $this->model->getUpdatedAtColumn(); |
96
|
|
|
|
97
|
2 |
|
$timestamps = []; |
98
|
2 |
|
$timestamps[$updatedAtColumn] = $timestamp; |
99
|
|
|
|
100
|
2 |
|
$createdAtColumn = $this->model->getCreatedAtColumn(); |
101
|
2 |
|
if (!isset($values[$createdAtColumn]) && !isset($this->model->$createdAtColumn)) { |
102
|
2 |
|
$timestamps[$createdAtColumn] = $timestamp; |
103
|
|
|
} |
104
|
|
|
|
105
|
2 |
|
$values = array_merge( |
106
|
2 |
|
$timestamps, |
107
|
2 |
|
$values, |
108
|
2 |
|
); |
109
|
|
|
|
110
|
2 |
|
return $values; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Add the "updated at" column to an array of values. |
115
|
|
|
* |
116
|
|
|
* @param array<string> $values |
117
|
|
|
* @return array<string> |
118
|
|
|
*/ |
119
|
17 |
|
protected function addUpdatedAtColumn(array $values): array |
120
|
|
|
{ |
121
|
|
|
if ( |
122
|
17 |
|
!$this->model->usesTimestamps() || |
123
|
17 |
|
is_null($this->model->getUpdatedAtColumn()) |
124
|
|
|
) { |
125
|
|
|
return $values; |
126
|
|
|
} |
127
|
|
|
|
128
|
17 |
|
$column = $this->model->getUpdatedAtColumn(); |
129
|
|
|
|
130
|
17 |
|
$values = array_merge( |
131
|
17 |
|
[$column => $this->model->freshTimestampString()], |
132
|
17 |
|
$values, |
133
|
17 |
|
); |
134
|
|
|
|
135
|
17 |
|
return $values; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Get the underlying query builder instance. |
140
|
|
|
* |
141
|
|
|
* @return QueryBuilder |
142
|
|
|
*/ |
143
|
151 |
|
public function getQuery() |
144
|
|
|
{ |
145
|
151 |
|
return $this->query; |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|