root/branches/1.3/runtime/classes/propel/adapter/DBMSSQL.php

Revision 989, 5.7 kB (checked in by heltem, 9 months ago)

Cleanup:

  • Replace CRLF by LF
  • Trim right spaces
  • Property svn:keywords set to Id Rev Date Author HeadURL Revision
Line 
1 <?php
2
3 /*
4 *  $Id$
5 *
6 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
7 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
8 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
9 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
10 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
11 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
12 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
13 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
14 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
15 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
16 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17 *
18 * This software consists of voluntary contributions made by many individuals
19 * and is licensed under the LGPL. For more information please see
20 * <http://propel.phpdb.org>.
21 */
22
23 /**
24  * This is used to connect to a MSSQL database.
25  *
26  * @author     Hans Lellelid <hans@xmpl.org> (Propel)
27  * @version    $Revision$
28  * @package    propel.adapter
29  */
30 class DBMSSQL extends DBAdapter {
31
32     /**
33      * This method is used to ignore case.
34      *
35      * @param      in The string to transform to upper case.
36      * @return     The upper case string.
37      */
38     public function toUpperCase($in)
39     {
40         return "UPPER(" . $in . ")";
41     }
42
43     /**
44      * This method is used to ignore case.
45      *
46      * @param      in The string whose case to ignore.
47      * @return     The string in a case that can be ignored.
48      */
49     public function ignoreCase($in)
50     {
51         return "UPPER(" . $in . ")";
52     }
53
54     /**
55      * Returns SQL which concatenates the second string to the first.
56      *
57      * @param      string String to concatenate.
58      * @param      string String to append.
59      * @return     string
60      */
61     public function concatString($s1, $s2)
62     {
63         return "($s1 + $s2)";
64     }
65
66     /**
67      * Returns SQL which extracts a substring.
68      *
69      * @param      string String to extract from.
70      * @param      int Offset to start from.
71      * @param      int Number of characters to extract.
72      * @return     string
73      */
74     public function subString($s, $pos, $len)
75     {
76         return "SUBSTRING($s, $pos, $len)";
77     }
78
79     /**
80      * Returns SQL which calculates the length (in chars) of a string.
81      *
82      * @param      string String to calculate length of.
83      * @return     string
84      */
85     public function strLength($s)
86     {
87         return "LEN($s)";
88     }
89
90     /**
91      * @see        DBAdapter::quoteIdentifier()
92      */
93     public function quoteIdentifier($text)
94     {
95         return '[' . $text . ']';
96     }
97
98     /**
99      * @see        DBAdapter::random()
100      */
101     public function random($seed = null)
102     {
103         return 'rand('.((int) $seed).')';
104     }
105
106     /**
107     * Simulated Limit/Offset
108     * This rewrites the $sql query to apply the offset and limit.
109     * @see        DBAdapter::applyLimit()
110     * @author     Justin Carlson <justin.carlson@gmail.com>
111     */
112     public function applyLimit(&$sql, $offset, $limit)
113     {
114         // make sure offset and limit are numeric
115         if (!is_numeric($offset) || !is_numeric($limit)){
116             throw new Exception("DBMSSQL ::applyLimit() expects a number for argument 2 and 3");
117         }
118
119         // obtain the original select statement
120         preg_match('/\A(.*)select(.*)from/si',$sql,$select_segment);
121         if (count($select_segment)>0)
122         {
123             $original_select = $select_segment[0];
124         } else {
125             throw new Exception("DBMSSQL ::applyLimit() could not locate the select statement at the start of the query. ");
126         }
127         $modified_select = substr_replace($original_select, null, stristr($original_select,'select') , 6 );
128
129         // obtain the original order by clause, or create one if there isn't one
130         preg_match('/order by(.*)\Z/si',$sql,$order_segment);
131         if (count($order_segment)>0)
132         {
133             $order_by = $order_segment[0];
134         } else {
135
136             // no order by clause, if there are columns we can attempt to sort by the columns in the select statement
137             $select_items = split(',',$modified_select);
138             if (count($select_items)>0)
139             {
140                 $item_number = 0;
141                 $order_by = null;
142                 while ($order_by === null && $item_number<count($select_items))
143                 {
144                     if ($select_items[$item_number]!='*' && !strstr($select_items[$item_number],'('))
145                     {
146                         $order_by = 'order by ' . $select_items[0] . ' asc';
147                     }
148                     $item_number++;
149                 }
150             }
151             if ($order_by === null)
152             {
153                 throw new Exception("DBMSSQL ::applyLimit() could not locate the order by statement at the end of your query or any columns at the start of your query. ");
154             } else {
155                 $sql.= ' ' . $order_by;
156             }
157
158         }
159
160         // remove the original select statement
161         $sql = str_replace($original_select , null, $sql);
162
163         /* modify the sort order by for paging */
164         $inverted_order = '';
165         $order_columns = split(',',str_ireplace('order by ','',$order_by));
166         $original_order_by = $order_by;
167         $order_by = '';
168         foreach ($order_columns as $column)
169         {
170             // strip "table." from order by columns
171             $column = array_reverse(split("\.",$column));
172             $column = $column[0];
173
174             // commas if we have multiple sort columns
175             if (strlen($inverted_order)>0){
176                 $order_by.= ', ';
177                 $inverted_order.=', ';
178             }
179
180             // put together order for paging wrapper
181             if (stristr($column,' desc'))
182             {
183                 $order_by .= $column;
184                 $inverted_order .= str_ireplace(' desc',' asc',$column);
185             } elseif (stristr($column,' asc')) {
186                 $order_by .= $column;
187                 $inverted_order .= str_ireplace(' asc',' desc',$column);
188             } else {
189                 $order_by .= $column;
190                 $inverted_order .= $column .' desc';
191             }
192         }
193         $order_by = 'order by ' . $order_by;
194         $inverted_order = 'order by ' . $inverted_order;
195
196         // build the query
197         $offset = ($limit+$offset);
198         $modified_sql = 'select * from (';
199         $modified_sql.= 'select top '.$limit.' * from (';
200         $modified_sql.= 'select top '.$offset.' '.$modified_select.$sql;
201         $modified_sql.= ') deriveda '.$inverted_order.') derivedb '.$order_by;
202         $sql = $modified_sql;
203
204     }
205
206 }
207
Note: See TracBrowser for help on using the browser.