PageRenderTime 41ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Cloud/DocumentService/Adapter/SimpleDb/Query.php

https://bitbucket.org/andrewjleavitt/magestudy
PHP | 175 lines | 104 code | 8 blank | 63 comment | 20 complexity | 1574247460cb96af6e32ae3455266862 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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-2010 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_Query
  21. */
  22. #require_once 'Zend/Cloud/DocumentService/Query.php';
  23. /**
  24. * Class implementing Query adapter for working with SimpleDb queries in a
  25. * structured way
  26. *
  27. * @category Zend
  28. * @package Zend_Cloud
  29. * @subpackage DocumentService
  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 Zend_Cloud_DocumentService_Adapter_SimpleDb_Query
  34. extends Zend_Cloud_DocumentService_Query
  35. {
  36. /**
  37. * @var Zend_Cloud_DocumentService_Adapter_SimpleDb
  38. */
  39. protected $_adapter;
  40. /**
  41. * Constructor
  42. *
  43. * @param Zend_Cloud_DocumentService_Adapter_SimpleDb $adapter
  44. * @param null|string $collectionName
  45. * @return void
  46. */
  47. public function __construct(Zend_Cloud_DocumentService_Adapter_SimpleDb $adapter, $collectionName = null)
  48. {
  49. $this->_adapter = $adapter;
  50. if (null !== $collectionName) {
  51. $this->from($collectionName);
  52. }
  53. }
  54. /**
  55. * Get adapter
  56. *
  57. * @return Zend_Cloud_DocumentService_Adapter_SimpleDb
  58. */
  59. public function getAdapter()
  60. {
  61. return $this->_adapter;
  62. }
  63. /**
  64. * Assemble the query into a format the adapter can utilize
  65. *
  66. * @var string $collectionName Name of collection from which to select
  67. * @return string
  68. */
  69. public function assemble($collectionName = null)
  70. {
  71. $adapter = $this->getAdapter()->getClient();
  72. $select = null;
  73. $from = null;
  74. $where = null;
  75. $order = null;
  76. $limit = null;
  77. foreach ($this->getClauses() as $clause) {
  78. list($name, $args) = $clause;
  79. switch ($name) {
  80. case self::QUERY_SELECT:
  81. $select = $args[0];
  82. break;
  83. case self::QUERY_FROM:
  84. if (null === $from) {
  85. // Only allow setting FROM clause once
  86. $from = $adapter->quoteName($args);
  87. }
  88. break;
  89. case self::QUERY_WHERE:
  90. $statement = $this->_parseWhere($args[0], $args[1]);
  91. if (null === $where) {
  92. $where = $statement;
  93. } else {
  94. $operator = empty($args[2]) ? 'AND' : $args[2];
  95. $where .= ' ' . $operator . ' ' . $statement;
  96. }
  97. break;
  98. case self::QUERY_WHEREID:
  99. $statement = $this->_parseWhere('ItemName() = ?', array($args));
  100. if (null === $where) {
  101. $where = $statement;
  102. } else {
  103. $operator = empty($args[2]) ? 'AND' : $args[2];
  104. $where .= ' ' . $operator . ' ' . $statement;
  105. }
  106. break;
  107. case self::QUERY_ORDER:
  108. $order = $adapter->quoteName($args[0]);
  109. if (isset($args[1])) {
  110. $order .= ' ' . $args[1];
  111. }
  112. break;
  113. case self::QUERY_LIMIT:
  114. $limit = $args;
  115. break;
  116. default:
  117. // Ignore unknown clauses
  118. break;
  119. }
  120. }
  121. if (empty($select)) {
  122. $select = "*";
  123. }
  124. if (empty($from)) {
  125. if (null === $collectionName) {
  126. #require_once 'Zend/Cloud/DocumentService/Exception.php';
  127. throw new Zend_Cloud_DocumentService_Exception("Query requires a FROM clause");
  128. }
  129. $from = $adapter->quoteName($collectionName);
  130. }
  131. $query = "select $select from $from";
  132. if (!empty($where)) {
  133. $query .= " where $where";
  134. }
  135. if (!empty($order)) {
  136. $query .= " order by $order";
  137. }
  138. if (!empty($limit)) {
  139. $query .= " limit $limit";
  140. }
  141. return $query;
  142. }
  143. /**
  144. * Parse a where statement into service-specific language
  145. *
  146. * @todo Ensure this fulfills the entire SimpleDB query specification for WHERE
  147. * @param string $where
  148. * @param array $args
  149. * @return string
  150. */
  151. protected function _parseWhere($where, $args)
  152. {
  153. if (!is_array($args)) {
  154. $args = (array) $args;
  155. }
  156. $adapter = $this->getAdapter()->getClient();
  157. $i = 0;
  158. while (false !== ($pos = strpos($where, '?'))) {
  159. $where = substr_replace($where, $adapter->quote($args[$i]), $pos);
  160. ++$i;
  161. }
  162. if (('(' != $where[0]) || (')' != $where[strlen($where) - 1])) {
  163. $where = '(' . $where . ')';
  164. }
  165. return $where;
  166. }
  167. }