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