PageRenderTime 25ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/w3-total-cache/lib/Microsoft/WindowsAzure/Storage/TableEntityQuery.php

https://github.com/sharpmachine/wakeupmedia.com
PHP | 363 lines | 154 code | 40 blank | 169 comment | 22 complexity | 5f753bd31e7bf39406e76c1df3cae8f7 MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright (c) 2009 - 2010, 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 - 2010, 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 - 2010, 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 string
  191. *
  192. * @param boolean $urlEncode Apply URL encoding to the query string
  193. * @return string
  194. */
  195. public function assembleQueryString($urlEncode = false)
  196. {
  197. $query = array();
  198. if (count($this->_where) != 0) {
  199. $filter = implode('', $this->_where);
  200. $query[] = '$filter=' . ($urlEncode ? self::encodeQuery($filter) : $filter);
  201. }
  202. if (count($this->_orderBy) != 0) {
  203. $orderBy = implode(',', $this->_orderBy);
  204. $query[] = '$orderby=' . ($urlEncode ? self::encodeQuery($orderBy) : $orderBy);
  205. }
  206. if (!is_null($this->_top)) {
  207. $query[] = '$top=' . $this->_top;
  208. }
  209. if (count($query) != 0) {
  210. return '?' . implode('&', $query);
  211. }
  212. return '';
  213. }
  214. /**
  215. * Assemble from
  216. *
  217. * @param boolean $includeParentheses Include parentheses? ()
  218. * @return string
  219. */
  220. public function assembleFrom($includeParentheses = true)
  221. {
  222. $identifier = '';
  223. if ($includeParentheses) {
  224. $identifier .= '(';
  225. if (!is_null($this->_partitionKey)) {
  226. $identifier .= 'PartitionKey=\'' . $this->_partitionKey . '\'';
  227. }
  228. if (!is_null($this->_partitionKey) && !is_null($this->_rowKey)) {
  229. $identifier .= ', ';
  230. }
  231. if (!is_null($this->_rowKey)) {
  232. $identifier .= 'RowKey=\'' . $this->_rowKey . '\'';
  233. }
  234. $identifier .= ')';
  235. }
  236. return $this->_from . $identifier;
  237. }
  238. /**
  239. * Assemble full query
  240. *
  241. * @return string
  242. */
  243. public function assembleQuery()
  244. {
  245. $assembledQuery = $this->assembleFrom();
  246. $queryString = $this->assembleQueryString();
  247. if ($queryString !== '') {
  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. * urlencode a query
  296. *
  297. * @param string $query Query to encode
  298. * @return string Encoded query
  299. */
  300. public static function encodeQuery($query)
  301. {
  302. $query = str_replace('/', '%2F', $query);
  303. $query = str_replace('?', '%3F', $query);
  304. $query = str_replace(':', '%3A', $query);
  305. $query = str_replace('@', '%40', $query);
  306. $query = str_replace('&', '%26', $query);
  307. $query = str_replace('=', '%3D', $query);
  308. $query = str_replace('+', '%2B', $query);
  309. $query = str_replace(',', '%2C', $query);
  310. $query = str_replace('$', '%24', $query);
  311. $query = str_replace(' ', '%20', $query);
  312. return $query;
  313. }
  314. /**
  315. * __toString overload
  316. *
  317. * @return string
  318. */
  319. public function __toString()
  320. {
  321. return $this->assembleQuery();
  322. }
  323. }