Source for file sqlite.php

Documentation is available at sqlite.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.  * SQLite Query Building Class.
  14.  *
  15.  * @package     Joomla.Platform
  16.  * @subpackage  Database
  17.  * @since       12.1
  18.  */
  19. {
  20.     /**
  21.      * @var    integer  The offset for the result set.
  22.      * @since  12.1
  23.      */
  24.     protected $offset;
  25.  
  26.     /**
  27.      * @var    integer  The limit for the result set.
  28.      * @since  12.1
  29.      */
  30.     protected $limit;
  31.  
  32.     /**
  33.      * @var    array  Bounded object array
  34.      * @since  12.1
  35.      */
  36.     protected $bounded = array();
  37.  
  38.     /**
  39.      * Method to add a variable to an internal array that will be bound to a prepared SQL statement before query execution. Also
  40.      * removes a variable that has been bounded from the internal bounded array when the passed in value is null.
  41.      *
  42.      * @param   string|integer $key            The key that will be used in your SQL query to reference the value. Usually of
  43.      *                                           the form ':key', but can also be an integer.
  44.      * @param   mixed           &$value         The value that will be bound. The value is passed by reference to support output
  45.      *                                           parameters such as those possible with stored procedures.
  46.      * @param   integer         $dataType       Constant corresponding to a SQL datatype.
  47.      * @param   integer         $length         The length of the variable. Usually required for OUTPUT parameters.
  48.      * @param   array           $driverOptions  Optional driver options to be used.
  49.      *
  50.      * @return  JDatabaseQuerySqlite 
  51.      *
  52.      * @since   12.1
  53.      */
  54.     public function bind($key null&$value null$dataType PDO::PARAM_STR$length 0$driverOptions array())
  55.     {
  56.         // Case 1: Empty Key (reset $bounded array)
  57.         if (empty($key))
  58.         {
  59.             $this->bounded = array();
  60.  
  61.             return $this;
  62.         }
  63.  
  64.         // Case 2: Key Provided, null value (unset key from $bounded array)
  65.         if (is_null($value))
  66.         {
  67.             if (isset($this->bounded[$key]))
  68.             {
  69.                 unset($this->bounded[$key]);
  70.             }
  71.  
  72.             return $this;
  73.         }
  74.  
  75.         $obj new stdClass;
  76.  
  77.         $obj->value &$value;
  78.         $obj->dataType $dataType;
  79.         $obj->length $length;
  80.         $obj->driverOptions $driverOptions;
  81.  
  82.         // Case 3: Simply add the Key/Value into the bounded array
  83.         $this->bounded[$key$obj;
  84.  
  85.         return $this;
  86.     }
  87.  
  88.     /**
  89.      * Retrieves the bound parameters array when key is null and returns it by reference. If a key is provided then that item is
  90.      * returned.
  91.      *
  92.      * @param   mixed  $key  The bounded variable key to retrieve.
  93.      *
  94.      * @return  mixed 
  95.      *
  96.      * @since   12.1
  97.      */
  98.     public function &getBounded($key null)
  99.     {
  100.         if (empty($key))
  101.         {
  102.             return $this->bounded;
  103.         }
  104.         else
  105.         {
  106.             if (isset($this->bounded[$key]))
  107.             {
  108.                 return $this->bounded[$key];
  109.             }
  110.         }
  111.     }
  112.  
  113.     /**
  114.      * Clear data from the query or a specific clause of the query.
  115.      *
  116.      * @param   string  $clause  Optionally, the name of the clause to clear, or nothing to clear the whole query.
  117.      *
  118.      * @return  JDatabaseQuerySqlite  Returns this object to allow chaining.
  119.      *
  120.      * @since   12.1
  121.      */
  122.     public function clear($clause null)
  123.     {
  124.         switch ($clause)
  125.         {
  126.             case null:
  127.                 $this->bounded = array();
  128.                 break;
  129.         }
  130.  
  131.         parent::clear($clause);
  132.  
  133.         return $this;
  134.     }
  135.  
  136.     /**
  137.      * Method to modify a query already in string format with the needed
  138.      * additions to make the query limited to a particular number of
  139.      * results, or start at a particular offset. This method is used
  140.      * automatically by the __toString() method if it detects that the
  141.      * query implements the JDatabaseQueryLimitable interface.
  142.      *
  143.      * @param   string   $query   The query in string format
  144.      * @param   integer  $limit   The limit for the result set
  145.      * @param   integer  $offset  The offset for the result set
  146.      *
  147.      * @return  string 
  148.      *
  149.      * @since   12.1
  150.      */
  151.     public function processLimit($query$limit$offset 0)
  152.     {
  153.         if ($limit || $offset 0)
  154.         {
  155.             $query .= ' LIMIT ' $offset ', ' $limit;
  156.         }
  157.  
  158.         return $query;
  159.     }
  160.  
  161.     /**
  162.      * Sets the offset and limit for the result set, if the database driver supports it.
  163.      *
  164.      * Usage:
  165.      * $query->setLimit(100, 0); (retrieve 100 rows, starting at first record)
  166.      * $query->setLimit(50, 50); (retrieve 50 rows, starting at 50th record)
  167.      *
  168.      * @param   integer  $limit   The limit for the result set
  169.      * @param   integer  $offset  The offset for the result set
  170.      *
  171.      * @return  JDatabaseQuerySqlite  Returns this object to allow chaining.
  172.      *
  173.      * @since   12.1
  174.      */
  175.     public function setLimit($limit 0$offset 0)
  176.     {
  177.         $this->limit = (int) $limit;
  178.         $this->offset = (int) $offset;
  179.  
  180.         return $this;
  181.     }
  182.  
  183.     /**
  184.      * Add to the current date and time.
  185.      * Usage:
  186.      * $query->select($query->dateAdd());
  187.      * Prefixing the interval with a - (negative sign) will cause subtraction to be used.
  188.      *
  189.      * @param   datetime  $date      The date or datetime to add to
  190.      * @param   string    $interval  The string representation of the appropriate number of units
  191.      * @param   string    $datePart  The part of the date to perform the addition on
  192.      *
  193.      * @return  string  The string with the appropriate sql for addition of dates
  194.      *
  195.      * @since   13.1
  196.      * @link    http://www.sqlite.org/lang_datefunc.html
  197.      */
  198.     public function dateAdd($date$interval$datePart)
  199.     {
  200.         // SQLite does not support microseconds as a separate unit. Convert the interval to seconds
  201.         if (strcasecmp($datePart'microseconds'== 0)
  202.         {
  203.             $interval .001 $interval;
  204.             $datePart 'seconds';
  205.         }
  206.  
  207.         if (substr($interval01!= '-')
  208.         {
  209.             return "datetime('" $date "', '+" $interval " " $datePart "')";
  210.         }
  211.         else
  212.         {
  213.             return "datetime('" $date "', '" $interval " " $datePart "')";
  214.         }
  215.     }
  216. }

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