web/lib/Zend/Cloud/DocumentService/Adapter/SimpleDb/Query.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     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 /*
       
    21  * @see Zend_Cloud_DocumentService_Query
       
    22  */
       
    23 require_once 'Zend/Cloud/DocumentService/Query.php';
       
    24 
       
    25 /**
       
    26  * Class implementing Query adapter for working with SimpleDb queries in a 
       
    27  * structured way
       
    28  *
       
    29  * @category   Zend
       
    30  * @package    Zend_Cloud
       
    31  * @subpackage DocumentService
       
    32  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    33  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    34  */
       
    35 class Zend_Cloud_DocumentService_Adapter_SimpleDb_Query
       
    36     extends Zend_Cloud_DocumentService_Query
       
    37 {
       
    38     /**
       
    39      * @var Zend_Cloud_DocumentService_Adapter_SimpleDb
       
    40      */
       
    41     protected $_adapter;
       
    42 
       
    43     /**
       
    44      * Constructor
       
    45      * 
       
    46      * @param  Zend_Cloud_DocumentService_Adapter_SimpleDb $adapter 
       
    47      * @param  null|string $collectionName
       
    48      * @return void
       
    49      */
       
    50     public function __construct(Zend_Cloud_DocumentService_Adapter_SimpleDb $adapter, $collectionName = null)
       
    51     {
       
    52         $this->_adapter = $adapter;
       
    53         if (null !== $collectionName) {
       
    54             $this->from($collectionName);
       
    55         }
       
    56     }
       
    57 
       
    58     /**
       
    59      * Get adapter
       
    60      * 
       
    61      * @return Zend_Cloud_DocumentService_Adapter_SimpleDb
       
    62      */
       
    63     public function getAdapter()
       
    64     {
       
    65         return $this->_adapter;
       
    66     }
       
    67 
       
    68     /**
       
    69      * Assemble the query into a format the adapter can utilize
       
    70      * 
       
    71      * @var    string $collectionName Name of collection from which to select
       
    72      * @return string
       
    73      */
       
    74     public function assemble($collectionName = null)
       
    75     {
       
    76         $adapter = $this->getAdapter()->getClient();
       
    77         $select  = null;
       
    78         $from    = null;
       
    79         $where   = null;
       
    80         $order   = null;
       
    81         $limit   = null;
       
    82         foreach ($this->getClauses() as $clause) {
       
    83             list($name, $args) = $clause;
       
    84 
       
    85             switch ($name) {
       
    86                 case self::QUERY_SELECT:
       
    87                     $select = $args[0];
       
    88                     break;
       
    89                 case self::QUERY_FROM:
       
    90                     if (null === $from) {
       
    91                         // Only allow setting FROM clause once
       
    92                         $from = $adapter->quoteName($args);
       
    93                     }
       
    94                     break;
       
    95                 case self::QUERY_WHERE:
       
    96                     $statement = $this->_parseWhere($args[0], $args[1]);
       
    97                     if (null === $where) {
       
    98                         $where = $statement;
       
    99                     } else {
       
   100                         $operator = empty($args[2]) ? 'AND' : $args[2];
       
   101                         $where .= ' ' . $operator . ' ' . $statement;
       
   102                     }
       
   103                     break;
       
   104                 case self::QUERY_WHEREID:
       
   105                     $statement = $this->_parseWhere('ItemName() = ?', array($args));
       
   106                     if (null === $where) {
       
   107                         $where = $statement;
       
   108                     } else {
       
   109                         $operator = empty($args[2]) ? 'AND' : $args[2];
       
   110                         $where .= ' ' . $operator . ' ' . $statement;
       
   111                     }
       
   112                     break;
       
   113                 case self::QUERY_ORDER:
       
   114                     $order = $adapter->quoteName($args[0]);
       
   115                     if (isset($args[1])) {
       
   116                         $order .= ' ' . $args[1];
       
   117                     }
       
   118                     break;
       
   119                 case self::QUERY_LIMIT:
       
   120                     $limit = $args;
       
   121                     break;
       
   122                 default:
       
   123                     // Ignore unknown clauses
       
   124                     break;
       
   125             }
       
   126         }
       
   127 
       
   128         if (empty($select)) {
       
   129             $select = "*";
       
   130         }
       
   131         if (empty($from)) {
       
   132             if (null === $collectionName) {
       
   133                 require_once 'Zend/Cloud/DocumentService/Exception.php';
       
   134                 throw new Zend_Cloud_DocumentService_Exception("Query requires a FROM clause");
       
   135             }
       
   136             $from = $adapter->quoteName($collectionName);
       
   137         }
       
   138         $query = "select $select from $from";
       
   139         if (!empty($where)) {
       
   140             $query .= " where $where";
       
   141         }
       
   142         if (!empty($order)) {
       
   143             $query .= " order by $order";
       
   144         }
       
   145         if (!empty($limit)) {
       
   146             $query .= " limit $limit";
       
   147         }
       
   148         return $query;
       
   149     }
       
   150 
       
   151     /**
       
   152      * Parse a where statement into service-specific language
       
   153      * 
       
   154      * @todo   Ensure this fulfills the entire SimpleDB query specification for WHERE
       
   155      * @param  string $where 
       
   156      * @param  array $args 
       
   157      * @return string
       
   158      */
       
   159     protected function _parseWhere($where, $args)
       
   160     {
       
   161         if (!is_array($args)) {
       
   162             $args = (array) $args;
       
   163         }
       
   164         $adapter = $this->getAdapter()->getClient();
       
   165         $i = 0;
       
   166         while (false !== ($pos = strpos($where, '?'))) {
       
   167            $where = substr_replace($where, $adapter->quote($args[$i]), $pos);
       
   168            ++$i;
       
   169         }
       
   170         if (('(' != $where[0]) || (')' != $where[strlen($where) - 1])) {
       
   171             $where = '(' . $where . ')';
       
   172         }
       
   173         return $where;
       
   174     }
       
   175  }