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