home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / adodb-odbtp.inc.php < prev    next >
Encoding:
PHP Script  |  2004-03-20  |  9.2 KB  |  340 lines

  1. <?php
  2. /* 
  3. V4.21 20 Mar 2004  (c) 2000-2004 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.   
  9.   Latest version is available at http://php.weblogs.com/
  10. */
  11.  
  12.  
  13. // Code contributed by "stefan bogdan" <sbogdan#rsb.ro>
  14. //odbtp adodb extension
  15.  
  16. class ADODB_odbtp extends ADOConnection{
  17.     var $databaseType = "odbtp";
  18.     var $fmtDate = "'Y-m-d'";
  19.     var $fmtTimeStamp = "'Y-m-d, h:i:sA'";
  20.     var $replaceQuote = "''"; // string to use to replace quotes
  21.     var $dataProvider = "odbtp";
  22.     var $odbc_driver ='';
  23.     var $hasAffectedRows = true;
  24.     var $_genSeqSQL = "create table %s (id integer)";
  25.     var $_autocommit = true;
  26.     
  27.     function ADODB_odbtp()
  28.     {
  29.     }
  30.     
  31.     function ErrorMsg()
  32.     {
  33.         if (empty($this->_connectionID)) return @odbtp_last_error();
  34.         return @odbtp_last_error($this->_connectionID);
  35.     }
  36.     
  37.     function ErrorNo()
  38.     {
  39.         if (empty($this->_connectionID)) return @odbtp_last_error_code();
  40.             return @odbtp_last_error_code($this->_connectionID);
  41.     }
  42.     function _affectedrows()
  43.     {
  44.         if ($this->lastQuery) {
  45.            return odbtp_affected_rows ($this->lastQuery);
  46.        } else
  47.         return 0;
  48.     }
  49.     function CreateSequence($seqname='adodbseq',$start=1)
  50.     {
  51.         if (empty($this->_genSeqSQL)) return false;
  52.         $ok = $this->Execute(sprintf($this->_genSeqSQL,$seqname));
  53.         if (!$ok) return false;
  54.         $start -= 1;
  55.         return $this->Execute("insert into $seqname values($start)");
  56.     }
  57.  
  58.     var $_dropSeqSQL = 'drop table %s';
  59.     function DropSequence($seqname)
  60.     {
  61.         if (empty($this->_dropSeqSQL)) return false;
  62.         return $this->Execute(sprintf($this->_dropSeqSQL,$seqname));
  63.     }
  64.     
  65.     function GenID($seq='adodbseq',$start=1)
  66.     {
  67.         // if you have to modify the parameter below, your database is overloaded,
  68.         // or you need to implement generation of id's yourself!
  69.         $MAXLOOPS = 100;
  70.         //$this->debug=1;
  71.         while (--$MAXLOOPS>=0) {
  72.             $num = $this->GetOne("select id from $seq");
  73.             if ($num === false) {
  74.                 $this->Execute(sprintf($this->_genSeqSQL ,$seq));
  75.                 //if(!$this->Execute("CREATE TABLE $seq ".
  76.                 //            '(id INT NOT NULL UNIQUE)')) echo 'naspa la creare tabela';
  77.  
  78.                 $start -= 1;
  79.                 $num = '0';
  80.                 $ok = $this->Execute("insert into $seq values($start)");
  81.                 if (!$ok) return false;
  82.             }
  83.             $this->Execute("update $seq set id=id+1 where id=$num");
  84.  
  85.             if ($this->affected_rows() > 0) {
  86.                 $num += 1;
  87.                 $this->genID = $num;
  88.                 return $num;
  89.             }
  90.         }
  91.         if ($fn = $this->raiseErrorFn) {
  92.             $fn($this->databaseType,'GENID',-32000,"Unable to generate unique id after $MAXLOOPS attempts",$seq,$num);
  93.         }
  94.         return false;
  95.     }
  96.  
  97.     //example for $argDSN
  98.     //for visual fox : DRIVER={Microsoft Visual FoxPro Driver};SOURCETYPE=DBF;SOURCEDB=c:\YourDbfFileDir;EXCLUSIVE=NO;
  99.     //for access : DRIVER={Microsoft Access Driver (*.mdb)};DBQ=c:\path_to_access_db\base_test.mdb;UID=root;PWD=;
  100.     //for mssql : DRIVER={SQL Server};SERVER=myserver;UID=myuid;PWD=mypwd;DATABASE=OdbtpTest;
  101.     //if uid & pwd can be separate
  102.     function _connect($argHost, $argUsername='', $argPassword='', $argDSN='',$persist=false)
  103.     {
  104.         if ($argUsername) $argDSN .= ";uid=$argUsername";
  105.         if ($argPassword) $argDSN .= ";pwd=$argPassword";
  106.         
  107.         if ($persist) $this->_connectionID = @odbtp_rconnect($argHost,$argDSN);
  108.         else $this->_connectionID = @odbtp_connect($argHost,$argDSN);
  109.         
  110.         if ($this->_connectionID === false)
  111.         {
  112.             $this->_errorMsg = $this->ErrorMsg() ;
  113.             return false;
  114.         }
  115.         $this->odbc_driver = @odbtp_get_attr(ODB_ATTR_DRIVER, $this->_connectionID);
  116.         //$this->oic_level = @odbtp_get_attr(ODB_ATTR_OICLEVEL, $this->_connectionID);
  117.         $tc = @odbtp_get_attr('ODB_ATTR_TXNCAPABLE', $this->_connectionID);
  118.         if($tc == 0){
  119.             $this->hasTransactions = false;
  120.         }
  121.         else {
  122.             $this->hasTransactions = true;
  123.         }
  124.         
  125.         return true;
  126.     }
  127.     function BeginTrans()
  128.     {
  129.         if (!$this->hasTransactions) return false;
  130.         if ($this->transOff) return true;
  131.         $this->transCnt += 1;
  132.         $this->_autocommit = false;
  133.         $rs = @odbtp_set_attr(ODB_ATTR_TRANSACTIONS,ODB_TXN_READUNCOMMITTED,$this->_connectionID);
  134.         if(!$rs) return false;
  135.         else return true;
  136.     }
  137.     function CommitTrans($ok=true)
  138.     {
  139.         if ($this->transOff) return true;
  140.         if (!$ok) return $this->RollbackTrans();
  141.         if ($this->transCnt) $this->transCnt -= 1;
  142.         $this->_autocommit = true;
  143.         @odbtp_set_attr(ODB_ATTR_TRANSACTIONS,ODB_TXN_READCOMMITTED,$this->_connectionID);//set transaction off
  144.         $ret = odbtp_commit($this->_connectionID);
  145.         return $ret;
  146.     }
  147.     function RollbackTrans()
  148.     {
  149.         if ($this->transOff) return true;
  150.         if ($this->transCnt) $this->transCnt -= 1;
  151.         $this->_autocommit = true;
  152.         $ret = odbtp_rollback($this->_connectionID);
  153.         @odbtp_set_attr(ODB_ATTR_TRANSACTIONS,ODB_TXN_READCOMMITTED,$this->_connectionID);//set transaction off
  154.         return $ret;
  155.     }
  156.     function Prepare($sql)
  157.     {
  158.     //    return $sql;
  159.         $stmt = odbtp_prepare($sql);
  160.         if (!$stmt) return false;
  161.         return array($sql,$stmt);
  162.     }
  163.     
  164.     function _pconnect($argHost, $argUsername='', $argPassword='', $argDSN='')
  165.     {
  166.           return $this->_connect($argHost,$argUsername,$argPassword,$argDSN,true);
  167.     }
  168.     
  169.     function _close()
  170.     {
  171.         $ret = @odbtp_close($this->_connectionID);
  172.         $this->_connectionID = false;
  173.         return $ret;
  174.     }
  175.     function _query($sql,$inputarr=false)
  176.     {
  177.     GLOBAL $php_errormsg;
  178.         $php_errormsg = '';
  179.         $this->_error = '';
  180.  
  181.         if ($inputarr) {
  182.             if (is_array($sql)) {
  183.                 $stmtid = $sql[1];
  184.             } else {
  185.                 $stmtid = odtp_prepare($sql,$this->_connectionID);
  186.  
  187.                 if ($stmtid == false) {
  188.                     $this->_errorMsg = $php_errormsg;
  189.                     return false;
  190.                 }
  191.             }
  192.             if (! odbtp_execute($stmtid,$inputarr)) {
  193.                 return false;
  194.             }
  195.  
  196.         } else if (is_array($sql)) {
  197.             $stmtid = $sql[1];
  198.             if (!odbtp_execute($stmtid)) {
  199.                 return false;
  200.             }
  201.         } else
  202.         {
  203.             $stmtid = odbtp_query($sql,$this->_connectionID);
  204.         }
  205.         $this->_lastAffectedRows = 0;
  206.         if ($stmtid) {
  207.                 $this->_lastAffectedRows = odbtp_affected_rows($stmtid);
  208.         }
  209.         $this->_errorMsg = $php_errormsg;
  210.         return $stmtid;
  211.         //return true;
  212.     }
  213. }
  214. class ADORecordSet_odbtp extends ADORecordSet {
  215.     var $databaseType = 'odbtp';
  216.     var $canSeek = true;
  217.     function ADORecordSet_odbtp($queryID,$mode=false)
  218.     {
  219.         if ($mode === false) {
  220.             global $ADODB_FETCH_MODE;
  221.             $mode = $ADODB_FETCH_MODE;
  222.         }
  223.         $this->fetchMode = $mode;
  224.         $this->ADORecordSet($queryID);
  225.         //$this->_currentRow = 0;
  226.     }
  227.     function _seek($row)
  228.     {
  229.         return @odbtp_data_seek($this->_queryID, $row);
  230.     }
  231.     function &FetchField($fieldOffset = 0)
  232.     {
  233.         $off=$fieldOffset; // offsets begin at 0
  234.         $o= new ADOFieldObject();
  235.         $o->name = odbtp_field_name($this->_queryID,$off);
  236.         $o->type = odbtp_field_type($this->_queryID,$off);
  237.         strstr( PHP_OS, "WIN") ? $o->max_length = odbtp_field_length($this->_queryID,$off): $o->max_length = 'unknown';
  238.         if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name);
  239.         else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name);
  240.         return $o;
  241.     }
  242.     function fields($colname)
  243.     {
  244.         if ($this->fetchMode & ADODB_FETCH_ASSOC) return $this->fields[$colname];
  245.         if (!$this->bind) {
  246.             $this->bind = array();
  247.             for ($i=0; $i < $this->_numOfFields; $i++) {
  248.                 $o = $this->FetchField($i);
  249.                 $this->bind[strtoupper($o->name)] = $i;
  250.             }
  251.         }
  252.          return $this->fields[$this->bind[strtoupper($colname)]];
  253.     }
  254.     function _initrs()
  255.     {
  256.         $this->_numOfRows = -1; //odbtp_num_rows - odbtp_num_rows -- get number of fetched rows from query ;
  257.         $this->_numOfFields = odbtp_num_fields($this->_queryID);
  258.     }
  259.     function _fetch()
  260.     {
  261.         $f = odbtp_fetch_row($this->_queryID);
  262.         if ($f === false) {
  263.             $this->fields = false;
  264.             return false;
  265.         }
  266.         
  267.         $this->fields = $f;
  268.         if ($this->fetchMode & ADODB_FETCH_ASSOC) {
  269.             $this->fields = $this->GetRowAssoc(ADODB_ASSOC_CASE);
  270.         }
  271.     //    print_r($f);
  272.         //return $stmtid;
  273.         return true;
  274.   /*
  275.         global $ADODB_FETCH_MODE;
  276.         if ($this->fetchMode & ADODB_FETCH_ASSOC) {
  277.             echo 'am luat-o pe assoc';
  278.             $f = odbtp_fetch_assoc($this->_queryID);
  279.             //$this->fields = $this->GetRowAssoc(ADODB_ASSOC_CASE);
  280.             if ($f === false) {
  281.                 $this->fields = false;
  282.                 return false;
  283.             }
  284.         }
  285.         else
  286.         {
  287.             $f = @odbtp_fetch_row($this->_queryID);
  288.             if ($f === false) {
  289.                 $this->fields = false;
  290.                 return false;
  291.             }
  292.             $this->fields = $f;
  293.         }*/
  294.         print_r($f);
  295.         
  296.         return true;
  297.     }
  298.     function MoveFirst()
  299.     {
  300.       $this->fields = @odbtp_fetch_row($this->_queryID, "ODB_FETCH_FIRST");
  301.       if ($this->fields) $this->EOF = false;
  302.       $this->_currentRow = 0;
  303.  
  304.       if ($this->fetchMode == ADODB_FETCH_NUM) {
  305.          foreach($this->fields as $v) {
  306.             $arr[] = $v;
  307.          }
  308.          $this->fields = $arr;
  309.       }
  310.  
  311.       return true;
  312.     }
  313.     function MoveLast()
  314.    {
  315.       $this->fields = @odbtp_fetch_row($this->_queryID, "ODB_FETCH_LAST");
  316.       if ($this->fields) $this->EOF = false;
  317.       $this->_currentRow = -1;
  318.  
  319.       if ($this->fetchMode == ADODB_FETCH_NUM) {
  320.          foreach($this->fields as $v) {
  321.             $arr[] = $v;
  322.          }
  323.          $this->fields = $arr;
  324.       }
  325.  
  326.       return true;
  327.     }
  328.     function NextRecordSet($result)
  329.     {
  330.         return @odbtp_next_result($result);
  331.     }
  332.  
  333.     function _close()
  334.     {
  335.         return @odbtp_free_query($this->_queryID);
  336.     }
  337. }
  338.  
  339. ?>
  340.