[ Index ]

PHP Cross Reference of moodle-2.8

title

Body

[close]

/lib/zend/Zend/Service/WindowsAzure/Storage/ -> TableEntityQuery.php (source)

   1  <?php
   2  /**
   3   * Zend Framework
   4   *
   5   * LICENSE
   6   *
   7   * This source file is subject to the new BSD license that is bundled
   8   * with this package in the file LICENSE.txt.
   9   * It is also available through the world-wide-web at this URL:
  10   * http://framework.zend.com/license/new-bsd
  11   * If you did not receive a copy of the license and are unable to
  12   * obtain it through the world-wide-web, please send an email
  13   * to [email protected] so we can send you a copy immediately.
  14   *
  15   * @category   Zend
  16   * @package    Zend_Service_WindowsAzure
  17   * @subpackage Storage
  18   * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19   * @license    http://framework.zend.com/license/new-bsd     New BSD License
  20   * @version    $Id$
  21   */
  22  
  23  /**
  24   * @category   Zend
  25   * @package    Zend_Service_WindowsAzure
  26   * @subpackage Storage
  27   * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  28   * @license    http://framework.zend.com/license/new-bsd     New BSD License
  29   */
  30  class Zend_Service_WindowsAzure_Storage_TableEntityQuery
  31  {
  32      /**
  33       * From
  34       * 
  35       * @var string
  36       */
  37      protected $_from  = '';
  38      
  39      /**
  40       * Where
  41       * 
  42       * @var array
  43       */
  44      protected $_where = array();
  45      
  46      /**
  47       * Order by
  48       * 
  49       * @var array
  50       */
  51      protected $_orderBy = array();
  52      
  53      /**
  54       * Top
  55       * 
  56       * @var int
  57       */
  58      protected $_top = null;
  59      
  60      /**
  61       * Partition key
  62       * 
  63       * @var string
  64       */
  65      protected $_partitionKey = null;
  66  
  67      /**
  68       * Row key
  69       * 
  70       * @var string
  71       */
  72      protected $_rowKey = null;
  73      
  74      /**
  75       * Select clause
  76       * 
  77       * @return Zend_Service_WindowsAzure_Storage_TableEntityQuery
  78       */
  79  	public function select()
  80      {
  81          return $this;
  82      }
  83      
  84      /**
  85       * From clause
  86       * 
  87       * @param string $name Table name to select entities from
  88       * @return Zend_Service_WindowsAzure_Storage_TableEntityQuery
  89       */
  90  	public function from($name)
  91      {
  92          $this->_from = $name;
  93          return $this;
  94      }
  95      
  96      /**
  97       * Specify partition key
  98       * 
  99       * @param string $value Partition key to query for
 100       * @return Zend_Service_WindowsAzure_Storage_TableEntityQuery
 101       */
 102  	public function wherePartitionKey($value = null)
 103      {
 104          $this->_partitionKey = $value;
 105          return $this;
 106      }
 107      
 108      /**
 109       * Specify row key
 110       * 
 111       * @param string $value Row key to query for
 112       * @return Zend_Service_WindowsAzure_Storage_TableEntityQuery
 113       */
 114  	public function whereRowKey($value = null)
 115      {
 116          $this->_rowKey = $value;
 117          return $this;
 118      }
 119      
 120      /**
 121       * Add where clause
 122       * 
 123       * @param string       $condition   Condition, can contain question mark(s) (?) for parameter insertion.
 124       * @param string|array $value       Value(s) to insert in question mark (?) parameters.
 125       * @param string       $cond        Condition for the clause (and/or/not)
 126       * @return Zend_Service_WindowsAzure_Storage_TableEntityQuery
 127       */
 128  	public function where($condition, $value = null, $cond = '')
 129      {
 130          $condition = $this->_replaceOperators($condition);
 131          
 132          if (!is_null($value)) {
 133              $condition = $this->_quoteInto($condition, $value);
 134          }
 135          
 136          if (count($this->_where) == 0) {
 137              $cond = '';
 138          } else if ($cond !== '') {
 139              $cond = ' ' . strtolower(trim($cond)) . ' ';
 140          }
 141          
 142          $this->_where[] = $cond . $condition;
 143          return $this;
 144      }
 145  
 146      /**
 147       * Add where clause with AND condition
 148       * 
 149       * @param string       $condition   Condition, can contain question mark(s) (?) for parameter insertion.
 150       * @param string|array $value       Value(s) to insert in question mark (?) parameters.
 151       * @return Zend_Service_WindowsAzure_Storage_TableEntityQuery
 152       */
 153  	public function andWhere($condition, $value = null)
 154      {
 155          return $this->where($condition, $value, 'and');
 156      }
 157      
 158      /**
 159       * Add where clause with OR 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 Zend_Service_WindowsAzure_Storage_TableEntityQuery
 164       */
 165  	public function orWhere($condition, $value = null)
 166      {
 167          return $this->where($condition, $value, 'or');
 168      }
 169      
 170      /**
 171       * OrderBy clause
 172       * 
 173       * @param string $column    Column to sort by
 174       * @param string $direction Direction to sort (asc/desc)
 175       * @return Zend_Service_WindowsAzure_Storage_TableEntityQuery
 176       */
 177  	public function orderBy($column, $direction = 'asc')
 178      {
 179          $this->_orderBy[] = $column . ' ' . $direction;
 180          return $this;
 181      }
 182      
 183      /**
 184       * Top clause
 185       * 
 186       * @param int $top  Top to fetch
 187       * @return Zend_Service_WindowsAzure_Storage_TableEntityQuery
 188       */
 189      public function top($top = null)
 190      {
 191          $this->_top  = (int)$top;
 192          return $this;
 193      }
 194      
 195      /**
 196       * Assembles the query string
 197       * 
 198       * @param boolean $urlEncode Apply URL encoding to the query string
 199       * @return string
 200       */
 201  	public function assembleQueryString($urlEncode = false)
 202      {
 203          $query = array();
 204          if (count($this->_where) != 0) {
 205              $filter = implode('', $this->_where);
 206              $query[] = '$filter=' . ($urlEncode ? urlencode($filter) : $filter);
 207          }
 208          
 209          if (count($this->_orderBy) != 0) {
 210              $orderBy = implode(',', $this->_orderBy);
 211              $query[] = '$orderby=' . ($urlEncode ? urlencode($orderBy) : $orderBy);
 212          }
 213          
 214          if (!is_null($this->_top)) {
 215              $query[] = '$top=' . $this->_top;
 216          }
 217          
 218          if (count($query) != 0) {
 219              return '?' . implode('&', $query);
 220          }
 221          
 222          return '';
 223      }
 224      
 225      /**
 226       * Assemble from
 227       * 
 228       * @param boolean $includeParentheses Include parentheses? ()
 229       * @return string
 230       */
 231  	public function assembleFrom($includeParentheses = true)
 232      {
 233          $identifier = '';
 234          if ($includeParentheses) {
 235              $identifier .= '(';
 236              
 237              if (!is_null($this->_partitionKey)) {
 238                  $identifier .= 'PartitionKey=\'' . $this->_partitionKey . '\'';
 239              }
 240                  
 241              if (!is_null($this->_partitionKey) && !is_null($this->_rowKey)) {
 242                  $identifier .= ', ';
 243              }
 244                  
 245              if (!is_null($this->_rowKey)) {
 246                  $identifier .= 'RowKey=\'' . $this->_rowKey . '\'';
 247              }
 248                  
 249              $identifier .= ')';
 250          }
 251          return $this->_from . $identifier;
 252      }
 253      
 254      /**
 255       * Assemble full query
 256       * 
 257       * @return string
 258       */
 259  	public function assembleQuery()
 260      {
 261          $assembledQuery = $this->assembleFrom();
 262          
 263          $queryString = $this->assembleQueryString();
 264          if ($queryString !== '') {
 265              $assembledQuery .= $queryString;
 266          }
 267          
 268          return $assembledQuery;
 269      }
 270      
 271      /**
 272       * Quotes a variable into a condition
 273       * 
 274       * @param string       $text   Condition, can contain question mark(s) (?) for parameter insertion.
 275       * @param string|array $value  Value(s) to insert in question mark (?) parameters.
 276       * @return string
 277       */
 278  	protected function _quoteInto($text, $value = null)
 279      {
 280          if (!is_array($value)) {
 281              $text = str_replace('?', '\'' . addslashes($value) . '\'', $text);
 282          } else {
 283              $i = 0;
 284              while(strpos($text, '?') !== false) {
 285                  if (is_numeric($value[$i])) {
 286                      $text = substr_replace($text, $value[$i++], strpos($text, '?'), 1);
 287                  } else {
 288                      $text = substr_replace($text, '\'' . addslashes($value[$i++]) . '\'', strpos($text, '?'), 1);
 289                  }
 290              }
 291          }
 292          return $text;
 293      }
 294      
 295      /**
 296       * Replace operators
 297       * 
 298       * @param string $text
 299       * @return string
 300       */
 301  	protected function _replaceOperators($text)
 302      {
 303          $text = str_replace('==', 'eq',  $text);
 304          $text = str_replace('>',  'gt',  $text);
 305          $text = str_replace('<',  'lt',  $text);
 306          $text = str_replace('>=', 'ge',  $text);
 307          $text = str_replace('<=', 'le',  $text);
 308          $text = str_replace('!=', 'ne',  $text);
 309          
 310          $text = str_replace('&&', 'and', $text);
 311          $text = str_replace('||', 'or',  $text);
 312          $text = str_replace('!',  'not', $text);
 313          
 314          return $text;
 315      }
 316      
 317      /**
 318       * __toString overload
 319       * 
 320       * @return string
 321       */
 322  	public function __toString()
 323      {
 324          return $this->assembleQuery();
 325      }
 326  }


Generated: Fri Nov 28 20:29:05 2014 Cross-referenced by PHPXref 0.7.1