Aleksander Machniak
2012-12-07 996af3bfd9bfcac84396790a9a215d177b17c79e
commit | author | age
0d94fd 1 <?php
AM 2
3d231c 3 /**
0d94fd 4  +-----------------------------------------------------------------------+
AM 5  | program/include/rcube_db_mssql.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 MS SQL Server database                                          |
17  |                                                                       |
18  +-----------------------------------------------------------------------+
19  | Author: Aleksander Machniak <alec@alec.pl>                            |
20  +-----------------------------------------------------------------------+
21 */
22
23
24 /**
25  * Database independent query interface
26  * This is a wrapper for the PHP PDO
27  *
9ab346 28  * @package    Framework
AM 29  * @subpackage Database
0d94fd 30  */
AM 31 class rcube_db_mssql extends rcube_db
32 {
88107d 33     public $db_provider = 'mssql';
TB 34
8c2375 35     /**
AM 36      * Driver initialization
37      */
0d94fd 38     protected function init()
AM 39     {
40         $this->options['identifier_start'] = '[';
41         $this->options['identifier_end'] = ']';
42     }
43
8c2375 44     /**
AM 45      * Character setting
46      */
0d94fd 47     protected function set_charset($charset)
AM 48     {
8c2375 49         // UTF-8 is default
0d94fd 50     }
AM 51
52     /**
53      * Return SQL function for current time and date
54      *
55      * @return string SQL function to use in query
56      */
57     public function now()
58     {
59         return "getdate()";
60     }
61
62     /**
63      * Return SQL statement to convert a field value into a unix timestamp
64      *
65      * This method is deprecated and should not be used anymore due to limitations
66      * of timestamp functions in Mysql (year 2038 problem)
67      *
3d231c 68      * @param string $field Field name
0d94fd 69      *
3d231c 70      * @return string SQL statement to use in query
0d94fd 71      * @deprecated
AM 72      */
73     public function unixtimestamp($field)
74     {
75         return "DATEDIFF(second, '19700101', $field) + DATEDIFF(second, GETDATE(), GETUTCDATE())";
76     }
77
78     /**
79      * Abstract SQL statement for value concatenation
80      *
81      * @return string SQL statement to be used in query
82      */
83     public function concat(/* col1, col2, ... */)
84     {
85         $args = func_get_args();
86
87         if (is_array($args[0])) {
88             $args = $args[0];
89         }
90
91         return '(' . join('+', $args) . ')';
92     }
93
94     /**
95      * Adds TOP (LIMIT,OFFSET) clause to the query
96      *
3d231c 97      * @param string $query  SQL query
AM 98      * @param int    $limit  Number of rows
99      * @param int    $offset Offset
8c2375 100      *
AM 101      * @return string SQL query
0d94fd 102      */
AM 103     protected function set_limit($query, $limit = 0, $offset = 0)
104     {
8c2375 105         $limit  = intval($limit);
AM 106         $offset = intval($offset);
107
108         $orderby = stristr($query, 'ORDER BY');
109         if ($orderby !== false) {
110             $sort  = (stripos($orderby, ' desc') !== false) ? 'desc' : 'asc';
111             $order = str_ireplace('ORDER BY', '', $orderby);
112             $order = trim(preg_replace('/\bASC\b|\bDESC\b/i', '', $order));
0d94fd 113         }
AM 114
8c2375 115         $query = preg_replace('/^SELECT\s/i', 'SELECT TOP ' . ($limit + $offset) . ' ', $query);
AM 116
117         $query = 'SELECT * FROM (SELECT TOP ' . $limit . ' * FROM (' . $query . ') AS inner_tbl';
118         if ($orderby !== false) {
119             $query .= ' ORDER BY ' . $order . ' ';
120             $query .= (stripos($sort, 'asc') !== false) ? 'DESC' : 'ASC';
121         }
122         $query .= ') AS outer_tbl';
123         if ($orderby !== false) {
124             $query .= ' ORDER BY ' . $order . ' ' . $sort;
125         }
0d94fd 126
AM 127         return $query;
128     }
129
130     /**
8c2375 131      * Returns PDO DSN string from DSN array
0d94fd 132      */
AM 133     protected function dsn_string($dsn)
134     {
135         $params = array();
136         $result = $dsn['phptype'] . ':';
137
138         if ($dsn['hostspec']) {
139             $host = $dsn['hostspec'];
140             if ($dsn['port']) {
141                 $host .= ',' . $dsn['port'];
142             }
143             $params[] = 'host=' . $host;
144         }
145
146         if ($dsn['database']) {
147             $params[] = 'dbname=' . $dsn['database'];
148         }
149
150         if (!empty($params)) {
151             $result .= implode(';', $params);
152         }
153
154         return $result;
155     }
156 }