Пример #1
0
static int
rasqal_join_rowsource_finish(rasqal_rowsource* rowsource, void *user_data)
{
  rasqal_join_rowsource_context* con;
  con = (rasqal_join_rowsource_context*)user_data;

  if(con->left_row)
    rasqal_free_row(con->left_row);
  
  if(con->left)
    rasqal_free_rowsource(con->left);
  
  if(con->right)
    rasqal_free_rowsource(con->right);
  
  if(con->right_map)
    RASQAL_FREE(int, con->right_map);
  
  if(con->expr)
    rasqal_free_expression(con->expr);
  
  if(con->rc_map)
    rasqal_free_row_compatible(con->rc_map);
  
  RASQAL_FREE(rasqal_join_rowsource_context, con);

  return 0;
}
Пример #2
0
/**
 * 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;
}
Пример #3
0
rasqal_rowsource*
rasqal_new_distinct_rowsource(rasqal_world *world,
                              rasqal_query *query,
                              rasqal_rowsource* rowsource)
{
  rasqal_distinct_rowsource_context *con;
  int flags = 0;
  
  if(!world || !query || !rowsource)
    goto fail;
  
  con = (rasqal_distinct_rowsource_context*)RASQAL_CALLOC(rasqal_distinct_rowsource_context, 1, sizeof(rasqal_distinct_rowsource_context));
  if(!con)
    goto fail;

  con->rowsource = rowsource;

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

  fail:
  if(rowsource)
    rasqal_free_rowsource(rowsource);
  return NULL;
}
Пример #4
0
/**
 * rasqal_new_sort_rowsource:
 * @world: query world
 * @query: query results object
 * @rowsource: input rowsource
 * @order_seq: order sequence (shared, may be NULL)
 * @distinct: distinct flag
 *
 * INTERNAL - create a SORT over rows from input rowsource
 *
 * The @rowsource becomes owned by the new rowsource.
 *
 * Return value: new rowsource or NULL on failure
 */
rasqal_rowsource*
rasqal_new_sort_rowsource(rasqal_world *world,
                          rasqal_query *query,
                          rasqal_rowsource *rowsource,
                          raptor_sequence* order_seq,
                          int distinct)
{
  rasqal_sort_rowsource_context *con;
  int flags = 0;

  if(!world || !query || !rowsource)
    goto fail;
  
  con = RASQAL_CALLOC(rasqal_sort_rowsource_context*, 1, sizeof(*con));
  if(!con)
    goto fail;

  con->rowsource = rowsource;
  con->order_seq = order_seq;
  con->distinct = distinct;

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

  fail:
  if(rowsource)
    rasqal_free_rowsource(rowsource);
  return NULL;
}
Пример #5
0
/**
 * rasqal_new_join_rowsource:
 * @world: world object
 * @query: query object
 * @left: input left (first) rowsource
 * @right: input right (second) rowsource
 * @join_type: join type
 * @expr: join expression to filter result rows
 *
 * INTERNAL - create a new JOIN over two rowsources
 *
 * This uses the number of variables in @vt to set the rowsource size
 * (order size is always 0) and then checks that all the rows in the
 * sequence are the same.  If not, construction fails and NULL is
 * returned.
 *
 * The @left and @right rowsources become owned by the rowsource.
 *
 * Return value: new rowsource or NULL on failure
 */
