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