Пример #1
0
/**
 * rasqal_new_service_rowsource:
 * @world: world object
 * @query: query object
 * @service_uri: service URI
 * @query_string: query to send to service
 * @data_graphs: sequence of data graphs (or NULL)
 * @rs_flags: service rowsource flags
 *
 * INTERNAL - create a new rowsource that takes rows from a service
 *
 * All arguments are copied.
 *
 * Return value: new rowsource or NULL on failure
 */
rasqal_rowsource*
rasqal_new_service_rowsource(rasqal_world *world, rasqal_query* query,
                             raptor_uri* service_uri,
                             const unsigned char* query_string,
                             raptor_sequence* data_graphs,
                             unsigned int rs_flags)
{
  rasqal_service_rowsource_context* con = NULL;
  rasqal_service* svc = NULL;
  int flags = 0;
  int silent = (rs_flags & RASQAL_ENGINE_BITFLAG_SILENT);

  if(!world || !query_string)
    goto fail;
  
  svc = rasqal_new_service(query->world, service_uri, query_string,
                           data_graphs);
  if(!svc) {
    if(!silent)
      goto fail;

    /* Silent errors so tidy up and return empty rowsource */
    RASQAL_FREE(cstring, query_string);
    if(data_graphs)
      raptor_free_sequence(data_graphs);

    return rasqal_new_empty_rowsource(world, query);
  }

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

  con->svc = svc;
  con->query = query;
  con->flags = rs_flags;

  return rasqal_new_rowsource_from_handler(world, query,
                                           con,
                                           &rasqal_service_rowsource_handler,
                                           query->vars_table,
                                           flags);

  fail:
  if(svc)
    rasqal_free_service(svc);
  if(con)
    RASQAL_FREE(rasqal_service_rowsource_context, con);
  if(query_string)
    RASQAL_FREE(cstring, query_string);
  if(data_graphs)
    raptor_free_sequence(data_graphs);

  return NULL;
}
Пример #2
0
static rasqal_rowsource*
rasqal_algebra_filter_algebra_node_to_rowsource(rasqal_engine_algebra_data* execution_data,
                                                rasqal_algebra_node* node,
                                                rasqal_engine_error *error_p)
{
  rasqal_query *query = execution_data->query;
  rasqal_rowsource *rs;

  if(node->node1) {
    rs = rasqal_algebra_node_to_rowsource(execution_data, node->node1, error_p);
  } else {
    rs = rasqal_new_empty_rowsource(query->world, query);
  }

  if(!rs || *error_p)
    return NULL;

  return rasqal_new_filter_rowsource(query->world, query, rs, node->expr);
}
Пример #3
0
static int
rasqal_service_rowsource_init(rasqal_rowsource* rowsource, void *user_data)
{
  rasqal_service_rowsource_context* con;

  con = (rasqal_service_rowsource_context*)user_data;

  con->rowsource = rasqal_service_execute_as_rowsource(con->svc,
                                                       con->query->vars_table);

  if(!con->rowsource) {
    /* Silent errors return an empty rowsource */

    if(con->flags & RASQAL_ENGINE_BITFLAG_SILENT) {
      con->rowsource = rasqal_new_empty_rowsource(con->query->world,
                                                  con->query);
      return 0;
    }

    return 1;
  }
  
  return 0;
}
Пример #4
0
static rasqal_rowsource*
rasqal_algebra_graph_algebra_node_to_rowsource(rasqal_engine_algebra_data* execution_data,
                                               rasqal_algebra_node* node,
                                               rasqal_engine_error *error_p)
{
  rasqal_query *query = execution_data->query;
  rasqal_rowsource *rs;
  rasqal_literal *graph = node->graph;
  rasqal_variable* v;

  if(!graph) {
    RASQAL_DEBUG1("graph algebra node has NULL graph\n");
    return NULL;
  }

/* 
This code checks that #1-#3 below are present and
then executes parts #1 and #2 here.

The graph rowsource created by rasqal_new_graph_rowsource() executes #3


http://www.w3.org/TR/2008/REC-rdf-sparql-query-20080115/#sparqlAlgebraEval

SPARQL Query Language for RDF - Evaluation of a Graph Pattern

#1 if IRI is a graph name in D
eval(D(G), Graph(IRI,P)) = eval(D(D[IRI]), P)

#2 if IRI is not a graph name in D
eval(D(G), Graph(IRI,P)) = the empty multiset

#3 eval(D(G), Graph(var,P)) =
     Let R be the empty multiset
     foreach IRI i in D
        R := Union(R, Join( eval(D(D[i]), P) , Omega(?var->i) )
     the result is R

*/
  v = rasqal_literal_as_variable(graph);
  if(!v && graph->type != RASQAL_LITERAL_URI) {
    /* value is neither a variable or URI literal - error */
    RASQAL_DEBUG1("graph algebra node is neither variable or URI\n");
    return NULL;
  }
  
  if(!v && graph->type == RASQAL_LITERAL_URI) {
    if(rasqal_query_dataset_contains_named_graph(query, graph->value.uri)) {
      /* case #1 - IRI is a graph name in D */

      /* Set the origin of all triple patterns inside node->node1 to
       * URI graph->value.uri
       *
       * FIXME - this is a hack.  The graph URI should be a parameter
       * to all rowsource constructors.
       */
      rasqal_algebra_node_set_origin(query, node->node1, graph);

      rs = rasqal_algebra_node_to_rowsource(execution_data, node->node1,
                                            error_p);
    } else {
      /* case #2 - IRI is not a graph name in D - return empty rowsource */
      rasqal_free_algebra_node(node->node1);
      node->node1 = NULL;
      
      rs = rasqal_new_empty_rowsource(query->world, query);
    }

    if(!rs || *error_p)
      rs = NULL;
    return rs;
  }


  /* case #3 - a variable */
  rs = rasqal_algebra_node_to_rowsource(execution_data, node->node1, error_p);
  if(!rs || *error_p)
    return NULL;

  return rasqal_new_graph_rowsource(query->world, query, rs, v);
}
Пример #5
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;
  rasqal_row* row = NULL;
  int count;
  raptor_sequence* seq = NULL;
  int failures = 0;

  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);
  
  rowsource = rasqal_new_empty_rowsource(world, query);
  if(!rowsource) {
    fprintf(stderr, "%s: failed to create empty rowsource\n", program);
    failures++;
    goto tidy;
  }

  row = rasqal_rowsource_read_row(rowsource);
  if(!row) {
    fprintf(stderr,
            "%s: read_row failed to return a row for an empty rowsource\n",
            program);
    failures++;
    goto tidy;
  }

  if(row->size) {
    fprintf(stderr,
            "%s: read_row returned an non-empty row size %d for a empty stream\n",
            program, row->size);
    failures++;
    goto tidy;
  }
  
  count = rasqal_rowsource_get_rows_count(rowsource);
  if(count != 1) {
    fprintf(stderr, "%s: read_rows returned count %d for a empty stream\n",
            program, count);
    failures++;
    goto tidy;
  }

  rasqal_free_row(row); row = NULL;

  rasqal_free_rowsource(rowsource);

  /* re-init rowsource */
  rowsource = rasqal_new_empty_rowsource(world, query);
  
  seq = rasqal_rowsource_read_all_rows(rowsource);
  if(!seq) {
    fprintf(stderr, "%s: read_rows returned a NULL seq for a empty stream\n",
            program);
    failures++;
    goto tidy;
  }

  count = raptor_sequence_size(seq);
  if(count != 1) {
    fprintf(stderr, "%s: read_rows returned size %d seq for a empty stream\n",
            program, count);
    failures++;
    goto tidy;
  }


  tidy:
  if(row)
    rasqal_free_row(row);
  if(seq)
    raptor_free_sequence(seq);
  if(rowsource)
    rasqal_free_rowsource(rowsource);
  if(query)
    rasqal_free_query(query);
  if(world)
    rasqal_free_world(world);

  return failures;
}