1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the MIT license. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace Doctrine\DBAL\Driver\IBMDB2; |
21
|
|
|
|
22
|
|
|
use Doctrine\DBAL\Driver\Connection; |
23
|
|
|
use Doctrine\DBAL\Driver\ServerInfoAwareConnection; |
24
|
|
|
use Doctrine\DBAL\ParameterType; |
25
|
|
|
use stdClass; |
26
|
|
|
use const DB2_AUTOCOMMIT_OFF; |
27
|
|
|
use const DB2_AUTOCOMMIT_ON; |
28
|
|
|
use function db2_autocommit; |
29
|
|
|
use function db2_commit; |
30
|
|
|
use function db2_conn_error; |
31
|
|
|
use function db2_conn_errormsg; |
32
|
|
|
use function db2_connect; |
33
|
|
|
use function db2_escape_string; |
34
|
|
|
use function db2_exec; |
35
|
|
|
use function db2_last_insert_id; |
36
|
|
|
use function db2_num_rows; |
37
|
|
|
use function db2_pconnect; |
38
|
|
|
use function db2_prepare; |
39
|
|
|
use function db2_rollback; |
40
|
|
|
use function db2_server_info; |
41
|
|
|
use function db2_stmt_errormsg; |
42
|
|
|
use function func_get_args; |
43
|
|
|
|
44
|
|
|
class DB2Connection implements Connection, ServerInfoAwareConnection |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* @var resource |
48
|
|
|
*/ |
49
|
|
|
private $_conn = null; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param array $params |
53
|
|
|
* @param string $username |
54
|
|
|
* @param string $password |
55
|
|
|
* @param array $driverOptions |
56
|
|
|
* |
57
|
|
|
* @throws \Doctrine\DBAL\Driver\IBMDB2\DB2Exception |
58
|
|
|
*/ |
59
|
21 |
|
public function __construct(array $params, $username, $password, $driverOptions = []) |
60
|
|
|
{ |
61
|
21 |
|
$isPersistent = (isset($params['persistent']) && $params['persistent'] == true); |
62
|
|
|
|
63
|
21 |
|
if ($isPersistent) { |
64
|
|
|
$this->_conn = db2_pconnect($params['dbname'], $username, $password, $driverOptions); |
65
|
|
|
} else { |
66
|
21 |
|
$this->_conn = db2_connect($params['dbname'], $username, $password, $driverOptions); |
67
|
|
|
} |
68
|
21 |
|
if ( ! $this->_conn) { |
69
|
|
|
throw new DB2Exception(db2_conn_errormsg()); |
70
|
|
|
} |
71
|
21 |
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function getServerVersion() |
77
|
|
|
{ |
78
|
|
|
/** @var stdClass $serverInfo */ |
79
|
|
|
$serverInfo = db2_server_info($this->_conn); |
80
|
|
|
|
81
|
|
|
return $serverInfo->DBMS_VER; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
1 |
|
public function requiresQueryForServerVersion() |
88
|
|
|
{ |
89
|
1 |
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
223 |
|
public function prepare($sql) |
96
|
|
|
{ |
97
|
223 |
|
$stmt = @db2_prepare($this->_conn, $sql); |
98
|
223 |
|
if ( ! $stmt) { |
|
|
|
|
99
|
|
|
throw new DB2Exception(db2_stmt_errormsg()); |
100
|
|
|
} |
101
|
|
|
|
102
|
223 |
|
return new DB2Statement($stmt); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* {@inheritdoc} |
107
|
|
|
*/ |
108
|
155 |
|
public function query() |
109
|
|
|
{ |
110
|
155 |
|
$args = func_get_args(); |
111
|
155 |
|
$sql = $args[0]; |
112
|
155 |
|
$stmt = $this->prepare($sql); |
113
|
155 |
|
$stmt->execute(); |
114
|
|
|
|
115
|
155 |
|
return $stmt; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* {@inheritdoc} |
120
|
|
|
*/ |
121
|
4 |
|
public function quote($input, $type = ParameterType::STRING) |
122
|
|
|
{ |
123
|
4 |
|
$input = db2_escape_string($input); |
124
|
|
|
|
125
|
4 |
|
if ($type === ParameterType::INTEGER) { |
126
|
|
|
return $input; |
127
|
|
|
} |
128
|
|
|
|
129
|
4 |
|
return "'".$input."'"; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* {@inheritdoc} |
134
|
|
|
*/ |
135
|
140 |
|
public function exec($statement) |
136
|
|
|
{ |
137
|
140 |
|
$stmt = @db2_exec($this->_conn, $statement); |
138
|
|
|
|
139
|
140 |
|
if (false === $stmt) { |
140
|
86 |
|
throw new DB2Exception(db2_stmt_errormsg()); |
141
|
|
|
} |
142
|
|
|
|
143
|
115 |
|
return db2_num_rows($stmt); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* {@inheritdoc} |
148
|
|
|
*/ |
149
|
2 |
|
public function lastInsertId($name = null) |
150
|
|
|
{ |
151
|
2 |
|
return db2_last_insert_id($this->_conn); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* {@inheritdoc} |
156
|
|
|
*/ |
157
|
13 |
|
public function beginTransaction() |
158
|
|
|
{ |
159
|
13 |
|
db2_autocommit($this->_conn, DB2_AUTOCOMMIT_OFF); |
|
|
|
|
160
|
13 |
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* {@inheritdoc} |
164
|
|
|
*/ |
165
|
5 |
|
public function commit() |
166
|
|
|
{ |
167
|
5 |
|
if (!db2_commit($this->_conn)) { |
168
|
|
|
throw new DB2Exception(db2_conn_errormsg($this->_conn)); |
169
|
|
|
} |
170
|
5 |
|
db2_autocommit($this->_conn, DB2_AUTOCOMMIT_ON); |
|
|
|
|
171
|
5 |
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* {@inheritdoc} |
175
|
|
|
*/ |
176
|
9 |
|
public function rollBack() |
177
|
|
|
{ |
178
|
9 |
|
if (!db2_rollback($this->_conn)) { |
179
|
|
|
throw new DB2Exception(db2_conn_errormsg($this->_conn)); |
180
|
|
|
} |
181
|
9 |
|
db2_autocommit($this->_conn, DB2_AUTOCOMMIT_ON); |
|
|
|
|
182
|
9 |
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* {@inheritdoc} |
186
|
|
|
*/ |
187
|
|
|
public function errorCode() |
188
|
|
|
{ |
189
|
|
|
return db2_conn_error($this->_conn); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* {@inheritdoc} |
194
|
|
|
*/ |
195
|
|
|
public function errorInfo() |
196
|
|
|
{ |
197
|
|
|
return [ |
198
|
|
|
0 => db2_conn_errormsg($this->_conn), |
199
|
|
|
1 => $this->errorCode(), |
200
|
|
|
]; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|