Completed
Push — master ( 608e83...f3805b )
by Arjay
01:39
created

OracleEloquent   A

Complexity

Total Complexity 32

Size/Duplication

Total Lines 279
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 6.74%

Importance

Changes 0
Metric Value
dl 0
loc 279
ccs 6
cts 89
cp 0.0674
rs 9.6
c 0
b 0
f 0
wmc 32
lcom 1
cbo 5

11 Methods

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