Extend dbClass for PostgreSQL (rename original Rico class)
authorJoey Schulze <joey@infodrom.org>
Wed, 20 Jan 2010 09:31:22 +0000 (10:31 +0100)
committerJoey Schulze <joey@infodrom.org>
Wed, 20 Jan 2010 09:31:22 +0000 (10:31 +0100)
lib/dbClass.php [new file with mode: 0644]
lib/rico/dbClass2.php

diff --git a/lib/dbClass.php b/lib/dbClass.php
new file mode 100644 (file)
index 0000000..09efe37
--- /dev/null
@@ -0,0 +1,107 @@
+<?php
+
+require_once('rico/dbClass2.php');
+
+class dbClass_postgresql
+{
+  var $lastQuery;
+  function dbClass_postgresql($conn) { $this->conn=$conn; }
+  function HasError() { return pg_last_error() !== ''; }
+  function ErrorMsg() { return pg_last_error(); }
+  function Close() { return pg_close(); }
+  function FreeResult($rsLookUp) { return pg_free_result($rsLookUp); }
+  function RunQuery($sqltext) { $this->lastQuery=$sqltext; return pg_query($sqltext); }
+  function NumFields($rsMain) { return pg_num_fields($rsMain); }
+  function NumRows($rsMain) { return pg_num_rows($rsMain); }
+  function FieldType($rsMain,$i) { return pg_field_type($rsMain,$i); }
+  function FetchRow($rsMain,&$result) { $result=pg_fetch_row($rsMain); return ($result==false) ? false : true; }
+  function FetchAssoc($rsMain,&$result) { $result=pg_fetch_assoc($rsMain); return ($result==false) ? false : true; }
+  function FetchArray($rsMain,&$result) { $result=pg_fetch_array($rsMain,MYSQL_NUM); return ($result==false) ? false : true; }
+  function AffectedRows($rsMain) { return pg_affected_rows($this->conn); }
+  function Seek($rsMain,$offset) { error_log("mysql_data_seek($rsMain,$offset);"); return false; }
+  function RunParamQuery($query, $phs = array()) {
+    foreach ($phs as $ph) {   // from php.net
+      if ( isset($ph) ) {
+        $ph = "'" . pg_escape_string($ph) . "'";
+      } else {
+        $ph = "NULL" ;
+      }
+      $query = substr_replace($query, $ph, strpos($query, '?'), 1);
+    }
+    $this->lastQuery=$query;
+    $result = pg_query($query);
+    return ($result===false) ? false : true;
+  }
+  function GetColumnInfo($TableName) {
+    $rsMain=$this->RunQuery("SHOW COLUMNS FROM ".$TableName);
+    if (!$rsMain) return null;
+    $arColumns=array();
+    while($this->FetchAssoc($rsMain,$row)) {
+      $colinfo=new dbColumn;
+      $colinfo->IsPKey=($row["Key"]=="PRI");
+      $colinfo->ColName=$row["Field"];
+      $colinfo->ColType=$row["Type"];
+      $colinfo->Nullable=($row["Null"]=="YES");
+      if (preg_match("/\((\d+)\)/", $row["Type"], $matches))
+        $colinfo->ColLength=$matches[1];
+      else
+        $colinfo->ColLength=0;
+      $colinfo->Writeable=($row["Extra"] != 'auto_increment');
+      array_push($arColumns, $colinfo);
+    } 
+    $this->FreeResult($rsMain);
+    return $arColumns;
+  }
+  function GetTableList($TableType) {
+    if ($TableType!='VIEW') $TableType='BASE TABLE';
+    $rsMain=$this->RunQuery("SHOW FULL TABLES WHERE Table_type='".$TableType."'");
+    if (!$rsMain) return null;
+    $arTables=array();
+    while($this->FetchRow($rsMain,$row)) {
+      array_push($arTables, $row[0]);
+    } 
+    $this->FreeResult($rsMain);
+    return $arTables;
+  }
+  function Concat($arStrings) {
+    return "concat(".implode(",",$arStrings).")";
+  }
+  function Convert2Char($s) {
+    return $s; // implicit conversion
+  }
+  function SqlDay($s) {
+    return "dayofmonth(".$s.")";
+  }
+  function SqlMonth($s) {
+    return "month(".$s.")";
+  }
+  function SqlYear($s) {
+    return "year(".$s.")";
+  }
+  function CurrentTime() {
+    return "LOCALTIMESTAMP";
+  }
+}
+
+class dbClass extends dbClassRico
+{
+
+//********************************************************************************************************
+// Attempts to connect to the Database. Returns true on success.
+//********************************************************************************************************
+  function PostgreSqlLogon($DefDB,$userid,$pw)
+  {
+    $this->Dialect="PostgreSQL";
+    $this->dbDefault = $DefDB;
+
+    $dsn = sprintf('host=%s dbname=%s user=%s password=%s',DBHOST,$DefDB,$userid,$pw);
+    $this->dbMain = pg_connect($dsn);
+    $this->db =& new dbClass_postgresql($this->dbMain);
+
+    if ($this->dbMain === false && $this->CheckForError("opening connection")) return false;
+    return true;
+  }
+
+}
+
+?>
\ No newline at end of file
index 268bf4c..891d124 100644 (file)
@@ -770,7 +770,7 @@ class dbClass_mssql
 
 
 
-class dbClass
+class dbClassRico
 {
 
   var $debug,$ConnTimeout,$CmdTimeout,$LockTimeout,$Provider;
@@ -785,7 +785,7 @@ class dbClass
 // -------------------------------------------------------------
 // Class Constructor
 // -------------------------------------------------------------
-  function dbClass()
+  function dbClassRico()
   {
     $this->Provider="localhost";
     $this->debug=false;