Completed
Push — 5.1 ( 05d0b9...c96f14 )
by Arjay
11s
created

OracleProcessor::saveLob()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 4
nop 4
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
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
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
     * Bind values to PDO statement.
49
     *
50
     * @param array $values
51
     * @param \PDOStatement $statement
52
     * @param int $parameter
53
     * @return int
54
     */
55
    private function bindValues(&$values, $statement, $parameter)
56
    {
57
        $count = count($values);
58
        for ($i = 0; $i < $count; $i++) {
59
            if (is_object($values[$i])) {
60
                $values[$i] = (string) $values[$i];
61
            }
62
            $type = $this->getPdoType($values[$i]);
63
            $statement->bindParam($parameter, $values[$i], $type);
64
            $parameter++;
65
        }
66
67
        return $parameter;
68
    }
69
70
    /**
71
     * Get PDO Type depending on value.
72
     *
73
     * @param mixed $value
74
     * @return int
75
     */
76
    private function getPdoType($value)
77
    {
78
        if (is_int($value)) {
79
            return PDO::PARAM_INT;
80
        } elseif (is_bool($value)) {
81
            return PDO::PARAM_BOOL;
82
        } elseif (is_null($value)) {
83
            return PDO::PARAM_NULL;
84
        } else {
85
            return PDO::PARAM_STR;
86
        }
87
    }
88
89
    /**
90
     * Save Query with Blob returning primary key value.
91
     *
92
     * @param  Builder $query
93
     * @param  string $sql
94
     * @param  array $values
95
     * @param  array $binaries
96
     * @return int
97
     */
98
    public function saveLob(Builder $query, $sql, array $values, array $binaries)
99
    {
100
        $id        = 0;
101
        $parameter = 0;
102
        $statement = $this->prepareStatement($query, $sql);
103
104
        $parameter = $this->bindValues($values, $statement, $parameter);
105
106
        $countBinary = count($binaries);
107
        for ($i = 0; $i < $countBinary; $i++) {
108
            $statement->bindParam($parameter, $binaries[$i], PDO::PARAM_LOB, -1);
109
            $parameter++;
110
        }
111
112
        // bind output param for the returning clause.
113
        $statement->bindParam($parameter, $id, PDO::PARAM_INT, 10);
114
115
        if (! $statement->execute()) {
116
            return false;
117
        }
118
119
        return (int) $id;
120
    }
121
122
    /**
123
     * Process the results of a column listing query.
124
     *
125
     * @param  array  $results
126
     * @return array
127
     */
128
    public function processColumnListing($results)
129
    {
130
        $mapping = function ($r) {
131
            $r = (object) $r;
132
            return $r->column_name;
133
        };
134
135
        return array_map($mapping, $results);
136
    }
137
}
138