27 if (!defined(
'ADODB_DIR'))
die();
40 var
$metaTablesSQL =
"select rdb\$relation_name from rdb\$relations where rdb\$relation_name not like 'RDB\$%'";
42 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";
59 if (defined(
'IBASE_DEFAULT')) $this->ibasetrans = IBASE_DEFAULT;
64 function _connect($argHostname, $argUsername, $argPassword, $argDatabasename,$persist=
false)
66 if (!function_exists(
'ibase_pconnect'))
return null;
67 if ($argDatabasename) $argHostname .=
':'.$argDatabasename;
68 $fn = ($persist) ?
'ibase_pconnect':
'ibase_connect';
70 $this->_connectionID = $fn($argHostname,$argUsername,$argPassword,
71 $this->charSet,$this->buffers,$this->dialect,$this->role);
73 $this->_connectionID = $fn($argHostname,$argUsername,$argPassword,
74 $this->charSet,$this->buffers,$this->dialect);
76 if ($this->dialect != 1) {
77 $this->replaceQuote =
"''";
79 if ($this->_connectionID ===
false) {
85 if (function_exists(
'ibase_timefmt')) {
86 ibase_timefmt($this->ibase_datefmt,IBASE_DATE );
87 if ($this->dialect == 1) ibase_timefmt($this->ibase_datefmt,IBASE_TIMESTAMP );
88 else ibase_timefmt($this->ibase_timestampfmt,IBASE_TIMESTAMP );
89 ibase_timefmt($this->ibase_timefmt,IBASE_TIME );
92 ini_set(
"ibase.timestampformat", $this->ibase_timestampfmt);
93 ini_set(
"ibase.dateformat", $this->ibase_datefmt);
94 ini_set(
"ibase.timeformat", $this->ibase_timefmt);
99 function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
101 return $this->
_connect($argHostname, $argUsername, $argPassword, $argDatabasename,
true);
107 if ($internalKey)
return array(
'RDB$DB_KEY');
109 $table = strtoupper($table);
111 $sql =
'SELECT S.RDB$FIELD_NAME AFIELDNAME 112 FROM RDB$INDICES I JOIN RDB$INDEX_SEGMENTS S ON I.RDB$INDEX_NAME=S.RDB$INDEX_NAME 113 WHERE I.RDB$RELATION_NAME=\''.$table.
'\' and I.RDB$INDEX_NAME like \
'RDB$PRIMARY%\' 114 ORDER BY I.RDB$INDEX_NAME,S.RDB$FIELD_POSITION';
116 $a = $this->GetCol(
$sql,
false,
true);
117 if ($a &&
sizeof($a)>0)
return $a;
124 switch($arr[
'dialect']) {
126 case '1': $s =
'Interbase 5.5 or earlier';
break;
127 case '2': $s =
'Interbase 5.6';
break;
129 case '3': $s =
'Interbase 6.0';
break;
131 $arr[
'version'] = ADOConnection::_findvers($s);
132 $arr[
'description'] = $s;
138 if ($this->transOff)
return true;
139 $this->transCnt += 1;
140 $this->autoCommit =
false;
141 $this->_transactionID = $this->_connectionID;
148 if ($this->transOff)
return true;
149 if ($this->transCnt) $this->transCnt -= 1;
151 $this->autoCommit =
true;
152 if ($this->_transactionID) {
154 $ret = ibase_commit($this->_transactionID);
156 $this->_transactionID =
false;
164 global $ADODB_COUNTRECS;
166 if ($this->_logsql) {
167 $savecrecs = $ADODB_COUNTRECS;
168 $ADODB_COUNTRECS =
true;
169 $ret = ADOConnection::_Execute(
$sql,$inputarr);
170 $ADODB_COUNTRECS = $savecrecs;
172 $ret = ADOConnection::_Execute(
$sql,$inputarr);
179 if ($this->transOff)
return true;
180 if ($this->transCnt) $this->transCnt -= 1;
182 $this->autoCommit =
true;
183 if ($this->_transactionID)
184 $ret = ibase_rollback($this->_transactionID);
185 $this->_transactionID =
false;
193 global $ADODB_FETCH_MODE;
195 $save = $ADODB_FETCH_MODE;
196 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
197 if ($this->fetchMode !== FALSE) {
198 $savem = $this->SetFetchMode(FALSE);
200 $table = strtoupper($table);
201 $sql =
"SELECT * FROM RDB\$INDICES WHERE RDB\$RELATION_NAME = '".$table.
"'";
203 $sql .=
" AND RDB\$INDEX_NAME NOT LIKE 'RDB\$%'";
205 $sql .=
" AND RDB\$INDEX_NAME NOT LIKE 'RDB\$FOREIGN%'";
209 if (!is_object(
$rs)) {
212 $this->SetFetchMode($savem);
214 $ADODB_FETCH_MODE = $save;
219 while ($row =
$rs->FetchRow()) {
221 if (!isset($indexes[$index])) {
222 if (is_null($row[3])) {$row[3] = 0;}
223 $indexes[$index] = array(
224 'unique' => ($row[3] == 1),
228 $sql =
"SELECT * FROM RDB\$INDEX_SEGMENTS WHERE RDB\$INDEX_NAME = '".$index.
"' ORDER BY RDB\$FIELD_POSITION ASC";
229 $rs1 = $this->Execute(
$sql);
230 while ($row1 = $rs1->FetchRow()) {
231 $indexes[$index][
'columns'][$row1[2]] = $row1[1];
236 $this->SetFetchMode($savem);
238 $ADODB_FETCH_MODE = $save;
248 $this->Execute(
"UPDATE $table SET $col=$col WHERE $where ");
255 $ok = $this->Execute((
"INSERT INTO RDB\$GENERATORS (RDB\$GENERATOR_NAME) VALUES (UPPER('$seqname'))" ));
256 if (!$ok)
return false;
257 return $this->Execute(
"SET GENERATOR $seqname TO ".($startID-1).
';');
262 $seqname = strtoupper($seqname);
263 $this->Execute(
"delete from RDB\$GENERATORS where RDB\$GENERATOR_NAME='$seqname'");
266 function GenID($seqname=
'adodbseq',$startID=1)
268 $getnext = (
"SELECT Gen_ID($seqname,1) FROM RDB\$DATABASE");
269 $rs = @$this->Execute($getnext);
271 $this->Execute((
"INSERT INTO RDB\$GENERATORS (RDB\$GENERATOR_NAME) VALUES (UPPER('$seqname'))" ));
272 $this->Execute(
"SET GENERATOR $seqname TO ".($startID-1).
';');
273 $rs = $this->Execute($getnext);
275 if (
$rs && !
$rs->EOF) $this->genID = (integer) reset(
$rs->fields);
276 else $this->genID = 0;
290 $this->_errorMsg = ibase_errmsg();
295 if (preg_match(
'/error code = ([\-0-9]*)/i', $this->_errorMsg,$arr))
return (integer) $arr[1];
301 return $this->_errorMsg;
306 $stmt = ibase_prepare($this->_connectionID,
$sql);
307 if (!$stmt)
return false;
308 return array(
$sql,$stmt);
316 if (!$this->autoCommit && $this->_transactionID) {
320 $conn = $this->_connectionID;
323 if (is_array(
$sql)) {
324 $fn =
'ibase_execute';
326 if (is_array($iarr)) {
327 if (ADODB_PHPVER >= 0x4050) {
328 if ( !isset($iarr[0]) ) $iarr[0] =
'';
329 $fnarr = array_merge( array(
$sql) , $iarr);
330 $ret = call_user_func_array($fn,$fnarr);
332 switch(
sizeof($iarr)) {
333 case 1: $ret = $fn(
$sql,$iarr[0]);
break;
334 case 2: $ret = $fn(
$sql,$iarr[0],$iarr[1]);
break;
335 case 3: $ret = $fn(
$sql,$iarr[0],$iarr[1],$iarr[2]);
break;
336 case 4: $ret = $fn(
$sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3]);
break;
337 case 5: $ret = $fn(
$sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4]);
break;
338 case 6: $ret = $fn(
$sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4],$iarr[5]);
break;
339 case 7: $ret = $fn(
$sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4],$iarr[5],$iarr[6]);
break;
340 default: ADOConnection::outp(
"Too many parameters to ibase query $sql");
341 case 8: $ret = $fn(
$sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4],$iarr[5],$iarr[6],$iarr[7]);
break;
344 }
else $ret = $fn(
$sql);
348 if (is_array($iarr)) {
349 if (ADODB_PHPVER >= 0x4050) {
350 if (
sizeof($iarr) == 0) $iarr[0] =
'';
351 $fnarr = array_merge( array(
$conn,
$sql) , $iarr);
352 $ret = call_user_func_array($fn,$fnarr);
354 switch(
sizeof($iarr)) {
355 case 1: $ret = $fn(
$conn,
$sql,$iarr[0]);
break;
356 case 2: $ret = $fn(
$conn,
$sql,$iarr[0],$iarr[1]);
break;
357 case 3: $ret = $fn(
$conn,
$sql,$iarr[0],$iarr[1],$iarr[2]);
break;
358 case 4: $ret = $fn(
$conn,
$sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3]);
break;
359 case 5: $ret = $fn(
$conn,
$sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4]);
break;
360 case 6: $ret = $fn(
$conn,
$sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4],$iarr[5]);
break;
361 case 7: $ret = $fn(
$conn,
$sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4],$iarr[5],$iarr[6]);
break;
362 default: ADOConnection::outp(
"Too many parameters to ibase query $sql");
363 case 8: $ret = $fn(
$conn,
$sql,$iarr[0],$iarr[1],$iarr[2],$iarr[3],$iarr[4],$iarr[5],$iarr[6],$iarr[7]);
break;
368 if ($docommit && $ret ===
true) ibase_commit($this->_connectionID);
377 if (!$this->autoCommit) @ibase_rollback($this->_connectionID);
378 return @ibase_close($this->_connectionID);
384 $fscale = abs($fscale);
385 $fld->max_length = $flen;
393 $fld->type = ($ftype == 7 ?
'smallint' :
'integer');
396 $fld->type =
'numeric';
397 $fld->max_length = $fprecision;
398 $fld->scale = $fscale;
401 $fld->type =
'decimal';
402 $fld->max_length = $fprecision;
403 $fld->scale = $fscale;
408 $fld->type =
'decimal';
409 $fld->scale = $fscale;
410 $fld->max_length = ($ftype == 7 ? 4 : 9);
412 $fld->type = ($ftype == 7 ?
'smallint' :
'integer');
420 $fld->type =
'decimal';
421 $fld->max_length = 18;
425 $fld->type =
'numeric';
426 $fld->max_length = $fprecision;
427 $fld->scale = $fscale;
430 $fld->type =
'decimal';
431 $fld->max_length = $fprecision;
432 $fld->scale = $fscale;
438 $fld->type =
'float';
445 $fld->type =
'decimal';
446 $fld->max_length = 15;
449 $fld->type =
'double';
454 $fld->type =
'timestamp';
466 $fld->type =
'varchar';
469 $fld->type =
'cstring';
473 $fld->max_length = -1;
481 global $ADODB_FETCH_MODE;
483 $save = $ADODB_FETCH_MODE;
484 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
486 $rs = $this->Execute(sprintf($this->metaColumnsSQL,strtoupper($table)));
488 $ADODB_FETCH_MODE = $save;
496 $dialect3 = ($this->dialect==3 ? true :
false);
499 $fld =
new ADOFieldObject();
500 $fld->name = trim(
$rs->fields[0]);
503 if (isset(
$rs->fields[1]) &&
$rs->fields[1]) {
504 $fld->not_null =
true;
506 if (isset(
$rs->fields[2])) {
508 $fld->has_default =
true;
509 $d = substr(
$rs->fields[2],strlen(
'default '));
513 case 'integer': $fld->default_value = (int) $d;
break;
517 case 'varchar': $fld->default_value = (string) substr($d,1,strlen($d)-2);
break;
519 case 'float': $fld->default_value = (float) $d;
break;
520 default: $fld->default_value = $d;
break;
524 if ((isset(
$rs->fields[5])) && ($fld->type ==
'blob')) {
525 $fld->sub_type =
$rs->fields[5];
527 $fld->sub_type = null;
530 if ($ADODB_FETCH_MODE == ADODB_FETCH_NUM) $retarr[] = $fld;
531 else $retarr[strtoupper($fld->name)] = $fld;
536 if ( empty($retarr))
return $false;
542 $blobid = ibase_blob_create( $this->_connectionID);
543 ibase_blob_add( $blobid, $blob );
544 return ibase_blob_close( $blobid );
561 $blobid = ibase_blob_open($this->_connectionID, $blob );
562 $realblob = ibase_blob_get( $blobid,$this->maxblobsize);
563 while($string = ibase_blob_get($blobid, 8192)){
564 $realblob .= $string;
566 ibase_blob_close( $blobid );
573 if (ADODB_PHPVER >= 0x5000) {
574 $blob_data = ibase_blob_info($this->_connectionID, $blob );
575 $blobid = ibase_blob_open($this->_connectionID, $blob );
578 $blob_data = ibase_blob_info( $blob );
579 $blobid = ibase_blob_open( $blob );
582 if( $blob_data[0] > $this->maxblobsize ) {
584 $realblob = ibase_blob_get($blobid, $this->maxblobsize);
586 while($string = ibase_blob_get($blobid, 8192)){
587 $realblob .= $string;
590 $realblob = ibase_blob_get($blobid, $blob_data[0]);
593 ibase_blob_close( $blobid );
599 $fd = fopen($path,
'rb');
600 if ($fd ===
false)
return false;
601 $blob_id = ibase_blob_create($this->_connectionID);
605 while ($val = fread($fd,32768)){
606 ibase_blob_add($blob_id, $val);
610 $blob_id_str = ibase_blob_close($blob_id);
613 return $this->Execute(
"UPDATE $table SET $column=(?) WHERE $where",array($blob_id_str)) !=
false;
625 function UpdateBlob($table,$column,$val,$where,$blobtype=
'BLOB')
627 $blob_id = ibase_blob_create($this->_connectionID);
636 $tail_size = $len % $chunk_size;
637 $n_chunks = ($len - $tail_size) / $chunk_size;
639 for ($n = 0; $n < $n_chunks; $n++) {
640 $start = $n * $chunk_size;
641 $data = substr($val, $start, $chunk_size);
642 ibase_blob_add($blob_id, $data);
646 $start = $n_chunks * $chunk_size;
647 $data = substr($val, $start, $tail_size);
648 ibase_blob_add($blob_id, $data);
652 $blob_id_str = ibase_blob_close($blob_id);
654 return $this->Execute(
"UPDATE $table SET $column=(?) WHERE $where",array($blob_id_str)) !=
false;
661 $blob_id = ibase_blob_create($this->_connectionID);
662 ibase_blob_add($blob_id, $val);
663 $blob_id_str = ibase_blob_close($blob_id);
664 return $this->Execute(
"UPDATE $table SET $column=(?) WHERE $where",array($blob_id_str)) !=
false;
676 for ($i=0; $i < $len; $i++) {
682 $s .=
"extract(year from $col)";
686 $s .=
"extract(month from $col)";
690 $s .=
"cast(((extract(month from $col)+2) / 3) as integer)";
694 $s .=
"(extract(day from $col))";
698 $s .=
"(extract(hour from $col))";
702 $s .=
"(extract(minute from $col))";
706 $s .=
"CAST((extract(second from $col)) AS INTEGER)";
712 $ch = substr($fmt,$i,1);
714 $s .= $this->qstr($ch);
735 global $ADODB_FETCH_MODE;
737 $this->fetchMode = ($mode ===
false) ? $ADODB_FETCH_MODE : $mode;
738 $this->ADORecordSet($id);
748 $fld =
new ADOFieldObject;
749 $ibf = ibase_field_info($this->_queryID,$fieldOffset);
750 switch (ADODB_ASSOC_CASE) {
752 $fld->name = ($ibf[
'alias']);
753 if (empty($fld->name)) $fld->name = ($ibf[
'name']);
756 $fld->name = strtoupper($ibf[
'alias']);
757 if (empty($fld->name)) $fld->name = strtoupper($ibf[
'name']);
760 $fld->name = strtolower($ibf[
'alias']);
761 if (empty($fld->name)) $fld->name = strtolower($ibf[
'name']);
765 $fld->type = $ibf[
'type'];
766 $fld->max_length = $ibf[
'length'];
769 $fld->not_null =
false;
770 $fld->has_default =
false;
771 $fld->default_value =
'null';
777 $this->_numOfRows = -1;
778 $this->_numOfFields = @ibase_num_fields($this->_queryID);
781 for ($i=0, $max = $this->_numOfFields; $i < $max; $i++) {
782 $f1 = $this->FetchField($i);
783 $this->_cacheType[] = $f1->type;
794 $f = @ibase_fetch_row($this->_queryID);
796 $this->fields =
false;
802 global $ADODB_ANSI_PADDING_OFF;
804 $rtrim = !empty($ADODB_ANSI_PADDING_OFF);
806 for ($i=0, $max = $this->_numOfFields; $i < $max; $i++) {
807 if ($this->_cacheType[$i]==
"BLOB") {
809 $f[$i] = $this->connection->_BlobDecode($f[$i]);
814 if (!isset($f[$i])) {
816 }
else if ($rtrim && is_string($f[$i])) {
817 $f[$i] = rtrim($f[$i]);
824 if ($this->fetchMode == ADODB_FETCH_ASSOC) {
825 $this->fields = $this->GetRowAssoc(ADODB_ASSOC_CASE);
826 }
else if ($this->fetchMode == ADODB_FETCH_BOTH) {
827 $this->fields = array_merge($this->fields,$this->GetRowAssoc(ADODB_ASSOC_CASE));
835 if ($this->fetchMode & ADODB_FETCH_ASSOC)
return $this->fields[$colname];
837 $this->bind = array();
838 for ($i=0; $i < $this->_numOfFields; $i++) {
839 $o = $this->FetchField($i);
840 $this->bind[strtoupper($o->name)] = $i;
844 return $this->fields[$this->bind[strtoupper($colname)]];
851 return @ibase_free_result($this->_queryID);
858 $t = $fieldobj->type;
859 $len = $fieldobj->max_length;
861 switch (strtoupper($t)) {
868 if ($len <= $this->blobSize)
return 'C';
874 case 'DATE':
return 'D';
875 case 'TIME':
return 'T';
881 case 'INTEGER':
return 'I';
MetaType($t, $len=-1, $fieldobj=false)
_pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
MetaColumns($table, $normalize=true)
if(isset($_REQUEST['nrows'])) else $rs
ADORecordset_ibase($id, $mode=false)
_connect($argHostname, $argUsername, $argPassword, $argDatabasename, $persist=false)
OldUpdateBlob($table, $column, $val, $where, $blobtype='BLOB')
FetchField($fieldOffset=-1)
GenID($seqname='adodbseq', $startID=1)
_query($sql, $iarr=false)
_Execute($sql, $inputarr=false)
MetaPrimaryKeys($table, $owner_notused=false, $internalKey=false)
SQLDate($fmt, $col=false)
CreateSequence($seqname, $startID=1)
UpdateBlobFile($table, $column, $path, $where, $blobtype='BLOB')
RowLock($tables, $where, $col=false)
UpdateBlob($table, $column, $val, $where, $blobtype='BLOB')
_ConvertFieldType(&$fld, $ftype, $flen, $fscale, $fsubtype, $fprecision, $dialect3)
MetaIndexes($table, $primary=FALSE, $owner=false)