"Fossies" - the Fresh Open Source Software Archive

Member "mythreads/lib/phplib/db_oci8.inc" (29 Mar 2001, 9128 Bytes) of package /linux/privat/mythreads-links_1.2.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) fasm source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file. For more information about "db_oci8.inc" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 1.2.0_vs_1.2.1.

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