home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / adodb-oracle.inc.php < prev    next >
Encoding:
PHP Script  |  2004-03-20  |  8.0 KB  |  299 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.
  7.  
  8.   Latest version is available at http://php.weblogs.com/
  9.   
  10.   Oracle data driver. Requires Oracle client. Works on Windows and Unix and Oracle 7 and 8.
  11.   
  12.   If you are using Oracle 8, use the oci8 driver which is much better and more reliable.
  13.   
  14. */
  15.  
  16. class ADODB_oracle extends ADOConnection {
  17.     var $databaseType = "oracle";
  18.     var $replaceQuote = "''"; // string to use to replace quotes
  19.     var $concat_operator='||';
  20.     var $_curs;
  21.     var $_initdate = true; // init date to YYYY-MM-DD
  22.     var $metaTablesSQL = 'select table_name from cat';    
  23.     var $metaColumnsSQL = "select cname,coltype,width from col where tname='%s' order by colno";
  24.     var $sysDate = "TO_DATE(TO_CHAR(SYSDATE,'YYYY-MM-DD'),'YYYY-MM-DD')";
  25.     var $sysTimeStamp = 'SYSDATE';
  26.     var $connectSID = true;
  27.     
  28.     function ADODB_oracle() 
  29.     {
  30.     }
  31.  
  32.     // format and return date string in database date format
  33.     function DBDate($d)
  34.     {
  35.         if (is_string($d)) $d = ADORecordSet::UnixDate($d);
  36.         return 'TO_DATE('.adodb_date($this->fmtDate,$d).",'YYYY-MM-DD')";
  37.     }
  38.     
  39.     // format and return date string in database timestamp format
  40.     function DBTimeStamp($ts)
  41.     {
  42.  
  43.         if (is_string($ts)) $d = ADORecordSet::UnixTimeStamp($ts);
  44.         return 'TO_DATE('.adodb_date($this->fmtTimeStamp,$ts).",'RRRR-MM-DD, HH:MI:SS AM')";
  45.     }
  46.  
  47.     
  48.     function BeginTrans()
  49.     {      
  50.          $this->autoCommit = false;
  51.          ora_commitoff($this->_connectionID);
  52.          return true;
  53.     }
  54.  
  55.     
  56.     function CommitTrans($ok=true) 
  57.     { 
  58.            if (!$ok) return $this->RollbackTrans();
  59.            $ret = ora_commit($this->_connectionID);
  60.            ora_commiton($this->_connectionID);
  61.            return $ret;
  62.     }
  63.  
  64.     
  65.     function RollbackTrans()
  66.     {
  67.         $ret = ora_rollback($this->_connectionID);
  68.         ora_commiton($this->_connectionID);
  69.         return $ret;
  70.     }
  71.  
  72.  
  73.     /* there seems to be a bug in the oracle extension -- always returns ORA-00000 - no error */
  74.     function ErrorMsg() 
  75.      {
  76.         $this->_errorMsg = @ora_error($this->_curs);
  77.          if (!$this->_errorMsg) $this->_errorMsg = @ora_error($this->_connectionID);
  78.         return $this->_errorMsg;
  79.     }
  80.  
  81.  
  82.     function ErrorNo() 
  83.     {
  84.         $err = @ora_errorcode($this->_curs);
  85.         if (!$err) return @ora_errorcode($this->_connectionID);
  86.     }
  87.  
  88.     
  89.  
  90.         // returns true or false
  91.         function _connect($argHostname, $argUsername, $argPassword, $argDatabasename, $mode=0)
  92.         {
  93.             // G. Giunta 2003/08/13 - This looks danegrously suspicious: why should we want to set
  94.             // the oracle home to the host name of remote DB?
  95. //            if ($argHostname) putenv("ORACLE_HOME=$argHostname");
  96.  
  97.             if($argHostname) { // code copied from version submitted for oci8 by Jorma Tuomainen <jorma.tuomainen@ppoy.fi>
  98.                 if (empty($argDatabasename)) $argDatabasename = $argHostname;
  99.                 else {
  100.                     if(strpos($argHostname,":")) {
  101.                         $argHostinfo=explode(":",$argHostname);
  102.                         $argHostname=$argHostinfo[0];
  103.                         $argHostport=$argHostinfo[1];
  104.                     } else {
  105.                         $argHostport="1521";
  106.                     }
  107.  
  108.  
  109.                     if ($this->connectSID) {
  110.                         $argDatabasename="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=".$argHostname
  111.                         .")(PORT=$argHostport))(CONNECT_DATA=(SID=$argDatabasename)))";
  112.                     } else
  113.                         $argDatabasename="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=".$argHostname
  114.                         .")(PORT=$argHostport))(CONNECT_DATA=(SERVICE_NAME=$argDatabasename)))";
  115.                 }
  116.  
  117.             }
  118.  
  119.             if ($argDatabasename) $argUsername .= "@$argDatabasename";
  120.  
  121.         //if ($argHostname) print "<p>Connect: 1st argument should be left blank for $this->databaseType</p>";
  122.             if ($mode = 1)
  123.                 $this->_connectionID = ora_plogon($argUsername,$argPassword);
  124.             else
  125.                 $this->_connectionID = ora_logon($argUsername,$argPassword);
  126.             if ($this->_connectionID === false) return false;
  127.             if ($this->autoCommit) ora_commiton($this->_connectionID);
  128.             if ($this->_initdate) {
  129.                 $rs = $this->_query("ALTER SESSION SET NLS_DATE_FORMAT='YYYY-MM-DD'");
  130.                 if ($rs) ora_close($rs);
  131.             }
  132.  
  133.             return true;
  134.         }
  135.  
  136.  
  137.         // returns true or false
  138.         function _pconnect($argHostname, $argUsername, $argPassword, $argDatabasename)
  139.         {
  140.             return $this->_connect($argHostname, $argUsername, $argPassword, $argDatabasename, 1);
  141.         }
  142.  
  143.  
  144.         // returns query ID if successful, otherwise false
  145.         function _query($sql,$inputarr=false)
  146.         {
  147.             $curs = ora_open($this->_connectionID);
  148.          
  149.              if ($curs === false) return false;
  150.             $this->_curs = $curs;
  151.             if (!ora_parse($curs,$sql)) return false;
  152.             if (ora_exec($curs)) return $curs;
  153.         
  154.              @ora_close($curs);
  155.             return false;
  156.         }
  157.  
  158.  
  159.         // returns true or false
  160.         function _close()
  161.         {
  162.             return @ora_logoff($this->_connectionID);
  163.         }
  164.  
  165.  
  166.  
  167. }
  168.  
  169.  
  170. /*--------------------------------------------------------------------------------------
  171.          Class Name: Recordset
  172. --------------------------------------------------------------------------------------*/
  173.  
  174. class ADORecordset_oracle extends ADORecordSet {
  175.  
  176.     var $databaseType = "oracle";
  177.     var $bind = false;
  178.  
  179.     function ADORecordset_oracle($queryID,$mode=false)
  180.     {
  181.         
  182.         if ($mode === false) { 
  183.             global $ADODB_FETCH_MODE;
  184.             $mode = $ADODB_FETCH_MODE;
  185.         }
  186.         $this->fetchMode = $mode;
  187.         
  188.         $this->_queryID = $queryID;
  189.     
  190.         $this->_inited = true;
  191.         $this->fields = array();
  192.         if ($queryID) {
  193.             $this->_currentRow = 0;
  194.             $this->EOF = !$this->_fetch();
  195.             @$this->_initrs();
  196.         } else {
  197.             $this->_numOfRows = 0;
  198.             $this->_numOfFields = 0;
  199.             $this->EOF = true;
  200.         }
  201.         
  202.         return $this->_queryID;
  203.     }
  204.  
  205.  
  206.  
  207.        /*        Returns: an object containing field information.
  208.                Get column information in the Recordset object. fetchField() can be used in order to obtain information about
  209.                fields in a certain query result. If the field offset isn't specified, the next field that wasn't yet retrieved by
  210.                fetchField() is retrieved.        */
  211.  
  212.        function FetchField($fieldOffset = -1)
  213.        {
  214.             $fld = new ADOFieldObject;
  215.             $fld->name = ora_columnname($this->_queryID, $fieldOffset);
  216.             $fld->type = ora_columntype($this->_queryID, $fieldOffset);
  217.             $fld->max_length = ora_columnsize($this->_queryID, $fieldOffset);
  218.             return $fld;
  219.        }
  220.  
  221.     /* Use associative array to get fields array */
  222.     function Fields($colname)
  223.     {
  224.         if (!$this->bind) {
  225.             $this->bind = array();
  226.             for ($i=0; $i < $this->_numOfFields; $i++) {
  227.                 $o = $this->FetchField($i);
  228.                 $this->bind[strtoupper($o->name)] = $i;
  229.             }
  230.         }
  231.         
  232.          return $this->fields[$this->bind[strtoupper($colname)]];
  233.     }
  234.     
  235.    function _initrs()
  236.    {
  237.            $this->_numOfRows = -1;
  238.            $this->_numOfFields = @ora_numcols($this->_queryID);
  239.    }
  240.  
  241.  
  242.    function _seek($row)
  243.    {
  244.            return false;
  245.    }
  246.  
  247.    function _fetch($ignore_fields=false) {
  248. // should remove call by reference, but ora_fetch_into requires it in 4.0.3pl1
  249.         if ($this->fetchMode & ADODB_FETCH_ASSOC)
  250.             return @ora_fetch_into($this->_queryID,&$this->fields,ORA_FETCHINTO_NULLS|ORA_FETCHINTO_ASSOC);
  251.            else 
  252.             return @ora_fetch_into($this->_queryID,&$this->fields,ORA_FETCHINTO_NULLS);
  253.    }
  254.  
  255.    /*        close() only needs to be called if you are worried about using too much memory while your script
  256.            is running. All associated result memory for the specified result identifier will automatically be freed.        */
  257.  
  258.    function _close() 
  259. {
  260.            return @ora_close($this->_queryID);
  261.    }
  262.  
  263.     function MetaType($t,$len=-1)
  264.     {
  265.         if (is_object($t)) {
  266.             $fieldobj = $t;
  267.             $t = $fieldobj->type;
  268.             $len = $fieldobj->max_length;
  269.         }
  270.         
  271.         switch (strtoupper($t)) {
  272.         case 'VARCHAR':
  273.         case 'VARCHAR2':
  274.         case 'CHAR':
  275.         case 'VARBINARY':
  276.         case 'BINARY':
  277.                 if ($len <= $this->blobSize) return 'C';
  278.         case 'LONG':
  279.         case 'LONG VARCHAR':
  280.         case 'CLOB':
  281.         return 'X';
  282.         case 'LONG RAW':
  283.         case 'LONG VARBINARY':
  284.         case 'BLOB':
  285.                 return 'B';
  286.         
  287.         case 'DATE': return 'D';
  288.         
  289.         //case 'T': return 'T';
  290.         
  291.         case 'BIT': return 'L';
  292.         case 'INT': 
  293.         case 'SMALLINT':
  294.         case 'INTEGER': return 'I';
  295.         default: return 'N';
  296.         }
  297.     }
  298. }
  299. ?>