web/lib/Zend/Cloud/DocumentService/Query.php
changeset 807 877f952ae2bd
parent 207 621fa6caec0c
child 1230 68c69c656a2c
equal deleted inserted replaced
805:5e7a0fedabdf 807:877f952ae2bd
    11  * to license@zend.com so we can send you a copy immediately.
    11  * to license@zend.com so we can send you a copy immediately.
    12  *
    12  *
    13  * @category   Zend
    13  * @category   Zend
    14  * @package    Zend_Cloud
    14  * @package    Zend_Cloud
    15  * @subpackage DocumentService
    15  * @subpackage DocumentService
    16  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    16  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    17  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    17  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    18  */
    18  */
    19 
    19 
    20 require_once 'Zend/Cloud/DocumentService/QueryAdapter.php';
    20 require_once 'Zend/Cloud/DocumentService/QueryAdapter.php';
    21 
    21 
    22 /**
    22 /**
    23  * Generic query object
    23  * Generic query object
    24  *
    24  *
    25  * Aggregates operations in an array of clauses, where the first element 
    25  * Aggregates operations in an array of clauses, where the first element
    26  * describes the clause type, and the next element describes the criteria.
    26  * describes the clause type, and the next element describes the criteria.
    27  *
    27  *
    28  * @category   Zend
    28  * @category   Zend
    29  * @package    Zend_Cloud
    29  * @package    Zend_Cloud
    30  * @subpackage DocumentService
    30  * @subpackage DocumentService
    31  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
    31  * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
    32  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    32  * @license    http://framework.zend.com/license/new-bsd     New BSD License
    33  */
    33  */
    34 class Zend_Cloud_DocumentService_Query
    34 class Zend_Cloud_DocumentService_Query
    35     implements Zend_Cloud_DocumentService_QueryAdapter
    35     implements Zend_Cloud_DocumentService_QueryAdapter
    36 {
    36 {
    44     const QUERY_LIMIT   = 'limit';
    44     const QUERY_LIMIT   = 'limit';
    45     const QUERY_ORDER   = 'order';
    45     const QUERY_ORDER   = 'order';
    46 
    46 
    47     /**
    47     /**
    48      * Clause list
    48      * Clause list
    49      * 
    49      *
    50      * @var array
    50      * @var array
    51      */
    51      */
    52     protected $_clauses = array();
    52     protected $_clauses = array();
    53 
    53 
    54     /**
    54     /**
    55      * Generic clause
    55      * Generic clause
    56      * 
    56      *
    57      * You can use any clause by doing $query->foo('bar')
    57      * You can use any clause by doing $query->foo('bar')
    58      * but concrete adapters should be able to recognise it
    58      * but concrete adapters should be able to recognise it
    59      * 
    59      *
    60      * The call will be iterpreted as clause 'foo' with argument 'bar' 
    60      * The call will be iterpreted as clause 'foo' with argument 'bar'
    61      * 
    61      *
    62      * @param  string $name Clause/method name
    62      * @param  string $name Clause/method name
    63      * @param  mixed $args
    63      * @param  mixed $args
    64      * @return Zend_Cloud_DocumentService_Query
    64      * @return Zend_Cloud_DocumentService_Query
    65      */
    65      */
    66     public function __call($name, $args) 
    66     public function __call($name, $args)
    67     {
    67     {
    68         $this->_clauses[] = array(strtolower($name), $args);
    68         $this->_clauses[] = array(strtolower($name), $args);
    69         return $this;
    69         return $this;
    70     }
    70     }
    71 
    71 
    72     /**
    72     /**
    73      * SELECT clause (fields to be selected)
    73      * SELECT clause (fields to be selected)
    74      * 
    74      *
    75      * @param  null|string|array $select 
    75      * @param  null|string|array $select
    76      * @return Zend_Cloud_DocumentService_Query
    76      * @return Zend_Cloud_DocumentService_Query
    77      */
    77      */
    78     public function select($select)
    78     public function select($select)
    79     {
    79     {
    80         if (empty($select)) {
    80         if (empty($select)) {
    81             return $this;
    81             return $this;
    82         }
    82         }
    83         if (!is_string($select) && !is_array($select)) {
    83         if (!is_string($select) && !is_array($select)) {
    84             require_once 'Zend/Cloud/DocumentService/Exception.php';           
    84             require_once 'Zend/Cloud/DocumentService/Exception.php';
    85             throw new Zend_Cloud_DocumentService_Exception("SELECT argument must be a string or an array of strings");
    85             throw new Zend_Cloud_DocumentService_Exception("SELECT argument must be a string or an array of strings");
    86         }
    86         }
    87         $this->_clauses[] = array(self::QUERY_SELECT, $select);
    87         $this->_clauses[] = array(self::QUERY_SELECT, $select);
    88         return $this;
    88         return $this;
    89     }
    89     }
    90     
    90 
    91     /**
    91     /**
    92      * FROM clause
    92      * FROM clause
    93      * 
    93      *
    94      * @param string $name Field names  
    94      * @param string $name Field names
    95      * @return Zend_Cloud_DocumentService_Query
    95      * @return Zend_Cloud_DocumentService_Query
    96      */
    96      */
    97     public function from($name)
    97     public function from($name)
    98     {
    98     {
    99         if(!is_string($name)) {
    99         if(!is_string($name)) {
   100             require_once 'Zend/Cloud/DocumentService/Exception.php';           
   100             require_once 'Zend/Cloud/DocumentService/Exception.php';
   101             throw new Zend_Cloud_DocumentService_Exception("FROM argument must be a string");
   101             throw new Zend_Cloud_DocumentService_Exception("FROM argument must be a string");
   102         }
   102         }
   103         $this->_clauses[] = array(self::QUERY_FROM, $name);
   103         $this->_clauses[] = array(self::QUERY_FROM, $name);
   104         return $this;
   104         return $this;
   105     }
   105     }
   106     
   106 
   107     /**
   107     /**
   108      * WHERE query
   108      * WHERE query
   109      * 
   109      *
   110      * @param string $cond Condition
   110      * @param string $cond Condition
   111      * @param array $args Arguments to substitute instead of ?'s in condition
   111      * @param array $args Arguments to substitute instead of ?'s in condition
   112      * @param string $op relation to other clauses - and/or
   112      * @param string $op relation to other clauses - and/or
   113      * @return Zend_Cloud_DocumentService_Query
   113      * @return Zend_Cloud_DocumentService_Query
   114      */
   114      */
   115     public function where($cond, $value = null, $op = 'and')
   115     public function where($cond, $value = null, $op = 'and')
   116     {
   116     {
   117         if (!is_string($cond)) {
   117         if (!is_string($cond)) {
   118             require_once 'Zend/Cloud/DocumentService/Exception.php';           
   118             require_once 'Zend/Cloud/DocumentService/Exception.php';
   119             throw new Zend_Cloud_DocumentService_Exception("WHERE argument must be a string");
   119             throw new Zend_Cloud_DocumentService_Exception("WHERE argument must be a string");
   120         }
   120         }
   121         $this->_clauses[] = array(self::QUERY_WHERE, array($cond, $value, $op));
   121         $this->_clauses[] = array(self::QUERY_WHERE, array($cond, $value, $op));
   122         return $this;
   122         return $this;
   123     }
   123     }
   124 
   124 
   125     /**
   125     /**
   126      * Select record or fields by ID
   126      * Select record or fields by ID
   127      * 
   127      *
   128      * @param  string|int $value Identifier to select by
   128      * @param  string|int $value Identifier to select by
   129      * @return Zend_Cloud_DocumentService_Query
   129      * @return Zend_Cloud_DocumentService_Query
   130      */
   130      */
   131     public function whereId($value)
   131     public function whereId($value)
   132     {
   132     {
   133         if (!is_scalar($value)) {
   133         if (!is_scalar($value)) {
   134             require_once 'Zend/Cloud/DocumentService/Exception.php';           
   134             require_once 'Zend/Cloud/DocumentService/Exception.php';
   135             throw new Zend_Cloud_DocumentService_Exception("WHEREID argument must be a scalar");
   135             throw new Zend_Cloud_DocumentService_Exception("WHEREID argument must be a scalar");
   136         }
   136         }
   137         $this->_clauses[] = array(self::QUERY_WHEREID, $value);
   137         $this->_clauses[] = array(self::QUERY_WHEREID, $value);
   138         return $this;
   138         return $this;
   139     }
   139     }
   140 
   140 
   141     /**
   141     /**
   142      * LIMIT clause (how many items to return)
   142      * LIMIT clause (how many items to return)
   143      * 
   143      *
   144      * @param  int $limit 
   144      * @param  int $limit
   145      * @return Zend_Cloud_DocumentService_Query
   145      * @return Zend_Cloud_DocumentService_Query
   146      */
   146      */
   147     public function limit($limit)
   147     public function limit($limit)
   148     {
   148     {
   149         if ($limit != (int) $limit) {
   149         if ($limit != (int) $limit) {
   150             require_once 'Zend/Cloud/DocumentService/Exception.php';           
   150             require_once 'Zend/Cloud/DocumentService/Exception.php';
   151             throw new Zend_Cloud_DocumentService_Exception("LIMIT argument must be an integer");
   151             throw new Zend_Cloud_DocumentService_Exception("LIMIT argument must be an integer");
   152         }
   152         }
   153         $this->_clauses[] = array(self::QUERY_LIMIT, $limit);
   153         $this->_clauses[] = array(self::QUERY_LIMIT, $limit);
   154         return $this;
   154         return $this;
   155     }
   155     }
   156 
   156 
   157     /**
   157     /**
   158      * ORDER clause; field or fields to sort by, and direction to sort
   158      * ORDER clause; field or fields to sort by, and direction to sort
   159      * 
   159      *
   160      * @param  string|int|array $sort 
   160      * @param  string|int|array $sort
   161      * @param  string $direction 
   161      * @param  string $direction
   162      * @return Zend_Cloud_DocumentService_Query
   162      * @return Zend_Cloud_DocumentService_Query
   163      */
   163      */
   164     public function order($sort, $direction = 'asc')
   164     public function order($sort, $direction = 'asc')
   165     {
   165     {
   166         $this->_clauses[] = array(self::QUERY_ORDER, array($sort, $direction));
   166         $this->_clauses[] = array(self::QUERY_ORDER, array($sort, $direction));
   169 
   169 
   170     /**
   170     /**
   171      * "Assemble" the query
   171      * "Assemble" the query
   172      *
   172      *
   173      * Simply returns the clauses present.
   173      * Simply returns the clauses present.
   174      * 
   174      *
   175      * @return array
   175      * @return array
   176      */
   176      */
   177     public function assemble()
   177     public function assemble()
   178     {
   178     {
   179         return $this->getClauses();
   179         return $this->getClauses();
   180     }
   180     }
   181     
   181 
   182     /**
   182     /**
   183      * Return query clauses as an array
   183      * Return query clauses as an array
   184      * 
   184      *
   185      * @return array Clauses in the query
   185      * @return array Clauses in the query
   186      */
   186      */
   187     public function getClauses()
   187     public function getClauses()
   188     {
   188     {
   189          return $this->_clauses;   
   189          return $this->_clauses;
   190     }
   190     }
   191 }
   191 }