Source for file adodb-odbtp.inc.php
Documentation is available at adodb-odbtp.inc.php
V4.97 22 Jan 2008 (c) 2000-2008 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. See License.txt.
Set tabs to 4 for best viewing.
Latest version is available at http://adodb.sourceforge.net
// Code contributed by "stefan bogdan" <sbogdan#rsb.ro>
define("_ADODB_ODBTP_LAYER", 2 );
var $_genSeqSQL = "create table %s (seq_name char(30) not null unique , seq_value integer not null)";
var $_dropSeqSQL = "delete from adodb_seq where seq_name = '%s'";
return array('description' => @odbtp_get_attr( ODB_ATTR_DBMSNAME, $this->_connectionID),
'version' => @odbtp_get_attr( ODB_ATTR_DBMSVER, $this->_connectionID));
if (empty($this->_connectionID)) return @odbtp_last_error_state();
// Returns the last IDENTITY value inserted into an IDENTITY column in
// the same scope. A scope is a module -- a stored procedure, trigger,
// function, or batch. Thus, two statements are in the same scope if
// they are in the same stored procedure, function, or batch.
return $this->GetOne($this->identitySQL);
return @odbtp_affected_rows ($this->_queryID);
$num = $this->GetOne("select seq_value from adodb_seq");
$path = @odbtp_get_attr( ODB_ATTR_DATABASENAME, $this->_connectionID );
$seqtab = $path . '/' . $seqtab;
$num = $this->GetOne("select seq_value from adodb_seq where seq_name='$seqname'");
return $this->Execute("insert into adodb_seq values('$seqname',$start)");
function GenID($seq= 'adodbseq',$start= 1)
$path = @odbtp_get_attr( ODB_ATTR_DATABASENAME, $this->_connectionID );
$seqtab = $path . '/' . $seqtab;
$num = $this->GetOne("select seq_value from adodb_seq where seq_name='$seq'");
//verify if abodb_seq table exist
$ok = $this->GetOne("select seq_value from adodb_seq ");
//creating the sequence table adodb_seq
$ok = $this->Execute("insert into adodb_seq values('$seq',$start)");
$ok = $this->Execute("update adodb_seq set seq_value=seq_value+1 where seq_name='$seq'");
$fn($this->databaseType,'GENID',- 32000,"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num);
//for visual fox : DRIVER={Microsoft Visual FoxPro Driver};SOURCETYPE=DBF;SOURCEDB=c:\YourDbfFileDir;EXCLUSIVE=NO;
//for visual fox dbc: DRIVER={Microsoft Visual FoxPro Driver};SOURCETYPE=DBC;SOURCEDB=c:\YourDbcFileDir\mydb.dbc;EXCLUSIVE=NO;
//for access : DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:\path_to_access_db\base_test.mdb;UID=root;PWD=;
//for mssql : DRIVER={SQL Server};SERVER=myserver;UID=myuid;PWD=mypwd;DATABASE=OdbtpTest;
//if uid & pwd can be separate
function _connect($HostOrInterface, $UserOrDSN= '', $argPassword= '', $argDatabase= '')
$this->_connectionID = odbtp_connect($HostOrInterface,$UserOrDSN,$argPassword,$argDatabase);
$this->odbc_name = $dbms;
// Account for inconsistent DBMS names
// Set DBMS specific attributes
case 'microsoft sql server':
$this->sysDate = 'convert(datetime,convert(char,GetDate(),102),102)';
$this->_canSelectDb = true;
$this->identitySQL = 'select SCOPE_IDENTITY()';
$this->metaDatabasesSQL = "select name from master..sysdatabases where name <> 'master'";
$this->sysDate = "FORMAT(NOW,'yyyy-mm-dd')";
$this->fmtDate = "'Y-m-d 00:00:00'";
$this->identitySQL = 'select SCOPE_IDENTITY()';
@odbtp_set_attr(ODB_ATTR_FULLCOLINFO, TRUE, $this->_connectionID );
@odbtp_set_attr(ODB_ATTR_UNICODESQL, TRUE, $this->_connectionID);
function _pconnect($HostOrInterface, $UserOrDSN= '', $argPassword= '', $argDatabase= '')
return $this->_connect($HostOrInterface, $UserOrDSN, $argPassword, $argDatabase);
$this->databaseName = $dbName; # obsolete, retained for compat with older adodb versions
function &MetaTables($ttype= '',$showSchema= false,$mask= false)
global $ADODB_FETCH_MODE;
$savem = $ADODB_FETCH_MODE;
$arr = & $this->GetArray("||SQLTables||||$ttype");
$ADODB_FETCH_MODE = $savem;
for ($i= 0; $i < sizeof($arr); $i++ ) {
if ($arr[$i][3] == 'SYSTEM TABLE' ) continue;
$arr2[] = $showSchema && $arr[$i][1]? $arr[$i][1]. '.'. $arr[$i][2] : $arr[$i][2];
global $ADODB_FETCH_MODE;
$savem = $ADODB_FETCH_MODE;
$rs = $this->Execute( "||SQLColumns||$schema|$table" );
$ADODB_FETCH_MODE = $savem;
$fld->name = $rs->fields[3];
$fld->type = $rs->fields[5];
$fld->max_length = $rs->fields[6];
$fld->not_null = !empty($rs->fields[9]);
$fld->scale = $rs->fields[7];
if (isset ($rs->fields[12])) // vfp does not have field 12
$fld->has_default = true;
$fld->default_value = $rs->fields[12];
} else if (!empty($retarr))
global $ADODB_FETCH_MODE;
$savem = $ADODB_FETCH_MODE;
$arr = & $this->GetArray("||SQLPrimaryKeys||$owner|$table");
$ADODB_FETCH_MODE = $savem;
for ($i= 0; $i < sizeof($arr); $i++ ) {
if ($arr[$i][3]) $arr2[] = $arr[$i][3];
global $ADODB_FETCH_MODE;
$savem = $ADODB_FETCH_MODE;
$constraints = & $this->GetArray("||SQLForeignKeys|||||$owner|$table");
$ADODB_FETCH_MODE = $savem;
foreach($constraints as $constr) {
$arr[$constr[11]][$constr[2]][] = $constr[7]. '='. $constr[3];
foreach($arr as $k => $v) {
foreach($v as $a => $b) {
$txn = ODB_TXN_READUNCOMMITTED;
$rs = @odbtp_set_attr(ODB_ATTR_TRANSACTIONS,$txn,$this->_connectionID);
$ret = @odbtp_set_attr(ODB_ATTR_TRANSACTIONS, ODB_TXN_NONE, $this->_connectionID);//set transaction off
$ret = @odbtp_set_attr(ODB_ATTR_TRANSACTIONS, ODB_TXN_NONE, $this->_connectionID);//set transaction off
function &SelectLimit($sql,$nrows=- 1,$offset=- 1, $inputarr= false,$secs2cache= 0)
// TOP requires ORDER BY for Visual FoxPro
if (!preg_match('/ORDER[ \t\r\n]+BY/is',$sql)) $sql .= ' ORDER BY 1';
// print "Prepare Error for ($sql) ".$this->ErrorMsg()."<br>";
return array($sql,$stmt,false);
if (!$this->_canPrepareSP) return $sql; // Can't prepare procedures
if (!$stmt) return false;
return array($sql,$stmt);
$stmt = $db->PrepareSP('SP_RUNSOMETHING'); -- takes 2 params, @myid and @group
# note that the parameter does not have @ in front!
$db->Parameter($stmt,$id,'myid');
$db->Parameter($stmt,$group,'group',false,64);
$db->Parameter($stmt,$group,'photo',false,100000,ODB_BINARY);
@param $stmt Statement returned by Prepare() or PrepareSP().
@param $var PHP variable to bind to. Can set to null (for isNull support).
@param $name Name of stored procedure variable name to bind to.
@param [$isOutput] Indicates direction of parameter 0/false=IN 1=OUT 2= IN/OUT. This is ignored in odbtp.
@param [$maxLen] Holds an maximum length of the variable.
@param [$type] The data type of $var. Legal values depend on driver.
See odbtp_attach_param documentation at http://odbtp.sourceforge.net.
function Parameter(&$stmt, &$var, $name, $isOutput= false, $maxLen= 0, $type= 0)
&& @odbtp_param_bindtype($stmt[1], $name) == ODB_CHAR )
return @odbtp_attach_param($stmt[1], $name, $var, $type, $maxLen);
Insert a null into the blob field of the table first.
Then use UpdateBlob to store the blob.
$conn->Execute('INSERT INTO blobtable (id, blobcol) VALUES (1, null)');
$conn->UpdateBlob('blobtable','blobcol',$blob,'id=1');
function UpdateBlob($table,$column,$val,$where,$blobtype= 'image')
$sql = "UPDATE $table SET $column = ? WHERE $where";
if( !@odbtp_input( $stmt, 1, ODB_BINARY, 1000000, $blobtype ) )
if( !@odbtp_set( $stmt, 1, $val ) )
return @odbtp_execute( $stmt ) != false;
function IfNull( $field, $ifNull )
return " ISNULL($field, $ifNull) ";
return " IIF(IsNull($field), $ifNull, $field) ";
return " CASE WHEN $field is null THEN $ifNull ELSE $field END ";
function _query($sql,$inputarr= false)
$num_params = @odbtp_num_params( $stmtid );
for( $param = 1; $param <= $num_params; $param++ ) {
@odbtp_input( $stmtid, $param );
@odbtp_set( $stmtid, $param, $inputarr[$param- 1] );
if (!@odbtp_execute($stmtid) ) {
if (!@odbtp_execute($stmtid)) {
$this->_lastAffectedRows = 0;
$this->_lastAffectedRows = @odbtp_affected_rows($stmtid);
global $ADODB_FETCH_MODE;
$mode = $ADODB_FETCH_MODE;
if ($this->connection->odbc_driver == ODB_DRIVER_JET) {
if (!@odbtp_get_attr(ODB_ATTR_MAPCHARTOWCHAR,
if (@odbtp_field_bindtype($this->_queryID, $f) == ODB_CHAR)
@odbtp_bind_field($this->_queryID, $f, ODB_WCHAR);
$off= $fieldOffset; // offsets begin at 0
$o->name = @odbtp_field_name($this->_queryID,$off);
$o->type = @odbtp_field_type($this->_queryID,$off);
$o->max_length = @odbtp_field_length($this->_queryID,$off);
if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name);
else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name);
return @odbtp_data_seek($this->_queryID, $row);
$name = @odbtp_field_name( $this->_queryID, $i );
foreach($this->fields as $k => $v) {
if (!@odbtp_next_result($this->_queryID)) return false;
return @odbtp_free_query($this->_queryID);
|