diff options
author | David Robillard <d@drobilla.net> | 2006-09-03 05:11:57 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2006-09-03 05:11:57 +0000 |
commit | 056f394151a48e9d129691fd7451a3d0bcbde3ba (patch) | |
tree | c7c95bccfc503e704003e33b9385aa4657d2a57a /src/query.c | |
parent | 6c8dc477bda2d6e67893f4475e603a11cf507017 (diff) | |
download | lilv-056f394151a48e9d129691fd7451a3d0bcbde3ba.tar.gz lilv-056f394151a48e9d129691fd7451a3d0bcbde3ba.tar.bz2 lilv-056f394151a48e9d129691fd7451a3d0bcbde3ba.zip |
Saner (internal) query API, though still needs work.
Preliminary (untested) support for host features/extensions/whatever.
Documentation fixes and minor cleanups.
git-svn-id: http://svn.drobilla.net/lad/libslv2@113 a436a847-0d15-0410-975c-d299462d15a1
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) |