00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090 if (!defined('ADODB_DIR')) die();
00091
00092 if (!defined('_ADODB_ODBC_LAYER')) {
00093 include(ADODB_DIR."/drivers/adodb-odbc.inc.php");
00094 }
00095 if (!defined('ADODB_ODBC_DB2')){
00096 define('ADODB_ODBC_DB2',1);
00097
00098 class ADODB_ODBC_DB2 extends ADODB_odbc {
00099 var $databaseType = "db2";
00100 var $concat_operator = '||';
00101 var $sysTime = 'CURRENT TIME';
00102 var $sysDate = 'CURRENT DATE';
00103 var $sysTimeStamp = 'CURRENT TIMESTAMP';
00104
00105
00106 var $fmtTimeStamp = "'Y-m-d-H.i.s'";
00107 var $ansiOuter = true;
00108 var $identitySQL = 'values IDENTITY_VAL_LOCAL()';
00109 var $_bindInputArray = true;
00110 var $hasInsertID = true;
00111 var $rsPrefix = 'ADORecordset_odbc_';
00112
00113 function ADODB_DB2()
00114 {
00115 if (strncmp(PHP_OS,'WIN',3) === 0) $this->curmode = SQL_CUR_USE_ODBC;
00116 $this->ADODB_odbc();
00117 }
00118
00119 function IfNull( $field, $ifNull )
00120 {
00121 return " COALESCE($field, $ifNull) ";
00122 }
00123
00124 function ServerInfo()
00125 {
00126
00127 $vers = $this->GetOne('select versionnumber from sysibm.sysversions');
00128
00129 return array('description'=>'DB2 ODBC driver', 'version'=>$vers);
00130 }
00131
00132 function _insertid()
00133 {
00134 return $this->GetOne($this->identitySQL);
00135 }
00136
00137 function RowLock($tables,$where,$flds='1 as ignore')
00138 {
00139 if ($this->_autocommit) $this->BeginTrans();
00140 return $this->GetOne("select $flds from $tables where $where for update");
00141 }
00142
00143 function &MetaTables($ttype=false,$showSchema=false, $qtable="%", $qschema="%")
00144 {
00145 global $ADODB_FETCH_MODE;
00146
00147 $savem = $ADODB_FETCH_MODE;
00148 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
00149 $qid = odbc_tables($this->_connectionID, "", $qschema, $qtable, "");
00150
00151 $rs = new ADORecordSet_odbc($qid);
00152
00153 $ADODB_FETCH_MODE = $savem;
00154 if (!$rs) {
00155 $false = false;
00156 return $false;
00157 }
00158 $rs->_has_stupid_odbc_fetch_api_change = $this->_has_stupid_odbc_fetch_api_change;
00159
00160 $arr =& $rs->GetArray();
00161
00162
00163 $rs->Close();
00164 $arr2 = array();
00165
00166 if ($ttype) {
00167 $isview = strncmp($ttype,'V',1) === 0;
00168 }
00169 for ($i=0; $i < sizeof($arr); $i++) {
00170
00171 if (!$arr[$i][2]) continue;
00172 if (strncmp($arr[$i][1],'SYS',3) === 0) continue;
00173
00174 $type = $arr[$i][3];
00175
00176 if ($showSchema) $arr[$i][2] = $arr[$i][1].'.'.$arr[$i][2];
00177
00178 if ($ttype) {
00179 if ($isview) {
00180 if (strncmp($type,'V',1) === 0) $arr2[] = $arr[$i][2];
00181 } else if (strncmp($type,'T',1) === 0) $arr2[] = $arr[$i][2];
00182 } else if (strncmp($type,'S',1) !== 0) $arr2[] = $arr[$i][2];
00183 }
00184 return $arr2;
00185 }
00186
00187 function &MetaIndexes ($table, $primary = FALSE, $owner=false)
00188 {
00189
00190 global $ADODB_FETCH_MODE;
00191 $save = $ADODB_FETCH_MODE;
00192 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
00193 if ($this->fetchMode !== FALSE) {
00194 $savem = $this->SetFetchMode(FALSE);
00195 }
00196 $false = false;
00197
00198 $table = strtoupper($table);
00199 $SQL="SELECT NAME, UNIQUERULE, COLNAMES FROM SYSIBM.SYSINDEXES WHERE TBNAME='$table'";
00200 if ($primary)
00201 $SQL.= " AND UNIQUERULE='P'";
00202 $rs = $this->Execute($SQL);
00203 if (!is_object($rs)) {
00204 if (isset($savem))
00205 $this->SetFetchMode($savem);
00206 $ADODB_FETCH_MODE = $save;
00207 return $false;
00208 }
00209 $indexes = array ();
00210
00211 while ($row = $rs->FetchRow()) {
00212 $indexes[$row[0]] = array(
00213 'unique' => ($row[1] == 'U' || $row[1] == 'P'),
00214 'columns' => array()
00215 );
00216 $cols = ltrim($row[2],'+');
00217 $indexes[$row[0]]['columns'] = explode('+', $cols);
00218 }
00219 if (isset($savem)) {
00220 $this->SetFetchMode($savem);
00221 $ADODB_FETCH_MODE = $save;
00222 }
00223 return $indexes;
00224 }
00225
00226
00227 function SQLDate($fmt, $col=false)
00228 {
00229
00230 if (!$col) $col = $this->sysDate;
00231 $s = '';
00232
00233 $len = strlen($fmt);
00234 for ($i=0; $i < $len; $i++) {
00235 if ($s) $s .= '||';
00236 $ch = $fmt[$i];
00237 switch($ch) {
00238 case 'Y':
00239 case 'y':
00240 $s .= "char(year($col))";
00241 break;
00242 case 'M':
00243 $s .= "substr(monthname($col),1,3)";
00244 break;
00245 case 'm':
00246 $s .= "right(digits(month($col)),2)";
00247 break;
00248 case 'D':
00249 case 'd':
00250 $s .= "right(digits(day($col)),2)";
00251 break;
00252 case 'H':
00253 case 'h':
00254 if ($col != $this->sysDate) $s .= "right(digits(hour($col)),2)";
00255 else $s .= "''";
00256 break;
00257 case 'i':
00258 case 'I':
00259 if ($col != $this->sysDate)
00260 $s .= "right(digits(minute($col)),2)";
00261 else $s .= "''";
00262 break;
00263 case 'S':
00264 case 's':
00265 if ($col != $this->sysDate)
00266 $s .= "right(digits(second($col)),2)";
00267 else $s .= "''";
00268 break;
00269 default:
00270 if ($ch == '\\') {
00271 $i++;
00272 $ch = substr($fmt,$i,1);
00273 }
00274 $s .= $this->qstr($ch);
00275 }
00276 }
00277 return $s;
00278 }
00279
00280
00281 function &SelectLimit($sql,$nrows=-1,$offset=-1,$inputArr=false)
00282 {
00283 $nrows = (integer) $nrows;
00284 if ($offset <= 0) {
00285
00286 if ($nrows >= 0) $sql .= " FETCH FIRST $nrows ROWS ONLY ";
00287 $rs =& $this->Execute($sql,$inputArr);
00288 } else {
00289 if ($offset > 0 && $nrows < 0);
00290 else {
00291 $nrows += $offset;
00292 $sql .= " FETCH FIRST $nrows ROWS ONLY ";
00293 }
00294 $rs =& ADOConnection::SelectLimit($sql,-1,$offset,$inputArr);
00295 }
00296
00297 return $rs;
00298 }
00299
00300 };
00301
00302
00303 class ADORecordSet_odbc_db2 extends ADORecordSet_odbc {
00304
00305 var $databaseType = "db2";
00306
00307 function ADORecordSet_db2($id,$mode=false)
00308 {
00309 $this->ADORecordSet_odbc($id,$mode);
00310 }
00311
00312 function MetaType($t,$len=-1,$fieldobj=false)
00313 {
00314 if (is_object($t)) {
00315 $fieldobj = $t;
00316 $t = $fieldobj->type;
00317 $len = $fieldobj->max_length;
00318 }
00319
00320 switch (strtoupper($t)) {
00321 case 'VARCHAR':
00322 case 'CHAR':
00323 case 'CHARACTER':
00324 case 'C':
00325 if ($len <= $this->blobSize) return 'C';
00326
00327 case 'LONGCHAR':
00328 case 'TEXT':
00329 case 'CLOB':
00330 case 'DBCLOB':
00331 case 'X':
00332 return 'X';
00333
00334 case 'BLOB':
00335 case 'GRAPHIC':
00336 case 'VARGRAPHIC':
00337 return 'B';
00338
00339 case 'DATE':
00340 case 'D':
00341 return 'D';
00342
00343 case 'TIME':
00344 case 'TIMESTAMP':
00345 case 'T':
00346 return 'T';
00347
00348
00349
00350
00351
00352
00353
00354
00355 case 'INT':
00356 case 'INTEGER':
00357 case 'BIGINT':
00358 case 'SMALLINT':
00359 case 'I':
00360 return 'I';
00361
00362 default: return 'N';
00363 }
00364 }
00365 }
00366
00367 }
00368 ?>