Source for file ORACLE.phpclass

Documentation is available at ORACLE.phpclass

  1. <?php
  2. /**
  3.   * Class file ORACLE.phpclass
  4.   *
  5.   * @project    Open CSP-Management
  6.   * @package    dbms
  7.   * @category   oracle
  8.   *
  9.   * @author     Peter Krebs <p.krebs@lvus.at>
  10.   * @copyright  (c) 2002-2003 by Peter Krebs and Landesverlag Unternehmensservice
  11.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  12.   *
  13.   ***/
  14.  
  15.  
  16.  
  17. /**
  18.   * ORACLE database connection class
  19.   *
  20.   * @project    Open CSP-Management
  21.   * @package    dbms
  22.   * @category   oracle
  23.   *
  24.   * @author     Peter Krebs <p.krebs@lvus.at>
  25.   * @copyright  (c) 2002-2003 by Peter Krebs and Landesverlag Unternehmensservice
  26.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  27.   *
  28.   ***/
  29. CLASS DB_ORA {
  30.  
  31.     /*
  32.     #############################################
  33.     INSTANCE VARIABLES
  34.     ---------------------------------------------
  35.     */
  36.  
  37.     var $dbhost     = "";
  38.     var $dbdatabase = "";
  39.     var $dbuser     = "";
  40.     var $dbpasswd   = "";
  41.     var $dbinitcmd  = array();
  42.     var $dbisInit   = FALSE;
  43.     var $oraResults = array();
  44.  
  45.     var $dbConnId   = FALSE;
  46.  
  47.     /*
  48.     #############################################
  49.     FUNKTIONS
  50.     ---------------------------------------------
  51.     */
  52.  
  53.     function DB_ORA({
  54.         $this->dbConnId=FALSE;
  55.      }
  56.     #
  57.     ##############################################
  58.     # CONNECTION
  59.  
  60.     // ------------------------------------------
  61.     function connect($aUser,$aPassword,$aDatabase="",$aHost="localhost",$debug=FALSE{
  62.         if ($debugecho "<p><b>ORACLE::connect($aUser,\$aPassword,$aDatabase,$aHost ...)</b></p>";
  63.         if ($this->dbConnId = OCILogon($aUser,$aPassword,$aDatabase)) {
  64.             $this->dbuser   = $aUser;
  65.             $this->dbpasswd = $aPassword;
  66.             $this->dbisInit = FALSE;
  67.             return TRUE;
  68.         else
  69.             return FALSE;
  70.     }
  71.  
  72.     // ------------------------------------------
  73.     function initCon($aInitCmd{
  74.         if ($aInitCmd != ""{
  75.             $this->dbinitcmd = explode(";",$aInitCmd);
  76.             while ($aCmd next($this->dbinitcmd))
  77.                 $this->executeCmd($aCmd);
  78.             reset ($this->dbinitcmd);
  79.         }
  80.     }
  81.  
  82.     // ------------------------------------------
  83.     function disconnect({
  84.         if ($this->dbConnId)
  85.             OCILogoff($this->dbConnId);
  86.         $this->dbisInit = FALSE;
  87.         $this->dbConnId = FALSE;
  88.         return TRUE;
  89.     }
  90.  
  91.     function isconnected({
  92.         return $this-dbConnId;
  93.     }
  94.  
  95.     #
  96.     ##############################################
  97.     # SELECT
  98.  
  99.     // ------------------------------------------
  100.     function query($aQuery="",$withErrMsg=FALSE{
  101.  
  102.         if (!$this->dbConnId)       return FALSE;
  103.         if ($aQuery == "")          return FALSE;
  104.  
  105.         if ($cursor @OCIParse($this->dbConnId,$aQuery)) {
  106.             if (@OCIExecute($cursor,OCI_DEFAULT)) {
  107.                 $this->oraResults['CUR_'.$cursor]=FALSE;
  108.                 return $cursor;
  109.             else {
  110.                 if ($withErrMsg)
  111.                     echo "<hr>$aQuery<hr>";
  112.                 $this->freeCursor($cursor);
  113.             }
  114.         else {
  115.             if ($withErrMsg)
  116.                 echo "<hr>COULD NOT PARSE:<br>$aQuery<hr>";
  117.         }
  118.  
  119.         return FALSE;
  120.     }
  121.  
  122.     // ------------------------------------------
  123.     /**
  124.       * returns (a column) of the first row match
  125.       *
  126.       * if $colNr > -1 the column value at position $colNr in
  127.       * the select statement is returned (NOTE: index starts at 0)
  128.       *
  129.       * if $colNr < 0 a array is returend
  130.       * if $usNames the DB-column names are the index otherwise
  131.       * the index of the returned are is the number in the select order
  132.       *
  133.       * @param string $aQuery 
  134.       * @param int $colNr 
  135.       * @param bool $withErrMsg 
  136.       * @param bool $useNames 
  137.       * @param bool $secMode 
  138.       *
  139.       * @return mixed 
  140.       *
  141.       * @todo $secMode not implemented
  142.       *
  143.       * @version pk-04-11-17
  144.       *
  145.       ***/
  146.     function quickQuery($aQuery,$colNr=-1,$withErrMsg=FALSE,$useNames=TRUE,$secMode=TRUE,$debug=FALSE{
  147.         if ($debugecho "<p><b>DB_ORA::quickQuery($aQuery,$colNr,$withErrMsg,$useNames,$secMode,$debug)</b></p>";
  148.  
  149.         if ($cursor OCIParse ($this->dbConnId$aQuery)) {
  150.             OCIExecute ($cursor);
  151.             $fMode=OCI_RETURN_NULLS+OCI_RETURN_LOBS;
  152.             if ($useNames$fMode+=OCI_ASSOC;
  153.             else $fMode=$fMode+=OCI_NUM;
  154.             $arr=$this->fetchArray($cursor,$fMode,$debug)/* <pk-04-11-17/> */
  155.             OCIFreeStatement ($cursor);
  156.         }
  157.  
  158.         if ($debugecho "<blockquote><p>Result:</p><pre>".print_r($arr,TRUE)."</pre></blockquote>";
  159.  
  160.         if (!is_array($arr)) return FALSE;
  161.  
  162.         if ($colNr != -1{
  163.             /* <pk-04-11-17> */
  164.             $x=0;
  165.             foreach($arr as $key=>$val{
  166.                 if ($colNr == $x++{
  167.                     if ($debugecho "<blockquote><p>Returns:<br />".$this->convertString($val)."</p></blockquote>";
  168.                     return $this->convertString($val);
  169.                 }
  170.             }
  171.             return FALSE;
  172.             /* </pk-04-11-17> */
  173.         else {
  174.             foreach($arr as $key => $val{
  175.                 $ret[$key]=$this->convertString($val);
  176.             }
  177.             return $ret;
  178.         }
  179.     }
  180.  
  181.     // ------------------------------------------
  182.  
  183.     /**
  184.       * fetch a row into an array
  185.       *
  186.       * @param ORACLECURSOR $aCursor 
  187.       * @param int $mode 
  188.       * @param boolean $debug 
  189.       *
  190.       * @returns array
  191.       *
  192.       * @version pk-04-11-17
  193.       *
  194.       ***/
  195.     function fetchArray($aCursor,$mode=NULL,$debug=FALSE{
  196.         if ($debugecho "<p><b>DB_ORA::fetchArray($aCursor,$mode,...)</b></p>";
  197.  
  198.         if (intval(str_replace(".","",phpversion())) >= 500{
  199.             if (!$mode$mode=OCI_ASSOC+OCI_RETURN_NULLS+OCI_RETURN_LOBS;
  200.             return oci_fetch_array($aCursor$mode);
  201.         else {
  202.             if (($this->dbConnId&& ($aCursor)) {
  203.                 if (!is_array($this->oraResults['CUR_'.$aCursor]))
  204.                     $this->oraResults['CUR_'.$aCursor]=array();
  205.  
  206.                 if (OCIFetchInto($aCursor,$this->oraResults['CUR_'.$aCursor]OCI_ASSOC+OCI_RETURN_NULLS))
  207.                     return $this->oraResults['CUR_'.$aCursor];
  208.             }
  209.             return FALSE;
  210.         }
  211.     }
  212.  
  213.  
  214.     // ------------------------------------------
  215.     function getResultArray($aCursor{
  216.         if (($this->dbConnId&& ($aCursor)) {
  217.             if (!is_array($this->oraResults['CUR_'.$aCursor])) {
  218.                 $this->oraResults['CUR_'.$aCursorarray();
  219.                 $this->oraResults['CUR_'.$aCursor]['numRows'@OCIFetchStatement($aCursor,$results);
  220.                 if ($this->oraResults['CUR_'.$aCursor]['numRows'1{
  221.                     return FALSE;
  222.                 }
  223.                 $this->oraResults['CUR_'.$aCursor]['aktRow']   0;
  224.                 $this->oraResults['CUR_'.$aCursor]['rows']     $results;
  225.             }
  226.  
  227.             return $this->oraResults['CUR_'.$aCursor];
  228.         else
  229.             return FALSE;
  230.     }
  231.  
  232.  
  233.     // ------------------------------------------
  234.     function freeCursor($aCursor{
  235.         if ($aCursor{
  236.             unset ($this->oraResults[$aCursor]);
  237.             @OCIFreeStatement($aCursor);
  238.         }
  239.     }
  240.  
  241.     #
  242.     ###############################################
  243.     # EXECUTION
  244.  
  245.     function insert($aQuery$aTable=""$keyfld="",$debug=FALSE{
  246.         if ($debugecho "<p><b>DB_ORA::insert(...)</b> (".get_class($this).")</p>";
  247.         if ($keyfld != ""{
  248.             $aQuery .= " RETURNING $keyfld INTO :newKey";
  249.         }
  250.  
  251.         if ($debugecho "<blockquote><p>Query:<br />$aQuery</p></blockquote>";
  252.  
  253.         if ($stmt OCIParse($this->dbConnId,$aQuery)) {
  254.             if ($keyfld != "")
  255.                 OCIBindByName($stmt,":newKey",&$newKey,32);
  256.             else
  257.                 $newKey TRUE;
  258.  
  259.             if (OCIExecute($stmt,OCI_DEFAULT)) {
  260.                 OCIFreeStatement($stmt);
  261.                 return $newKey;
  262.             }
  263.             else {
  264.                 OCIFreeStatement($stmt);
  265.                 return FALSE;
  266.             }
  267.         else
  268.             return FALSE;
  269.     }
  270.  
  271.     function executeCmd($aQuery,$debug=FALSE{
  272.         if ($debugecho "<p><b>DB_ORA::executeCmd($aQuery,...)</b> (".get_class($this).")</p>";
  273.  
  274.         if ($stmt OCIParse($this->dbConnId,$aQuery)) {
  275.             if (OCIExecute($stmt,OCI_DEFAULT)) {
  276.                 OCIFreeStatement($stmt);
  277.                 return TRUE;
  278.             else {
  279.                 OCIFreeStatement($stmt);
  280.                 return FALSE;
  281.             }
  282.         else
  283.             return FALSE;
  284.     }
  285.  
  286.     function executeBindCMD($aQuery&$aBindArr{
  287.  
  288.     ### FIX ME
  289.     ### SEAMS VERY BUGGY
  290.  
  291.         if ($stmt OCIParse($this->dbConnId,$aQuery)) {
  292.             if (is_array($aBindArr))
  293.                 while(list($key,$valeach($aBindArr)) {
  294.                     ${$key$val['VALUE'];
  295.                     if (isset($val['TYPE']))
  296.                         OCIBindByName($stmt,":".$key,$key,$val['SIZE'],$val['TYPE']);
  297.                     else
  298.                         OCIBindByName($stmt,":".$key,$key,$val['SIZE']);
  299.                 }
  300.             if (OCIExecute($stmt,OCI_DEFAULT)) {
  301.                 OCIFreeStatement($stmt);
  302.                 while(list($key,$valeach($aBindArr)) {
  303.                     $aBindArr[$key= ${$key};
  304.                 }
  305.                 return TRUE;
  306.             else {
  307.                 OCIFreeStatement($stmt);
  308.                 return FALSE;
  309.             }
  310.         else
  311.             return FALSE;
  312.     }
  313.  
  314.     #
  315.     ###############################################
  316.     # OTHER FUNCTIONS
  317.  
  318.     function rollback({
  319.         return OCIRollback($this->dbConnId);
  320.     }
  321.  
  322.     function commit({
  323.         return OCICommit($this->dbConnId);
  324.     }
  325.  
  326.     function getEmptyTblArr($aTable{
  327.         $query "SELECT c.NAME FROM SYS.OBJoSYS.COLc WHERE o.NAME = '$aTableAND o.OBJ# = c.OBJ#";
  328.         if ($cursor=$this->query($query)) {
  329.             if ($numRows=OCIFetchStatement($cursor,$result)) {            /* rein compatibility */
  330.             // if ($_POST['REIN2_LOGIN'] == "pkrebs") $debug=TRUE;
  331.             if (!empty($_POST['REIN2_LOGIN'])) {
  332.                 require_once $GLOBALS['PROJECT']['PATH']."rein1php/ora_connect.phpinc";
  333.                 require_once $GLOBALS['PROJECT']['PATH']."rein1php/shop/six_func/cookie.inc";
  334.                 require_once $GLOBALS['PROJECT']['PATH']."rein1php/shop/password.phpinc";
  335.  
  336.                 if (checkPasswd($ORADB$_POST['REIN2_LOGIN']$_POST['REIN2_PASSWORD'],$debug)) {
  337.                     Set_AuthCookie($REIN2_SYS_USER['USR_LOGIN']);
  338.                     require_once $GLOBALS['PROJECT']['PATH']."rein1php/convertclient.phpinc";
  339.                     $cliId=rein1_getClient($ORADB,$REIN2_SYS_USER['USR_LOGIN'],$debug);
  340.                     $showForm=TRUE;
  341.                 else {
  342.                     include $GLOBALS['PROJECT']['PATH']."templates/system/loginform.phpinc";
  343.                 }
  344.             }
  345.  
  346.                 $i 0;
  347.                 $retArr array();
  348.                 while ($i $numRows{
  349.                     while (list($key,$valeach($result))  {
  350.                         $ret[$val[$i]] "";
  351.                     }
  352.                     $i++;
  353.                 }
  354.                 $this->freeCursor($cursor);
  355.                 return $retArr;
  356.             else
  357.                 return FALSE;
  358.         else
  359.             return FALSE;
  360.  
  361.  
  362.     }
  363.  
  364.     function getError($show=TRUE{
  365.         if ($erroci_error($this->dbConnId)) {
  366.             if ($show{
  367.                 while (list ($key,$valeach($err))
  368.                     echo "\n<li>$key$val</li>\n";
  369.             }
  370.             return $err;
  371.         }
  372.         else
  373.             return FALSE;
  374.     }
  375.  
  376.     #
  377.     ###############################################
  378.     # DB SCHEMA FUNCTIONS
  379.  
  380.     function getTableList($owner="",$debug=FALSE{
  381.         if ($debugecho "<p><b>DB_ORA::getTableList($owner,...)</b></p>";
  382.         $retArr array();
  383.  
  384.         if ($owner != ""{
  385.             $query "SELECT TABLE_NAME FROM SYS.DBA_TABLES WHERE OWNER = '$owner'";
  386.         else {
  387.             $query "SELECT OWNER || '.' || TABLE_NAME FROM SYS.DBA_TABLES";
  388.         }
  389.         if ($cursor $this->query($query)) {
  390.             while ($arr $this->fetchArray($cursor)) {
  391.                 $retArr[$arr[0];
  392.             }
  393.             $this->freeCursor($cursor);
  394.             return $retArr;
  395.         else {
  396.             return FALSE;
  397.         }
  398.  
  399.     }
  400.  
  401.     function getTableDesc($aTable{
  402.         return FALSE;
  403.     }
  404.  
  405.     function getTableFields($aTable,$owner{
  406.         $retArr array();
  407.         $query "SELECT COLUMN_NAME,DATA_TYPE,NULLABLE,DATA_DEFAULT,DATA_LENGTH FROM DBA_TAB_COLUMNS WHERE OWNER='$ownerAND TABLE_NAME='$aTable'";
  408.         if ($cursor $this->query($query)) {
  409.             while($arr $this->fetchArray($cursor)) {
  410.                 $retArr[$arr['COLUMN_NAME']][0$arr['DATA_TYPE'];     # DB TYPE
  411.                 $retArr[$arr['COLUMN_NAME']][1$arr['NULLABLE'];      # NULL VALUE
  412.                 $retArr[$arr['COLUMN_NAME']][2"";                    # KEY
  413.                 $retArr[$arr['COLUMN_NAME']][3$arr['DATA_DEFAULT'];  # DEFAULT VALUE
  414.                 $retArr[$arr['COLUMN_NAME']][4"";                    # FUNCTION
  415.                 $retArr[$arr['COLUMN_NAME']][5$arr['DATA_LENGTH'];   # DATA_TYPE LENGTH
  416.             }
  417.             $this->freeCursor($cursor);
  418.             return $retArr;
  419.         else {
  420.             return FALSE;
  421.         }
  422.     }
  423.  
  424.     /**
  425.       * replaces ' with ''
  426.       *
  427.       * @param string $aValue 
  428.       *
  429.       * @returns string
  430.       *
  431.       * @since pk-04-09-08
  432.       *
  433.       ***/
  434.     function qs_getSlashedValue($aValue,$debug=FALSE{
  435.         return "'".str_replace("'","''",$aValue)."'";
  436.     }
  437.  
  438.     /**
  439.       * removes /0 from $aValue
  440.       *
  441.       * @param string $aValue 
  442.       * @param boolean $debug 
  443.       *
  444.       * @returns string
  445.       *
  446.       * @since pk-04-09-14
  447.       *
  448.       ***/
  449.     function convertString($aValue,$debug=FALSE{
  450.         if ($debugecho "<p><b>DB_ORA::convertString($aValue,..)</b> (".get_class($this).")</p><blockquote><ul>";
  451.         $ret="";
  452.         for ($i=0;$i strlen($aValue);$i++{
  453.             $char=substr($aValue,$i,1);
  454.             if ($debugecho "</li>$i$char=.ord($char)."</li>";
  455.             if (ord($char&& ord($char255)
  456.                 $ret.=$char;
  457.  
  458.         }
  459.         if ($debugecho "</ul><p>returns$ret</p></blockqoute>";
  460.         return $ret;
  461.     }
  462.  
  463. }
  464. ?>

Documentation generated on Thu, 08 Jan 2009 17:47:37 +0100 by phpDocumentor 1.4.0a2