Keystone PHP Cross Reference Customer Relationship Management

Source: /oracle.php3 - 275 lines - 7688 bytes - Summary - Text - Print

   1  <?
   2      #---------------------------------------------------------------------#
   3      # This source code is (c)2003 Stonekeep Consulting, Inc               #
   4      # see http://www.stonekeep.com/license.html for licensing information #
   5      #---------------------------------------------------------------------#
   6  
   7      #---------------------------------------------------------------------
   8      # $Id: oracle.php3 6 2003-01-30 02:51:42Z shevett $
   9      #---------------------------------------------------------------------
  10      # PHP Database independent for PTS supporting: oracle
  11      # John Williams <williams@morinda.com> is to blame for this
  12      #---------------------------------------------------------------------
  13  
  14      # This is a really ugly, kludgy way to do things, 
  15      # but oracle is a really ugly, kludgy (but fast) database.
  16  
  17      # db_query loads all the data which may or may not be asked for
  18      # into an array, and returns the array.
  19  
  20      # I may alter it in the future to keep the cursor open and load
  21      # things as needed, but db_numrows will still require all rows
  22      # to be fetched in order to determine the number of rows fetched.
  23  
  24  
  25  	function db_oraerror()
  26      {
  27          $e = OCIerror($conn);
  28          $ecode = $e["code"];
  29          $etxt = $e["message"];
  30          echo "Here we are.  Code is $ecode, text is $etxt<br>";
  31          report_error("oracle.php3","$ecode","$etxt");
  32          LogIt("ERR","oracle.php3","$etxt");
  33      }
  34  
  35  	function db_connect($target = "") 
  36      {
  37          global $dbname,$dbuser,$dbpasswd,$options,$conn,$dbhost,$oracle_home;
  38          putenv("ORACLE_HOME=".$oracle_home);
  39          #putenv("ORACLE_SID=ORCL");
  40                #LogIt("DEBUG", "oracle.php3", "$dbuser $dbpasswd @ $dbname");
  41          $conn    = OCIPLogon($dbuser, $dbpasswd, $dbname);
  42          if (Is_Array(OCIError()))
  43          {
  44             db_oraerror();
  45          }
  46          else 
  47          {
  48          # I'm not sure if we need to commit here. No harm though. 
  49             OCICommit($conn);
  50          }
  51      }
  52  
  53  	function db_query($qstring)
  54      {
  55          global $conn;
  56          LogIt("DEBUG","oracle.php3","Query passed is: $qstring");
  57          # field name fixes: uid -> userid, _rowid -> x_rowid
  58          $qstring = eregi_replace("([^A-Za-z_])uid([^A-Za-z_])","\\1userid\\2",$qstring);
  59          $qstring = eregi_replace("([^A-Za-z_])_rowid([^A-Za-z_])","\\1x_rowid\\2",$qstring);
  60          # quoting fixes: only ' is special, and is represented by ''
  61          # Yes, php really requires all those slashes!
  62          $qstring = eregi_replace("\\\\\"","\"",$qstring);
  63          $qstring = eregi_replace("\\\\'","''",$qstring);
  64          $qstring = eregi_replace("\\\\\\\\","\\",$qstring);
  65          $ora_result_set = array();
  66          if (OCIError())
  67          { 
  68              db_oraerror(); 
  69          }
  70          $statement = OCIParse($conn,$qstring);
  71          if(!$statement)
  72          {
  73              db_oraerror();
  74              return 0;
  75          }
  76          $handle = OCIExecute($statement,OCI_COMMIT_ON_SUCCESS);
  77          if(!$handle)
  78          {
  79              LogIt("DEBUG" , "oracle.php3", "EXECUTE FAILED");
  80              if (OCIError($conn)) 
  81              {
  82                  db_oraerror();
  83                  return 0;
  84              }
  85              else
  86              {
  87                  LogIt("DEBUG", "oracle.php3", "NO OCI ERROR");
  88              }
  89          }
  90          LogIt("DEBUG","oracle.php3","Successful query, cleaned up string is $qstring");
  91          # for insert/update; "numrows" is overwritten later for select
  92          $ora_result_set["numrows"] = OCIRowCount($statement);
  93          eregi("(from|into)[ \t\n\r]+([A-Za-z_]+)",$qstring,$tablematch);
  94          $ora_result_set["table"] = $tablematch[2];
  95  
  96          #return an array if the qry was a select
  97          if (eregi("^[^A-Za-z]*select",$qstring)) 
  98          {
  99              $nc = OCINumCols($statement);
 100                      for ($i=1 ; $i<=$nc ; $i++)
 101              {
 102                    $ora_result_set["name"][$i] = strtolower(OCIColumnName($statement,$i));
 103                 $ora_result_set["type"][$i] = strtolower(OCIColumnType($statement,$i));
 104                 $ora_result_set["len"][$i]  = strtolower(OCIColumnSize($statement, $i));
 105              }
 106          
 107              $temp = array();
 108              $row = 0;
 109              while(OCIFetchInto($statement, &$temp)) 
 110              {
 111                 $ora_result_set[$row++] = $temp;
 112                 $temp = array();  // nulls do not overwrite
 113              }
 114  
 115              $ora_result_set["numrows"] = $row;
 116              $ora_result_set["numcols"] = $nc;
 117          }
 118          if (eregi("^[^A-Za-z]*insert",$qstring)) 
 119          {
 120              OCICommit($conn);
 121              eregi("into[ \t\n\r]+([A-Za-z_]+)",$qstring,$t_a);
 122              $seq = $t_a[1]."_seq";
 123  #            $cid = ora_open($conn);
 124              $seqstate = OCIParse($conn,"select sequence_name from user_sequences where sequence_name = upper('$seq')");
 125              OCIExecute($seqstate,OCI_COMMIT_ON_SUCCESS);
 126              if (OCIFetchInto($seqstate,&$temp))
 127              {
 128                  $iidstate == OCIParse($conn,"select $seq.currval from dual");
 129                  OCIExecute($iidstate,OCI_COMMIT_ON_SUCCESS);
 130                  OCIFetchInto($iidstate,&$iid_a);
 131                  echo $iid_a[0];
 132                  $ora_result_set["insertid"] = $iid_a[0];
 133              }
 134          }
 135          $ora_result_set["row"] = 0;
 136          return $ora_result_set;
 137      }
 138  
 139  	function db_numrows(&$qhandle) {
 140          return $qhandle["numrows"];
 141      }
 142  
 143  	function db_result(&$qhandle,$row,$field) {
 144          if (is_string($field)) {
 145              db_seek($qhandle,$row);
 146              $ary = db_fetch_array($qhandle);
 147              return $ary[$field];
 148          } else {
 149              return $qhandle[$row][$field];
 150          }
 151      }
 152  
 153  	function db_listfields($tname) {
 154          $result = db_query("select * from $tname where 0=1");
 155          $result["table"] = $tname;
 156          return $result;
 157      }
 158  
 159  	function db_numfields(&$lhandle) {
 160          return $lhandle["numcols"];
 161      }
 162  
 163  	function db_fieldname(&$lhandle,$which) {
 164          return $lhandle["name"][$which];
 165      }
 166          
 167  	function db_fieldtype(&$lhandle,$which) {
 168          return $lhandle["type"][$which];
 169      }
 170          
 171  	function db_fieldlen(&$lhandle,$which) {
 172          return $lhandle["len"][$which];
 173      }
 174  
 175      # This returns the result of ora_numrows for insert/update
 176      # but php-3.0.9 ora_numrows always returns 1
 177  	function db_affected_rows(&$qhandle) {
 178          return $qhandle["numrows"];
 179      }
 180  
 181  	function db_fetch_array(&$qhandle) 
 182      {
 183          if ($qhandle["row"] >= $qhandle["numrows"])
 184          {
 185              // empty array evaluate as false in boolean context
 186              LogIt("DEBUG", "oracle.php3", "Empty Aray detected");
 187              return array();
 188          }
 189  
 190          $result = array();
 191          $nc = $qhandle["numcols"];
 192          $qrow = $qhandle[$qhandle["row"]];
 193          for ($i=1 ; $i<=$nc ; $i++ )
 194          {
 195              $name = $qhandle["name"][$i];
 196              $result[$name] = $qrow[$i-1];
 197              if ($name == 'x_rowid')
 198              {
 199                  $result[_rowid] = $result[$name];
 200              }
 201              if ($name == 'pnumber')
 202              {
 203                  $result[number] = $result[$name];
 204              }
 205              if ($name == 'userid') 
 206              {
 207                  $result[uid] = $result[$name];
 208              }
 209          }
 210          $qhandle["row"]++;
 211          return $result;
 212      }
 213  
 214  	function db_seek(&$qhandle,$qrows) {
 215          $qhandle["row"] = $qrows;
 216          return 1;
 217      }
 218  
 219  	function db_fieldflags(&$lhandle,$which) {
 220          global $dbuser;
 221          $table = $lhandle["table"];
 222          $column = $lhandle["name"][$which];
 223          $query = "select decode(cons.constraint_type,'P','primary key','') "
 224              ."from    user_cons_columns ucc, user_constraints cons "
 225              ."where    ucc.owner = upper('$dbuser') "
 226              ."and    ucc.table_name = upper('$table') "
 227              ."and    ucc.column_name = upper('$column') "
 228              ."and    ucc.constraint_name = cons.constraint_name "
 229              ."and    cons.constraint_type = 'P' ";
 230          $qh = db_query($query);
 231          return db_result($qh,0,0);
 232      }
 233  
 234  	function db_primarykey($tablename) {
 235          global $dbuser;
 236          $query = "select lower(ucc.column_name) "
 237              ."from user_cons_columns ucc, user_constraints cons "
 238              ."where ucc.owner = upper('$dbuser') "
 239              ."and ucc.table_name = upper('$tablename') "
 240              ."and ucc.constraint_name = cons.constraint_name "
 241              ."and cons.constraint_type = 'P' ";
 242          $qh = db_query($query);
 243          $priinfo[0] = db_result($qh,0,0);
 244          $priinfo[1] = "";
 245  
 246          $query = "select data_type "
 247              ."from user_tab_columns "
 248              ."where table_name = upper('$tablename') "
 249              ."and column_name = upper('".$priinfo[0]."') ";
 250          $qh = db_query($query);
 251          if (eregi("char",db_result($qh,0,0))) {
 252              $priinfo[1] = "'";
 253          }
 254          return ($priinfo);
 255      }
 256  
 257  	function db_insertid(&$r) {
 258          return $r["insertid"];
 259      }
 260  
 261  	function db_errno() {
 262          global $conn;
 263          return ora_errorcode($conn);
 264      }
 265  
 266  	function db_error() {
 267          global $conn;
 268          return ora_error($conn);
 269      }
 270  
 271  	function dbx(&$x) {
 272          echo serialize($x);
 273      }
 274  
 275  ?>

title

Description

title

Description

title

Description

title

title

Body