📄 adodb-odbc.inc.php
字号:
<?php/*V4.54 5 Nov 2004 (c) 2000-2004 John Lim (jlim#natsoft.com.my). All rights reserved. Released under both BSD license and Lesser GPL library license. Whenever there is any discrepancy between the two licenses, the BSD license will take precedence.Set tabs to 4 for best viewing. Latest version is available at http://adodb.sourceforge.net Requires ODBC. Works on Windows and Unix.*/// security - hide pathsif (!defined('ADODB_DIR')) die(); define("_ADODB_ODBC_LAYER", 2 );/*----------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/class ADODB_odbc extends ADOConnection { var $databaseType = "odbc"; var $fmtDate = "'Y-m-d'"; var $fmtTimeStamp = "'Y-m-d, h:i:sA'"; var $replaceQuote = "''"; // string to use to replace quotes var $dataProvider = "odbc"; var $hasAffectedRows = true; var $binmode = ODBC_BINMODE_RETURN; var $useFetchArray = false; // setting this to true will make array elements in FETCH_ASSOC mode case-sensitive // breaking backward-compat //var $longreadlen = 8000; // default number of chars to return for a Blob/Long field var $_bindInputArray = false; var $curmode = SQL_CUR_USE_DRIVER; // See sqlext.h, SQL_CUR_DEFAULT == SQL_CUR_USE_DRIVER == 2L var $_genSeqSQL = "create table %s (id integer)"; var $_autocommit = true; var $_haserrorfunctions = true; var $_has_stupid_odbc_fetch_api_change = true; var $_lastAffectedRows = 0; var $uCaseTables = true; // for meta* functions, uppercase table names function ADODB_odbc() { $this->_haserrorfunctions = ADODB_PHPVER >= 0x4050; $this->_has_stupid_odbc_fetch_api_change = ADODB_PHPVER >= 0x4200; } // returns true or false function _connect($argDSN, $argUsername, $argPassword, $argDatabasename) { global $php_errormsg; if (!function_exists('odbc_connect')) return null; if ($this->debug && $argDatabasename && $this->databaseType != 'vfp') { ADOConnection::outp("For odbc Connect(), $argDatabasename is not used. Place dsn in 1st parameter."); } if (isset($php_errormsg)) $php_errormsg = ''; if ($this->curmode === false) $this->_connectionID = odbc_connect($argDSN,$argUsername,$argPassword); else $this->_connectionID = odbc_connect($argDSN,$argUsername,$argPassword,$this->curmode); $this->_errorMsg = isset($php_errormsg) ? $php_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('odbc_connect')) return null; if (isset($php_errormsg)) $php_errormsg = ''; $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : ''; if ($this->debug && $argDatabasename) { ADOConnection::outp("For odbc PConnect(), $argDatabasename is not used. Place dsn in 1st parameter."); } // print "dsn=$argDSN u=$argUsername p=$argPassword<br>"; flush(); if ($this->curmode === false) $this->_connectionID = odbc_connect($argDSN,$argUsername,$argPassword); else $this->_connectionID = odbc_pconnect($argDSN,$argUsername,$argPassword,$this->curmode); $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : ''; if ($this->_connectionID && $this->autoRollback) @odbc_rollback($this->_connectionID); if (isset($this->connectStmt)) $this->Execute($this->connectStmt); return $this->_connectionID != false; } function ServerInfo() { if (!empty($this->host) && ADODB_PHPVER >= 0x4300) { $dsn = strtoupper($this->host); $first = true; $found = false; if (!function_exists('odbc_data_source')) return false; while(true) { $rez = @odbc_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; $start -= 1; return $this->Execute("insert into $seqname values($start)"); } var $_dropSeqSQL = 'drop table %s'; 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! $MAXLOOPS = 100; //$this->debug=1; while (--$MAXLOOPS>=0) { $num = $this->GetOne("select id from $seq"); if ($num === false) { $this->Execute(sprintf($this->_genSeqSQL ,$seq)); $start -= 1; $num = '0'; $ok = $this->Execute("insert into $seq values($start)"); if (!$ok) return false; } $this->Execute("update $seq set id=id+1 where id=$num"); if ($this->affected_rows() > 0) { $num += 1; $this->genID = $num; return $num; } } if ($fn = $this->raiseErrorFn) { $fn($this->databaseType,'GENID',-32000,"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num); } return false; } function ErrorMsg() { if ($this->_haserrorfunctions) { if ($this->_errorMsg !== false) return $this->_errorMsg; if (empty($this->_connectionID)) return @odbc_errormsg(); return @odbc_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 = @odbc_error(); else $e = @odbc_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 odbc_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 = odbc_commit($this->_connectionID); odbc_autocommit($this->_connectionID,true); return $ret; } function RollbackTrans() { if ($this->transOff) return true; if ($this->transCnt) $this->transCnt -= 1; $this->_autocommit = true; $ret = odbc_rollback($this->_connectionID); odbc_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 = @odbc_primarykeys($this->_connectionID,'',$schema,$table); if (!$qid) { $ADODB_FETCH_MODE = $savem; return false; } $rs = new ADORecordSet_odbc($qid); $ADODB_FETCH_MODE = $savem; if (!$rs) return false; $rs->_has_stupid_odbc_fetch_api_change = $this->_has_stupid_odbc_fetch_api_change; $arr =& $rs->GetArray(); $rs->Close(); //print_r($arr); $arr2 = array(); for ($i=0; $i < sizeof($arr); $i++) { if ($arr[$i][3]) $arr2[] = $arr[$i][3]; } return $arr2; } function &MetaTables($ttype=false) { global $ADODB_FETCH_MODE; $savem = $ADODB_FETCH_MODE; $ADODB_FETCH_MODE = ADODB_FETCH_NUM; $qid = odbc_tables($this->_connectionID); $rs = new ADORecordSet_odbc($qid); $ADODB_FETCH_MODE = $savem; if (!$rs) { $false = false; return $false; } $rs->_has_stupid_odbc_fetch_api_change = $this->_has_stupid_odbc_fetch_api_change; $arr =& $rs->GetArray(); //print_r($arr); $rs->Close(); $arr2 = array(); if ($ttype) { $isview = strncmp($ttype,'V',1) === 0; } for ($i=0; $i < sizeof($arr); $i++) { if (!$arr[$i][2]) continue; $type = $arr[$i][3]; if ($ttype) { if ($isview) { if (strncmp($type,'V',1) === 0) $arr2[] = $arr[$i][2]; } else if (strncmp($type,'SYS',3) !== 0) $arr2[] = $arr[$i][2]; } else if (strncmp($type,'SYS',3) !== 0) $arr2[] = $arr[$i][2]; } return $arr2; }/*See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odbcdatetime_data_type_changes.asp/ SQL data type codes /#define SQL_UNKNOWN_TYPE 0#define SQL_CHAR 1#define SQL_NUMERIC 2#define SQL_DECIMAL 3#define SQL_INTEGER 4#define SQL_SMALLINT 5#define SQL_FLOAT 6#define SQL_REAL 7#define SQL_DOUBLE 8#if (ODBCVER >= 0x0300)#define SQL_DATETIME 9#endif#define SQL_VARCHAR 12/ One-parameter shortcuts for date/time data types /#if (ODBCVER >= 0x0300)#define SQL_TYPE_DATE 91#define SQL_TYPE_TIME 92#define SQL_TYPE_TIMESTAMP 93#define SQL_UNICODE (-95)#define SQL_UNICODE_VARCHAR (-96)#define SQL_UNICODE_LONGVARCHAR (-97)*/ function ODBCTypes($t) { switch ((integer)$t) { case 1: case 12: case 0: case -95: case -96: return 'C'; case -97: case -1: //text return 'X'; case -4: //image return 'B'; case 9: case 91: return 'D'; case 10: case 11: case 92: case 93: return 'T'; case 4: case 5: case -6: return 'I'; case -11: // uniqidentifier return 'R'; case -7: //bit return 'L'; default: return 'N'; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -