Source for file sqlsrv.php

Documentation is available at sqlsrv.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  Database
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * Query Building Class.
  14.  *
  15.  * @package     Joomla.Platform
  16.  * @subpackage  Database
  17.  * @since       11.1
  18.  */
  19. {
  20.     /**
  21.      * The character(s) used to quote SQL statement names such as table names or field names,
  22.      * etc.  The child classes should define this as necessary.  If a single character string the
  23.      * same character is used for both sides of the quoted name, else the first character will be
  24.      * used for the opening quote and the second for the closing quote.
  25.      *
  26.      * @var    string 
  27.      * @since  11.1
  28.      */
  29.     protected $name_quotes = '`';
  30.  
  31.     /**
  32.      * The null or zero representation of a timestamp for the database driver.  This should be
  33.      * defined in child classes to hold the appropriate value for the engine.
  34.      *
  35.      * @var    string 
  36.      * @since  11.1
  37.      */
  38.     protected $null_date = '1900-01-01 00:00:00';
  39.  
  40.     /**
  41.      * @var    integer  The affected row limit for the current SQL statement.
  42.      * @since  3.2 CMS
  43.      */
  44.     protected $limit = 0;
  45.  
  46.     /**
  47.      * @var    integer  The affected row offset to apply for the current SQL statement.
  48.      * @since  3.2 CMS
  49.      */
  50.     protected $offset = 0;
  51.  
  52.     /**
  53.      * Magic function to convert the query to a string.
  54.      *
  55.      * @return  string    The completed query.
  56.      *
  57.      * @since   11.1
  58.      */
  59.     public function __toString()
  60.     {
  61.         $query '';
  62.  
  63.         switch ($this->type)
  64.         {
  65.             case 'select':
  66.                 $query .= (string) $this->select;
  67.                 $query .= (string) $this->from;
  68.  
  69.                 if ($this instanceof JDatabaseQueryLimitable && ($this->limit > || $this->offset > 0))
  70.                 {
  71.                     if ($this->order)
  72.                     {
  73.                         $query .= (string) $this->order;
  74.                     }
  75.  
  76.                     $query $this->processLimit($query$this->limit$this->offset);
  77.                 }
  78.  
  79.                 if ($this->join)
  80.                 {
  81.                     // Special case for joins
  82.                     foreach ($this->join as $join)
  83.                     {
  84.                         $query .= (string) $join;
  85.                     }
  86.                 }
  87.  
  88.                 if ($this->where)
  89.                 {
  90.                     $query .= (string) $this->where;
  91.                 }
  92.  
  93.                 if ($this->group)
  94.                 {
  95.                     $query .= (string) $this->group;
  96.                 }
  97.  
  98.                 if ($this->having)
  99.                 {
  100.                     $query .= (string) $this->having;
  101.                 }
  102.  
  103.                 break;
  104.  
  105.             case 'insert':
  106.                 $query .= (string) $this->insert;
  107.  
  108.                 // Set method
  109.                 if ($this->set)
  110.                 {
  111.                     $query .= (string) $this->set;
  112.                 }
  113.                 // Columns-Values method
  114.                 elseif ($this->values)
  115.                 {
  116.                     if ($this->columns)
  117.                     {
  118.                         $query .= (string) $this->columns;
  119.                     }
  120.  
  121.                     $elements $this->insert->getElements();
  122.                     $tableName array_shift($elements);
  123.  
  124.                     $query .= 'VALUES ';
  125.                     $query .= (string) $this->values;
  126.  
  127.                     if ($this->autoIncrementField)
  128.                     {
  129.                         $query 'SET IDENTITY_INSERT ' $tableName ' ON;' $query 'SET IDENTITY_INSERT ' $tableName ' OFF;';
  130.                     }
  131.  
  132.                     if ($this->where)
  133.                     {
  134.                         $query .= (string) $this->where;
  135.                     }
  136.                 }
  137.  
  138.                 break;
  139.  
  140.             default:
  141.                 $query parent::__toString();
  142.                 break;
  143.         }
  144.  
  145.         return $query;
  146.     }
  147.  
  148.     /**
  149.      * Casts a value to a char.
  150.      *
  151.      * Ensure that the value is properly quoted before passing to the method.
  152.      *
  153.      * @param   string  $value  The value to cast as a char.
  154.      *
  155.      * @return  string  Returns the cast value.
  156.      *
  157.      * @since   11.1
  158.      */
  159.     public function castAsChar($value)
  160.     {
  161.         return 'CAST(' $value ' as NVARCHAR(10))';
  162.     }
  163.  
  164.     /**
  165.      * Gets the function to determine the length of a character string.
  166.      *
  167.      * @param   string  $field      A value.
  168.      * @param   string  $operator   Comparison operator between charLength integer value and $condition
  169.      * @param   string  $condition  Integer value to compare charLength with.
  170.      *
  171.      * @return  string  The required char length call.
  172.      *
  173.      * @since 11.1
  174.      */
  175.     public function charLength($field$operator null$condition null)
  176.     {
  177.         return 'DATALENGTH(' $field ')' (isset($operator&& isset($condition' ' $operator ' ' $condition '');
  178.     }
  179.  
  180.     /**
  181.      * Concatenates an array of column names or values.
  182.      *
  183.      * @param   array   $values     An array of values to concatenate.
  184.      * @param   string  $separator  As separator to place between each value.
  185.      *
  186.      * @return  string  The concatenated values.
  187.      *
  188.      * @since   11.1
  189.      */
  190.     public function concatenate($values$separator null)
  191.     {
  192.         if ($separator)
  193.         {
  194.             return '(' implode('+' $this->quote($separator'+'$values')';
  195.         }
  196.         else
  197.         {
  198.             return '(' implode('+'$values')';
  199.         }
  200.     }
  201.  
  202.     /**
  203.      * Gets the current date and time.
  204.      *
  205.      * @return  string 
  206.      *
  207.      * @since   11.1
  208.      */
  209.     public function currentTimestamp()
  210.     {
  211.         return 'GETDATE()';
  212.     }
  213.  
  214.     /**
  215.      * Get the length of a string in bytes.
  216.      *
  217.      * @param   string  $value  The string to measure.
  218.      *
  219.      * @return  integer 
  220.      *
  221.      * @since   11.1
  222.      */
  223.     public function length($value)
  224.     {
  225.         return 'LEN(' $value ')';
  226.     }
  227.  
  228.     /**
  229.      * Add to the current date and time.
  230.      * Usage:
  231.      * $query->select($query->dateAdd());
  232.      * Prefixing the interval with a - (negative sign) will cause subtraction to be used.
  233.      *
  234.      * @param   datetime  $date      The date to add to; type may be time or datetime.
  235.      * @param   string    $interval  The string representation of the appropriate number of units
  236.      * @param   string    $datePart  The part of the date to perform the addition on
  237.      *
  238.      * @return  string  The string with the appropriate sql for addition of dates
  239.      *
  240.      * @since   13.1
  241.      * @note    Not all drivers support all units.
  242.      * @link    http://msdn.microsoft.com/en-us/library/ms186819.aspx for more information
  243.      */
  244.     public function dateAdd($date$interval$datePart)
  245.     {
  246.         return "DATEADD('" $datePart "', '" $interval "', '" $date "'" ')';
  247.     }
  248.  
  249.     /**
  250.      * Method to modify a query already in string format with the needed
  251.      * additions to make the query limited to a particular number of
  252.      * results, or start at a particular offset.
  253.      *
  254.      * @param   string   $query   The query in string format
  255.      * @param   integer  $limit   The limit for the result set
  256.      * @param   integer  $offset  The offset for the result set
  257.      *
  258.      * @return  string 
  259.      *
  260.      * @since   12.1
  261.      */
  262.     public function processLimit($query$limit$offset 0)
  263.     {
  264.         $start $offset 1;
  265.         $end   $offset $limit;
  266.  
  267.         $orderBy stristr($query'ORDER BY');
  268.  
  269.         if (is_null($orderBy|| empty($orderBy))
  270.         {
  271.             $orderBy 'ORDER BY (select 0)';
  272.         }
  273.  
  274.         $query str_ireplace($orderBy''$query);
  275.  
  276.         $rowNumberText ', ROW_NUMBER() OVER (' $orderBy ') AS RowNumber FROM ';
  277.  
  278.         $query preg_replace('/\sFROM\s/i'$rowNumberText$query1);
  279.         $query 'SELECT * FROM (' $query ') _myResults WHERE RowNumber BETWEEN ' $start ' AND ' $end;
  280.  
  281.         return $query;
  282.     }
  283.  
  284.     /**
  285.      * Sets the offset and limit for the result set, if the database driver supports it.
  286.      *
  287.      * Usage:
  288.      * $query->setLimit(100, 0); (retrieve 100 rows, starting at first record)
  289.      * $query->setLimit(50, 50); (retrieve 50 rows, starting at 50th record)
  290.      *
  291.      * @param   integer  $limit   The limit for the result set
  292.      * @param   integer  $offset  The offset for the result set
  293.      *
  294.      * @return  JDatabaseQuery  Returns this object to allow chaining.
  295.      *
  296.      * @since   12.1
  297.      */
  298.     public function setLimit($limit 0$offset 0)
  299.     {
  300.         $this->limit  = (int) $limit;
  301.         $this->offset = (int) $offset;
  302.  
  303.         return $this;
  304.     }
  305. }

Documentation generated on Tue, 19 Nov 2013 15:14:17 +0100 by phpDocumentor 1.4.3