Query.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. /**
  3. * LICENSE
  4. *
  5. * This source file is subject to the new BSD license that is bundled
  6. * with this package in the file LICENSE.txt.
  7. * It is also available through the world-wide-web at this URL:
  8. * http://framework.zend.com/license/new-bsd
  9. * If you did not receive a copy of the license and are unable to
  10. * obtain it through the world-wide-web, please send an email
  11. * to license@zend.com so we can send you a copy immediately.
  12. *
  13. * @category Zend
  14. * @package Zend_Cloud
  15. * @subpackage DocumentService
  16. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  17. * @license http://framework.zend.com/license/new-bsd New BSD License
  18. */
  19. /*
  20. * @see Zend_Cloud_DocumentService_QueryAdapter
  21. */
  22. require_once 'Zend/Cloud/DocumentService/QueryAdapter.php';
  23. /**
  24. * Class implementing Query adapter for working with Azure queries in a
  25. * structured way
  26. *
  27. * @todo Look into preventing a query injection attack.
  28. * @category Zend
  29. * @package Zend_Cloud
  30. * @subpackage DocumentService
  31. * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. */
  34. class Zend_Cloud_DocumentService_Adapter_WindowsAzure_Query
  35. implements Zend_Cloud_DocumentService_QueryAdapter
  36. {
  37. /**
  38. * Azure concrete query
  39. *
  40. * @var Zend_Service_WindowsAzure_Storage_TableEntityQuery
  41. */
  42. protected $_azureSelect;
  43. /**
  44. * Constructor
  45. *
  46. * @param null|Zend_Service_WindowsAzure_Storage_TableEntityQuery $select Table select object
  47. * @return void
  48. */
  49. public function __construct($select = null)
  50. {
  51. if (!$select instanceof Zend_Service_WindowsAzure_Storage_TableEntityQuery) {
  52. require_once 'Zend/Service/WindowsAzure/Storage/TableEntityQuery.php';
  53. $select = new Zend_Service_WindowsAzure_Storage_TableEntityQuery();
  54. }
  55. $this->_azureSelect = $select;
  56. }
  57. /**
  58. * SELECT clause (fields to be selected)
  59. *
  60. * Does nothing for Azure.
  61. *
  62. * @param string $select
  63. * @return Zend_Cloud_DocumentService_Adapter_WindowsAzure_Query
  64. */
  65. public function select($select)
  66. {
  67. return $this;
  68. }
  69. /**
  70. * FROM clause (table name)
  71. *
  72. * @param string $from
  73. * @return Zend_Cloud_DocumentService_Adapter_WindowsAzure_Query
  74. */
  75. public function from($from)
  76. {
  77. $this->_azureSelect->from($from);
  78. return $this;
  79. }
  80. /**
  81. * WHERE clause (conditions to be used)
  82. *
  83. * @param string $where
  84. * @param mixed $value Value or array of values to be inserted instead of ?
  85. * @param string $op Operation to use to join where clauses (AND/OR)
  86. * @return Zend_Cloud_DocumentService_Adapter_WindowsAzure_Query
  87. */
  88. public function where($where, $value = null, $op = 'and')
  89. {
  90. if (!empty($value) && !is_array($value)) {
  91. // fix buglet in Azure - numeric values are quoted unless passed as an array
  92. $value = array($value);
  93. }
  94. $this->_azureSelect->where($where, $value, $op);
  95. return $this;
  96. }
  97. /**
  98. * WHERE clause for item ID
  99. *
  100. * This one should be used when fetching specific rows since some adapters
  101. * have special syntax for primary keys
  102. *
  103. * @param array $value Row ID for the document (PartitionKey, RowKey)
  104. * @return Zend_Cloud_DocumentService_Adapter_WindowsAzure_Query
  105. */
  106. public function whereId($value)
  107. {
  108. if (!is_array($value)) {
  109. require_once 'Zend/Cloud/DocumentService/Exception.php';
  110. throw new Zend_Cloud_DocumentService_Exception('Invalid document key');
  111. }
  112. $this->_azureSelect->wherePartitionKey($value[0])->whereRowKey($value[1]);
  113. return $this;
  114. }
  115. /**
  116. * LIMIT clause (how many rows to return)
  117. *
  118. * @param int $limit
  119. * @return Zend_Cloud_DocumentService_Adapter_WindowsAzure_Query
  120. */
  121. public function limit($limit)
  122. {
  123. $this->_azureSelect->top($limit);
  124. return $this;
  125. }
  126. /**
  127. * ORDER BY clause (sorting)
  128. *
  129. * @todo Azure service doesn't seem to support this yet; emulate?
  130. * @param string $sort Column to sort by
  131. * @param string $direction Direction - asc/desc
  132. * @return Zend_Cloud_DocumentService_Adapter_WindowsAzure_Query
  133. * @throws Zend_Cloud_OperationNotAvailableException
  134. */
  135. public function order($sort, $direction = 'asc')
  136. {
  137. require_once 'Zend/Cloud/OperationNotAvailableException.php';
  138. throw new Zend_Cloud_OperationNotAvailableException('No support for sorting for Azure yet');
  139. }
  140. /**
  141. * Get Azure select query
  142. *
  143. * @return Zend_Service_WindowsAzure_Storage_TableEntityQuery
  144. */
  145. public function getAzureSelect()
  146. {
  147. return $this->_azureSelect;
  148. }
  149. /**
  150. * Assemble query
  151. *
  152. * Simply return the WindowsAzure table entity query object
  153. *
  154. * @return Zend_Service_WindowsAzure_Storage_TableEntityQuery
  155. */
  156. public function assemble()
  157. {
  158. return $this->getAzureSelect();
  159. }
  160. }