Thomas Bruederli
2012-10-03 7bcd291517e9aca620e8938e965347a73b620a7a
commit | author | age
0d94fd 1 <?php
AM 2
3d231c 3 /**
0d94fd 4  +-----------------------------------------------------------------------+
AM 5  | program/include/rcube_db_mysql.php                                    |
6  |                                                                       |
7  | This file is part of the Roundcube Webmail client                     |
8  | Copyright (C) 2005-2012, The Roundcube Dev Team                       |
9  |                                                                       |
10  | Licensed under the GNU General Public License version 3 or            |
11  | any later version with exceptions for skins & plugins.                |
12  | See the README file for a full license statement.                     |
13  |                                                                       |
14  | PURPOSE:                                                              |
15  |   Database wrapper class that implements PHP PDO functions            |
16  |   for MySQL database                                                  |
17  |                                                                       |
18  +-----------------------------------------------------------------------+
19  | Author: Aleksander Machniak <alec@alec.pl>                            |
20  +-----------------------------------------------------------------------+
21 */
22
23
24 /**
25  * Database independent query interface
26  *
27  * This is a wrapper for the PHP PDO
28  *
3d231c 29  * @package Database
AM 30  * @version 1.0
0d94fd 31  */
AM 32 class rcube_db_mysql extends rcube_db
33 {
8c2375 34     /**
AM 35      * Driver initialization/configuration
36      */
0d94fd 37     protected function init()
AM 38     {
39         // SQL identifiers quoting
40         $this->options['identifier_start'] = '`';
41         $this->options['identifier_end'] = '`';
42     }
43
44     /**
45      * Abstract SQL statement for value concatenation
46      *
47      * @return string SQL statement to be used in query
48      */
49     public function concat(/* col1, col2, ... */)
50     {
51         $args = func_get_args();
52
53         if (is_array($args[0])) {
54             $args = $args[0];
55         }
56
57         return 'CONCAT(' . join(', ', $args) . ')';
58     }
59
60     /**
8c2375 61      * Returns PDO DSN string from DSN array
c2b20f 62      *
AM 63      * @param array $dsn DSN parameters
64      *
65      * @return string Connection string
0d94fd 66      */
AM 67     protected function dsn_string($dsn)
68     {
69         $params = array();
70         $result = 'mysql:';
71
72         if ($dsn['database']) {
73             $params[] = 'dbname=' . $dsn['database'];
74         }
75
76         if ($dsn['hostspec']) {
77             $params[] = 'host=' . $dsn['hostspec'];
78         }
79
80         if ($dsn['port']) {
81             $params[] = 'port=' . $dsn['port'];
82         }
83
84         if ($dsn['socket']) {
85             $params[] = 'unix_socket=' . $dsn['socket'];
86         }
87
88         $params[] = 'charset=utf8';
89
90         if (!empty($params)) {
91             $result .= implode(';', $params);
92         }
93
94         return $result;
95     }
96
c389a8 97     /**
c2b20f 98      * Returns driver-specific connection options
AM 99      *
100      * @param array $dsn DSN parameters
101      *
102      * @return array Connection options
103      */
104     protected function dsn_options($dsn)
105     {
106         $result = array();
107
108         if (!empty($dsn['key'])) {
109             $result[PDO::MYSQL_ATTR_KEY] = $dsn['key'];
110         }
111
112         if (!empty($dsn['cipher'])) {
113             $result[PDO::MYSQL_ATTR_CIPHER] = $dsn['cipher'];
114         }
115
116         if (!empty($dsn['cert'])) {
117             $result[PDO::MYSQL_ATTR_SSL_CERT] = $dsn['cert'];
118         }
119
120         if (!empty($dsn['capath'])) {
121             $result[PDO::MYSQL_ATTR_SSL_CAPATH] = $dsn['capath'];
122         }
123
124         if (!empty($dsn['ca'])) {
125             $result[PDO::MYSQL_ATTR_SSL_CA] = $dsn['ca'];
126         }
127
128         return $result;
129     }
130
131     /**
c389a8 132      * Get database runtime variables
AM 133      *
3d231c 134      * @param string $varname Variable name
AM 135      * @param mixed  $default Default value if variable is not set
c389a8 136      *
AM 137      * @return mixed Variable value or default
138      */
139     public function get_variable($varname, $default = null)
140     {
141         if (!isset($this->variables)) {
142             $this->variables = array();
143
144             $result = $this->query('SHOW VARIABLES');
145
146             while ($sql_arr = $this->fetch_array($result)) {
147                 $this->variables[$row[0]] = $row[1];
148             }
149         }
150
151         return isset($this->variables[$varname]) ? $this->variables[$varname] : $default;
152     }
153
0d94fd 154 }