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 - 2011, 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 - 2011, 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 - 2011, 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.             $condition $this->_quoteInto($condition$value);
  146.         }
  147.         
  148.         if (count($this->_where== 0{
  149.             $cond '';
  150.         else if ($cond !== ''{
  151.             $cond ' ' strtolower(trim($cond)) ' ';
  152.         }
  153.         
  154.         $this->_where[$cond $condition;
  155.         return $this;
  156.     }
  157.  
  158.     /**
  159.      * Add where clause with AND condition
  160.      * 
  161.      * @param string       $condition   Condition, can contain question mark(s) (?) for parameter insertion.
  162.      * @param string|array$value       Value(s) to insert in question mark (?) parameters.
  163.      * @return Microsoft_WindowsAzure_Storage_TableEntityQuery 
  164.      */
  165.     public function andWhere($condition$value null)
  166.     {
  167.         return $this->where($condition$value'and');
  168.     }
  169.     
  170.     /**
  171.      * Add where clause with OR condition
  172.      * 
  173.      * @param string       $condition   Condition, can contain question mark(s) (?) for parameter insertion.
  174.      * @param string|array$value       Value(s) to insert in question mark (?) parameters.
  175.      * @return Microsoft_WindowsAzure_Storage_TableEntityQuery 
  176.      */
  177.     public function orWhere($condition$value null)
  178.     {
  179.         return $this->where($condition$value'or');
  180.     }
  181.     
  182.     /**
  183.      * OrderBy clause
  184.      * 
  185.      * @param string $column    Column to sort by
  186.      * @param string $direction Direction to sort (asc/desc)
  187.      * @return Microsoft_WindowsAzure_Storage_TableEntityQuery 
  188.      */
  189.     public function orderBy($column$direction 'asc')
  190.     {
  191.         $this->_orderBy[$column ' ' $direction;
  192.         return $this;
  193.     }
  194.     
  195.     /**
  196.      * Top clause
  197.      * 
  198.      * @param int $top  Top to fetch
  199.      * @return Microsoft_WindowsAzure_Storage_TableEntityQuery 
  200.      */
  201.     public function top($top null)
  202.     {
  203.         $this->_top  = (int)$top;
  204.         return $this;
  205.     }
  206.     
  207.     /**
  208.      * Assembles the query string
  209.      * 
  210.      * @param boolean $urlEncode Apply URL encoding to the query string
  211.      * @return string 
  212.      */
  213.     public function assembleQueryString($urlEncode false)
  214.     {
  215.         $query array();
  216.         if (count($this->_where!= 0{
  217.             $filter implode(''$this->_where);
  218.             $query['$filter=' ($urlEncode self::encodeQuery($filter$filter);
  219.         }
  220.         
  221.         if (count($this->_orderBy!= 0{
  222.             $orderBy implode(','$this->_orderBy);
  223.             $query['$orderby=' ($urlEncode self::encodeQuery($orderBy$orderBy);
  224.         }
  225.         
  226.         if (!is_null($this->_top)) {
  227.             $query['$top=' $this->_top;
  228.         }
  229.         
  230.         if (count($query!= 0{
  231.             return '?' implode('&'$query);
  232.         }
  233.         
  234.         return '';
  235.     }
  236.     
  237.     /**
  238.      * Assemble from
  239.      * 
  240.      * @param boolean $includeParentheses Include parentheses? ()
  241.      * @return string 
  242.      */
  243.     public function assembleFrom($includeParentheses true)
  244.     {
  245.         $identifier '';
  246.         if ($includeParentheses{
  247.             $identifier .= '(';
  248.             
  249.             if (!is_null($this->_partitionKey)) {
  250.                 $identifier .= 'PartitionKey=\'' self::encodeQuery($this->_partitionKey'\'';
  251.             }
  252.                 
  253.             if (!is_null($this->_partitionKey&& !is_null($this->_rowKey)) {
  254.                 $identifier .= ', ';
  255.             }
  256.                 
  257.             if (!is_null($this->_rowKey)) {
  258.                 $identifier .= 'RowKey=\'' self::encodeQuery($this->_rowKey'\'';
  259.             }
  260.                 
  261.             $identifier .= ')';
  262.         }
  263.         return $this->_from . $identifier;
  264.     }
  265.     
  266.     /**
  267.      * Assemble full query
  268.      * 
  269.      * @return string 
  270.      */
  271.     public function assembleQuery()
  272.     {
  273.         $assembledQuery $this->assembleFrom();
  274.         
  275.         $queryString $this->assembleQueryString();
  276.         if ($queryString !== ''{
  277.             $assembledQuery .= $queryString;
  278.         }
  279.         
  280.         return $assembledQuery;
  281.     }
  282.     
  283.     /**
  284.      * Quotes a variable into a condition
  285.      * 
  286.      * @param string       $text   Condition, can contain question mark(s) (?) for parameter insertion.
  287.      * @param string|array$value  Value(s) to insert in question mark (?) parameters.
  288.      * @return string 
  289.      */
  290.     protected function _quoteInto($text$value null)
  291.     {
  292.         if (!is_array($value)) {
  293.             $text str_replace('?''\'' addslashes($value'\''$text);
  294.         else {
  295.             $i 0;
  296.             while(strpos($text'?'!== false{
  297.                 if (is_numeric($value[$i])) {
  298.                     $text substr_replace($text$value[$i++]strpos($text'?')1);
  299.                 else {
  300.                     $text substr_replace($text'\'' addslashes($value[$i++]'\''strpos($text'?')1);
  301.                 }
  302.             }
  303.         }
  304.         return $text;
  305.     }
  306.     
  307.     /**
  308.      * Replace operators
  309.      * 
  310.      * @param string $text 
  311.      * @return string 
  312.      */
  313.     protected function _replaceOperators($text)
  314.     {
  315.         $text str_replace('==''eq',  $text);
  316.         $text str_replace('>',  'gt',  $text);
  317.         $text str_replace('<',  'lt',  $text);
  318.         $text str_replace('>=''ge',  $text);
  319.         $text str_replace('<=''le',  $text);
  320.         $text str_replace('!=''ne',  $text);
  321.         
  322.         $text str_replace('&&''and'$text);
  323.         $text str_replace('||''or',  $text);
  324.         $text str_replace('!',  'not'$text);
  325.         
  326.         return $text;
  327.     }
  328.     
  329.     /**
  330.      * urlencode a query
  331.      * 
  332.      * @param string $query Query to encode
  333.      * @return string Encoded query
  334.      */
  335.     public static function encodeQuery($query)
  336.     {
  337.         $query str_replace('/''%2F'$query);
  338.         $query str_replace('?''%3F'$query);
  339.         $query str_replace(':''%3A'$query);
  340.         $query str_replace('@''%40'$query);
  341.         $query str_replace('&''%26'$query);
  342.         $query str_replace('=''%3D'$query);
  343.         $query str_replace('+''%2B'$query);
  344.         $query str_replace(',''%2C'$query);
  345.         $query str_replace('$''%24'$query);
  346.         $query str_replace('{''%7B'$query);
  347.         $query str_replace('}''%7D'$query);
  348.  
  349.         $query str_replace(' ''%20'$query);
  350.         
  351.         return $query;
  352.     }
  353.     
  354.     /**
  355.      * __toString overload
  356.      * 
  357.      * @return string 
  358.      */
  359.     public function __toString()
  360.     {
  361.         return $this->assembleQuery();
  362.     }
  363. }

Documentation generated on Wed, 18 May 2011 12:07:00 +0200 by phpDocumentor 1.4.3