Example #1
0
/**
 * Use an error handler and test it by executing a query that raises an error.
 */
int
cerror_example_2(XQC_Implementation* impl)
{
  XQC_Expression*   lExpr;
  XQC_DynamicContext* lCtx;
  XQC_ErrorHandler* lErrorHandler = create_error_handler();
  XQC_Sequence*     lResult;

  // compile the query
  impl->prepare(impl, "1 div 0", NULL, &lExpr);

  // set the error handler for handling errors
  // during query execution
  lExpr->create_context(lExpr, &lCtx);
  lCtx->set_error_handler(lCtx, lErrorHandler);

  // execute it and iterate results
  lExpr->execute(lExpr, lCtx, &lResult);
  while (lResult->next(lResult) == XQC_NO_ERROR);
  
  // release all resources
  lResult->free(lResult);
  lExpr->free(lExpr);
  lCtx->free(lCtx);
  free(lErrorHandler);

  return gError == XQC_DYNAMIC_ERROR ? 1 : 0;
}
Example #2
0
/**
 * Utility testing method for parsed documents.
 */
int
parse_test(XQC_Implementation* impl, XQC_Sequence* doc)
{
  XQC_Error           lError;
  XQC_Expression*     lExpr;
  XQC_DynamicContext* lContext;
  XQC_Sequence*       lResult;

  // Advance to the document node in the sequence (since we will bind
  // to the context *item*)
  lError = doc->next(doc);
  if (check_error("next", lError)) return 0;

  // Compile query over document
  lError = impl->prepare(impl, "./doc/a/elem/text()", NULL, &lExpr);
  if (check_error("prepare", lError)) return 0;

  // get the dynamic context and set the context item
  lError = lExpr->create_context(lExpr, &lContext);
  if (check_error("create_context", lError)) return 0;
  lError = lContext->set_context_item(lContext, doc);
  if (check_error("set_context_item", lError)) return 0;

  // execute the query
  lError = lExpr->execute(lExpr, lContext, &lResult);
  if (check_error("execute", lError)) return 0;
  while ( (lResult->next(lResult)) == XQC_NO_ERROR) {
    const char* lValue;
    lResult->string_value(lResult, &lValue);
    printf("%s\n", lValue);
  }

  // free all resources
  lResult->free(lResult);
  lContext->free(lContext);
  lExpr->free(lExpr);
  // Must free the document sequence also since it was bound to the
  // context item, not a variable
  doc->free(doc);
  return 1;
}