rasqal_rowsource*
rasqal_new_join_rowsource(rasqal_world *world,
                          rasqal_query* query,
                          rasqal_rowsource* left,
                          rasqal_rowsource* right,
                          rasqal_join_type join_type,
                          rasqal_expression *expr)
{
  rasqal_join_rowsource_context* con;
  int flags = 0;

  if(!world || !query || !left || !right)
    goto fail;

  /* only left outer join and cross join supported */
  if(join_type != RASQAL_JOIN_TYPE_LEFT &&
     join_type != RASQAL_JOIN_TYPE_NATURAL)
    goto fail;
  
  con = RASQAL_CALLOC(rasqal_join_rowsource_context*, 1, sizeof(*con));
  if(!con)
    goto fail;

  con->left = left;
  con->right = right;
  con->join_type = join_type;
  con->expr = rasqal_new_expression_from_expression(expr);
  
  return rasqal_new_rowsource_from_handler(world, query,
                                           con,
                                           &rasqal_join_rowsource_handler,
                                           query->vars_table,
                                           flags);

  fail:
  if(left)
    rasqal_free_rowsource(left);
  if(right)
    rasqal_free_rowsource(right);
  return NULL;
}
Пример #6
0
static int
rasqal_aggregation_rowsource_finish(rasqal_rowsource* rowsource,
                                    void *user_data)
{
  rasqal_aggregation_rowsource_context* con;

  con = (rasqal_aggregation_rowsource_context*)user_data;

  if(con->expr_data) {
    int i;

    for(i = 0; i < con->expr_count; i++) {
      rasqal_agg_expr_data* expr_data = &con->expr_data[i];

      if(expr_data->agg_user_data)
        rasqal_builtin_agg_expression_execute_finish(expr_data->agg_user_data);

      if(expr_data->exprs_seq)
        raptor_free_sequence(expr_data->exprs_seq);

      if(expr_data->expr)
        rasqal_free_expression(expr_data->expr);

      if(expr_data->map)
        rasqal_free_map(expr_data->map);
    }

    RASQAL_FREE(rasqal_agg_expr_data, con->expr_data);
  }
  
  if(con->exprs_seq)
    raptor_free_sequence(con->exprs_seq);

  if(con->vars_seq)
    raptor_free_sequence(con->vars_seq);

  if(con->rowsource)
    rasqal_free_rowsource(con->rowsource);
  
  if(con->saved_row)
    rasqal_free_row(con->saved_row);

  if(con->input_values)
    raptor_free_sequence(con->input_values);

  RASQAL_FREE(rasqal_aggregation_rowsource_context, con);

  return 0;
}
Пример #7
0
static int
rasqal_graph_rowsource_finish(rasqal_rowsource* rowsource, void *user_data)
{
  rasqal_graph_rowsource_context *con;
  con = (rasqal_graph_rowsource_context*)user_data;

  if(con->rowsource)
    rasqal_free_rowsource(con->rowsource);
  
  rasqal_variable_set_value(con->var, NULL);

  RASQAL_FREE(rasqal_graph_rowsource_context, con);

  return 0;
}
Пример #8
0
static int
rasqal_distinct_rowsource_finish(rasqal_rowsource* rowsource, void *user_data)
{
  rasqal_distinct_rowsource_context *con;
  con = (rasqal_distinct_rowsource_context*)user_data;

  if(con->rowsource)
    rasqal_free_rowsource(con->rowsource);
  
  if(con->map)
    rasqal_free_map(con->map);

  RASQAL_FREE(rasqal_distinct_rowsource_context, con);

  return 0;
}
Пример #9
0
static int
rasqal_service_rowsource_finish(rasqal_rowsource* rowsource, void *user_data)
{
  rasqal_service_rowsource_context* con;

  con = (rasqal_service_rowsource_context*)user_data;

  if(con->svc)
    rasqal_free_service(con->svc);

  if(con->rowsource)
    rasqal_free_rowsource(con->rowsource);

  RASQAL_FREE(rasqal_service_rowsource_context, con);

  return 0;
}
Пример #10
0
rasqal_rowsource*
rasqal_new_groupby_rowsource(rasqal_world *world, rasqal_query* query,
                             rasqal_rowsource* rowsource,
                             raptor_sequence* expr_seq)
{
  rasqal_groupby_rowsource_context* con;
  int flags = 0;

  if(!world || !query)
    return NULL;
  
  con = (rasqal_groupby_rowsource_context*)RASQAL_CALLOC(rasqal_groupby_rowsource_context, 1, sizeof(*con));
  if(!con)
    goto fail;

  con->rowsource = rowsource;
  con->expr_seq_size = 0;

  if(expr_seq) {
    con->expr_seq = rasqal_expression_copy_expression_sequence(expr_seq);

    if(!con->expr_seq)
      goto fail;

    con->expr_seq_size = raptor_sequence_size(expr_seq);
  }
  
  return rasqal_new_rowsource_from_handler(world, query,
                                           con,
                                           &rasqal_groupby_rowsource_handler,
                                           query->vars_table,
                                           flags);

  fail:

  if(rowsource)
    rasqal_free_rowsource(rowsource);
  if(expr_seq)
    raptor_free_sequence(expr_seq);

  return NULL;
}
Пример #11
0
static int
rasqal_query_engine_algebra_execute_finish(void* ex_data,
                                           rasqal_engine_error *error_p)
{
  rasqal_engine_algebra_data* execution_data;

  execution_data = (rasqal_engine_algebra_data*)ex_data;

  if(execution_data) {
    if(execution_data->algebra_node)
      rasqal_free_algebra_node(execution_data->algebra_node);

    if(execution_data->triples_source)
      rasqal_free_triples_source(execution_data->triples_source);

    if(execution_data->rowsource)
      rasqal_free_rowsource(execution_data->rowsource);
  }

  return 0;
}
Пример #12
0
/**
 * rasqal_query_results_formatter_read:
 * @world: rasqal world object
 * @iostr: #raptor_iostream to read the query from
 * @formatter: #rasqal_query_results_formatter object
 * @results: #rasqal_query_results query results format
 * @base_uri: #raptor_uri base URI of the input format
 *
 * Read the query results using the given formatter from an iostream
 * 
 * See rasqal_query_results_formats_enumerate() to get the
 * list of syntax URIs and their description. 
 *
 * Return value: non-0 on failure
 **/
