Updated Rico 2 and Rico 3 with all patches submitted on Sourceforge.
[infodrom/rico3] / plugins / php / dbClass3.php
index 9dce42b..d7d7dc7 100644 (file)
@@ -1,10 +1,10 @@
 <?php\r
 /*****************************************************************\r
 <?php\r
 /*****************************************************************\r
- Page        : dbClass2.php\r
- Description : Routines to access MySQL, SQL Server, Oracle, & ODBC databases\r
+ Page        : dbClass3.php\r
+ Description : Routines to access MySQL, PostgreSQL, SQL Server, Oracle, & ODBC databases\r
  Date        : 25 May 2006\r
  Author      : Matt Brown (dowdybrown@yahoo.com)\r
  Date        : 25 May 2006\r
  Author      : Matt Brown (dowdybrown@yahoo.com)\r
- Copyright (C) 2006-2008 Matt Brown\r
+ Copyright (C) 2006-2011 Matt Brown\r
 \r
  Rico is licensed under the Apache License, Version 2.0 (the "License"); you may not use this
  file except in compliance with the License. You may obtain a copy of the License at
 \r
  Rico is licensed under the Apache License, Version 2.0 (the "License"); you may not use this
  file except in compliance with the License. You may obtain a copy of the License at
@@ -836,6 +836,86 @@ class dbClass_mssql
   }\r
 }\r
 \r
   }\r
 }\r
 \r
+// for PostgreSQL server\r
+class dbClass_postgresql\r
+{\r
+  var $lastQuery;\r
+  function dbClass_postgresql($conn) { $this->conn=$conn; }\r
+  function HasError() { return pg_last_error() !== ''; }\r
+  function ErrorMsg() { return pg_last_error(); }\r
+  function Close() { return pg_close(); }\r
+  function FreeResult($rsLookUp) { return pg_free_result($rsLookUp); }\r
+  function RunQuery($sqltext) { $this->lastQuery=$sqltext; return pg_query($sqltext); }\r
+  function NumFields($rsMain) { return pg_num_fields($rsMain); }\r
+  function NumRows($rsMain) { return pg_num_rows($rsMain); }\r
+  function FieldType($rsMain,$i) { return pg_field_type($rsMain,$i); }\r
+  function FetchRow($rsMain,&$result) { $result=pg_fetch_row($rsMain); return ($result==false) ? false : true; }\r
+  function FetchAssoc($rsMain,&$result) { $result=pg_fetch_assoc($rsMain); return ($result==false) ? false : true; }\r
+  function FetchArray($rsMain,&$result) { $result=pg_fetch_array($rsMain); return ($result==false) ? false : true; }\r
+  function AffectedRows($rsMain) { return pg_affected_rows($this->conn); }\r
+  function Seek($rsMain,$offset) { return pg_result_seek($rsMain,$offset); }\r
+  function RunParamQuery($query, $phs = array()) {\r
+    foreach ($phs as $ph) {   // from php.net\r
+      if ( isset($ph) ) {\r
+        $ph = "'" . pg_escape_string($ph) . "'";\r
+      } else {\r
+        $ph = "NULL" ;\r
+      }\r
+      $query = substr_replace($query, $ph, strpos($query, '?'), 1);\r
+    }\r
+    $this->lastQuery=$query;\r
+    return pg_query($query);\r
+  }\r
+  function GetColumnInfo($TableName) {\r
+    $rsMain=$this->RunQuery("SHOW COLUMNS FROM ".$TableName);\r
+    if (!$rsMain) return null;\r
+    $arColumns=array();\r
+    while($this->FetchAssoc($rsMain,$row)) {\r
+      $colinfo=new dbColumn;\r
+      $colinfo->IsPKey=($row["Key"]=="PRI");\r
+      $colinfo->ColName=$row["Field"];\r
+      $colinfo->ColType=$row["Type"];\r
+      $colinfo->Nullable=($row["Null"]=="YES");\r
+      if (preg_match("/\((\d+)\)/", $row["Type"], $matches))\r
+        $colinfo->ColLength=$matches[1];\r
+      else\r
+        $colinfo->ColLength=0;\r
+      $colinfo->Writeable=($row["Extra"] != 'auto_increment');\r
+      array_push($arColumns, $colinfo);\r
+    } \r
+    $this->FreeResult($rsMain);\r
+    return $arColumns;\r
+  }\r
+  function GetTableList($TableType) {\r
+    if ($TableType!='VIEW') $TableType='BASE TABLE';\r
+    $rsMain=$this->RunQuery("SHOW FULL TABLES WHERE Table_type='".$TableType."'");\r
+    if (!$rsMain) return null;\r
+    $arTables=array();\r
+    while($this->FetchRow($rsMain,$row)) {\r
+      array_push($arTables, $row[0]);\r
+    } \r
+    $this->FreeResult($rsMain);\r
+    return $arTables;\r
+  }\r
+  function Concat($arStrings) {\r
+    return "concat(".implode(",",$arStrings).")";\r
+  }\r
+  function Convert2Char($s) {\r
+    return $s; // implicit conversion\r
+  }\r
+  function SqlDay($s) {\r
+    return "dayofmonth(".$s.")";\r
+  }\r
+  function SqlMonth($s) {\r
+    return "month(".$s.")";\r
+  }\r
+  function SqlYear($s) {\r
+    return "year(".$s.")";\r
+  }\r
+  function CurrentTime() {\r
+    return "LOCALTIMESTAMP";\r
+  }\r
+}\r
 \r
 \r
 class dbClass\r
 \r
 \r
 class dbClass\r
@@ -939,6 +1019,19 @@ class dbClass
     return true;\r
   }\r
 \r
     return true;\r
   }\r
 \r
+//********************************************************************************************************\r
+// Attempts to connect to the Database. Returns true on success.\r
+//********************************************************************************************************\r
+  function PostgreSqlLogon($connection_string)\r
+  {\r
+    $this->Dialect="PostgreSQL";\r
+    $this->dbDefault = "";\r
+    $this->dbMain = pg_connect($connection_string);\r
+    $this->db =& new dbClass_postgresql($this->dbMain);\r
+    if ($this->CheckForError("opening connection")) return false;\r
+    return true;\r
+  }\r
+\r
 //********************************************************************************************************\r
 // Attempts to connect to the Database. Returns true on success.\r
 //********************************************************************************************************\r
 //********************************************************************************************************\r
 // Attempts to connect to the Database. Returns true on success.\r
 //********************************************************************************************************\r