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