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