You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

rcube_db_pgsql.php 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. <?php
  2. /**
  3. +-----------------------------------------------------------------------+
  4. | This file is part of the Roundcube Webmail client |
  5. | Copyright (C) 2005-2012, The Roundcube Dev Team |
  6. | |
  7. | Licensed under the GNU General Public License version 3 or |
  8. | any later version with exceptions for skins & plugins. |
  9. | See the README file for a full license statement. |
  10. | |
  11. | PURPOSE: |
  12. | Database wrapper class that implements PHP PDO functions |
  13. | for PostgreSQL database |
  14. +-----------------------------------------------------------------------+
  15. | Author: Aleksander Machniak <alec@alec.pl> |
  16. +-----------------------------------------------------------------------+
  17. */
  18. /**
  19. * Database independent query interface
  20. * This is a wrapper for the PHP PDO
  21. *
  22. * @package Framework
  23. * @subpackage Database
  24. */
  25. class rcube_db_pgsql extends rcube_db
  26. {
  27. public $db_provider = 'postgres';
  28. /**
  29. * Driver-specific configuration of database connection
  30. *
  31. * @param array $dsn DSN for DB connections
  32. * @param PDO $dbh Connection handler
  33. */
  34. protected function conn_configure($dsn, $dbh)
  35. {
  36. $dbh->query("SET NAMES 'utf8'");
  37. }
  38. /**
  39. * Get last inserted record ID
  40. *
  41. * @param string $table Table name (to find the incremented sequence)
  42. *
  43. * @return mixed ID or false on failure
  44. */
  45. public function insert_id($table = null)
  46. {
  47. if (!$this->db_connected || $this->db_mode == 'r') {
  48. return false;
  49. }
  50. if ($table) {
  51. $table = $this->sequence_name($table);
  52. }
  53. $id = $this->dbh->lastInsertId($table);
  54. return $id;
  55. }
  56. /**
  57. * Return correct name for a specific database sequence
  58. *
  59. * @param string $table Table name
  60. *
  61. * @return string Translated sequence name
  62. */
  63. protected function sequence_name($table)
  64. {
  65. // Note: we support only one sequence per table
  66. // Note: The sequence name must be <table_name>_seq
  67. $sequence = $table . '_seq';
  68. // modify sequence name if prefix is configured
  69. if ($prefix = $this->options['table_prefix']) {
  70. return $prefix . $sequence;
  71. }
  72. return $sequence;
  73. }
  74. /**
  75. * Return SQL statement to convert a field value into a unix timestamp
  76. *
  77. * @param string $field Field name
  78. *
  79. * @return string SQL statement to use in query
  80. * @deprecated
  81. */
  82. public function unixtimestamp($field)
  83. {
  84. return "EXTRACT (EPOCH FROM $field)";
  85. }
  86. /**
  87. * Return SQL function for current time and date
  88. *
  89. * @param int $interval Optional interval (in seconds) to add/subtract
  90. *
  91. * @return string SQL function to use in query
  92. */
  93. public function now($interval = 0)
  94. {
  95. if ($interval) {
  96. $add = ' ' . ($interval > 0 ? '+' : '-') . " interval '";
  97. $add .= $interval > 0 ? intval($interval) : intval($interval) * -1;
  98. $add .= " seconds'";
  99. }
  100. return "now()" . $add;
  101. }
  102. /**
  103. * Return SQL statement for case insensitive LIKE
  104. *
  105. * @param string $column Field name
  106. * @param string $value Search value
  107. *
  108. * @return string SQL statement to use in query
  109. */
  110. public function ilike($column, $value)
  111. {
  112. return $this->quote_identifier($column) . ' ILIKE ' . $this->quote($value);
  113. }
  114. /**
  115. * Get database runtime variables
  116. *
  117. * @param string $varname Variable name
  118. * @param mixed $default Default value if variable is not set
  119. *
  120. * @return mixed Variable value or default
  121. */
  122. public function get_variable($varname, $default = null)
  123. {
  124. // There's a known case when max_allowed_packet is queried
  125. // PostgreSQL doesn't have such limit, return immediately
  126. if ($varname == 'max_allowed_packet') {
  127. return rcube::get_instance()->config->get('db_' . $varname, $default);
  128. }
  129. $this->variables[$varname] = rcube::get_instance()->config->get('db_' . $varname);
  130. if (!isset($this->variables)) {
  131. $this->variables = array();
  132. $result = $this->query('SHOW ALL');
  133. while ($row = $this->fetch_array($result)) {
  134. $this->variables[$row[0]] = $row[1];
  135. }
  136. }
  137. return isset($this->variables[$varname]) ? $this->variables[$varname] : $default;
  138. }
  139. /**
  140. * Returns list of tables in a database
  141. *
  142. * @return array List of all tables of the current database
  143. */
  144. public function list_tables()
  145. {
  146. // get tables if not cached
  147. if ($this->tables === null) {
  148. $q = $this->query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES"
  149. . " WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA NOT IN ('pg_catalog', 'information_schema')"
  150. . " ORDER BY TABLE_NAME");
  151. $this->tables = $q ? $q->fetchAll(PDO::FETCH_COLUMN, 0) : array();
  152. }
  153. return $this->tables;
  154. }
  155. /**
  156. * Returns PDO DSN string from DSN array
  157. *
  158. * @param array $dsn DSN parameters
  159. *
  160. * @return string DSN string
  161. */
  162. protected function dsn_string($dsn)
  163. {
  164. $params = array();
  165. $result = 'pgsql:';
  166. if ($dsn['hostspec']) {
  167. $params[] = 'host=' . $dsn['hostspec'];
  168. }
  169. else if ($dsn['socket']) {
  170. $params[] = 'host=' . $dsn['socket'];
  171. }
  172. if ($dsn['port']) {
  173. $params[] = 'port=' . $dsn['port'];
  174. }
  175. if ($dsn['database']) {
  176. $params[] = 'dbname=' . $dsn['database'];
  177. }
  178. if (!empty($params)) {
  179. $result .= implode(';', $params);
  180. }
  181. return $result;
  182. }
  183. /**
  184. * Parse SQL file and fix table names according to table prefix
  185. */
  186. protected function fix_table_names($sql)
  187. {
  188. if (!$this->options['table_prefix']) {
  189. return $sql;
  190. }
  191. $sql = parent::fix_table_names($sql);
  192. // replace sequence names, and other postgres-specific commands
  193. $sql = preg_replace_callback(
  194. '/((SEQUENCE |RENAME TO |nextval\()["\']*)([^"\' \r\n]+)/',
  195. array($this, 'fix_table_names_callback'),
  196. $sql
  197. );
  198. return $sql;
  199. }
  200. }