1 | <?php |
---|
2 | /* |
---|
3 | V4.51 29 July 2004 (c) 2000-2004 John Lim (jlim@natsoft.com.my). All rights reserved. |
---|
4 | Released under both BSD license and Lesser GPL library license. |
---|
5 | Whenever there is any discrepancy between the two licenses, |
---|
6 | the BSD license will take precedence. |
---|
7 | Set tabs to 4 for best viewing. |
---|
8 | |
---|
9 | Latest version is available at http://adodb.sourceforge.net |
---|
10 | |
---|
11 | DB2 data driver. Requires ODBC. |
---|
12 | |
---|
13 | From phpdb list: |
---|
14 | |
---|
15 | Hi Andrew, |
---|
16 | |
---|
17 | thanks a lot for your help. Today we discovered what |
---|
18 | our real problem was: |
---|
19 | |
---|
20 | After "playing" a little bit with the php-scripts that try |
---|
21 | to connect to the IBM DB2, we set the optional parameter |
---|
22 | Cursortype when calling odbc_pconnect(....). |
---|
23 | |
---|
24 | And the exciting thing: When we set the cursor type |
---|
25 | to SQL_CUR_USE_ODBC Cursor Type, then |
---|
26 | the whole query speed up from 1 till 10 seconds |
---|
27 | to 0.2 till 0.3 seconds for 100 records. Amazing!!! |
---|
28 | |
---|
29 | Therfore, PHP is just almost fast as calling the DB2 |
---|
30 | from Servlets using JDBC (don't take too much care |
---|
31 | about the speed at whole: the database was on a |
---|
32 | completely other location, so the whole connection |
---|
33 | was made over a slow network connection). |
---|
34 | |
---|
35 | I hope this helps when other encounter the same |
---|
36 | problem when trying to connect to DB2 from |
---|
37 | PHP. |
---|
38 | |
---|
39 | Kind regards, |
---|
40 | Christian Szardenings |
---|
41 | |
---|
42 | 2 Oct 2001 |
---|
43 | Mark Newnham has discovered that the SQL_CUR_USE_ODBC is not supported by |
---|
44 | IBM's DB2 ODBC driver, so this must be a 3rd party ODBC driver. |
---|
45 | |
---|
46 | From the IBM CLI Reference: |
---|
47 | |
---|
48 | SQL_ATTR_ODBC_CURSORS (DB2 CLI v5) |
---|
49 | This connection attribute is defined by ODBC, but is not supported by DB2 |
---|
50 | CLI. Any attempt to set or get this attribute will result in an SQLSTATE of |
---|
51 | HYC00 (Driver not capable). |
---|
52 | |
---|
53 | A 32-bit option specifying how the Driver Manager uses the ODBC cursor |
---|
54 | library. |
---|
55 | |
---|
56 | So I guess this means the message [above] was related to using a 3rd party |
---|
57 | odbc driver. |
---|
58 | |
---|
59 | Setting SQL_CUR_USE_ODBC |
---|
60 | ======================== |
---|
61 | To set SQL_CUR_USE_ODBC for drivers that require it, do this: |
---|
62 | |
---|
63 | $db = NewADOConnection('db2'); |
---|
64 | $db->curMode = SQL_CUR_USE_ODBC; |
---|
65 | $db->Connect($dsn, $userid, $pwd); |
---|
66 | |
---|
67 | |
---|
68 | |
---|
69 | USING CLI INTERFACE |
---|
70 | =================== |
---|
71 | |
---|
72 | I have had reports that the $host and $database params have to be reversed in |
---|
73 | Connect() when using the CLI interface. From Halmai Csongor csongor.halmai#nexum.hu: |
---|
74 | |
---|
75 | > The symptom is that if I change the database engine from postgres or any other to DB2 then the following |
---|
76 | > connection command becomes wrong despite being described this version to be correct in the docs. |
---|
77 | > |
---|
78 | > $connection_object->Connect( $DATABASE_HOST, $DATABASE_AUTH_USER_NAME, $DATABASE_AUTH_PASSWORD, $DATABASE_NAME ) |
---|
79 | > |
---|
80 | > In case of DB2 I had to swap the first and last arguments in order to connect properly. |
---|
81 | |
---|
82 | |
---|
83 | */ |
---|
84 | |
---|
85 | // security - hide paths |
---|
86 | if (!defined('ADODB_DIR')) die(); |
---|
87 | |
---|
88 | if (!defined('_ADODB_ODBC_LAYER')) { |
---|
89 | include(ADODB_DIR."/drivers/adodb-odbc.inc.php"); |
---|
90 | } |
---|
91 | if (!defined('ADODB_DB2')){ |
---|
92 | define('ADODB_DB2',1); |
---|
93 | |
---|
94 | class ADODB_DB2 extends ADODB_odbc { |
---|
95 | var $databaseType = "db2"; |
---|
96 | var $concat_operator = '||'; |
---|
97 | var $sysDate = 'CURRENT_DATE'; |
---|
98 | var $sysTimeStamp = 'CURRENT TIMESTAMP'; |
---|
99 | // The complete string representation of a timestamp has the form |
---|
100 | // yyyy-mm-dd-hh.mm.ss.nnnnnn. |
---|
101 | var $fmtTimeStamp = "'Y-m-d-H.i.s'"; |
---|
102 | var $ansiOuter = true; |
---|
103 | var $identitySQL = 'values IDENTITY_VAL_LOCAL()'; |
---|
104 | var $_bindInputArray = true; |
---|
105 | var $hasInsertID = true; |
---|
106 | |
---|
107 | function ADODB_DB2() |
---|
108 | { |
---|
109 | if (strncmp(PHP_OS,'WIN',3) === 0) $this->curmode = SQL_CUR_USE_ODBC; |
---|
110 | $this->ADODB_odbc(); |
---|
111 | } |
---|
112 | |
---|
113 | function IfNull( $field, $ifNull ) |
---|
114 | { |
---|
115 | return " COALESCE($field, $ifNull) "; // if DB2 UDB |
---|
116 | } |
---|
117 | |
---|
118 | function ServerInfo() |
---|
119 | { |
---|
120 | //odbc_setoption($this->_connectionID,1,101 /*SQL_ATTR_ACCESS_MODE*/, 1 /*SQL_MODE_READ_ONLY*/); |
---|
121 | $vers = $this->GetOne('select versionnumber from sysibm.sysversions'); |
---|
122 | //odbc_setoption($this->_connectionID,1,101, 0 /*SQL_MODE_READ_WRITE*/); |
---|
123 | return array('description'=>'DB2 ODBC driver', 'version'=>$vers); |
---|
124 | } |
---|
125 | |
---|
126 | function _insertid() |
---|
127 | { |
---|
128 | return $this->GetOne($this->identitySQL); |
---|
129 | } |
---|
130 | |
---|
131 | function RowLock($tables,$where) |
---|
132 | { |
---|
133 | if ($this->_autocommit) $this->BeginTrans(); |
---|
134 | return $this->GetOne("select 1 as ignore from $tables where $where for update"); |
---|
135 | } |
---|
136 | |
---|
137 | function &MetaTables($ttype=false,$showSchema=false, $qtable="%", $qschema="%") |
---|
138 | { |
---|
139 | global $ADODB_FETCH_MODE; |
---|
140 | |
---|
141 | $savem = $ADODB_FETCH_MODE; |
---|
142 | $ADODB_FETCH_MODE = ADODB_FETCH_NUM; |
---|
143 | $qid = odbc_tables($this->_connectionID, "", $qschema, $qtable, ""); |
---|
144 | |
---|
145 | $rs = new ADORecordSet_odbc($qid); |
---|
146 | |
---|
147 | $ADODB_FETCH_MODE = $savem; |
---|
148 | if (!$rs) return false; |
---|
149 | |
---|
150 | $rs->_has_stupid_odbc_fetch_api_change = $this->_has_stupid_odbc_fetch_api_change; |
---|
151 | |
---|
152 | $arr =& $rs->GetArray(); |
---|
153 | //print_r($arr); |
---|
154 | |
---|
155 | $rs->Close(); |
---|
156 | $arr2 = array(); |
---|
157 | |
---|
158 | if ($ttype) { |
---|
159 | $isview = strncmp($ttype,'V',1) === 0; |
---|
160 | } |
---|
161 | for ($i=0; $i < sizeof($arr); $i++) { |
---|
162 | |
---|
163 | if (!$arr[$i][2]) continue; |
---|
164 | if (strncmp($arr[$i][1],'SYS',3) === 0) continue; |
---|
165 | |
---|
166 | $type = $arr[$i][3]; |
---|
167 | |
---|
168 | if ($showSchema) $arr[$i][2] = $arr[$i][1].'.'.$arr[$i][2]; |
---|
169 | |
---|
170 | if ($ttype) { |
---|
171 | if ($isview) { |
---|
172 | if (strncmp($type,'V',1) === 0) $arr2[] = $arr[$i][2]; |
---|
173 | } else if (strncmp($type,'T',1) === 0) $arr2[] = $arr[$i][2]; |
---|
174 | } else if (strncmp($type,'S',1) !== 0) $arr2[] = $arr[$i][2]; |
---|
175 | } |
---|
176 | return $arr2; |
---|
177 | } |
---|
178 | |
---|
179 | |
---|
180 | // Format date column in sql string given an input format that understands Y M D |
---|
181 | function SQLDate($fmt, $col=false) |
---|
182 | { |
---|
183 | // use right() and replace() ? |
---|
184 | if (!$col) $col = $this->sysDate; |
---|
185 | $s = ''; |
---|
186 | |
---|
187 | $len = strlen($fmt); |
---|
188 | for ($i=0; $i < $len; $i++) { |
---|
189 | if ($s) $s .= '||'; |
---|
190 | $ch = $fmt[$i]; |
---|
191 | switch($ch) { |
---|
192 | case 'Y': |
---|
193 | case 'y': |
---|
194 | $s .= "char(year($col))"; |
---|
195 | break; |
---|
196 | case 'M': |
---|
197 | $s .= "substr(monthname($col),1,3)"; |
---|
198 | break; |
---|
199 | case 'm': |
---|
200 | $s .= "right(digits(month($col)),2)"; |
---|
201 | break; |
---|
202 | case 'D': |
---|
203 | case 'd': |
---|
204 | $s .= "right(digits(day($col)),2)"; |
---|
205 | break; |
---|
206 | case 'H': |
---|
207 | case 'h': |
---|
208 | if ($col != $this->sysDate) $s .= "right(digits(hour($col)),2)"; |
---|
209 | else $s .= "''"; |
---|
210 | break; |
---|
211 | case 'i': |
---|
212 | case 'I': |
---|
213 | if ($col != $this->sysDate) |
---|
214 | $s .= "right(digits(minute($col)),2)"; |
---|
215 | else $s .= "''"; |
---|
216 | break; |
---|
217 | case 'S': |
---|
218 | case 's': |
---|
219 | if ($col != $this->sysDate) |
---|
220 | $s .= "right(digits(second($col)),2)"; |
---|
221 | else $s .= "''"; |
---|
222 | break; |
---|
223 | default: |
---|
224 | if ($ch == '\\') { |
---|
225 | $i++; |
---|
226 | $ch = substr($fmt,$i,1); |
---|
227 | } |
---|
228 | $s .= $this->qstr($ch); |
---|
229 | } |
---|
230 | } |
---|
231 | return $s; |
---|
232 | } |
---|
233 | |
---|
234 | |
---|
235 | function &SelectLimit($sql,$nrows=-1,$offset=-1,$inputArr=false) |
---|
236 | { |
---|
237 | if ($offset <= 0) { |
---|
238 | // could also use " OPTIMIZE FOR $nrows ROWS " |
---|
239 | if ($nrows >= 0) $sql .= " FETCH FIRST $nrows ROWS ONLY "; |
---|
240 | $rs =& $this->Execute($sql,$inputArr); |
---|
241 | } else { |
---|
242 | if ($offset > 0 && $nrows < 0); |
---|
243 | else { |
---|
244 | $nrows += $offset; |
---|
245 | $sql .= " FETCH FIRST $nrows ROWS ONLY "; |
---|
246 | } |
---|
247 | $rs =& ADOConnection::SelectLimit($sql,-1,$offset,$inputArr); |
---|
248 | } |
---|
249 | |
---|
250 | return $rs; |
---|
251 | } |
---|
252 | |
---|
253 | }; |
---|
254 | |
---|
255 | |
---|
256 | class ADORecordSet_db2 extends ADORecordSet_odbc { |
---|
257 | |
---|
258 | var $databaseType = "db2"; |
---|
259 | |
---|
260 | function ADORecordSet_db2($id,$mode=false) |
---|
261 | { |
---|
262 | $this->ADORecordSet_odbc($id,$mode); |
---|
263 | } |
---|
264 | |
---|
265 | function MetaType($t,$len=-1,$fieldobj=false) |
---|
266 | { |
---|
267 | if (is_object($t)) { |
---|
268 | $fieldobj = $t; |
---|
269 | $t = $fieldobj->type; |
---|
270 | $len = $fieldobj->max_length; |
---|
271 | } |
---|
272 | |
---|
273 | switch (strtoupper($t)) { |
---|
274 | case 'VARCHAR': |
---|
275 | case 'CHAR': |
---|
276 | case 'CHARACTER': |
---|
277 | if ($len <= $this->blobSize) return 'C'; |
---|
278 | |
---|
279 | case 'LONGCHAR': |
---|
280 | case 'TEXT': |
---|
281 | case 'CLOB': |
---|
282 | case 'DBCLOB': // double-byte |
---|
283 | return 'X'; |
---|
284 | |
---|
285 | case 'BLOB': |
---|
286 | case 'GRAPHIC': |
---|
287 | case 'VARGRAPHIC': |
---|
288 | return 'B'; |
---|
289 | |
---|
290 | case 'DATE': |
---|
291 | return 'D'; |
---|
292 | |
---|
293 | case 'TIME': |
---|
294 | case 'TIMESTAMP': |
---|
295 | return 'T'; |
---|
296 | |
---|
297 | //case 'BOOLEAN': |
---|
298 | //case 'BIT': |
---|
299 | // return 'L'; |
---|
300 | |
---|
301 | //case 'COUNTER': |
---|
302 | // return 'R'; |
---|
303 | |
---|
304 | case 'INT': |
---|
305 | case 'INTEGER': |
---|
306 | case 'BIGINT': |
---|
307 | case 'SMALLINT': |
---|
308 | return 'I'; |
---|
309 | |
---|
310 | default: return 'N'; |
---|
311 | } |
---|
312 | } |
---|
313 | } |
---|
314 | |
---|
315 | } //define |
---|
316 | ?> |
---|