_adapter = $adapter; if (null !== $collectionName) { $this->from($collectionName); } } /** * Get adapter * * @return Zend_Cloud_DocumentService_Adapter_SimpleDb */ public function getAdapter() { return $this->_adapter; } /** * Assemble the query into a format the adapter can utilize * * @var string $collectionName Name of collection from which to select * @return string */ public function assemble($collectionName = null) { $adapter = $this->getAdapter()->getClient(); $select = null; $from = null; $where = null; $order = null; $limit = null; foreach ($this->getClauses() as $clause) { list($name, $args) = $clause; switch ($name) { case self::QUERY_SELECT: $select = $args[0]; break; case self::QUERY_FROM: if (null === $from) { // Only allow setting FROM clause once $from = $adapter->quoteName($args); } break; case self::QUERY_WHERE: $statement = $this->_parseWhere($args[0], $args[1]); if (null === $where) { $where = $statement; } else { $operator = empty($args[2]) ? 'AND' : $args[2]; $where .= ' ' . $operator . ' ' . $statement; } break; case self::QUERY_WHEREID: $statement = $this->_parseWhere('ItemName() = ?', array($args)); if (null === $where) { $where = $statement; } else { $operator = empty($args[2]) ? 'AND' : $args[2]; $where .= ' ' . $operator . ' ' . $statement; } break; case self::QUERY_ORDER: $order = $adapter->quoteName($args[0]); if (isset($args[1])) { $order .= ' ' . $args[1]; } break; case self::QUERY_LIMIT: $limit = $args; break; default: // Ignore unknown clauses break; } } if (empty($select)) { $select = "*"; } if (empty($from)) { if (null === $collectionName) { #require_once 'Zend/Cloud/DocumentService/Exception.php'; throw new Zend_Cloud_DocumentService_Exception("Query requires a FROM clause"); } $from = $adapter->quoteName($collectionName); } $query = "select $select from $from"; if (!empty($where)) { $query .= " where $where"; } if (!empty($order)) { $query .= " order by $order"; } if (!empty($limit)) { $query .= " limit $limit"; } return $query; } /** * Parse a where statement into service-specific language * * @todo Ensure this fulfills the entire SimpleDB query specification for WHERE * @param string $where * @param array $args * @return string */ protected function _parseWhere($where, $args) { if (!is_array($args)) { $args = (array) $args; } $adapter = $this->getAdapter()->getClient(); $i = 0; while (false !== ($pos = strpos($where, '?'))) { $where = substr_replace($where, $adapter->quote($args[$i]), $pos); ++$i; } if (('(' != $where[0]) || (')' != $where[strlen($where) - 1])) { $where = '(' . $where . ')'; } return $where; } }