propel-adapter
[ class tree: propel-adapter ] [ index: propel-adapter ] [ all elements ]

Source for file DBMSSQL.php

Documentation is available at DBMSSQL.php

  1. <?php
  2.  
  3. /*
  4. *  $Id: DBMSSQL.php 805 2007-11-15 00:05:30Z heltem $
  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.  For now, this class
  25.  * simply extends the adaptor for Sybase.
  26.  *
  27.  * @author     Hans Lellelid <hans@xmpl.org> (Propel)
  28.  * @version    $Revision: 805 $
  29.  * @package    propel.adapter
  30.  */
  31. class DBMSSQL extends DBSybase {
  32.  
  33.     /**
  34.     * Simulated Limit/Offset
  35.     * This rewrites the $sql query to apply the offset and limit.
  36.     * @see        DBAdapter::applyLimit()
  37.     * @author     Justin Carlson <justin.carlson@gmail.com>
  38.     */
  39.     public function applyLimit(&$sql$offset$limit)
  40.     {
  41.         // make sure offset and limit are numeric
  42.         if (!is_numeric($offset|| !is_numeric($limit)){
  43.             throw new Exception("DBMSSQL ::applyLimit() expects a number for argument 2 and 3");
  44.         }
  45.  
  46.         // obtain the original select statement
  47.         preg_match('/\A(.*)select(.*)from/si',$sql,$select_segment);
  48.         if (count($select_segment)>0)
  49.         {
  50.             $original_select $select_segment[0];
  51.         else {
  52.             throw new Exception("DBMSSQL ::applyLimit() could not locate the select statement at the start of the query. ");
  53.         }
  54.         $modified_select substr_replace($original_selectnullstristr($original_select,'select');
  55.  
  56.         // obtain the original order by clause, or create one if there isn't one
  57.         preg_match('/order by(.*)\Z/si',$sql,$order_segment);
  58.         if (count($order_segment)>0)
  59.         {
  60.             $order_by $order_segment[0];
  61.         else {
  62.  
  63.             // no order by clause, if there are columns we can attempt to sort by the columns in the select statement
  64.             $select_items split(',',$modified_select);
  65.             if (count($select_items)>0)
  66.             {
  67.                 $item_number 0;
  68.                 $order_by null;
  69.                 while ($order_by === null && $item_number<count($select_items))
  70.                 {
  71.                     if ($select_items[$item_number]!='*' && !strstr($select_items[$item_number],'('))
  72.                     {
  73.                         $order_by 'order by ' $select_items[0' asc';
  74.                     }
  75.                     $item_number++;
  76.                 }
  77.             }
  78.             if ($order_by === null)
  79.             {
  80.                 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. ");
  81.             else {
  82.                 $sql.= ' ' $order_by;
  83.             }
  84.  
  85.         }
  86.  
  87.         // remove the original select statement
  88.         $sql str_replace($original_select null$sql);
  89.  
  90.         /* modify the sort order by for paging */
  91.         $inverted_order '';
  92.         $order_columns split(',',str_ireplace('order by ','',$order_by));
  93.         $original_order_by $order_by;
  94.         $order_by '';
  95.         foreach ($order_columns as $column)
  96.         {
  97.             // strip "table." from order by columns
  98.             $column array_reverse(split("\.",$column));
  99.             $column $column[0];
  100.  
  101.             // commas if we have multiple sort columns
  102.             if (strlen($inverted_order)>0){
  103.                 $order_by.= ', ';
  104.                 $inverted_order.=', ';
  105.             }
  106.  
  107.             // put together order for paging wrapper
  108.             if (stristr($column,' desc'))
  109.             {
  110.                 $order_by .= $column;
  111.                 $inverted_order .= str_ireplace(' desc',' asc',$column);
  112.             elseif (stristr($column,' asc')) {
  113.                 $order_by .= $column;
  114.                 $inverted_order .= str_ireplace(' asc',' desc',$column);
  115.             else {
  116.                 $order_by .= $column;
  117.                 $inverted_order .= $column .' desc';
  118.             }
  119.         }
  120.         $order_by 'order by ' $order_by;
  121.         $inverted_order 'order by ' $inverted_order;
  122.  
  123.         // build the query
  124.         $offset ($limit+$offset);
  125.         $modified_sql 'select * from (';
  126.         $modified_sql.= 'select top '.$limit.' * from (';
  127.         $modified_sql.= 'select top '.$offset.' '.$modified_select.$sql;
  128.         $modified_sql.= ') deriveda '.$inverted_order.') derivedb '.$order_by;
  129.         $sql $modified_sql;
  130.  
  131.     }
  132.  
  133.     public function random($seed=NULL)
  134.     {
  135.         return 'NEWID()';
  136.     }
  137.  
  138. }

Documentation generated on Thu, 22 Nov 2007 03:33:40 +0000 by phpDocumentor 1.4.0