Completed
Push — master ( 6df8f5...4829ab )
by Arjay
12:03
created

OracleBuilder::fromSub()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 6
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
        list($query, $bindings) = $this->createSub($query);
121
122
        return $this->fromRaw('('.$query.') '.$this->grammar->wrap($as), $bindings);
123
    }
124
}
125