コード例 #1
0
ファイル: rasqal_result_formats.c プロジェクト: kasei/rasqal
int
rasqal_init_result_formats(rasqal_world* world)
{
  int rc = 0;
  
#ifdef HAVE_RAPTOR2_API
  world->query_results_formats = raptor_new_sequence((raptor_data_free_handler)rasqal_free_query_results_format_factory, NULL);
#else
  world->query_results_formats = raptor_new_sequence((raptor_sequence_free_handler*)rasqal_free_query_results_format_factory, NULL);
#endif
  if(!world->query_results_formats)
    return 1;

  rc += rasqal_init_result_format_sparql_xml(world) != 0;

  rc += rasqal_init_result_format_json(world) != 0;

  rc += rasqal_init_result_format_table(world) != 0;

  rc += rasqal_init_result_format_sv(world) != 0;

  rc += rasqal_init_result_format_html(world) != 0;

  rc += rasqal_init_result_format_turtle(world) != 0;

  return rc;
}
コード例 #2
0
/* create a new serializer */
static int
raptor_dot_serializer_init(raptor_serializer *serializer, const char *name)
{
  raptor_dot_context * context = (raptor_dot_context *)serializer->context;
  const raptor_uri_handler *uri_handler;
  void* uri_context;

  raptor_uri_get_handler(&uri_handler, &uri_context);

  /* Setup namespace handling */
  context->nstack =
    raptor_new_namespaces(uri_handler, uri_context,
			  (raptor_simple_message_handler)raptor_serializer_simple_error,
			  serializer, 1);
  context->namespaces=raptor_new_sequence((raptor_sequence_free_handler *)raptor_free_namespace, NULL);

  /* We keep a list of nodes to avoid duplication (which isn't
   * critical in graphviz, but why bloat the file?)
   */
  context->resources =
    raptor_new_sequence((raptor_sequence_free_handler *)raptor_dot_serializer_free_node, NULL);
  context->literals =
    raptor_new_sequence((raptor_sequence_free_handler *)raptor_dot_serializer_free_node, NULL);
  context->bnodes =
    raptor_new_sequence((raptor_sequence_free_handler *)raptor_dot_serializer_free_node, NULL);

  return 0;
}
コード例 #3
0
ファイル: rasqal_service.c プロジェクト: kasei/rasqal
/**
 * rasqal_new_service:
 * @world: rasqal_world object
 * @service_uri: sparql protocol service URI
 * @query_string: query string (or NULL)
 * @data_graphs: sequence of #rasqal_data_graph graphs for service
 *
 * Constructor - create a new rasqal protocol service object.
 *
 * Create a structure to execute a sparql protocol service at
 * @service_uri running the query @query_string and returning
 * a sparql result set.
 *
 * All arguments are copied by the service object
 *
 * Return value: a new #rasqal_query object or NULL on failure
 */
rasqal_service*
rasqal_new_service(rasqal_world* world, raptor_uri* service_uri,
                   const char* query_string,
                   raptor_sequence* data_graphs)
{
  rasqal_service* svc;
  size_t len = 0;
  
  RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, rasqal_world, NULL);
  RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(service_uri, raptor_uri, NULL);
  
  svc = (rasqal_service*)RASQAL_CALLOC(rasqal_service, 1, sizeof(*svc));
  if(!svc)
    return NULL;
  
  svc->world = world;
  svc->service_uri = raptor_uri_copy(service_uri);

  if(query_string) {
    len = strlen(query_string);
    svc->query_string = RASQAL_MALLOC(cstring, len + 1);
    if(!svc->query_string) {
      rasqal_free_service(svc);
      return NULL;
    }
  
    memcpy(svc->query_string, query_string, len + 1);
  }
  svc->query_string_len = len;

  if(data_graphs) {
    int i;
    rasqal_data_graph* dg;
    
#ifdef HAVE_RAPTOR2_API
    svc->data_graphs = raptor_new_sequence((raptor_data_free_handler)rasqal_free_data_graph,
                                           NULL);

#else
    svc->data_graphs = raptor_new_sequence((raptor_sequence_free_handler*)rasqal_free_data_graph,
                                           NULL);
#endif
    if(!svc->data_graphs) {
      rasqal_free_service(svc);
      return NULL;
    }

    for(i = 0;
        (dg = (rasqal_data_graph*)raptor_sequence_get_at(data_graphs, i));
        i++) {
      raptor_sequence_push(svc->data_graphs,
                           rasqal_new_data_graph_from_data_graph(dg));
    }
  }
  
  return svc;
}
コード例 #4
0
ファイル: rasqal_rowsource.c プロジェクト: sengels/rasqal
/**
 * rasqal_rowsource_read_all_rows:
 * @rowsource: rasqal rowsource
 *
 * Read all rows from a rowsource
 *
 * After calling this, the rowsource will be empty of rows and finished
 * and if a sequence is returned, it is owned by the caller.
 *
 * Return value: new sequence of all rows (may be size 0) or NULL on failure
 **/
raptor_sequence*
rasqal_rowsource_read_all_rows(rasqal_rowsource *rowsource)
{
    raptor_sequence* seq;

    if(rasqal_rowsource_ensure_variables(rowsource))
        return NULL;

    if(rowsource->handler->read_all_rows) {
        seq = rowsource->handler->read_all_rows(rowsource, rowsource->user_data);
        if(!seq) {
            seq = raptor_new_sequence((raptor_data_free_handler)rasqal_free_row,
                                      (raptor_data_print_handler)rasqal_row_print);
        }

        if(seq && rowsource->generate_group) {
            int i;
            rasqal_row* row;

            /* Set group for all rows if there are no groups returned */

            for(i = 0; (row = (rasqal_row*)raptor_sequence_get_at(seq, i)); i++) {
                /* if first row has a group ID, end */
                if(!i && row->group_id >= 0)
                    break;

                row->group_id = 0;
            }
        }

        return seq;
    }

    seq = raptor_new_sequence((raptor_data_free_handler)rasqal_free_row,
                              (raptor_data_print_handler)rasqal_row_print);
    if(!seq)
        return NULL;

    while(1) {
        rasqal_row* row = rasqal_rowsource_read_row(rowsource);
        if(!row)
            break;

        /* Generate a group around all rows if there are no groups returned */
        if(rowsource->generate_group && row->group_id < 0)
            row->group_id = 0;

        raptor_sequence_push(seq, row);
    }

    return seq;
}
コード例 #5
0
ファイル: rasqal_result_formats.c プロジェクト: 0u812/rasqal
int
rasqal_init_result_formats(rasqal_world* world)
{
  int rc = 0;
  
  world->query_results_formats = raptor_new_sequence((raptor_data_free_handler)rasqal_free_query_results_format_factory, NULL);
  if(!world->query_results_formats)
    return 1;

  rc += rasqal_init_result_format_sparql_xml(world) != 0;

  rc += rasqal_init_result_format_json(world) != 0;

  rc += rasqal_init_result_format_table(world) != 0;

  rc += rasqal_init_result_format_sv(world) != 0;

  rc += rasqal_init_result_format_html(world) != 0;

  rc += rasqal_init_result_format_turtle(world) != 0;

  rc += rasqal_init_result_format_rdf(world) != 0;

  return rc;
}
コード例 #6
0
ファイル: rasqal_variable.c プロジェクト: rollxx/rasqal
/**
 * rasqal_new_variables_table:
 * @world: rasqal world
 *
 * Constructor - create a new variables table
 *
 * Return value: new variables table or NULL On failure
 */
rasqal_variables_table*
rasqal_new_variables_table(rasqal_world* world)
{
  rasqal_variables_table* vt;
  
  vt = (rasqal_variables_table*)RASQAL_CALLOC(rasqal_variables_table, 1,
                                              sizeof(rasqal_variables_table));
  if(!vt)
    return NULL;

  vt->usage = 1;
  vt->world = world;

#ifdef HAVE_RAPTOR2_API
  vt->variables_sequence = raptor_new_sequence((raptor_data_free_handler)rasqal_free_variable,
                                               (raptor_data_print_handler)rasqal_variable_print);
#else
  vt->variables_sequence = raptor_new_sequence((raptor_sequence_free_handler*)rasqal_free_variable,
                                               (raptor_sequence_print_handler*)rasqal_variable_print);
#endif
  if(!vt->variables_sequence)
    goto tidy;

#ifdef HAVE_RAPTOR2_API
  vt->anon_variables_sequence = raptor_new_sequence((raptor_data_free_handler)rasqal_free_variable,
                                                    (raptor_data_print_handler)rasqal_variable_print);
#else
  vt->anon_variables_sequence = raptor_new_sequence((raptor_sequence_free_handler*)rasqal_free_variable,
                                                    (raptor_sequence_print_handler*)rasqal_variable_print);
#endif
  if(!vt->anon_variables_sequence)
    goto tidy;

  vt->variable_names = NULL;

  return vt;

  tidy:
  rasqal_free_variables_table(vt);
  vt = NULL;
  
  return vt;
}
コード例 #7
0
ファイル: rasqal_rowsource.c プロジェクト: 0u812/rasqal
/**
 * rasqal_new_rowsource_from_handler:
 * @query: query object
 * @user_data: pointer to context information to pass in to calls
 * @handler: pointer to handler methods
 * @vars_table: variables table to use for rows (or NULL)
 * @flags: 0 (none defined so far)
 *
 * Create a new rowsource over a user-defined handler.
 *
 * Return value: new #rasqal_rowsource object or NULL on failure
 **/
rasqal_rowsource*
rasqal_new_rowsource_from_handler(rasqal_world* world,
                                  rasqal_query* query,
                                  void *user_data,
                                  const rasqal_rowsource_handler *handler,
                                  rasqal_variables_table* vars_table,
                                  int flags)
{
  rasqal_rowsource* rowsource;

  if(!world || !handler)
    return NULL;

  if(handler->version < 1 || handler->version > 1)
    return NULL;

  rowsource = RASQAL_CALLOC(rasqal_rowsource*, 1, sizeof(*rowsource));
  if(!rowsource) {
    if(handler->finish)
      handler->finish(NULL, user_data);
    return NULL;
  }

  rowsource->usage = 1;
  rowsource->world = world;
  rowsource->query = query;
  rowsource->user_data = (void*)user_data;
  rowsource->handler = handler;
  rowsource->flags = flags;

  rowsource->size = 0;

  rowsource->generate_group = 0;
  
  if(vars_table)
    rowsource->vars_table = rasqal_new_variables_table_from_variables_table(vars_table);
  else
    rowsource->vars_table = NULL;

  rowsource->variables_sequence = raptor_new_sequence((raptor_data_free_handler)rasqal_free_variable,
                                                      (raptor_data_print_handler)rasqal_variable_print);
  if(!rowsource->variables_sequence) {
    rasqal_free_rowsource(rowsource);
    return NULL;
  }
  
  if(rowsource->handler->init && 
     rowsource->handler->init(rowsource, rowsource->user_data)) {
    RASQAL_DEBUG2("rowsource %s init failed\n", rowsource->handler->name);
    rasqal_free_rowsource(rowsource);
    return NULL;
  }

  return rowsource;
}
コード例 #8
0
/* create a new serializer */
static int
raptor_dot_serializer_init(raptor_serializer *serializer, const char *name)
{
  raptor_dot_context * context = (raptor_dot_context *)serializer->context;

  /* Setup namespace handling */
  context->nstack = raptor_new_namespaces(serializer->world, 1);
  context->namespaces = raptor_new_sequence((raptor_data_free_handler)raptor_free_namespace, NULL);

  /* We keep a list of nodes to avoid duplication (which isn't
   * critical in graphviz, but why bloat the file?)
   */
  context->resources =
    raptor_new_sequence((raptor_data_free_handler)raptor_free_term, NULL);
  context->literals =
    raptor_new_sequence((raptor_data_free_handler)raptor_free_term, NULL);
  context->bnodes =
    raptor_new_sequence((raptor_data_free_handler)raptor_free_term, NULL);

  return 0;
}
コード例 #9
0
ファイル: raptor_serialize.c プロジェクト: Distrotech/raptor
int
raptor_serializers_init(raptor_world* world)
{
  int rc = 0;

  world->serializers = raptor_new_sequence((raptor_data_free_handler)raptor_free_serializer_factory, NULL);
  if(!world->serializers)
    return 1;

#ifdef RAPTOR_SERIALIZER_NTRIPLES
  rc += raptor_init_serializer_ntriples(world) != 0;
#endif

#ifdef RAPTOR_SERIALIZER_TURTLE
  rc += raptor_init_serializer_turtle(world) != 0;
#endif

#ifdef RAPTOR_SERIALIZER_RDFXML_ABBREV
  rc += raptor_init_serializer_rdfxmla(world) != 0;
#endif

#ifdef RAPTOR_SERIALIZER_RDFXML
  rc += raptor_init_serializer_rdfxml(world) != 0;
#endif

#ifdef RAPTOR_SERIALIZER_RSS_1_0
  rc += raptor_init_serializer_rss10(world) != 0;
#endif

#ifdef RAPTOR_SERIALIZER_ATOM
  rc += raptor_init_serializer_atom(world) != 0;
#endif

#ifdef RAPTOR_SERIALIZER_DOT
  rc += raptor_init_serializer_dot(world) != 0;
#endif

#ifdef RAPTOR_SERIALIZER_JSON
  rc += raptor_init_serializer_json(world) != 0;
#endif

#ifdef RAPTOR_SERIALIZER_HTML
  rc += raptor_init_serializer_html(world) != 0;
#endif

#ifdef RAPTOR_SERIALIZER_NQUADS
  rc += raptor_init_serializer_nquads(world) != 0;
#endif

  return rc;
}
コード例 #10
0
static int
rasqal_sort_rowsource_process(rasqal_rowsource* rowsource,
                              rasqal_sort_rowsource_context* con)
{
  int offset = 0;

  /* already processed */
  if(con->seq)
    return 0;

  con->seq = raptor_new_sequence((raptor_data_free_handler)rasqal_free_row,
                                 (raptor_data_print_handler)rasqal_row_print);
  if(!con->seq)
    return 1;
  
  while(1) {
    rasqal_row* row;

    row = rasqal_rowsource_read_row(con->rowsource);
    if(!row)
      break;

    if(rasqal_row_set_order_size(row, con->order_size)) {
      rasqal_free_row(row);
      return 1;
    }

    rasqal_engine_rowsort_calculate_order_values(rowsource->query, con->order_seq, row);

    row->offset = offset;

    /* after this, row is owned by map */
    if(!rasqal_engine_rowsort_map_add_row(con->map, row))
      offset++;
  }
  
#ifdef RASQAL_DEBUG
  if(con->map) {
    fputs("resulting ", DEBUG_FH);
    rasqal_map_print(con->map, DEBUG_FH);
    fputs("\n", DEBUG_FH);
  }
#endif
  
  /* do sort/distinct: walk map in order, adding rows to sequence */
  rasqal_engine_rowsort_map_to_sequence(con->map, con->seq);
  rasqal_free_map(con->map); con->map = NULL;

  return 0;
}
コード例 #11
0
static raptor_avltree* raptor_new_avltree(raptor_data_compare_handler compare_handler, raptor_data_free_handler free_handler, unsigned int flags)
{
  raptor_avltree* tree;

  tree = (raptor_avltree*)RASQAL_CALLOC(raptor_avltree, sizeof(*tree), 1);
  if(!tree)
    return NULL;

  tree->compare_handler = compare_handler;
  tree->free_handler = free_handler;

  tree->seq = raptor_new_sequence(free_handler, NULL);
  
  return tree;
}
コード例 #12
0
ファイル: raptor_rss_common.c プロジェクト: Anchakor/raptor
raptor_rss_item*
raptor_new_rss_item(raptor_world* world)
{
  raptor_rss_item* item;

  item = RAPTOR_CALLOC(raptor_rss_item*, 1, sizeof(*item));
  if(!item)
    return NULL;
  
  item->world = world;
  item->triples = raptor_new_sequence((raptor_data_free_handler)raptor_free_statement, (raptor_data_print_handler)raptor_statement_print);
  if(!item->triples) {
    RAPTOR_FREE(raptor_rss_item, item);
    return NULL;
  }
  return item;
}
コード例 #13
0
ファイル: manifest.c プロジェクト: pombredanne/rasqal
static manifest_test_result*
manifest_new_test_result(manifest_test_state state)
{
  manifest_test_result* result;
  int i;

  result = (manifest_test_result*)calloc(sizeof(*result), 1);
  if(!result)
    return NULL;

  result->state = state;
  /* total_result->details = NULL; */
  for(i = 0; i < STATE_LAST; i++)
    /* Holding pointers; the tests are owned by the testsuites */
    result->states[i] = raptor_new_sequence(NULL, NULL);
  return result;
}
コード例 #14
0
/*
 * rasqal_projection_add_variable:
 * @projection: #rasqal_projection object
 * @var: #rasqal_variable variable
 *
 * INTERNAL - add a binding variable to the projection.
 *
 * Return value: non-0 on failure
 **/
int
rasqal_projection_add_variable(rasqal_projection* projection,
                               rasqal_variable* var)
{
  RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(projection, rasqal_projection, 1);
  RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(var, rasqal_variable, 1);

  if(!projection->variables) {
    projection->variables = raptor_new_sequence((raptor_data_free_handler)rasqal_free_variable,
                                                (raptor_data_print_handler)rasqal_variable_print);
    if(!projection->variables)
      return 1;
  }

  var = rasqal_new_variable_from_variable(var);

  return raptor_sequence_push(projection->variables, (void*)var);
}
コード例 #15
0
ファイル: rasqal_rowsource_empty.c プロジェクト: 0u812/rasqal
static raptor_sequence*
rasqal_empty_rowsource_read_all_rows(rasqal_rowsource* rowsource,
                                     void *user_data)
{
  /* rasqal_empty_rowsource_context* con;
  con = (rasqal_empty_rowsource_context*)user_data; */
  raptor_sequence *seq;

  seq = raptor_new_sequence((raptor_data_free_handler)rasqal_free_row,
                            (raptor_data_print_handler)rasqal_row_print);
  if(seq) {
    rasqal_row* row = rasqal_new_row(rowsource);

    raptor_sequence_push(seq, row);
  }
  
  return seq;
}
コード例 #16
0
ファイル: raptor_abbrev.c プロジェクト: Kirushanr/audacity
/*
 * raptor_abbrev_subject implementation
 *
 * The subject of triples, with all predicates and values
 * linked from them.
 *
 **/
raptor_abbrev_subject*
raptor_new_abbrev_subject(raptor_abbrev_node* node)
{
  raptor_abbrev_subject* subject;
  
  if(!(node->type == RAPTOR_IDENTIFIER_TYPE_RESOURCE ||
        node->type == RAPTOR_IDENTIFIER_TYPE_ANONYMOUS ||
        node->type == RAPTOR_IDENTIFIER_TYPE_ORDINAL)) {
    RAPTOR_FATAL1("Subject node must be a resource, blank, or ordinal\n");
    return NULL;
  }  
  
  subject = (raptor_abbrev_subject*)RAPTOR_CALLOC(raptor_subject, 1,
                                                  sizeof(raptor_abbrev_subject));

  if(subject) {
    subject->node = node;
    subject->node->ref_count++;
    subject->node->count_as_subject++;
    
    subject->node_type = NULL;

    subject->properties =
      raptor_new_avltree((raptor_data_compare_function)raptor_compare_abbrev_po,
                         (raptor_data_free_function)raptor_free_abbrev_po,
                         0);
#ifdef RAPTOR_DEBUG
    raptor_avltree_set_print_handler(subject->properties,
                                     (raptor_data_print_function)raptor_print_abbrev_po);
#endif

    subject->list_items =
      raptor_new_sequence((raptor_sequence_free_handler *)raptor_free_abbrev_node, NULL);

    if(!subject->node || !subject->properties || !subject->list_items) {
      raptor_free_abbrev_subject(subject);
      subject = NULL;
    }
  
  }

  return subject;
}
コード例 #17
0
ファイル: rasqal_query_results.c プロジェクト: egh/rasqal
/**
 * rasqal_query_results_add_row:
 * @query_results: query results object
 * @row: query result row
 *
 * Add a query result row to the sequence of result rows
 *
 * Return value: non-0 on failure
 */
int
rasqal_query_results_add_row(rasqal_query_results* query_results,
                             rasqal_row* row)
{
  RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(query_results, rasqal_query_results, 1);
  RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(row, rasqal_row, 1);

  if(!query_results->results_sequence) {
    query_results->results_sequence = raptor_new_sequence((raptor_data_free_handler)rasqal_free_row, (raptor_data_print_handler)rasqal_row_print);
    if(!query_results->results_sequence)
      return 1;
    
    query_results->result_count = 0;
  }

  row->offset = raptor_sequence_size(query_results->results_sequence);

  return raptor_sequence_push(query_results->results_sequence, row);
}
コード例 #18
0
static int
rasqal_aggregation_rowsource_init(rasqal_rowsource* rowsource, void *user_data)
{
  rasqal_aggregation_rowsource_context* con;

  con = (rasqal_aggregation_rowsource_context*)user_data;

  con->input_values = raptor_new_sequence((raptor_data_free_handler)rasqal_free_literal,
                                          (raptor_data_print_handler)rasqal_literal_print);
    

  con->last_group_id = -1;
  con->offset = 0;
  con->step_count = 0;
  
  if(rasqal_rowsource_request_grouping(con->rowsource))
    return 1;
  
  return 0;
}
コード例 #19
0
ファイル: raptor_xml.c プロジェクト: Distrotech/raptor
/**
 * raptor_xml_element_declare_namespace:
 * @xml_element: XML Element
 * @nspace: raptor_namespace to declare
 * 
 * Declare a namespace on the XML Element.
 *
 * Return value: non-0 if namespace cannot be declared 
 **/
int
raptor_xml_element_declare_namespace(raptor_xml_element* xml_element,
                                     raptor_namespace *nspace)
{
  int i;
  const raptor_namespace *ns;

  if(!xml_element->declared_nspaces)
    xml_element->declared_nspaces = raptor_new_sequence(NULL, NULL);

  if((ns = xml_element->name->nspace)) {
    /* Cannot have same namespace already seen */
    if(ns == nspace ||
       /* ... or two default nspaces */
       (!ns->prefix && !nspace->prefix) ||
       /* ... or two same prefixes */
       (ns->prefix && nspace->prefix &&
        !strcmp((const char*)ns->prefix, (const char*)nspace->prefix))
       )
      return 1;
  }

  
  for(i = 0;
      (ns = (const raptor_namespace*)raptor_sequence_get_at(xml_element->declared_nspaces, i));
      i++) {
    /* Cannot have same namespace already seen */
    if(ns == nspace ||
       /* ... or two default nspaces */
       (!ns->prefix && !nspace->prefix) ||
       /* ... or two same prefixes */
       (ns->prefix && nspace->prefix &&
        !strcmp((const char*)ns->prefix, (const char*)nspace->prefix))
       )
      return 1;
  }

  raptor_sequence_push(xml_element->declared_nspaces, nspace);

  return 0;
}
コード例 #20
0
ファイル: rasqal_format_sparql_xml.c プロジェクト: egh/rasqal
/*
 * rasqal_query_results_getrowsource_sparql_xml:
 * @world: rasqal world object
 * @iostr: #raptor_iostream to read the query results from
 * @base_uri: #raptor_uri base URI of the input format
 *
 * Read the fourth version of the SPARQL XML query results format from an
 * iostream in a format returning a rwosurce - INTERNAL.
 * 
 * Return value: a new rasqal_rowsource or NULL on failure
 **/
