Beispiel #1
0
static int
print_graph_result(rasqal_query* rq,
                   rasqal_query_results *results,
                   raptor_world* raptor_world_ptr,
                   FILE* output,
                   const char* serializer_syntax_name, raptor_uri* base_uri,
                   int quiet)
{
    int triple_count = 0;
    rasqal_prefix* prefix;
    int i;
    raptor_serializer* serializer = NULL;

    if(!quiet)
        fprintf(stderr, "%s: Query has a graph result:\n", program);

    if(!raptor_world_is_serializer_name(raptor_world_ptr, serializer_syntax_name)) {
        fprintf(stderr,
                "%s: invalid query result serializer name `%s' for `" HELP_ARG(r, results) "'\n",
                program, serializer_syntax_name);
        return 1;
    }

    serializer = raptor_new_serializer(raptor_world_ptr,
                                       serializer_syntax_name);
    if(!serializer) {
        fprintf(stderr, "%s: Failed to create raptor serializer type %s\n",
                program, serializer_syntax_name);
        return(1);
    }

    /* Declare any query namespaces in the output serializer */
    for(i = 0; (prefix = rasqal_query_get_prefix(rq, i)); i++)
        raptor_serializer_set_namespace(serializer, prefix->uri, prefix->prefix);
    raptor_serializer_start_to_file_handle(serializer, base_uri, output);

    while(1) {
        raptor_statement *rs = rasqal_query_results_get_triple(results);
        if(!rs)
            break;

        raptor_serializer_serialize_statement(serializer, rs);
        triple_count++;

        if(rasqal_query_results_next_triple(results))
            break;
    }

    raptor_serializer_serialize_end(serializer);
    raptor_free_serializer(serializer);

    if(!quiet)
        fprintf(stderr, "%s: Total %d triples\n", program, triple_count);

    return 0;
}
Beispiel #2
0
static int
librdf_query_rasqal_query_results_update_statement(void* context)
{
  librdf_query_rasqal_stream_context* scontext=(librdf_query_rasqal_stream_context*)context;
  librdf_world* world=scontext->query->world;
  librdf_node* node;
  
  raptor_statement *rstatement=rasqal_query_results_get_triple(scontext->qcontext->results);
  if(!rstatement)
    return 1;
  
  scontext->statement=librdf_new_statement(world);
  if(!scontext->statement)
    return 1;

  /* subject */
  
  if(rstatement->subject->type == RAPTOR_TERM_TYPE_BLANK) {
    node = librdf_new_node_from_blank_identifier(world, rstatement->subject->value.blank.string);
  } else if(rstatement->subject->type == RAPTOR_TERM_TYPE_URI) {
    node = librdf_new_node_from_uri_string(world,
                                           librdf_uri_as_string((librdf_uri*)rstatement->subject->value.uri));
  } else {
    librdf_log(world,
               0, LIBRDF_LOG_ERROR, LIBRDF_FROM_QUERY, NULL,
               "Unknown Raptor subject identifier type %d",
               rstatement->subject->type);
    goto fail;
  }

  if(!node) {
    librdf_log(world,
               0, LIBRDF_LOG_ERROR, LIBRDF_FROM_QUERY, NULL,
               "Could not create subject node");
    goto fail;
  }
  
  librdf_statement_set_subject(scontext->statement, node);

  /* predicate */

  if(rstatement->predicate->type == RAPTOR_TERM_TYPE_URI) {
    node = librdf_new_node_from_uri_string(world,
                                           librdf_uri_as_string((librdf_uri*)rstatement->predicate->value.uri));
  } else {
    librdf_log(world,
               0, LIBRDF_LOG_ERROR, LIBRDF_FROM_QUERY, NULL,
               "Unknown Raptor predicate identifier type %d",
               rstatement->predicate->type);
    goto fail;
  }

  if(!node) {
    librdf_log(world,
               0, LIBRDF_LOG_ERROR, LIBRDF_FROM_QUERY, NULL,
               "Could not create predicate node");
    goto fail;
  }
  
  librdf_statement_set_predicate(scontext->statement, node);
  
  /* object */

  if(rstatement->object->type == RAPTOR_TERM_TYPE_LITERAL) {
    node = librdf_new_node_from_typed_literal(world,
                                              rstatement->object->value.literal.string,
                                              (const char*)rstatement->object->value.literal.language,
                                              (librdf_uri*)rstatement->object->value.literal.datatype);
  } else if(rstatement->object->type == RAPTOR_TERM_TYPE_BLANK) {
    node = librdf_new_node_from_blank_identifier(world, rstatement->object->value.blank.string);
  } else if(rstatement->object->type == RAPTOR_TERM_TYPE_URI) {
    node = librdf_new_node_from_uri_string(world,
                                           librdf_uri_as_string((librdf_uri*)rstatement->object->value.uri));
  } else {
    librdf_log(world,
               0, LIBRDF_LOG_ERROR, LIBRDF_FROM_PARSER, NULL,
               "Unknown Raptor object identifier type %d",
               rstatement->object->type);
    goto fail;
  }

  if(!node) {
    librdf_log(world,
               0, LIBRDF_LOG_ERROR, LIBRDF_FROM_QUERY, NULL,
               "Could not create object node");
    goto fail;
  }

  librdf_statement_set_object(scontext->statement, node);

  return 0; /* success */

  fail:
  librdf_free_statement(scontext->statement);
  scontext->statement=NULL;
  return 1;
}