Microsoft_WindowsAzure
[ class tree: Microsoft_WindowsAzure ] [ index: Microsoft_WindowsAzure ] [ all elements ]

Source for file TableEntityQuery.php

Documentation is available at TableEntityQuery.php

  1. <?php
  2. /**
  3.  * Copyright (c) 2009, RealDolmen
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions are met:
  8.  *     * Redistributions of source code must retain the above copyright
  9.  *       notice, this list of conditions and the following disclaimer.
  10.  *     * Redistributions in binary form must reproduce the above copyright
  11.  *       notice, this list of conditions and the following disclaimer in the
  12.  *       documentation and/or other materials provided with the distribution.
  13.  *     * Neither the name of RealDolmen nor the
  14.  *       names of its contributors may be used to endorse or promote products
  15.  *       derived from this software without specific prior written permission.
  16.  *
  17.  * THIS SOFTWARE IS PROVIDED BY RealDolmen ''AS IS'' AND ANY
  18.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19.  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20.  * DISCLAIMED. IN NO EVENT SHALL RealDolmen BE LIABLE FOR ANY
  21.  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  24.  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26.  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27.  *
  28.  * @category   Microsoft
  29.  * @package    Microsoft_WindowsAzure
  30.  * @subpackage Storage
  31.  * @copyright  Copyright (c) 2009, RealDolmen (http://www.realdolmen.com)
  32.  * @license    http://phpazure.codeplex.com/license
  33.  * @version    $Id: Blob.php 14561 2009-05-07 08:05:12Z unknown $
  34.  */
  35.  
  36. /**
  37.  * @category   Microsoft
  38.  * @package    Microsoft_WindowsAzure
  39.  * @subpackage Storage
  40.  * @copyright  Copyright (c) 2009, RealDolmen (http://www.realdolmen.com)
  41.  * @license    http://phpazure.codeplex.com/license
  42.  */
  43. {
  44.     /**
  45.      * From
  46.      * 
  47.      * @var string 
  48.      */
  49.     protected $_from  = '';
  50.     
  51.     /**
  52.      * Where
  53.      * 
  54.      * @var array 
  55.      */
  56.     protected $_where = array();
  57.     
  58.     /**
  59.      * Order by
  60.      * 
  61.      * @var array 
  62.      */
  63.     protected $_orderBy = array();
  64.     
  65.     /**
  66.      * Top
  67.      * 
  68.      * @var int 
  69.      */
  70.     protected $_top = null;
  71.     
  72.     /**
  73.      * Partition key
  74.      * 
  75.      * @var string 
  76.      */
  77.     protected $_partitionKey = null;
  78.  
  79.     /**
  80.      * Row key
  81.      * 
  82.      * @var string 
  83.      */
  84.     protected $_rowKey = null;
  85.     
  86.     /**
  87.      * Select clause
  88.      * 
  89.      * @return Microsoft_WindowsAzure_Storage_TableEntityQuery 
  90.      */
  91.     public function select()
  92.     {
  93.         return $this;
  94.     }
  95.     
  96.     /**
  97.      * From clause
  98.      * 
  99.      * @param string $name Table name to select entities from
  100.      * @return Microsoft_WindowsAzure_Storage_TableEntityQuery 
  101.      */
  102.     public function from($name)
  103.     {
  104.         $this->_from = $name;
  105.         return $this;
  106.     }
  107.     
  108.     /**
  109.      * Specify partition key
  110.      * 
  111.      * @param string $value Partition key to query for
  112.      * @return Microsoft_WindowsAzure_Storage_TableEntityQuery 
  113.      */
  114.     public function wherePartitionKey($value null)
  115.     {
  116.         $this->_partitionKey = $value;
  117.         return $this;
  118.     }
  119.     
  120.     /**
  121.      * Specify row key
  122.      * 
  123.      * @param string $value Row key to query for
  124.      * @return Microsoft_WindowsAzure_Storage_TableEntityQuery 
  125.      */
  126.     public function whereRowKey($value null)
  127.     {
  128.         $this->_rowKey = $value;
  129.         return $this;
  130.     }
  131.     
  132.     /**
  133.      * Add where clause
  134.      * 
  135.      * @param string       $condition   Condition, can contain question mark(s) (?) for parameter insertion.
  136.      * @param string|array$value       Value(s) to insert in question mark (?) parameters.
  137.      * @param string       $cond        Condition for the clause (and/or/not)
  138.      * @return Microsoft_WindowsAzure_Storage_TableEntityQuery 
  139.      */
  140.     public function where($condition$value null$cond '')
  141.     {
  142.         $condition $this->replaceOperators($condition);
  143.         
  144.         if (!is_null($value))
  145.         {
  146.             $condition $this->quoteInto($condition$value);
  147.         }
  148.         
  149.         if (count($this->_where== 0)
  150.         {
  151.             $cond '';
  152.         }
  153.         else if ($cond !== '')
  154.         {
  155.             $cond ' ' strtolower(trim($cond)) ' ';
  156.         }
  157.         
  158.         $this->_where[$cond $condition;
  159.         return $this;
  160.     }
  161.  
  162.     /**
  163.      * Add where clause with AND condition
  164.      * 
  165.      * @param string       $condition   Condition, can contain question mark(s) (?) for parameter insertion.
  166.      * @param string|array$value       Value(s) to insert in question mark (?) parameters.
  167.      * @return Microsoft_WindowsAzure_Storage_TableEntityQuery 
  168.      */
  169.     public function andWhere($condition$value null)
  170.     {
  171.         return $this->where($condition$value'and');
  172.     }
  173.     
  174.     /**
  175.      * Add where clause with OR condition
  176.      * 
  177.      * @param string       $condition   Condition, can contain question mark(s) (?) for parameter insertion.
  178.      * @param string|array$value       Value(s) to insert in question mark (?) parameters.
  179.      * @return Microsoft_WindowsAzure_Storage_TableEntityQuery 
  180.      */
  181.     public function orWhere($condition$value null)
  182.     {
  183.         return $this->where($condition$value'or');
  184.     }
  185.     
  186.     /**
  187.      * OrderBy clause
  188.      * 
  189.      * @param string $column    Column to sort by
  190.      * @param string $direction Direction to sort (asc/desc)
  191.      * @return Microsoft_WindowsAzure_Storage_TableEntityQuery 
  192.      */
  193.     public function orderBy($column$direction 'asc')
  194.     {
  195.         $this->_orderBy[$column ' ' $direction;
  196.         return $this;
  197.     }
  198.     
  199.     /**
  200.      * Top clause
  201.      * 
  202.      * @param int $top  Top to fetch
  203.      * @return Microsoft_WindowsAzure_Storage_TableEntityQuery 
  204.      */
  205.     public function top($top null)
  206.     {
  207.         $this->_top  = (int)$top;
  208.         return $this;
  209.     }
  210.     
  211.     /**
  212.      * Assembles the query string
  213.      * 
  214.      * @param boolean $urlEncode Apply URL encoding to the query string
  215.      * @return string 
  216.      */
  217.     public function assembleQueryString($urlEncode false)
  218.     {
  219.         $query array();
  220.         if (count($this->_where!= 0)
  221.         {
  222.             $filter implode(''$this->_where);
  223.             $query['$filter=' ($urlEncode urlencode($filter$filter);
  224.         }
  225.         if (count($this->_orderBy!= 0)
  226.         {
  227.             $orderBy implode(','$this->_orderBy);
  228.             $query['$orderby=' ($urlEncode urlencode($orderBy$orderBy);
  229.         }
  230.         if (!is_null($this->_top))
  231.         {
  232.             $query['$top=' $this->_top;
  233.         }
  234.         if (count($query!= 0)
  235.         {
  236.             return '?' implode('&'$query);
  237.         }
  238.         
  239.         return '';
  240.     }
  241.     
  242.     /**
  243.      * Assemble from
  244.      * 
  245.      * @param boolean $includeParentheses Include parentheses? ()
  246.      * @return string 
  247.      */
  248.     public function assembleFrom($includeParentheses true)
  249.     {
  250.         $identifier '';
  251.         if ($includeParentheses)
  252.         {
  253.             $identifier .= '(';
  254.             
  255.             if (!is_null($this->_partitionKey))
  256.                 $identifier .= 'PartitionKey=\'' $this->_partitionKey . '\'';
  257.                 
  258.             if (!is_null($this->_partitionKey&& !is_null($this->_rowKey))
  259.                 $identifier .= ', ';
  260.                 
  261.             if (!is_null($this->_rowKey))
  262.                 $identifier .= 'RowKey=\'' $this->_rowKey . '\'';
  263.                 
  264.             $identifier .= ')';
  265.         }
  266.         return $this->_from . $identifier;
  267.     }
  268.     
  269.     /**
  270.      * Assemble full query
  271.      * 
  272.      * @return string 
  273.      */
  274.     public function assembleQuery()
  275.     {
  276.         $assembledQuery $this->assembleFrom();
  277.         
  278.         $queryString $this->assembleQueryString();
  279.         if ($queryString !== '')
  280.             $assembledQuery .= $queryString;
  281.         
  282.         return $assembledQuery;
  283.     }
  284.     
  285.     /**
  286.      * Quotes a variable into a condition
  287.      * 
  288.      * @param string       $text   Condition, can contain question mark(s) (?) for parameter insertion.
  289.      * @param string|array$value  Value(s) to insert in question mark (?) parameters.
  290.      * @return string 
  291.      */
  292.     protected function quoteInto($text$value null)
  293.     {
  294.         if (!is_array($value))
  295.         {
  296.             $text str_replace('?''\'' addslashes($value'\''$text);
  297.         }
  298.         else
  299.         {
  300.             $i 0;
  301.             while(strpos($text'?'!== false)
  302.             {
  303.                 if (is_numeric($value[$i]))
  304.                 {
  305.                     $text substr_replace($text$value[$i++]strpos($text'?')1);
  306.                 }
  307.                 else
  308.                 {
  309.                     $text substr_replace($text'\'' addslashes($value[$i++]'\''strpos($text'?')1);
  310.                 }
  311.             }
  312.         }
  313.         return $text;
  314.     }
  315.     
  316.     /**
  317.      * Replace operators
  318.      * 
  319.      * @param string $text 
  320.      * @return string 
  321.      */
  322.     protected function replaceOperators($text)
  323.     {
  324.         $text str_replace('==''eq',  $text);
  325.         $text str_replace('>',  'gt',  $text);
  326.         $text str_replace('<',  'lt',  $text);
  327.         $text str_replace('>=''ge',  $text);
  328.         $text str_replace('<=''le',  $text);
  329.         $text str_replace('!=''ne',  $text);
  330.         
  331.         $text str_replace('&&''and'$text);
  332.         $text str_replace('||''or',  $text);
  333.         $text str_replace('!',  'not'$text);
  334.         
  335.         return $text;
  336.     }
  337.     
  338.     /**
  339.      * __toString overload
  340.      * 
  341.      * @return string 
  342.      */
  343.     public function __toString()
  344.     {
  345.         return $this->assembleQuery();
  346.     }
  347. }

Documentation generated on Thu, 26 Nov 2009 08:05:37 +0100 by phpDocumentor 1.4.3