Completed
Push — master ( e7b533...82e63d )
by Arjay
01:59
created

OracleProcessor::bindValues()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 9
nc 3
nop 3
1
<?php
2
3
namespace Yajra\Oci8\Query\Processors;
4
5
use Illuminate\Database\Query\Builder;
6
use Illuminate\Database\Query\Processors\Processor;
7
use PDO;
8
9
class OracleProcessor extends Processor
10
{
11
    /**
12
     * Process an "insert get ID" query.
13
     *
14
     * @param  Builder $query
15
     * @param  string $sql
16
     * @param  array $values
17
     * @param  string $sequence
18
     * @return int
19
     */
20
    public function processInsertGetId(Builder $query, $sql, $values, $sequence = null)
21
    {
22
        $id        = 0;
23
        $parameter = 0;
24
        $statement = $this->prepareStatement($query, $sql);
25
        $values    = $this->incrementBySequence($values, $sequence);
26
        $parameter = $this->bindValues($values, $statement, $parameter);
27
        $statement->bindParam($parameter, $id, PDO::PARAM_INT, 10);
28
        $statement->execute();
29
30
        return (int) $id;
31
    }
32
33
    /**
34
     * Get prepared statement.
35
     *
36
     * @param Builder $query
37
     * @param string $sql
38
     * @return \PDOStatement|\Yajra\Pdo\Oci8
39
     */
40
    private function prepareStatement(Builder $query, $sql)
41
    {
42
        $pdo = $query->getConnection()->getPdo();
43
44
        return $pdo->prepare($sql);
45
    }
46
47
    /**
48
     * Insert a new record and get the value of the primary key.
49
     *
50
     * @param array $values
51
     * @param string $sequence
52
     * @return array
53
     */
54
    protected function incrementBySequence(array $values, $sequence)
55
    {
56
        $builder     = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 5)[4]['object'];
57
        $builderArgs = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 5)[3]['args'];
58
59
        if (! isset($builderArgs[1][0][$sequence])) {
60
            if (method_exists($builder, 'getModel')) {
61
                $model = $builder->getModel();
62
                if ($model->sequence && $model->incrementing) {
63
                    $values[] = (int) $model->getConnection()->getSequence()->nextValue($model->sequence);
64
                }
65
            }
66
        }
67
68
        return $values;
69
    }
70
71
    /**
72
     * Bind values to PDO statement.
73
     *
74
     * @param array $values
75
     * @param \PDOStatement $statement
76
     * @param int $parameter
77
     * @return int
78
     */
79
    private function bindValues(&$values, $statement, $parameter)
80
    {
81
        $count = count($values);
82
        for ($i = 0; $i < $count; $i++) {
83
            if (is_object($values[$i])) {
84
                $values[$i] = (string) $values[$i];
85
            }
86
            $type = $this->getPdoType($values[$i]);
87
            $statement->bindParam($parameter, $values[$i], $type);
88
            $parameter++;
89
        }
90
91
        return $parameter;
92
    }
93
94
    /**
95
     * Get PDO Type depending on value.
96
     *
97
     * @param mixed $value
98
     * @return int
99
     */
100
    private function getPdoType($value)
101
    {
102
        if (is_int($value)) {
103
            return PDO::PARAM_INT;
104
        } elseif (is_bool($value)) {
105
            return PDO::PARAM_BOOL;
106
        } elseif (is_null($value)) {
107
            return PDO::PARAM_NULL;
108
        } else {
109
            return PDO::PARAM_STR;
110
        }
111
    }
112
113
    /**
114
     * Save Query with Blob returning primary key value.
115
     *
116
     * @param  Builder $query
117
     * @param  string $sql
118
     * @param  array $values
119
     * @param  array $binaries
120
     * @return int
121
     */
122
    public function saveLob(Builder $query, $sql, array $values, array $binaries)
123
    {
124
        $id        = 0;
125
        $parameter = 0;
126
        $statement = $this->prepareStatement($query, $sql);
127
128
        $parameter = $this->bindValues($values, $statement, $parameter);
129
130
        $countBinary = count($binaries);
131
        for ($i = 0; $i < $countBinary; $i++) {
132
            $statement->bindParam($parameter, $binaries[$i], PDO::PARAM_LOB, -1);
133
            $parameter++;
134
        }
135
136
        // bind output param for the returning clause.
137
        $statement->bindParam($parameter, $id, PDO::PARAM_INT, 10);
138
139
        if (! $statement->execute()) {
140
            return false;
141
        }
142
143
        return (int) $id;
144
    }
145
146
     /**
147
     * Process the results of a column listing query.
148
     *
149
     * @param  array  $results
150
     * @return array
151
     */
152
    public function processColumnListing($results)
153
    {
154
        $mapping = function ($r) {
155
            $r = (object) $r;
156
157
            return $r->column_name;
158
        };
159
160
        return array_map($mapping, $results);
161
    }
162
}
163