📄 adodb-ibase.inc.php
字号:
<?php
/*
V4.04 13 Nov 2003 (c) 2000-2003 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.
Latest version is available at http://php.weblogs.com/
Interbase data driver. Requires interbase client. Works on Windows and Unix.
3 Jan 2002 -- suggestions by Hans-Peter Oeri <kampfcaspar75@oeri.ch>
changed transaction handling and added experimental blob stuff
Docs to interbase at the website
http://www.synectics.co.za/php3/tutorial/IB_PHP3_API.html
To use gen_id(), see
http://www.volny.cz/iprenosil/interbase/ip_ib_code.htm#_code_creategen
$rs = $conn->Execute('select gen_id(adodb,1) from rdb$database');
$id = $rs->fields[0];
$conn->Execute("insert into table (id, col1,...) values ($id, $val1,...)");
*/
class ADODB_ibase extends ADOConnection {
var $databaseType = "ibase";
var $dataProvider = "ibase";
var $replaceQuote = "''"; // string to use to replace quotes
var $ibase_timefmt = '%Y-%m-%d'; // For hours,mins,secs change to '%Y-%m-%d %H:%M:%S';
var $fmtDate = "'Y-m-d'";
var $fmtTimeStamp = "'Y-m-d, H:i:s'";
var $concat_operator='||';
var $_transactionID;
var $metaTablesSQL = "select rdb\$relation_name from rdb\$relations where rdb\$relation_name not like 'RDB\$%'";
//OPN STUFF start
var $metaColumnsSQL = "select a.rdb\$field_name, a.rdb\$null_flag, a.rdb\$default_source, b.rdb\$field_length, b.rdb\$field_scale, b.rdb\$field_sub_type, b.rdb\$field_precision, b.rdb\$field_type from rdb\$relation_fields a, rdb\$fields b where a.rdb\$field_source = b.rdb\$field_name and a.rdb\$relation_name = '%s' order by a.rdb\$field_position asc";
//OPN STUFF end
var $ibasetrans;
var $hasGenID = true;
var $_bindInputArray = true;
var $buffers = 0;
var $dialect = 1;
var $sysDate = "cast('TODAY' as date)";
var $sysTimeStamp = "cast('NOW' as timestamp)";
var $ansiOuter = true;
var $hasAffectedRows = false;
var $poorAffectedRows = true;
var $blobEncodeType = 'C';
function ADODB_ibase()
{
if (defined('IBASE_DEFAULT')) $this->ibasetrans = IBASE_DEFAULT;
}
function MetaPrimaryKeys($table,$owner_notused=false,$internalKey=false)
{
if ($internalKey) return array('RDB$DB_KEY');
$table = strtoupper($table);
$sql = 'SELECT S.RDB$FIELD_NAME AFIELDNAME
FROM RDB$INDICES I JOIN RDB$INDEX_SEGMENTS S ON I.RDB$INDEX_NAME=S.RDB$INDEX_NAME
WHERE I.RDB$RELATION_NAME=\''.$table.'\' and I.RDB$INDEX_NAME like \'RDB$PRIMARY%\'
ORDER BY I.RDB$INDEX_NAME,S.RDB$FIELD_POSITION';
$a = $this->GetCol($sql,false,true);
if ($a && sizeof($a)>0) return $a;
return false;
}
function ServerInfo()
{
$arr['dialect'] = $this->dialect;
switch($arr['dialect']) {
case '':
case '1': $s = 'Interbase 5.5 or earlier'; break;
case '2': $s = 'Interbase 5.6'; break;
default:
case '3': $s = 'Interbase 6.0'; break;
}
$arr['version'] = ADOConnection::_findvers($s);
$arr['description'] = $s;
return $arr;
}
function BeginTrans()
{
if ($this->transOff) return true;
$this->transCnt += 1;
$this->autoCommit = false;
$this->_transactionID = $this->_connectionID;//ibase_trans($this->ibasetrans, $this->_connectionID);
return $this->_transactionID;
}
function CommitTrans($ok=true)
{
if (!$ok) return $this->RollbackTrans();
if ($this->transOff) return true;
if ($this->transCnt) $this->transCnt -= 1;
$ret = false;
$this->autoCommit = true;
if ($this->_transactionID) {
//print ' commit ';
$ret = ibase_commit($this->_transactionID);
}
$this->_transactionID = false;
return $ret;
}
function RollbackTrans()
{
if ($this->transOff) return true;
if ($this->transCnt) $this->transCnt -= 1;
$ret = false;
$this->autoCommit = true;
if ($this->_transactionID)
$ret = ibase_rollback($this->_transactionID);
$this->_transactionID = false;
return $ret;
}
// See http://community.borland.com/article/0,1410,25844,00.html
function RowLock($tables,$where,$col)
{
if ($this->autoCommit) $this->BeginTrans();
$this->Execute("UPDATE $table SET $col=$col WHERE $where "); // is this correct - jlim?
return 1;
}
function CreateSequence($seqname,$startID=1)
{
$ok = $this->Execute(("INSERT INTO RDB\$GENERATORS (RDB\$GENERATOR_NAME) VALUES (UPPER('$seqname'))" ));
if (!$ok) return false;
return $this->Execute("SET GENERATOR $seqname TO ".($startID-1).';');
}
function DropSequence($seqname)
{
$seqname = strtoupper($seqname);
$this->Execute("delete from RDB\$GENERATORS where RDB\$GENERATOR_NAME='$seqname'");
}
function GenID($seqname='adodbseq',$startID=1)
{
$getnext = ("SELECT Gen_ID($seqname,1) FROM RDB\$DATABASE");
$rs = @$this->Execute($getnext);
if (!$rs) {
$this->Execute(("INSERT INTO RDB\$GENERATORS (RDB\$GENERATOR_NAME) VALUES (UPPER('$seqname'))" ));
$this->Execute("SET GENERATOR $seqname TO ".($startID-1).';');
$rs = $this->Execute($getnext);
}
if ($rs && !$rs->EOF) $this->genID = (integer) reset($rs->fields);
else $this->genID = 0; // false
if ($rs) $rs->Close();
return $this->genID;
}
function SelectDB($dbName)
{
return false;
}
function _handleerror()
{
$this->_errorMsg = ibase_errmsg();
}
function ErrorNo()
{
if (preg_match('/error code = ([\-0-9]*)/i', $this->_errorMsg,$arr)) return (integer) $arr[1];
else return 0;
}
function ErrorMsg()
{
return $this->_errorMsg;
}
// returns true or false
function _connect($argHostname, $argUsername, $argPassword, $argDatabasename)
{
if ($argDatabasename) $argHostname .= ':'.$argDatabasename;
$this->_connectionID = ibase_connect($argHostname,$argUsername,$argPassword,$this->charSet,$this->buffers,$this->dialect);
if ($this->dialect != 1) { // http://www.ibphoenix.com/ibp_60_del_id_ds.html
$this->replaceQuote = "''";
}
if ($this->_connectionID === false) {
$this->_handleerror();
return false;
}
ibase_timefmt($this->ibase_timefmt);
return true;
}
// returns true or false
function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
{
if ($argDatabasename) $argHostname .= ':'.$argDatabasename;
$this->_connectionID = ibase_pconnect($argHostname,$argUsername,$argPassword,$this->charSet,$this->buffers,$this->dialect);
if ($this->dialect != 1) { // http://www.ibphoenix.com/ibp_60_del_id_ds.html
$this->replaceQuote = "''";
}
if ($this->_connectionID === false) {
$this->_handleerror();
return false;
}
ibase_timefmt($this->ibase_timefmt);
return true;
}
function Prepare($sql)
{
// return $sql;
$stmt = ibase_prepare($sql);
if (!$stmt) return false;
return array($sql,$stmt);
}
// returns query ID if successful, otherwise false
// there have been reports of problems with nested queries - the code is probably not re-entrant?
function _query($sql,$iarr=false)
{
if (!$this->autoCommit && $this->_transactionID) {
$conn = $this->_transactionID;
$docommit = false;
} else {
$conn = $this->_connectionID;
$docommit = true;
}
if (is_array($sql)) {
$fn = 'ibase_execute';
$sql = $sql[1];
if (is_array($iarr)) {
if (ADODB_PHPVER >= 0x4050) { // actually 4.0.4
$fnarr =& array_merge( array($sql) , $iarr);
$ret = call_user_func_array($fn,$fnarr);
} else {
switch(sizeof($iarr)) {
case 1: $ret = $fn($sql,$iarr[0]); break;
case 2: $ret = $fn($sql,$iarr[0],$iarr[1]); break;
case 3: $ret = $fn($sql,$iarr[0],$iarr[1],$iarr[2]); break;
case 4: $ret = $fn($sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3]); break;
case 5: $ret = $fn($sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4]); break;
case 6: $ret = $fn($sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4],$iarr[5]); break;
case 7: $ret = $fn($sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4],$iarr[5],$iarr[6]); break;
default: ADOConnection::outp( "Too many parameters to ibase query $sql");
case 8: $ret = $fn($sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4],$iarr[5],$iarr[6],$iarr[7]); break;
}
}
} else $ret = $fn($sql);
} else {
$fn = 'ibase_query';
if (is_array($iarr)) {
if (ADODB_PHPVER >= 0x4050) { // actually 4.0.4
$fnarr =& array_merge( array($conn,$sql) , $iarr);
$ret = call_user_func_array($fn,$fnarr);
} else {
switch(sizeof($iarr)) {
case 1: $ret = $fn($conn,$sql,$iarr[0]); break;
case 2: $ret = $fn($conn,$sql,$iarr[0],$iarr[1]); break;
case 3: $ret = $fn($conn,$sql,$iarr[0],$iarr[1],$iarr[2]); break;
case 4: $ret = $fn($conn,$sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3]); break;
case 5: $ret = $fn($conn,$sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4]); break;
case 6: $ret = $fn($conn,$sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4],$iarr[5]); break;
case 7: $ret = $fn($conn,$sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4],$iarr[5],$iarr[6]); break;
default: ADOConnection::outp( "Too many parameters to ibase query $sql");
case 8: $ret = $fn($conn,$sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4],$iarr[5],$iarr[6],$iarr[7]); break;
}
}
} else $ret = $fn($conn,$sql);
}
if ($docommit && $ret === true) ibase_commit($this->_connectionID);
$this->_handleerror();
return $ret;
}
// returns true or false
function _close()
{
if (!$this->autoCommit) @ibase_rollback($this->_connectionID);
return @ibase_close($this->_connectionID);
}
//OPN STUFF start
function _ConvertFieldType(&$fld, $ftype, $flen, $fscale, $fsubtype, $fprecision, $isInterbase6)
{
$fscale = abs($fscale);
$fld->max_length = $flen;
$fld->scale = null;
switch($ftype){
case 7:
case 8:
if ($isInterbase6) {
switch($fsubtype){
case 0:
$fld->type = ($ftype == 7 ? 'smallint' : 'integer');
break;
case 1:
$fld->type = 'numeric';
$fld->max_length = $fprecision;
$fld->scale = $fscale;
break;
case 2:
$fld->type = 'decimal';
$fld->max_length = $fprecision;
$fld->scale = $fscale;
break;
} // switch
} else {
if ($fscale !=0) {
$fld->type = 'decimal';
$fld->scale = $fscale;
$fld->max_length = ($ftype == 7 ? 4 : 9);
} else {
$fld->type = ($ftype == 7 ? 'smallint' : 'integer');
}
}
break;
case 16:
if ($isInterbase6) {
switch($fsubtype){
case 0:
$fld->type = 'decimal';
$fld->max_length = 18;
$fld->scale = 0;
break;
case 1:
$fld->type = 'numeric';
$fld->max_length = $fprecision;
$fld->scale = $fscale;
break;
case 2:
$fld->type = 'decimal';
$fld->max_length = $fprecision;
$fld->scale = $fscale;
break;
} // switch
}
break;
case 10:
$fld->type = 'float';
break;
case 14:
$fld->type = 'char';
break;
case 27:
if ($fscale !=0) {
$fld->type = 'decimal';
$fld->max_length = 15;
$fld->scale = 5;
} else {
$fld->type = 'double';
}
break;
case 35:
if ($isInterbase6) {
$fld->type = 'timestamp';
} else {
$fld->type = 'date';
}
break;
case 12:
case 13:
$fld->type = 'date';
break;
case 37:
$fld->type = 'varchar';
break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -