Source for file postgresql.php

Documentation is available at postgresql.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.3
  18.  */
  19. {
  20.     /**
  21.      * @var    object  The FOR UPDATE element used in "FOR UPDATE"  lock
  22.      * @since  11.3
  23.      */
  24.     protected $forUpdate = null;
  25.  
  26.     /**
  27.      * @var    object  The FOR SHARE element used in "FOR SHARE"  lock
  28.      * @since  11.3
  29.      */
  30.     protected $forShare = null;
  31.  
  32.     /**
  33.      * @var    object  The NOWAIT element used in "FOR SHARE" and "FOR UPDATE" lock
  34.      * @since  11.3
  35.      */
  36.     protected $noWait = null;
  37.  
  38.     /**
  39.      * @var    object  The LIMIT element
  40.      * @since  11.3
  41.      */
  42.     protected $limit = null;
  43.  
  44.     /**
  45.      * @var    object  The OFFSET element
  46.      * @since  11.3
  47.      */
  48.     protected $offset = null;
  49.  
  50.     /**
  51.      * @var    object  The RETURNING element of INSERT INTO
  52.      * @since  11.3
  53.      */
  54.     protected $returning = null;
  55.  
  56.     /**
  57.      * Magic function to convert the query to a string, only for postgresql specific query
  58.      *
  59.      * @return  string    The completed query.
  60.      *
  61.      * @since   11.3
  62.      */
  63.     public function __toString()
  64.     {
  65.         $query '';
  66.  
  67.         switch ($this->type)
  68.         {
  69.             case 'select':
  70.                 $query .= (string) $this->select;
  71.                 $query .= (string) $this->from;
  72.  
  73.                 if ($this->join)
  74.                 {
  75.                     // Special case for joins
  76.                     foreach ($this->join as $join)
  77.                     {
  78.                         $query .= (string) $join;
  79.                     }
  80.                 }
  81.  
  82.                 if ($this->where)
  83.                 {
  84.                     $query .= (string) $this->where;
  85.                 }
  86.  
  87.                 if ($this->group)
  88.                 {
  89.                     $query .= (string) $this->group;
  90.                 }
  91.  
  92.                 if ($this->having)
  93.                 {
  94.                     $query .= (string) $this->having;
  95.                 }
  96.  
  97.                 if ($this->order)
  98.                 {
  99.                     $query .= (string) $this->order;
  100.                 }
  101.  
  102.                 if ($this->forUpdate)
  103.                 {
  104.                     $query .= (string) $this->forUpdate;
  105.                 }
  106.                 else
  107.                 {
  108.                     if ($this->forShare)
  109.                     {
  110.                         $query .= (string) $this->forShare;
  111.                     }
  112.                 }
  113.  
  114.                 if ($this->noWait)
  115.                 {
  116.                     $query .= (string) $this->noWait;
  117.                 }
  118.  
  119.                 break;
  120.  
  121.             case 'update':
  122.                 $query .= (string) $this->update;
  123.                 $query .= (string) $this->set;
  124.  
  125.                 if ($this->join)
  126.                 {
  127.                     $onWord ' ON ';
  128.  
  129.                     // Workaround for special case of JOIN with UPDATE
  130.                     foreach ($this->join as $join)
  131.                     {
  132.                         $joinElem $join->getElements();
  133.  
  134.                         $joinArray explode($onWord$joinElem[0]);
  135.  
  136.                         $this->from($joinArray[0]);
  137.                         $this->where($joinArray[1]);
  138.                     }
  139.  
  140.                     $query .= (string) $this->from;
  141.                 }
  142.  
  143.                 if ($this->where)
  144.                 {
  145.                     $query .= (string) $this->where;
  146.                 }
  147.  
  148.                 break;
  149.  
  150.             case 'insert':
  151.                 $query .= (string) $this->insert;
  152.  
  153.                 if ($this->values)
  154.                 {
  155.                     if ($this->columns)
  156.                     {
  157.                         $query .= (string) $this->columns;
  158.                     }
  159.  
  160.                     $elements $this->values->getElements();
  161.  
  162.                     if (!($elements[0instanceof $this))
  163.                     {
  164.                         $query .= ' VALUES ';
  165.                     }
  166.  
  167.                     $query .= (string) $this->values;
  168.  
  169.                     if ($this->returning)
  170.                     {
  171.                         $query .= (string) $this->returning;
  172.                     }
  173.                 }
  174.  
  175.                 break;
  176.  
  177.             default:
  178.                 $query parent::__toString();
  179.                 break;
  180.         }
  181.  
  182.         if ($this instanceof JDatabaseQueryLimitable)
  183.         {
  184.             $query $this->processLimit($query$this->limit$this->offset);
  185.         }
  186.  
  187.         return $query;
  188.     }
  189.  
  190.     /**
  191.      * Clear data from the query or a specific clause of the query.
  192.      *
  193.      * @param   string  $clause  Optionally, the name of the clause to clear, or nothing to clear the whole query.
  194.      *
  195.      * @return  JDatabaseQueryPostgresql  Returns this object to allow chaining.
  196.      *
  197.      * @since   11.3
  198.      */
  199.     public function clear($clause null)
  200.     {
  201.         switch ($clause)
  202.         {
  203.             case 'limit':
  204.                 $this->limit = null;
  205.                 break;
  206.  
  207.             case 'offset':
  208.                 $this->offset = null;
  209.                 break;
  210.  
  211.             case 'forUpdate':
  212.                 $this->forUpdate = null;
  213.                 break;
  214.  
  215.             case 'forShare':
  216.                 $this->forShare = null;
  217.                 break;
  218.  
  219.             case 'noWait':
  220.                 $this->noWait = null;
  221.                 break;
  222.  
  223.             case 'returning':
  224.                 $this->returning = null;
  225.                 break;
  226.  
  227.             case 'select':
  228.             case 'update':
  229.             case 'delete':
  230.             case 'insert':
  231.             case 'from':
  232.             case 'join':
  233.             case 'set':
  234.             case 'where':
  235.             case 'group':
  236.             case 'having':
  237.             case 'order':
  238.             case 'columns':
  239.             case 'values':
  240.                 parent::clear($clause);
  241.                 break;
  242.  
  243.             default:
  244.                 $this->type = null;
  245.                 $this->limit = null;
  246.                 $this->offset = null;
  247.                 $this->forUpdate = null;
  248.                 $this->forShare = null;
  249.                 $this->noWait = null;
  250.                 $this->returning = null;
  251.                 parent::clear($clause);
  252.                 break;
  253.         }
  254.  
  255.         return $this;
  256.     }
  257.  
  258.     /**
  259.      * Casts a value to a char.
  260.      *
  261.      * Ensure that the value is properly quoted before passing to the method.
  262.      *
  263.      * Usage:
  264.      * $query->select($query->castAsChar('a'));
  265.      *
  266.      * @param   string  $value  The value to cast as a char.
  267.      *
  268.      * @return  string  Returns the cast value.
  269.      *
  270.      * @since   11.3
  271.      */
  272.     public function castAsChar($value)
  273.     {
  274.         return $value '::text';
  275.     }
  276.  
  277.     /**
  278.      * Concatenates an array of column names or values.
  279.      *
  280.      * Usage:
  281.      * $query->select($query->concatenate(array('a', 'b')));
  282.      *
  283.      * @param   array   $values     An array of values to concatenate.
  284.      * @param   string  $separator  As separator to place between each value.
  285.      *
  286.      * @return  string  The concatenated values.
  287.      *
  288.      * @since   11.3
  289.      */
  290.     public function concatenate($values$separator null)
  291.     {
  292.         if ($separator)
  293.         {
  294.             return implode(' || ' $this->quote($separator' || '$values);
  295.         }
  296.         else
  297.         {
  298.             return implode(' || '$values);
  299.         }
  300.     }
  301.  
  302.     /**
  303.      * Gets the current date and time.
  304.      *
  305.      * @return  string  Return string used in query to obtain
  306.      *
  307.      * @since   11.3
  308.      */
  309.     public function currentTimestamp()
  310.     {
  311.         return 'NOW()';
  312.     }
  313.  
  314.     /**
  315.      * Sets the FOR UPDATE lock on select's output row
  316.      *
  317.      * @param   string  $table_name  The table to lock
  318.      * @param   string  $glue        The glue by which to join the conditions. Defaults to ',' .
  319.      *
  320.      * @return  JDatabaseQueryPostgresql  FOR UPDATE query element
  321.      *
  322.      * @since   11.3
  323.      */
  324.     public function forUpdate($table_name$glue ',')
  325.     {
  326.         $this->type = 'forUpdate';
  327.  
  328.         if (is_null($this->forUpdate))
  329.         {
  330.             $glue            strtoupper($glue);
  331.             $this->forUpdate = new JDatabaseQueryElement('FOR UPDATE''OF ' $table_name"$glue ");
  332.         }
  333.         else
  334.         {
  335.             $this->forUpdate->append($table_name);
  336.         }
  337.  
  338.         return $this;
  339.     }
  340.  
  341.     /**
  342.      * Sets the FOR SHARE lock on select's output row
  343.      *
  344.      * @param   string  $table_name  The table to lock
  345.      * @param   string  $glue        The glue by which to join the conditions. Defaults to ',' .
  346.      *
  347.      * @return  JDatabaseQueryPostgresql  FOR SHARE query element
  348.      *
  349.      * @since   11.3
  350.      */
  351.     public function forShare($table_name$glue ',')
  352.     {
  353.         $this->type = 'forShare';
  354.  
  355.         if (is_null($this->forShare))
  356.         {
  357.             $glue           strtoupper($glue);
  358.             $this->forShare = new JDatabaseQueryElement('FOR SHARE''OF ' $table_name"$glue ");
  359.         }
  360.         else
  361.         {
  362.             $this->forShare->append($table_name);
  363.         }
  364.  
  365.         return $this;
  366.     }
  367.  
  368.     /**
  369.      * Used to get a string to extract year from date column.
  370.      *
  371.      * Usage:
  372.      * $query->select($query->year($query->quoteName('dateColumn')));
  373.      *
  374.      * @param   string  $date  Date column containing year to be extracted.
  375.      *
  376.      * @return  string  Returns string to extract year from a date.
  377.      *
  378.      * @since   12.1
  379.      */
  380.     public function year($date)
  381.     {
  382.         return 'EXTRACT (YEAR FROM ' $date ')';
  383.     }
  384.  
  385.     /**
  386.      * Used to get a string to extract month from date column.
  387.      *
  388.      * Usage:
  389.      * $query->select($query->month($query->quoteName('dateColumn')));
  390.      *
  391.      * @param   string  $date  Date column containing month to be extracted.
  392.      *
  393.      * @return  string  Returns string to extract month from a date.
  394.      *
  395.      * @since   12.1
  396.      */
  397.     public function month($date)
  398.     {
  399.         return 'EXTRACT (MONTH FROM ' $date ')';
  400.     }
  401.  
  402.     /**
  403.      * Used to get a string to extract day from date column.
  404.      *
  405.      * Usage:
  406.      * $query->select($query->day($query->quoteName('dateColumn')));
  407.      *
  408.      * @param   string  $date  Date column containing day to be extracted.
  409.      *
  410.      * @return  string  Returns string to extract day from a date.
  411.      *
  412.      * @since   12.1
  413.      */
  414.     public function day($date)
  415.     {
  416.         return 'EXTRACT (DAY FROM ' $date ')';
  417.     }
  418.  
  419.     /**
  420.      * Used to get a string to extract hour from date column.
  421.      *
  422.      * Usage:
  423.      * $query->select($query->hour($query->quoteName('dateColumn')));
  424.      *
  425.      * @param   string  $date  Date column containing hour to be extracted.
  426.      *
  427.      * @return  string  Returns string to extract hour from a date.
  428.      *
  429.      * @since   12.1
  430.      */
  431.     public function hour($date)
  432.     {
  433.         return 'EXTRACT (HOUR FROM ' $date ')';
  434.     }
  435.  
  436.     /**
  437.      * Used to get a string to extract minute from date column.
  438.      *
  439.      * Usage:
  440.      * $query->select($query->minute($query->quoteName('dateColumn')));
  441.      *
  442.      * @param   string  $date  Date column containing minute to be extracted.
  443.      *
  444.      * @return  string  Returns string to extract minute from a date.
  445.      *
  446.      * @since   12.1
  447.      */
  448.     public function minute($date)
  449.     {
  450.         return 'EXTRACT (MINUTE FROM ' $date ')';
  451.     }
  452.  
  453.     /**
  454.      * Used to get a string to extract seconds from date column.
  455.      *
  456.      * Usage:
  457.      * $query->select($query->second($query->quoteName('dateColumn')));
  458.      *
  459.      * @param   string  $date  Date column containing second to be extracted.
  460.      *
  461.      * @return  string  Returns string to extract second from a date.
  462.      *
  463.      * @since   12.1
  464.      */
  465.     public function second($date)
  466.     {
  467.         return 'EXTRACT (SECOND FROM ' $date ')';
  468.     }
  469.  
  470.     /**
  471.      * Sets the NOWAIT lock on select's output row
  472.      *
  473.      * @return  JDatabaseQueryPostgresql  NO WAIT query element
  474.      *
  475.      * @since   11.3
  476.      */
  477.     public function noWait ()
  478.     {
  479.         $this->type = 'noWait';
  480.  
  481.         if (is_null($this->noWait))
  482.         {
  483.             $this->noWait = new JDatabaseQueryElement('NOWAIT'null);
  484.         }
  485.  
  486.         return $this;
  487.     }
  488.  
  489.     /**
  490.      * Set the LIMIT clause to the query
  491.      *
  492.      * @param   integer  $limit  An int of how many row will be returned
  493.      *
  494.      * @return  JDatabaseQueryPostgresql  Returns this object to allow chaining.
  495.      *
  496.      * @since   11.3
  497.      */
  498.     public function limit($limit 0)
  499.     {
  500.         if (is_null($this->limit))
  501.         {
  502.             $this->limit = new JDatabaseQueryElement('LIMIT'(int) $limit);
  503.         }
  504.  
  505.         return $this;
  506.     }
  507.  
  508.     /**
  509.      * Set the OFFSET clause to the query
  510.      *
  511.      * @param   integer  $offset  An int for skipping row
  512.      *
  513.      * @return  JDatabaseQueryPostgresql  Returns this object to allow chaining.
  514.      *
  515.      * @since   11.3
  516.      */
  517.     public function offset($offset 0)
  518.     {
  519.         if (is_null($this->offset))
  520.         {
  521.             $this->offset = new JDatabaseQueryElement('OFFSET'(int) $offset);
  522.         }
  523.  
  524.         return $this;
  525.     }
  526.  
  527.     /**
  528.      * Add the RETURNING element to INSERT INTO statement.
  529.      *
  530.      * @param   mixed  $pkCol  The name of the primary key column.
  531.      *
  532.      * @return  JDatabaseQueryPostgresql  Returns this object to allow chaining.
  533.      *
  534.      * @since   11.3
  535.      */
  536.     public function returning($pkCol)
  537.     {
  538.         if (is_null($this->returning))
  539.         {
  540.             $this->returning = new JDatabaseQueryElement('RETURNING'$pkCol);
  541.         }
  542.  
  543.         return $this;
  544.     }
  545.  
  546.     /**
  547.      * Sets the offset and limit for the result set, if the database driver supports it.
  548.      *
  549.      * Usage:
  550.      * $query->setLimit(100, 0); (retrieve 100 rows, starting at first record)
  551.      * $query->setLimit(50, 50); (retrieve 50 rows, starting at 50th record)
  552.      *
  553.      * @param   integer  $limit   The limit for the result set
  554.      * @param   integer  $offset  The offset for the result set
  555.      *
  556.      * @return  JDatabaseQueryPostgresql  Returns this object to allow chaining.
  557.      *
  558.      * @since   12.1
  559.      */
  560.     public function setLimit($limit 0$offset 0)
  561.     {
  562.         $this->limit  = (int) $limit;
  563.         $this->offset = (int) $offset;
  564.  
  565.         return $this;
  566.     }
  567.  
  568.     /**
  569.      * Method to modify a query already in string format with the needed
  570.      * additions to make the query limited to a particular number of
  571.      * results, or start at a particular offset.
  572.      *
  573.      * @param   string   $query   The query in string format
  574.      * @param   integer  $limit   The limit for the result set
  575.      * @param   integer  $offset  The offset for the result set
  576.      *
  577.      * @return  string 
  578.      *
  579.      * @since   12.1
  580.      */
  581.     public function processLimit($query$limit$offset 0)
  582.     {
  583.         if ($limit 0)
  584.         {
  585.             $query .= ' LIMIT ' $limit;
  586.         }
  587.  
  588.         if ($offset 0)
  589.         {
  590.             $query .= ' OFFSET ' $offset;
  591.         }
  592.  
  593.         return $query;
  594.     }
  595.  
  596.     /**
  597.      * Add to the current date and time in Postgresql.
  598.      * Usage:
  599.      * $query->select($query->dateAdd());
  600.      * Prefixing the interval with a - (negative sign) will cause subtraction to be used.
  601.      *
  602.      * @param   datetime  $date      The date to add to
  603.      * @param   string    $interval  The string representation of the appropriate number of units
  604.      * @param   string    $datePart  The part of the date to perform the addition on
  605.      *
  606.      * @return  string  The string with the appropriate sql for addition of dates
  607.      *
  608.      * @since   13.1
  609.      * @note    Not all drivers support all units. Check appropriate references
  610.      * @link    http://www.postgresql.org/docs/9.0/static/functions-datetime.html.
  611.      */
  612.     public function dateAdd($date$interval$datePart)
  613.     {
  614.         if (substr($interval01!= '-')
  615.         {
  616.             return "timestamp '" $date "' + interval '" $interval " " $datePart "'";
  617.         }
  618.         else
  619.         {
  620.             return "timestamp '" $date "' - interval '" ltrim($interval'-'" " $datePart "'";
  621.         }
  622.     }
  623. }

Documentation generated on Tue, 19 Nov 2013 15:11:01 +0100 by phpDocumentor 1.4.3