Completed
Pull Request — master (#550)
by
unknown
13:41
created

OracleBuilder   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 142
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 63.89%

Importance

Changes 0
Metric Value
dl 0
loc 142
ccs 23
cts 36
cp 0.6389
rs 10
c 0
b 0
f 0
wmc 13
lcom 1
cbo 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A insertLob() 0 14 1
A updateLob() 0 16 1
B whereIn() 0 21 7
A runSelect() 0 12 2
A fromSub() 0 6 1
A joinSub() 0 10 1
1
<?php
2
3
namespace Yajra\Oci8\Query;
4
5
use Illuminate\Database\Query\Builder;
6
use Illuminate\Contracts\Support\Arrayable;
7
8
class OracleBuilder extends Builder
9
{
10
    /**
11
     * Insert a new record and get the value of the primary key.
12
     *
13
     * @param  array $values
14
     * @param  array $binaries
15
     * @param  string $sequence
16
     * @return int
17
     */
18 6
    public function insertLob(array $values, array $binaries, $sequence = 'id')
19
    {
20
        /** @var \Yajra\Oci8\Query\Grammars\OracleGrammar $grammar */
21 6
        $grammar = $this->grammar;
22 6
        $sql     = $grammar->compileInsertLob($this, $values, $binaries, $sequence);
23
24 6
        $values   = $this->cleanBindings($values);
25 6
        $binaries = $this->cleanBindings($binaries);
26
27
        /** @var \Yajra\Oci8\Query\Processors\OracleProcessor $processor */
28 6
        $processor = $this->processor;
29
30 6
        return $processor->saveLob($this, $sql, $values, $binaries);
31
    }
32
33
    /**
34
     * Update a new record with blob field.
35
     *
36
     * @param  array $values
37
     * @param  array $binaries
38
     * @param  string $sequence
39
     * @return bool
40
     */
41 6
    public function updateLob(array $values, array $binaries, $sequence = 'id')
42
    {
43 6
        $bindings = array_values(array_merge($values, $this->getBindings()));
44
45
        /** @var \Yajra\Oci8\Query\Grammars\OracleGrammar $grammar */
46 6
        $grammar = $this->grammar;
47 6
        $sql     = $grammar->compileUpdateLob($this, $values, $binaries, $sequence);
48
49 6
        $values   = $this->cleanBindings($bindings);
50 6
        $binaries = $this->cleanBindings($binaries);
51
52
        /** @var \Yajra\Oci8\Query\Processors\OracleProcessor $processor */
53 6
        $processor = $this->processor;
54
55 6
        return $processor->saveLob($this, $sql, $values, $binaries);
56
    }
57
58
    /**
59
     * Add a "where in" clause to the query.
60
     * Split one WHERE IN clause into multiple clauses each
61
     * with up to 1000 expressions to avoid ORA-01795.
62
     *
63
     * @param  string $column
64
     * @param  mixed $values
65
     * @param  string $boolean
66
     * @param  bool $not
67
     * @return \Illuminate\Database\Query\Builder|\Yajra\Oci8\Query\OracleBuilder
68
     */
69 9
    public function whereIn($column, $values, $boolean = 'and', $not = false)
70
    {
71 9
        $type = $not ? 'NotIn' : 'In';
72
73 9
        if ($values instanceof Arrayable) {
74
            $values = $values->toArray();
75
        }
76
77 9
        if (is_array($values) && count($values) > 1000) {
78
            $chunks = array_chunk($values, 1000);
79
80
            return $this->where(function ($query) use ($column, $chunks, $type, $not) {
81
                foreach ($chunks as $ch) {
82
                    $sqlClause = $not ? 'where' . $type : 'orWhere' . $type;
83
                    $query->{$sqlClause}($column, $ch);
84
                }
85
            }, null, null, $boolean);
86
        }
87
88 9
        return parent::whereIn($column, $values, $boolean, $not);
89
    }
90
91
    /**
92
     * Run the query as a "select" statement against the connection.
93
     *
94
     * @return array
95
     */
96 27
    protected function runSelect()
97
    {
98 27
        if ($this->lock) {
99
            $this->connection->beginTransaction();
100
            $result = $this->connection->select($this->toSql(), $this->getBindings(), ! $this->useWritePdo);
101
            $this->connection->commit();
102
103
            return $result;
104
        }
105
106 27
        return $this->connection->select($this->toSql(), $this->getBindings(), ! $this->useWritePdo);
107
    }
108
109
    /**
110
     * Makes "from" fetch from a subquery.
111
     *
112
     * @param  \Closure|\Illuminate\Database\Query\Builder|string $query
113
     * @param  string  $as
114
     * @return \Illuminate\Database\Query\Builder|static
115
     *
116
     * @throws \InvalidArgumentException
117
     */
118
    public function fromSub($query, $as)
119
    {
120
        [$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...
121
122
        return $this->fromRaw('('.$query.') '.$this->grammar->wrap($as), $bindings);
123
    }
124
125
    /**
126
     * Add a subquery join clause to the query.
127
     *
128
     * @param  \Closure|\Illuminate\Database\Query\Builder|string $query
129
     * @param  string  $as
130
     * @param  \Closure|string  $first
131
     * @param  string|null  $operator
132
     * @param  string|null  $second
133
     * @param  string  $type
134
     * @param  bool    $where
135
     * @return \Illuminate\Database\Query\Builder|static
136
     *
137
     * @throws \InvalidArgumentException
138
     */
139
    public function joinSub($query, $as, $first, $operator = null, $second = null, $type = 'inner', $where = false)
140
    {
141
        [$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...
142
143
        $expression = '('.$query.') '.$this->grammar->wrapTable($as);
144
145
        $this->addBinding($bindings, 'join');
146
147
        return $this->join(new Expression($expression), $first, $operator, $second, $type, $where);
148
    }
149
}
150