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

https://github.com/kiranatama/sagalaya · PHP · 314 lines · 141 code · 37 blank · 136 comment · 23 complexity · 5d16001232c45ada15320bd624b36164 MD5 · raw file

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