1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Driver\OCI8; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Driver\Connection; |
8
|
|
|
use Doctrine\DBAL\Driver\ResultStatement; |
9
|
|
|
use Doctrine\DBAL\Driver\ServerInfoAwareConnection; |
10
|
|
|
use Doctrine\DBAL\Driver\Statement as DriverStatement; |
11
|
|
|
use UnexpectedValueException; |
12
|
|
|
use const OCI_DEFAULT; |
13
|
|
|
use function addcslashes; |
14
|
|
|
use function oci_commit; |
15
|
|
|
use function oci_connect; |
16
|
|
|
use function oci_error; |
17
|
|
|
use function oci_pconnect; |
18
|
|
|
use function oci_rollback; |
19
|
|
|
use function oci_server_version; |
20
|
|
|
use function preg_match; |
21
|
|
|
use function sprintf; |
22
|
|
|
use function str_replace; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* OCI8 implementation of the Connection interface. |
26
|
|
|
*/ |
27
|
|
|
class OCI8Connection implements Connection, ServerInfoAwareConnection |
28
|
|
|
{ |
29
|
|
|
/** @var resource */ |
30
|
|
|
protected $dbh; |
31
|
|
|
|
32
|
|
|
/** @var ExecutionMode */ |
33
|
|
|
private $executionMode; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Creates a Connection to an Oracle Database using oci8 extension. |
37
|
|
|
* |
38
|
|
|
* @throws OCI8Exception |
39
|
|
|
*/ |
40
|
|
|
public function __construct( |
41
|
|
|
string $username, |
42
|
30 |
|
string $password, |
43
|
|
|
string $db, |
44
|
|
|
string $charset = '', |
45
|
|
|
int $sessionMode = OCI_DEFAULT, |
46
|
|
|
bool $persistent = false |
47
|
|
|
) { |
48
|
|
|
$dbh = $persistent |
49
|
|
|
? @oci_pconnect($username, $password, $db, $charset, $sessionMode) |
50
|
30 |
|
: @oci_connect($username, $password, $db, $charset, $sessionMode); |
51
|
|
|
|
52
|
30 |
|
if ($dbh === false) { |
53
|
|
|
throw OCI8Exception::fromErrorInfo(oci_error()); |
54
|
30 |
|
} |
55
|
3 |
|
|
56
|
|
|
$this->dbh = $dbh; |
57
|
|
|
$this->executionMode = new ExecutionMode(); |
58
|
27 |
|
} |
59
|
27 |
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
* |
63
|
|
|
* @throws UnexpectedValueException If the version string returned by the database server |
64
|
|
|
* does not contain a parsable version number. |
65
|
|
|
*/ |
66
|
|
|
public function getServerVersion() : string |
67
|
|
|
{ |
68
|
|
|
$version = oci_server_version($this->dbh); |
69
|
|
|
|
70
|
|
|
if ($version === false) { |
71
|
|
|
throw OCI8Exception::fromErrorInfo(oci_error($this->dbh)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if (! preg_match('/\s+(\d+\.\d+\.\d+\.\d+\.\d+)\s+/', $version, $matches)) { |
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
|
|
|
$version |
80
|
|
|
) |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $matches[1]; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
|
|
public function requiresQueryForServerVersion() : bool |
91
|
2 |
|
{ |
92
|
|
|
return false; |
93
|
2 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
|
|
public function prepare(string $sql) : DriverStatement |
99
|
348 |
|
{ |
100
|
|
|
return new OCI8Statement($this->dbh, $sql, $this->executionMode); |
101
|
348 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
|
|
public function query(string $sql) : ResultStatement |
107
|
208 |
|
{ |
108
|
|
|
$stmt = $this->prepare($sql); |
109
|
208 |
|
$stmt->execute(); |
110
|
208 |
|
|
111
|
|
|
return $stmt; |
112
|
205 |
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* {@inheritdoc} |
116
|
|
|
*/ |
117
|
|
|
public function quote(string $input) : string |
118
|
2 |
|
{ |
119
|
|
|
return "'" . addcslashes(str_replace("'", "''", $input), "\000\n\r\\\032") . "'"; |
120
|
2 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
|
|
public function exec(string $statement) : int |
126
|
177 |
|
{ |
127
|
|
|
$stmt = $this->prepare($statement); |
128
|
177 |
|
$stmt->execute(); |
129
|
177 |
|
|
130
|
|
|
return $stmt->rowCount(); |
131
|
171 |
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
|
|
public function lastInsertId(?string $name = null) : string |
137
|
4 |
|
{ |
138
|
|
|
if ($name === null) { |
139
|
4 |
|
throw new OCI8Exception('The driver does not support identity columns.'); |
140
|
1 |
|
} |
141
|
|
|
|
142
|
|
|
$sql = 'SELECT ' . $name . '.CURRVAL FROM DUAL'; |
143
|
3 |
|
$stmt = $this->query($sql); |
144
|
3 |
|
$result = $stmt->fetchColumn(); |
145
|
3 |
|
|
146
|
|
|
if ($result === false) { |
147
|
3 |
|
throw new OCI8Exception('lastInsertId failed: Query was executed but no result was returned.'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $result; |
151
|
3 |
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* {@inheritdoc} |
155
|
|
|
*/ |
156
|
|
|
public function beginTransaction() : void |
157
|
343 |
|
{ |
158
|
|
|
$this->executionMode->disableAutoCommit(); |
159
|
343 |
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* {@inheritdoc} |
163
|
|
|
*/ |
164
|
|
|
public function commit() : void |
165
|
16 |
|
{ |
166
|
|
|
if (! oci_commit($this->dbh)) { |
167
|
16 |
|
throw OCI8Exception::fromErrorInfo(oci_error($this->dbh)); |
168
|
16 |
|
} |
169
|
|
|
|
170
|
|
|
$this->executionMode->enableAutoCommit(); |
171
|
|
|
} |
172
|
|
|
|
173
|
7 |
|
/** |
174
|
|
|
* {@inheritdoc} |
175
|
7 |
|
*/ |
176
|
|
|
public function rollBack() : void |
177
|
|
|
{ |
178
|
|
|
if (! oci_rollback($this->dbh)) { |
179
|
7 |
|
throw OCI8Exception::fromErrorInfo(oci_error($this->dbh)); |
180
|
7 |
|
} |
181
|
|
|
|
182
|
|
|
$this->executionMode->enableAutoCommit(); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|