1 /*
2  * Copyright 2006-2008 The FLWOR Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #pragma once
17 #ifndef ZORBA_RUNTIME_SEQUENCETYPES
18 #define ZORBA_RUNTIME_SEQUENCETYPES
19 
20 #include "common/shared_types.h"
21 
22 #include "runtime/base/unarybase.h"
23 #include "runtime/base/narybase.h"
24 
25 #include "types/typeconstants.h"
26 
27 namespace zorba
28 {
29 
30 /*******************************************************************************
31   Implement 3.12.1: Instance Of
32 
33   The boolean operator instance of returns true if the value of its first
34   operand matches the SequenceType in its second operand, according to the rules
35   for SequenceType matching; otherwise it returns false.
36 ********************************************************************************/
37 class InstanceOfIterator : public UnaryBaseIterator<InstanceOfIterator,
38                                                     PlanIteratorState>
39 {
40 private:
41   xqtref_t theSequenceType;
42 
43 public:
44   SERIALIZABLE_CLASS(InstanceOfIterator);
45   SERIALIZABLE_CLASS_CONSTRUCTOR2T(
46   InstanceOfIterator,
47   UnaryBaseIterator<InstanceOfIterator, PlanIteratorState>);
48   void serialize(::zorba::serialization::Archiver& ar);
49 
50 public:
51   InstanceOfIterator(
52         static_context* sctx,
53         const QueryLoc& loc,
54         PlanIter_t& aTreatExpr,
55         xqtref_t aSequenceType);
56 
57   ~InstanceOfIterator();
58 
59   void accept(PlanIterVisitor& v) const;
60 
61   bool nextImpl(store::Item_t& result, PlanState& planState) const;
62 };
63 
64 
65 /*******************************************************************************
66   Implement 3.12.3: Cast
67 
68   http://www.w3.org/TR/xquery/#id-cast
69 ********************************************************************************/
70 class CastIterator : public UnaryBaseIterator<CastIterator, PlanIteratorState>
71 {
72   friend class PrinterVisitor;
73 
74 private:
75   xqtref_t                    theCastType;
76   TypeConstants::quantifier_t theQuantifier;
77 
78 public:
79   SERIALIZABLE_CLASS(CastIterator);
80   SERIALIZABLE_CLASS_CONSTRUCTOR2T(
81   CastIterator,
82   UnaryBaseIterator<CastIterator, PlanIteratorState>);
83   void serialize(::zorba::serialization::Archiver& ar);
84 
85 public:
86   CastIterator(
87       static_context* sctx,
88       const QueryLoc& loc,
89       PlanIter_t& aChild,
90       const xqtref_t& aCastType);
91 
92   ~CastIterator();
93 
94   void accept(PlanIterVisitor& v) const;
95 
96   bool nextImpl(store::Item_t& result, PlanState& aPlanState) const;
97 };
98 
99 
100 /*******************************************************************************
101   Implement 3.12.4: Castable
102 
103   http://www.w3.org/TR/xquery/#id-castable
104 ********************************************************************************/
105 class CastableIterator : public UnaryBaseIterator<CastableIterator,
106                                                   PlanIteratorState>
107 {
108   friend class PrinterVisitor;
109 
110 private:
111   xqtref_t                    theCastType;
112   TypeConstants::quantifier_t theQuantifier;
113 
114 public:
115   SERIALIZABLE_CLASS(CastableIterator);
116   SERIALIZABLE_CLASS_CONSTRUCTOR2T(
117   CastableIterator,
118   UnaryBaseIterator<CastableIterator, PlanIteratorState>);
119   void serialize(::zorba::serialization::Archiver& ar);
120 
121 public:
122   CastableIterator(
123         static_context* sctx,
124         const QueryLoc& aLoc,
125         PlanIter_t& aChild,
126         const xqtref_t& aCastType);
127 
128   ~CastableIterator();
129 
130   void accept(PlanIterVisitor& v) const;
131 
132   bool nextImpl(store::Item_t& result, PlanState& aPlanState) const;
133 };
134 
135 
136 /*******************************************************************************
137   Iterator which tries to promote an item to the passed target type. If it
138   is not possible, a type error is thrown. If the type of the item is a
139   subtype of the target type, then no promotion is done (it's a noop).
140 ********************************************************************************/
141 class PromoteIterator : public UnaryBaseIterator<PromoteIterator,
142                                                  PlanIteratorState>
143 {
144   friend class PrinterVisitor;
145 
146 public:
147   typedef enum
148   {
149     FUNC_RETURN,
150     FUNC_PARAM,
151     TYPE_PROMOTION,
152     JSONIQ_ARRAY_SELECTOR,
153     JSONIQ_OBJECT_SELECTOR,
154     JSONIQ_SELECTOR
155   } ErrorKind;
156 
157 private:
158   xqtref_t                    thePromoteType;
159   TypeConstants::quantifier_t theQuantifier;
160   ErrorKind                   theErrorKind;
161   store::Item_t								theQName;
162 
163 public:
164   SERIALIZABLE_CLASS(PromoteIterator);
165   SERIALIZABLE_CLASS_CONSTRUCTOR2T(
166   PromoteIterator,
167   UnaryBaseIterator<PromoteIterator, PlanIteratorState>);
168   void serialize(::zorba::serialization::Archiver& ar);
169 
170 public:
171   PromoteIterator(
172       static_context* sctx,
173       const QueryLoc& loc,
174       PlanIter_t& child,
175       const xqtref_t& promoteType,
176       ErrorKind err,
177       store::Item_t qname = NULL);
178 
179   ~PromoteIterator();
180 
181   void accept(PlanIterVisitor& v) const;
182 
183   bool nextImpl(store::Item_t& result, PlanState& aPlanState) const;
184 
185 protected:
186   void raiseError(const zstring& valueType) const;
187 };
188 
189 
190 /*******************************************************************************
191   Iterator which checks if a sequence is an instance of the target type. If not,
192   an error is thrown.
193 
194   theTreatType  :
195   theQuantifier :
196   theErrorCode  :
197   theFnQName    : Stores the QName of the function, if the treat iterator is
198                   used to check the result type a function.
199 ********************************************************************************/
200 class TreatIterator : public UnaryBaseIterator<TreatIterator,
201                                                PlanIteratorState>
202 {
203   friend class PrinterVisitor;
204 
205 public:
206   typedef enum
207   {
208     FUNC_RETURN,
209     FUNC_PARAM,
210     TYPE_MATCH,
211     TREAT_EXPR,
212     INDEX_DOMAIN,
213     INDEX_KEY,
214     PATH_STEP,
215     PATH_DOT,
216     MULTI_VALUED_GROUPING_KEY,
217     JSONIQ_VALUE,
218     JSONIQ_UPDATE_TARGET,
219     JSONIQ_OBJECT_UPDATE_TARGET,
220     JSONIQ_OBJECT_UPDATE_CONTENT,
221     JSONIQ_ARRAY_UPDATE_TARGET,
222     JSONIQ_OBJECT_UPDATE_VALUE
223   } ErrorKind;
224 
225 private:
226   xqtref_t                    theTreatType;
227   TypeConstants::quantifier_t theQuantifier;
228   bool                        theCheckPrime;
229   ErrorKind                   theErrorKind;
230   store::Item_t								theQName;
231 
232 public:
233   SERIALIZABLE_CLASS(TreatIterator);
234   SERIALIZABLE_CLASS_CONSTRUCTOR2T(
235   TreatIterator,
236   UnaryBaseIterator<TreatIterator, PlanIteratorState>);
237   void serialize(::zorba::serialization::Archiver& ar);
238 
239 public:
240   TreatIterator(
241       static_context* sctx,
242       const QueryLoc& loc,
243       PlanIter_t& child,
244       const xqtref_t& treatType,
245       bool check_prime,
246       ErrorKind errorKind,
247       store::Item_t qname);
248 
249   void accept(PlanIterVisitor& v) const;
250 
251   bool nextImpl(store::Item_t& result, PlanState& aPlanState) const;
252 
253 protected:
254   void raiseError(const zstring& valueType) const;
255 };
256 
257 
258 /*******************************************************************************
259 
260 ********************************************************************************/
261 class EitherNodesOrAtomicsIteratorState : public PlanIteratorState
262 {
263 public:
264   bool atomics;
265 };
266 
267 NARY_ITER_STATE(EitherNodesOrAtomicsIterator, EitherNodesOrAtomicsIteratorState);
268 
269 
270 } /* namespace zorba */
271 #endif
272 
273 /*
274  * Local variables:
275  * mode: c++
276  * End:
277  */
278 /* vim:set et sw=2 ts=2: */
279