static rasqal_rowsource*
rasqal_query_results_get_rowsource_sparql_xml(rasqal_query_results_formatter* formatter,
                                              rasqal_world *world,
                                              rasqal_variables_table* vars_table,
                                              raptor_iostream *iostr,
                                              raptor_uri *base_uri)
{
  rasqal_rowsource_sparql_xml_context* con;
  
  con=(rasqal_rowsource_sparql_xml_context*)RASQAL_CALLOC(rasqal_rowsource_sparql_xml_context, 1, sizeof(rasqal_rowsource_sparql_xml_context));
  if(!con)
    return NULL;

  con->world=world;
  con->base_uri = base_uri ? raptor_uri_copy(base_uri) : NULL;
  con->iostr=iostr;

  con->locator.uri=base_uri;

  con->sax2 = raptor_new_sax2(world->raptor_world_ptr, &con->locator, con);
  if(!con->sax2)
    return NULL;
  
  raptor_sax2_set_start_element_handler(con->sax2,
                                        rasqal_sparql_xml_sax2_start_element_handler);
  raptor_sax2_set_characters_handler(con->sax2,
                                     rasqal_sparql_xml_sax2_characters_handler);

  raptor_sax2_set_end_element_handler(con->sax2,
                                      rasqal_sparql_xml_sax2_end_element_handler);

  con->results_sequence = raptor_new_sequence((raptor_data_free_handler)rasqal_free_row, (raptor_data_print_handler)rasqal_row_print);

  con->vars_table = rasqal_new_variables_table_from_variables_table(vars_table);
  
  return rasqal_new_rowsource_from_handler(world, NULL,
                                           con,
                                           &rasqal_rowsource_sparql_xml_handler,
                                           con->vars_table,
                                           0);
}
コード例 #21
0
static raptor_sequence*
rasqal_rowsequence_rowsource_read_all_rows(rasqal_rowsource* rowsource,
                                           void *user_data)
{
  rasqal_rowsequence_rowsource_context* con;
  raptor_sequence* seq;
  
  con = (rasqal_rowsequence_rowsource_context*)user_data;
  if(con->offset < 0)
    return NULL;

  seq = raptor_new_sequence((raptor_data_free_handler)rasqal_free_row,
                            (raptor_data_print_handler)rasqal_row_print);
  if(seq) {
    int i;
    int size = raptor_sequence_size(con->seq);
    for(i = 0; i < size; i++) {
      rasqal_row *row = (rasqal_row*)raptor_sequence_get_at(con->seq, i);
      raptor_sequence_push(seq, rasqal_new_row_from_row(row));
    }
  }
  
  return seq;
}
コード例 #22
0
ファイル: values.c プロジェクト: Kirushanr/audacity
SLV2Values
slv2_values_new()
{
	return raptor_new_sequence((void (*)(void*))&slv2_value_free, NULL);
}
コード例 #23
0
/* create a new serializer */
static int
raptor_turtle_serialize_init(raptor_serializer* serializer, const char *name)
{
  raptor_turtle_context* context=(raptor_turtle_context*)serializer->context;
  raptor_uri *rdf_type_uri;

  context->nstack=raptor_new_namespaces_v2(serializer->world,
                                           (raptor_simple_message_handler)raptor_serializer_simple_error,
                                           serializer,
                                           1);
  if(!context->nstack)
    return 1;
  context->rdf_nspace=raptor_new_namespace(context->nstack,
                                           (const unsigned char*)"rdf",
                                           (const unsigned char*)raptor_rdf_namespace_uri,
                                           0);

  context->namespaces=raptor_new_sequence(NULL, NULL);

  context->subjects =
    raptor_new_avltree(serializer->world,
		       (raptor_data_compare_function)raptor_abbrev_subject_cmp,
		       (raptor_data_free_function)raptor_free_abbrev_subject, 0);

  context->blanks =
    raptor_new_avltree(serializer->world,
		       (raptor_data_compare_function)raptor_abbrev_subject_cmp,
		       (raptor_data_free_function)raptor_free_abbrev_subject, 0);

  context->nodes =
    raptor_new_avltree(serializer->world,
                       (raptor_data_compare_function)raptor_abbrev_node_cmp,
                       (raptor_data_free_function)raptor_free_abbrev_node, 0);

  rdf_type_uri = raptor_new_uri_for_rdf_concept_v2(serializer->world, "type");
  if(rdf_type_uri) {
    context->rdf_type = raptor_new_abbrev_node(serializer->world,
                                               RAPTOR_IDENTIFIER_TYPE_RESOURCE,
                                               rdf_type_uri, NULL, NULL);
    raptor_free_uri_v2(serializer->world, rdf_type_uri);
  } else
    context->rdf_type = NULL;

  context->rdf_xml_literal_uri=raptor_new_uri_v2(serializer->world, raptor_xml_literal_datatype_uri_string);
  context->rdf_first_uri=raptor_new_uri_v2(serializer->world, (const unsigned char*)"http://www.w3.org/1999/02/22-rdf-syntax-ns#first");
  context->rdf_rest_uri=raptor_new_uri_v2(serializer->world, (const unsigned char*)"http://www.w3.org/1999/02/22-rdf-syntax-ns#rest");
  context->rdf_nil_uri=raptor_new_uri_v2(serializer->world, (const unsigned char*)"http://www.w3.org/1999/02/22-rdf-syntax-ns#nil");

  if(!context->rdf_nspace || !context->namespaces ||
     !context->subjects || !context->blanks || !context->nodes ||
     !context->rdf_xml_literal_uri || !context->rdf_first_uri ||
     !context->rdf_rest_uri || !context->rdf_nil_uri || !context->rdf_type)
  {
    raptor_turtle_serialize_terminate(serializer);
    return 1;
  }

  /* Note: item 0 in the list is rdf:RDF's namespace */
  if(raptor_sequence_push(context->namespaces, context->rdf_nspace)) {
    raptor_turtle_serialize_terminate(serializer);
    return 1;
  }

  return 0;
}
コード例 #24
0
rasqal_rowsource*
rasqal_new_aggregation_rowsource(rasqal_world *world, rasqal_query* query,
                                 rasqal_rowsource* rowsource,
                                 raptor_sequence* exprs_seq,
                                 raptor_sequence* vars_seq)
{
  rasqal_aggregation_rowsource_context* con;
  int flags = 0;
  int size;
  int i;
  
  if(!world || !query || !rowsource || !exprs_seq || !vars_seq)
    goto fail;

  exprs_seq = rasqal_expression_copy_expression_sequence(exprs_seq);
  vars_seq = rasqal_variable_copy_variable_sequence(vars_seq);

  size = raptor_sequence_size(exprs_seq);
  if(size != raptor_sequence_size(vars_seq)) {
    RASQAL_DEBUG3("expressions sequence size %d does not match vars sequence size %d\n", size, raptor_sequence_size(vars_seq));
    goto fail;
  }


  con = (rasqal_aggregation_rowsource_context*)RASQAL_CALLOC(rasqal_aggregation_rowsource_context, 1, sizeof(*con));
  if(!con)
    goto fail;

  con->rowsource = rowsource;

  con->exprs_seq = exprs_seq;
  con->vars_seq = vars_seq;
  
  /* allocate per-expr data */
  con->expr_count = size;
  con->expr_data = (rasqal_agg_expr_data*)RASQAL_CALLOC(rasqal_agg_expr_data, 
                                                        sizeof(rasqal_agg_expr_data),
                                                        size);
  if(!con->expr_data)
    goto fail;

  /* Initialise per-expr data */
  for(i = 0; i < size; i++) {
    rasqal_expression* expr = (rasqal_expression *)raptor_sequence_get_at(exprs_seq, i);
    rasqal_variable* variable = (rasqal_variable*)raptor_sequence_get_at(vars_seq, i);
    rasqal_agg_expr_data* expr_data = &con->expr_data[i];

    expr_data->expr = rasqal_new_expression_from_expression(expr);
    expr_data->variable = variable;

    /* Prepare expression arguments sequence in per-expr data */
    if(expr->args) {
      /* list of #rasqal_expression arguments already in expr
       * #RASQAL_EXPR_FUNCTION and #RASQAL_EXPR_GROUP_CONCAT 
       */
      expr_data->exprs_seq = rasqal_expression_copy_expression_sequence(expr->args);
    } else {
      /* single argument */
      
      expr_data->exprs_seq = raptor_new_sequence((raptor_data_free_handler)rasqal_free_expression,
                                               (raptor_data_print_handler)rasqal_expression_print);
      raptor_sequence_push(expr_data->exprs_seq,
                           rasqal_new_expression_from_expression(expr->arg1));
    }
  }
  
  
  return rasqal_new_rowsource_from_handler(world, query,
                                           con,
                                           &rasqal_aggregation_rowsource_handler,
                                           query->vars_table,
                                           flags);

  fail:

  if(rowsource)
    rasqal_free_rowsource(rowsource);
  if(exprs_seq)
    raptor_free_sequence(exprs_seq);
  if(vars_seq)
    raptor_free_sequence(vars_seq);

  return NULL;
}
コード例 #25
0
int
main(int argc, char *argv[]) 
{
  const char *program = rasqal_basename(argv[0]);
  rasqal_rowsource *rowsource = NULL;
  rasqal_world* world = NULL;
  rasqal_query* query = NULL;
  raptor_sequence* row_seq = NULL;
  raptor_sequence* expr_args_seq = NULL;
  int failures = 0;
  rasqal_variables_table* vt;
  rasqal_rowsource *input_rs = NULL;
  raptor_sequence* vars_seq = NULL;
  raptor_sequence* exprs_seq = NULL;
  int test_id;

  world = rasqal_new_world();
  if(!world || rasqal_world_open(world)) {
    fprintf(stderr, "%s: rasqal_world init failed\n", program);
    return(1);
  }
  
  query = rasqal_new_query(world, "sparql", NULL);

  vt = query->vars_table;
  
  for(test_id = 0; test_id < AGGREGATION_TESTS_COUNT; test_id++) {
    int input_vars_count = test_data[test_id].input_vars;
    int output_rows_count = test_data[test_id].output_rows;
    int output_vars_count = test_data[test_id].output_vars;
    const int* input_group_ids = test_data[test_id].group_ids;
    const int* result_int_data = test_data[test_id].result_data;
    const char* const* result_string_data = test_data[test_id].result_string_data;
    rasqal_op op  = test_data[test_id].op;
    raptor_sequence* seq = NULL;
    int count;
    int size;
    int i;
    char* output_var_name;
    rasqal_variable* output_var;
    rasqal_expression* expr;
    int output_row_size = (input_vars_count + output_vars_count);
    
    if(output_vars_count != 1) {
      fprintf(stderr,
              "%s: test %d expects %d variables which is not supported. Test skipped\n",
              program, test_id, output_vars_count);
      failures++;
      goto tidy;
    }

    row_seq = rasqal_new_row_sequence(world, vt, test_data[test_id].data,
                                      test_data[test_id].input_vars, &vars_seq);
    if(row_seq) {
      for(i = 0; i < test_data[test_id].input_rows; i++) {
        rasqal_row* row = (rasqal_row*)raptor_sequence_get_at(row_seq, i);
        row->group_id = input_group_ids[i];
      }
      
      input_rs = rasqal_new_rowsequence_rowsource(world, query, vt, 
                                                  row_seq, vars_seq);
      /* vars_seq and row_seq are now owned by input_rs */
      vars_seq = row_seq = NULL;
    }
    if(!input_rs) {
      fprintf(stderr, "%s: failed to create rowsequence rowsource\n", program);
      failures++;
      goto tidy;
    }

    expr_args_seq = raptor_new_sequence((raptor_data_free_handler)rasqal_free_expression,
                                        (raptor_data_print_handler)rasqal_expression_print);

    if(test_data[test_id].expr_agg_vars[0] != NULL) {
      int vindex;
      const unsigned char* var_name;
      
      for(vindex = 0;
          (var_name = (const unsigned char*)test_data[test_id].expr_agg_vars[vindex] );
          vindex++) {
        rasqal_variable* v;
        rasqal_literal *l = NULL;
        rasqal_expression* e = NULL;

        v = rasqal_variables_table_get_by_name(vt, var_name);
        if(v)
          l = rasqal_new_variable_literal(world, v);

        if(l)
          e = rasqal_new_literal_expression(world, l);

        if(e)
          raptor_sequence_push(expr_args_seq, e);
        else {
          fprintf(stderr, "%s: failed to create variable %s\n", program,
                  (const char*)var_name);
          failures++;
          goto tidy;
        }
        
      }
    } /* if vars */


    output_var_name = (char*)RASQAL_MALLOC(cstring, 5);
    memcpy(output_var_name, "fake", 5);
    output_var = rasqal_variables_table_add(vt, RASQAL_VARIABLE_TYPE_ANONYMOUS, 
                                            (const unsigned char*)output_var_name, NULL);

    expr = make_test_expr(world, expr_args_seq, op);
    /* expr_args_seq is now owned by expr */
    expr_args_seq = NULL;

    exprs_seq = raptor_new_sequence((raptor_data_free_handler)rasqal_free_expression,
                                    (raptor_data_print_handler)rasqal_expression_print);
    raptor_sequence_push(exprs_seq, expr);
    /* expr is now owned by exprs_seq */
    expr = NULL;
    
    vars_seq = raptor_new_sequence((raptor_data_free_handler)rasqal_free_variable,
                                   (raptor_data_print_handler)rasqal_variable_print);
    output_var = rasqal_new_variable_from_variable(output_var);
    raptor_sequence_push(vars_seq, output_var);

    rowsource = rasqal_new_aggregation_rowsource(world, query, input_rs,
                                                 exprs_seq, vars_seq);
    /* exprs_seq, vars_seq and input_rs are now owned by rowsource */
    exprs_seq = NULL; vars_seq = NULL; input_rs = NULL;

    if(!rowsource) {
      fprintf(stderr, "%s: failed to create aggregation rowsource\n", program);
      failures++;
      goto tidy;
    }


    /* Test the rowsource */
    seq = rasqal_rowsource_read_all_rows(rowsource);
    if(!seq) {
      fprintf(stderr,
              "%s: test %d rasqal_rowsource_read_all_rows() returned a NULL seq for a aggregation rowsource\n",
              program, test_id);
      failures++;
      goto tidy;
    }
    count = raptor_sequence_size(seq);
    if(count != output_rows_count) {
      fprintf(stderr,
              "%s: test %d rasqal_rowsource_read_all_rows() returned %d rows for a aggregation rowsource, expected %d\n",
              program, test_id, count, output_rows_count);
      failures++;
      goto tidy;
    }

    size = rasqal_rowsource_get_size(rowsource);
    if(size != output_row_size) {
      fprintf(stderr,
              "%s: test %d rasqal_rowsource_get_size() returned %d columns (variables) for a aggregation rowsource, expected %d\n",
              program, test_id, size, output_row_size);
      failures++;
      goto tidy;
    }

    if(result_int_data) {
      for(i = 0; i < output_rows_count; i++) {
        rasqal_row* row = (rasqal_row*)raptor_sequence_get_at(seq, i);
        rasqal_literal* value;
        int integer;
        int expected_integer = result_int_data[i];
        int vc;
        
        if(row->size != output_row_size) {
          fprintf(stderr,
                  "%s: test %d row #%d is size %d expected %d\n",
                  program, test_id, i, row->size, output_row_size);
          failures++;
          goto tidy;
        }
        
        /* Expected variable ordering in output row is:
         * {input vars} {output_vars}
         */
        for(vc = 0; vc < output_vars_count; vc++) {
          rasqal_variable* row_var;
          int offset = input_vars_count + vc;
          
          row_var = rasqal_rowsource_get_variable_by_offset(rowsource, offset);
          value = row->values[offset]; 

          if(!value) {
            fprintf(stderr,
                    "%s: test %d row #%d %s value #%d result is NULL\n",
                    program, test_id, i, row_var->name, vc);
            failures++;
            goto tidy;
          }

          if(value->type != RASQAL_LITERAL_INTEGER) {
            fprintf(stderr,
                    "%s: test %d row #%d %s value #%d result is type %s expected integer\n",
                    program, test_id, i, row_var->name, vc,
                    rasqal_literal_type_label(value->type));
            failures++;
            goto tidy;
          }

          integer = rasqal_literal_as_integer(value, NULL);

          if(integer != expected_integer) {
            fprintf(stderr,
                    "%s: test %d row #%d %s value #%d result is %d expected %d\n",
                    program, test_id, i, row_var->name, vc,
                    integer, expected_integer);
            failures++;
            goto tidy;
          }
        }
        
      }
    }
    
    if(result_string_data) {
      for(i = 0; i < output_rows_count; i++) {
        rasqal_row* row = (rasqal_row*)raptor_sequence_get_at(seq, i);
        rasqal_literal* value;
        const unsigned char* str;
        const char* expected_string = result_string_data[i];
        int vc;
        
        if(row->size != output_row_size) {
          fprintf(stderr,
                  "%s: test %d row #%d is size %d expected %d\n",
                  program, test_id, i, row->size, output_row_size);
          failures++;
          goto tidy;
        }
        
        /* Expected variable ordering in output row is:
         * {input vars} {output_vars}
         */
        for(vc = 0; vc < output_vars_count; vc++) {
          rasqal_variable* row_var;
          int offset = input_vars_count + vc;

          row_var = rasqal_rowsource_get_variable_by_offset(rowsource, offset);
          value = row->values[offset]; 

          if(!value) {
            fprintf(stderr,
                    "%s: test %d row #%d %s value #%d result is NULL\n",
                    program, test_id, i, row_var->name, vc);
            failures++;
            goto tidy;
          }

          if(value->type != RASQAL_LITERAL_STRING) {
            fprintf(stderr,
                    "%s: test %d row #%d %s value #%d is type %s expected integer\n",
                    program, test_id, i, row_var->name, vc,
                    rasqal_literal_type_label(value->type));
            failures++;
            goto tidy;
          }

          str = rasqal_literal_as_string(value);

          if(strcmp((const char*)str, expected_string)) {
            fprintf(stderr,
                    "%s: test %d row #%d %s value #%d is %s expected %s\n",
                    program, test_id, i, row_var->name, vc,
                    str, expected_string);
            failures++;
            goto tidy;
          }
        }
        
      }
    }
    

#ifdef RASQAL_DEBUG
    rasqal_rowsource_print_row_sequence(rowsource, seq, stderr);
#endif

    raptor_free_sequence(seq); seq = NULL;

    rasqal_free_rowsource(rowsource); rowsource = NULL;

    if(expr_args_seq)
      raptor_free_sequence(expr_args_seq);
    expr_args_seq = NULL;
  }
  
  
  tidy:
  if(exprs_seq)
    raptor_free_sequence(exprs_seq);
  if(vars_seq)
    raptor_free_sequence(vars_seq);
  if(expr_args_seq)
    raptor_free_sequence(expr_args_seq);
  if(rowsource)
    rasqal_free_rowsource(rowsource);
  if(input_rs)
    rasqal_free_rowsource(input_rs);
  if(query)
    rasqal_free_query(query);
  if(world)
    rasqal_free_world(world);

  return failures;
}
コード例 #26
0
ファイル: rasqal_xsd_datatypes.c プロジェクト: sengels/rasqal
int
main(int argc, char *argv[])
{

#if 0
  raptor_uri *xsd_uri;
  raptor_uri *dateTime_uri;
  rasqal_literal *l1, *l2;
  int fn_i;
  raptor_uri* fn_uri;
  const unsigned char *fn_name;
  rasqal_extension_fn fn;
  raptor_sequence *fn_args;
  char *error;
  rasqal_literal *fn_result;
  rasqal_world *world;
  
  world = rasqal_new_world();
  if(!world || rasqal_world_open(world)) {
    fprintf(stderr, "%s: rasqal_world init failed\n", program);
    return(1);
  }

  xsd_uri = raptor_new_uri(raptor_xmlschema_datatypes_namespace_uri);
  dateTime_uri = raptor_new_uri_from_uri_local_name(xsd_uri, (const unsigned char*)"dateTime");

  rasqal_init_datatypes();

  fn_args = raptor_new_sequence((raptor_sequence_free_handler*)rasqal_free_literal, (raptor_sequence_print_handler*)rasqal_literal_print);
  l1 = rasqal_new_string_literal((unsigned char*)strdup("2004-05-04"), NULL, raptor_uri_copy(dateTime_uri), NULL);
  raptor_sequence_push(fn_args, l1);
  l2 = rasqal_new_string_literal((unsigned char*)strdup("2003-01-02"), NULL, raptor_uri_copy(dateTime_uri), NULL);
  raptor_sequence_push(fn_args, l2);
  
  fn_i = 0;
  fn_name = rasqal_xsd_datatype_fns[fn_i].name;
  fn = rasqal_xsd_datatype_fns[fn_i].fn;
  fn_uri = rasqal_xsd_datatype_fns[fn_i].uri;

  error = NULL;
  fn_result = fn(fn_uri, fn_args, &error);
  raptor_free_sequence(fn_args);

  if(!fn_result) {
    if(error)
      fprintf(stderr, "function %s failed with error %s\n", fn_name, error);
    else
      fprintf(stderr, "function %s unknown error\n", fn_name);
  } else {
    fprintf(stderr, "function %s returned result: ", fn_name);
    rasqal_literal_print(fn_result, stderr);
    fputc('\n', stderr);
  }
  

  if(fn_result) 
    rasqal_free_literal(fn_result);

  rasqal_finish_datatypes();
  
  raptor_free_uri(xsd_uri);
  raptor_free_uri(dateTime_uri);

  rasqal_free_world(world);
#endif

  return 0;
}
コード例 #27
0
ファイル: check_query.c プロジェクト: xoration/rasqal
int
main(int argc, char *argv[]) 
{ 
  rasqal_world *world;
  raptor_world* raptor_world_ptr = NULL;
  int rc = 0;
  int usage = 0;
  int help = 0;
  const char* query_language = DEFAULT_QUERY_LANGUAGE;
  const char* query_filename = NULL;
  const char* data_format_name = NULL;
  const char* result_filename = NULL;
  const char* result_format_name = NULL;
  unsigned char* query_string = NULL;
  size_t query_len = 0;
  unsigned char *query_base_uri_string = NULL;
  int free_query_base_uri_string = 0;
  raptor_uri* query_base_uri = NULL;
  rasqal_query* rq = NULL;
  rasqal_query_results* results = NULL;
  rasqal_query_results* expected_results = NULL;
  raptor_sequence* data_graphs = NULL;
  raptor_iostream* result_iostr = NULL;
  rasqal_dataset* ds = NULL;
  rasqal_query_results_type results_type;

  /* Set globals */
  if(1) {
    char *p;
    
    program = argv[0];
    if((p = strrchr(program, '/')))
      program = p + 1;
    else if((p = strrchr(program, '\\')))
      program = p + 1;
    argv[0] = program;
  }
  
  world = rasqal_new_world();
  if(!world || rasqal_world_open(world)) {
    fprintf(stderr, "%s: rasqal_world init failed\n", program);
    return(1);
  }
  
  raptor_world_ptr = rasqal_world_get_raptor(world);
  rasqal_world_set_log_handler(world, world, check_query_log_handler);

  /* Option parsing */
  while (!usage && !help)
  {
    int c;
    
#ifdef HAVE_GETOPT_LONG
    int option_index = 0;

    c = getopt_long (argc, argv, GETOPT_STRING, long_options, &option_index);
#else
    c = getopt (argc, argv, GETOPT_STRING);
#endif
    if (c == -1)
      break;

    switch (c) {
      case 0:
      case '?': /* getopt() - unknown option */
        usage = 1;
        break;
        
      case 'd':
        verbose++;
        break;

      case 'F':
        if(optarg) {
          data_format_name = optarg;
        }
        break;
        
      case 'h':
        help = 1;
        break;

      case 'l':
        if(optarg) {
          query_language = optarg;
        }
        break;

      case 'q':
        if(optarg) {
          query_filename = optarg;
        }
        break;

      case 'Q':
        if(optarg) {
          query_base_uri_string = (unsigned char*)optarg;
        }
        break;

      case 'r':
        if(optarg) {
          result_filename = optarg;
        }
        break;
        
      case 'R':
        if(optarg) {
          result_format_name = optarg;
        }
        break;
        
      case 'v':
        fputs(rasqal_version_string, stdout);
        fputc('\n', stdout);
        rasqal_free_world(world);
        exit(0);

      case 'g':
      case 'n':
        if(optarg) {
          rasqal_data_graph *dg = NULL;
          rasqal_data_graph_flags type;

          type = (c == 'n') ? RASQAL_DATA_GRAPH_NAMED : 
                              RASQAL_DATA_GRAPH_BACKGROUND;

          if(!access((const char*)optarg, R_OK)) {
            /* file: use URI */
            unsigned char* source_uri_string;
            raptor_uri* source_uri;
            raptor_uri* graph_name = NULL;

            source_uri_string = raptor_uri_filename_to_uri_string((const char*)optarg);
            source_uri = raptor_new_uri(raptor_world_ptr, source_uri_string);
            raptor_free_memory(source_uri_string);

            if(type == RASQAL_DATA_GRAPH_NAMED) 
              graph_name = source_uri;
            
            if(source_uri)
              dg = rasqal_new_data_graph_from_uri(world,
                                                  source_uri,
                                                  graph_name,
                                                  type,
                                                  NULL, data_format_name,
                                                  NULL);

            if(source_uri)
              raptor_free_uri(source_uri);
          } else {
            raptor_uri* source_uri;
            raptor_uri* graph_name = NULL;

            /* URI: use URI */
            source_uri = raptor_new_uri(raptor_world_ptr,
                                        (const unsigned char*)optarg);
            if(type == RASQAL_DATA_GRAPH_NAMED) 
              graph_name = source_uri;
            
            if(source_uri)
              dg = rasqal_new_data_graph_from_uri(world,
                                                  source_uri,
                                                  graph_name,
                                                  type,
                                                  NULL, data_format_name,
                                                  NULL);

            if(source_uri)
              raptor_free_uri(source_uri);
          }
          
          if(!dg) {
            fprintf(stderr, "%s: Failed to create data graph for `%s'\n",
                    program, optarg);
            return(1);
          }
          
          if(!data_graphs) {
            data_graphs = raptor_new_sequence((raptor_data_free_handler)rasqal_free_data_graph,
                                              NULL);

            if(!data_graphs) {
              fprintf(stderr, "%s: Failed to create data graphs sequence\n",
                      program);
              return(1);
            }
          }

          raptor_sequence_push(data_graphs, dg);
        }
        break;

    }
    
  } /* end while option */


  if(!help && !usage) {
    if(optind != argc) {
      fprintf(stderr, "%s: Extra arguments.\n", program);
      usage = 1;
    } else if(!result_filename) {
      usage = 2; /* Title and usage */
    } else if(!query_filename) {
      usage = 2; /* Title and usage */
    }
  }

  
  if(usage) {
    if(usage > 1) {
      fprintf(stderr, title_format_string, rasqal_version_string);
      fputs("Rasqal home page: ", stderr);
      fputs(rasqal_home_url_string, stderr);
      fputc('\n', stderr);
      fputs(rasqal_copyright_string, stderr);
      fputs("\nLicense: ", stderr);
      fputs(rasqal_license_string, stderr);
      fputs("\n\n", stderr);
    }
    fprintf(stderr, "Try `%s " HELP_ARG(h, help) "' for more information.\n",
            program);
    rasqal_free_world(world);

    exit(1);
  }

  if(help) {
    int i;

    printf(title_format_string, rasqal_version_string);
    puts("Run an RDF query and check it against a known result.");
    printf("Usage: %s [OPTIONS] -g DATA -q QUERY-FILE -r RESULT-FILE\n\n", program);

    fputs(rasqal_copyright_string, stdout);
    fputs("\nLicense: ", stdout);
    puts(rasqal_license_string);
    fputs("Rasqal home page: ", stdout);
    puts(rasqal_home_url_string);

    puts("\nNormal operation is to execute the query in the QUERY-FILE and\ncompare to the query results in RESULT-FILE.");
    puts("\nMain options:");
    puts(HELP_TEXT("g URI", "default-graph URI", "Use URI as the default graph in the dataset"));
    puts(HELP_TEXT("l", "language LANGUAGE    ", "Set query language name to one of:"));
    for(i = 0; 1; i++) {
      const raptor_syntax_description* desc;

      desc = rasqal_world_get_query_language_description(world, i);
      if(!desc)
         break;

      printf("    %-15s              %s", desc->names[0], desc->label);
      if(!i)
        puts(" (default)");
      else
        putchar('\n');
    }
    puts(HELP_TEXT("n URI", "named-graph URI  ", "Add named graph URI to dataset"));
    puts(HELP_TEXT("q FILE", "query QUERY-FILE", "Execute query in file QUERY-FILE"));
    puts(HELP_TEXT("r FILE", "result FILE     ", "Compare to result in file RESULTS-FILE"));


    puts("\nAdditional options:");
    puts(HELP_TEXT("d", "debug                ", "Increase debug message level"));
    puts(HELP_TEXT("F", "data-format NAME     ", "Set the data source format NAME (default: " DEFAULT_DATA_FORMAT_NAME_GRAPH ")"));
    puts(HELP_TEXT("h", "help                 ", "Print this help, then exit"));
    puts(HELP_TEXT("Q URI", "query-base-uri URI", "Set the base URI for the query"));
    puts(HELP_TEXT("R", "result-format NAME   ", "Set the result format NAME (default: " DEFAULT_RESULT_FORMAT_NAME_GRAPH ")"));
    puts("    For variable bindings and boolean results:");

    for(i = 0; 1; i++) {
      const raptor_syntax_description* desc;
 
      desc = rasqal_world_get_query_results_format_description(world, i);
      if(!desc)
         break;
 
      if(desc->flags & RASQAL_QUERY_RESULTS_FORMAT_FLAG_READER) {
        printf("      %-10s     %s", desc->names[0], desc->label);
        if(!strcmp(desc->names[0], DEFAULT_RESULT_FORMAT_NAME_GRAPH))
          puts(" (default)");
        else
          putchar('\n');
      }
    }

    puts("    For RDF graph results:");

    for(i = 0; 1; i++) {
      const raptor_syntax_description *desc;
      desc = raptor_world_get_parser_description(raptor_world_ptr, i);
      if(!desc)
        break;

      printf("      %-15s%s", desc->names[0], desc->label);
      if(!strcmp(desc->names[0], DEFAULT_DATA_FORMAT_NAME_GRAPH))
        puts(" (default)");
      else
        putchar('\n');
    }
    puts(HELP_TEXT("v", "version              ", "Print the Rasqal version"));

    puts("\nReport bugs to http://bugs.librdf.org/");

    rasqal_free_world(world);
    
    exit(0);
  }


  /* Compute query base URI from filename or passed in -Q QUERY-BASE-URI */
  if(!query_base_uri_string) {
    query_base_uri_string = raptor_uri_filename_to_uri_string(query_filename);
    free_query_base_uri_string = 1;
  }
  
  query_base_uri = raptor_new_uri(raptor_world_ptr, query_base_uri_string);
  if(!query_base_uri) {
    fprintf(stderr, "%s: Failed to create URI for %s\n",
            program, query_base_uri_string);
    return(1);
  }

  /* Read query from file into a string */
  query_string = check_query_read_file_string(query_filename,
                                              "query file", &query_len);
  if(!query_string) {
    rc = 1;
    goto tidy_setup;
  }


  /* Report information */
  if(verbose) {
    fprintf(stderr, "%s: Reading query in language %s from file %s  URI %s:\n", 
            program, query_language, query_filename,
            raptor_uri_as_string(query_base_uri));
    if(verbose > 1)
      fprintf(stderr, "%s\n", (const char*)query_string);
    fprintf(stderr, "%s: Reading results from file '%s'\n", 
            program, result_filename);
  }


  /* Parse and prepare query */
  rq = check_query_init_query(world, query_language, query_string,
                              query_base_uri, data_graphs);
  if(!rq) {
    fprintf(stderr, "%s: Parsing query in %s failed\n", program,
            query_filename);
    goto tidy_query;
  }

  /* Query prepared OK - we now know the query details such as result type */


  /* Read expected results */
  if(1) {
    results_type = rasqal_query_get_result_type(rq);
    fprintf(stderr, "%s: Expecting result type %d\n", program, results_type);

    /* Read result file */
    result_iostr = raptor_new_iostream_from_filename(raptor_world_ptr,
                                                     result_filename);
    if(!result_iostr) {
      fprintf(stderr, "%s: result file '%s' open failed - %s", 
              program, result_filename, strerror(errno));
      rc = 1;
      goto tidy_setup;
    }


    switch(results_type) {
      case RASQAL_QUERY_RESULTS_BINDINGS:
      case RASQAL_QUERY_RESULTS_BOOLEAN:
        /* read results via rasqal query results format */
        expected_results = check_query_read_results(world,
                                                    raptor_world_ptr,
                                                    results_type,
                                                    result_iostr,
                                                    result_filename,
                                                    result_format_name);
        raptor_free_iostream(result_iostr); result_iostr = NULL;

        break;

      case RASQAL_QUERY_RESULTS_GRAPH:
        /* read results via raptor parser */

        if(1) {
          const char* format_name = NULL;

          if(result_format_name) {
            if(!raptor_world_is_parser_name(raptor_world_ptr,
                                            result_format_name)) {
              fprintf(stderr,
                      "%s: invalid parser name `%s' for `" HELP_ARG(R, result-format) "'\n\n",
                      program, result_format_name);
            } else
              format_name = result_format_name;
          }

          if(!format_name)
            format_name = DEFAULT_RESULT_FORMAT_NAME_GRAPH;

          
          ds = rasqal_new_dataset(world);
          if(!ds) {
            fprintf(stderr, "%s: Failed to create dataset", program);
            rc = 1;
            goto tidy_setup;
          }

          if(rasqal_dataset_load_graph_iostream(ds, format_name, 
                                                result_iostr, NULL)) {
            fprintf(stderr, "%s: Failed to load graph into dataset", program);
            rc = 1;
            goto tidy_setup;
          }

          raptor_free_iostream(result_iostr); result_iostr = NULL;

          rasqal_free_dataset(ds); ds = NULL;
        }
        break;
        
      case RASQAL_QUERY_RESULTS_SYNTAX:
        fprintf(stderr, 
                "%s: Reading query results format 'syntax' is not supported", 
                program);
        rc = 1;
        goto tidy_setup;
        break;

      case RASQAL_QUERY_RESULTS_UNKNOWN:
        /* failure */
        fprintf(stderr, "%s: Unknown query result format cannot be tested.", 
                program);
        rc = 1;
        goto tidy_setup;
        break;
    }
    
  }


  /* save results for query execution so we can print and rewind */
  rasqal_query_set_store_results(rq, 1);

  results = rasqal_query_execute(rq);
  if(results) {

    switch(results_type) {
      case RASQAL_QUERY_RESULTS_BINDINGS:
        fprintf(stderr, "%s: Expected bindings results:\n", program);
        print_bindings_result_simple(expected_results, stderr, 1);
        
        fprintf(stderr, "%s: Actual bindings results:\n", program);
        print_bindings_result_simple(results, stderr, 1);

        rasqal_query_results_rewind(expected_results);
        rasqal_query_results_rewind(results);

        /* FIXME: should NOT do this if results are expected to be ordered */
        rasqal_query_results_sort(expected_results, rasqal_row_compare);
        rasqal_query_results_sort(results, rasqal_row_compare);

        if(1) {
          compare_query_results* cqr;
          cqr = new_compare_query_results(world,
                                          expected_results, "expected",
                                          results, "actual");
          compare_query_results_set_log_handler(cqr, world,
                                                check_query_log_handler);
          rc = !compare_query_results_compare(cqr);
          free_compare_query_results(cqr); cqr = NULL;
        }
        
        break;
        
      case RASQAL_QUERY_RESULTS_BOOLEAN:
      case RASQAL_QUERY_RESULTS_GRAPH:
      case RASQAL_QUERY_RESULTS_SYNTAX:
      case RASQAL_QUERY_RESULTS_UNKNOWN:
        /* failure */
        fprintf(stderr, "%s: Query result format %d cannot be tested.", 
                program, results_type);
        rc = 1;
        goto tidy_setup;
        break;
    }
  } else
    rc = 1;


  if(verbose) {
    fprintf(stdout, "%s: Result: %s\n", program, rc ? "FAILURE" : "success");
  }

  if(results) {
    rasqal_free_query_results(results); results = NULL;
  }


  tidy_query:
  if(rq)
    rasqal_free_query(rq);


  tidy_setup:
  if(expected_results)
    rasqal_free_query_results(expected_results);

  if(results)
    rasqal_free_query_results(results);

  if(result_iostr)
    raptor_free_iostream(result_iostr);
  
  if(ds)
    rasqal_free_dataset(ds);

  if(free_query_base_uri_string)
    raptor_free_memory(query_base_uri_string);

  if(query_base_uri)
    raptor_free_uri(query_base_uri);

  if(data_graphs)
    raptor_free_sequence(data_graphs);

  rasqal_free_world(world);
  
  return (rc);
}
コード例 #28
0
ファイル: update.c プロジェクト: CloCkWeRX/4store
static int update_op(struct update_context *uc)
{
    fs_rid_vector *vec[4];
    switch (uc->op->type) {
    case RASQAL_UPDATE_TYPE_UNKNOWN:
        add_message(uc, "Unknown update operation", 0);
        return 1;
    case RASQAL_UPDATE_TYPE_CLEAR:
        fs_clear(uc, graph_arg(uc->op->graph_uri));
        return 0;
    case RASQAL_UPDATE_TYPE_CREATE:
        return 0;
    case RASQAL_UPDATE_TYPE_DROP:
        fs_clear(uc, graph_arg(uc->op->graph_uri));
        return 0;
    case RASQAL_UPDATE_TYPE_LOAD:
        fs_load(uc, graph_arg(uc->op->document_uri),
                    graph_arg(uc->op->graph_uri));
        return 0;
#if RASQAL_VERSION >= 924
    case RASQAL_UPDATE_TYPE_ADD:
        fs_add(uc, graph_arg(uc->op->graph_uri),
                   graph_arg(uc->op->document_uri));
        return 0;
    case RASQAL_UPDATE_TYPE_MOVE:
        fs_move(uc, graph_arg(uc->op->graph_uri),
                    graph_arg(uc->op->document_uri));
        return 0;
    case RASQAL_UPDATE_TYPE_COPY:
        fs_copy(uc, graph_arg(uc->op->graph_uri),
                    graph_arg(uc->op->document_uri));
        return 0;
#endif
    case RASQAL_UPDATE_TYPE_UPDATE:
        break;
    }

    fs_hash_freshen();

    raptor_sequence *todel = NULL;
    raptor_sequence *toins = NULL;

    if (uc->op->delete_templates && !uc->op->where) {
        int where = 0;

        /* check to see if it's a DELETE WHERE { } */
        for (int t=0; t<raptor_sequence_size(uc->op->delete_templates); t++) {
            rasqal_triple *tr = raptor_sequence_get_at(uc->op->delete_templates, t);
            if (any_vars(tr)) {
                where = 1;
                break;
            }
        }
        if (where) {
            fs_error(LOG_ERR, "DELETE WHERE { x } not yet supported");
            add_message(uc, "DELETE WHERE { x } not yet supported, use DELETE { x } WHERE { x }", 0);

            return 1;
        }
    }

#if RASQAL_VERSION >= 923
    if (uc->op->where) {
        todel = raptor_new_sequence(NULL, NULL);
        toins = raptor_new_sequence(NULL, NULL);
        raptor_sequence *todel_p = raptor_new_sequence(NULL, NULL);
        raptor_sequence *toins_p = raptor_new_sequence(NULL, NULL);
        raptor_sequence *vars = raptor_new_sequence(NULL, NULL);

        fs_query *q = calloc(1, sizeof(fs_query));
        uc->q = q;
        q->qs = uc->qs;
        q->rq = uc->rq;
        q->flags = FS_BIND_DISTINCT;
#ifdef DEBUG_MERGE
        q->flags |= FS_QUERY_CONSOLE_OUTPUT;
#endif
        q->boolean = 1;
        q->opt_level = 3;
        q->soft_limit = -1;
        q->segments = fsp_link_segments(uc->link);
        q->link = uc->link;
        q->bb[0] = fs_binding_new();
        q->bt = q->bb[0];

        /* hashtable to hold runtime created resources */
        q->tmp_resources = g_hash_table_new_full(fs_rid_hash, fs_rid_equal, g_free, fs_free_cached_resource);

        /* add column to denote join ordering */
        fs_binding_create(q->bb[0], "_ord", FS_RID_NULL, 0);

        if (uc->op->delete_templates) {
            for (int t=0; t<raptor_sequence_size(uc->op->delete_templates); t++) {
                rasqal_triple *tr = raptor_sequence_get_at(uc->op->delete_templates, t);
                if (any_vars(tr)) {
                    fs_check_cons_slot(q, vars, tr->subject);
                    fs_check_cons_slot(q, vars, tr->predicate);
                    fs_check_cons_slot(q, vars, tr->object);
                    raptor_sequence_push(todel_p, tr);
                } else {
                    raptor_sequence_push(todel, tr);
                }
            }
        }

        if (uc->op->insert_templates) {
            for (int t=0; t<raptor_sequence_size(uc->op->insert_templates); t++) {
                rasqal_triple *tr = raptor_sequence_get_at(uc->op->insert_templates, t);
                if (any_vars(tr)) {
                    fs_check_cons_slot(q, vars, tr->subject);
                    fs_check_cons_slot(q, vars, tr->predicate);
                    fs_check_cons_slot(q, vars, tr->object);
                    raptor_sequence_push(toins_p, tr);
                } else {
                    raptor_sequence_push(toins, tr);
                }
            }
        }

        q->num_vars = raptor_sequence_size(vars);

        for (int i=0; i < q->num_vars; i++) {
            rasqal_variable *v = raptor_sequence_get_at(vars, i);
            fs_binding_add(q->bb[0], v, FS_RID_NULL, 1);
        }

        /* perform the WHERE match */
        fs_query_process_pattern(q, uc->op->where, vars);

        q->length = fs_binding_length(q->bb[0]);

        for (int s=0; s<4; s++) {
            vec[s] = fs_rid_vector_new(0);
        }
        for (int t=0; t<raptor_sequence_size(todel_p); t++) {
            rasqal_triple *triple = raptor_sequence_get_at(todel_p, t);
            for (int row=0; row < q->length; row++) {
                delete_rasqal_triple(uc, vec, triple, row);
            }
            if (fs_rid_vector_length(vec[0]) > 1000) {
                fsp_delete_quads_all(uc->link, vec);
            }
        }
        if (fs_rid_vector_length(vec[0]) > 0) {
            fsp_delete_quads_all(uc->link, vec);
        }
        for (int s=0; s<4; s++) {
//fs_rid_vector_print(vec[s], 0, stdout);
            fs_rid_vector_free(vec[s]);
            vec[s] = NULL;
        }

        for (int t=0; t<raptor_sequence_size(toins_p); t++) {
            rasqal_triple *triple = raptor_sequence_get_at(toins_p, t);
            for (int row=0; row < q->length; row++) {
                insert_rasqal_triple(uc, triple, row);
            }
        }

        /* must not free the rasqal_query */
        q->rq = NULL;
        fs_query_free(q);
        uc->q = NULL;
    } else {
        todel = uc->op->delete_templates;
        toins = uc->op->insert_templates;
    }
#else
    if (uc->op->where) {
        fs_error(LOG_ERR, "DELETE/INSERT WHERE requires Rasqal 0.9.23 or newer");
        add_message(uc, "DELETE/INSERT WHERE requires Rasqal 0.9.23 or newer", 0);
    }
#endif

    /* delete constant triples */
    if (todel) {
        for (int s=0; s<4; s++) {
            vec[s] = fs_rid_vector_new(0);
        }
        for (int t=0; t<raptor_sequence_size(todel); t++) {
            rasqal_triple *triple = raptor_sequence_get_at(todel, t);
            if (any_vars(triple)) {
                continue;
            }
            delete_rasqal_triple(uc, vec, triple, 0);
        }
        if (fs_rid_vector_length(vec[0]) > 0) {
            fsp_delete_quads_all(uc->link, vec);
        }
        for (int s=0; s<4; s++) {
            fs_rid_vector_free(vec[s]);
            vec[s] = NULL;
        }
    }

    /* insert constant triples */
    if (toins) {
        for (int t=0; t<raptor_sequence_size(toins); t++) {
            rasqal_triple *triple = raptor_sequence_get_at(toins, t);
            if (any_vars(triple)) {
                continue;
            }
            insert_rasqal_triple(uc, triple, 0);
        }
    }
    fs_hash_freshen();

    return 0;
}
コード例 #29
0
int
main(int argc, char *argv[]) 
{
  const char *program = rasqal_basename(argv[0]);
  rasqal_rowsource *rowsource = NULL;
  rasqal_world* world = NULL;
  rasqal_query* query = NULL;
  raptor_sequence* row_seq = NULL;
  raptor_sequence* expr_seq = NULL;
  int failures = 0;
  rasqal_variables_table* vt;
  rasqal_rowsource *input_rs = NULL;
  int vars_count;
  raptor_sequence* vars_seq = NULL;
  int test_id;

  world = rasqal_new_world();
  if(!world || rasqal_world_open(world)) {
    fprintf(stderr, "%s: rasqal_world init failed\n", program);
    return(1);
  }
  
  query = rasqal_new_query(world, "sparql", NULL);

  vt = query->vars_table;
  
  for(test_id = 0; test_id < GROUP_TESTS_COUNT; test_id++) {
    int expected_rows_count = test_data[test_id].rows;
    int expected_vars_count = test_data[test_id].vars;
    const int* expected_group_ids = test_data[test_id].group_ids;
    int expected_ngroups = test_data[test_id].ngroups;
    raptor_sequence* seq = NULL;
    int count;
    int size;
    int i;
    int groups_counted;
    int last_group_id;
    
    vars_count = expected_vars_count;
    row_seq = rasqal_new_row_sequence(world, vt, test_data[test_id].data,
                                      vars_count, &vars_seq);
    if(row_seq) {
      input_rs = rasqal_new_rowsequence_rowsource(world, query, vt, 
                                                  row_seq, vars_seq);
      /* vars_seq and row_seq are now owned by input_rs */
      vars_seq = row_seq = NULL;
    }
    if(!input_rs) {
      fprintf(stderr, "%s: failed to create rowsequence rowsource\n", program);
      failures++;
      goto tidy;
    }


#ifdef HAVE_RAPTOR2_API
    expr_seq = raptor_new_sequence((raptor_data_free_handler)rasqal_free_expression,
                                   (raptor_data_print_handler)rasqal_expression_print);
#else
    expr_seq = raptor_new_sequence((raptor_sequence_free_handler*)rasqal_free_expression,
                                   (raptor_sequence_print_handler*)rasqal_expression_print);
#endif

    if(test_data[test_id].expr_vars[0] != NULL) {
      int vindex;
      const unsigned char* var_name;
      
      for(vindex = 0;
          (var_name = (const unsigned char*)test_data[test_id].expr_vars[vindex] );
          vindex++) {
        rasqal_variable* v;
        rasqal_literal *l = NULL;
        rasqal_expression* e = NULL;

        v = rasqal_variables_table_get_by_name(vt, var_name);
        if(v)
          l = rasqal_new_variable_literal(world, v);

        if(l)
          e = rasqal_new_literal_expression(world, l);

        if(e)
          raptor_sequence_push(expr_seq, e);
        else {
          fprintf(stderr, "%s: failed to create variable %s\n", program,
                  (const char*)var_name);
          failures++;
          goto tidy;
        }
        
      }
    }
    
    rowsource = rasqal_new_groupby_rowsource(world, query, input_rs, expr_seq);
    /* input_rs is now owned by rowsource */
    input_rs = NULL;
   
    if(!rowsource) {
      fprintf(stderr, "%s: failed to create groupby rowsource\n", program);
      failures++;
      goto tidy;
    }

    seq = rasqal_rowsource_read_all_rows(rowsource);
    if(!seq) {
      fprintf(stderr,
              "%s: test %d rasqal_rowsource_read_all_rows() returned a NULL seq for a groupby rowsource\n",
              program, test_id);
      failures++;
      goto tidy;
    }
    count = raptor_sequence_size(seq);
    if(count != expected_rows_count) {
      fprintf(stderr,
              "%s: test %d rasqal_rowsource_read_all_rows() returned %d rows for a groupby rowsource, expected %d\n",
              program, test_id, count, expected_rows_count);
      failures++;
      goto tidy;
    }

    size = rasqal_rowsource_get_size(rowsource);
    if(size != expected_vars_count) {
      fprintf(stderr,
              "%s: test %d rasqal_rowsource_get_size() returned %d columns (variables) for a groupby rowsource, expected %d\n",
              program, test_id, size, expected_vars_count);
      failures++;
      goto tidy;
    }

    groups_counted = 0;
    last_group_id = -1;
    for(i = 0; i < count; i++) {
      rasqal_row* row = raptor_sequence_get_at(seq, i);

      if(row->group_id != last_group_id) {
        groups_counted++;
        last_group_id = row->group_id;
      }

      if(row->group_id != expected_group_ids[i]) {
        fprintf(stderr, "%s: test %d row #%d has group_id %d, expected %d\n",
                program, test_id, i, row->group_id, expected_group_ids[i]);
        failures++;
        goto tidy;
      }
      
    }
    
    if(groups_counted != expected_ngroups) {
        fprintf(stderr, "%s: test %d returnd %d groups, expected %d\n",
                program, test_id, groups_counted, expected_ngroups);
        failures++;
        goto tidy;
      }

#ifdef RASQAL_DEBUG
    rasqal_rowsource_print_row_sequence(rowsource, seq, stderr);
#endif

    raptor_free_sequence(seq); seq = NULL;

    rasqal_free_rowsource(rowsource); rowsource = NULL;

    if(expr_seq)
      raptor_free_sequence(expr_seq);
    expr_seq = NULL;
  }
  
  tidy:
  if(expr_seq)
    raptor_free_sequence(expr_seq);
  if(rowsource)
    rasqal_free_rowsource(rowsource);
  if(input_rs)
    rasqal_free_rowsource(input_rs);
  if(query)
    rasqal_free_query(query);
  if(world)
    rasqal_free_world(world);

  return failures;
}
コード例 #30
0
ファイル: roqet.c プロジェクト: sengels/rasqal
int
main(int argc, char *argv[])
{
    int query_from_string = 0;
    unsigned char *query_string = NULL;
    unsigned char *uri_string = NULL;
    int free_uri_string = 0;
    unsigned char *base_uri_string = NULL;
    rasqal_query *rq = NULL;
    rasqal_query_results *results;
    const char *ql_name = "sparql";
    char *ql_uri = NULL;
    int rc = 0;
    raptor_uri *uri = NULL;
    raptor_uri *base_uri = NULL;
    char *filename = NULL;
    char *p;
    int usage = 0;
    int help = 0;
    int quiet = 0;
    int count = 0;
    int dryrun = 0;
    raptor_sequence* data_graphs = NULL;
    const char *result_format = NULL;
    query_output_format output_format = QUERY_OUTPUT_UNKNOWN;
    rasqal_feature query_feature = (rasqal_feature)-1;
    int query_feature_value= -1;
    unsigned char* query_feature_string_value = NULL;
    rasqal_world *world;
    raptor_world* raptor_world_ptr = NULL;
#ifdef RASQAL_INTERNAL
    int store_results = -1;
#endif
    char* data_graph_parser_name = NULL;
    raptor_iostream* iostr = NULL;
    const unsigned char* service_uri_string = 0;
    raptor_uri* service_uri = NULL;

    program = argv[0];
    if((p = strrchr(program, '/')))
        program = p + 1;
    else if((p = strrchr(program, '\\')))
        program = p + 1;
    argv[0] = program;

    world = rasqal_new_world();
    if(!world || rasqal_world_open(world)) {
        fprintf(stderr, "%s: rasqal_world init failed\n", program);
        return(1);
    }

    raptor_world_ptr = rasqal_world_get_raptor(world);
    rasqal_world_set_log_handler(world, world, roqet_log_handler);

#ifdef STORE_RESULTS_FLAG
    /* This is for debugging only */
    if(1) {
        char* sr = getenv("RASQAL_DEBUG_STORE_RESULTS");
        if(sr)
            store_results = atoi(sr);
    }
#endif

    while (!usage && !help)
    {
        int c;

#ifdef HAVE_GETOPT_LONG
        int option_index = 0;

        c = getopt_long (argc, argv, GETOPT_STRING, long_options, &option_index);
#else
        c = getopt (argc, argv, GETOPT_STRING);
#endif
        if (c == -1)
            break;

        switch (c) {
        case 0:
        case '?': /* getopt() - unknown option */
            usage = 1;
            break;

        case 'c':
            count = 1;
            break;

        case 'd':
            output_format = QUERY_OUTPUT_UNKNOWN;
            if(optarg) {
                int i;

                for(i = 1; i <= QUERY_OUTPUT_LAST; i++)
                    if(!strcmp(optarg, query_output_format_labels[i][0])) {
                        output_format = (query_output_format)i;
                        break;
                    }

            }
            if(output_format == QUERY_OUTPUT_UNKNOWN) {
                int i;
                fprintf(stderr,
                        "%s: invalid argument `%s' for `" HELP_ARG(d, dump-query) "'\n",
                        program, optarg);
                for(i = 1; i <= QUERY_OUTPUT_LAST; i++)
                    fprintf(stderr,
                            "  %-12s for %s\n", query_output_format_labels[i][0],
                            query_output_format_labels[i][1]);
                usage = 1;
            }
            break;


        case 'e':
            if(optarg) {
                query_string = (unsigned char*)optarg;
                query_from_string = 1;
            }
            break;

        case 'f':
            if(optarg) {
                if(!strcmp(optarg, "help")) {
                    int i;

                    fprintf(stderr, "%s: Valid query features are:\n", program);
                    for(i = 0; i < (int)rasqal_get_feature_count(); i++) {
                        const char *feature_name;
                        const char *feature_label;
                        if(!rasqal_features_enumerate(world, (rasqal_feature)i,
                                                      &feature_name, NULL,
                                                      &feature_label)) {
                            const char *feature_type;
                            feature_type = (rasqal_feature_value_type((rasqal_feature)i) == 0) ? "" : " (string)";
                            fprintf(stderr, "  %-20s  %s%s\n", feature_name, feature_label,
                                    feature_type);
                        }
                    }
                    fputs("Features are set with `" HELP_ARG(f, feature) " FEATURE=VALUE or `-f FEATURE'\nand take a decimal integer VALUE except where noted, defaulting to 1 if omitted.\n", stderr);

                    rasqal_free_world(world);
                    exit(0);
                } else {
                    int i;
                    size_t arg_len = strlen(optarg);

                    for(i = 0; i < (int)rasqal_get_feature_count(); i++) {
                        const char *feature_name;
                        size_t len;

                        if(rasqal_features_enumerate(world, (rasqal_feature)i,
                                                     &feature_name, NULL, NULL))
                            continue;

                        len = strlen(feature_name);

                        if(!strncmp(optarg, feature_name, len)) {
                            query_feature = (rasqal_feature)i;
                            if(rasqal_feature_value_type(query_feature) == 0) {
                                if(len < arg_len && optarg[len] == '=')
                                    query_feature_value=atoi(&optarg[len + 1]);
                                else if(len == arg_len)
                                    query_feature_value = 1;
                            } else {
                                if(len < arg_len && optarg[len] == '=')
                                    query_feature_string_value = (unsigned char*)&optarg[len + 1];
                                else if(len == arg_len)
                                    query_feature_string_value = (unsigned char*)"";
                            }
                            break;
                        }
                    }

                    if(query_feature_value < 0 && !query_feature_string_value) {
                        fprintf(stderr, "%s: invalid argument `%s' for `" HELP_ARG(f, feature) "'\nTry '%s " HELP_ARG(f, feature) " help' for a list of valid features\n",
                                program, optarg, program);
                        usage = 1;
                    }
                }
            }
            break;

        case 'F':
            if(optarg) {
                if(!raptor_world_is_parser_name(raptor_world_ptr, optarg)) {
                    fprintf(stderr,
                            "%s: invalid parser name `%s' for `" HELP_ARG(F, format) "'\n\n",
                            program, optarg);
                    usage = 1;
                } else {
                    data_graph_parser_name = optarg;
                }
            }
            break;

        case 'h':
            help = 1;
            break;

        case 'n':
            dryrun = 1;
            break;

        case 'p':
            if(optarg)
                service_uri_string = (const unsigned char*)optarg;
            break;

        case 'r':
            if(optarg)
                result_format = optarg;
            break;

        case 'i':
            if(rasqal_language_name_check(world, optarg))
                ql_name = optarg;
            else {
                int i;

                fprintf(stderr,
                        "%s: invalid argument `%s' for `" HELP_ARG(i, input) "'\n",
                        program, optarg);
                fprintf(stderr, "Valid arguments are:\n");
                for(i = 0; 1; i++) {
                    const raptor_syntax_description* desc;
                    desc = rasqal_world_get_query_language_description(world, i);
                    if(desc == NULL)
                        break;

                    fprintf(stderr, "  %-18s for %s\n", desc->names[0], desc->label);
                }
                usage = 1;
            }
            break;

        case 'q':
            quiet = 1;
            break;

        case 's':
        case 'D':
        case 'G':
            if(optarg) {
                rasqal_data_graph *dg = NULL;
                rasqal_data_graph_flags type;

                type = (c == 's' || c == 'G') ? RASQAL_DATA_GRAPH_NAMED :
                       RASQAL_DATA_GRAPH_BACKGROUND;

                if(!strcmp((const char*)optarg, "-")) {
                    /* stdin: use an iostream not a URI data graph */
                    unsigned char* source_uri_string;
                    raptor_uri* iostr_base_uri = NULL;
                    raptor_uri* graph_name = NULL;

                    /* FIXME - get base URI from somewhere else */
                    source_uri_string = (unsigned char*)"file:///dev/stdin";

                    iostr_base_uri = raptor_new_uri(raptor_world_ptr, source_uri_string);
                    if(iostr_base_uri) {
                        iostr = raptor_new_iostream_from_file_handle(raptor_world_ptr,
                                stdin);
                        if(iostr)
                            dg = rasqal_new_data_graph_from_iostream(world,
                                    iostr, iostr_base_uri,
                                    graph_name,
                                    type,
                                    NULL,
                                    data_graph_parser_name,
                                    NULL);
                    }

                    if(base_uri)
                        raptor_free_uri(base_uri);
                } else if(!access((const char*)optarg, R_OK)) {
                    /* file: use URI */
                    unsigned char* source_uri_string;
                    raptor_uri* source_uri;
                    raptor_uri* graph_name = NULL;

                    source_uri_string = raptor_uri_filename_to_uri_string((const char*)optarg);
                    source_uri = raptor_new_uri(raptor_world_ptr, source_uri_string);
                    raptor_free_memory(source_uri_string);

                    if(type == RASQAL_DATA_GRAPH_NAMED)
                        graph_name = source_uri;

                    if(source_uri)
                        dg = rasqal_new_data_graph_from_uri(world,
                                                            source_uri,
                                                            graph_name,
                                                            type,
                                                            NULL, data_graph_parser_name,
                                                            NULL);

                    if(source_uri)
                        raptor_free_uri(source_uri);
                } else {
                    raptor_uri* source_uri;
                    raptor_uri* graph_name = NULL;

                    /* URI: use URI */
                    source_uri = raptor_new_uri(raptor_world_ptr,
                                                (const unsigned char*)optarg);
                    if(type == RASQAL_DATA_GRAPH_NAMED)
                        graph_name = source_uri;

                    if(source_uri)
                        dg = rasqal_new_data_graph_from_uri(world,
                                                            source_uri,
                                                            graph_name,
                                                            type,
                                                            NULL, data_graph_parser_name,
                                                            NULL);

                    if(source_uri)
                        raptor_free_uri(source_uri);
                }

                if(!dg) {
                    fprintf(stderr, "%s: Failed to create data graph for `%s'\n",
                            program, optarg);
                    return(1);
                }

                if(!data_graphs) {
                    data_graphs = raptor_new_sequence((raptor_data_free_handler)rasqal_free_data_graph,
                                                      NULL);

                    if(!data_graphs) {
                        fprintf(stderr, "%s: Failed to create data graphs sequence\n",
                                program);
                        return(1);
                    }
                }

                raptor_sequence_push(data_graphs, dg);
            }
            break;

        case 'W':
            if(optarg)
                warning_level = atoi(optarg);
            else
                warning_level = 0;
            rasqal_world_set_warning_level(world, warning_level);
            break;

        case 'E':
            ignore_errors = 1;
            break;

        case 'v':
            fputs(rasqal_version_string, stdout);
            fputc('\n', stdout);
            rasqal_free_world(world);
            exit(0);

#ifdef STORE_RESULTS_FLAG
        case STORE_RESULTS_FLAG:
            store_results = (!strcmp(optarg, "yes") || !strcmp(optarg, "YES"));
            break;
#endif

        }

    }

    if(!help && !usage) {
        if(service_uri_string) {
            if(optind != argc && optind != argc-1)
                usage = 2; /* Title and usage */
        } else if(query_string) {
            if(optind != argc && optind != argc-1)
                usage = 2; /* Title and usage */
        } else {
            if(optind != argc-1 && optind != argc-2)
                usage = 2; /* Title and usage */
        }
    }


    if(usage) {
        if(usage > 1) {
            fprintf(stderr, title_format_string, rasqal_version_string);
            fputs("Rasqal home page: ", stderr);
            fputs(rasqal_home_url_string, stderr);
            fputc('\n', stderr);
            fputs(rasqal_copyright_string, stderr);
            fputs("\nLicense: ", stderr);
            fputs(rasqal_license_string, stderr);
            fputs("\n\n", stderr);
        }
        fprintf(stderr, "Try `%s " HELP_ARG(h, help) "' for more information.\n",
                program);
        rasqal_free_world(world);

        exit(1);
    }

    if(help) {
        int i;

        printf(title_format_string, rasqal_version_string);
        puts("Run an RDF query giving variable bindings or RDF triples.");
        printf("Usage: %s [OPTIONS] <query URI> [base URI]\n", program);
        printf("       %s [OPTIONS] -e <query string> [base URI]\n", program);
        printf("       %s [OPTIONS] -p <SPARQL protocol service URI> -e <query string> [base URI]\n\n", program);

        fputs(rasqal_copyright_string, stdout);
        fputs("\nLicense: ", stdout);
        puts(rasqal_license_string);
        fputs("Rasqal home page: ", stdout);
        puts(rasqal_home_url_string);

        puts("\nNormal operation is to execute the query retrieved from URI <query URI>");
        puts("and print the results in a simple text format.");
        puts("\nMain options:");
        puts(HELP_TEXT("e", "exec QUERY      ", "Execute QUERY string instead of <query URI>"));
        puts(HELP_TEXT("p", "protocol URI    ", "Execute QUERY against a SPARQL protocol service URI"));
        puts(HELP_TEXT("i", "input LANGUAGE  ", "Set query language name to one of:"));
        for(i = 0; 1; i++) {
            const raptor_syntax_description* desc;

            desc = rasqal_world_get_query_language_description(world, i);
            if(!desc)
                break;

            printf("    %-15s         %s", desc->names[0], desc->label);
            if(!i)
                puts(" (default)");
            else
                putchar('\n');
        }

        puts(HELP_TEXT("r", "results FORMAT  ", "Set query results output format to one of:"));
        puts("    For variable bindings and boolean results:");
        puts("      simple                A simple text format (default)");

        for(i = 0; 1; i++) {
            const raptor_syntax_description* desc;

            desc = rasqal_world_get_query_results_format_description(world, i);
            if(!desc)
                break;

            if(desc->flags & RASQAL_QUERY_RESULTS_FORMAT_FLAG_WRITER)
                printf("      %-10s            %s\n", desc->names[0], desc->label);
        }

        puts("    For RDF graph results:");

        for(i = 0; 1; i++) {
            const raptor_syntax_description *desc;
            desc = raptor_world_get_parser_description(raptor_world_ptr, i);
            if(!desc)
                break;

            printf("      %-15s       %s", desc->names[0], desc->label);
            if(!i)
                puts(" (default)");
            else
                putchar('\n');
        }
        puts("\nAdditional options:");
        puts(HELP_TEXT("c", "count             ", "Count triples - no output"));
        puts(HELP_TEXT("d FORMAT", "dump-query FORMAT", HELP_PAD "Print the parsed query out in FORMAT:"));
        puts(HELP_TEXT("D URI", "data URI      ", "RDF data source URI"));
        for(i = 1; i <= QUERY_OUTPUT_LAST; i++)
            printf("      %-15s         %s\n", query_output_format_labels[i][0],
                   query_output_format_labels[i][1]);
        puts(HELP_TEXT("E", "ignore-errors     ", "Ignore error messages"));
        puts(HELP_TEXT("f FEATURE(=VALUE)", "feature FEATURE(=VALUE)", HELP_PAD "Set query features" HELP_PAD "Use `-f help' for a list of valid features"));
        puts(HELP_TEXT("F NAME", "format NAME  ", "Set data source format name (default: guess)"));
        puts(HELP_TEXT("G URI", "named URI     ", "RDF named graph data source URI"));
        puts(HELP_TEXT("h", "help              ", "Print this help, then exit"));
        puts(HELP_TEXT("n", "dryrun            ", "Prepare but do not run the query"));
        puts(HELP_TEXT("q", "quiet             ", "No extra information messages"));
        puts(HELP_TEXT("s URI", "source URI    ", "Same as `-G URI'"));
        puts(HELP_TEXT("v", "version           ", "Print the Rasqal version"));
        puts(HELP_TEXT("w", "walk-query        ", "Print query.  Same as '-d structure'"));
        puts(HELP_TEXT("W LEVEL", "warnings LEVEL", HELP_PAD "Set warning message LEVEL from 0: none to 100: all"));
#ifdef STORE_RESULTS_FLAG
        puts(HELP_TEXT_LONG("store-results BOOL", "DEBUG: Set store results yes/no BOOL"));
#endif
        puts("\nReport bugs to http://bugs.librdf.org/");

        rasqal_free_world(world);

        exit(0);
    }


    if(service_uri_string) {
        service_uri = raptor_new_uri(raptor_world_ptr, service_uri_string);
        if(optind == argc-1) {
            base_uri_string = (unsigned char*)argv[optind];
        }
    } else if(query_string) {
        if(optind == argc-1)
            base_uri_string = (unsigned char*)argv[optind];
    } else {
        if(optind == argc-1)
            uri_string = (unsigned char*)argv[optind];
        else {
            uri_string = (unsigned char*)argv[optind++];
            base_uri_string = (unsigned char*)argv[optind];
        }

        /* If uri_string is "path-to-file", turn it into a file: URI */
        if(!strcmp((const char*)uri_string, "-")) {
            if(!base_uri_string) {
                fprintf(stderr, "%s: A Base URI is required when reading from standard input.\n",
                        program);
                return(1);
            }

            uri_string = NULL;
        } else if(!access((const char*)uri_string, R_OK)) {
            filename = (char*)uri_string;
            uri_string = raptor_uri_filename_to_uri_string(filename);
            free_uri_string = 1;
        }

        if(uri_string) {
            uri = raptor_new_uri(raptor_world_ptr, uri_string);
            if(!uri) {
                fprintf(stderr, "%s: Failed to create URI for %s\n",
                        program, uri_string);
                return(1);
            }
        } else
            uri = NULL; /* stdin */
    }

    if(!base_uri_string) {
        if(uri)
            base_uri = raptor_uri_copy(uri);
    } else
        base_uri = raptor_new_uri(raptor_world_ptr, base_uri_string);

    if(base_uri_string && !base_uri) {
        fprintf(stderr, "%s: Failed to create URI for %s\n",
                program, base_uri_string);
        return(1);
    }


    if(service_uri_string) {
        /* NOP - nothing to do here */
    } else if(query_string) {
        /* NOP - already got it */
    } else if(!uri_string) {
        size_t read_len;

        query_string = (unsigned char*)calloc(FILE_READ_BUF_SIZE, 1);
        read_len = fread(query_string, FILE_READ_BUF_SIZE, 1, stdin);
        if(read_len < FILE_READ_BUF_SIZE) {
            if(ferror(stdin))
                fprintf(stderr, "%s: query string stdin read failed - %s\n",
                        program, strerror(errno));
            else
                fprintf(stderr, "%s: query string stdin read failed - no error\n",
                        program);
            rc = 1;
            goto tidy_setup;
        }

        query_from_string = 0;
    } else if(filename) {
        raptor_stringbuffer *sb = raptor_new_stringbuffer();
        size_t len;
        FILE *fh;
        unsigned char* buffer;

        fh = fopen(filename, "r");
        if(!fh) {
            fprintf(stderr, "%s: file '%s' open failed - %s",
                    program, filename, strerror(errno));
            rc = 1;
            goto tidy_setup;
        }

        buffer = (unsigned char*)malloc(FILE_READ_BUF_SIZE);

        while(!feof(fh)) {
            size_t read_len;

            read_len = fread((char*)buffer, 1, FILE_READ_BUF_SIZE, fh);
            if(read_len > 0)
                raptor_stringbuffer_append_counted_string(sb, buffer, read_len, 1);

            if(read_len < FILE_READ_BUF_SIZE) {
                if(ferror(fh)) {
                    fprintf(stderr, "%s: file '%s' read failed - %s\n",
                            program, filename, strerror(errno));
                    free(buffer);
                    fclose(fh);
                    rc = 1;
                    goto tidy_setup;
                }

                break;
            }
        }
        free(buffer);
        fclose(fh);

        len = raptor_stringbuffer_length(sb);
        query_string = (unsigned char*)malloc(len + 1);
        raptor_stringbuffer_copy_to_string(sb, query_string, len);
        raptor_free_stringbuffer(sb);
        query_from_string = 0;
    } else {
        raptor_www *www;

        www = raptor_new_www(raptor_world_ptr);
        if(www) {
            raptor_www_fetch_to_string(www, uri, (void**)&query_string, NULL, malloc);
            raptor_free_www(www);
        }

        if(!query_string || error_count) {
            fprintf(stderr, "%s: Retrieving query at URI '%s' failed\n",
                    program, uri_string);
            rc = 1;
            goto tidy_setup;
        }

        query_from_string = 0;
    }


    if(!quiet) {
        if(service_uri) {
            fprintf(stderr, "%s: Calling SPARQL service at URI %s", program,
                    service_uri_string);
            if(query_string)
                fprintf(stderr, " with query '%s'", query_string);

            if(base_uri_string)
                fprintf(stderr, " with base URI %s\n", base_uri_string);

            fputc('\n', stderr);
        } else if(query_from_string) {
            if(base_uri_string)
                fprintf(stderr, "%s: Running query '%s' with base URI %s\n", program,
                        query_string, base_uri_string);
            else
                fprintf(stderr, "%s: Running query '%s'\n", program,
                        query_string);
        } else if(filename) {
            if(base_uri_string)
                fprintf(stderr, "%s: Querying from file %s with base URI %s\n", program,
                        filename, base_uri_string);
            else
                fprintf(stderr, "%s: Querying from file %s\n", program, filename);
        } else if(uri_string) {
            if(base_uri_string)
                fprintf(stderr, "%s: Querying URI %s with base URI %s\n", program,
                        uri_string, base_uri_string);
            else
                fprintf(stderr, "%s: Querying URI %s\n", program, uri_string);
        }
    }



    if(service_uri) {
        /* Execute query remotely */
        if(!dryrun)
            results = roqet_call_sparql_service(world, service_uri, query_string,
                                                data_graphs,
                                                /* service_format */ NULL);
    } else {
        /* Execute query in this query engine (from URI or from -e QUERY) */
        rq = roqet_init_query(world,
                              ql_name, ql_uri, query_string,
                              base_uri,
                              query_feature, query_feature_value,
                              query_feature_string_value,
                              store_results,
                              data_graphs);

        if(!rq) {
            rc = 1;
            goto tidy_query;
        }

        if(output_format != QUERY_OUTPUT_UNKNOWN && !quiet)
            roqet_print_query(rq, raptor_world_ptr, output_format, base_uri);

        if(!dryrun) {
            results = rasqal_query_execute(rq);
        }

    }


    /* No results from dryrun */
    if(dryrun)
        goto tidy_query;

    if(!results) {
        fprintf(stderr, "%s: Query execution failed\n", program);
        rc = 1;
        goto tidy_query;
    }

    if(rasqal_query_results_is_bindings(results)) {
        if(result_format)
            rc = print_formatted_query_results(world, results,
                                               raptor_world_ptr, stdout,
                                               result_format, base_uri, quiet);
        else
            print_bindings_result_simple(results, stdout, quiet, count);
    } else if(rasqal_query_results_is_boolean(results)) {
        if(result_format)
            rc = print_formatted_query_results(world, results,
                                               raptor_world_ptr, stdout,
                                               result_format, base_uri, quiet);
        else
            print_boolean_result_simple(results, stdout, quiet);
    } else if(rasqal_query_results_is_graph(results)) {
        if(!result_format)
            result_format = DEFAULT_GRAPH_FORMAT;

        rc = print_graph_result(rq, results, raptor_world_ptr,
                                stdout, result_format, base_uri, quiet);
    } else {
        fprintf(stderr, "%s: Query returned unknown result format\n", program);
        rc = 1;
    }

    rasqal_free_query_results(results);

tidy_query:
    if(!query_from_string)
        free(query_string);

    if(rq)
        rasqal_free_query(rq);

tidy_setup:

    if(data_graphs)
        raptor_free_sequence(data_graphs);
    if(base_uri)
        raptor_free_uri(base_uri);
    if(uri)
        raptor_free_uri(uri);
    if(free_uri_string)
        raptor_free_memory(uri_string);
    if(iostr)
        raptor_free_iostream(iostr);
    if(service_uri)
        raptor_free_uri(service_uri);

    rasqal_free_world(world);

    if(error_count && !ignore_errors)
        return 1;

    if(warning_count && warning_level != 0)
        return 2;

    return (rc);
}