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