Completed
Push — 2.4 ( c9a027...8ef238 )
by Arjay
01:59
created

OracleProcessor   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A processInsertGetId() 0 12 1
A prepareStatement() 0 6 1
A bindValues() 0 11 2
A getPdoType() 0 12 4
A saveLob() 0 23 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
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
            $type = $this->getPdoType($values[$i]);
60
            $statement->bindParam($parameter, $values[$i], $type);
61
            $parameter++;
62
        }
63
64
        return $parameter;
65
    }
66
67
    /**
68
     * Get PDO Type depending on value.
69
     *
70
     * @param mixed $value
71
     * @return int
72
     */
73
    private function getPdoType($value)
74
    {
75
        if (is_int($value)) {
76
            return PDO::PARAM_INT;
77
        } elseif (is_bool($value)) {
78
            return PDO::PARAM_BOOL;
79
        } elseif (is_null($value)) {
80
            return PDO::PARAM_NULL;
81
        } else {
82
            return PDO::PARAM_STR;
83
        }
84
    }
85
86
    /**
87
     * Save Query with Blob returning primary key value.
88
     *
89
     * @param  Builder $query
90
     * @param  string $sql
91
     * @param  array $values
92
     * @param  array $binaries
93
     * @return int
94
     */
95
    public function saveLob(Builder $query, $sql, array $values, array $binaries)
96
    {
97
        $id        = 0;
98
        $parameter = 0;
99
        $statement = $this->prepareStatement($query, $sql);
100
101
        $parameter = $this->bindValues($values, $statement, $parameter);
102
103
        $countBinary = count($binaries);
104
        for ($i = 0; $i < $countBinary; $i++) {
105
            $statement->bindParam($parameter, $binaries[$i], PDO::PARAM_LOB, -1);
106
            $parameter++;
107
        }
108
109
        // bind output param for the returning clause.
110
        $statement->bindParam($parameter, $id, PDO::PARAM_INT, 10);
111
112
        if (! $statement->execute()) {
113
            return false;
114
        }
115
116
        return (int) $id;
117
    }
118
}
119