From 5f8adabb6286fdcb0ff8a0ea5d1d58f40eef51f4 Mon Sep 17 00:00:00 2001
From: Aleksander Machniak <alec@alec.pl>
Date: Mon, 27 Aug 2012 03:28:16 -0400
Subject: [PATCH] Add simple (constructor) tests for Framework classes

---
 program/include/rcube_db_pgsql.php |   76 ++++++++++++++++++++++++++++++-------
 1 files changed, 61 insertions(+), 15 deletions(-)

diff --git a/program/include/rcube_db_pgsql.php b/program/include/rcube_db_pgsql.php
index 63bd92b..285b8e2 100644
--- a/program/include/rcube_db_pgsql.php
+++ b/program/include/rcube_db_pgsql.php
@@ -1,6 +1,6 @@
 <?php
 
-/*
+/**
  +-----------------------------------------------------------------------+
  | program/include/rcube_db_pgsql.php                                    |
  |                                                                       |
@@ -26,21 +26,19 @@
  *
  * This is a wrapper for the PHP PDO
  *
- * @package    Database
- * @version    1.0
+ * @package Database
+ * @version 1.0
  */
 class rcube_db_pgsql extends rcube_db
 {
-
     /**
      * Get last inserted record ID
-     * For Postgres databases, a table name is required
      *
-     * @param  string $table  Table name (to find the incremented sequence)
+     * @param string $table Table name (to find the incremented sequence)
      *
-     * @return mixed   ID or false on failure
+     * @return mixed ID or false on failure
      */
-    public function insert_id($table = '')
+    public function insert_id($table = null)
     {
         if (!$this->db_connected || $this->db_mode == 'r') {
             return false;
@@ -55,6 +53,26 @@
         return $id;
     }
 
+    /**
+     * Return correct name for a specific database sequence
+     *
+     * @param string $sequence Secuence name
+     *
+     * @return string Translated sequence name
+     */
+    protected function sequence_name($sequence)
+    {
+        $rcube = rcube::get_instance();
+
+        // return sequence name if configured
+        $config_key = 'db_sequence_'.$sequence;
+
+        if ($name = $rcube->config->get($config_key)) {
+            return $name;
+        }
+
+        return $sequence;
+    }
 
     /**
      * Return SQL statement to convert a field value into a unix timestamp
@@ -62,9 +80,9 @@
      * This method is deprecated and should not be used anymore due to limitations
      * of timestamp functions in Mysql (year 2038 problem)
      *
-     * @param  string $field Field name
+     * @param string $field Field name
      *
-     * @return string  SQL statement to use in query
+     * @return string SQL statement to use in query
      * @deprecated
      */
     public function unixtimestamp($field)
@@ -72,18 +90,46 @@
         return "EXTRACT (EPOCH FROM $field)";
     }
 
-
     /**
      * Return SQL statement for case insensitive LIKE
      *
-     * @param  string $column  Field name
-     * @param  string $value   Search value
+     * @param string $column Field name
+     * @param string $value  Search value
      *
-     * @return string  SQL statement to use in query
+     * @return string SQL statement to use in query
      */
     public function ilike($column, $value)
     {
-        return $this->quote_identifier($column).' ILIKE '.$this->quote($value);
+        return $this->quote_identifier($column) . ' ILIKE ' . $this->quote($value);
+    }
+
+    /**
+     * Get database runtime variables
+     *
+     * @param string $varname Variable name
+     * @param mixed  $default Default value if variable is not set
+     *
+     * @return mixed Variable value or default
+     */
+    public function get_variable($varname, $default = null)
+    {
+        // There's a known case when max_allowed_packet is queried
+        // PostgreSQL doesn't have such limit, return immediately
+        if ($varname == 'max_allowed_packet') {
+            return $default;
+        }
+
+        if (!isset($this->variables)) {
+            $this->variables = array();
+
+            $result = $this->query('SHOW ALL');
+
+            while ($row = $this->fetch_array($result)) {
+                $this->variables[$row[0]] = $row[1];
+            }
+        }
+
+        return isset($this->variables[$varname]) ? $this->variables[$varname] : $default;
     }
 
 }

--
Gitblit v1.9.1