📄 adodb-db2.inc.php
字号:
<?php/* V4.98 13 Feb 2008 (c) 2006 John Lim (jlim#natsoft.com.my). All rights reserved. This is a version of the ADODB driver for DB2. It uses the 'ibm_db2' PECL extension for PHP (http://pecl.php.net/package/ibm_db2), which in turn requires DB2 V8.2.2 or higher. Originally tested with PHP 5.1.1 and Apache 2.0.55 on Windows XP SP2. More recently tested with PHP 5.1.2 and Apache 2.0.55 on Windows XP SP2. This file was ported from "adodb-odbc.inc.php" by Larry Menard, "larry.menard#rogers.com". I ripped out what I believed to be a lot of redundant or obsolete code, but there are probably still some remnants of the ODBC support in this file; I'm relying on reviewers of this code to point out any other things that can be removed.*/// security - hide pathsif (!defined('ADODB_DIR')) die(); define("_ADODB_DB2_LAYER", 2 ); /*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/class ADODB_db2 extends ADOConnection { var $databaseType = "db2"; var $fmtDate = "'Y-m-d'"; var $concat_operator = '||'; var $sysTime = 'CURRENT TIME'; var $sysDate = 'CURRENT DATE'; var $sysTimeStamp = 'CURRENT TIMESTAMP'; var $fmtTimeStamp = "'Y-m-d-H:i:s'"; var $replaceQuote = "''"; // string to use to replace quotes var $dataProvider = "db2"; var $hasAffectedRows = true; var $binmode = DB2_BINARY; var $useFetchArray = false; // setting this to true will make array elements in FETCH_ASSOC mode case-sensitive // breaking backward-compat var $_bindInputArray = false; var $_genIDSQL = "VALUES NEXTVAL FOR %s"; var $_genSeqSQL = "CREATE SEQUENCE %s START WITH 1 NO MAXVALUE NO CYCLE"; var $_dropSeqSQL = "DROP SEQUENCE %s"; var $_autocommit = true; var $_haserrorfunctions = true; var $_lastAffectedRows = 0; var $uCaseTables = true; // for meta* functions, uppercase table names var $hasInsertID = true; function _insertid() { return ADOConnection::GetOne('VALUES IDENTITY_VAL_LOCAL()'); } function ADODB_db2() { $this->_haserrorfunctions = ADODB_PHPVER >= 0x4050; } // returns true or false function _connect($argDSN, $argUsername, $argPassword, $argDatabasename) { global $php_errormsg; if (!function_exists('db2_connect')) { ADOConnection::outp("Warning: The old ODBC based DB2 driver has been renamed 'odbc_db2'. This ADOdb driver calls PHP's native db2 extension."); return null; } // This needs to be set before the connect(). // Replaces the odbc_binmode() call that was in Execute() ini_set('ibm_db2.binmode', $this->binmode); if ($argDatabasename) { $this->_connectionID = db2_connect($argDatabasename,$argUsername,$argPassword); } else { $this->_connectionID = db2_connect($argDSN,$argUsername,$argPassword); } if (isset($php_errormsg)) $php_errormsg = ''; // For db2_connect(), there is an optional 4th arg. If present, it must be // an array of valid options. So far, we don't use them. $this->_errorMsg = @db2_conn_errormsg(); if (isset($this->connectStmt)) $this->Execute($this->connectStmt); return $this->_connectionID != false; } // returns true or false function _pconnect($argDSN, $argUsername, $argPassword, $argDatabasename) { global $php_errormsg; if (!function_exists('db2_connect')) return null; // This needs to be set before the connect(). // Replaces the odbc_binmode() call that was in Execute() ini_set('ibm_db2.binmode', $this->binmode); if (isset($php_errormsg)) $php_errormsg = ''; $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : ''; if ($argDatabasename) { $this->_connectionID = db2_pconnect($argDatabasename,$argUsername,$argPassword); } else { $this->_connectionID = db2_pconnect($argDSN,$argUsername,$argPassword); } if (isset($php_errormsg)) $php_errormsg = ''; $this->_errorMsg = @db2_conn_errormsg(); if ($this->_connectionID && $this->autoRollback) @db2_rollback($this->_connectionID); if (isset($this->connectStmt)) $this->Execute($this->connectStmt); return $this->_connectionID != false; } // format and return date string in database timestamp format function DBTimeStamp($ts) { if (empty($ts) && $ts !== 0) return 'null'; if (is_string($ts)) $ts = ADORecordSet::UnixTimeStamp($ts); return 'TO_DATE('.adodb_date($this->fmtTimeStamp,$ts).",'YYYY-MM-DD HH24:MI:SS')"; } // Format date column in sql string given an input format that understands Y M D function SQLDate($fmt, $col=false) { // use right() and replace() ? if (!$col) $col = $this->sysDate; /* use TO_CHAR() if $fmt is TO_CHAR() allowed fmt */ if ($fmt== 'Y-m-d H:i:s') return 'TO_CHAR('.$col.", 'YYYY-MM-DD HH24:MI:SS')"; $s = ''; $len = strlen($fmt); for ($i=0; $i < $len; $i++) { if ($s) $s .= $this->concat_operator; $ch = $fmt[$i]; switch($ch) { case 'Y': case 'y': if ($len==1) return "year($col)"; $s .= "char(year($col))"; break; case 'M': if ($len==1) return "monthname($col)"; $s .= "substr(monthname($col),1,3)"; break; case 'm': if ($len==1) return "month($col)"; $s .= "right(digits(month($col)),2)"; break; case 'D': case 'd': if ($len==1) return "day($col)"; $s .= "right(digits(day($col)),2)"; break; case 'H': case 'h': if ($len==1) return "hour($col)"; if ($col != $this->sysDate) $s .= "right(digits(hour($col)),2)"; else $s .= "''"; break; case 'i': case 'I': if ($len==1) return "minute($col)"; if ($col != $this->sysDate) $s .= "right(digits(minute($col)),2)"; else $s .= "''"; break; case 'S': case 's': if ($len==1) return "second($col)"; if ($col != $this->sysDate) $s .= "right(digits(second($col)),2)"; else $s .= "''"; break; default: if ($ch == '\\') { $i++; $ch = substr($fmt,$i,1); } $s .= $this->qstr($ch); } } return $s; } function ServerInfo() { if (!empty($this->host) && ADODB_PHPVER >= 0x4300) { $dsn = strtoupper($this->host); $first = true; $found = false; if (!function_exists('db2_data_source')) return false; while(true) { $rez = @db2_data_source($this->_connectionID, $first ? SQL_FETCH_FIRST : SQL_FETCH_NEXT); $first = false; if (!is_array($rez)) break; if (strtoupper($rez['server']) == $dsn) { $found = true; break; } } if (!$found) return ADOConnection::ServerInfo(); if (!isset($rez['version'])) $rez['version'] = ''; return $rez; } else { return ADOConnection::ServerInfo(); } } function CreateSequence($seqname='adodbseq',$start=1) { if (empty($this->_genSeqSQL)) return false; $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname)); if (!$ok) return false; return true; } function DropSequence($seqname) { if (empty($this->_dropSeqSQL)) return false; return $this->Execute(sprintf($this->_dropSeqSQL,$seqname)); } /* This algorithm is not very efficient, but works even if table locking is not available. Will return false if unable to generate an ID after $MAXLOOPS attempts. */ function GenID($seq='adodbseq',$start=1) { // if you have to modify the parameter below, your database is overloaded, // or you need to implement generation of id's yourself! $num = $this->GetOne("VALUES NEXTVAL FOR $seq"); return $num; } function ErrorMsg() { if ($this->_haserrorfunctions) { if ($this->_errorMsg !== false) return $this->_errorMsg; if (empty($this->_connectionID)) return @db2_conn_errormsg(); return @db2_conn_errormsg($this->_connectionID); } else return ADOConnection::ErrorMsg(); } function ErrorNo() { if ($this->_haserrorfunctions) { if ($this->_errorCode !== false) { // bug in 4.0.6, error number can be corrupted string (should be 6 digits) return (strlen($this->_errorCode)<=2) ? 0 : $this->_errorCode; } if (empty($this->_connectionID)) $e = @db2_conn_error(); else $e = @db2_conn_error($this->_connectionID); // bug in 4.0.6, error number can be corrupted string (should be 6 digits) // so we check and patch if (strlen($e)<=2) return 0; return $e; } else return ADOConnection::ErrorNo(); } function BeginTrans() { if (!$this->hasTransactions) return false; if ($this->transOff) return true; $this->transCnt += 1; $this->_autocommit = false; return db2_autocommit($this->_connectionID,false); } function CommitTrans($ok=true) { if ($this->transOff) return true; if (!$ok) return $this->RollbackTrans(); if ($this->transCnt) $this->transCnt -= 1; $this->_autocommit = true; $ret = db2_commit($this->_connectionID); db2_autocommit($this->_connectionID,true); return $ret; } function RollbackTrans() { if ($this->transOff) return true; if ($this->transCnt) $this->transCnt -= 1; $this->_autocommit = true; $ret = db2_rollback($this->_connectionID); db2_autocommit($this->_connectionID,true); return $ret; } function MetaPrimaryKeys($table) { global $ADODB_FETCH_MODE; if ($this->uCaseTables) $table = strtoupper($table); $schema = ''; $this->_findschema($table,$schema); $savem = $ADODB_FETCH_MODE; $ADODB_FETCH_MODE = ADODB_FETCH_NUM; $qid = @db2_primarykeys($this->_connectionID,'',$schema,$table); if (!$qid) { $ADODB_FETCH_MODE = $savem; return false; } $rs = new ADORecordSet_db2($qid); $ADODB_FETCH_MODE = $savem; if (!$rs) return false; $arr =& $rs->GetArray(); $rs->Close(); $arr2 = array(); for ($i=0; $i < sizeof($arr); $i++) { if ($arr[$i][3]) $arr2[] = $arr[$i][3]; } return $arr2; } function MetaForeignKeys($table, $owner = FALSE, $upper = FALSE, $asociative = FALSE ) { global $ADODB_FETCH_MODE; if ($this->uCaseTables) $table = strtoupper($table); $schema = ''; $this->_findschema($table,$schema); $savem = $ADODB_FETCH_MODE; $ADODB_FETCH_MODE = ADODB_FETCH_NUM; $qid = @db2_foreign_keys($this->_connectionID,'',$schema,$table); if (!$qid) { $ADODB_FETCH_MODE = $savem; return false; } $rs = new ADORecordSet_db2($qid); $ADODB_FETCH_MODE = $savem; /* $rs->fields indices 0 PKTABLE_CAT 1 PKTABLE_SCHEM 2 PKTABLE_NAME 3 PKCOLUMN_NAME 4 FKTABLE_CAT 5 FKTABLE_SCHEM 6 FKTABLE_NAME 7 FKCOLUMN_NAME */ if (!$rs) return false; $foreign_keys = array(); while (!$rs->EOF) { if (strtoupper(trim($rs->fields[2])) == $table && (!$schema || strtoupper($rs->fields[1]) == $schema)) { if (!is_array($foreign_keys[$rs->fields[5].'.'.$rs->fields[6]])) $foreign_keys[$rs->fields[5].'.'.$rs->fields[6]] = array(); $foreign_keys[$rs->fields[5].'.'.$rs->fields[6]][$rs->fields[7]] = $rs->fields[3]; } $rs->MoveNext(); } $rs->Close(); return $foreign_key; } function &MetaTables($ttype=false,$schema=false) { global $ADODB_FETCH_MODE; $savem = $ADODB_FETCH_MODE; $ADODB_FETCH_MODE = ADODB_FETCH_NUM; $qid = db2_tables($this->_connectionID); $rs = new ADORecordSet_db2($qid); $ADODB_FETCH_MODE = $savem; if (!$rs) { $false = false; return $false; } $arr =& $rs->GetArray(); $rs->Close(); $arr2 = array(); if ($ttype) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -