File indexing completed on 2025-01-26 05:25:30
0001 <?php 0002 /** 0003 * Zend Framework 0004 * 0005 * LICENSE 0006 * 0007 * This source file is subject to the new BSD license that is bundled 0008 * with this package in the file LICENSE.txt. 0009 * It is also available through the world-wide-web at this URL: 0010 * http://framework.zend.com/license/new-bsd 0011 * If you did not receive a copy of the license and are unable to 0012 * obtain it through the world-wide-web, please send an email 0013 * to license@zend.com so we can send you a copy immediately. 0014 * 0015 * @category Zend 0016 * @package Zend_Service_WindowsAzure 0017 * @subpackage Storage 0018 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0019 * @license http://framework.zend.com/license/new-bsd New BSD License 0020 * @version $Id$ 0021 */ 0022 0023 /** 0024 * @category Zend 0025 * @package Zend_Service_WindowsAzure 0026 * @subpackage Storage 0027 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com) 0028 * @license http://framework.zend.com/license/new-bsd New BSD License 0029 */ 0030 class Zend_Service_WindowsAzure_Storage_TableEntityQuery 0031 { 0032 /** 0033 * From 0034 * 0035 * @var string 0036 */ 0037 protected $_from = ''; 0038 0039 /** 0040 * Where 0041 * 0042 * @var array 0043 */ 0044 protected $_where = array(); 0045 0046 /** 0047 * Order by 0048 * 0049 * @var array 0050 */ 0051 protected $_orderBy = array(); 0052 0053 /** 0054 * Top 0055 * 0056 * @var int 0057 */ 0058 protected $_top = null; 0059 0060 /** 0061 * Partition key 0062 * 0063 * @var string 0064 */ 0065 protected $_partitionKey = null; 0066 0067 /** 0068 * Row key 0069 * 0070 * @var string 0071 */ 0072 protected $_rowKey = null; 0073 0074 /** 0075 * Select clause 0076 * 0077 * @return Zend_Service_WindowsAzure_Storage_TableEntityQuery 0078 */ 0079 public function select() 0080 { 0081 return $this; 0082 } 0083 0084 /** 0085 * From clause 0086 * 0087 * @param string $name Table name to select entities from 0088 * @return Zend_Service_WindowsAzure_Storage_TableEntityQuery 0089 */ 0090 public function from($name) 0091 { 0092 $this->_from = $name; 0093 return $this; 0094 } 0095 0096 /** 0097 * Specify partition key 0098 * 0099 * @param string $value Partition key to query for 0100 * @return Zend_Service_WindowsAzure_Storage_TableEntityQuery 0101 */ 0102 public function wherePartitionKey($value = null) 0103 { 0104 $this->_partitionKey = $value; 0105 return $this; 0106 } 0107 0108 /** 0109 * Specify row key 0110 * 0111 * @param string $value Row key to query for 0112 * @return Zend_Service_WindowsAzure_Storage_TableEntityQuery 0113 */ 0114 public function whereRowKey($value = null) 0115 { 0116 $this->_rowKey = $value; 0117 return $this; 0118 } 0119 0120 /** 0121 * Add where clause 0122 * 0123 * @param string $condition Condition, can contain question mark(s) (?) for parameter insertion. 0124 * @param string|array $value Value(s) to insert in question mark (?) parameters. 0125 * @param string $cond Condition for the clause (and/or/not) 0126 * @return Zend_Service_WindowsAzure_Storage_TableEntityQuery 0127 */ 0128 public function where($condition, $value = null, $cond = '') 0129 { 0130 $condition = $this->_replaceOperators($condition); 0131 0132 if (!is_null($value)) { 0133 $condition = $this->_quoteInto($condition, $value); 0134 } 0135 0136 if (count($this->_where) == 0) { 0137 $cond = ''; 0138 } else if ($cond !== '') { 0139 $cond = ' ' . strtolower(trim($cond)) . ' '; 0140 } 0141 0142 $this->_where[] = $cond . $condition; 0143 return $this; 0144 } 0145 0146 /** 0147 * Add where clause with AND condition 0148 * 0149 * @param string $condition Condition, can contain question mark(s) (?) for parameter insertion. 0150 * @param string|array $value Value(s) to insert in question mark (?) parameters. 0151 * @return Zend_Service_WindowsAzure_Storage_TableEntityQuery 0152 */ 0153 public function andWhere($condition, $value = null) 0154 { 0155 return $this->where($condition, $value, 'and'); 0156 } 0157 0158 /** 0159 * Add where clause with OR condition 0160 * 0161 * @param string $condition Condition, can contain question mark(s) (?) for parameter insertion. 0162 * @param string|array $value Value(s) to insert in question mark (?) parameters. 0163 * @return Zend_Service_WindowsAzure_Storage_TableEntityQuery 0164 */ 0165 public function orWhere($condition, $value = null) 0166 { 0167 return $this->where($condition, $value, 'or'); 0168 } 0169 0170 /** 0171 * OrderBy clause 0172 * 0173 * @param string $column Column to sort by 0174 * @param string $direction Direction to sort (asc/desc) 0175 * @return Zend_Service_WindowsAzure_Storage_TableEntityQuery 0176 */ 0177 public function orderBy($column, $direction = 'asc') 0178 { 0179 $this->_orderBy[] = $column . ' ' . $direction; 0180 return $this; 0181 } 0182 0183 /** 0184 * Top clause 0185 * 0186 * @param int $top Top to fetch 0187 * @return Zend_Service_WindowsAzure_Storage_TableEntityQuery 0188 */ 0189 public function top($top = null) 0190 { 0191 $this->_top = (int)$top; 0192 return $this; 0193 } 0194 0195 /** 0196 * Assembles the query string 0197 * 0198 * @param boolean $urlEncode Apply URL encoding to the query string 0199 * @return string 0200 */ 0201 public function assembleQueryString($urlEncode = false) 0202 { 0203 $query = array(); 0204 if (count($this->_where) != 0) { 0205 $filter = implode('', $this->_where); 0206 $query[] = '$filter=' . ($urlEncode ? self::encodeQuery($filter) : $filter); 0207 } 0208 0209 if (count($this->_orderBy) != 0) { 0210 $orderBy = implode(',', $this->_orderBy); 0211 $query[] = '$orderby=' . ($urlEncode ? self::encodeQuery($orderBy) : $orderBy); 0212 } 0213 0214 if (!is_null($this->_top)) { 0215 $query[] = '$top=' . $this->_top; 0216 } 0217 0218 if (count($query) != 0) { 0219 return '?' . implode('&', $query); 0220 } 0221 0222 return ''; 0223 } 0224 0225 /** 0226 * Assemble from 0227 * 0228 * @param boolean $includeParentheses Include parentheses? () 0229 * @return string 0230 */ 0231 public function assembleFrom($includeParentheses = true) 0232 { 0233 $identifier = ''; 0234 if ($includeParentheses) { 0235 $identifier .= '('; 0236 0237 if (!is_null($this->_partitionKey)) { 0238 $identifier .= 'PartitionKey=\'' . self::encodeQuery($this->_partitionKey) . '\''; 0239 } 0240 0241 if (!is_null($this->_partitionKey) && !is_null($this->_rowKey)) { 0242 $identifier .= ', '; 0243 } 0244 0245 if (!is_null($this->_rowKey)) { 0246 $identifier .= 'RowKey=\'' . self::encodeQuery($this->_rowKey) . '\''; 0247 } 0248 0249 $identifier .= ')'; 0250 } 0251 return $this->_from . $identifier; 0252 } 0253 0254 /** 0255 * Assemble full query 0256 * 0257 * @return string 0258 */ 0259 public function assembleQuery() 0260 { 0261 $assembledQuery = $this->assembleFrom(); 0262 0263 $queryString = $this->assembleQueryString(); 0264 if ($queryString !== '') { 0265 $assembledQuery .= $queryString; 0266 } 0267 0268 return $assembledQuery; 0269 } 0270 0271 /** 0272 * Quotes a variable into a condition 0273 * 0274 * @param string $text Condition, can contain question mark(s) (?) for parameter insertion. 0275 * @param string|array $value Value(s) to insert in question mark (?) parameters. 0276 * @return string 0277 */ 0278 protected function _quoteInto($text, $value = null) 0279 { 0280 if (!is_array($value)) { 0281 $text = str_replace('?', '\'' . addslashes($value) . '\'', $text); 0282 } else { 0283 $i = 0; 0284 while(strpos($text, '?') !== false) { 0285 if (is_numeric($value[$i])) { 0286 $text = substr_replace($text, $value[$i++], strpos($text, '?'), 1); 0287 } else { 0288 $text = substr_replace($text, '\'' . addslashes($value[$i++]) . '\'', strpos($text, '?'), 1); 0289 } 0290 } 0291 } 0292 return $text; 0293 } 0294 0295 /** 0296 * Replace operators 0297 * 0298 * @param string $text 0299 * @return string 0300 */ 0301 protected function _replaceOperators($text) 0302 { 0303 $text = str_replace('==', 'eq', $text); 0304 $text = str_replace('>', 'gt', $text); 0305 $text = str_replace('<', 'lt', $text); 0306 $text = str_replace('>=', 'ge', $text); 0307 $text = str_replace('<=', 'le', $text); 0308 $text = str_replace('!=', 'ne', $text); 0309 0310 $text = str_replace('&&', 'and', $text); 0311 $text = str_replace('||', 'or', $text); 0312 $text = str_replace('!', 'not', $text); 0313 0314 return $text; 0315 } 0316 0317 /** 0318 * urlencode a query 0319 * 0320 * @param string $query Query to encode 0321 * @return string Encoded query 0322 */ 0323 public static function encodeQuery($query) 0324 { 0325 $query = str_replace('/', '%2F', $query); 0326 $query = str_replace('?', '%3F', $query); 0327 $query = str_replace(':', '%3A', $query); 0328 $query = str_replace('@', '%40', $query); 0329 $query = str_replace('&', '%26', $query); 0330 $query = str_replace('=', '%3D', $query); 0331 $query = str_replace('+', '%2B', $query); 0332 $query = str_replace(',', '%2C', $query); 0333 $query = str_replace('$', '%24', $query); 0334 $query = str_replace('{', '%7B', $query); 0335 $query = str_replace('}', '%7D', $query); 0336 0337 $query = str_replace(' ', '%20', $query); 0338 0339 return $query; 0340 } 0341 0342 /** 0343 * __toString overload 0344 * 0345 * @return string 0346 */ 0347 public function __toString() 0348 { 0349 return $this->assembleQuery(); 0350 } 0351 }