3 V4.92a 29 Aug 2006 (c) 2000-2006 John Lim (jlim#natsoft.com.my). All rights reserved.
4 Released under both BSD license and Lesser GPL library license.
5 Whenever there is any discrepancy between the two licenses,
6 the BSD license will take precedence. See License.txt.
7 Set tabs to 4 for best viewing.
8 Latest version is available at http://adodb.sourceforge.net
10 // Code contributed by "stefan bogdan" <sbogdan#rsb.ro>
12 // security - hide paths
13 if (!defined('ADODB_DIR')) die();
15 define("_ADODB_ODBTP_LAYER", 2 );
17 class ADODB_odbtp
extends ADOConnection
{
18 var $databaseType = "odbtp";
19 var $dataProvider = "odbtp";
20 var $fmtDate = "'Y-m-d'";
21 var $fmtTimeStamp = "'Y-m-d, h:i:sA'";
22 var $replaceQuote = "''"; // string to use to replace quotes
24 var $hasAffectedRows = true;
25 var $hasInsertID = false;
27 var $hasMoveFirst = true;
29 var $_genSeqSQL = "create table %s (seq_name char(30) not null unique , seq_value integer not null)";
30 var $_dropSeqSQL = "delete from adodb_seq where seq_name = '%s'";
31 var $_bindInputArray = false;
32 var $_useUnicodeSQL = false;
33 var $_canPrepareSP = false;
34 var $_dontPoolDBC = true;
36 function ADODB_odbtp()
42 return array('description' => @odbtp_get_attr
( ODB_ATTR_DBMSNAME
, $this->_connectionID
),
43 'version' => @odbtp_get_attr
( ODB_ATTR_DBMSVER
, $this->_connectionID
));
48 if (empty($this->_connectionID
)) return @odbtp_last_error
();
49 return @odbtp_last_error
($this->_connectionID
);
54 if (empty($this->_connectionID
)) return @odbtp_last_error_state
();
55 return @odbtp_last_error_state
($this->_connectionID
);
61 // Returns the last IDENTITY value inserted into an IDENTITY column in
62 // the same scope. A scope is a module -- a stored procedure, trigger,
63 // function, or batch. Thus, two statements are in the same scope if
64 // they are in the same stored procedure, function, or batch.
65 return $this->GetOne($this->identitySQL
);
68 function _affectedrows()
70 if ($this->_queryID
) {
71 return @odbtp_affected_rows
($this->_queryID
);
76 function CreateSequence($seqname='adodbseq',$start=1)
79 $num = $this->GetOne("select seq_value from adodb_seq");
81 if( $this->odbc_driver
== ODB_DRIVER_FOXPRO
) {
82 $path = @odbtp_get_attr
( ODB_ATTR_DATABASENAME
, $this->_connectionID
);
83 //if using vfp dbc file
84 if( !strcasecmp(strrchr($path, '.'), '.dbc') )
85 $path = substr($path,0,strrpos($path,'\/'));
86 $seqtab = $path . '/' . $seqtab;
89 if (empty($this->_genSeqSQL
)) return false;
90 $ok = $this->Execute(sprintf($this->_genSeqSQL
,$seqtab));
92 $num = $this->GetOne("select seq_value from adodb_seq where seq_name='$seqname'");
97 return $this->Execute("insert into adodb_seq values('$seqname',$start)");
100 function DropSequence($seqname)
102 if (empty($this->_dropSeqSQL
)) return false;
103 return $this->Execute(sprintf($this->_dropSeqSQL
,$seqname));
106 function GenID($seq='adodbseq',$start=1)
109 if( $this->odbc_driver
== ODB_DRIVER_FOXPRO
) {
110 $path = @odbtp_get_attr
( ODB_ATTR_DATABASENAME
, $this->_connectionID
);
111 //if using vfp dbc file
112 if( !strcasecmp(strrchr($path, '.'), '.dbc') )
113 $path = substr($path,0,strrpos($path,'\/'));
114 $seqtab = $path . '/' . $seqtab;
117 while (--$MAXLOOPS>=0) {
118 $num = $this->GetOne("select seq_value from adodb_seq where seq_name='$seq'");
119 if ($num === false) {
120 //verify if abodb_seq table exist
121 $ok = $this->GetOne("select seq_value from adodb_seq ");
123 //creating the sequence table adodb_seq
124 $this->Execute(sprintf($this->_genSeqSQL
,$seqtab));
128 $ok = $this->Execute("insert into adodb_seq values('$seq',$start)");
129 if (!$ok) return false;
131 $ok = $this->Execute("update adodb_seq set seq_value=seq_value+1 where seq_name='$seq'");
138 if ($fn = $this->raiseErrorFn
) {
139 $fn($this->databaseType
,'GENID',-32000,"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num);
144 //example for $UserOrDSN
145 //for visual fox : DRIVER={Microsoft Visual FoxPro Driver};SOURCETYPE=DBF;SOURCEDB=c:\YourDbfFileDir;EXCLUSIVE=NO;
146 //for visual fox dbc: DRIVER={Microsoft Visual FoxPro Driver};SOURCETYPE=DBC;SOURCEDB=c:\YourDbcFileDir\mydb.dbc;EXCLUSIVE=NO;
147 //for access : DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:\path_to_access_db\base_test.mdb;UID=root;PWD=;
148 //for mssql : DRIVER={SQL Server};SERVER=myserver;UID=myuid;PWD=mypwd;DATABASE=OdbtpTest;
149 //if uid & pwd can be separate
150 function _connect($HostOrInterface, $UserOrDSN='', $argPassword='', $argDatabase='')
152 $this->_connectionID
= @odbtp_connect
($HostOrInterface,$UserOrDSN,$argPassword,$argDatabase);
153 odbtp_convert_datetime($this->_connectionID
,true);
155 if ($this->_connectionID
=== false) {
156 $this->_errorMsg
= $this->ErrorMsg() ;
159 if ($this->_dontPoolDBC
) {
160 if (function_exists('odbtp_dont_pool_dbc'))
161 @odbtp_dont_pool_dbc
($this->_connectionID
);
164 $this->_dontPoolDBC
= true;
166 $this->odbc_driver
= @odbtp_get_attr
(ODB_ATTR_DRIVER
, $this->_connectionID
);
167 $dbms = strtolower(@odbtp_get_attr
(ODB_ATTR_DBMSNAME
, $this->_connectionID
));
168 $this->odbc_name
= $dbms;
170 // Account for inconsistent DBMS names
171 if( $this->odbc_driver
== ODB_DRIVER_ORACLE
)
173 else if( $this->odbc_driver
== ODB_DRIVER_SYBASE
)
176 // Set DBMS specific attributes
178 case 'microsoft sql server':
179 $this->databaseType
= 'odbtp_mssql';
180 $this->fmtDate
= "'Y-m-d'";
181 $this->fmtTimeStamp
= "'Y-m-d h:i:sA'";
182 $this->sysDate
= 'convert(datetime,convert(char,GetDate(),102),102)';
183 $this->sysTimeStamp
= 'GetDate()';
184 $this->ansiOuter
= true;
185 $this->leftOuter
= '*=';
186 $this->rightOuter
= '=*';
187 $this->hasTop
= 'top';
188 $this->hasInsertID
= true;
189 $this->hasTransactions
= true;
190 $this->_bindInputArray
= true;
191 $this->_canSelectDb
= true;
192 $this->substr
= "substring";
193 $this->length
= 'len';
194 $this->identitySQL
= 'select @@IDENTITY';
195 $this->metaDatabasesSQL
= "select name from master..sysdatabases where name <> 'master'";
196 $this->_canPrepareSP
= true;
199 $this->databaseType
= 'odbtp_access';
200 $this->fmtDate
= "#Y-m-d#";
201 $this->fmtTimeStamp
= "#Y-m-d h:i:sA#";
202 $this->sysDate
= "FORMAT(NOW,'yyyy-mm-dd')";
203 $this->sysTimeStamp
= 'NOW';
204 $this->hasTop
= 'top';
205 $this->hasTransactions
= false;
206 $this->_canPrepareSP
= true; // For MS Access only.
208 case 'visual foxpro':
209 $this->databaseType
= 'odbtp_vfp';
210 $this->fmtDate
= "{^Y-m-d}";
211 $this->fmtTimeStamp
= "{^Y-m-d, h:i:sA}";
212 $this->sysDate
= 'date()';
213 $this->sysTimeStamp
= 'datetime()';
214 $this->ansiOuter
= true;
215 $this->hasTop
= 'top';
216 $this->hasTransactions
= false;
217 $this->replaceQuote
= "'+chr(39)+'";
219 $this->false = '.F.';
222 $this->databaseType
= 'odbtp_oci8';
223 $this->fmtDate
= "'Y-m-d 00:00:00'";
224 $this->fmtTimeStamp
= "'Y-m-d h:i:sA'";
225 $this->sysDate
= 'TRUNC(SYSDATE)';
226 $this->sysTimeStamp
= 'SYSDATE';
227 $this->hasTransactions
= true;
228 $this->_bindInputArray
= true;
229 $this->concat_operator
= '||';
232 $this->databaseType
= 'odbtp_sybase';
233 $this->fmtDate
= "'Y-m-d'";
234 $this->fmtTimeStamp
= "'Y-m-d H:i:s'";
235 $this->sysDate
= 'GetDate()';
236 $this->sysTimeStamp
= 'GetDate()';
237 $this->leftOuter
= '*=';
238 $this->rightOuter
= '=*';
239 $this->hasInsertID
= true;
240 $this->hasTransactions
= true;
241 $this->identitySQL
= 'select @@IDENTITY';
244 $this->databaseType
= 'odbtp';
245 if( @odbtp_get_attr
(ODB_ATTR_TXNCAPABLE
, $this->_connectionID
) )
246 $this->hasTransactions
= true;
248 $this->hasTransactions
= false;
250 @odbtp_set_attr
(ODB_ATTR_FULLCOLINFO
, TRUE, $this->_connectionID
);
252 if ($this->_useUnicodeSQL
)
253 @odbtp_set_attr
(ODB_ATTR_UNICODESQL
, TRUE, $this->_connectionID
);
258 function _pconnect($HostOrInterface, $UserOrDSN='', $argPassword='', $argDatabase='')
260 $this->_dontPoolDBC
= false;
261 return $this->_connect($HostOrInterface, $UserOrDSN, $argPassword, $argDatabase);
264 function SelectDB($dbName)
266 if (!@odbtp_select_db
($dbName, $this->_connectionID
)) {
269 $this->database
= $dbName;
270 $this->databaseName
= $dbName; # obsolete, retained for compat with older adodb versions
274 function &MetaTables($ttype='',$showSchema=false,$mask=false)
276 global $ADODB_FETCH_MODE;
278 $savem = $ADODB_FETCH_MODE;
279 $ADODB_FETCH_MODE = ADODB_FETCH_NUM
;
280 if ($this->fetchMode
!== false) $savefm = $this->SetFetchMode(false);
282 $arr =& $this->GetArray("||SQLTables||||$ttype");
284 if (isset($savefm)) $this->SetFetchMode($savefm);
285 $ADODB_FETCH_MODE = $savem;
288 for ($i=0; $i < sizeof($arr); $i++
) {
289 if ($arr[$i][3] == 'SYSTEM TABLE' ) continue;
291 $arr2[] = $showSchema ?
$arr[$i][1].'.'.$arr[$i][2] : $arr[$i][2];
296 function &MetaColumns($table,$upper=true)
298 global $ADODB_FETCH_MODE;
301 $this->_findschema($table,$schema);
302 if ($upper) $table = strtoupper($table);
304 $savem = $ADODB_FETCH_MODE;
305 $ADODB_FETCH_MODE = ADODB_FETCH_NUM
;
306 if ($this->fetchMode
!== false) $savefm = $this->SetFetchMode(false);
308 $rs = $this->Execute( "||SQLColumns||$schema|$table" );
310 if (isset($savefm)) $this->SetFetchMode($savefm);
311 $ADODB_FETCH_MODE = $savem;
313 if (!$rs ||
$rs->EOF
) {
319 //print_r($rs->fields);
320 if (strtoupper($rs->fields
[2]) == $table) {
321 $fld = new ADOFieldObject();
322 $fld->name
= $rs->fields
[3];
323 $fld->type
= $rs->fields
[5];
324 $fld->max_length
= $rs->fields
[6];
325 $fld->not_null
= !empty($rs->fields
[9]);
326 $fld->scale
= $rs->fields
[7];
327 if (!is_null($rs->fields
[12])) {
328 $fld->has_default
= true;
329 $fld->default_value
= $rs->fields
[12];
331 $retarr[strtoupper($fld->name
)] = $fld;
332 } else if (!empty($retarr))
341 function &MetaPrimaryKeys($table, $owner='')
343 global $ADODB_FETCH_MODE;
345 $savem = $ADODB_FETCH_MODE;
346 $ADODB_FETCH_MODE = ADODB_FETCH_NUM
;
347 $arr =& $this->GetArray("||SQLPrimaryKeys||$owner|$table");
348 $ADODB_FETCH_MODE = $savem;
352 for ($i=0; $i < sizeof($arr); $i++
) {
353 if ($arr[$i][3]) $arr2[] = $arr[$i][3];
358 function &MetaForeignKeys($table, $owner='', $upper=false)
360 global $ADODB_FETCH_MODE;
362 $savem = $ADODB_FETCH_MODE;
363 $ADODB_FETCH_MODE = ADODB_FETCH_NUM
;
364 $constraints =& $this->GetArray("||SQLForeignKeys|||||$owner|$table");
365 $ADODB_FETCH_MODE = $savem;
368 foreach($constraints as $constr) {
370 $arr[$constr[11]][$constr[2]][] = $constr[7].'='.$constr[3];
379 foreach($arr as $k => $v) {
380 foreach($v as $a => $b) {
381 if ($upper) $a = strtoupper($a);
388 function BeginTrans()
390 if (!$this->hasTransactions
) return false;
391 if ($this->transOff
) return true;
392 $this->transCnt +
= 1;
393 $this->autoCommit
= false;
394 if (defined('ODB_TXN_DEFAULT'))
395 $txn = ODB_TXN_DEFAULT
;
397 $txn = ODB_TXN_READUNCOMMITTED
;
398 $rs = @odbtp_set_attr
(ODB_ATTR_TRANSACTIONS
,$txn,$this->_connectionID
);
399 if(!$rs) return false;
403 function CommitTrans($ok=true)
405 if ($this->transOff
) return true;
406 if (!$ok) return $this->RollbackTrans();
407 if ($this->transCnt
) $this->transCnt
-= 1;
408 $this->autoCommit
= true;
409 if( ($ret = @odbtp_commit
($this->_connectionID
)) )
410 $ret = @odbtp_set_attr
(ODB_ATTR_TRANSACTIONS
, ODB_TXN_NONE
, $this->_connectionID
);//set transaction off
414 function RollbackTrans()
416 if ($this->transOff
) return true;
417 if ($this->transCnt
) $this->transCnt
-= 1;
418 $this->autoCommit
= true;
419 if( ($ret = @odbtp_rollback
($this->_connectionID
)) )
420 $ret = @odbtp_set_attr
(ODB_ATTR_TRANSACTIONS
, ODB_TXN_NONE
, $this->_connectionID
);//set transaction off
424 function &SelectLimit($sql,$nrows=-1,$offset=-1, $inputarr=false,$secs2cache=0)
426 // TOP requires ORDER BY for Visual FoxPro
427 if( $this->odbc_driver
== ODB_DRIVER_FOXPRO
) {
428 if (!preg_match('/ORDER[ \t\r\n]+BY/is',$sql)) $sql .= ' ORDER BY 1';
430 $ret =& ADOConnection
::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache);
434 function Prepare($sql)
436 if (! $this->_bindInputArray
) return $sql; // no binding
437 $stmt = @odbtp_prepare
($sql,$this->_connectionID
);
439 // print "Prepare Error for ($sql) ".$this->ErrorMsg()."<br>";
442 return array($sql,$stmt,false);
445 function PrepareSP($sql)
447 if (!$this->_canPrepareSP
) return $sql; // Can't prepare procedures
449 $stmt = @odbtp_prepare_proc
($sql,$this->_connectionID
);
450 if (!$stmt) return false;
451 return array($sql,$stmt);
456 $stmt = $db->PrepareSP('SP_RUNSOMETHING'); -- takes 2 params, @myid and @group
458 # note that the parameter does not have @ in front!
459 $db->Parameter($stmt,$id,'myid');
460 $db->Parameter($stmt,$group,'group',false,64);
461 $db->Parameter($stmt,$group,'photo',false,100000,ODB_BINARY);
464 @param $stmt Statement returned by Prepare() or PrepareSP().
465 @param $var PHP variable to bind to. Can set to null (for isNull support).
466 @param $name Name of stored procedure variable name to bind to.
467 @param [$isOutput] Indicates direction of parameter 0/false=IN 1=OUT 2= IN/OUT. This is ignored in odbtp.
468 @param [$maxLen] Holds an maximum length of the variable.
469 @param [$type] The data type of $var. Legal values depend on driver.
471 See odbtp_attach_param documentation at http://odbtp.sourceforge.net.
473 function Parameter(&$stmt, &$var, $name, $isOutput=false, $maxLen=0, $type=0)
475 if ( $this->odbc_driver
== ODB_DRIVER_JET
) {
476 $name = '['.$name.']';
477 if( !$type && $this->_useUnicodeSQL
478 && @odbtp_param_bindtype
($stmt[1], $name) == ODB_CHAR
)
486 return @odbtp_attach_param
($stmt[1], $name, $var, $type, $maxLen);
490 Insert a null into the blob field of the table first.
491 Then use UpdateBlob to store the blob.
495 $conn->Execute('INSERT INTO blobtable (id, blobcol) VALUES (1, null)');
496 $conn->UpdateBlob('blobtable','blobcol',$blob,'id=1');
499 function UpdateBlob($table,$column,$val,$where,$blobtype='image')
501 $sql = "UPDATE $table SET $column = ? WHERE $where";
502 if( !($stmt = @odbtp_prepare
($sql, $this->_connectionID
)) )
504 if( !@odbtp_input
( $stmt, 1, ODB_BINARY
, 1000000, $blobtype ) )
506 if( !@odbtp_set
( $stmt, 1, $val ) )
508 return @odbtp_execute
( $stmt ) != false;
511 function IfNull( $field, $ifNull )
513 switch( $this->odbc_driver
) {
514 case ODB_DRIVER_MSSQL
:
515 return " ISNULL($field, $ifNull) ";
517 return " IIF(IsNull($field), $ifNull, $field) ";
519 return " CASE WHEN $field is null THEN $ifNull ELSE $field END ";
522 function _query($sql,$inputarr=false)
524 global $php_errormsg;
527 if (is_array($sql)) {
530 $stmtid = @odbtp_prepare
($sql,$this->_connectionID
);
531 if ($stmtid == false) {
532 $this->_errorMsg
= $php_errormsg;
536 $num_params = @odbtp_num_params
( $stmtid );
537 for( $param = 1; $param <= $num_params; $param++
) {
538 @odbtp_input
( $stmtid, $param );
539 @odbtp_set
( $stmtid, $param, $inputarr[$param-1] );
541 if (!@odbtp_execute
($stmtid) ) {
544 } else if (is_array($sql)) {
546 if (!@odbtp_execute
($stmtid)) {
550 $stmtid = @odbtp_query
($sql,$this->_connectionID
);
552 $this->_lastAffectedRows
= 0;
554 $this->_lastAffectedRows
= @odbtp_affected_rows
($stmtid);
561 $ret = @odbtp_close
($this->_connectionID
);
562 $this->_connectionID
= false;
567 class ADORecordSet_odbtp
extends ADORecordSet
{
569 var $databaseType = 'odbtp';
572 function ADORecordSet_odbtp($queryID,$mode=false)
574 if ($mode === false) {
575 global $ADODB_FETCH_MODE;
576 $mode = $ADODB_FETCH_MODE;
578 $this->fetchMode
= $mode;
579 $this->ADORecordSet($queryID);
584 $this->_numOfFields
= @odbtp_num_fields
($this->_queryID
);
585 if (!($this->_numOfRows
= @odbtp_num_rows
($this->_queryID
)))
586 $this->_numOfRows
= -1;
588 if (!$this->connection
->_useUnicodeSQL
) return;
590 if ($this->connection
->odbc_driver
== ODB_DRIVER_JET
) {
591 if (!@odbtp_get_attr
(ODB_ATTR_MAPCHARTOWCHAR
,
592 $this->connection
->_connectionID
))
594 for ($f = 0; $f < $this->_numOfFields
; $f++
) {
595 if (@odbtp_field_bindtype
($this->_queryID
, $f) == ODB_CHAR
)
596 @odbtp_bind_field
($this->_queryID
, $f, ODB_WCHAR
);
602 function &FetchField($fieldOffset = 0)
604 $off=$fieldOffset; // offsets begin at 0
605 $o= new ADOFieldObject();
606 $o->name
= @odbtp_field_name
($this->_queryID
,$off);
607 $o->type
= @odbtp_field_type
($this->_queryID
,$off);
608 $o->max_length
= @odbtp_field_length
($this->_queryID
,$off);
609 if (ADODB_ASSOC_CASE
== 0) $o->name
= strtolower($o->name
);
610 else if (ADODB_ASSOC_CASE
== 1) $o->name
= strtoupper($o->name
);
616 return @odbtp_data_seek
($this->_queryID
, $row);
619 function fields($colname)
621 if ($this->fetchMode
& ADODB_FETCH_ASSOC
) return $this->fields
[$colname];
624 $this->bind
= array();
625 for ($i=0; $i < $this->_numOfFields
; $i++
) {
626 $name = @odbtp_field_name
( $this->_queryID
, $i );
627 $this->bind
[strtoupper($name)] = $i;
630 return $this->fields
[$this->bind
[strtoupper($colname)]];
633 function _fetch_odbtp($type=0)
635 switch ($this->fetchMode
) {
636 case ADODB_FETCH_NUM
:
637 $this->fields
= @odbtp_fetch_row
($this->_queryID
, $type);
639 case ADODB_FETCH_ASSOC
:
640 $this->fields
= @odbtp_fetch_assoc
($this->_queryID
, $type);
643 $this->fields
= @odbtp_fetch_array
($this->_queryID
, $type);
645 return is_array($this->fields
);
650 return $this->_fetch_odbtp();
655 if (!$this->_fetch_odbtp(ODB_FETCH_FIRST
)) return false;
657 $this->_currentRow
= 0;
663 if (!$this->_fetch_odbtp(ODB_FETCH_LAST
)) return false;
665 $this->_currentRow
= $this->_numOfRows
- 1;
669 function NextRecordSet()
671 if (!@odbtp_next_result
($this->_queryID
)) return false;
672 $this->_inited
= false;
674 $this->_currentRow
= -1;
681 return @odbtp_free_query
($this->_queryID
);
685 class ADORecordSet_odbtp_mssql
extends ADORecordSet_odbtp
{
687 var $databaseType = 'odbtp_mssql';
689 function ADORecordSet_odbtp_mssql($id,$mode=false)
691 return $this->ADORecordSet_odbtp($id,$mode);
695 class ADORecordSet_odbtp_access
extends ADORecordSet_odbtp
{
697 var $databaseType = 'odbtp_access';
699 function ADORecordSet_odbtp_access($id,$mode=false)
701 return $this->ADORecordSet_odbtp($id,$mode);
705 class ADORecordSet_odbtp_vfp
extends ADORecordSet_odbtp
{
707 var $databaseType = 'odbtp_vfp';
709 function ADORecordSet_odbtp_vfp($id,$mode=false)
711 return $this->ADORecordSet_odbtp($id,$mode);
715 class ADORecordSet_odbtp_oci8
extends ADORecordSet_odbtp
{
717 var $databaseType = 'odbtp_oci8';
719 function ADORecordSet_odbtp_oci8($id,$mode=false)
721 return $this->ADORecordSet_odbtp($id,$mode);
725 class ADORecordSet_odbtp_sybase
extends ADORecordSet_odbtp
{
727 var $databaseType = 'odbtp_sybase';
729 function ADORecordSet_odbtp_sybase($id,$mode=false)
731 return $this->ADORecordSet_odbtp($id,$mode);