1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\DBAL\Driver\OCI8; |
4
|
|
|
|
5
|
|
|
use Doctrine\DBAL\Driver\Connection; |
6
|
|
|
use Doctrine\DBAL\Driver\ServerInfoAwareConnection; |
7
|
|
|
use Doctrine\DBAL\ParameterType; |
8
|
|
|
use UnexpectedValueException; |
9
|
|
|
use function addcslashes; |
10
|
|
|
use function func_get_args; |
11
|
|
|
use function is_float; |
12
|
|
|
use function is_int; |
13
|
|
|
use function oci_commit; |
14
|
|
|
use function oci_connect; |
15
|
|
|
use function oci_error; |
16
|
|
|
use function oci_pconnect; |
17
|
|
|
use function oci_rollback; |
18
|
|
|
use function oci_server_version; |
19
|
|
|
use function preg_match; |
20
|
|
|
use function sprintf; |
21
|
|
|
use function str_replace; |
22
|
|
|
use const OCI_COMMIT_ON_SUCCESS; |
23
|
|
|
use const OCI_NO_AUTO_COMMIT; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* OCI8 implementation of the Connection interface. |
27
|
|
|
*/ |
28
|
|
|
class OCI8Connection implements Connection, ServerInfoAwareConnection |
29
|
|
|
{ |
30
|
|
|
/** @var resource */ |
31
|
|
|
protected $dbh; |
32
|
|
|
|
33
|
|
|
/** @var int */ |
34
|
|
|
protected $executeMode = OCI_COMMIT_ON_SUCCESS; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Creates a Connection to an Oracle Database using oci8 extension. |
38
|
|
|
* |
39
|
|
|
* @param string $username |
40
|
|
|
* @param string $password |
41
|
|
|
* @param string $db |
42
|
|
|
* @param string $charset |
43
|
|
|
* @param int $sessionMode |
44
|
|
|
* @param bool $persistent |
45
|
|
|
* |
46
|
|
|
* @throws OCI8Exception |
47
|
|
|
*/ |
48
|
32 |
|
public function __construct( |
49
|
|
|
$username, |
50
|
|
|
$password, |
51
|
|
|
$db, |
52
|
|
|
$charset = '', |
53
|
|
|
$sessionMode = OCI_NO_AUTO_COMMIT, |
54
|
|
|
$persistent = false |
55
|
|
|
) { |
56
|
32 |
|
$dbh = $persistent |
57
|
|
|
? @oci_pconnect($username, $password, $db, $charset, $sessionMode) |
58
|
32 |
|
: @oci_connect($username, $password, $db, $charset, $sessionMode); |
59
|
|
|
|
60
|
32 |
|
if ($dbh === false) { |
61
|
3 |
|
throw OCI8Exception::fromErrorInfo(oci_error()); |
62
|
|
|
} |
63
|
|
|
|
64
|
29 |
|
$this->dbh = $dbh; |
65
|
29 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
* |
70
|
|
|
* @throws UnexpectedValueException If the version string returned by the database server |
71
|
|
|
* does not contain a parsable version number. |
72
|
|
|
*/ |
73
|
|
|
public function getServerVersion() |
74
|
|
|
{ |
75
|
|
|
$version = oci_server_version($this->dbh); |
76
|
|
|
|
77
|
|
|
if ($version === false) { |
78
|
|
|
throw OCI8Exception::fromErrorInfo(oci_error($this->dbh)); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (! preg_match('/\s+(\d+\.\d+\.\d+\.\d+\.\d+)\s+/', $version, $matches)) { |
82
|
|
|
throw new UnexpectedValueException( |
83
|
|
|
sprintf( |
84
|
|
|
'Unexpected database version string "%s". Cannot parse an appropriate version number from it. ' . |
85
|
|
|
'Please report this database version string to the Doctrine team.', |
86
|
|
|
$version |
87
|
|
|
) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $matches[1]; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritdoc} |
96
|
|
|
*/ |
97
|
1 |
|
public function requiresQueryForServerVersion() |
98
|
|
|
{ |
99
|
1 |
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritdoc} |
104
|
|
|
*/ |
105
|
309 |
|
public function prepare($prepareString) |
106
|
|
|
{ |
107
|
309 |
|
return new OCI8Statement($this->dbh, $prepareString, $this); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
213 |
|
public function query() |
114
|
|
|
{ |
115
|
213 |
|
$args = func_get_args(); |
116
|
213 |
|
$sql = $args[0]; |
117
|
|
|
//$fetchMode = $args[1]; |
118
|
213 |
|
$stmt = $this->prepare($sql); |
119
|
213 |
|
$stmt->execute(); |
120
|
|
|
|
121
|
210 |
|
return $stmt; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* {@inheritdoc} |
126
|
|
|
*/ |
127
|
4 |
|
public function quote($value, $type = ParameterType::STRING) |
128
|
|
|
{ |
129
|
4 |
|
if (is_int($value) || is_float($value)) { |
130
|
2 |
|
return $value; |
131
|
|
|
} |
132
|
|
|
|
133
|
3 |
|
$value = str_replace("'", "''", $value); |
134
|
|
|
|
135
|
3 |
|
return "'" . addcslashes($value, "\000\n\r\\\032") . "'"; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* {@inheritdoc} |
140
|
|
|
*/ |
141
|
182 |
|
public function exec($statement) |
142
|
|
|
{ |
143
|
182 |
|
$stmt = $this->prepare($statement); |
144
|
182 |
|
$stmt->execute(); |
145
|
|
|
|
146
|
176 |
|
return $stmt->rowCount(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
4 |
|
public function lastInsertId($name = null) |
153
|
|
|
{ |
154
|
4 |
|
if ($name === null) { |
155
|
1 |
|
return false; |
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
3 |
|
$sql = 'SELECT ' . $name . '.CURRVAL FROM DUAL'; |
159
|
3 |
|
$stmt = $this->query($sql); |
160
|
3 |
|
$result = $stmt->fetchColumn(); |
161
|
|
|
|
162
|
3 |
|
if ($result === false) { |
163
|
|
|
throw new OCI8Exception('lastInsertId failed: Query was executed but no result was returned.'); |
164
|
|
|
} |
165
|
|
|
|
166
|
3 |
|
return (int) $result; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Returns the current execution mode. |
171
|
|
|
* |
172
|
|
|
* @return int |
173
|
|
|
*/ |
174
|
304 |
|
public function getExecuteMode() |
175
|
|
|
{ |
176
|
304 |
|
return $this->executeMode; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* {@inheritdoc} |
181
|
|
|
*/ |
182
|
16 |
|
public function beginTransaction() |
183
|
|
|
{ |
184
|
16 |
|
$this->executeMode = OCI_NO_AUTO_COMMIT; |
185
|
|
|
|
186
|
16 |
|
return true; |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* {@inheritdoc} |
191
|
|
|
*/ |
192
|
7 |
|
public function commit() |
193
|
|
|
{ |
194
|
7 |
|
if (! oci_commit($this->dbh)) { |
195
|
|
|
throw OCI8Exception::fromErrorInfo($this->errorInfo()); |
196
|
|
|
} |
197
|
|
|
|
198
|
7 |
|
$this->executeMode = OCI_COMMIT_ON_SUCCESS; |
199
|
|
|
|
200
|
7 |
|
return true; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* {@inheritdoc} |
205
|
|
|
*/ |
206
|
10 |
|
public function rollBack() |
207
|
|
|
{ |
208
|
10 |
|
if (! oci_rollback($this->dbh)) { |
209
|
|
|
throw OCI8Exception::fromErrorInfo($this->errorInfo()); |
210
|
|
|
} |
211
|
|
|
|
212
|
10 |
|
$this->executeMode = OCI_COMMIT_ON_SUCCESS; |
213
|
|
|
|
214
|
10 |
|
return true; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* {@inheritdoc} |
219
|
|
|
*/ |
220
|
|
|
public function errorCode() |
221
|
|
|
{ |
222
|
|
|
$error = oci_error($this->dbh); |
223
|
|
|
if ($error !== false) { |
224
|
|
|
$error = $error['code']; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
return $error; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* {@inheritdoc} |
232
|
|
|
*/ |
233
|
|
|
public function errorInfo() |
234
|
|
|
{ |
235
|
|
|
$error = oci_error($this->dbh); |
236
|
|
|
|
237
|
|
|
if ($error === false) { |
238
|
|
|
return []; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
return $error; |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
In the issue above, the returned value is violating the contract defined by the mentioned interface.
Let's take a look at an example: