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 #include "stdafx.h"
17 
18 #include "system/globalenv.h"
19 
20 #include "store/api/store.h"        // for checking if index exists
21 #include "store/api/item_factory.h" // for creating pul
22 #include "store/api/iterator_factory.h" // for creating the probe iterator
23 #include "store/api/pul.h"
24 #include "store/api/index.h"
25 
26 #include "runtime/indexing/ic_ddl.h"
27 #include "runtime/api/plan_wrapper.h"
28 #include "runtime/api/plan_iterator_wrapper.h"
29 
30 #include "compiler/xqddf/value_ic.h"
31 
32 #include "context/static_context.h"
33 #include "context/dynamic_context.h"
34 
35 
36 namespace zorba
37 {
38 
39 /*******************************************************************************
40  Implementation for:
41    activate($icName as xs:QName) as ()
42 ******************************************************************************/
43 bool
nextImpl(store::Item_t & result,PlanState & planState) const44 ActivateICIterator::nextImpl(store::Item_t& result, PlanState& planState) const
45 {
46   store::Item_t qname;
47   ValueIC_t vic;
48 
49   PlanIteratorState* state;
50   DEFAULT_STACK_INIT(PlanIteratorState, state, planState);
51 
52   if (!consumeNext(qname, theChild, planState))
53     ZORBA_ASSERT(false); // error should already be raised by function call
54 
55   if ((vic = theSctx->lookup_ic(qname)) == NULL)
56   {
57     throw XQUERY_EXCEPTION(
58       zerr::ZDDY0031_IC_NOT_DECLARED,
59       ERROR_PARAMS( qname->getStringValue() ),
60       ERROR_LOC( loc )
61     );
62   }
63 
64   // already activated => noop
65   if (GENV_STORE.getIC(qname) == NULL)
66   {
67     result = GENV_ITEMFACTORY->createPendingUpdateList();
68 
69     switch ( vic->getICKind() )
70     {
71     case store::IC::ic_collection:
72       static_cast<store::PUL*>(result.getp())->addActivateIC(
73           &loc,
74           vic->getICName(),
75           vic->getCollectionName());
76       break;
77 
78     case store::IC::ic_foreignkey:
79       static_cast<store::PUL*>(result.getp())->addActivateForeignKeyIC(
80           &loc,
81           vic->getICName(),
82           vic->getFromCollectionName(),
83           vic->getToCollectionName());
84       break;
85 
86     default:
87       ZORBA_ASSERT(false);
88       break;
89     }
90   }
91 
92   STACK_PUSH(result!=NULL, state);
93 
94   STACK_END(state);
95 }
96 
97 
98 /*******************************************************************************
99  Implementation for:
100    deactivate($icName as xs:QName) as ()
101 ******************************************************************************/
102 bool
nextImpl(store::Item_t & result,PlanState & planState) const103 DeactivateICIterator::nextImpl(store::Item_t& result, PlanState& planState) const
104 {
105   store::Item_t qname;
106 
107   PlanIteratorState* state;
108   DEFAULT_STACK_INIT(PlanIteratorState, state, planState);
109 
110   if (!consumeNext(qname, theChild, planState))
111     ZORBA_ASSERT(false);
112 
113   if (theSctx->lookup_ic(qname) == NULL)
114   {
115     throw XQUERY_EXCEPTION(
116       zerr::ZDDY0031_IC_NOT_DECLARED,
117       ERROR_PARAMS( qname->getStringValue() ),
118       ERROR_LOC( loc )
119     );
120   }
121 
122   if (GENV_STORE.getIC(qname) == NULL)
123   {
124     throw XQUERY_EXCEPTION(
125       zerr::ZDDY0032_IC_NOT_ACTIVATED,
126       ERROR_PARAMS( qname->getStringValue() ),
127       ERROR_LOC( loc )
128     );
129   }
130   else
131   {
132     result = GENV_ITEMFACTORY->createPendingUpdateList();
133     static_cast<store::PUL*>(result.getp())->addDeActivateIC(&loc, qname);
134   }
135 
136   STACK_PUSH(result != NULL, state);
137 
138   STACK_END(state);
139 }
140 
141 /*******************************************************************************
142  Implementation for:
143    check-integrity-constraint($icName as xs:QName) as xs:boolean
144 ******************************************************************************/
145 bool
nextImpl(store::Item_t & result,PlanState & planState) const146 CheckICIterator::nextImpl(store::Item_t& result, PlanState& planState) const
147 {
148   store::Item_t qname;
149 
150   PlanIteratorState* state;
151   DEFAULT_STACK_INIT(PlanIteratorState, state, planState);
152 
153   if (!consumeNext(qname, theChild, planState))
154     ZORBA_ASSERT(false);
155   else
156   {
157     ValueIC* vic = theSctx->lookup_ic(qname);
158 
159     if ( vic == NULL)
160     {
161       throw XQUERY_EXCEPTION(
162         zerr::ZDDY0031_IC_NOT_DECLARED,
163         ERROR_PARAMS( qname->getStringValue() ),
164         ERROR_LOC( loc )
165       );
166     }
167 
168     // run iterator
169     store::Iterator_t iter = vic->getIterator();
170 
171     iter->open();
172     iter->next(result);
173     iter->close();
174 
175     // return result
176     //xs_boolean icFuncResult;
177     //STACK_PUSH(GENV_ITEMFACTORY->createBoolean(result, icFuncResult),
178     //             state);
179   }
180 
181   STACK_PUSH(result.isNull(), state);
182 
183   STACK_PUSH(true, state);
184 
185   STACK_END(state);
186 }
187 
188 } // namespace zorba
189 /* vim:set et sw=2 ts=2: */
190