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
|
|
View Code Duplication |
for ($i = 0; $i < count($values); $i++) { |
|
|
|
|
27
|
|
|
$type = $this->getPdoType($values[$i]); |
28
|
|
|
$statement->bindParam($parameter, $values[$i], $type); |
29
|
|
|
$parameter++; |
30
|
|
|
} |
31
|
|
|
$statement->bindParam($parameter, $id, PDO::PARAM_INT, 10); |
32
|
|
|
$statement->execute(); |
33
|
|
|
|
34
|
|
|
return (int) $id; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Get prepared statement. |
39
|
|
|
* |
40
|
|
|
* @param Builder $query |
41
|
|
|
* @param string $sql |
42
|
|
|
* @return \PDOStatement | \Yajra\Pdo\Oci8 |
43
|
|
|
*/ |
44
|
|
|
private function prepareStatement(Builder $query, $sql) |
45
|
|
|
{ |
46
|
|
|
$pdo = $query->getConnection()->getPdo(); |
47
|
|
|
|
48
|
|
|
return $pdo->prepare($sql); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Get PDO Type depending on value. |
53
|
|
|
* |
54
|
|
|
* @param mixed $value |
55
|
|
|
* @return int |
56
|
|
|
*/ |
57
|
|
|
private function getPdoType($value) |
58
|
|
|
{ |
59
|
|
|
if (is_int($value)) { |
60
|
|
|
return PDO::PARAM_INT; |
61
|
|
|
} elseif (is_bool($value)) { |
62
|
|
|
return PDO::PARAM_BOOL; |
63
|
|
|
} elseif (is_null($value)) { |
64
|
|
|
return PDO::PARAM_NULL; |
65
|
|
|
} else { |
66
|
|
|
return PDO::PARAM_STR; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* save Query with Blob returning primary key value |
72
|
|
|
* |
73
|
|
|
* @param Builder $query |
74
|
|
|
* @param string $sql |
75
|
|
|
* @param array $values |
76
|
|
|
* @param array $binaries |
77
|
|
|
* @return int |
78
|
|
|
*/ |
79
|
|
|
public function saveLob(Builder $query, $sql, array $values, array $binaries) |
80
|
|
|
{ |
81
|
|
|
$id = 0; |
82
|
|
|
$parameter = 0; |
83
|
|
|
$statement = $this->prepareStatement($query, $sql); |
84
|
|
|
|
85
|
|
|
// bind values. |
86
|
|
View Code Duplication |
for ($i = 0; $i < count($values); $i++) { |
|
|
|
|
87
|
|
|
$type = $this->getPdoType($values[$i]); |
88
|
|
|
$statement->bindParam($parameter, $values[$i], $type); |
89
|
|
|
$parameter++; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// bind blob fields. |
93
|
|
|
for ($i = 0; $i < count($binaries); $i++) { |
|
|
|
|
94
|
|
|
$statement->bindParam($parameter, $binaries[$i], PDO::PARAM_LOB, -1); |
95
|
|
|
$parameter++; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
// bind output param for the returning clause. |
99
|
|
|
$statement->bindParam($parameter, $id, PDO::PARAM_INT, 10); |
100
|
|
|
|
101
|
|
|
if (! $statement->execute()) { |
102
|
|
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return (int) $id; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
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: