예제 #1
0
파일: cerror.c 프로젝트: buchenberg/zorba
/**
 * 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;
}
예제 #2
0
파일: cerror.c 프로젝트: buchenberg/zorba
int
cerror_example_3(XQC_Implementation* impl) 
{
  XQC_Error       lError = XQC_NO_ERROR;
  XQC_Expression* lExpr;
  XQC_StaticContext* lCtx;
  const char*     lStringValue;
  XQC_Sequence*   lResult;
  XQC_ErrorHandler* lErrorHandler = create_error_handler();

  // Assign error handler to the static context
  impl->create_context(impl, &lCtx);
  lCtx->set_error_handler(lCtx, lErrorHandler);

  // compile the query and get the result as a sequence
  lError = impl->prepare(impl, "for $i in (3, 2, 1, 0) return 3 div $i",
    lCtx, &lExpr);

  lExpr->execute(lExpr, NULL, &lResult);

  // an error is reported during the last for iteration
  // the error callback function is called and the loop terminates
  while ( lResult->next(lResult) == XQC_NO_ERROR ) {
    lResult->string_value(lResult, &lStringValue);
    printf("%s ", lStringValue);
  }

  // release all aquired resources
  free(lErrorHandler);
  lResult->free(lResult);
  lExpr->free(lExpr);
  lCtx->free(lCtx);

  return (gError == XQC_DYNAMIC_ERROR && lError == XQC_NO_ERROR) ? 1 : 0;
}
예제 #3
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;
}