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