int
rasqal_query_results_formatter_read(rasqal_world *world,
                                    raptor_iostream *iostr,
                                    rasqal_query_results_formatter* formatter,
                                    rasqal_query_results* results,
                                    raptor_uri *base_uri)
{
  rasqal_rowsource* rowsource = NULL;
  
  RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(world, rasqal_world, 1);
  RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(iostr, raptor_iostream, 1);
  RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(formatter, rasqal_query_results_formatter, 1);
  RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(results, rasqal_query_results, 1);
  RASQAL_ASSERT_OBJECT_POINTER_RETURN_VALUE(base_uri, raptor_uri, 1);

  if(formatter->factory->reader)
    return formatter->factory->reader(iostr, results, base_uri);

  if(!formatter->factory->get_rowsource)
    return 1;
  
  rowsource = formatter->factory->get_rowsource(world, 
                                                rasqal_query_results_get_variables_table(results),
                                                iostr, base_uri);
  if(!rowsource)
    return 1;

  while(1) {
    rasqal_row* row = rasqal_rowsource_read_row(rowsource);
    if(!row)
      break;
    rasqal_query_results_add_row(results, row);
  }

  if(rowsource)
    rasqal_free_rowsource(rowsource);
  
  return 0;
}
Пример #13
0
static int
rasqal_groupby_rowsource_finish(rasqal_rowsource* rowsource, void *user_data)
{
  rasqal_groupby_rowsource_context* con;
  con = (rasqal_groupby_rowsource_context*)user_data;

  if(con->rowsource)
    rasqal_free_rowsource(con->rowsource);
  
  if(con->expr_seq)
    raptor_free_sequence(con->expr_seq);
  
  if(con->tree)
    raptor_free_avltree(con->tree);
  
  if(con->group_iterator)
    raptor_free_avltree_iterator(con->group_iterator);

  RASQAL_FREE(rasqal_groupby_rowsource_context, con);

  return 0;
}
Пример #14
0
static rasqal_rowsource*
rasqal_algebra_join_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 *left_rs;
  rasqal_rowsource *right_rs;

  left_rs = rasqal_algebra_node_to_rowsource(execution_data, node->node1,
                                             error_p);
  if(!left_rs || *error_p)
    return NULL;

  right_rs = rasqal_algebra_node_to_rowsource(execution_data, node->node2,
                                              error_p);
  if(!right_rs || *error_p) {
    rasqal_free_rowsource(left_rs);
    return NULL;
  }

  return rasqal_new_join_rowsource(query->world, query, left_rs, right_rs, RASQAL_JOIN_TYPE_NATURAL, node->expr);
}
Пример #15
0
int
main(int argc, char *argv[]) 
{
  const char *program = rasqal_basename(argv[0]);
  rasqal_rowsource *rowsource = NULL;
  rasqal_rowsource *left_rs = NULL;
  rasqal_rowsource *right_rs = NULL;
  rasqal_world* world = NULL;
  rasqal_query* query = NULL;
  int count;
  raptor_sequence* seq = NULL;
  int failures = 0;
  rasqal_variables_table* vt;
  int size;
  int expected_size = EXPECTED_COLUMNS_COUNT;
  int i;
  raptor_sequence* vars_seq = NULL;
  int test_count;
  
  world = rasqal_new_world(); rasqal_world_open(world);
  
  query = rasqal_new_query(world, "sparql", NULL);
  
  vt = query->vars_table;

  for(test_count = 0; test_count < JOIN_TESTS_COUNT; test_count++) {
    rasqal_join_type join_type = join_test_config[test_count].join_type;
    int expected_count = join_test_config[test_count].expected;
    int vars_count;

    fprintf(stderr, "%s: test #%d  join type %d\n", program, test_count,
            RASQAL_GOOD_CAST(int, join_type));

    /* 2 variables and 3 rows */
    vars_count = 2;
    seq = rasqal_new_row_sequence(world, vt, join_1_data_2x3_rows, vars_count,
                                  &vars_seq);
    if(!seq) {
      fprintf(stderr,
              "%s: failed to create left sequence of %d vars\n", program,
              vars_count);
      failures++;
      goto tidy;
    }

    left_rs = rasqal_new_rowsequence_rowsource(world, query, vt, seq, vars_seq);
    if(!left_rs) {
      fprintf(stderr, "%s: failed to create left rowsource\n", program);
      failures++;
      goto tidy;
    }
    /* vars_seq and seq are now owned by left_rs */
    vars_seq = seq = NULL;

    /* 3 variables and 2 rows */
    vars_count = 3;
    seq = rasqal_new_row_sequence(world, vt, join_2_data_3x2_rows, vars_count,
                                  &vars_seq);
    if(!seq) {
      fprintf(stderr,
              "%s: failed to create right sequence of %d rows\n", program,
              vars_count);
      failures++;
      goto tidy;
    }

    right_rs = rasqal_new_rowsequence_rowsource(world, query, vt, seq, vars_seq);
    if(!right_rs) {
      fprintf(stderr, "%s: failed to create right rowsource\n", program);
      failures++;
      goto tidy;
    }
    /* vars_seq and seq are now owned by right_rs */
    vars_seq = seq = NULL;

    rowsource = rasqal_new_join_rowsource(world, query, left_rs, right_rs,
                                          join_type, NULL);
    if(!rowsource) {
      fprintf(stderr, "%s: failed to create join rowsource\n", program);
      failures++;
      goto tidy;
    }
    /* left_rs and right_rs are now owned by rowsource */
    left_rs = right_rs = NULL;

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

    size = rasqal_rowsource_get_size(rowsource);
    if(size != expected_size) {
      fprintf(stderr,
              "%s: read_rows returned %d columns (variables) for a join rowsource, expected %d\n",
              program, size, expected_size);
      failures++;
      goto tidy;
    }
    for(i = 0; i < expected_size; i++) {
      rasqal_variable* v;
      const char* name = NULL;
      const char *expected_name = join_result_vars[i];

      v = rasqal_rowsource_get_variable_by_offset(rowsource, i);
      if(!v) {
        fprintf(stderr,
              "%s: read_rows had NULL column (variable) #%d expected %s\n",
                program, i, expected_name);
        failures++;
        goto tidy;
      }
      name = RASQAL_GOOD_CAST(const char*, v->name);
      if(strcmp(name, expected_name)) {
        fprintf(stderr,
              "%s: read_rows returned column (variable) #%d %s but expected %s\n",
                program, i, name, expected_name);
        failures++;
        goto tidy;
      }
    }

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

    raptor_free_sequence(seq); seq = NULL;
    rasqal_free_rowsource(rowsource); rowsource = NULL;
    
    /* end test_count loop */
  }
  
  tidy:
  if(seq)
    raptor_free_sequence(seq);
  if(left_rs)
    rasqal_free_rowsource(left_rs);
  if(right_rs)
    rasqal_free_rowsource(right_rs);
  if(rowsource)
    rasqal_free_rowsource(rowsource);
  if(query)
    rasqal_free_query(query);
  if(world)
    rasqal_free_world(world);

  return failures;
}
Пример #16
0
int
main(int argc, char *argv[]) 
{
  const char *program = rasqal_basename(argv[0]);
  rasqal_rowsource *rowsource = NULL;
  raptor_sequence *seq = NULL;
  rasqal_world* world = NULL;
  rasqal_query* query = NULL;
  rasqal_row* row = NULL;
  int count;
  int failures = 0;
  rasqal_variables_table* vt;
  int rows_count;
  int i;
  raptor_sequence* vars_seq = NULL;
  
  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);
  
  /* test 1-row rowsource (2 variables) */
  rows_count = 1;
  
#ifdef RASQAL_DEBUG  
  RASQAL_DEBUG2("Testing %d-row rowsource\n", rows_count);
#endif

  vt = rasqal_new_variables_table(world);

  /* add 2 variables to table and 1 row sequence */
  seq = rasqal_new_row_sequence(world, vt, test_1_rows, 2, &vars_seq);
  if(!seq) {
    fprintf(stderr, "%s: failed to create sequence of %d rows\n", program,
            rows_count);
    failures++;
    goto tidy;
  }

  rowsource = rasqal_new_rowsequence_rowsource(world, query, vt, seq, vars_seq);
  if(!rowsource) {
    fprintf(stderr, "%s: failed to create %d-row sequence rowsource\n",
            program, rows_count);
    failures++;
    goto tidy;
  }
  /* vars_seq and seq are now owned by rowsource */
  vars_seq = seq = NULL;

  row = rasqal_rowsource_read_row(rowsource);
  if(!row) {
    fprintf(stderr,
            "%s: read_row returned no row for a %d-row sequence rowsource\n",
            program, rows_count);
    failures++;
    goto tidy;
  }

#ifdef RASQAL_DEBUG  
  RASQAL_DEBUG1("Result Row:\n  ");
  rasqal_row_print(row, stderr);
  fputc('\n', stderr);
