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_QueryAdapter
0022  */
0023 // require_once 'Zend/Cloud/DocumentService/QueryAdapter.php';
0024 
0025 /**
0026  * Class implementing Query adapter for working with Azure queries in a
0027  * structured way
0028  *
0029  * @todo       Look into preventing a query injection attack.
0030  * @category   Zend
0031  * @package    Zend_Cloud
0032  * @subpackage DocumentService
0033  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0034  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0035  */
0036 class Zend_Cloud_DocumentService_Adapter_WindowsAzure_Query
0037     implements Zend_Cloud_DocumentService_QueryAdapter
0038 {
0039     /**
0040      * Azure concrete query
0041      *
0042      * @var Zend_Service_WindowsAzure_Storage_TableEntityQuery
0043      */
0044     protected $_azureSelect;
0045 
0046     /**
0047      * Constructor
0048      *
0049      * @param  null|Zend_Service_WindowsAzure_Storage_TableEntityQuery $select Table select object
0050      * @return void
0051      */
0052     public function __construct($select = null)
0053     {
0054         if (!$select instanceof Zend_Service_WindowsAzure_Storage_TableEntityQuery) {
0055             // require_once 'Zend/Service/WindowsAzure/Storage/TableEntityQuery.php';
0056             $select = new Zend_Service_WindowsAzure_Storage_TableEntityQuery();
0057         }
0058         $this->_azureSelect = $select;
0059     }
0060 
0061     /**
0062      * SELECT clause (fields to be selected)
0063      *
0064      * Does nothing for Azure.
0065      *
0066      * @param  string $select
0067      * @return Zend_Cloud_DocumentService_Adapter_WindowsAzure_Query
0068      */
0069     public function select($select)
0070     {
0071         return $this;
0072     }
0073 
0074     /**
0075      * FROM clause (table name)
0076      *
0077      * @param string $from
0078      * @return Zend_Cloud_DocumentService_Adapter_WindowsAzure_Query
0079      */
0080     public function from($from)
0081     {
0082         $this->_azureSelect->from($from);
0083         return $this;
0084     }
0085 
0086     /**
0087      * WHERE clause (conditions to be used)
0088      *
0089      * @param string $where
0090      * @param mixed $value Value or array of values to be inserted instead of ?
0091      * @param string $op Operation to use to join where clauses (AND/OR)
0092      * @return Zend_Cloud_DocumentService_Adapter_WindowsAzure_Query
0093      */
0094     public function where($where, $value = null, $op = 'and')
0095     {
0096         if (!empty($value) && !is_array($value)) {
0097             // fix buglet in Azure - numeric values are quoted unless passed as an array
0098             $value = array($value);
0099         }
0100         $this->_azureSelect->where($where, $value, $op);
0101         return $this;
0102     }
0103 
0104     /**
0105      * WHERE clause for item ID
0106      *
0107      * This one should be used when fetching specific rows since some adapters
0108      * have special syntax for primary keys
0109      *
0110      * @param  array $value Row ID for the document (PartitionKey, RowKey)
0111      * @return Zend_Cloud_DocumentService_Adapter_WindowsAzure_Query
0112      */
0113     public function whereId($value)
0114     {
0115         if (!is_array($value)) {
0116             // require_once 'Zend/Cloud/DocumentService/Exception.php';
0117             throw new Zend_Cloud_DocumentService_Exception('Invalid document key');
0118         }
0119         $this->_azureSelect->wherePartitionKey($value[0])->whereRowKey($value[1]);
0120         return $this;
0121     }
0122 
0123     /**
0124      * LIMIT clause (how many rows to return)
0125      *
0126      * @param  int $limit
0127      * @return Zend_Cloud_DocumentService_Adapter_WindowsAzure_Query
0128      */
0129     public function limit($limit)
0130     {
0131         $this->_azureSelect->top($limit);
0132         return $this;
0133     }
0134 
0135     /**
0136      * ORDER BY clause (sorting)
0137      *
0138      * @todo   Azure service doesn't seem to support this yet; emulate?
0139      * @param  string $sort Column to sort by
0140      * @param  string $direction Direction - asc/desc
0141      * @return Zend_Cloud_DocumentService_Adapter_WindowsAzure_Query
0142      * @throws Zend_Cloud_OperationNotAvailableException
0143      */
0144     public function order($sort, $direction = 'asc')
0145     {
0146         // require_once 'Zend/Cloud/OperationNotAvailableException.php';
0147         throw new Zend_Cloud_OperationNotAvailableException('No support for sorting for Azure yet');
0148     }
0149 
0150     /**
0151      * Get Azure select query
0152      *
0153      * @return Zend_Service_WindowsAzure_Storage_TableEntityQuery
0154      */
0155     public function getAzureSelect()
0156     {
0157         return  $this->_azureSelect;
0158     }
0159 
0160     /**
0161      * Assemble query
0162      *
0163      * Simply return the WindowsAzure table entity query object
0164      *
0165      * @return Zend_Service_WindowsAzure_Storage_TableEntityQuery
0166      */
0167     public function assemble()
0168     {
0169         return $this->getAzureSelect();
0170     }
0171 }