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