Completed
Push — vendor ( b46fe0...bb4bad )
by Arjay
01:58
created

OracleBuilder   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 2
cbo 4
dl 0
loc 131
rs 10

6 Methods

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