Completed
Push — 4.0 ( 7b9ec6...defc50 )
by Arjay
02:04
created

OracleProcessor::prepareStatement()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 5
rs 9.4286
cc 1
eloc 3
nc 1
nop 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
     * DB Statement
13
     *
14
     * @var \PDOStatement
15
     */
16
    protected $statement;
17
18
    /**
19
     * Process an "insert get ID" query.
20
     *
21
     * @param  Builder $query
22
     * @param  string $sql
23
     * @param  array $values
24
     * @param  string $sequence
25
     * @return int
26
     */
27
    public function processInsertGetId(Builder $query, $sql, $values, $sequence = null)
28
    {
29
        $parameter = 0;
30
        $id        = 0;
31
32
        $this->prepareStatement($query, $sql);
33
        for ($i = 0; $i < count($values); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
34
            ${'param' . $i} = $values[$i];
35
            $this->bindValue($parameter, ${'param' . $i});
36
            $parameter++;
37
        }
38
        $this->bindValue($parameter, $id);
39
        $this->statement->execute();
40
41
        return (int) $id;
42
    }
43
44
    /**
45
     * @param Builder $query
46
     * @param string $sql
47
     * @internal param $PDOStatement
48
     */
49
    private function prepareStatement(Builder $query, $sql)
50
    {
51
        $pdo             = $query->getConnection()->getPdo();
52
        $this->statement = $pdo->prepare($sql);
53
    }
54
55
    /**
56
     * save Query with Blob returning primary key value
57
     *
58
     * @param  Builder $query
59
     * @param  string $sql
60
     * @param  array $values
61
     * @param  array $binaries
62
     * @return int
63
     */
64
    public function saveLob(Builder $query, $sql, array $values, array $binaries)
65
    {
66
        $parameter = 0;
67
        $lob       = [];
68
        $id        = 0;
69
70
        // begin transaction
71
        $pdo           = $query->getConnection()->getPdo();
72
        $inTransaction = $pdo->inTransaction();
73
        if (! $inTransaction) {
74
            $pdo->beginTransaction();
75
        }
76
77
        $this->prepareStatement($query, $sql);
78
        foreach ($values as $value) {
79
            $this->bindValue($parameter, $value);
80
            $parameter++;
81
        }
82
83
        $binariesCount = count($binaries);
84
        for ($i = 0; $i < $binariesCount; $i++) {
85
            // bind blob descriptor
86
            $this->statement->bindParam($parameter, $lob[$i], PDO::PARAM_LOB);
87
            $parameter++;
88
        }
89
90
        // bind output param for the returning clause
91
        $this->statement->bindParam($parameter, $id, PDO::PARAM_INT);
92
93
        // execute statement
94
        if (! $this->statement->execute()) {
95
            $pdo->rollBack();
96
97
            return false;
98
        }
99
100
        for ($i = 0; $i < $binariesCount; $i++) {
101
            // Discard the existing LOB contents
102
            if (! $lob[$i]->truncate()) {
103
                $pdo->rollBack();
104
105
                return false;
106
            }
107
            // save blob content
108
            if (! $lob[$i]->save($binaries[$i])) {
109
                $pdo->rollBack();
110
111
                return false;
112
            }
113
        }
114
115
        if (! $inTransaction) {
116
            // commit statements
117
            $pdo->commit();
118
        }
119
120
        return (int) $id;
121
    }
122
123
    /**
124
     * Bind value to an statement
125
     *
126
     * @param int $parameter
127
     * @param mixed $variable
128
     */
129
    private function bindValue($parameter, &$variable)
130
    {
131
        $param     = PDO::PARAM_STR;
132
        $maxLength = -1;
133
134
        if (is_int($variable)) {
135
            $param     = PDO::PARAM_INT;
136
            $maxLength = 10;
137
        } elseif (is_bool($variable)) {
138
            $param = PDO::PARAM_BOOL;
139
        } elseif (is_null($variable)) {
140
            $param = PDO::PARAM_NULL;
141
        } elseif ($variable instanceof \DateTimeInterface) {
142
            $variable = $variable->format('Y-m-d H:i:s');
143
        }
144
145
        $this->statement->bindParam($parameter, $variable, $param, $maxLength);
146
    }
147
}
148