Completed
Pull Request — master (#551)
by
unknown
29:44 queued 14:46
created

OracleBuilder::updateLob()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 16
ccs 7
cts 7
cp 1
crap 1
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace Yajra\Oci8\Query;
4
5
use Illuminate\Contracts\Support\Arrayable;
6
use Illuminate\Database\Query\Builder;
7
use Illuminate\Database\Query\Expression;
8
9
class OracleBuilder extends Builder
10
{
11
    /**
12
     * Insert a new record and get the value of the primary key.
13
     *
14
     * @param  array $values
15
     * @param  array $binaries
16
     * @param  string $sequence
17
     * @return int
18 6
     */
19
    public function insertLob(array $values, array $binaries, $sequence = 'id')
20
    {
21 6
        /** @var \Yajra\Oci8\Query\Grammars\OracleGrammar $grammar */
22 6
        $grammar = $this->grammar;
23
        $sql     = $grammar->compileInsertLob($this, $values, $binaries, $sequence);
24 6
25 6
        $values   = $this->cleanBindings($values);
26
        $binaries = $this->cleanBindings($binaries);
27
28 6
        /** @var \Yajra\Oci8\Query\Processors\OracleProcessor $processor */
29
        $processor = $this->processor;
30 6
31
        return $processor->saveLob($this, $sql, $values, $binaries);
32
    }
33
34
    /**
35
     * Update a new record with blob field.
36
     *
37
     * @param  array $values
38
     * @param  array $binaries
39
     * @param  string $sequence
40
     * @return bool
41 6
     */
42
    public function updateLob(array $values, array $binaries, $sequence = 'id')
43 6
    {
44
        $bindings = array_values(array_merge($values, $this->getBindings()));
45
46 6
        /** @var \Yajra\Oci8\Query\Grammars\OracleGrammar $grammar */
47 6
        $grammar = $this->grammar;
48
        $sql     = $grammar->compileUpdateLob($this, $values, $binaries, $sequence);
49 6
50 6
        $values   = $this->cleanBindings($bindings);
51
        $binaries = $this->cleanBindings($binaries);
52
53 6
        /** @var \Yajra\Oci8\Query\Processors\OracleProcessor $processor */
54
        $processor = $this->processor;
55 6
56
        return $processor->saveLob($this, $sql, $values, $binaries);
57
    }
58
59
    /**
60
     * Add a "where in" clause to the query.
61
     * Split one WHERE IN clause into multiple clauses each
62
     * with up to 1000 expressions to avoid ORA-01795.
63
     *
64
     * @param  string $column
65
     * @param  mixed $values
66
     * @param  string $boolean
67
     * @param  bool $not
68
     * @return \Illuminate\Database\Query\Builder|\Yajra\Oci8\Query\OracleBuilder
69 9
     */
70
    public function whereIn($column, $values, $boolean = 'and', $not = false)
71 9
    {
72
        $type = $not ? 'NotIn' : 'In';
73 9
74
        if ($values instanceof Arrayable) {
75
            $values = $values->toArray();
76
        }
77 9
78
        if (is_array($values) && count($values) > 1000) {
79
            $chunks = array_chunk($values, 1000);
80
81
            return $this->where(function ($query) use ($column, $chunks, $type, $not) {
82
                foreach ($chunks as $ch) {
83
                    $sqlClause = $not ? 'where' . $type : 'orWhere' . $type;
84
                    $query->{$sqlClause}($column, $ch);
85
                }
86
            }, null, null, $boolean);
87
        }
88 9
89
        return parent::whereIn($column, $values, $boolean, $not);
90
    }
91
92
    /**
93
     * Run the query as a "select" statement against the connection.
94
     *
95
     * @return array
96 27
     */
97
    protected function runSelect()
98 27
    {
99
        if ($this->lock) {
100
            $this->connection->beginTransaction();
101
            $result = $this->connection->select($this->toSql(), $this->getBindings(), ! $this->useWritePdo);
102
            $this->connection->commit();
103
104
            return $result;
105
        }
106 27
107
        return $this->connection->select($this->toSql(), $this->getBindings(), ! $this->useWritePdo);
108
    }
109
110
    /**
111
     * Makes "from" fetch from a subquery.
112
     *
113
     * @param  \Closure|\Illuminate\Database\Query\Builder|string $query
114
     * @param  string  $as
115
     * @return \Illuminate\Database\Query\Builder|static
116
     *
117
     * @throws \InvalidArgumentException
118
     */
119
    public function fromSub($query, $as)
120
    {
121
        [$query, $bindings] = $this->createSub($query);
0 ignored issues
show
Bug introduced by
The variable $bindings does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
122
123
        return $this->fromRaw('('.$query.') '.$this->grammar->wrap($as), $bindings);
124
    }
125
126
    /**
127
     * Add a subquery join clause to the query.
128
     *
129
     * @param  \Closure|\Illuminate\Database\Query\Builder|string $query
130
     * @param  string  $as
131
     * @param  \Closure|string  $first
132
     * @param  string|null  $operator
133
     * @param  string|null  $second
134
     * @param  string  $type
135
     * @param  bool    $where
136
     * @return \Illuminate\Database\Query\Builder|static
137
     *
138
     * @throws \InvalidArgumentException
139
     */
140
    public function joinSub($query, $as, $first, $operator = null, $second = null, $type = 'inner', $where = false)
141
    {
142
        [$query, $bindings] = $this->createSub($query);
0 ignored issues
show
Bug introduced by
The variable $bindings does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
143
144
        $expression = '('.$query.') '.$this->grammar->wrapTable($as);
145
146
        $this->addBinding($bindings, 'join');
147
148
        return $this->join(new Expression($expression), $first, $operator, $second, $type, $where);
149
    }
150
}
151