From c389a85978bc5cf8f0f9d06c58664a35c4746447 Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Sun, 8 Jul 2012 10:32:13 +0200 Subject: Add get_variable() implementation --- program/include/rcube_db.php | 17 ++++++++++++++++- program/include/rcube_db_mysql.php | 23 +++++++++++++++++++++++ program/include/rcube_db_pgsql.php | 37 +++++++++++++++++++++++++++++++++---- 3 files changed, 72 insertions(+), 5 deletions(-) (limited to 'program/include') diff --git a/program/include/rcube_db.php b/program/include/rcube_db.php index ba7b96c9d..b1cbd8505 100644 --- a/program/include/rcube_db.php +++ b/program/include/rcube_db.php @@ -42,6 +42,7 @@ class rcube_db protected $a_query_results = array('dummy'); protected $last_res_id = 0; protected $tables; + protected $variables; protected $db_index = 0; protected $options = array( @@ -279,6 +280,20 @@ class rcube_db return !empty($this->db_dsnr) && $this->db_dsnw != $this->db_dsnr; } + /** + * 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) + { + // to be implemented by driver class + return $default; + } + /** * Execute a SQL query * @@ -332,7 +347,7 @@ class rcube_db protected function _query($query, $offset, $numrows, $params) { // Read or write ? - $mode = preg_match('/^select/i', ltrim($query)) ? 'r' : 'w'; + $mode = preg_match('/^(select|show)/i', ltrim($query)) ? 'r' : 'w'; $this->db_connect($mode); diff --git a/program/include/rcube_db_mysql.php b/program/include/rcube_db_mysql.php index 84a324701..71f81956a 100644 --- a/program/include/rcube_db_mysql.php +++ b/program/include/rcube_db_mysql.php @@ -90,4 +90,27 @@ class rcube_db_mysql extends rcube_db return $result; } + /** + * 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) + { + if (!isset($this->variables)) { + $this->variables = array(); + + $result = $this->query('SHOW VARIABLES'); + + while ($sql_arr = $this->fetch_array($result)) { + $this->variables[$row[0]] = $row[1]; + } + } + + return isset($this->variables[$varname]) ? $this->variables[$varname] : $default; + } + } diff --git a/program/include/rcube_db_pgsql.php b/program/include/rcube_db_pgsql.php index 641b884d6..d357d88ce 100644 --- a/program/include/rcube_db_pgsql.php +++ b/program/include/rcube_db_pgsql.php @@ -1,6 +1,6 @@ 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; } } -- cgit v1.2.3