1 | <?php |
||
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 | $values = $this->incrementBySequence($values, $sequence); |
||
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 | /** @var \Yajra\Oci8\Oci8Connection $connection */ |
||
43 | $connection = $query->getConnection(); |
||
44 | $pdo = $connection->getPdo(); |
||
45 | |||
46 | return $pdo->prepare($sql); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Insert a new record and get the value of the primary key. |
||
51 | * |
||
52 | * @param array $values |
||
53 | * @param string $sequence |
||
54 | * @return array |
||
55 | */ |
||
56 | protected function incrementBySequence(array $values, $sequence) |
||
57 | { |
||
58 | $builder = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 5)[4]['object']; |
||
59 | $builderArgs = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 5)[3]['args']; |
||
60 | |||
61 | if (! isset($builderArgs[1][0][$sequence])) { |
||
62 | if (method_exists($builder, 'getModel')) { |
||
63 | $model = $builder->getModel(); |
||
64 | if ($model->sequence && $model->incrementing) { |
||
65 | $values[] = (int) $model->getConnection()->getSequence()->nextValue($model->sequence); |
||
66 | } |
||
67 | } |
||
68 | } |
||
69 | |||
70 | return $values; |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * Bind values to PDO statement. |
||
75 | * |
||
76 | * @param array $values |
||
77 | * @param \PDOStatement $statement |
||
78 | * @param int $parameter |
||
79 | * @return int |
||
80 | */ |
||
81 | private function bindValues(&$values, $statement, $parameter) |
||
82 | { |
||
83 | $count = count($values); |
||
84 | for ($i = 0; $i < $count; $i++) { |
||
85 | if (is_object($values[$i])) { |
||
86 | $values[$i] = (string) $values[$i]; |
||
87 | } |
||
88 | $type = $this->getPdoType($values[$i]); |
||
89 | $statement->bindParam($parameter, $values[$i], $type); |
||
90 | $parameter++; |
||
91 | } |
||
92 | |||
93 | return $parameter; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * Get PDO Type depending on value. |
||
98 | * |
||
99 | * @param mixed $value |
||
100 | * @return int |
||
101 | */ |
||
102 | private function getPdoType($value) |
||
103 | { |
||
104 | if (is_int($value)) { |
||
105 | return PDO::PARAM_INT; |
||
106 | } elseif (is_bool($value)) { |
||
107 | return PDO::PARAM_BOOL; |
||
108 | } elseif (is_null($value)) { |
||
109 | return PDO::PARAM_NULL; |
||
110 | } else { |
||
111 | return PDO::PARAM_STR; |
||
112 | } |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * Save Query with Blob returning primary key value. |
||
117 | * |
||
118 | * @param Builder $query |
||
119 | * @param string $sql |
||
120 | * @param array $values |
||
121 | * @param array $binaries |
||
122 | * @return int |
||
123 | */ |
||
124 | public function saveLob(Builder $query, $sql, array $values, array $binaries) |
||
125 | { |
||
126 | $id = 0; |
||
127 | $parameter = 0; |
||
128 | $statement = $this->prepareStatement($query, $sql); |
||
129 | |||
130 | $parameter = $this->bindValues($values, $statement, $parameter); |
||
131 | |||
132 | $countBinary = count($binaries); |
||
133 | for ($i = 0; $i < $countBinary; $i++) { |
||
134 | $statement->bindParam($parameter, $binaries[$i], PDO::PARAM_LOB, -1); |
||
135 | $parameter++; |
||
136 | } |
||
137 | |||
138 | // bind output param for the returning clause. |
||
139 | $statement->bindParam($parameter, $id, PDO::PARAM_INT, 10); |
||
140 | |||
141 | if (! $statement->execute()) { |
||
142 | return false; |
||
143 | } |
||
144 | |||
145 | return (int) $id; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * Process the results of a column listing query. |
||
150 | * |
||
151 | * @param array $results |
||
152 | * @return array |
||
153 | */ |
||
154 | public function processColumnListing($results) |
||
164 | } |
||
165 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.