|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=0); |
|
4
|
|
|
|
|
5
|
|
|
namespace Doctrine\DBAL\Driver\IBMDB2; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Driver\ResultStatement; |
|
8
|
|
|
use Doctrine\DBAL\Driver\ServerInfoAwareConnection; |
|
9
|
|
|
use Doctrine\DBAL\Driver\Statement as DriverStatement; |
|
10
|
|
|
use stdClass; |
|
11
|
|
|
use const DB2_AUTOCOMMIT_OFF; |
|
12
|
|
|
use const DB2_AUTOCOMMIT_ON; |
|
13
|
|
|
use function db2_autocommit; |
|
14
|
|
|
use function db2_commit; |
|
15
|
|
|
use function db2_connect; |
|
16
|
|
|
use function db2_escape_string; |
|
17
|
|
|
use function db2_exec; |
|
18
|
|
|
use function db2_last_insert_id; |
|
19
|
|
|
use function db2_num_rows; |
|
20
|
|
|
use function db2_pconnect; |
|
21
|
|
|
use function db2_prepare; |
|
22
|
|
|
use function db2_rollback; |
|
23
|
|
|
use function db2_server_info; |
|
24
|
|
|
|
|
25
|
|
|
class DB2Connection implements ServerInfoAwareConnection |
|
26
|
|
|
{ |
|
27
|
|
|
/** @var resource */ |
|
28
|
|
|
private $conn = null; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @param array<string, mixed> $params |
|
32
|
|
|
* @param array<string, mixed> $driverOptions |
|
33
|
|
|
* |
|
34
|
|
|
* @throws DB2Exception |
|
35
|
|
|
*/ |
|
36
|
|
|
public function __construct(array $params, string $username, string $password, array $driverOptions = []) |
|
37
|
|
|
{ |
|
38
|
|
|
if (isset($params['persistent']) && $params['persistent'] === true) { |
|
39
|
|
|
$conn = db2_pconnect($params['dbname'], $username, $password, $driverOptions); |
|
40
|
227 |
|
} else { |
|
41
|
|
|
$conn = db2_connect($params['dbname'], $username, $password, $driverOptions); |
|
42
|
227 |
|
} |
|
43
|
|
|
|
|
44
|
227 |
|
if ($conn === false) { |
|
45
|
|
|
throw DB2Exception::fromConnectionError(); |
|
46
|
|
|
} |
|
47
|
227 |
|
|
|
48
|
|
|
$this->conn = $conn; |
|
49
|
|
|
} |
|
50
|
227 |
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritdoc} |
|
53
|
|
|
*/ |
|
54
|
227 |
|
public function getServerVersion() : string |
|
55
|
227 |
|
{ |
|
56
|
|
|
/** @var stdClass $serverInfo */ |
|
57
|
|
|
$serverInfo = db2_server_info($this->conn); |
|
58
|
|
|
|
|
59
|
|
|
return $serverInfo->DBMS_VER; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
public function requiresQueryForServerVersion() : bool |
|
66
|
|
|
{ |
|
67
|
|
|
return false; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
228 |
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
228 |
|
public function prepare(string $sql) : DriverStatement |
|
74
|
|
|
{ |
|
75
|
|
|
$stmt = @db2_prepare($this->conn, $sql); |
|
76
|
|
|
if (! $stmt) { |
|
|
|
|
|
|
77
|
|
|
throw DB2Exception::fromStatementError(); |
|
78
|
|
|
} |
|
79
|
227 |
|
|
|
80
|
|
|
return new DB2Statement($stmt); |
|
81
|
227 |
|
} |
|
82
|
227 |
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* {@inheritdoc} |
|
85
|
|
|
*/ |
|
86
|
227 |
|
public function query(string $sql) : ResultStatement |
|
87
|
|
|
{ |
|
88
|
|
|
$stmt = $this->prepare($sql); |
|
89
|
|
|
$stmt->execute(); |
|
90
|
|
|
|
|
91
|
|
|
return $stmt; |
|
92
|
227 |
|
} |
|
93
|
|
|
|
|
94
|
227 |
|
/** |
|
95
|
227 |
|
* {@inheritdoc} |
|
96
|
227 |
|
*/ |
|
97
|
227 |
|
public function quote(string $input) : string |
|
98
|
|
|
{ |
|
99
|
227 |
|
return "'" . db2_escape_string($input) . "'"; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* {@inheritdoc} |
|
104
|
|
|
*/ |
|
105
|
227 |
|
public function exec(string $statement) : int |
|
106
|
|
|
{ |
|
107
|
227 |
|
$stmt = @db2_exec($this->conn, $statement); |
|
108
|
|
|
|
|
109
|
227 |
|
if ($stmt === false) { |
|
110
|
|
|
throw DB2Exception::fromStatementError(); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
227 |
|
return db2_num_rows($stmt); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* {@inheritdoc} |
|
118
|
|
|
*/ |
|
119
|
227 |
|
public function lastInsertId() |
|
120
|
|
|
{ |
|
121
|
227 |
|
$lastInsertId = db2_last_insert_id($this->conn); |
|
122
|
|
|
|
|
123
|
227 |
|
if ($lastInsertId === '') { |
|
124
|
227 |
|
throw new DB2Exception('No identity value was generated by the last statement.'); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
227 |
|
return $lastInsertId; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* {@inheritdoc} |
|
132
|
|
|
*/ |
|
133
|
7 |
|
public function getSequenceNumber(string $name) |
|
134
|
|
|
{ |
|
135
|
7 |
|
throw new DB2Exception('Sequences on IBM DB2 are not currently supported by the DBAL.'); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* {@inheritdoc} |
|
140
|
|
|
*/ |
|
141
|
220 |
|
public function beginTransaction() : void |
|
142
|
|
|
{ |
|
143
|
220 |
|
if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_OFF)) { |
|
144
|
220 |
|
throw DB2Exception::fromConnectionError($this->conn); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
216 |
|
* {@inheritdoc} |
|
150
|
|
|
*/ |
|
151
|
216 |
|
public function commit() : void |
|
152
|
|
|
{ |
|
153
|
|
|
if (! db2_commit($this->conn)) { |
|
154
|
216 |
|
throw DB2Exception::fromConnectionError($this->conn); |
|
155
|
216 |
|
} |
|
156
|
|
|
|
|
157
|
|
|
if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON)) { |
|
158
|
|
|
throw DB2Exception::fromConnectionError($this->conn); |
|
159
|
|
|
} |
|
160
|
220 |
|
} |
|
161
|
|
|
|
|
162
|
220 |
|
/** |
|
163
|
|
|
* {@inheritdoc} |
|
164
|
|
|
*/ |
|
165
|
220 |
|
public function rollBack() : void |
|
166
|
220 |
|
{ |
|
167
|
|
|
if (! db2_rollback($this->conn)) { |
|
168
|
|
|
throw DB2Exception::fromConnectionError($this->conn); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
if (! db2_autocommit($this->conn, DB2_AUTOCOMMIT_ON)) { |
|
172
|
|
|
throw DB2Exception::fromConnectionError($this->conn); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|