/Zend/Service/WindowsAzure/Storage/TableEntityQuery.php

https://github.com/ftaiolivista/Zend-Framework-Namespaced- · PHP · 355 lines · 155 code · 41 blank · 159 comment · 22 complexity · 81b2ef5ca1c63126d2c5d5f852a0e631 MD5 · raw file

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