13 if (!defined(
'ADODB_DIR'))
die();
15 define(
"_ADODB_ODBTP_LAYER", 2 );
29 var
$_genSeqSQL =
"create table %s (seq_name char(30) not null unique , seq_value integer not null)";
42 return array(
'description' => @odbtp_get_attr( ODB_ATTR_DBMSNAME, $this->_connectionID),
43 'version' => @odbtp_get_attr( ODB_ATTR_DBMSVER, $this->_connectionID));
48 if ($this->_errorMsg !==
false)
return $this->_errorMsg;
49 if (empty($this->_connectionID))
return @odbtp_last_error();
50 return @odbtp_last_error($this->_connectionID);
55 if ($this->_errorCode !==
false)
return $this->_errorCode;
56 if (empty($this->_connectionID))
return @odbtp_last_error_state();
57 return @odbtp_last_error_state($this->_connectionID);
88 return $this->GetOne($this->identitySQL);
93 if ($this->_queryID) {
94 return @odbtp_affected_rows ($this->_queryID);
102 $num = $this->GetOne(
"select seq_value from adodb_seq");
104 if( $this->odbc_driver == ODB_DRIVER_FOXPRO ) {
105 $path = @odbtp_get_attr( ODB_ATTR_DATABASENAME, $this->_connectionID );
107 if( !strcasecmp(strrchr($path,
'.'),
'.dbc') )
108 $path = substr($path,0,strrpos($path,
'\/'));
109 $seqtab = $path .
'/' . $seqtab;
112 if (empty($this->_genSeqSQL))
return false;
113 $ok = $this->Execute(sprintf($this->_genSeqSQL ,$seqtab));
115 $num = $this->GetOne(
"select seq_value from adodb_seq where seq_name='$seqname'");
120 return $this->Execute(
"insert into adodb_seq values('$seqname',$start)");
125 if (empty($this->_dropSeqSQL))
return false;
126 return $this->Execute(sprintf($this->_dropSeqSQL,$seqname));
129 function GenID($seq=
'adodbseq',$start=1)
132 if( $this->odbc_driver == ODB_DRIVER_FOXPRO) {
133 $path = @odbtp_get_attr( ODB_ATTR_DATABASENAME, $this->_connectionID );
135 if( !strcasecmp(strrchr($path,
'.'),
'.dbc') )
136 $path = substr($path,0,strrpos($path,
'\/'));
137 $seqtab = $path .
'/' . $seqtab;
140 while (--$MAXLOOPS>=0) {
141 $num = $this->GetOne(
"select seq_value from adodb_seq where seq_name='$seq'");
142 if ($num ===
false) {
144 $ok = $this->GetOne(
"select seq_value from adodb_seq ");
147 $this->Execute(sprintf($this->_genSeqSQL ,$seqtab));
151 $ok = $this->Execute(
"insert into adodb_seq values('$seq',$start)");
152 if (!$ok)
return false;
154 $ok = $this->Execute(
"update adodb_seq set seq_value=seq_value+1 where seq_name='$seq'");
161 if ($fn = $this->raiseErrorFn) {
162 $fn($this->databaseType,
'GENID',-32000,
"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num);
173 function _connect($HostOrInterface, $UserOrDSN=
'', $argPassword=
'', $argDatabase=
'')
175 if ($argPassword && stripos($UserOrDSN,
'DRIVER=') !==
false) {
176 $this->_connectionID = odbtp_connect($HostOrInterface,$UserOrDSN.
';PWD='.$argPassword);
178 $this->_connectionID = odbtp_connect($HostOrInterface,$UserOrDSN,$argPassword,$argDatabase);
179 if ($this->_connectionID ===
false) {
180 $this->_errorMsg = $this->
ErrorMsg() ;
184 odbtp_convert_datetime($this->_connectionID,
true);
186 if ($this->_dontPoolDBC) {
187 if (function_exists(
'odbtp_dont_pool_dbc'))
188 @odbtp_dont_pool_dbc($this->_connectionID);
191 $this->_dontPoolDBC =
true;
193 $this->odbc_driver = @odbtp_get_attr(ODB_ATTR_DRIVER, $this->_connectionID);
194 $dbms = strtolower(@odbtp_get_attr(ODB_ATTR_DBMSNAME, $this->_connectionID));
195 $this->odbc_name = $dbms;
198 if( $this->odbc_driver == ODB_DRIVER_ORACLE )
200 else if( $this->odbc_driver == ODB_DRIVER_SYBASE )
205 case 'microsoft sql server':
206 $this->databaseType =
'odbtp_mssql';
207 $this->fmtDate =
"'Y-m-d'";
208 $this->fmtTimeStamp =
"'Y-m-d h:i:sA'";
209 $this->sysDate =
'convert(datetime,convert(char,GetDate(),102),102)';
210 $this->sysTimeStamp =
'GetDate()';
211 $this->ansiOuter =
true;
212 $this->leftOuter =
'*=';
213 $this->rightOuter =
'=*';
214 $this->hasTop =
'top';
215 $this->hasInsertID =
true;
216 $this->hasTransactions =
true;
217 $this->_bindInputArray =
true;
218 $this->_canSelectDb =
true;
219 $this->substr =
"substring";
220 $this->length =
'len';
221 $this->identitySQL =
'select SCOPE_IDENTITY()';
222 $this->metaDatabasesSQL =
"select name from master..sysdatabases where name <> 'master'";
223 $this->_canPrepareSP =
true;
226 $this->databaseType =
'odbtp_access';
227 $this->fmtDate =
"#Y-m-d#";
228 $this->fmtTimeStamp =
"#Y-m-d h:i:sA#";
229 $this->sysDate =
"FORMAT(NOW,'yyyy-mm-dd')";
230 $this->sysTimeStamp =
'NOW';
231 $this->hasTop =
'top';
232 $this->hasTransactions =
false;
233 $this->_canPrepareSP =
true;
235 case 'visual foxpro':
236 $this->databaseType =
'odbtp_vfp';
237 $this->fmtDate =
"{^Y-m-d}";
238 $this->fmtTimeStamp =
"{^Y-m-d, h:i:sA}";
239 $this->sysDate =
'date()';
240 $this->sysTimeStamp =
'datetime()';
241 $this->ansiOuter =
true;
242 $this->hasTop =
'top';
243 $this->hasTransactions =
false;
244 $this->replaceQuote =
"'+chr(39)+'";
246 $this->
false =
'.F.';
250 $this->databaseType =
'odbtp_oci8';
251 $this->fmtDate =
"'Y-m-d 00:00:00'";
252 $this->fmtTimeStamp =
"'Y-m-d h:i:sA'";
253 $this->sysDate =
'TRUNC(SYSDATE)';
254 $this->sysTimeStamp =
'SYSDATE';
255 $this->hasTransactions =
true;
256 $this->_bindInputArray =
true;
257 $this->concat_operator =
'||';
260 $this->databaseType =
'odbtp_sybase';
261 $this->fmtDate =
"'Y-m-d'";
262 $this->fmtTimeStamp =
"'Y-m-d H:i:s'";
263 $this->sysDate =
'GetDate()';
264 $this->sysTimeStamp =
'GetDate()';
265 $this->leftOuter =
'*=';
266 $this->rightOuter =
'=*';
267 $this->hasInsertID =
true;
268 $this->hasTransactions =
true;
269 $this->identitySQL =
'select SCOPE_IDENTITY()';
272 $this->databaseType =
'odbtp';
273 if( @odbtp_get_attr(ODB_ATTR_TXNCAPABLE, $this->_connectionID) )
274 $this->hasTransactions =
true;
276 $this->hasTransactions =
false;
278 @odbtp_set_attr(ODB_ATTR_FULLCOLINFO, TRUE, $this->_connectionID );
280 if ($this->_useUnicodeSQL )
281 @odbtp_set_attr(ODB_ATTR_UNICODESQL, TRUE, $this->_connectionID);
286 function _pconnect($HostOrInterface, $UserOrDSN=
'', $argPassword=
'', $argDatabase=
'')
288 $this->_dontPoolDBC =
false;
289 return $this->
_connect($HostOrInterface, $UserOrDSN, $argPassword, $argDatabase);
294 if (!@odbtp_select_db($dbName, $this->_connectionID)) {
297 $this->database = $dbName;
298 $this->databaseName = $dbName; # obsolete, retained
for compat with older adodb versions
304 global $ADODB_FETCH_MODE;
306 $savem = $ADODB_FETCH_MODE;
307 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
308 if ($this->fetchMode !==
false) $savefm = $this->SetFetchMode(
false);
310 $arr = $this->GetArray(
"||SQLTables||||$ttype");
312 if (isset($savefm)) $this->SetFetchMode($savefm);
313 $ADODB_FETCH_MODE = $savem;
316 for ($i=0; $i <
sizeof($arr); $i++) {
317 if ($arr[$i][3] ==
'SYSTEM TABLE' )
continue;
319 $arr2[] = $showSchema && $arr[$i][1]? $arr[$i][1].
'.'.$arr[$i][2] : $arr[$i][2];
326 global $ADODB_FETCH_MODE;
329 $this->_findschema($table,$schema);
330 if ($upper) $table = strtoupper($table);
332 $savem = $ADODB_FETCH_MODE;
333 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
334 if ($this->fetchMode !==
false) $savefm = $this->SetFetchMode(
false);
336 $rs = $this->Execute(
"||SQLColumns||$schema|$table" );
338 if (isset($savefm)) $this->SetFetchMode($savefm);
339 $ADODB_FETCH_MODE = $savem;
348 if (strtoupper(
$rs->fields[2]) == $table) {
349 $fld =
new ADOFieldObject();
350 $fld->name =
$rs->fields[3];
351 $fld->type =
$rs->fields[5];
352 $fld->max_length =
$rs->fields[6];
353 $fld->not_null = !empty(
$rs->fields[9]);
354 $fld->scale =
$rs->fields[7];
355 if (isset(
$rs->fields[12]))
356 if (!is_null(
$rs->fields[12])) {
357 $fld->has_default =
true;
358 $fld->default_value =
$rs->fields[12];
360 $retarr[strtoupper($fld->name)] = $fld;
361 }
else if (!empty($retarr))
372 global $ADODB_FETCH_MODE;
374 $savem = $ADODB_FETCH_MODE;
375 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
376 $arr = $this->GetArray(
"||SQLPrimaryKeys||$owner|$table");
377 $ADODB_FETCH_MODE = $savem;
381 for ($i=0; $i <
sizeof($arr); $i++) {
382 if ($arr[$i][3]) $arr2[] = $arr[$i][3];
389 global $ADODB_FETCH_MODE;
391 $savem = $ADODB_FETCH_MODE;
392 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
393 $constraints = $this->GetArray(
"||SQLForeignKeys|||||$owner|$table");
394 $ADODB_FETCH_MODE = $savem;
397 foreach($constraints as $constr) {
399 $arr[$constr[11]][$constr[2]][] = $constr[7].
'='.$constr[3];
408 foreach($arr as $k => $v) {
409 foreach($v as $a => $b) {
410 if ($upper) $a = strtoupper($a);
419 if (!$this->hasTransactions)
return false;
420 if ($this->transOff)
return true;
421 $this->transCnt += 1;
422 $this->autoCommit =
false;
423 if (defined(
'ODB_TXN_DEFAULT'))
424 $txn = ODB_TXN_DEFAULT;
426 $txn = ODB_TXN_READUNCOMMITTED;
427 $rs = @odbtp_set_attr(ODB_ATTR_TRANSACTIONS,$txn,$this->_connectionID);
428 if(!
$rs)
return false;
434 if ($this->transOff)
return true;
436 if ($this->transCnt) $this->transCnt -= 1;
437 $this->autoCommit =
true;
438 if( ($ret = @odbtp_commit($this->_connectionID)) )
439 $ret = @odbtp_set_attr(ODB_ATTR_TRANSACTIONS, ODB_TXN_NONE, $this->_connectionID);
445 if ($this->transOff)
return true;
446 if ($this->transCnt) $this->transCnt -= 1;
447 $this->autoCommit =
true;
448 if( ($ret = @odbtp_rollback($this->_connectionID)) )
449 $ret = @odbtp_set_attr(ODB_ATTR_TRANSACTIONS, ODB_TXN_NONE, $this->_connectionID);
456 if( $this->odbc_driver == ODB_DRIVER_FOXPRO ) {
457 if (!preg_match(
'/ORDER[ \t\r\n]+BY/is',
$sql))
$sql .=
' ORDER BY 1';
459 $ret = ADOConnection::SelectLimit(
$sql,$nrows,$offset,$inputarr,$secs2cache);
465 if (! $this->_bindInputArray)
return $sql;
467 $this->_errorMsg =
false;
468 $this->_errorCode =
false;
470 $stmt = @odbtp_prepare(
$sql,$this->_connectionID);
475 return array(
$sql,$stmt,
false);
480 if (!$this->_canPrepareSP)
return $sql;
482 $this->_errorMsg =
false;
483 $this->_errorCode =
false;
485 $stmt = @odbtp_prepare_proc(
$sql,$this->_connectionID);
486 if (!$stmt)
return false;
487 return array(
$sql,$stmt);
509 function Parameter(&$stmt, &$var, $name, $isOutput=
false, $maxLen=0, $type=0)
511 if ( $this->odbc_driver == ODB_DRIVER_JET ) {
512 $name =
'['.$name.
']';
513 if( !$type && $this->_useUnicodeSQL
514 && @odbtp_param_bindtype($stmt[1], $name) == ODB_CHAR )
522 return @odbtp_attach_param($stmt[1], $name, $var, $type, $maxLen);
535 function UpdateBlob($table,$column,$val,$where,$blobtype=
'image')
537 $sql =
"UPDATE $table SET $column = ? WHERE $where";
538 if( !($stmt = @odbtp_prepare(
$sql, $this->_connectionID)) )
540 if( !@odbtp_input( $stmt, 1, ODB_BINARY, 1000000, $blobtype ) )
542 if( !@odbtp_set( $stmt, 1, $val ) )
544 return @odbtp_execute( $stmt ) !=
false;
549 switch ( $this->odbc_driver) {
550 case ODB_DRIVER_MSSQL:
559 $table = strtolower($this->qstr($table));
561 $sql =
"SELECT i.name AS ind_name, C.name AS col_name, USER_NAME(O.uid) AS Owner, c.colid, k.Keyno, 562 CASE WHEN I.indid BETWEEN 1 AND 254 AND (I.status & 2048 = 2048 OR I.Status = 16402 AND O.XType = 'V') THEN 1 ELSE 0 END AS IsPK, 563 CASE WHEN I.status & 2 = 2 THEN 1 ELSE 0 END AS IsUnique 564 FROM dbo.sysobjects o INNER JOIN dbo.sysindexes I ON o.id = i.id 565 INNER JOIN dbo.sysindexkeys K ON I.id = K.id AND I.Indid = K.Indid 566 INNER JOIN dbo.syscolumns c ON K.id = C.id AND K.colid = C.Colid 567 WHERE LEFT(i.name, 8) <> '_WA_Sys_' AND o.status >= 0 AND lower(O.Name) = $table 568 ORDER BY O.name, I.Name, K.keyno";
570 global $ADODB_FETCH_MODE;
571 $save = $ADODB_FETCH_MODE;
572 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
573 if ($this->fetchMode !== FALSE) {
574 $savem = $this->SetFetchMode(FALSE);
579 $this->SetFetchMode($savem);
581 $ADODB_FETCH_MODE = $save;
583 if (!is_object(
$rs)) {
588 while ($row =
$rs->FetchRow()) {
589 if ($primary && !$row[5])
continue;
591 $indexes[$row[0]][
'unique'] = $row[6];
592 $indexes[$row[0]][
'columns'][] = $row[1];
599 switch( $this->odbc_driver ) {
600 case ODB_DRIVER_MSSQL:
601 return " ISNULL($field, $ifNull) ";
603 return " IIF(IsNull($field), $ifNull, $field) ";
605 return " CASE WHEN $field is null THEN $ifNull ELSE $field END ";
610 global $php_errormsg;
612 $this->_errorMsg =
false;
613 $this->_errorCode =
false;
616 if (is_array(
$sql)) {
619 $stmtid = @odbtp_prepare(
$sql,$this->_connectionID);
620 if ($stmtid ==
false) {
621 $this->_errorMsg = $php_errormsg;
625 $num_params = @odbtp_num_params( $stmtid );
633 foreach($inputarr as $v) {
634 @odbtp_input( $stmtid, $param );
635 @odbtp_set( $stmtid, $param, $v );
637 if ($param > $num_params)
break;
640 if (!@odbtp_execute($stmtid) ) {
643 }
else if (is_array(
$sql)) {
645 if (!@odbtp_execute($stmtid)) {
649 $stmtid = odbtp_query(
$sql,$this->_connectionID);
651 $this->_lastAffectedRows = 0;
653 $this->_lastAffectedRows = @odbtp_affected_rows($stmtid);
660 $ret = @odbtp_close($this->_connectionID);
661 $this->_connectionID =
false;
673 if ($mode ===
false) {
674 global $ADODB_FETCH_MODE;
675 $mode = $ADODB_FETCH_MODE;
677 $this->fetchMode = $mode;
678 $this->ADORecordSet($queryID);
683 $this->_numOfFields = @odbtp_num_fields($this->_queryID);
684 if (!($this->_numOfRows = @odbtp_num_rows($this->_queryID)))
685 $this->_numOfRows = -1;
687 if (!$this->connection->_useUnicodeSQL)
return;
689 if ($this->connection->odbc_driver == ODB_DRIVER_JET) {
690 if (!@odbtp_get_attr(ODB_ATTR_MAPCHARTOWCHAR,
691 $this->connection->_connectionID))
693 for ($f = 0; $f < $this->_numOfFields; $f++) {
694 if (@odbtp_field_bindtype($this->_queryID, $f) == ODB_CHAR)
695 @odbtp_bind_field($this->_queryID, $f, ODB_WCHAR);
704 $o=
new ADOFieldObject();
705 $o->name = @odbtp_field_name($this->_queryID,$off);
706 $o->type = @odbtp_field_type($this->_queryID,$off);
707 $o->max_length = @odbtp_field_length($this->_queryID,$off);
708 if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name);
709 else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name);
715 return @odbtp_data_seek($this->_queryID, $row);
720 if ($this->fetchMode & ADODB_FETCH_ASSOC)
return $this->fields[$colname];
723 $this->bind = array();
724 for ($i=0; $i < $this->_numOfFields; $i++) {
725 $name = @odbtp_field_name( $this->_queryID, $i );
726 $this->bind[strtoupper($name)] = $i;
729 return $this->fields[$this->bind[strtoupper($colname)]];
734 switch ($this->fetchMode) {
735 case ADODB_FETCH_NUM:
736 $this->fields = @odbtp_fetch_row($this->_queryID, $type);
738 case ADODB_FETCH_ASSOC:
739 $this->fields = @odbtp_fetch_assoc($this->_queryID, $type);
742 $this->fields = @odbtp_fetch_array($this->_queryID, $type);
744 if ($this->databaseType =
'odbtp_vfp') {
746 foreach($this->fields as $k => $v) {
747 if (strncmp($v,
'1899-12-30',10) == 0) $this->fields[$k] =
'';
750 return is_array($this->fields);
755 return $this->_fetch_odbtp();
760 if (!$this->_fetch_odbtp(ODB_FETCH_FIRST))
return false;
762 $this->_currentRow = 0;
768 if (!$this->_fetch_odbtp(ODB_FETCH_LAST))
return false;
770 $this->_currentRow = $this->_numOfRows - 1;
776 if (!@odbtp_next_result($this->_queryID))
return false;
777 $this->_inited =
false;
779 $this->_currentRow = -1;
786 return @odbtp_free_query($this->_queryID);
UpdateBlob($table, $column, $val, $where, $blobtype='image')
ADORecordSet_odbtp($queryID, $mode=false)
if(isset($_REQUEST['nrows'])) else $rs
MetaIndexes($table, $primary=false, $owner=false)
_connect($HostOrInterface, $UserOrDSN='', $argPassword='', $argDatabase='')
FetchField($fieldOffset=0)
ADORecordSet_odbtp_access($id, $mode=false)
Parameter(&$stmt, &$var, $name, $isOutput=false, $maxLen=0, $type=0)
ADORecordSet_odbtp_oci8($id, $mode=false)
ADORecordSet_odbtp_mssql($id, $mode=false)
CreateSequence($seqname='adodbseq', $start=1)
ADORecordSet_odbtp_sybase($id, $mode=false)
MetaColumns($table, $upper=true)
MetaPrimaryKeys($table, $owner='')
MetaForeignKeys($table, $owner='', $upper=false)
MetaIndexes_mssql($table, $primary=false, $owner=false)
MetaTables($ttype='', $showSchema=false, $mask=false)
GenID($seq='adodbseq', $start=1)
ADORecordSet_odbtp_vfp($id, $mode=false)
SelectLimit($sql, $nrows=-1, $offset=-1, $inputarr=false, $secs2cache=0)
_pconnect($HostOrInterface, $UserOrDSN='', $argPassword='', $argDatabase='')
_query($sql, $inputarr=false)