home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / groupoffice-com-2.01 / database / oci8.class.inc < prev    next >
Text File  |  2004-03-08  |  9KB  |  331 lines

  1. <?php
  2.  
  3. /*
  4.  * Oracle/OCI8 accessor based on Session Management for PHP3
  5.  *
  6.  * (C) Copyright 1999-2000 Stefan Sels phplib@sels.com
  7.  *
  8.  * based on db_oracle.inc by Luis Francisco Gonzalez Hernandez
  9.  * contains metadata() from db_oracle.inc 1.10
  10.  *
  11.  * $Id: oci8.class.inc,v 1.1.1.1 2003/07/03 15:04:02 mschering Exp $
  12.  *
  13.  */
  14.  
  15. class db {
  16.   var $Debug    =  0;
  17.   var $sqoe     =  1; // sqoe= show query on error
  18.  
  19.   var $Database = "";
  20.   var $User     = "";
  21.   var $Password = "";
  22.  
  23.   var $Link_ID    = 0;
  24.   var $Record    = array();
  25.   var $Row;
  26.   var $Parse;
  27.   var $Error     = "";
  28.  
  29.   /* public: constructor */
  30.   function db($query = "") {
  31.       global $GO_CONFIG;
  32.       $this->Host = $GO_CONFIG->db_host;
  33.       $this->Database = $GO_CONFIG->db_name;
  34.       $this->User = $GO_CONFIG->db_user;
  35.         $this->Password = $GO_CONFIG->db_pass;
  36.       $this->query($query);
  37.   }
  38.  
  39.   function connect() {
  40.       if ( 0 == $this->Link_ID ) {
  41.           if($this->Debug) {
  42.               printf("<br>Connecting to $this->Database...<br>\n");
  43.           }
  44.           $this->Link_ID=OCIplogon
  45.                 ("$this->User","$this->Password","$this->Database");
  46.  
  47.           if (!$this->Link_ID) {
  48.               $this->halt("Link-ID == false " .
  49.                           "($this->Link_ID), OCILogon failed");
  50.           }
  51.  
  52.           if($this->Debug) {
  53.               printf("<br>Obtained the Link_ID: $this->Link_ID<br>\n");
  54.           }
  55.       }
  56.   }
  57.  
  58.   function query($Query_String) {
  59.  
  60.     /* No empty queries, please, since PHP4 chokes on them. */
  61.     if ($Query_String == "")
  62.       /* The empty query string is passed on from the constructor,
  63.        * when calling the class without a query, e.g. in situations
  64.        * like these: '$db = new DB_Sql_Subclass;'
  65.        */
  66.       return 0;
  67.  
  68.  
  69.       $this->connect();
  70.  
  71.        $this->Parse=OCIParse($this->Link_ID,$Query_String);
  72.       if(!$this->Parse) {
  73.            $this->Error=OCIError($this->Parse);
  74.       } else { OCIExecute($this->Parse);
  75.           $this->Error=OCIError($this->Parse);
  76.       }
  77.  
  78.       $this->Row=0;
  79.  
  80.       if($this->Debug) {
  81.           printf("Debug: query = %s<br>\n", $Query_String);
  82.       }
  83.  
  84.       if ($this->Error["code"]!=1403 && $this->Error["code"]!=0 && $this->sqoe)
  85.       echo "<BR><FONT color=red><B>".$this->Error["message"]."<BR>Query :\"$Query_String\"</B></FONT>";
  86.       return $this->Parse;
  87.   }
  88.  
  89.   function next_record() {
  90.       if(0 == OCIFetchInto($this->Parse,$result,OCI_ASSOC+OCI_RETURN_NULLS)) {
  91.           if ($this->Debug) {
  92.             printf("<br>ID: %d,Rows: %d<br>\n",
  93.               $this->Link_ID,$this->num_rows());
  94.           }
  95.           $this->Row        +=1;
  96.  
  97.           $errno=OCIError($this->Parse);
  98.           if(1403 == $errno) { # 1043 means no more records found
  99.               $this->Error="";
  100.               $this->disconnect();
  101.               $stat=0;
  102.           } else {
  103.               $this->Error=OCIError($this->Parse);
  104.               if($this->Debug) {
  105.                   printf("<br>Error: %s",
  106.                   $this->Error["message"]);
  107.               }
  108.               $stat=0;
  109.           }
  110.       } else {
  111.           for($ix=1;$ix<=OCINumcols($this->Parse);$ix++) {
  112.               $col=strtoupper(OCIColumnname($this->Parse,$ix));
  113.               $colreturn=strtolower($col);
  114.               $this->Record[ "$colreturn" ] = $result["$col"];
  115.               if($this->Debug) echo"<b>[$col]</b>:".$result["$col"]."<br>\n";
  116.           }
  117.           $stat=1;
  118.       }
  119.  
  120.   return $stat;
  121.   }
  122.  
  123.   function seek($pos) {
  124.       $this->Row=$pos;
  125.   }
  126.  
  127.   function metadata($table,$full=false) {
  128.       $count = 0;
  129.       $id    = 0;
  130.       $res   = array();
  131.  
  132.     /*
  133.      * Due to compatibility problems with Table we changed the behavior
  134.      * of metadata();
  135.      * depending on $full, metadata returns the following values:
  136.      *
  137.      * - full is false (default):
  138.      * $result[]:
  139.      *   [0]["table"]  table name
  140.      *   [0]["name"]   field name
  141.      *   [0]["type"]   field type
  142.      *   [0]["len"]    field length
  143.      *   [0]["flags"]  field flags ("NOT NULL", "INDEX")
  144.      *   [0]["format"] precision and scale of number (eg. "10,2") or empty
  145.      *   [0]["index"]  name of index (if has one)
  146.      *   [0]["chars"]  number of chars (if any char-type)
  147.      *
  148.      * - full is true
  149.      * $result[]:
  150.      *   ["num_fields"] number of metadata records
  151.      *   [0]["table"]  table name
  152.      *   [0]["name"]   field name
  153.      *   [0]["type"]   field type
  154.      *   [0]["len"]    field length
  155.      *   [0]["flags"]  field flags ("NOT NULL", "INDEX")
  156.      *   [0]["format"] precision and scale of number (eg. "10,2") or empty
  157.      *   [0]["index"]  name of index (if has one)
  158.      *   [0]["chars"]  number of chars (if any char-type)
  159.      *   ["meta"][field name]  index of field named "field name"
  160.      *   The last one is used, if you have a field name, but no index.
  161.      *   Test:  if (isset($result['meta']['myfield'])) {} ...
  162.      */
  163.  
  164.       $this->connect();
  165.  
  166.       ## This is a RIGHT OUTER JOIN: "(+)", if you want to see, what
  167.       ## this query results try the following:
  168.       ## $table = new Table; $db = new my_DB_Sql; # you have to make
  169.       ##                                          # your own class
  170.       ## $table->show_results($db->query(see query vvvvvv))
  171.       ##
  172.       $this->query("SELECT T.table_name,T.column_name,T.data_type,".
  173.            "T.data_length,T.data_precision,T.data_scale,T.nullable,".
  174.            "T.char_col_decl_length,I.index_name".
  175.            " FROM ALL_TAB_COLUMNS T,ALL_IND_COLUMNS I".
  176.            " WHERE T.column_name=I.column_name (+)".
  177.            " AND T.table_name=I.table_name (+)".
  178.            " AND T.table_name=UPPER('$table') ORDER BY T.column_id");
  179.  
  180.       $i=0;
  181.       while ($this->next_record()) {
  182.         $res[$i]["table"] =  $this->Record[table_name];
  183.         $res[$i]["name"]  =  strtolower($this->Record[column_name]);
  184.         $res[$i]["type"]  =  $this->Record[data_type];
  185.         $res[$i]["len"]   =  $this->Record[data_length];
  186.         if ($this->Record[index_name]) $res[$i]["flags"] = "INDEX ";
  187.         $res[$i]["flags"] .= ( $this->Record[nullable] == 'N') ? '' : 'NOT NULL';
  188.         $res[$i]["format"]=  (int)$this->Record[data_precision].",".
  189.                              (int)$this->Record[data_scale];
  190.         if ("0,0"==$res[$i]["format"]) $res[$i]["format"]='';
  191.         $res[$i]["index"] =  $this->Record[index_name];
  192.         $res[$i]["chars"] =  $this->Record[char_col_decl_length];
  193.         if ($full) {
  194.                 $j=$res[$i]["name"];
  195.                 $res["meta"][$j] = $i;
  196.                 $res["meta"][strtoupper($j)] = $i;
  197.         }
  198.         if ($full) $res["meta"][$res[$i]["name"]] = $i;
  199.         $i++;
  200.       }
  201.       if ($full) $res["num_fields"]=$i;
  202. #      $this->disconnect();
  203.       return $res;
  204.   }
  205.  
  206.  
  207.   function affected_rows() {
  208.     return $this->num_rows();
  209.   }
  210.  
  211.   function num_rows() {
  212.     return OCIrowcount($this->Parse);
  213.   }
  214.  
  215.   function num_fields() {
  216.       return OCINumcols($this->Parse);
  217.   }
  218.  
  219.   function nf() {
  220.     return $this->num_rows();
  221.   }
  222.  
  223.   function np() {
  224.     print $this->num_rows();
  225.   }
  226.  
  227.   function f($Name) {
  228.     if (is_object($this->Record[$Name]))
  229.     {
  230.       return $this->Record[$Name]->load();
  231.     } else
  232.     {
  233.       return $this->Record[$Name];
  234.     }
  235.   }
  236.  
  237.   function p($Name) {
  238.     print $this->f($Name);
  239.   }
  240.  
  241.   function nextid($seqname)
  242.   {
  243.     $this->connect();
  244.  
  245.     $Query_ID=@ociparse($this->Link_ID,"SELECT $seqname.NEXTVAL FROM DUAL");
  246.  
  247.     if(!@ociexecute($Query_ID))
  248.     {
  249.     $this->Error=@OCIError($Query_ID);
  250.     if($this->Error["code"]==2289)
  251.     {
  252.         $Query_ID=ociparse($this->Link_ID,"CREATE SEQUENCE $seqname");
  253.         if(!ociexecute($Query_ID))
  254.         {
  255.         $this->Error=OCIError($Query_ID);
  256.         $this->halt("<BR> nextid() function - unable to create sequence<br>".$this->Error["message"]);
  257.         } else
  258.          {
  259.         $Query_ID=ociparse($this->Link_ID,"SELECT $seqname.NEXTVAL FROM DUAL");
  260.         ociexecute($Query_ID);
  261.         }
  262.     }
  263.     }
  264.  
  265.     if (ocifetch($Query_ID))
  266.     {
  267.        $next_id = ociresult($Query_ID,"NEXTVAL");
  268.     } else
  269.     {
  270.        $next_id = 0;
  271.     }
  272.     ocifreestatement($Query_ID);
  273.     return $next_id;
  274.   }
  275.  
  276.   function disconnect() {
  277.       if($this->Debug) {
  278.           printf("Disconnecting...<br>\n");
  279.       }
  280.       OCILogoff($this->Link_ID);
  281.   }
  282.  
  283.   function halt($msg) {
  284.     printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
  285.     printf("<b>ORACLE Error</b>: %s<br>\n",
  286.       $this->Error["message"]);
  287.     die("Session halted.");
  288.   }
  289.  
  290.   function lock($table, $mode = "write") {
  291.     $this->connect();
  292.     if ($mode == "write") {
  293.       $Parse=OCIParse($this->Link_ID,"lock table $table in row exclusive mode");
  294.       OCIExecute($Parse);
  295.     } else {
  296.       $result = 1;
  297.     }
  298.     return $result;
  299.   }
  300.  
  301.   function unlock() {
  302.     return $this->query("commit");
  303.   }
  304.  
  305.   function table_names() {
  306.    $this->connect();
  307.    $this->query("
  308.    SELECT table_name,tablespace_name
  309.      FROM user_tables");
  310.    $i=0;
  311.    while ($this->next_record())
  312.    {
  313.    $info[$i]["table_name"]     =$this->Record["table_name"];
  314.    $info[$i]["tablespace_name"]=$this->Record["tablespace_name"];
  315.    $i++;
  316.    }
  317.   return $info;
  318.   }
  319.  
  320.   function add_specialcharacters($query)
  321.   {
  322.   return str_replace("'","''",$query);
  323.   }
  324.  
  325.   function split_specialcharacters($query)
  326.   {
  327.   return str_replace("''","'",$query);
  328.   }
  329. }
  330. ?>
  331.