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