static librdf_query_results* librdf_query_rasqal_execute(librdf_query* query, librdf_model* model) { librdf_query_rasqal_context *context=(librdf_query_rasqal_context*)query->context; librdf_query_results* results; if (context->model) librdf_free_model(context->model); /* model is always non-NULL */ context->model = model; librdf_model_add_reference(model); /* This assumes raptor's URI implementation is librdf_uri */ if(rasqal_query_prepare(context->rq, context->query_string, (raptor_uri*)context->uri)) return NULL; if(context->results) rasqal_free_query_results(context->results); context->results=rasqal_query_execute(context->rq); if(!context->results) return NULL; results = LIBRDF_MALLOC(librdf_query_results*, sizeof(*results)); if(!results) { rasqal_free_query_results(context->results); context->results=NULL; } else { results->query=query; } return results; }
ssStatus_t parseM3_sparql_bindings(GSList** template_query_, unsigned char * query_str, sib_data_structure* p) { GSList* template_query =NULL; librdf_world * world; rasqal_query* rq; rasqal_graph_pattern* gp; gchar* prefix_query_str; gchar* prefix_str; //printf("RECEIVED SPARQL IN\n"); //printf("q: %s\n",op->req->query_str); prefix_str= g_strdup_printf("PREFIX %s <%s> PREFIX %s <%s> PREFIX %s <%s> PREFIX %s <%s> ",rdf_c ,rdf_ex ,fn_c_2,fn_ex,rdfs_c,rdfs_ex,xsd_c , xsd_ex); prefix_query_str=g_strdup_printf("%s %s", prefix_str, query_str); rq = rasqal_new_query(p->sparql_preprocessing_world, "sparql",NULL); //printf ("pre parsing\n"); if(rasqal_query_prepare(rq, prefix_query_str, NULL)) { rasqal_free_query(rq); rq = NULL; *template_query_ =template_query; g_free( prefix_str); g_free( prefix_query_str); rasqal_free_world(world); return ss_GeneralError; } //printf ("query prepared\n"); gp = rasqal_query_get_query_graph_pattern(rq); navigator_graph_pattern_load(gp, -1, 0, &template_query); *template_query_ =template_query; g_free( prefix_str); g_free( prefix_query_str); rasqal_free_query(rq); return ss_StatusOK; }
static rasqal_query* check_query_init_query(rasqal_world *world, const char* ql_name, const unsigned char* query_string, raptor_uri* base_uri, raptor_sequence* data_graphs) { rasqal_query* rq; rq = rasqal_new_query(world, (const char*)ql_name, NULL); if(!rq) { fprintf(stderr, "%s: Failed to create query in language %s\n", program, ql_name); goto tidy_query; } if(rasqal_query_prepare(rq, (const unsigned char*)query_string, base_uri)) { fprintf(stderr, "%s: Parsing query failed\n", program); rasqal_free_query(rq); rq = NULL; goto tidy_query; } if(data_graphs) { rasqal_data_graph* dg; while((dg = (rasqal_data_graph*)raptor_sequence_pop(data_graphs))) { if(rasqal_query_add_data_graph(rq, dg)) { fprintf(stderr, "%s: Failed to add data graph to query\n", program); goto tidy_query; } } } tidy_query: return rq; }
int fs_update(fs_query_state *qs, char *update, char **message, int unsafe) { rasqal_query *rq = rasqal_new_query(qs->rasqal_world, "sparql11-update", NULL); if (!rq) { *message = g_strdup_printf("Unable to initialise update parser"); return 1; } struct update_context uctxt; rasqal_world_set_log_handler(qs->rasqal_world, &uctxt, error_handler); memset(&uctxt, 0, sizeof(uctxt)); uctxt.link = qs->link; uctxt.segments = fsp_link_segments(qs->link); uctxt.qs = qs; uctxt.rq = rq; raptor_uri *bu = raptor_new_uri(qs->raptor_world, (unsigned char *)"local:local"); rasqal_query_prepare(rq, (unsigned char *)update, bu); if (uctxt.error) { if (uctxt.messages) { *message = build_update_error_message(uctxt.messages); g_slist_free(uctxt.messages); } return 1; } if (!quad_buffer) { quad_buffer = calloc(uctxt.segments, sizeof(struct quad_buf)); } int ok = 1; for (int i=0; 1; i++) { rasqal_update_operation *op = rasqal_query_get_update_operation(rq, i); if (!op) { break; } uctxt.op = op; uctxt.opid = i; if (update_op(&uctxt)) { ok = 0; break; } } fsp_res_import_commit_all(qs->link); fsp_quad_import_commit_all(qs->link, FS_BIND_BY_SUBJECT); rasqal_free_query(rq); if (uctxt.messages) { *message = build_update_error_message(uctxt.messages); g_slist_free(uctxt.messages); } for (GSList *it=uctxt.freeable; it; it=it->next) { g_free(it->data); } g_slist_free(uctxt.freeable); raptor_free_uri(bu); if (ok) { return 0; } else { return 1; } }
static rasqal_query* roqet_init_query(rasqal_world *world, const char* ql_name, const char* ql_uri, const unsigned char* query_string, raptor_uri* base_uri, rasqal_feature query_feature, int query_feature_value, const unsigned char* query_feature_string_value, int store_results, raptor_sequence* data_graphs) { rasqal_query* rq; rq = rasqal_new_query(world, (const char*)ql_name, (const unsigned char*)ql_uri); if(!rq) { fprintf(stderr, "%s: Failed to create query name %s\n", program, ql_name); goto tidy_query; } if(query_feature_value >= 0) rasqal_query_set_feature(rq, query_feature, query_feature_value); if(query_feature_string_value) rasqal_query_set_feature_string(rq, query_feature, query_feature_string_value); #ifdef STORE_RESULTS_FLAG if(store_results >= 0) rasqal_query_set_store_results(rq, store_results); #endif if(data_graphs) { rasqal_data_graph* dg; while((dg = (rasqal_data_graph*)raptor_sequence_pop(data_graphs))) { if(rasqal_query_add_data_graph(rq, dg)) { fprintf(stderr, "%s: Failed to add data graph to query\n", program); goto tidy_query; } } } if(rasqal_query_prepare(rq, query_string, base_uri)) { size_t len = strlen((const char*)query_string); fprintf(stderr, "%s: Parsing query '", program); if(len > MAX_QUERY_ERROR_REPORT_LEN) { (void)fwrite(query_string, MAX_QUERY_ERROR_REPORT_LEN, sizeof(char), stderr); fprintf(stderr, "...' (%d bytes) failed\n", (int)len); } else { (void)fwrite(query_string, len, sizeof(char), stderr); fputs("' failed\n", stderr); } rasqal_free_query(rq); rq = NULL; goto tidy_query; } tidy_query: return rq; }
int main(int argc, char **argv) { const char *program=rasqal_basename(argv[0]); const char *query_language_name=QUERY_LANGUAGE; raptor_uri *base_uri; unsigned char *uri_string; int test_i; limit_test* test; int tests_failed_count=0; int single_shot= -1; int query_i; rasqal_world *world; world=rasqal_new_world(); if(!world || rasqal_world_open(world)) { fprintf(stderr, "%s: rasqal_world init failed\n", program); return(1); } if(argc < 2 || argc > 3) { fprintf(stderr, "USAGE: %s data-filename [test number]\n", program); return(1); } if(argv[2]) single_shot=atoi(argv[2]); uri_string=raptor_uri_filename_to_uri_string(""); base_uri = raptor_new_uri(world->raptor_world_ptr, uri_string); raptor_free_memory(uri_string); for(query_i=0; query_i < NQUERIES; query_i++) { const char *query_format = limit_queries[query_i]; int dynamic_limits = (strstr(query_format, "%s %s") != NULL); for(test_i=(single_shot >=0 ? single_shot : 0); (test=&limit_offset_tests[test_i]) && (test->expected_count >=0); test_i++) { int result_i; rasqal_query *query = NULL; rasqal_query_results *results = NULL; const char* expected_results; int test_ok=1; unsigned char *data_string; unsigned char *query_string; size_t qs_len; #define LIM_OFF_BUF_SIZE 20 data_string=raptor_uri_filename_to_uri_string(argv[1]); qs_len = strlen((const char*)data_string) + strlen(query_format) + (2 * LIM_OFF_BUF_SIZE); query_string = RASQAL_MALLOC(unsigned char*, qs_len + 1); if(dynamic_limits) { char lim[LIM_OFF_BUF_SIZE]; char off[LIM_OFF_BUF_SIZE]; if(test->limit >= 0) snprintf(lim, LIM_OFF_BUF_SIZE, "LIMIT %d", test->limit); else *lim = '\0'; if(test->offset >= 0) snprintf(off, LIM_OFF_BUF_SIZE, "OFFSET %d", test->offset); else *off = '\0'; IGNORE_FORMAT_NONLITERAL_START snprintf((char*)query_string, qs_len, query_format, data_string, lim, off); IGNORE_FORMAT_NONLITERAL_END } else { IGNORE_FORMAT_NONLITERAL_START snprintf((char*)query_string, qs_len, query_format, data_string); IGNORE_FORMAT_NONLITERAL_END } raptor_free_memory(data_string); query = rasqal_new_query(world, query_language_name, NULL); if(!query) { fprintf(stderr, "%s: creating query in language %s FAILED\n", program, query_language_name); return(1); } #if defined(RASQAL_DEBUG) && RASQAL_DEBUG > 1 fprintf(stderr, "%s: preparing query %d test %d - %s\n", program, query_i, test_i, query_string); #endif if(rasqal_query_prepare(query, query_string, base_uri)) { fprintf(stderr, "%s: query %d test %d prepare '%s' FAILED\n", program, query_i, test_i, query_string); return(1); } if(!dynamic_limits) { rasqal_query_set_limit(query, test->limit); rasqal_query_set_offset(query, test->offset); } #if defined(RASQAL_DEBUG) && RASQAL_DEBUG > 1 fprintf(stderr, "%s: executing query %d test %d\n", program, query_i, test_i); #endif results=rasqal_query_execute(query); if(!results) { fprintf(stderr, "%s: query %d test %d FAILED\n", program, query_i, test_i); return(1); } /* check results */ expected_results=test->expected_results[query_i]; result_i=0; tests_failed_count=0; while(results && !rasqal_query_results_finished(results)) { rasqal_literal *value; const char* str; char expected_str[2]={0,0}; int failed=1; if(result_i < test->expected_count) expected_str[0]=expected_results[result_i]; value=rasqal_query_results_get_binding_value(results, 0); if(value) { int error=0; str=(const char*)rasqal_literal_as_string_flags(value, 0, &error); if(!error && str) { size_t len=strlen(str); if(len == 1 && str[0] == expected_str[0]) failed=0; } } if(failed) { fprintf(stderr, "%s: query %d test %d result %d FAILED returning value '%s' expected value '%s'\n", program, query_i, test_i, result_i, (str ? str: "NULL"), expected_str); test_ok=0; } rasqal_query_results_next(results); result_i++; } if(results) rasqal_free_query_results(results); if(!test_ok) { fprintf(stderr, "%s: query %d test %d limit %d offset %d FAILED\n", program, query_i, test_i, test->limit, test->offset); } else { if(result_i != test->expected_count) { fprintf(stderr, "%s: query %d test %d limit %d offset %d FAILED returned %d results, expected %d\n", program, query_i, test_i, test->limit, test->offset, result_i, test->expected_count); test_ok=0; } } if(!test_ok) { tests_failed_count++; } else { #if defined(RASQAL_DEBUG) && RASQAL_DEBUG > 1 fprintf(stderr, "%s: query %d test %d OK\n", program, query_i, test_i); #endif } rasqal_free_query(query); RASQAL_FREE(char*, query_string); if(single_shot >=0) break; } /* end test loop */ } /* end query loop */
int main(int argc, char **argv) { const char *program=rasqal_basename(argv[0]); rasqal_query *query = NULL; rasqal_query_results *results = NULL; raptor_uri *base_uri; unsigned char *data_string; unsigned char *uri_string; const char *query_language_name=QUERY_LANGUAGE; const char *query_format=QUERY_FORMAT; unsigned char *query_string; int count; rasqal_world *world; const char *data_file; world=rasqal_new_world(); if(!world || rasqal_world_open(world)) { fprintf(stderr, "%s: rasqal_world init failed\n", program); return(1); } if((data_file = getenv("RDF_DATA_FILE"))) { /* got data from environment */ } else { if(argc != 2) { fprintf(stderr, "USAGE: %s data-filename\n", program); return(1); } data_file = argv[1]; } data_string = raptor_uri_filename_to_uri_string(data_file); query_string=(unsigned char*)RASQAL_MALLOC(cstring, strlen((const char*)data_string)+strlen(query_format)+1); sprintf((char*)query_string, query_format, data_string); raptor_free_memory(data_string); uri_string=raptor_uri_filename_to_uri_string(""); base_uri = raptor_new_uri(world->raptor_world_ptr, uri_string); raptor_free_memory(uri_string); query=rasqal_new_query(world, query_language_name, NULL); if(!query) { fprintf(stderr, "%s: creating query in language %s FAILED\n", program, query_language_name); return(1); } printf("%s: preparing %s query\n", program, query_language_name); if(rasqal_query_prepare(query, query_string, base_uri)) { fprintf(stderr, "%s: %s query prepare FAILED\n", program, query_language_name); return(1); } RASQAL_FREE(cstring, query_string); printf("%s: executing query #1\n", program); results=rasqal_query_execute(query); if(!results) { fprintf(stderr, "%s: query execution 1 FAILED\n", program); return(1); } count=0; while(results && !rasqal_query_results_finished(results)) { int i; for(i=0; i<rasqal_query_results_get_bindings_count(results); i++) { const unsigned char *name=rasqal_query_results_get_binding_name(results, i); rasqal_literal *value=rasqal_query_results_get_binding_value(results, i); printf("result %d: variable %s=", count+1, (char*)name); rasqal_literal_print(value, stdout); putchar('\n'); } rasqal_query_results_next(results); count++; } if(results) rasqal_free_query_results(results); if(count != EXPECTED_RESULTS_COUNT) { fprintf(stderr, "%s: query execution 1 returned %d results, expected %d\n", program, count, EXPECTED_RESULTS_COUNT); return(1); } printf("%s: executing query #2\n", program); results = rasqal_query_execute(query); if(!results) { fprintf(stderr, "%s: query execution 2 FAILED\n", program); return(1); } count=0; while(results && !rasqal_query_results_finished(results)) { int i; for(i=0; i<rasqal_query_results_get_bindings_count(results); i++) { const unsigned char *name=rasqal_query_results_get_binding_name(results, i); rasqal_literal *value=rasqal_query_results_get_binding_value(results, i); printf("result %d: variable %s=", count+1, (char*)name); rasqal_literal_print(value, stdout); putchar('\n'); } rasqal_query_results_next(results); count++; } if(results) rasqal_free_query_results(results); if(count != EXPECTED_RESULTS_COUNT) { fprintf(stderr, "%s: query execution 2 returned %d results, expected %d\n", program, count, EXPECTED_RESULTS_COUNT); return(1); } printf("%s: executing query #3\n", program); results = rasqal_query_execute(query); if(!results) { fprintf(stderr, "%s: query execution 3 FAILED\n", program); return(1); } rasqal_free_query_results(results); printf("%s: executing query #4\n", program); results = rasqal_query_execute(query); if(!results) { fprintf(stderr, "%s: query execution 4 FAILED\n", program); return(1); } rasqal_free_query_results(results); rasqal_free_query(query); raptor_free_uri(base_uri); rasqal_free_world(world); return 0; }