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_mysql.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 MySQL database |
  14. +-----------------------------------------------------------------------+
  15. | Author: Aleksander Machniak <alec@alec.pl> |
  16. +-----------------------------------------------------------------------+
  17. */
  18. /**
  19. * Database independent query interface
  20. *
  21. * This is a wrapper for the PHP PDO
  22. *
  23. * @package Framework
  24. * @subpackage Database
  25. */
  26. class rcube_db_mysql extends rcube_db
  27. {
  28. public $db_provider = 'mysql';
  29. /**
  30. * Object constructor
  31. *
  32. * @param string $db_dsnw DSN for read/write operations
  33. * @param string $db_dsnr Optional DSN for read only operations
  34. * @param bool $pconn Enables persistent connections
  35. */
  36. public function __construct($db_dsnw, $db_dsnr = '', $pconn = false)
  37. {
  38. parent::__construct($db_dsnw, $db_dsnr, $pconn);
  39. // SQL identifiers quoting
  40. $this->options['identifier_start'] = '`';
  41. $this->options['identifier_end'] = '`';
  42. }
  43. /**
  44. * Driver-specific configuration of database connection
  45. *
  46. * @param array $dsn DSN for DB connections
  47. * @param PDO $dbh Connection handler
  48. */
  49. protected function conn_configure($dsn, $dbh)
  50. {
  51. $dbh->query("SET NAMES 'utf8'");
  52. }
  53. /**
  54. * Abstract SQL statement for value concatenation
  55. *
  56. * @return string SQL statement to be used in query
  57. */
  58. public function concat(/* col1, col2, ... */)
  59. {
  60. $args = func_get_args();
  61. if (is_array($args[0])) {
  62. $args = $args[0];
  63. }
  64. return 'CONCAT(' . join(', ', $args) . ')';
  65. }
  66. /**
  67. * Returns PDO DSN string from DSN array
  68. *
  69. * @param array $dsn DSN parameters
  70. *
  71. * @return string Connection string
  72. */
  73. protected function dsn_string($dsn)
  74. {
  75. $params = array();
  76. $result = 'mysql:';
  77. if ($dsn['database']) {
  78. $params[] = 'dbname=' . $dsn['database'];
  79. }
  80. if ($dsn['hostspec']) {
  81. $params[] = 'host=' . $dsn['hostspec'];
  82. }
  83. if ($dsn['port']) {
  84. $params[] = 'port=' . $dsn['port'];
  85. }
  86. if ($dsn['socket']) {
  87. $params[] = 'unix_socket=' . $dsn['socket'];
  88. }
  89. $params[] = 'charset=utf8';
  90. if (!empty($params)) {
  91. $result .= implode(';', $params);
  92. }
  93. return $result;
  94. }
  95. /**
  96. * Returns driver-specific connection options
  97. *
  98. * @param array $dsn DSN parameters
  99. *
  100. * @return array Connection options
  101. */
  102. protected function dsn_options($dsn)
  103. {
  104. $result = parent::dsn_options($dsn);
  105. if (!empty($dsn['key'])) {
  106. $result[PDO::MYSQL_ATTR_SSL_KEY] = $dsn['key'];
  107. }
  108. if (!empty($dsn['cipher'])) {
  109. $result[PDO::MYSQL_ATTR_SSL_CIPHER] = $dsn['cipher'];
  110. }
  111. if (!empty($dsn['cert'])) {
  112. $result[PDO::MYSQL_ATTR_SSL_CERT] = $dsn['cert'];
  113. }
  114. if (!empty($dsn['capath'])) {
  115. $result[PDO::MYSQL_ATTR_SSL_CAPATH] = $dsn['capath'];
  116. }
  117. if (!empty($dsn['ca'])) {
  118. $result[PDO::MYSQL_ATTR_SSL_CA] = $dsn['ca'];
  119. }
  120. // Always return matching (not affected only) rows count
  121. $result[PDO::MYSQL_ATTR_FOUND_ROWS] = true;
  122. // Enable AUTOCOMMIT mode (#1488902)
  123. $result[PDO::ATTR_AUTOCOMMIT] = true;
  124. return $result;
  125. }
  126. /**
  127. * Returns list of tables in a database
  128. *
  129. * @return array List of all tables of the current database
  130. */
  131. public function list_tables()
  132. {
  133. // get tables if not cached
  134. if ($this->tables === null) {
  135. // first fetch current database name
  136. $d = $this->query("SELECT database()");
  137. $d = $this->fetch_array($d);
  138. // get list of tables in current database
  139. $q = $this->query("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES"
  140. . " WHERE TABLE_SCHEMA = ? AND TABLE_TYPE = 'BASE TABLE'"
  141. . " ORDER BY TABLE_NAME", $d ? $d[0] : '');
  142. $this->tables = $q ? $q->fetchAll(PDO::FETCH_COLUMN, 0) : array();
  143. }
  144. return $this->tables;
  145. }
  146. /**
  147. * Get database runtime variables
  148. *
  149. * @param string $varname Variable name
  150. * @param mixed $default Default value if variable is not set
  151. *
  152. * @return mixed Variable value or default
  153. */
  154. public function get_variable($varname, $default = null)
  155. {
  156. if (!isset($this->variables)) {
  157. $this->variables = array();
  158. }
  159. if (array_key_exists($varname, $this->variables)) {
  160. return $this->variables[$varname];
  161. }
  162. // configured value has higher prio
  163. $conf_value = rcube::get_instance()->config->get('db_' . $varname);
  164. if ($conf_value !== null) {
  165. return $this->variables[$varname] = $conf_value;
  166. }
  167. $result = $this->query('SHOW VARIABLES LIKE ?', $varname);
  168. while ($row = $this->fetch_array($result)) {
  169. $this->variables[$row[0]] = $row[1];
  170. }
  171. // not found, use default
  172. if (!isset($this->variables[$varname])) {
  173. $this->variables[$varname] = $default;
  174. }
  175. return $this->variables[$varname];
  176. }
  177. /**
  178. * Handle DB errors, re-issue the query on deadlock errors from InnoDB row-level locking
  179. *
  180. * @param string Query that triggered the error
  181. * @return mixed Result to be stored and returned
  182. */
  183. protected function handle_error($query)
  184. {
  185. $error = $this->dbh->errorInfo();
  186. // retry after "Deadlock found when trying to get lock" errors
  187. $retries = 2;
  188. while ($error[1] == 1213 && $retries >= 0) {
  189. usleep(50000); // wait 50 ms
  190. $result = $this->dbh->query($query);
  191. if ($result !== false) {
  192. return $result;
  193. }
  194. $error = $this->dbh->errorInfo();
  195. $retries--;
  196. }
  197. return parent::handle_error($query);
  198. }
  199. }