/** * 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; }
/** * rasqal_free_query_results: * @query_results: #rasqal_query_results object * * Destructor - destroy a rasqal_query_results. * **/ void rasqal_free_query_results(rasqal_query_results* query_results) { rasqal_query* query; if(!query_results) return; query = query_results->query; if(query_results->executed) { if(query_results->execution_factory->execute_finish) { rasqal_engine_error execution_error = RASQAL_ENGINE_OK; query_results->execution_factory->execute_finish(query_results->execution_data, &execution_error); /* ignoring failure of execute_finish */ } } if(query_results->execution_data) RASQAL_FREE(rasqal_engine_execution_data, query_results->execution_data); if(query_results->row) rasqal_free_row(query_results->row); if(query_results->results_sequence) raptor_free_sequence(query_results->results_sequence); /* free terms owned by static query_results->result_triple */ raptor_free_statement(&query_results->result_triple); if(query_results->triple) rasqal_free_triple(query_results->triple); if(query_results->vars_table) rasqal_free_variables_table(query_results->vars_table); if(query) rasqal_query_remove_query_result(query, query_results); RASQAL_FREE(rasqal_query_results, query_results); }
/** * rasqal_free_rowsource: * @rowsource: rowsource object * * INTERNAL - Destructor - destroy an rowsource. **/ void rasqal_free_rowsource(rasqal_rowsource *rowsource) { if(!rowsource) return; if(rowsource->handler->finish) rowsource->handler->finish(rowsource, rowsource->user_data); if(rowsource->vars_table) rasqal_free_variables_table(rowsource->vars_table); if(rowsource->variables_sequence) raptor_free_sequence(rowsource->variables_sequence); if(rowsource->rows_sequence) raptor_free_sequence(rowsource->rows_sequence); RASQAL_FREE(rasqal_rowsource, rowsource); }
static int rasqal_rowsource_sparql_xml_finish(rasqal_rowsource* rowsource, void *user_data) { rasqal_rowsource_sparql_xml_context* con; con=(rasqal_rowsource_sparql_xml_context*)user_data; if(con->base_uri) raptor_free_uri(con->base_uri); if(con->sax2) raptor_free_sax2(con->sax2); if(con->results_sequence) raptor_free_sequence(con->results_sequence); if(con->vars_table) rasqal_free_variables_table(con->vars_table); RASQAL_FREE(rasqal_rowsource_sparql_xml_context, con); return 0; }
static rasqal_query_results* check_query_read_results(rasqal_world* world, raptor_world* raptor_world_ptr, rasqal_query_results_type results_type, raptor_iostream* result_iostr, const char* result_filename, const char* result_format_name) { rasqal_variables_table* vars_table = NULL; const char* format_name = NULL; rasqal_query_results_formatter* qrf = NULL; unsigned char *query_results_base_uri_string = NULL; raptor_uri* query_results_base_uri = NULL; rasqal_query_results* results = NULL; query_results_base_uri_string = raptor_uri_filename_to_uri_string(result_filename); query_results_base_uri = raptor_new_uri(raptor_world_ptr, query_results_base_uri_string); vars_table = rasqal_new_variables_table(world); results = rasqal_new_query_results(world, NULL, results_type, vars_table); rasqal_free_variables_table(vars_table); vars_table = NULL; if(!results) { fprintf(stderr, "%s: Failed to create query results\n", program); goto tidy_fail; } if(result_format_name) { /* FIXME validate result format name is legal query * results formatter name */ format_name = result_format_name; } if(!format_name) format_name = rasqal_world_guess_query_results_format_name(world, NULL /* uri */, NULL /* mime_type */, NULL /*buffer */, 0, (const unsigned char*)result_filename); qrf = rasqal_new_query_results_formatter(world, format_name, NULL /* mime type */, NULL /* uri */); if(!qrf) goto tidy_fail; if(rasqal_query_results_formatter_read(world, result_iostr, qrf, results, query_results_base_uri)) { fprintf(stderr, "%s: Failed to read query results from %s with format %s", program, result_filename, format_name); goto tidy_fail; } rasqal_free_query_results_formatter(qrf); qrf = NULL; return results; tidy_fail: if(vars_table) rasqal_free_variables_table(vars_table); if(results) rasqal_free_query_results(results); if(query_results_base_uri) raptor_free_uri(query_results_base_uri); return NULL; }
/** * rasqal_service_execute: * @svc: rasqal service * * Execute a rasqal sparql protocol service * * Return value: query results or NULL on failure */ rasqal_query_results* rasqal_service_execute(rasqal_service* svc) { rasqal_query_results* results = NULL; unsigned char* result_string = NULL; size_t result_length; raptor_iostream* read_iostr = NULL; raptor_uri* read_base_uri = NULL; rasqal_variables_table* vars_table = NULL; rasqal_query_results_formatter* read_formatter = NULL; raptor_uri* retrieval_uri = NULL; raptor_stringbuffer* uri_sb = NULL; size_t len; unsigned char* str; raptor_world* raptor_world_ptr = rasqal_world_get_raptor(svc->world); if(!svc->www) { svc->www = raptor_new_www(raptor_world_ptr); if(!svc->www) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to create WWW"); goto error; } } svc->started = 0; svc->final_uri = NULL; svc->sb = raptor_new_stringbuffer(); svc->content_type = NULL; if(svc->format) raptor_www_set_http_accept(svc->www, svc->format); else raptor_www_set_http_accept(svc->www, DEFAULT_FORMAT); raptor_www_set_write_bytes_handler(svc->www, rasqal_service_write_bytes, svc); raptor_www_set_content_type_handler(svc->www, rasqal_service_content_type_handler, svc); /* Construct a URI to retrieve following SPARQL protocol HTTP * binding from concatenation of * * 1. service_uri * 2. '?' * 3. "query=" query_string * 4. "&default-graph-uri=" background graph URI if any * 5. "&named-graph-uri=" named graph URI for all named graphs * with URI-escaping of the values */ uri_sb = raptor_new_stringbuffer(); if(!uri_sb) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to create stringbuffer"); goto error; } str = raptor_uri_as_counted_string(svc->service_uri, &len); raptor_stringbuffer_append_counted_string(uri_sb, str, len, 1); raptor_stringbuffer_append_counted_string(uri_sb, (const unsigned char*)"?", 1, 1); if(svc->query_string) { raptor_stringbuffer_append_counted_string(uri_sb, (const unsigned char*)"query=", 6, 1); raptor_stringbuffer_append_uri_escaped_counted_string(uri_sb, svc->query_string, svc->query_string_len, 1); } if(svc->data_graphs) { rasqal_data_graph* dg; int i; int bg_graph_count; for(i = 0, bg_graph_count = 0; (dg = (rasqal_data_graph*)raptor_sequence_get_at(svc->data_graphs, i)); i++) { unsigned char* graph_str; size_t graph_len; raptor_uri* graph_uri; if(dg->flags & RASQAL_DATA_GRAPH_BACKGROUND) { if(bg_graph_count++) { if(bg_graph_count == 2) { /* Warn once, only when the second BG is seen */ rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_WARN, NULL, "Attempted to add use background graphs"); } /* always skip after first BG */ continue; } raptor_stringbuffer_append_counted_string(uri_sb, (const unsigned char*)"&default-graph-uri=", 19, 1); graph_uri = dg->uri; } else { raptor_stringbuffer_append_counted_string(uri_sb, (const unsigned char*)"&named-graph-uri=", 17, 1); graph_uri = dg->name_uri; } graph_str = raptor_uri_as_counted_string(graph_uri, &graph_len); raptor_stringbuffer_append_uri_escaped_counted_string(uri_sb, (const char*)graph_str, graph_len, 1); } } str = raptor_stringbuffer_as_string(uri_sb); retrieval_uri = raptor_new_uri(raptor_world_ptr, str); if(!retrieval_uri) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to create retrieval URI %s", raptor_uri_as_string(retrieval_uri)); goto error; } raptor_free_stringbuffer(uri_sb); uri_sb = NULL; if(raptor_www_fetch(svc->www, retrieval_uri)) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to fetch retrieval URI %s", raptor_uri_as_string(retrieval_uri)); goto error; } vars_table = rasqal_new_variables_table(svc->world); if(!vars_table) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to create variables table"); goto error; } results = rasqal_new_query_results(svc->world, NULL, RASQAL_QUERY_RESULTS_BINDINGS, vars_table); /* (results takes a reference/copy to vars_table) */ rasqal_free_variables_table(vars_table); vars_table = NULL; if(!results) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to create query results"); goto error; } result_length = raptor_stringbuffer_length(svc->sb); result_string = raptor_stringbuffer_as_string(svc->sb); read_iostr = raptor_new_iostream_from_string(raptor_world_ptr, result_string, result_length); if(!read_iostr) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to create iostream from string"); rasqal_free_query_results(results); results = NULL; goto error; } read_base_uri = svc->final_uri ? svc->final_uri : svc->service_uri; read_formatter = rasqal_new_query_results_formatter(svc->world, /* format name */ NULL, svc->content_type, /* format URI */ NULL); if(!read_formatter) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to create query formatter for type %s", svc->content_type); rasqal_free_query_results(results); results = NULL; goto error; } if(rasqal_query_results_formatter_read(svc->world, read_iostr, read_formatter, results, read_base_uri)) { rasqal_log_error_simple(svc->world, RAPTOR_LOG_LEVEL_ERROR, NULL, "Failed to read from query formatter"); rasqal_free_query_results(results); results = NULL; goto error; } error: if(retrieval_uri) raptor_free_uri(retrieval_uri); if(uri_sb) raptor_free_stringbuffer(uri_sb); if(read_formatter) rasqal_free_query_results_formatter(read_formatter); if(read_iostr) raptor_free_iostream(read_iostr); if(vars_table) rasqal_free_variables_table(vars_table); if(svc->final_uri) { raptor_free_uri(svc->final_uri); svc->final_uri = NULL; } if(svc->content_type) { RASQAL_FREE(cstring, svc->content_type); svc->content_type = NULL; } if(svc->sb) { raptor_free_stringbuffer(svc->sb); svc->sb = NULL; } return results; }
int main(int argc, char *argv[]) { const char *program = rasqal_basename(argv[0]); rasqal_world* world = NULL; rasqal_variables_table* vt = NULL; #define NUM_VARS 3 const char* var_names[NUM_VARS] = {"normal-null", "normal-value", "anon"}; unsigned char* names[NUM_VARS]; rasqal_variable* vars[NUM_VARS]; rasqal_literal *value = NULL; int i; int rc = 0; world = rasqal_new_world(); if(!world || rasqal_world_open(world)) { fprintf(stderr, "%s: rasqal_world init failed\n", program); rc = 1; goto tidy; } vt = rasqal_new_variables_table(world); if(!vt) { fprintf(stderr, "%s: Failed to make variables table\n", program); rc = 1; goto tidy; } for(i = 0; i < NUM_VARS; i++) { size_t len = strlen(var_names[i]); names[i] = (unsigned char*)malloc(len+1); memcpy(names[i], var_names[i], len + 1); } vars[0] = rasqal_variables_table_add(vt, RASQAL_VARIABLE_TYPE_NORMAL, names[0], NULL); if(!vars[0]) { fprintf(stderr, "%s: Failed to make normal variable with NULL value\n", program); rc = 1; goto tidy; } else { /* now owned by vars[0] owned by vt */ names[0] = NULL; } /* vars[0] now owned by vt */ value = rasqal_new_double_literal(world, 42.0); if(!value) { fprintf(stderr, "%s: Failed to make double literal\n", program); rc = 1; goto tidy; } vars[1] = rasqal_variables_table_add(vt, RASQAL_VARIABLE_TYPE_NORMAL, names[1], value); if(!vars[1]) { fprintf(stderr, "%s: Failed to make normal variable with literal value\n", program); rc = 1; goto tidy; } else { /* now owned by vars[1] owned by vt */ names[1] = NULL; value = NULL; } /* vars[1] now owned by vt */ vars[2] = rasqal_variables_table_add(vt, RASQAL_VARIABLE_TYPE_ANONYMOUS, names[2], NULL); if(!vars[2]) { fprintf(stderr, "%s: Failed to make anonymous variable with NULL value\n", program); rc = 1; goto tidy; } else { /* now owned by vars[2] owned by vt */ names[2] = NULL; } /* vars[2] now owned by vt */ tidy: for(i = 0; i < NUM_VARS; i++) { if(names[i]) free(names[i]); } if(value) rasqal_free_literal(value); if(vt) rasqal_free_variables_table(vt); if(world) rasqal_free_world(world); return 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; }