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