Completed
Push — feature/scrutinizer ( dc1915...8b8a9d )
by Arjay
01:58
created

OracleProcessor   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

5 Methods

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