1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Yajra\Oci8\Eloquent; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Yajra\Oci8\Oci8Connection; |
8
|
|
|
use Yajra\Oci8\Query\OracleBuilder as QueryBuilder; |
9
|
|
|
|
10
|
|
|
class OracleEloquent extends Model |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* List of binary (blob) columns |
14
|
|
|
* |
15
|
|
|
* @var array |
16
|
|
|
*/ |
17
|
|
|
protected $binaries = []; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
protected $wrapBinaries = []; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Sequence name variable |
26
|
|
|
* |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
protected $sequence = null; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Get model's sequence name |
33
|
|
|
* |
34
|
|
|
* @return string |
35
|
|
|
*/ |
36
|
|
|
public function getSequenceName() |
37
|
|
|
{ |
38
|
|
|
if ($this->sequence) { |
39
|
|
|
return $this->sequence; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
return $this->getTable() . '_' . $this->getKeyName() . '_seq'; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Set sequence name |
47
|
|
|
* |
48
|
|
|
* @param string $name |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function setSequenceName($name) |
52
|
|
|
{ |
53
|
|
|
return $this->sequence = $name; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Update the model in the database. |
58
|
|
|
* |
59
|
|
|
* @param array $attributes |
60
|
|
|
* @return bool|int |
61
|
|
|
*/ |
62
|
|
|
public function update(array $attributes = []) |
63
|
|
|
{ |
64
|
|
|
if (! $this->exists) { |
65
|
|
|
// If dirty attributes contains binary field |
66
|
|
|
// extract binary fields to new array |
67
|
|
|
if ($this->wrapBinary($dirty)) { |
68
|
|
|
return $this->newQuery()->updateLob($attributes, $this->wrapBinaries, $this->getKeyName()); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->newQuery()->update($attributes); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $this->fill($attributes)->save(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* wrap binaries to each attributes |
79
|
|
|
* |
80
|
|
|
* @param array $attributes |
81
|
|
|
* @return array |
82
|
|
|
*/ |
83
|
|
|
public function wrapBinary(&$attributes) |
84
|
|
|
{ |
85
|
|
|
// If attributes contains binary field |
86
|
|
|
// extract binary fields to new array |
87
|
|
|
$binaries = []; |
88
|
|
|
if ($this->checkBinary($attributes) and $this->getConnection() instanceof Oci8Connection) { |
89
|
|
|
foreach ($attributes as $key => $value) { |
90
|
|
|
if (in_array($key, $this->binaries)) { |
91
|
|
|
$binaries[$key] = $value; |
92
|
|
|
unset($attributes[$key]); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $this->wrapBinaries = $binaries; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Check if attributes contains binary field |
102
|
|
|
* |
103
|
|
|
* @param array $attributes |
104
|
|
|
* @return boolean |
105
|
|
|
*/ |
106
|
|
|
public function checkBinary(array $attributes) |
107
|
|
|
{ |
108
|
|
|
foreach ($attributes as $key => $value) { |
109
|
|
|
// if attribute is in binary field list |
110
|
|
|
if (in_array($key, $this->binaries)) { |
111
|
|
|
return true; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return false; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get the table qualified key name. |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function getQualifiedKeyName() |
124
|
|
|
{ |
125
|
|
|
$pos = strpos($this->getTable(), '@'); |
126
|
|
|
|
127
|
|
|
if ($pos === false) { |
128
|
|
|
return $this->getTable() . '.' . $this->getKeyName(); |
129
|
|
|
} else { |
130
|
|
|
$table = substr($this->getTable(), 0, $pos); |
131
|
|
|
$dblink = substr($this->getTable(), $pos); |
132
|
|
|
|
133
|
|
|
return $table . '.' . $this->getKeyName() . $dblink; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get a new query builder instance for the connection. |
139
|
|
|
* |
140
|
|
|
* @return \Yajra\Oci8\Query\OracleBuilder |
141
|
|
|
*/ |
142
|
|
|
protected function newBaseQueryBuilder() |
143
|
|
|
{ |
144
|
|
|
$conn = $this->getConnection(); |
145
|
|
|
|
146
|
|
|
$grammar = $conn->getQueryGrammar(); |
147
|
|
|
|
148
|
|
|
return new QueryBuilder($conn, $grammar, $conn->getPostProcessor()); |
|
|
|
|
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Perform a model update operation. |
153
|
|
|
* |
154
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
155
|
|
|
* @param array $options |
156
|
|
|
* @return boolean |
157
|
|
|
*/ |
158
|
|
|
protected function performUpdate(Builder $query, array $options = []) |
159
|
|
|
{ |
160
|
|
|
$dirty = $this->getDirty(); |
161
|
|
|
|
162
|
|
|
if (count($dirty) > 0) { |
163
|
|
|
// If the updating event returns false, we will cancel the update operation so |
164
|
|
|
// developers can hook Validation systems into their models and cancel this |
165
|
|
|
// operation if the model does not pass validation. Otherwise, we update. |
166
|
|
|
if ($this->fireModelEvent('updating') === false) { |
167
|
|
|
return false; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
// First we need to create a fresh query instance and touch the creation and |
171
|
|
|
// update timestamp on the model which are maintained by us for developer |
172
|
|
|
// convenience. Then we will just continue saving the model instances. |
173
|
|
|
if ($this->timestamps && array_get($options, 'timestamps', true)) { |
174
|
|
|
$this->updateTimestamps(); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
// Once we have run the update operation, we will fire the "updated" event for |
178
|
|
|
// this model instance. This will allow developers to hook into these after |
179
|
|
|
// models are updated, giving them a chance to do any special processing. |
180
|
|
|
$dirty = $this->getDirty(); |
181
|
|
|
|
182
|
|
|
if (count($dirty) > 0) { |
183
|
|
|
// If dirty attributes contains binary field |
184
|
|
|
// extract binary fields to new array |
185
|
|
|
$this->updateBinary($query, $dirty); |
186
|
|
|
|
187
|
|
|
$this->fireModelEvent('updated', false); |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return true; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @param Builder $query |
196
|
|
|
* @param $dirty |
197
|
|
|
*/ |
198
|
|
|
protected function updateBinary(Builder $query, $dirty) |
199
|
|
|
{ |
200
|
|
|
if ($this->wrapBinary($dirty)) { |
201
|
|
|
$this->setKeysForSaveQuery($query)->updateLob($dirty, $this->wrapBinaries, $this->getKeyName()); |
202
|
|
|
} else { |
203
|
|
|
$this->setKeysForSaveQuery($query)->update($dirty); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Perform a model insert operation. |
209
|
|
|
* |
210
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
211
|
|
|
* @param array $options |
212
|
|
|
* @return bool |
213
|
|
|
*/ |
214
|
|
|
protected function performInsert(Builder $query, array $options = []) |
215
|
|
|
{ |
216
|
|
|
if ($this->fireModelEvent('creating') === false) { |
217
|
|
|
return false; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
// First we'll need to create a fresh query instance and touch the creation and |
221
|
|
|
// update timestamps on this model, which are maintained by us for developer |
222
|
|
|
// convenience. After, we will just continue saving these model instances. |
223
|
|
|
if ($this->timestamps && array_get($options, 'timestamps', true)) { |
224
|
|
|
$this->updateTimestamps(); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
// If the model has an incrementing key, we can use the "insertGetId" method on |
228
|
|
|
// the query builder, which will give us back the final inserted ID for this |
229
|
|
|
// table from the database. Not all tables have to be incrementing though. |
230
|
|
|
$attributes = $this->attributes; |
231
|
|
|
|
232
|
|
|
if ($this->incrementing) { |
233
|
|
|
$this->insertAndSetId($query, $attributes); |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
// If the table is not incrementing we'll simply insert this attributes as they |
237
|
|
|
// are, as this attributes arrays must contain an "id" column already placed |
238
|
|
|
// there by the developer as the manually determined key for these models. |
239
|
|
|
else { |
240
|
|
|
// If attributes contains binary field |
241
|
|
|
// extract binary fields to new array |
242
|
|
|
if ($this->wrapBinary($attributes)) { |
243
|
|
|
$query->getQuery()->insertLob($attributes, $this->wrapBinaries, $this->getKeyName()); |
244
|
|
|
} else { |
245
|
|
|
$query->insert($attributes); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
// We will go ahead and set the exists property to true, so that it is set when |
250
|
|
|
// the created event is fired, just in case the developer tries to update it |
251
|
|
|
// during the event. This will allow them to do so and run an update here. |
252
|
|
|
$this->exists = true; |
253
|
|
|
|
254
|
|
|
$this->wasRecentlyCreated = true; |
255
|
|
|
|
256
|
|
|
$this->fireModelEvent('created', false); |
257
|
|
|
|
258
|
|
|
return true; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
/** |
262
|
|
|
* Insert the given attributes and set the ID on the model. |
263
|
|
|
* |
264
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
265
|
|
|
* @param array $attributes |
266
|
|
|
* @return int|void |
267
|
|
|
*/ |
268
|
|
|
protected function insertAndSetId(Builder $query, $attributes) |
269
|
|
|
{ |
270
|
|
|
if ($binaries = $this->wrapBinary($attributes)) { |
271
|
|
|
$id = $query->getQuery()->insertLob($attributes, $binaries, $keyName = $this->getKeyName()); |
272
|
|
|
} else { |
273
|
|
|
$id = $query->insertGetId($attributes, $keyName = $this->getKeyName()); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
$this->setAttribute($keyName, $id); |
277
|
|
|
} |
278
|
|
|
} |
279
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.