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 <cassert>
17 #include <fstream>
18
19 #include <zorba/store_manager.h>
20 #include <zorba/zorba.h>
21 #include <zorba/xquery.h>
22 #include <zorba/xquery_exception.h>
23 #include <zorba/serializer.h>
24 #include <zorba/item_sequence.h>
25 #include <zorba/singleton_item_sequence.h>
26 #include <zorba/static_collection_manager.h>
27
28 #include "system/properties.h"
29
30 using namespace zorba;
31
32 bool
staticcollectionamanger1(zorba::Zorba * z)33 staticcollectionamanger1(zorba::Zorba* z)
34 {
35 std::ifstream lIn("module1.xq");
36
37 zorba::XQuery_t lQuery = z->createQuery();
38 Zorba_CompilerHints lHints;
39 lQuery->compile(lIn, lHints);
40
41 StaticCollectionManager* lColMgr = lQuery->getStaticCollectionManager();
42
43 ItemFactory* lFac = z->getItemFactory();
44 Item lCollName2 = lFac->createQName("http://www.mod2.com/", "coll");
45 Item lIdxName = lFac->createQName("http://www.mod2.com/", "index");
46 Item lCollName3 = lFac->createQName("http://www.mod3.com/", "coll");
47
48 ItemSequence_t lSeq = lColMgr->declaredCollections();
49 Iterator_t lIter = lSeq->getIterator();
50 lIter->open();
51 Item lTmp;
52 int num_colls = 0;
53 while (lIter->next(lTmp)) {
54 std::cout << "name " << lTmp.getStringValue() << std::endl;
55 ++num_colls;
56 }
57
58 if (num_colls != 2) {
59 return false;
60 }
61
62 int num_idxs = 0;
63 lSeq = lColMgr->declaredIndexes();
64 lIter = lSeq->getIterator();
65 lIter->open();
66 while (lIter->next(lTmp)) {
67 std::cout << "name " << lTmp.getStringValue() << std::endl;
68 ++num_idxs;
69 }
70
71 if (num_idxs != 1) {
72 return false;
73 }
74
75 if (!lColMgr->isDeclaredCollection(lCollName2) ||
76 !lColMgr->isDeclaredCollection(lCollName3)) {
77 return false;
78 }
79
80 if (!lColMgr->isDeclaredIndex(lIdxName)) {
81 return false;
82 }
83
84 lColMgr->createCollection(lCollName2);
85 lColMgr->createCollection(lCollName3);
86 lColMgr->createIndex(lIdxName);
87
88 if (!lColMgr->isAvailableCollection(lCollName2) ||
89 !lColMgr->isAvailableCollection(lCollName3) ||
90 !lColMgr->isAvailableIndex(lIdxName)) {
91 return false;
92 }
93
94 ItemSequence_t lAvailableColls = lColMgr->availableCollections();
95 lIter = lAvailableColls->getIterator();
96 lIter->open();
97 num_colls = 0;
98 while (lIter->next(lTmp)) {
99 std::cout << "name " << lTmp.getStringValue() << std::endl;
100 ++num_colls;
101 }
102
103 if (num_colls != 2) {
104 return false;
105 }
106
107 lColMgr->deleteIndex(lIdxName);
108 lColMgr->deleteCollection(lCollName2);
109 lColMgr->deleteCollection(lCollName3);
110
111 if (lColMgr->isAvailableCollection(lCollName2) ||
112 lColMgr->isAvailableCollection(lCollName3) ||
113 lColMgr->isAvailableIndex(lIdxName)) {
114 return false;
115 }
116
117 return true;
118 }
119
120 bool
staticcollectionamanger2(zorba::Zorba * z)121 staticcollectionamanger2(zorba::Zorba* z)
122 {
123 std::ifstream lIn("module1.xq");
124
125 zorba::XQuery_t lQuery = z->createQuery();
126 Zorba_CompilerHints lHints;
127 lQuery->compile(lIn, lHints);
128
129 StaticCollectionManager* lColMgr = lQuery->getStaticCollectionManager();
130
131 ItemFactory* lFac = z->getItemFactory();
132 Item lCollName2 = lFac->createQName("http://www.mod2.com/", "coll");
133
134 lColMgr->createCollection(lCollName2);
135
136 Collection_t lCollection = lColMgr->getCollection(lCollName2);
137
138 std::vector<Annotation_t> lAnnotations;
139 lCollection->getAnnotations(lAnnotations);
140 size_t num_annotations = 0;
141 for (std::vector<Annotation_t>::const_iterator lIter = lAnnotations.begin();
142 lIter != lAnnotations.end(); ++lIter)
143 {
144 std::cout << "Annotation QName " << (*lIter)->getQName().getStringValue() << std::endl;
145 ++num_annotations;
146 }
147
148 if (num_annotations != 3)
149 {
150 return false;
151 }
152
153 if (lCollection->getType()->getKind() != IdentTypes::ANY_NODE_TYPE)
154 {
155 return false;
156 }
157
158 std::stringstream lInStream;
159 lInStream
160 << "<books>" << std::endl
161 << " <book>Book 1</book>" << std::endl
162 << " <book>Book 2</book>" << std::endl
163 << "</books>";
164
165 Item lDoc = z->getXmlDataManager()->parseXML(lInStream);
166
167 for (size_t i = 0; i < 10; ++i) {
168 lCollection->insertNodesLast(new SingletonItemSequence(lDoc));
169 }
170
171 lCollection->deleteNodesLast(2);
172
173 ItemSequence_t lContents = lCollection->contents();
174 Iterator_t lIter = lContents->getIterator();
175 size_t num_nodes = 0;
176 lIter->open();
177 while (lIter->next(lDoc)) {
178 ++num_nodes;
179 }
180 lIter->close();
181
182 if (num_nodes != 8) {
183 return false;
184 }
185
186 lDoc = NULL;
187 lColMgr->deleteCollection(lCollName2);
188
189 return true;
190 }
191
192 bool
staticcollectionamanger3(zorba::Zorba * z)193 staticcollectionamanger3(zorba::Zorba* z)
194 {
195 std::ifstream lIn("module1.xq");
196
197 zorba::XQuery_t lQuery = z->createQuery();
198 Zorba_CompilerHints lHints;
199 lQuery->compile(lIn, lHints);
200
201 const StaticContext* lSctx = lQuery->getStaticContext();
202
203 StaticCollectionManager* lColMgr = lSctx->getStaticCollectionManager();
204
205 ItemFactory* lFac = z->getItemFactory();
206 Item lCollName2 = lFac->createQName("http://www.mod2.com/", "coll");
207
208 lColMgr->createCollection(lCollName2);
209 {
210 Collection_t lColl = lColMgr->getCollection(lCollName2);
211
212 std::vector<Annotation_t> lAnnotations;
213 lColl->getAnnotations(lAnnotations);
214 size_t num_annotations = 0;
215 for (std::vector<Annotation_t>::const_iterator lIter = lAnnotations.begin();
216 lIter != lAnnotations.end(); ++lIter)
217 {
218 std::cout << "Annotation QName " << (*lIter)->getQName().getStringValue() << std::endl;
219 ++num_annotations;
220 }
221
222 if (num_annotations != 3)
223 {
224 return false;
225 }
226 }
227 lColMgr->deleteCollection(lCollName2);
228
229 return true;
230 }
231
232
233 bool
staticcollectionamanger4(zorba::Zorba * z)234 staticcollectionamanger4(zorba::Zorba* z)
235 {
236 std::ifstream lIn("main_invoke.xq");
237 assert(lIn.good());
238
239 zorba::XQuery_t lQuery = z->createQuery();
240 Zorba_CompilerHints lHints;
241 lQuery->compile(lIn, lHints);
242
243 size_t i = 0;
244
245 StaticCollectionManager* lMgr = lQuery->getStaticCollectionManager();
246
247 zorba::ItemSequence_t lSeq = lMgr->declaredCollections();
248 zorba::Iterator_t lIter = lSeq->getIterator();
249 lIter->open();
250 zorba::Item lName;
251 while (lIter->next(lName))
252 {
253 if (!lMgr->isAvailableCollection(lName))
254 {
255 lMgr->createCollection(lName);
256 std::cout << "created collection " << lName.getStringValue() << std::endl;
257 ++i;
258 }
259 }
260
261 return i == 1;
262 }
263
264 bool
check_types(StaticCollectionManager * aColMgr,ItemFactory * aFac,const char * const aCollName,int aDepth,IdentTypes::kind_t someKinds[],IdentTypes::quantifier_t someQuantifiers[])265 check_types(StaticCollectionManager* aColMgr,
266 ItemFactory* aFac,
267 const char* const aCollName,
268 int aDepth,
269 IdentTypes::kind_t someKinds[],
270 IdentTypes::quantifier_t someQuantifiers[])
271 {
272 Item lCollName = aFac->createQName("http://www.mod6.com/", aCollName);
273 if (! aColMgr->isDeclaredCollection(lCollName)) {
274 std::cout << "no collection "
275 << lCollName.getStringValue().c_str()
276 << std::endl;
277 return false;
278 }
279 aColMgr->createCollection(lCollName);
280 Collection_t lCollection = aColMgr->getCollection(lCollName);
281 zorba::TypeIdentifier_t lType = lCollection->getType();
282 std::cout << lType << std::endl;
283
284 for (int i = 0; i < aDepth; ++i) {
285 if (lType->getKind() != someKinds[i]) {
286 std::cout << lType << std::endl;
287 return false;
288 }
289 if (lType->getQuantifier() != someQuantifiers[i]) {
290 std::cout << lType << std::endl;
291 return false;
292 }
293 lType = lType->getContentType();
294 }
295 assert(lType.isNull());
296
297 aColMgr->deleteCollection(lCollName);
298 return true;
299 }
300
301 bool
staticcollectionamanger5(zorba::Zorba * z)302 staticcollectionamanger5(zorba::Zorba* z)
303 {
304 std::ifstream lIn("module5.xq");
305 zorba::XQuery_t lQuery = z->createQuery();
306
307 try {
308 Zorba_CompilerHints lHints;
309 lQuery->compile(lIn, lHints);
310 } catch (zorba::XQueryException &e) {
311 std::cout << e << std::endl;
312 return false;
313 } catch (...) {
314 std::cout << "compilation failed" << std::endl;
315 return false;
316 }
317
318 StaticCollectionManager* lColMgr = lQuery->getStaticCollectionManager();
319 ItemFactory* lFac = z->getItemFactory();
320
321 IdentTypes::kind_t lC01Kinds[] = { IdentTypes::DOCUMENT_TYPE,
322 IdentTypes::ELEMENT_TYPE,
323 IdentTypes::NAMED_TYPE };
324 IdentTypes::quantifier_t lC01Quants[] = { IdentTypes::QUANT_STAR,
325 IdentTypes::QUANT_ONE,
326 // this '*' is probably a bug
327 IdentTypes::QUANT_STAR };
328 if (!check_types(lColMgr, lFac, "coll01", 3, lC01Kinds, lC01Quants)) {
329 return false;
330 }
331
332 IdentTypes::kind_t lC02Kinds[] = { IdentTypes::DOCUMENT_TYPE,
333 IdentTypes::ELEMENT_TYPE,
334 IdentTypes::NAMED_TYPE };
335 IdentTypes::quantifier_t lC02Quants[] = { IdentTypes::QUANT_STAR,
336 IdentTypes::QUANT_ONE,
337 IdentTypes::QUANT_ONE };
338 if (!check_types(lColMgr, lFac, "coll02", 3, lC02Kinds, lC02Quants)) {
339 return false;
340 }
341
342 IdentTypes::kind_t lC03Kinds[] = { IdentTypes::DOCUMENT_TYPE,
343 IdentTypes::SCHEMA_ELEMENT_TYPE };
344 IdentTypes::quantifier_t lC03Quants[] = { IdentTypes::QUANT_STAR,
345 IdentTypes::QUANT_ONE };
346 if (!check_types(lColMgr, lFac, "coll03", 2, lC03Kinds, lC03Quants)) {
347 return false;
348 }
349
350 IdentTypes::kind_t lC04Kinds[] = { IdentTypes::ELEMENT_TYPE,
351 IdentTypes::NAMED_TYPE };
352 IdentTypes::quantifier_t lC04Quants[] = { IdentTypes::QUANT_STAR,
353 // this '*' is probably a bug
354 IdentTypes::QUANT_STAR };
355 if (!check_types(lColMgr, lFac, "coll04", 2, lC04Kinds, lC04Quants)) {
356 return false;
357 }
358
359 IdentTypes::kind_t lC05Kinds[] = { IdentTypes::ELEMENT_TYPE,
360 IdentTypes::NAMED_TYPE };
361 IdentTypes::quantifier_t lC05Quants[] = { IdentTypes::QUANT_STAR,
362 IdentTypes::QUANT_ONE };
363 if (!check_types(lColMgr, lFac, "coll05", 2, lC05Kinds, lC05Quants)) {
364 return false;
365 }
366
367 IdentTypes::kind_t lC06Kinds[] = { IdentTypes::SCHEMA_ELEMENT_TYPE };
368 IdentTypes::quantifier_t lC06Quants[] = { IdentTypes::QUANT_STAR };
369 if (!check_types(lColMgr, lFac, "coll06", 1, lC06Kinds, lC06Quants)) {
370 return false;
371 }
372
373 IdentTypes::kind_t lC07Kinds[] = { IdentTypes::ATTRIBUTE_TYPE,
374 IdentTypes::NAMED_TYPE };
375 IdentTypes::quantifier_t lC07Quants[] = { IdentTypes::QUANT_STAR,
376 // this '*' is probably a bug
377 IdentTypes::QUANT_STAR };
378 if (!check_types(lColMgr, lFac, "coll07", 2, lC07Kinds, lC07Quants)) {
379 return false;
380 }
381
382 IdentTypes::kind_t lC08Kinds[] = { IdentTypes::ATTRIBUTE_TYPE,
383 IdentTypes::NAMED_TYPE };
384 IdentTypes::quantifier_t lC08Quants[] = { IdentTypes::QUANT_STAR,
385 IdentTypes::QUANT_ONE };
386 if (!check_types(lColMgr, lFac, "coll08", 2, lC08Kinds, lC08Quants)) {
387 return false;
388 }
389
390 IdentTypes::kind_t lC09Kinds[] = { IdentTypes::SCHEMA_ATTRIBUTE_TYPE };
391 IdentTypes::quantifier_t lC09Quants[] = { IdentTypes::QUANT_STAR };
392 if (!check_types(lColMgr, lFac, "coll09", 1, lC09Kinds, lC09Quants)) {
393 return false;
394 }
395
396 IdentTypes::kind_t lC10Kinds[] = { IdentTypes::COMMENT_TYPE };
397 IdentTypes::quantifier_t lC10Quants[] = { IdentTypes::QUANT_ONE };
398 if (!check_types(lColMgr, lFac, "coll10", 1, lC10Kinds, lC10Quants)) {
399 return false;
400 }
401
402 IdentTypes::kind_t lC11Kinds[] = { IdentTypes::PI_TYPE };
403 IdentTypes::quantifier_t lC11Quants[] = { IdentTypes::QUANT_QUESTION };
404 if (!check_types(lColMgr, lFac, "coll11", 1, lC11Kinds, lC11Quants)) {
405 return false;
406 }
407
408 IdentTypes::kind_t lC12Kinds[] = { IdentTypes::TEXT_TYPE };
409 IdentTypes::quantifier_t lC12Quants[] = { IdentTypes::QUANT_PLUS };
410 if (!check_types(lColMgr, lFac, "coll12", 1, lC12Kinds, lC12Quants)) {
411 return false;
412 }
413
414 IdentTypes::kind_t lC13Kinds[] = { IdentTypes::ANY_NODE_TYPE };
415 IdentTypes::quantifier_t lC13Quants[] = { IdentTypes::QUANT_STAR };
416 if (!check_types(lColMgr, lFac, "coll13", 1, lC13Kinds, lC13Quants)) {
417 return false;
418 }
419
420 return true;
421 }
422
423 /**
424 * test that declaredIndexes doesn't return temporary indexes and crashes
425 * if one tries to create one
426 */
427 bool
staticcollectionmanager6(zorba::Zorba * z)428 staticcollectionmanager6(zorba::Zorba* z)
429 {
430 std::ifstream lIn("module1.xq");
431
432 zorba::XQuery_t lQuery = z->createQuery();
433 Zorba_CompilerHints lHints;
434 lQuery->compile(lIn, lHints);
435
436 StaticCollectionManager* lColMgr = lQuery->getStaticCollectionManager();
437
438 ItemFactory* lFac = z->getItemFactory();
439 Item lCollName2 = lFac->createQName("http://www.mod2.com/", "coll");
440 Item lIdxName = lFac->createQName("http://www.mod2.com/", "index");
441 Item lCollName3 = lFac->createQName("http://www.mod3.com/", "coll");
442
443 ItemSequence_t lSeq = lColMgr->declaredCollections();
444 Iterator_t lIter = lSeq->getIterator();
445 lIter->open();
446 Item lTmp;
447 while (lIter->next(lTmp)) {
448 std::cout << "name " << lTmp.getStringValue() << std::endl;
449 lColMgr->createCollection(lTmp);
450 }
451
452 lSeq = lColMgr->declaredIndexes();
453 lIter = lSeq->getIterator();
454 lIter->open();
455 while (lIter->next(lTmp)) {
456 std::cout << "name " << lTmp.getStringValue() << std::endl;
457 lColMgr->createIndex(lTmp);
458 }
459
460 return true;
461 }
462
463 int
staticcollectionmanager(int argc,char * argv[])464 staticcollectionmanager(int argc, char* argv[])
465 {
466 void* store = zorba::StoreManager::getStore();
467 zorba::Zorba* z = zorba::Zorba::getInstance(store);
468
469 zorba::Properties::load(0, NULL);
470
471 std::cout << "executing example 1" << std::endl;
472 if (!staticcollectionamanger1(z))
473 return 1;
474 std::cout << std::endl;
475
476 std::cout << "executing example 2" << std::endl;
477 if (!staticcollectionamanger2(z))
478 return 2;
479 std::cout << std::endl;
480
481 std::cout << "executing example 3" << std::endl;
482 if (!staticcollectionamanger3(z))
483 return 3;
484 std::cout << std::endl;
485
486 std::cout << "executing example 4" << std::endl;
487 if (!staticcollectionamanger4(z))
488 return 4;
489 std::cout << std::endl;
490
491 std::cout << "executing example 5" << std::endl;
492 if (!staticcollectionamanger5(z))
493 return 5;
494 std::cout << std::endl;
495
496 std::cout << "executing example 6" << std::endl;
497 if (!staticcollectionmanager6(z))
498 return 6;
499 std::cout << std::endl;
500
501 return 0;
502 }
503
504 /* vim:set et sw=2 ts=2: */
505