Extbase:
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Classes / Persistence / QOM / QueryObjectModel.php
1 <?php
2 /***************************************************************
3 * Copyright notice
4 *
5 * (c) 2009 Jochen Rau <jochen.rau@typoplanet.de>
6 * All rights reserved
7 *
8 * This class is a backport of the corresponding class of FLOW3.
9 * All credits go to the v5 team.
10 *
11 * This script is part of the TYPO3 project. The TYPO3 project is
12 * free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * The GNU General Public License can be found at
18 * http://www.gnu.org/copyleft/gpl.html.
19 *
20 * This script is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * This copyright notice MUST APPEAR in all copies of the script!
26 ***************************************************************/
27
28 /**
29 * A query in the JCR query object model.
30 *
31 * The JCR query object model describes the queries that can be evaluated by a JCR
32 * repository independent of any particular query language, such as SQL.
33 *
34 * A query consists of:
35 *
36 * a source. When the query is evaluated, the source evaluates its selectors and
37 * the joins between them to produce a (possibly empty) set of node-tuples. This
38 * is a set of 1-tuples if the query has one selector (and therefore no joins), a
39 * set of 2-tuples if the query has two selectors (and therefore one join), a set
40 * of 3-tuples if the query has three selectors (two joins), and so forth.
41 * an optional constraint. When the query is evaluated, the constraint filters the
42 * set of node-tuples.
43 * a list of zero or more orderings. The orderings specify the order in which the
44 * node-tuples appear in the query results. The relative order of two node-tuples
45 * is determined by evaluating the specified orderings, in list order, until
46 * encountering an ordering for which one node-tuple precedes the other. If no
47 * orderings are specified, or if for none of the specified orderings does one
48 * node-tuple precede the other, then the relative order of the node-tuples is
49 * implementation determined (and may be arbitrary).
50 * a list of zero or more columns to include in the tabular view of the query
51 * results. If no columns are specified, the columns available in the tabular view
52 * are implementation determined, but minimally include, for each selector, a column
53 * for each single-valued non-residual property of the selector's node type.
54 *
55 * The query object model representation of a query is created by factory methods in the QueryObjectModelFactory.
56 *
57 * @package Extbase
58 * @subpackage Persistence
59 * @version $Id: QueryObjectModel.php 1877 2009-02-05 11:29:07Z k-fish $
60 * @scope prototype
61 */
62 class Tx_Extbase_Persistence_QOM_QueryObjectModel extends Tx_Extbase_Persistence_Query implements Tx_Extbase_Persistence_QOM_QueryObjectModelInterface {
63
64 /**
65 * @var Tx_Extbase_Persistence_DataMapper
66 */
67 protected $dataMapper;
68
69 /**
70 * @var Tx_Extbase_Persistence_QOM_SourceInterface
71 */
72 protected $source;
73
74 /**
75 * @var Tx_Extbase_Persistence_QOM_ConstraintInterface
76 */
77 protected $constraint;
78
79 /**
80 * @var array
81 */
82 protected $orderings;
83
84 /**
85 * @var array
86 */
87 protected $columns;
88
89 /**
90 * @var Tx_Extbase_Persistence_Storage_BackendInterface
91 */
92 protected $storageBackend;
93
94 /**
95 * integer
96 */
97 protected $limit;
98
99 /**
100 * integer
101 */
102 protected $offset;
103
104 /**
105 * @var array
106 */
107 protected $boundVariables = array();
108
109 /**
110 * Constructs this QueryObjectModel instance
111 *
112 * @param Tx_Extbase_Persistence_QOM_SourceInterface $selectorOrSource
113 * @param Tx_Extbase_Persistence_QOM_ConstraintInterface $constraint (null if none)
114 * @param array $orderings
115 * @param array $columns
116 */
117 public function __construct(Tx_Extbase_Persistence_QOM_SourceInterface $selectorOrSource, $constraint, array $orderings, array $columns) {
118 $this->source = $selectorOrSource;
119 $this->constraint = $constraint;
120 $this->orderings = $orderings;
121 $this->columns = $columns;
122
123 if ($this->constraint !== NULL) {
124 $this->constraint->collectBoundVariableNames($this->boundVariables);
125 }
126 }
127
128 /**
129 * Injects the StorageBackend
130 *
131 * @param Tx_Extbase_Persistence_Storage_BackendInterface $storageBackend
132 * @return void
133 */
134 public function injectStorageBackend(Tx_Extbase_Persistence_Storage_BackendInterface $storageBackend) {
135 $this->storageBackend = $storageBackend;
136 }
137
138 /**
139 * Injects the Data Mapper to map nodes to objects
140 *
141 * @param Tx_Extbase_Persistence_Mapper_DataMapper $dataMapper
142 * @return void
143 */
144 public function injectDataMapper(Tx_Extbase_Persistence_Mapper_DataMapper $dataMapper) {
145 $this->dataMapper = $dataMapper;
146 }
147
148 /**
149 * Returns the class name the query handles
150 *
151 * @return string The class name
152 */
153 public function getSelectorName() {
154 $this->dataMapper->convertClassNameToSelectorName($this->className);
155 }
156
157 /**
158 * Gets the node-tuple source for this query.
159 *
160 * @return Tx_Extbase_Persistence_QOM_SourceInterface the node-tuple source; non-null
161 */
162 public function getSource() {
163 return $this->source;
164 }
165
166 /**
167 * Gets the constraint for this query.
168 *
169 * @return Tx_Extbase_Persistence_QOM_ConstraintInterface the constraint, or null if none
170 */
171 public function getConstraint() {
172 return $this->constraint;
173 }
174
175 /**
176 * Gets the orderings for this query.
177 *
178 * @return array an array of zero or more Tx_Extbase_Persistence_QOM_OrderingInterface; non-null
179 */
180 public function getOrderings() {
181 return $this->orderings;
182 }
183
184 /**
185 * Gets the columns for this query.
186 *
187 * @return array an array of zero or more Tx_Extbase_Persistence_QOM_ColumnInterface; non-null
188 */
189 public function getColumns() {
190 return $this->columns;
191 }
192
193 /**
194 * Binds the given value to the variable named $varName.
195 *
196 * @param string $varName name of variable in query
197 * @param Tx_Extbase_Persistence_ValueInterface $value value to bind
198 * @return void
199 * @throws InvalidArgumentException if $varName is not a valid variable in this query.
200 * @throws RepositoryException if an error occurs.
201 */
202 public function bindValue($varName, Tx_Extbase_Persistence_ValueInterface $value) {
203 if (array_key_exists($varName, $this->boundVariables) === FALSE) {
204 throw new InvalidArgumentException('Invalid variable name "' . $varName . '" given to bindValue.', 1217241834);
205 }
206 $this->boundVariables[$varName] = $value->getString();
207 }
208
209 /**
210 * Returns the values of all bound variables.
211 *
212 * @return array()
213 */
214 public function getBoundVariableValues() {
215 return $this->boundVariables;
216 }
217
218 /**
219 * Executes this query and returns a QueryResult object.
220 *
221 * @return Tx_Extbase_Persistence_QueryResultInterface A QueryResult object
222 */
223 public function execute() {
224 return t3lib_div::makeInstance('Tx_Extbase_Persistence_QueryResult', $this->storageBackend->getRows($this));
225 }
226
227 /**
228 * Returns the statement defined for this query.
229 * If the language of this query is string-based (like JCR-SQL2), this method
230 * will return the statement that was used to create this query.
231 *
232 * If the language of this query is JCR-JQOM, this method will return the
233 * JCR-SQL2 equivalent of the JCR-JQOM object tree.
234 *
235 * This is the standard serialization of JCR-JQOM and is also the string stored
236 * in the jcr:statement property if the query is persisted. See storeAsNode($absPath).
237 *
238 * @return string the query statement.
239 */
240 public function getStatement() {
241 throw new Exception('Method not yet implemented, sorry!', 1216897752);
242 }
243
244
245 public function useEnableFields() {
246 return TRUE; // TODO Make enableFields configurable
247 }
248
249 }
250 ?>