14 if (!defined(
'ADODB_DIR'))
die();
16 define(
"_ADODB_ODBC_LAYER", 2 );
44 $this->_haserrorfunctions = ADODB_PHPVER >= 0x4050;
45 $this->_has_stupid_odbc_fetch_api_change = ADODB_PHPVER >= 0x4200;
49 function _connect($argDSN, $argUsername, $argPassword, $argDatabasename)
53 if (!function_exists(
'odbc_connect'))
return null;
55 if (!empty($argDatabasename) && stristr($argDSN,
'Database=') ===
false) {
56 $argDSN = trim($argDSN);
57 $endDSN = substr($argDSN, strlen($argDSN) - 1);
58 if ($endDSN !=
';') $argDSN .=
';';
59 $argDSN .=
'Database='.$argDatabasename;
62 if (isset($php_errormsg)) $php_errormsg =
'';
63 if ($this->curmode ===
false) $this->_connectionID = odbc_connect($argDSN,$argUsername,$argPassword);
64 else $this->_connectionID = odbc_connect($argDSN,$argUsername,$argPassword,$this->curmode);
65 $this->_errorMsg = isset($php_errormsg) ? $php_errormsg :
'';
66 if (isset($this->connectStmt)) $this->Execute($this->connectStmt);
68 return $this->_connectionID !=
false;
72 function _pconnect($argDSN, $argUsername, $argPassword, $argDatabasename)
76 if (!function_exists(
'odbc_connect'))
return null;
78 if (isset($php_errormsg)) $php_errormsg =
'';
79 $this->_errorMsg = isset($php_errormsg) ? $php_errormsg :
'';
80 if ($this->
debug && $argDatabasename) {
81 ADOConnection::outp(
"For odbc PConnect(), $argDatabasename is not used. Place dsn in 1st parameter.");
84 if ($this->curmode ===
false) $this->_connectionID = odbc_connect($argDSN,$argUsername,$argPassword);
85 else $this->_connectionID = odbc_pconnect($argDSN,$argUsername,$argPassword,$this->curmode);
87 $this->_errorMsg = isset($php_errormsg) ? $php_errormsg :
'';
88 if ($this->_connectionID && $this->autoRollback) @odbc_rollback($this->_connectionID);
89 if (isset($this->connectStmt)) $this->Execute($this->connectStmt);
91 return $this->_connectionID !=
false;
98 if (!empty($this->host) && ADODB_PHPVER >= 0x4300) {
99 $dsn = strtoupper($this->host);
103 if (!function_exists(
'odbc_data_source'))
return false;
107 $rez = @odbc_data_source($this->_connectionID,
108 $first ? SQL_FETCH_FIRST : SQL_FETCH_NEXT);
110 if (!is_array($rez))
break;
111 if (strtoupper($rez[
'server']) == $dsn) {
116 if (!$found)
return ADOConnection::ServerInfo();
117 if (!isset($rez[
'version'])) $rez[
'version'] =
'';
120 return ADOConnection::ServerInfo();
127 if (empty($this->_genSeqSQL))
return false;
128 $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname));
129 if (!$ok)
return false;
131 return $this->Execute(
"insert into $seqname values($start)");
137 if (empty($this->_dropSeqSQL))
return false;
138 return $this->Execute(sprintf($this->_dropSeqSQL,$seqname));
147 function GenID($seq=
'adodbseq',$start=1)
153 while (--$MAXLOOPS>=0) {
154 $num = $this->GetOne(
"select id from $seq");
155 if ($num ===
false) {
156 $this->Execute(sprintf($this->_genSeqSQL ,$seq));
159 $ok = $this->Execute(
"insert into $seq values($start)");
160 if (!$ok)
return false;
162 $this->Execute(
"update $seq set id=id+1 where id=$num");
164 if ($this->affected_rows() > 0) {
168 } elseif ($this->affected_rows() == 0) {
170 $value = $this->GetOne(
"select id from $seq");
171 if ($value == $num + 1) {
176 if ($fn = $this->raiseErrorFn) {
177 $fn($this->databaseType,
'GENID',-32000,
"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num);
185 if ($this->_haserrorfunctions) {
186 if ($this->_errorMsg !==
false)
return $this->_errorMsg;
187 if (empty($this->_connectionID))
return @odbc_errormsg();
188 return @odbc_errormsg($this->_connectionID);
189 }
else return ADOConnection::ErrorMsg();
195 if ($this->_haserrorfunctions) {
196 if ($this->_errorCode !==
false) {
198 return (strlen($this->_errorCode)<=2) ? 0 : $this->_errorCode;
201 if (empty($this->_connectionID)) $e = @odbc_error();
202 else $e = @odbc_error($this->_connectionID);
206 if (strlen($e)<=2)
return 0;
208 }
else return ADOConnection::ErrorNo();
215 if (!$this->hasTransactions)
return false;
216 if ($this->transOff)
return true;
217 $this->transCnt += 1;
218 $this->_autocommit =
false;
219 return odbc_autocommit($this->_connectionID,
false);
224 if ($this->transOff)
return true;
226 if ($this->transCnt) $this->transCnt -= 1;
227 $this->_autocommit =
true;
228 $ret = odbc_commit($this->_connectionID);
229 odbc_autocommit($this->_connectionID,
true);
235 if ($this->transOff)
return true;
236 if ($this->transCnt) $this->transCnt -= 1;
237 $this->_autocommit =
true;
238 $ret = odbc_rollback($this->_connectionID);
239 odbc_autocommit($this->_connectionID,
true);
245 global $ADODB_FETCH_MODE;
247 if ($this->uCaseTables) $table = strtoupper($table);
249 $this->_findschema($table,$schema);
251 $savem = $ADODB_FETCH_MODE;
252 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
253 $qid = @odbc_primarykeys($this->_connectionID,
'',$schema,$table);
256 $ADODB_FETCH_MODE = $savem;
260 $ADODB_FETCH_MODE = $savem;
262 if (!
$rs)
return false;
265 $arr =
$rs->GetArray();
269 for ($i=0; $i <
sizeof($arr); $i++) {
270 if ($arr[$i][3]) $arr2[] = $arr[$i][3];
277 function MetaTables($ttype=
false,$showSchema=
false,$mask=
false)
279 global $ADODB_FETCH_MODE;
281 $savem = $ADODB_FETCH_MODE;
282 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
283 $qid = odbc_tables($this->_connectionID);
287 $ADODB_FETCH_MODE = $savem;
294 $arr =
$rs->GetArray();
301 $isview = strncmp($ttype,
'V',1) === 0;
303 for ($i=0; $i <
sizeof($arr); $i++) {
304 if (!$arr[$i][2])
continue;
308 if (strncmp($type,
'V',1) === 0) $arr2[] = $arr[$i][2];
309 }
else if (strncmp($type,
'SYS',3) !== 0) $arr2[] = $arr[$i][2];
310 }
else if (strncmp($type,
'SYS',3) !== 0) $arr2[] = $arr[$i][2];
345 switch ((integer)$t) {
385 global $ADODB_FETCH_MODE;
388 if ($this->uCaseTables) $table = strtoupper($table);
390 $this->_findschema($table,$schema);
392 $savem = $ADODB_FETCH_MODE;
393 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
416 switch ($this->databaseType) {
419 $qid = odbc_columns($this->_connectionID);#,
'%',
'',strtoupper($table),
'%');
425 $qid = odbc_columns($this->_connectionID,
"", $schema, $table, $colname);
429 $qid = @odbc_columns($this->_connectionID,
'%',
'%',strtoupper($table),
'%');
430 if (empty($qid)) $qid = odbc_columns($this->_connectionID);
433 if (empty($qid))
return $false;
436 $ADODB_FETCH_MODE = $savem;
438 if (!
$rs)
return $false;
461 if (strtoupper(trim(
$rs->fields[2])) == $table && (!$schema || strtoupper(
$rs->fields[1]) == $schema)) {
462 $fld =
new ADOFieldObject();
463 $fld->name =
$rs->fields[3];
468 if ($fld->type ==
'C' or $fld->type ==
'X') {
469 if ($this->databaseType ==
'access')
470 $fld->max_length =
$rs->fields[6];
471 else if (
$rs->fields[4] <= -95)
472 $fld->max_length =
$rs->fields[7]/2;
474 $fld->max_length =
$rs->fields[7];
476 $fld->max_length =
$rs->fields[7];
477 $fld->not_null = !empty(
$rs->fields[10]);
478 $fld->scale =
$rs->fields[8];
479 $retarr[strtoupper($fld->name)] = $fld;
480 }
else if (
sizeof($retarr)>0)
486 if (empty($retarr)) $retarr =
false;
492 if (! $this->_bindInputArray)
return $sql;
493 $stmt = odbc_prepare($this->_connectionID,
$sql);
498 return array(
$sql,$stmt,
false);
504 GLOBAL $php_errormsg;
505 if (isset($php_errormsg)) $php_errormsg =
'';
509 if (is_array(
$sql)) {
512 $stmtid = odbc_prepare($this->_connectionID,
$sql);
514 if ($stmtid ==
false) {
515 $this->_errorMsg = isset($php_errormsg) ? $php_errormsg :
'';
520 if (! odbc_execute($stmtid,$inputarr)) {
522 if ($this->_haserrorfunctions) {
523 $this->_errorMsg = odbc_errormsg();
524 $this->_errorCode = odbc_error();
529 }
else if (is_array(
$sql)) {
531 if (!odbc_execute($stmtid)) {
533 if ($this->_haserrorfunctions) {
534 $this->_errorMsg = odbc_errormsg();
535 $this->_errorCode = odbc_error();
540 $stmtid = odbc_exec($this->_connectionID,
$sql);
542 $this->_lastAffectedRows = 0;
544 if (@odbc_num_fields($stmtid) == 0) {
545 $this->_lastAffectedRows = odbc_num_rows($stmtid);
548 $this->_lastAffectedRows = 0;
549 odbc_binmode($stmtid,$this->binmode);
550 odbc_longreadlen($stmtid,$this->maxblobsize);
553 if ($this->_haserrorfunctions) {
554 $this->_errorMsg =
'';
555 $this->_errorCode = 0;
557 $this->_errorMsg = isset($php_errormsg) ? $php_errormsg :
'';
559 if ($this->_haserrorfunctions) {
560 $this->_errorMsg = odbc_errormsg();
561 $this->_errorCode = odbc_error();
563 $this->_errorMsg = isset($php_errormsg) ? $php_errormsg :
'';
577 function UpdateBlob($table,$column,$val,$where,$blobtype=
'BLOB')
579 return $this->Execute(
"UPDATE $table SET $column=? WHERE $where",array($val)) !=
false;
585 $ret = @odbc_close($this->_connectionID);
586 $this->_connectionID =
false;
611 if ($mode ===
false) {
612 global $ADODB_FETCH_MODE;
613 $mode = $ADODB_FETCH_MODE;
615 $this->fetchMode = $mode;
617 $this->_queryID = $id;
621 $this->_currentRow = -1;
632 $o=
new ADOFieldObject();
633 $o->name = @odbc_field_name($this->_queryID,$off);
634 $o->type = @odbc_field_type($this->_queryID,$off);
635 $o->max_length = @odbc_field_len($this->_queryID,$off);
636 if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name);
637 else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name);
644 if ($this->fetchMode & ADODB_FETCH_ASSOC)
return $this->fields[$colname];
646 $this->bind = array();
647 for ($i=0; $i < $this->_numOfFields; $i++) {
648 $o = $this->FetchField($i);
649 $this->bind[strtoupper($o->name)] = $i;
653 return $this->fields[$this->bind[strtoupper($colname)]];
659 global $ADODB_COUNTRECS;
660 $this->_numOfRows = ($ADODB_COUNTRECS) ? @odbc_num_rows($this->_queryID) : -1;
661 $this->_numOfFields = @odbc_num_fields($this->_queryID);
663 if ($this->_numOfRows == 0) $this->_numOfRows = -1;
665 $this->_has_stupid_odbc_fetch_api_change = ADODB_PHPVER >= 0x4200;
677 $rs = $this->GetArray($nrows);
680 $savem = $this->fetchMode;
681 $this->fetchMode = ADODB_FETCH_NUM;
682 $this->Move($offset);
683 $this->fetchMode = $savem;
685 if ($this->fetchMode & ADODB_FETCH_ASSOC) {
686 $this->fields = $this->GetRowAssoc(ADODB_ASSOC_CASE);
691 while (!$this->EOF && $nrows != $cnt) {
692 $results[$cnt++] = $this->fields;
702 if ($this->_numOfRows != 0 && !$this->EOF) {
703 $this->_currentRow++;
704 if( $this->_fetch() ) {
708 $this->fields =
false;
715 $this->fields =
false;
716 if ($this->_has_stupid_odbc_fetch_api_change)
717 $rez = @odbc_fetch_into($this->_queryID,$this->fields);
720 $rez = @odbc_fetch_into($this->_queryID,$row,$this->fields);
723 if ($this->fetchMode & ADODB_FETCH_ASSOC) {
724 $this->fields = $this->GetRowAssoc(ADODB_ASSOC_CASE);
733 return @odbc_free_result($this->_queryID);
CreateSequence($seqname='adodbseq', $start=1)
GenID($seq='adodbseq', $start=1)
if(isset($_REQUEST['nrows'])) else $rs
ADORecordSet_odbc($id, $mode=false)
FetchField($fieldOffset=-1)
UpdateBlob($table, $column, $val, $where, $blobtype='BLOB')
MetaColumns($table, $normalize=true)
$_has_stupid_odbc_fetch_api_change
_query($sql, $inputarr=false)
_pconnect($argDSN, $argUsername, $argPassword, $argDatabasename)
debug($variable='', $name=' *variable *', $line=' *line *', $file=' *file *', $recursiveDepth=3, $debugLevel=E_DEBUG)
GetArrayLimit($nrows, $offset=-1)
MetaPrimaryKeys($table, $owner=false)
MetaTables($ttype=false, $showSchema=false, $mask=false)
$_has_stupid_odbc_fetch_api_change
_connect($argDSN, $argUsername, $argPassword, $argDatabasename)