[PHPLIB-DEV] cvs commit From: ssilk (phplib-dev <email protected>)
Date: 11/17/99

From: ssilk
Date: Thu Nov 18 01:26:33 1999
Modified files:
      php-lib/php/db_oracle.inc

Log message:
Slight modifications, num_rows() works now also with subqueries

Index: php-lib/php/db_oracle.inc
diff -u php-lib/php/db_oracle.inc:1.17 php-lib/php/db_oracle.inc:1.18
--- php-lib/php/db_oracle.inc:1.17 Tue Oct 26 15:33:37 1999
+++ php-lib/php/db_oracle.inc Thu Nov 18 01:26:02 1999
@@ -4,7 +4,7 @@
  *
  * Copyright (c) 1998,1999 Luis Francisco Gonzalez Hernandez
  *
- * $Id: db_oracle.inc,v 1.17 1999/10/26 13:33:37 kir Exp $
+ * $Id: db_oracle.inc,v 1.18 1999/11/18 00:26:02 ssilk Exp $
  *
  */
 
@@ -12,9 +12,16 @@
   var $Debug = false;
   var $Home = "/u01/app/oracle/product/8.0.4";
   var $Remote = 1;
+ /* This Query will be sent directly after the first connection
+ Example:
+ var $ConnectQuery="ALTER SESSION SET nls_date_language=german nls_date_format='DD.MM.RRRR'";
+ -> Set the date format for this session, this is fine when your ora-role
+ cannot be altered */
+ var $ConnectQuery='';
   /* Due to a strange error with Oracle 8.0.5, Apache and PHP3.0.6
      you don't need to set the ENV - on my system Apache
      will change to a zombie, if I don't set this to FALSE!
+ Instead I set these ENV-vars before the startup of apache.
      If unsure try it out, if it works. */
   var $OraPutEnv = true;
 
@@ -35,7 +42,7 @@
   /* copied from db_mysql for completeness */
   /* public: identification constant. never change this. */
   var $type = "oracle";
- var $revision = "Revision: 1.2";
+ var $revision = "Revision: 1.3";
   var $Halt_On_Error = "yes"; ## "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore errror, but spit a warning)
 
   /* public: constructor */
@@ -92,7 +99,11 @@
         }
         if($this->Debug) {
           printf("<br>connect() Obtained the Link_ID: $this->Link_ID<br>\n");
- }
+ }
+ ## Execute Connect Query
+ if ($this->ConnectQuery) {
+ $this->query($this->ConnectQuery);
+ }
       }
   }
   
@@ -141,8 +152,11 @@
       if (!$this->no_next_fetch &&
           0 == ora_fetch($this->Query_ID)) {
           if ($this->Debug) {
- printf("<br>next_record(): ID: %d,Rows: %d<br>\n",
- $this->Query_ID,$this->num_rows());
+ printf("<br>next_record(): ID: %d Row: %d<br>\n",
+ $this->Query_ID,$this->Row+1);
+ // more info for $this->Row+1 is $this->num_rows(),
+ // but dosn't work in all cases (complicated selects)
+ // and it is very slow here
           }
           $this->Row +=1;
           
@@ -169,7 +183,7 @@
               $value=ora_getcolumn($this->Query_ID,$ix);
               $this->Record[ "$col" ] = $value;
               $this->Record[ $ix ] = $value;
-# echo"<b>[$col]</b>: $value <br>\n";
+#DBG echo"<b>[$col]</b>: $value <br>\n";
           }
           $stat=1;
       }
@@ -187,6 +201,7 @@
         ## do nothing
       } else {
         $this->halt("Invalid seek(): Position is cannot be handled by API.<BR>".
+ "Only a seek to the last element is allowed in this version<BR>".
               "Difference too big. Wanted: $pos Current pos: $this->Row");
       }
       if ($this->Debug) echo "<BR>Debug: seek = $pos<BR>";
@@ -206,6 +221,8 @@
     return ora_do($this->Link_ID, "commit");
   }
 
+ // Important note: This function dosn't work with Oracle-Database-Links!
+ // You are free to get a better method. :)
   function metadata($table,$full=false) {
       $count = 0;
       $id = 0;
@@ -294,24 +311,35 @@
   ## Known bugs: It will not work for SELECT DISTINCT and any
   ## other constructs which are depending on the resulting rows.
   ## So you *really need* to check every query you make, if it
- ## will work with it.
+ ## will work with it!
   ##
   ## Also, for a qualified replacement you need to parse the
   ## selection, cause this will fail: "SELECT id, from FROM ...").
- ## "FROM" is - as far as I know a keyword in Oracle, so it can
+ ## "from" is - as far as I know a keyword in Oracle, so it can
   ## only be used in this way. But you have been warned.
   function num_rows() {
     $curs=ora_open($this->Link_ID);
 
     ## this is the important part and it is also the HACK!
     if (eregi("^[[:space:]]*SELECT[[:space:]]",$this->lastQuery) ) {
- $from_pos = strpos(strtoupper($this->lastQuery),"FROM");
- $q = "SELECT count(*) ". substr($this->lastQuery, $from_pos);
+ # works also for subselects:
+ if (eregi("[[:space:]]+FROM([[:space:]]+.*[[:space:]]+FROM)",$this->lastQuery,$r))
+ $areplace=$r[1];
+ $q=eregi_Replace("^[[:space:]]*SELECT[[:space:]]+".
+ ".*[[:space:]]+FROM",
+ "SELECT COUNT(*) FROM$areplace",
+ $this->lastQuery);
+ if ($this->Debug) echo "<BR>Debug: num_rows: $q<BR>";
+// This is of course faster, but dosn't work in such many cases
+// (and yes, I like regular expressions :)
+// Comment the above (after 'works also for subselects') and uncomment this:
+// $from_pos = strpos(strtoupper($this->lastQuery),"FROM");
+// $q = "SELECT count(*) ". substr($this->lastQuery, $from_pos);
       
       ORA_parse($curs,$q);
       ORA_exec($curs);
       ORA_fetch($curs);
- if ($this->Debug) echo "<BR>Debug: num_rows=". ORA_getcolumn($curs,0)."<BR>";
+ if ($this->Debug) echo "<BR>Debug: ID ".$this->QueryID." num_rows=". ORA_getcolumn($curs,0)."<BR>";
       return(ORA_getcolumn($curs,0));
     } else {
       $this->halt("Last Query was not a SELECT: $this->lastQuery");
@@ -400,7 +428,7 @@
   }
 
   function haltmsg($msg) {
- printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
+ printf("</td></tr></table><br><b>Database error:</b> %s<br>\n", $msg);
     printf("<b>Oracle Error</b>: %s (%s)<br>\n",
       $this->Errno,
       $this->Error);

-
PHPLIB Developers Mailing List. Send messages to <phplib-dev <email protected>>.
To unsubscribe, send "unsubscribe" to <phplib-dev-request <email protected>> in
the body, not the subject, of your message.