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