PageRenderTime 173ms CodeModel.GetById 80ms app.highlight 59ms RepoModel.GetById 22ms app.codeStats 0ms

/vendor/Microsoft/WindowsAzure/Storage/TableEntityQuery.php

https://bitbucket.org/ktos/tinyshare
PHP | 339 lines | 141 code | 36 blank | 162 comment | 20 complexity | 418dadee28c3b26345f22bb3e61e4119 MD5 | raw file
  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/**
 38 * @category   Microsoft
 39 * @package    Microsoft_WindowsAzure
 40 * @subpackage Storage
 41 * @copyright  Copyright (c) 2009 - 2011, RealDolmen (http://www.realdolmen.com)
 42 * @license    http://phpazure.codeplex.com/license
 43 */
 44class Microsoft_WindowsAzure_Storage_TableEntityQuery
 45{
 46    /**
 47     * From
 48     * 
 49     * @var string
 50     */
 51	protected $_from  = '';
 52	
 53	/**
 54	 * Where
 55	 * 
 56	 * @var array
 57	 */
 58	protected $_where = array();
 59	
 60	/**
 61	 * Order by
 62	 * 
 63	 * @var array
 64	 */
 65	protected $_orderBy = array();
 66	
 67	/**
 68	 * Top
 69	 * 
 70	 * @var int
 71	 */
 72	protected $_top = null;
 73	
 74	/**
 75	 * Partition key
 76	 * 
 77	 * @var string
 78	 */
 79	protected $_partitionKey = null;
 80
 81	/**
 82	 * Row key
 83	 * 
 84	 * @var string
 85	 */
 86	protected $_rowKey = null;
 87	
 88	/**
 89	 * Select clause
 90	 * 
 91	 * @return Microsoft_WindowsAzure_Storage_TableEntityQuery
 92	 */
 93	public function select()
 94	{
 95		return $this;
 96	}
 97	
 98	/**
 99	 * From clause
100	 * 
101	 * @param string $name Table name to select entities from
102	 * @return Microsoft_WindowsAzure_Storage_TableEntityQuery
103	 */
104	public function from($name)
105	{
106		$this->_from = $name;
107		return $this;
108	}
109	
110	/**
111	 * Specify partition key
112	 * 
113	 * @param string $value Partition key to query for
114	 * @return Microsoft_WindowsAzure_Storage_TableEntityQuery
115	 */
116	public function wherePartitionKey($value = null)
117	{
118	    $this->_partitionKey = $value;
119	    return $this;
120	}
121	
122	/**
123	 * Specify row key
124	 * 
125	 * @param string $value Row key to query for
126	 * @return Microsoft_WindowsAzure_Storage_TableEntityQuery
127	 */
128	public function whereRowKey($value = null)
129	{
130	    $this->_rowKey = $value;
131	    return $this;
132	}
133	
134	/**
135	 * Add where clause
136	 * 
137	 * @param string       $condition   Condition, can contain question mark(s) (?) for parameter insertion.
138	 * @param string|array $value       Value(s) to insert in question mark (?) parameters.
139	 * @param string       $cond        Condition for the clause (and/or/not)
140	 * @return Microsoft_WindowsAzure_Storage_TableEntityQuery
141	 */
142	public function where($condition, $value = null, $cond = '')
143	{
144	    $condition = $this->_replaceOperators($condition);
145	    
146	    if (!is_null($value)) {
147	        $condition = $this->_quoteInto($condition, $value);
148	    }
149	    
150		if (count($this->_where) == 0) {
151			$cond = '';
152		} else if ($cond !== '') {
153			$cond = ' ' . strtolower(trim($cond)) . ' ';
154		}
155		
156		$this->_where[] = $cond . $condition;
157		return $this;
158	}
159
160	/**
161	 * Add where clause with AND condition
162	 * 
163	 * @param string       $condition   Condition, can contain question mark(s) (?) for parameter insertion.
164	 * @param string|array $value       Value(s) to insert in question mark (?) parameters.
165	 * @return Microsoft_WindowsAzure_Storage_TableEntityQuery
166	 */
167	public function andWhere($condition, $value = null)
168	{
169		return $this->where($condition, $value, 'and');
170	}
171	
172	/**
173	 * Add where clause with OR condition
174	 * 
175	 * @param string       $condition   Condition, can contain question mark(s) (?) for parameter insertion.
176	 * @param string|array $value       Value(s) to insert in question mark (?) parameters.
177	 * @return Microsoft_WindowsAzure_Storage_TableEntityQuery
178	 */
179	public function orWhere($condition, $value = null)
180	{
181		return $this->where($condition, $value, 'or');
182	}
183	
184	/**
185	 * OrderBy clause
186	 * 
187	 * @param string $column    Column to sort by
188	 * @param string $direction Direction to sort (asc/desc)
189	 * @return Microsoft_WindowsAzure_Storage_TableEntityQuery
190	 */
191	public function orderBy($column, $direction = 'asc')
192	{
193		$this->_orderBy[] = $column . ' ' . $direction;
194		return $this;
195	}
196    
197	/**
198	 * Top clause
199	 * 
200	 * @param int $top  Top to fetch
201	 * @return Microsoft_WindowsAzure_Storage_TableEntityQuery
202	 */
203    public function top($top = null)
204    {
205        $this->_top  = (int)$top;
206        return $this;
207    }
208	
209    /**
210     * Assembles the query 
211     * 
212     * @return array
213     */
214	public function assembleQuery()
215	{
216		$query = array();
217		if (count($this->_where) != 0) {
218		    $filter = implode('', $this->_where);
219			$query['$filter'] = $filter;
220		}
221		
222		if (count($this->_orderBy) != 0) {
223		    $orderBy = implode(',', $this->_orderBy);
224			$query['$orderby'] = $orderBy;
225		}
226		
227		if (!is_null($this->_top)) {
228			$query['$top'] = $this->_top;
229		}
230		
231		return $query;
232	}
233	
234	/**
235	 * Assemble from
236	 * 
237	 * @param boolean $includeParentheses
238	 * @return string
239	 */
240	public function assembleFrom($includeParentheses = true)
241	{
242	    $identifier = '';
243	    if ($includeParentheses) {
244	        $identifier .= '(';
245	        
246	        if (!is_null($this->_partitionKey)) {
247	            $identifier .= 'PartitionKey=\'' . rawurlencode($this->_partitionKey) . '\'';
248	        }
249	            
250	        if (!is_null($this->_partitionKey) && !is_null($this->_rowKey)) {
251	            $identifier .= ', ';
252	        }
253	            
254	        if (!is_null($this->_rowKey)) {
255	            $identifier .= 'RowKey=\'' . rawurlencode($this->_rowKey) . '\'';
256	        }
257	            
258	        $identifier .= ')';
259	    }
260		return $this->_from . $identifier;
261	}
262	
263	/**
264	 * Assemble full query
265	 * 
266	 * @return string
267	 */
268	public function assemble()
269	{
270		$assembledQuery = $this->assembleFrom();
271		
272		$query = $this->assembleQuery();
273		if (count($query) > 0) {
274			$queryString = '';
275			foreach ($query as $key => $value) {
276				$queryString .= ($queryString ? '&' : '?') . rawurlencode($key) . '=' . rawurlencode($value);
277			}			
278			$assembledQuery .= $queryString;
279		}
280		
281		return $assembledQuery;
282	}
283	
284	/**
285	 * Quotes a variable into a condition
286	 * 
287	 * @param string       $text   Condition, can contain question mark(s) (?) for parameter insertion.
288	 * @param string|array $value  Value(s) to insert in question mark (?) parameters.
289	 * @return string
290	 */
291	protected function _quoteInto($text, $value = null)
292	{
293		if (!is_array($value)) {
294	        $text = str_replace('?', '\'' . addslashes($value) . '\'', $text);
295	    } else {
296	        $i = 0;
297	        while(strpos($text, '?') !== false) {
298	            if (is_numeric($value[$i])) {
299	                $text = substr_replace($text, $value[$i++], strpos($text, '?'), 1);
300	            } else {
301	                $text = substr_replace($text, '\'' . addslashes($value[$i++]) . '\'', strpos($text, '?'), 1);
302	            }
303	        }
304	    }
305	    return $text;
306	}
307	
308	/**
309	 * Replace operators
310	 * 
311	 * @param string $text
312	 * @return string
313	 */
314	protected function _replaceOperators($text)
315	{
316	    $text = str_replace('==', 'eq',  $text);
317	    $text = str_replace('>',  'gt',  $text);
318	    $text = str_replace('<',  'lt',  $text);
319	    $text = str_replace('>=', 'ge',  $text);
320	    $text = str_replace('<=', 'le',  $text);
321	    $text = str_replace('!=', 'ne',  $text);
322	    
323	    $text = str_replace('&&', 'and', $text);
324	    $text = str_replace('||', 'or',  $text);
325	    $text = str_replace('!',  'not', $text);
326	    
327	    return $text;
328	}
329	
330	/**
331	 * __toString overload
332	 * 
333	 * @return string
334	 */
335	public function __toString()
336	{
337		return $this->assemble();
338	}
339}