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++) { |
|
|
|
|
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
|
|
|
|
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: