-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathDb2supp.php
217 lines (192 loc) · 6.09 KB
/
Db2supp.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
namespace ToolkitApi;
/**
* Class db2supp
*
* @todo define common transport class/interface extended/implemented by all transports
*
* @package ToolkitApi
*/
class db2supp
{
private $last_errorcode;
private $last_errormsg;
/**
*
*
* @todo Throw in your "transport/adapter" framework for a real OO look and feel ....
* Throw new Exception("Fail execute ($sql) ".db2_stmt_errormsg(),db2_stmt_error());
* ... and retrieve via try/catch + Exception methods.
*
* @param $database
* @param $user
* @param $password
* @param null $options 'persistent' is one option
* @return bool
*/
public function connect($database, $user, $password, $options = null)
{
// Compensate for older ibm_db2 driver that may not do this check.
if ($user && empty($password)) {
$this->setErrorCode('08001');
$this->setErrorMsg('Authorization failure on distributed database connection attempt. SQLCODE=-30082');
return false;
}
if ($options) {
$driver_options = array();
// Test for existence of driver options
if (array_key_exists('driver_options', $options)) {
$driver_options = $options['driver_options'] ?: array();
}
if ((isset($options['persistent'])) && $options['persistent']) {
$conn = db2_pconnect($database, $user, $password, $driver_options);
} else {
$conn = db2_connect($database, $user, $password, $driver_options);
}
if (is_resource($conn)) {
return $conn;
}
}
$this->setErrorCode(db2_conn_error());
$this->setErrorMsg(db2_conn_errormsg());
return false;
}
/**
* @param $conn
*/
public function disconnect($conn)
{
if (is_resource($conn)) {
db2_close($conn);
}
}
/**
* disconnect, truly close, a persistent connection.
*
* NOTE: Only available on i5/OS
*
* @param $conn
*/
public function disconnectPersistent($conn)
{
if (is_resource($conn)) {
db2_pclose($conn);
}
}
/**
* @return string
*/
public function getErrorCode()
{
return $this->last_errorcode;
}
/**
* @return string
*/
public function getErrorMsg()
{
return $this->last_errormsg;
}
/**
* set error code and message based on last db2 prepare or execute error.
*
* @todo: consider using GET DIAGNOSTICS for even more message text:
* http://publib.boulder.ibm.com/infocenter/iseries/v5r4/index.jsp?topic=%2Frzala%2Frzalafinder.htm
*
* @param null $stmt
*/
protected function setStmtError($stmt = null)
{
if ($stmt) {
$this->setErrorCode(db2_stmt_error($stmt));
$this->setErrorMsg(db2_stmt_errormsg($stmt));
} else {
$this->setErrorCode(db2_stmt_error());
$this->setErrorMsg(db2_stmt_errormsg());
}
}
/**
* @param $errorCode
*/
protected function setErrorCode($errorCode)
{
$this->last_errorcode = $errorCode;
}
/**
* @param $errorMsg
*/
protected function setErrorMsg($errorMsg)
{
$this->last_errormsg = $errorMsg;
}
/**
* this function used for special stored procedure call only
*
* @param $conn
* @param $sql
* @return bool
*/
public function execXMLStoredProcedure($conn, $sql, $bindArray)
{
$internalKey = $bindArray['internalKey'];
$controlKey = $bindArray['controlKey'];
$inputXml = $bindArray['inputXml'];
$outputXml = $bindArray['outputXml'];
// @todo see why error doesn't properly bubble up to top level.
$crsr = @db2_prepare($conn, $sql);
if (!$crsr) {
$this->setStmtError();
return false;
}
// stored procedure takes four parameters. Each 'name' will be bound to a real PHP variable
$params = array(
array('position' => 1, 'name' => "internalKey", 'inout' => DB2_PARAM_IN),
array('position' => 2, 'name' => "controlKey", 'inout' => DB2_PARAM_IN),
array('position' => 3, 'name' => "inputXml", 'inout' => DB2_PARAM_IN),
array('position' => 4, 'name' => "outputXml", 'inout' => DB2_PARAM_OUT),
);
// bind the four parameters
foreach ($params as $param) {
$ret = db2_bind_param ($crsr, $param['position'], $param['name'], $param['inout']);
if (!$ret) {
// unable to bind a param. Set error and exit
$this->setStmtError($crsr);
return false;
}
}
$ret = @db2_execute($crsr);
if (!$ret) {
// execution of XMLSERVICE stored procedure failed.
$this->setStmtError($crsr);
return false;
}
return $outputXml;
}
/**
* returns a first column from sql stmt result set
*
* used in one place: iToolkitService's ReadSPLFData().
*
* @todo eliminate this method if possible.
*
* @param $conn
* @param $sql
* @throws \Exception
* @return array
*/
public function executeQuery($conn, $sql)
{
$txt = array();
$stmt = db2_exec($conn, $sql, array('cursor' => DB2_SCROLLABLE));
if (is_resource($stmt)) {
if (db2_fetch_row($stmt)) {
$column = db2_result($stmt, 0);
$txt[] = $column;
}
} else {
$this->setStmtError();
Throw new \Exception("Failure executing SQL: ($sql) " . db2_stmt_errormsg(), db2_stmt_error());
}
return $txt;
}
}