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_TRYCATCH
18 #define ZORBA_RUNTIME_TRYCATCH
19 
20 #include <vector>
21 
22 #include "common/shared_types.h"
23 
24 #include "runtime/base/unarybase.h"
25 
26 namespace zorba
27 {
28 
29 class TryCatchIteratorState : public  PlanIteratorState
30 {
31 public:
32   TryCatchIteratorState();
33   ~TryCatchIteratorState();
34 
35   void init(PlanState&);
36   void reset(PlanState&);
37 
38   // used for evaluating the target expression eagerly
39   store::TempSeq_t               theTargetSequence;
40 
41   store::Iterator_t              theTempIterator;
42 
43   PlanIter_t                     theCatchIterator;
44 
45   std::vector<store::Iterator_t> theErrorIters;
46 };
47 
48 
49 class TryCatchIterator : public UnaryBaseIterator<TryCatchIterator,
50                                                   TryCatchIteratorState>
51 {
52 public:
53   class CatchClause : public ::zorba::serialization::SerializeBaseClass
54   {
55   public:
56     enum var_type
57     {
58       err_code = 0,
59       err_desc,
60       err_value,
61       err_module,
62       err_line_no,
63       err_column_no,
64       zerr_stack_trace
65     };
66 
67   public:
68     typedef std::map<ulong, std::vector<LetVarIter_t> > VarMap_t;
69 
70   public:
71     std::vector<NodeNameTest_t> node_names;
72     PlanIter_t                  catch_expr;
73     VarMap_t                    theVars;
74 
75   public:
76     SERIALIZABLE_CLASS(CatchClause);
77     SERIALIZABLE_CLASS_CONSTRUCTOR(CatchClause);
78 
serialize(::zorba::serialization::Archiver & ar)79     void serialize(::zorba::serialization::Archiver& ar)
80     {
81       ar & node_names;
82       ar & catch_expr;
83       ar & theVars;
84     }
85 
CatchClause()86     CatchClause() {}
87 
88     ~CatchClause();
89   };
90 
91 protected:
92   std::vector<CatchClause> theCatchClauses;
93 
94 public:
95   SERIALIZABLE_CLASS(TryCatchIterator);
96 
97   SERIALIZABLE_CLASS_CONSTRUCTOR2T(
98   TryCatchIterator,
99   UnaryBaseIterator<TryCatchIterator, TryCatchIteratorState>);
100 
serialize(::zorba::serialization::Archiver & ar)101   void serialize(::zorba::serialization::Archiver& ar)
102   {
103     serialize_baseclass(ar,
104     (UnaryBaseIterator<TryCatchIterator, TryCatchIteratorState>*)this);
105 
106     ar & theCatchClauses;
107   }
108 
109 
110 public:
111   TryCatchIterator(
112         static_context* sctx,
113         const QueryLoc& loc,
114         PlanIter_t& aBlock,
115         std::vector<CatchClause>& aCatchClauses);
116 
117   virtual ~TryCatchIterator();
118 
119   void openImpl(PlanState& planState, uint32_t& offset);
120   bool nextImpl(store::Item_t& result, PlanState& aPlanState) const;
121   void resetImpl(PlanState& planState) const;
122   void closeImpl(PlanState& planState);
123 
124   void accept(PlanIterVisitor& v) const;
125 
126   uint32_t getStateSizeOfSubtree() const;
127 
128 protected:
129   bool matchedCatch(
130       ZorbaException const& e,
131       TryCatchIteratorState* state,
132       PlanState&) const;
133 
134   void bindErrorVars(
135       ZorbaException const& e,
136       const CatchClause* state,
137       PlanState&) const;
138 
139   store::Item_t getStackTrace(const XQueryStackTrace&) const;
140 };
141 
142 
143 
144 } /* namespace zorba */
145 
146 #endif /* ZORBA_TRYCATCH_H */
147 /* vim:set et sw=2 ts=2: */
148