diff options
Diffstat (limited to 'src/query.c')
-rw-r--r-- | src/query.c | 127 |
1 files changed, 93 insertions, 34 deletions
diff --git a/src/query.c b/src/query.c index 56903ac..177d06e 100644 --- a/src/query.c +++ b/src/query.c @@ -58,9 +58,55 @@ slv2_query_lang_filter(const char* variable) } -rasqal_query_results* -slv2_plugin_run_query(const SLV2Plugin* p, - const char* query) +SLV2Property +slv2_query_get_variable_bindings(rasqal_query_results* results, const char* var_name) +{ + struct _Property* result = NULL; + + if (rasqal_query_results_get_bindings_count(results) > 0) { + result = malloc(sizeof(struct _Property)); + result->num_values = 0; + result->values = NULL; + } + + while (!rasqal_query_results_finished(results)) { + + rasqal_literal* literal = + rasqal_query_results_get_binding_value_by_name(results, (const unsigned char*)var_name); + assert(literal != NULL); + + // Add value on to the array, reallocing all the way. + // Yes, this is disgusting. Roughly as disgusting as the rasqal query + // results API. coincidentally. + result->num_values++; + result->values = realloc(result->values, result->num_values * sizeof(char*)); + result->values[result->num_values-1] = strdup((const char*)rasqal_literal_as_string(literal)); + + rasqal_query_results_next(results); + } + + return result; +} + + +size_t +slv2_query_count_variable_bindings(rasqal_query_results* results) +{ + size_t count = 0; + + while (!rasqal_query_results_finished(results)) { + ++count; + rasqal_query_results_next(results); + } + + return count; +} + + +SLV2Property +slv2_query_get_results(const SLV2Plugin* p, + const char* query, + const char* var_name) { char* header = slv2_query_header(p); char* query_str = strjoin(header, query, NULL); @@ -68,21 +114,63 @@ slv2_plugin_run_query(const SLV2Plugin* p, assert(p); assert(query_str); + rasqal_init(); + rasqal_query *rq = rasqal_new_query("sparql", NULL); //printf("Query: \n%s\n\n", query_str); rasqal_query_prepare(rq, (unsigned char*)query_str, NULL); rasqal_query_results* results = rasqal_query_execute(rq); + assert(results); + SLV2Property ret = slv2_query_get_variable_bindings(results, var_name); + rasqal_free_query_results(results); rasqal_free_query(rq); + rasqal_finish(); + free(query_str); free(header); + + return ret; +} + + +size_t +slv2_query_count_results(const SLV2Plugin* p, + const char* query) +{ + char* header = slv2_query_header(p); + char* query_str = strjoin(header, query, NULL); + + assert(p); + assert(query_str); + + rasqal_init(); + + rasqal_query *rq = rasqal_new_query("sparql", NULL); + + //printf("Query: \n%s\n\n", query_str); + + rasqal_query_prepare(rq, (unsigned char*)query_str, NULL); + rasqal_query_results* results = rasqal_query_execute(rq); + assert(results); - return results; + size_t count = slv2_query_count_variable_bindings(results); + + rasqal_free_query_results(results); + rasqal_free_query(rq); + + rasqal_finish(); + + free(query_str); + free(header); + + return count; } +/* size_t slv2_query_get_num_results(rasqal_query_results* results, const char* var_name) { @@ -97,36 +185,7 @@ slv2_query_get_num_results(rasqal_query_results* results, const char* var_name) return result; } - -SLV2Property -slv2_query_get_results(rasqal_query_results* results, const char* var_name) -{ - struct _Property* result = NULL; - - if (rasqal_query_results_get_bindings_count(results) > 0) { - result = malloc(sizeof(struct _Property)); - result->num_values = 0; - result->values = NULL; - } - - while (!rasqal_query_results_finished(results)) { - - rasqal_literal* literal = - rasqal_query_results_get_binding_value_by_name(results, (const unsigned char*)var_name); - assert(literal != NULL); - - // Add value on to the array, reallocing all the way. - // Yes, this is disgusting. Roughly as disgusting as the rasqal query - // results API. coincidentally. - result->num_values++; - result->values = realloc(result->values, result->num_values * sizeof(char*)); - result->values[result->num_values-1] = strdup((const char*)rasqal_literal_as_string(literal)); - - rasqal_query_results_next(results); - } - - return result; -} +*/ void slv2_property_free(struct _Property* prop) |