File indexing completed on 2024-12-29 05:27:29

0001 <?php
0002 /**
0003  * LICENSE
0004  *
0005  * This source file is subject to the new BSD license that is bundled
0006  * with this package in the file LICENSE.txt.
0007  * It is also available through the world-wide-web at this URL:
0008  * http://framework.zend.com/license/new-bsd
0009  * If you did not receive a copy of the license and are unable to
0010  * obtain it through the world-wide-web, please send an email
0011  * to license@zend.com so we can send you a copy immediately.
0012  *
0013  * @category   Zend
0014  * @package    Zend_Cloud
0015  * @subpackage DocumentService
0016  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0017  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0018  */
0019 
0020 /*
0021  * @see Zend_Cloud_DocumentService_Query
0022  */
0023 // require_once 'Zend/Cloud/DocumentService/Query.php';
0024 
0025 /**
0026  * Class implementing Query adapter for working with SimpleDb queries in a
0027  * structured way
0028  *
0029  * @category   Zend
0030  * @package    Zend_Cloud
0031  * @subpackage DocumentService
0032  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0033  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0034  */
0035 class Zend_Cloud_DocumentService_Adapter_SimpleDb_Query
0036     extends Zend_Cloud_DocumentService_Query
0037 {
0038     /**
0039      * @var Zend_Cloud_DocumentService_Adapter_SimpleDb
0040      */
0041     protected $_adapter;
0042 
0043     /**
0044      * Constructor
0045      *
0046      * @param  Zend_Cloud_DocumentService_Adapter_SimpleDb $adapter
0047      * @param  null|string $collectionName
0048      * @return void
0049      */
0050     public function __construct(Zend_Cloud_DocumentService_Adapter_SimpleDb $adapter, $collectionName = null)
0051     {
0052         $this->_adapter = $adapter;
0053         if (null !== $collectionName) {
0054             $this->from($collectionName);
0055         }
0056     }
0057 
0058     /**
0059      * Get adapter
0060      *
0061      * @return Zend_Cloud_DocumentService_Adapter_SimpleDb
0062      */
0063     public function getAdapter()
0064     {
0065         return $this->_adapter;
0066     }
0067 
0068     /**
0069      * Assemble the query into a format the adapter can utilize
0070      *
0071      * @var    string $collectionName Name of collection from which to select
0072      * @return string
0073      */
0074     public function assemble($collectionName = null)
0075     {
0076         $adapter = $this->getAdapter()->getClient();
0077         $select  = null;
0078         $from    = null;
0079         $where   = null;
0080         $order   = null;
0081         $limit   = null;
0082         foreach ($this->getClauses() as $clause) {
0083             list($name, $args) = $clause;
0084 
0085             switch ($name) {
0086                 case self::QUERY_SELECT:
0087                     $select = $args[0];
0088                     break;
0089                 case self::QUERY_FROM:
0090                     if (null === $from) {
0091                         // Only allow setting FROM clause once
0092                         $from = $adapter->quoteName($args);
0093                     }
0094                     break;
0095                 case self::QUERY_WHERE:
0096                     $statement = $this->_parseWhere($args[0], $args[1]);
0097                     if (null === $where) {
0098                         $where = $statement;
0099                     } else {
0100                         $operator = empty($args[2]) ? 'AND' : $args[2];
0101                         $where .= ' ' . $operator . ' ' . $statement;
0102                     }
0103                     break;
0104                 case self::QUERY_WHEREID:
0105                     $statement = $this->_parseWhere('ItemName() = ?', array($args));
0106                     if (null === $where) {
0107                         $where = $statement;
0108                     } else {
0109                         $operator = empty($args[2]) ? 'AND' : $args[2];
0110                         $where .= ' ' . $operator . ' ' . $statement;
0111                     }
0112                     break;
0113                 case self::QUERY_ORDER:
0114                     $order = $adapter->quoteName($args[0]);
0115                     if (isset($args[1])) {
0116                         $order .= ' ' . $args[1];
0117                     }
0118                     break;
0119                 case self::QUERY_LIMIT:
0120                     $limit = $args;
0121                     break;
0122                 default:
0123                     // Ignore unknown clauses
0124                     break;
0125             }
0126         }
0127 
0128         if (empty($select)) {
0129             $select = "*";
0130         }
0131         if (empty($from)) {
0132             if (null === $collectionName) {
0133                 // require_once 'Zend/Cloud/DocumentService/Exception.php';
0134                 throw new Zend_Cloud_DocumentService_Exception("Query requires a FROM clause");
0135             }
0136             $from = $adapter->quoteName($collectionName);
0137         }
0138         $query = "select $select from $from";
0139         if (!empty($where)) {
0140             $query .= " where $where";
0141         }
0142         if (!empty($order)) {
0143             $query .= " order by $order";
0144         }
0145         if (!empty($limit)) {
0146             $query .= " limit $limit";
0147         }
0148         return $query;
0149     }
0150 
0151     /**
0152      * Parse a where statement into service-specific language
0153      *
0154      * @todo   Ensure this fulfills the entire SimpleDB query specification for WHERE
0155      * @param  string $where
0156      * @param  array $args
0157      * @return string
0158      */
0159     protected function _parseWhere($where, $args)
0160     {
0161         if (!is_array($args)) {
0162             $args = (array) $args;
0163         }
0164         $adapter = $this->getAdapter()->getClient();
0165         $i = 0;
0166         while (false !== ($pos = strpos($where, '?'))) {
0167            $where = substr_replace($where, $adapter->quote($args[$i]), $pos);
0168            ++$i;
0169         }
0170         if (('(' != $where[0]) || (')' != $where[strlen($where) - 1])) {
0171             $where = '(' . $where . ')';
0172         }
0173         return $where;
0174     }
0175  }