#endif

  rasqal_free_row(row); row = NULL;

  count = rasqal_rowsource_get_rows_count(rowsource);
  if(count != rows_count) {
    fprintf(stderr,
            "%s: read_rows returned count %d instead of %d for a %d-row sequence rowsource\n",
            program, count, rows_count, rows_count);
    failures++;
    goto tidy;
  }

  row = rasqal_rowsource_read_row(rowsource);
  if(row) {
    fprintf(stderr,
            "%s: read_row returned > %d rows for a %d-row sequence rowsource\n",
            program, rows_count, rows_count);
    failures++;
    goto tidy;
  }
  

  rasqal_free_rowsource(rowsource); rowsource = NULL;
  rasqal_free_variables_table(vt); vt = NULL;

  /* test 3-row rowsource */
  rows_count = 3;

#ifdef RASQAL_DEBUG  
  RASQAL_DEBUG2("Testing %d-row rowsource\n", rows_count);
#endif

  vt = rasqal_new_variables_table(world);

  /* add 4 variables to table and 3 row sequence */
  seq = rasqal_new_row_sequence(world, vt, test_3_rows, 4, &vars_seq);
  if(!seq) {
    fprintf(stderr, "%s: failed to create sequence of %d rows\n",
            program, rows_count);
    failures++;
    goto tidy;
  }

  rowsource = rasqal_new_rowsequence_rowsource(world, query, vt, seq, vars_seq);
  if(!rowsource) {
    fprintf(stderr, "%s: failed to create %d-row sequence rowsource\n",
            program, rows_count);
    failures++;
    goto tidy;
  }
  /* vars_seq and seq are now owned by rowsource */
  vars_seq = seq = NULL;

  for(i = 0; i < rows_count; i++) {
    row = rasqal_rowsource_read_row(rowsource);
    if(!row) {
      fprintf(stderr,
              "%s: read_row returned no row for row %d in a %d-row sequence rowsource\n",
              program, i, rows_count);
      failures++;
      goto tidy;
    }

  #ifdef RASQAL_DEBUG  
    RASQAL_DEBUG1("Result Row:\n  ");
    rasqal_row_print(row, stderr);
    fputc('\n', stderr);
  #endif

    rasqal_free_row(row); row = NULL;
  }
  
  count = rasqal_rowsource_get_rows_count(rowsource);
  if(count != rows_count) {
    fprintf(stderr,
            "%s: read_rows returned count %d instead of %d for a %d-row sequence rowsource\n",
            program, count, rows_count, rows_count);
    failures++;
    goto tidy;
  }

  row = rasqal_rowsource_read_row(rowsource);
  if(row) {
    fprintf(stderr,
            "%s: read_row returned >%d rows for a %d-row sequence rowsource\n",
            program, rows_count, rows_count);
    failures++;
    goto tidy;
  }
  
  rasqal_free_rowsource(rowsource); rowsource = NULL;
  rasqal_free_variables_table(vt); vt = NULL;


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

  return failures;
}
Пример #17
0
int
main(int argc, char *argv[])
{
    const char *program = rasqal_basename(argv[0]);
#define TEST_ITEMS_COUNT 9
    int i;

    for(i = 0; i < 4; i++) {
        rasqal_rowsource *rowsource;
        size_t count;

        /* for _from_file */
        FILE *handle = NULL;
        /* for _from_string */
        void *string;
        size_t string_len;

        switch(i) {
        case 0:
#ifdef RASQAL_DEBUG
            fprintf(stderr, "%s: Creating rowsource from a filename '%s'\n",
                    program, OUT_FILENAME);
#endif
            rowsource = rasqal_new_rowsource_from_filename((const char*)IN_FILENAME);
            if(!rowsource) {
                fprintf(stderr, "%s: Failed to create rowsource to filename '%s'\n",
                        program, OUT_FILENAME);
                exit(1);
            }
            break;

        case 1:
#ifdef RASQAL_DEBUG
            fprintf(stderr, "%s: Creating rowsource from file handle\n", program);
#endif
            handle = fopen((const char*)OUT_FILENAME, "wb");
            rowsource = rasqal_new_rowsource_from_file_handle(handle);
            if(!rowsource) {
                fprintf(stderr, "%s: Failed to create rowsource from a file handle\n", program);
                exit(1);
            }
            break;

        case 2:
#ifdef RASQAL_DEBUG
            fprintf(stderr, "%s: Creating rowsource from a string\n", program);
#endif
            rowsource = rasqal_new_rowsource_from_string(&string, &string_len, NULL);
            if(!rowsource) {
                fprintf(stderr, "%s: Failed to create rowsource from a string\n", program);
                exit(1);
            }
            break;

        case 3:
#ifdef RASQAL_DEBUG
            fprintf(stderr, "%s: Creating rowsource from a sink\n", program);
#endif
            rowsource = rasqal_new_rowsource_from_sink();
            if(!rowsource) {
                fprintf(stderr, "%s: Failed to create rowsource from a sink\n", program);
                exit(1);
            }
            break;

        default:
            fprintf(stderr, "%s: Unknown test case %d init\n", program, i);
            exit(1);
        }


        count = rasqal_rowsource_get_rows_count(rowsource);
        if(count != OUT_BYTES_COUNT) {
            fprintf(stderr, "%s: I/O stream wrote %d bytes, expected %d\n", program,
                    (int)count, (int)OUT_BYTES_COUNT);
            return 1;
        }

#ifdef RASQAL_DEBUG
        fprintf(stderr, "%s: Freeing rowsource\n", program);
#endif
        rasqal_free_rowsource(rowsource);

        switch(i) {
        case 0:
            remove(OUT_FILENAME);
            break;

        case 1:
            fclose(handle);
            remove(OUT_FILENAME);
            break;

        case 2:
            if(!string) {
                fprintf(stderr, "%s: I/O stream failed to create a string\n", program);
                return 1;
            }
            if(string_len != count) {
                fprintf(stderr, "%s: I/O stream created a string length %d, expected %d\n", program, (int)string_len, (int)count);
                return 1;
            }
            rasqal_free_memory(string);
            break;

        case 3:
            break;

        default:
            fprintf(stderr, "%s: Unknown test case %d tidy\n", program, i);
            exit(1);
        }

    }

    /* keep gcc -Wall happy */
    return(0);
}
Пример #18
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;
}
Пример #19
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;
}
Пример #20
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;
}
Пример #21
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;
  raptor_uri* service_uri;
  const unsigned char* query_string;
  raptor_sequence* data_graphs = NULL;
  unsigned int rs_flags = 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);
  
  service_uri = raptor_new_uri(world->raptor_world_ptr,
                               (const unsigned char *)"http://example.org/service");
  query_string = (const unsigned char*)"SELECT * WHERE { ?s ?p ?o }";
  rowsource = rasqal_new_service_rowsource(world, query, service_uri,
                                           query_string, data_graphs,
                                           rs_flags);
  if(!rowsource) {
    fprintf(stderr, "%s: failed to create service 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 service rowsource\n",
            program);
    failures++;
    goto tidy;
  }

  if(row->size) {
    fprintf(stderr,
            "%s: read_row returned an non-service row size %d for a service 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 service stream\n",
            program, count);
    failures++;
    goto tidy;
  }
  
  rasqal_free_rowsource(rowsource);

  /* re-init rowsource */
  rowsource = rasqal_new_service_rowsource(world, query, service_uri,
                                           query_string, data_graphs,
                                           rs_flags);
  
  seq = rasqal_rowsource_read_all_rows(rowsource);
  if(!seq) {
    fprintf(stderr, "%s: read_rows returned a NULL seq for a service 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 service stream\n",
            program, count);
    failures++;
    goto tidy;
  }


  tidy:
  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;
}
Пример #22
0
int
main(int argc, char *argv[]) 
{
  const char *program = rasqal_basename(argv[0]);
  rasqal_rowsource *rowsource = NULL;
  rasqal_rowsource *left_rs = NULL;
  rasqal_rowsource *right_rs = NULL;
  rasqal_world* world = NULL;
  rasqal_query* query = NULL;
  raptor_sequence* seq = NULL;
  int failures = 0;
  int vars_count;
  rasqal_variables_table* vt;
  raptor_sequence* vars_seq = NULL;
  rasqal_row_compatible* rc_map = NULL;
  int i;
  
  world = rasqal_new_world(); rasqal_world_open(world);
  
  query = rasqal_new_query(world, "sparql", NULL);
  
  vt = query->vars_table;

  /* 3 variables and 4 rows */
  vars_count = 3;
  seq = rasqal_new_row_sequence(world, vt, compatible_data_abc_rows, vars_count,
                                &vars_seq);
  if(!seq) {
    fprintf(stderr,
            "%s: failed to create left sequence of %d vars\n", program,
            vars_count);
    failures++;
    goto tidy;
  }

  left_rs = rasqal_new_rowsequence_rowsource(world, query, vt, seq, vars_seq);
  if(!left_rs) {
    fprintf(stderr, "%s: failed to create left rowsource\n", program);
    failures++;
    goto tidy;
  }
  /* vars_seq and seq are now owned by left_rs */
  vars_seq = seq = NULL;
  
  /* 3 variables and 4 rows */
  vars_count = 3;
  seq = rasqal_new_row_sequence(world, vt, compatible_data_abcd_rows,
                                vars_count, &vars_seq);
  if(!seq) {
    fprintf(stderr,
            "%s: failed to create right sequence of %d rows\n", program,
            vars_count);
    failures++;
    goto tidy;
  }

  right_rs = rasqal_new_rowsequence_rowsource(world, query, vt, seq, vars_seq);
  if(!right_rs) {
    fprintf(stderr, "%s: failed to create right rowsource\n", program);
    failures++;
    goto tidy;
  }
  /* vars_seq and seq are now owned by right_rs */
  vars_seq = seq = NULL;

  rc_map = rasqal_new_row_compatible(vt, left_rs, right_rs);
  if(!rc_map) {
    fprintf(stderr, "%s: failed to create row compatible\n", program);
    failures++;
    goto tidy;
  }

  rasqal_print_row_compatible(stderr, rc_map);

#ifdef RASQAL_DEBUG
  fputs("\n", stderr);
#endif

  for(i = 0; i < EXPECTED_ROWS_COUNT; i++) {
    rasqal_row *left_row = rasqal_rowsource_read_row(left_rs);
    rasqal_row *right_row = rasqal_rowsource_read_row(right_rs);
    int expected = expected_compatible_results[i];
    int compatible;

    if(!left_row) {
      fprintf(stderr, "%s: FAILED left rowsource ended early at row #%d\n", program, i);
      failures++;
      goto tidy;
    }
    if(!right_row) {
      fprintf(stderr, "%s: FAILED right rowsource ended early at row #%d\n", program, i);
      failures++;
      goto tidy;
    }

    compatible = rasqal_row_compatible_check(rc_map, left_row, right_row);
    RASQAL_DEBUG4("%s: compatible check for row #%d returned %d\n",
                  program, i, compatible);
    if(compatible != expected) {
      fprintf(stderr, 
              "%s: FAILED compatible check for row #%d returned %d  expected %d\n",
              program, i, compatible, expected);
      failures++;
    }

#ifdef RASQAL_DEBUG
    fputs("\n", stderr);
#endif

    if(left_row)
      rasqal_free_row(left_row);
    if(right_row)
      rasqal_free_row(right_row);
  }
  
  tidy:
  if(rc_map)
    rasqal_free_row_compatible(rc_map);
  if(seq)
    raptor_free_sequence(seq);
  if(left_rs)
    rasqal_free_rowsource(left_rs);
  if(right_rs)
    rasqal_free_rowsource(right_rs);
  if(rowsource)
    rasqal_free_rowsource(rowsource);
  if(query)
    rasqal_free_query(query);
  if(world)
    rasqal_free_world(world);

  return failures;
}
Пример #23
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;
}