-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathOdbcsupp.php
190 lines (168 loc) · 5.33 KB
/
Odbcsupp.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
<?php
namespace ToolkitApi;
/**
* Class odbcsupp
*
* @package ToolkitApi
*/
class odbcsupp
{
/**
* @var string
*/
private $last_errorcode;
/**
* @var string
*/
private $last_errormsg;
/**
*
* @todo should perhaps handle this method differently if $options are not passed
*
* @param string $database
* @param string $user
* @param string $password
* @param array|null $options
* @return bool|resource
*/
public function connect($database, $user, $password, $options = null)
{
if ($options) {
if ((isset($options['persistent'])) && $options['persistent']) {
$conn = odbc_pconnect($database, $user, $password);
} else {
$conn = odbc_connect($database, $user, $password);
}
if ((version_compare(PHP_VERSION, '8.4.0', '<') && is_resource($conn)) ||
(version_compare(PHP_VERSION, '8.4.0', '>=') && $conn instanceof \Odbc\Connection)) {
return $conn;
}
}
$this->setError();
return false;
}
/**
* @param resource $conn
*/
public function disconnect($conn)
{
if ((version_compare(PHP_VERSION, '8.4.0', '<') && is_resource($conn)) ||
(version_compare(PHP_VERSION, '8.4.0', '>=') && $conn instanceof \Odbc\Connection)) {
odbc_close($conn);
}
}
/**
* set error code and message based on last odbc connection/prepare/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 resource|null $conn
*/
protected function setError($conn = null)
{
// is conn resource provided, or do we get last error?
if ($conn) {
$this->setErrorCode(odbc_error($conn));
$this->setErrorMsg(odbc_errormsg($conn));
} else {
$this->setErrorCode(odbc_error());
$this->setErrorMsg(odbc_errormsg());
}
}
/**
* @param string $errorCode
*/
protected function setErrorCode($errorCode)
{
$this->last_errorcode = $errorCode;
}
/**
* @return string
*/
public function getErrorCode()
{
return $this->last_errorcode;
}
/**
* @param string $errorMsg
*/
protected function setErrorMsg($errorMsg)
{
$this->last_errormsg = $errorMsg;
}
/**
* @return string
*/
public function getErrorMsg()
{
return $this->last_errormsg;
}
/**
* this function used for special stored procedure call only
*
* @param resource $conn
* @param string $stmt
* @param array $bindArray
* @return string
*/
public function execXMLStoredProcedure($conn, $stmt, $bindArray)
{
$crsr = odbc_prepare($conn, $stmt);
if (!$crsr) {
$this->setError($conn);
return false;
}
// extension problem: sends warning message into the php_log or stdout
// about number of result sets. (switch on return code of SQLExecute()
// SQL_SUCCESS_WITH_INFO
if (!@odbc_execute($crsr , array($bindArray['internalKey'], $bindArray['controlKey'], $bindArray['inputXml']))) {
$this->setError($conn);
return "ODBC error code: " . $this->getErrorCode() . ' msg: ' . $this->getErrorMsg();
}
// disconnect operation cause crush in fetch, nothing appears as sql script.
$row='';
$outputXML = '';
if (!$bindArray['disconnect']) {
while (odbc_fetch_row($crsr)) {
$tmp = odbc_result($crsr, 1);
if ($tmp) {
// because of ODBC problem blob transferring should execute some "clean" on returned data
if (strstr($tmp , "</script>")) {
$pos = strpos($tmp, "</script>");
$pos += strlen("</script>"); // @todo why append this value?
$row .= substr($tmp, 0, $pos);
break;
} else {
$row .= $tmp;
}
}
}
$outputXML = $row;
}
return $outputXML;
}
/**
* @param resource $conn
* @param string $stmt
* @return array
*/
public function executeQuery($conn, $stmt)
{
$txt = array();
$crsr = odbc_exec($conn, $stmt);
if ((version_compare(PHP_VERSION, '8.4.0', '<') && is_resource($crsr)) ||
(version_compare(PHP_VERSION, '8.4.0', '>=') && $crsr instanceof \Odbc\Result)) {
while (odbc_fetch_row($crsr)) {
$row = odbc_result($crsr, 1);
if (!$row) {
break;
}
$txt[]= $row;
}
} else {
$this->setError($conn);
}
return $txt;
}
}