/library/Service/WindowsAzure/Storage/TableEntityQuery.php

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