ccontext.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <zorba/zorbac.h>
#include <zorba/store_manager_c.h>
#include "helpers.h"
int
check_sequence(XQC_Sequence* seq, const char* value, int count)
{
XQC_Error lError;
int lCount = 0;
while ( (lError = seq->next(seq)) == XQC_NO_ERROR) {
const char* lValue;
seq->string_value(seq, &lValue);
printf("%s\n", lValue);
if ( (strcmp(value, lValue)) != 0 ) {
printf("Incorrect value: %s (expecting %s)\n", lValue, value);
return 1;
}
lCount++;
}
if (lError != XQC_END_OF_SEQUENCE) {
printf("Error %d reached before end of sequence\n", lError);
return 1;
}
if (lCount != count) {
printf("Saw %d xs:string values, not %d\n", lCount, count);
return 1;
}
return 0;
}
int
ccontext_example_1(XQC_Implementation* impl)
{
XQC_Error lError;
XQC_Expression* lExpr;
XQC_DynamicContext* lContext;
XQC_Sequence* lSeq;
XQC_Sequence* lContextItem;
XQC_Sequence* lResult;
static const char* lZorba[] = { "Zorba" };
lError = impl->create_string_sequence(impl, lZorba, 1, &lSeq);
if (check_error("create_string_sequence", lError)) return 0;
lError = lSeq->next(lSeq);
if (check_error("next", lError)) return 0;
lError = impl->prepare(impl, "(., ., .)", NULL, &lExpr);
if (check_error("prepare", lError)) return 0;
lError = lExpr->create_context(lExpr, &lContext);
if (check_error("create_context", lError)) return 0;
lError = lContext->set_context_item(lContext, lSeq);
if (check_error("set_context_item", lError)) return 0;
lError = lContext->get_context_item(lContext, &lContextItem);
if (check_error("get_context_item", lError)) return 0;
if (check_sequence(lContextItem, lZorba[0], 1)) return 0;
lError = lExpr->execute(lExpr, lContext, &lResult);
if (check_error("execute", lError)) return 0;
if (check_sequence(lResult, lZorba[0], 3)) return 0;
lResult->free(lResult);
lContextItem->free(lContextItem);
lContext->free(lContext);
lExpr->free(lExpr);
lSeq->free(lSeq);
return 1;
}
int
ccontext_example_2(XQC_Implementation* impl)
{
return 1;
}
int
ccontext_example_3(XQC_Implementation* impl)
{
return 1;
}
int
ccontext_example_4(XQC_Implementation* impl)
{
XQC_Expression* lExpr1;
XQC_Expression* lExpr2;
XQC_DynamicContext* lContext;
XQC_Sequence* lSequence1;
XQC_Sequence* lSequence2;
const char* lStringValue;
impl->prepare(impl, "for $i in (1, 2, 3) return $i", NULL, &lExpr1);
lExpr1->execute(lExpr1, NULL, &lSequence1);
impl->prepare(impl, "declare variable $var external; ($var, $var)", NULL, &lExpr2);
lExpr2->create_context(lExpr2, &lContext);
lContext->set_variable(lContext, NULL, "var", lSequence1);
lExpr2->execute(lExpr2, lContext, &lSequence2);
while ( lSequence2->next(lSequence2) != XQC_END_OF_SEQUENCE )
{
lSequence2->string_value(lSequence2, &lStringValue);
printf("%s ", lStringValue);
}
lSequence2->free(lSequence2);
lExpr2->free(lExpr2);
lContext->free(lContext);
lExpr1->free(lExpr1);
return 1;
}
int
ccontext_example_5(XQC_Implementation* impl)
{
XQC_Error lError;
XQC_Expression* lExpr;
XQC_DynamicContext* lContext;
XQC_Sequence* lSeq;
XQC_Sequence* lResult;
static const char* lZorba[] = { "Zorba" };
lError = impl->create_string_sequence(impl, lZorba, 1, &lSeq);
if (check_error("create_string_sequence", lError)) return 0;
lError = impl->prepare(impl,
"declare namespace ns=\"http://zorba.io/\";\n"
"declare variable $ns:foo as xs:string external;\n"
"($ns:foo, $ns:foo, $ns:foo)", NULL, &lExpr);
if (check_error("prepare", lError)) return 0;
lError = lExpr->create_context(lExpr, &lContext);
if (check_error("create_context", lError)) return 0;
lError = lContext->set_variable(lContext, "http://zorba.io/",
"foo", lSeq);
if (check_error("set_variable", lError)) return 0;
lError = lExpr->execute(lExpr, lContext, &lResult);
if (check_error("execute", lError)) return 0;
if (check_sequence(lResult, lZorba[0], 3)) return 0;
lResult->free(lResult);
lContext->free(lContext);
lExpr->free(lExpr);
return 1;
}
int
ccontext_example_6(XQC_Implementation* impl)
{
XQC_Error lError;
XQC_Expression* lExpr;
XQC_DynamicContext* lContext1;
XQC_DynamicContext* lContext2;
XQC_Sequence* lSeq1;
XQC_Sequence* lSeq2;
XQC_Sequence* lVariable1;
XQC_Sequence* lVariable2;
XQC_Sequence* lResult1;
XQC_Sequence* lResult2;
static const char* lZorba[] = { "Zorba" };
static const char* lOther[] = { "Other" };
impl->create_string_sequence(impl, lZorba, 1, &lSeq1);
impl->create_string_sequence(impl, lOther, 1, &lSeq2);
lError = impl->prepare(impl,
"declare namespace ns=\"http://zorba.io/\";\n"
"declare variable $ns:foo as xs:string external;\n"
"($ns:foo, $ns:foo, $ns:foo)", NULL, &lExpr);
if (check_error("prepare", lError)) return 0;
lError = lExpr->create_context(lExpr, &lContext1);
if (check_error("create_context 1", lError)) return 0;
lError = lContext1->set_variable(lContext1, "http://zorba.io/",
"foo", lSeq1);
if (check_error("set_variable 1", lError)) return 0;
lError = lExpr->create_context(lExpr, &lContext2);
if (check_error("create_context 2", lError)) return 0;
lError = lContext2->set_variable(lContext2, "http://zorba.io/",
"foo", lSeq2);
if (check_error("set_variable 2", lError)) return 0;
lError = lContext1->get_variable(lContext1, "http://zorba.io/",
"foo", &lVariable1);
if (check_error("get_variable 1", lError)) return 0;
lError = lContext2->get_variable(lContext2, "http://zorba.io/",
"foo", &lVariable2);
if (check_error("get_variable 2", lError)) return 0;
if (check_sequence(lVariable1, lZorba[0], 1)) return 0;
if (check_sequence(lVariable2, lOther[0], 1)) return 0;
lError = lExpr->execute(lExpr, lContext1, &lResult1);
if (check_error("execute 1", lError)) return 0;
lError = lExpr->execute(lExpr, lContext2, &lResult2);
if (check_error("execute 2", lError)) return 0;
if (check_sequence(lResult1, lZorba[0], 3)) return 0;
if (check_sequence(lResult2, lOther[0], 3)) return 0;
lResult1->free(lResult1);
lResult2->free(lResult2);
lVariable1->free(lVariable1);
lVariable2->free(lVariable2);
lContext1->free(lContext1);
lContext2->free(lContext2);
lExpr->free(lExpr);
return 1;
}
int
ccontext(int argc, char** argv)
{
int res = 0;
XQC_Implementation* impl;
void* store = create_store();
if ( zorba_implementation(&impl, store) != XQC_NO_ERROR)
return 1;
printf("executing C example 4\n");
res = ccontext_example_4(impl);
if (!res) { impl->free(impl); return 1; };
printf("\n");
impl->free(impl);
shutdown_store(store);
return 0;
}