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 7.1KB

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