diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/plugin.c | 31 | ||||
-rw-r--r-- | src/port.c | 44 | ||||
-rw-r--r-- | src/query.c | 102 | ||||
-rw-r--r-- | src/slv2_internal.h | 11 |
4 files changed, 122 insertions, 66 deletions
diff --git a/src/plugin.c b/src/plugin.c index ae2f8f3..b58a3ec 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -26,6 +26,7 @@ #include "slv2/collections.h" #include "slv2/plugin.h" #include "slv2/pluginclass.h" +#include "slv2/query.h" #include "slv2/types.h" #include "slv2/util.h" #include "slv2_internal.h" @@ -328,18 +329,18 @@ slv2_plugin_verify(SLV2Plugin plugin) "doap:license ?license ;\n" "lv2:port [ lv2:index ?port ] .\n}"; - librdf_query_results* results = slv2_plugin_query(plugin, query_str); + SLV2Results results = slv2_plugin_query_sparql(plugin, query_str); bool has_type = false; bool has_name = false; bool has_license = false; bool has_port = false; - while (!librdf_query_results_finished(results)) { - librdf_node* type_node = librdf_query_results_get_binding_value(results, 0); - librdf_node* name_node = librdf_query_results_get_binding_value(results, 1); - librdf_node* license_node = librdf_query_results_get_binding_value(results, 2); - librdf_node* port_node = librdf_query_results_get_binding_value(results, 3); + while (!librdf_query_results_finished(results->rdf_results)) { + librdf_node* type_node = librdf_query_results_get_binding_value(results->rdf_results, 0); + librdf_node* name_node = librdf_query_results_get_binding_value(results->rdf_results, 1); + librdf_node* license_node = librdf_query_results_get_binding_value(results->rdf_results, 2); + librdf_node* port_node = librdf_query_results_get_binding_value(results->rdf_results, 3); if (librdf_node_get_type(type_node) == LIBRDF_NODE_TYPE_RESOURCE) has_type = true; @@ -358,10 +359,10 @@ slv2_plugin_verify(SLV2Plugin plugin) librdf_free_node(license_node); librdf_free_node(port_node); - librdf_query_results_next(results); + librdf_query_results_next(results->rdf_results); } - librdf_free_query_results(results); + slv2_results_free(results); if ( ! (has_type && has_name && has_license && has_port) ) { fprintf(stderr, "Invalid LV2 Plugin %s\n", @@ -828,14 +829,14 @@ slv2_plugin_get_uis(SLV2Plugin plugin) " uiext:binary ?binary .\n" "}\n"; - librdf_query_results* results = slv2_plugin_query(plugin, query_str); + SLV2Results results = slv2_plugin_query_sparql(plugin, query_str); SLV2UIs result = slv2_uis_new(); - while (!librdf_query_results_finished(results)) { - librdf_node* uri_node = librdf_query_results_get_binding_value(results, 0); - librdf_node* type_node = librdf_query_results_get_binding_value(results, 1); - librdf_node* binary_node = librdf_query_results_get_binding_value(results, 2); + while (!librdf_query_results_finished(results->rdf_results)) { + librdf_node* uri_node = librdf_query_results_get_binding_value(results->rdf_results, 0); + librdf_node* type_node = librdf_query_results_get_binding_value(results->rdf_results, 1); + librdf_node* binary_node = librdf_query_results_get_binding_value(results->rdf_results, 2); SLV2UI ui = slv2_ui_new(plugin->world, librdf_node_get_uri(uri_node), @@ -848,10 +849,10 @@ slv2_plugin_get_uis(SLV2Plugin plugin) librdf_free_node(type_node); librdf_free_node(binary_node); - librdf_query_results_next(results); + librdf_query_results_next(results->rdf_results); } - librdf_free_query_results(results); + slv2_results_free(results); if (slv2_uis_size(result) > 0) { return result; @@ -22,10 +22,11 @@ #include <stdio.h> #include <string.h> #include <limits.h> +#include "slv2/collections.h" #include "slv2/port.h" +#include "slv2/query.h" #include "slv2/types.h" #include "slv2/util.h" -#include "slv2/collections.h" #include "slv2_internal.h" @@ -113,13 +114,13 @@ slv2_port_supports_event(SLV2Plugin p, " lv2ev:supportsEvent <", slv2_value_as_uri(event), "> .\n" "}", NULL); - librdf_query_results* results = slv2_plugin_query(p, query); - assert(librdf_query_results_is_boolean(results)); + SLV2Results results = slv2_plugin_query_sparql(p, query); + assert(librdf_query_results_is_boolean(results->rdf_results)); - const bool ret = librdf_query_results_get_boolean(results); + const bool ret = librdf_query_results_get_boolean(results->rdf_results); free(query); - librdf_free_query_results(results); + slv2_results_free(results); return ret; } @@ -268,12 +269,12 @@ slv2_port_get_range(SLV2Plugin p, "OPTIONAL { ?port lv2:maximum ?max }\n", "\n}", NULL); - librdf_query_results* results = slv2_plugin_query(p, query); + SLV2Results results = slv2_plugin_query_sparql(p, query); - while (!librdf_query_results_finished(results)) { - librdf_node* def_node = librdf_query_results_get_binding_value(results, 0); - librdf_node* min_node = librdf_query_results_get_binding_value(results, 1); - librdf_node* max_node = librdf_query_results_get_binding_value(results, 2); + while (!librdf_query_results_finished(results->rdf_results)) { + librdf_node* def_node = librdf_query_results_get_binding_value(results->rdf_results, 0); + librdf_node* min_node = librdf_query_results_get_binding_value(results->rdf_results, 1); + librdf_node* max_node = librdf_query_results_get_binding_value(results->rdf_results, 2); if (def && def_node && !*def) *def = slv2_value_new_librdf_node(p->world, def_node); @@ -285,10 +286,10 @@ slv2_port_get_range(SLV2Plugin p, if ((!def || *def) && (!min || *min) && (!max || *max)) break; - librdf_query_results_next(results); + librdf_query_results_next(results->rdf_results); } - - librdf_free_query_results(results); + + slv2_results_free(results); free(query); } @@ -307,27 +308,24 @@ slv2_port_get_scale_points(SLV2Plugin p, " rdfs:label ?label .\n" "\n} ORDER BY ?value", NULL); - librdf_query_results* results = slv2_plugin_query(p, query); + SLV2Results results = slv2_plugin_query_sparql(p, query); SLV2ScalePoints ret = NULL; - if (!librdf_query_results_finished(results)) + if (!slv2_results_finished(results)) ret = slv2_scale_points_new(); - while (!librdf_query_results_finished(results)) { - librdf_node* value_node = librdf_query_results_get_binding_value(results, 0); - librdf_node* label_node = librdf_query_results_get_binding_value(results, 1); - - SLV2Value value = slv2_value_new_librdf_node(p->world, value_node); - SLV2Value label = slv2_value_new_librdf_node(p->world, label_node); + while (!slv2_results_finished(results)) { + SLV2Value value = slv2_results_get_binding_value(results, 0); + SLV2Value label = slv2_results_get_binding_value(results, 1); if (value && label) raptor_sequence_push(ret, slv2_scale_point_new(value, label)); - librdf_query_results_next(results); + slv2_results_next(results); } - librdf_free_query_results(results); + slv2_results_free(results); free(query); diff --git a/src/query.c b/src/query.c index edd28b4..8990c9b 100644 --- a/src/query.c +++ b/src/query.c @@ -25,6 +25,7 @@ #include <string.h> #include "slv2/collections.h" #include "slv2/plugin.h" +#include "slv2/query.h" #include "slv2/util.h" #include "slv2_internal.h" @@ -81,53 +82,53 @@ slv2_value_from_librdf_node(SLV2World world, librdf_node* node) SLV2Values -slv2_query_get_variable_bindings(SLV2World world, - librdf_query_results* results, - int variable) +slv2_query_get_variable_bindings(SLV2World world, + SLV2Results results, + int variable) { SLV2Values result = NULL; - if (!librdf_query_results_finished(results)) + if (!librdf_query_results_finished(results->rdf_results)) result = slv2_values_new(); - while (!librdf_query_results_finished(results)) { - librdf_node* node = librdf_query_results_get_binding_value(results, variable); + while (!librdf_query_results_finished(results->rdf_results)) { + librdf_node* node = librdf_query_results_get_binding_value(results->rdf_results, variable); if (node == NULL) { fprintf(stderr, "SLV2 ERROR: Variable %d bound to NULL.\n", variable); - librdf_query_results_next(results); + librdf_query_results_next(results->rdf_results); continue; } - + SLV2Value val = slv2_value_from_librdf_node(world, node); if (val) raptor_sequence_push(result, val); librdf_free_node(node); - librdf_query_results_next(results); - } + librdf_query_results_next(results->rdf_results); + } return result; } -size_t -slv2_query_count_bindings(librdf_query_results* results) +unsigned +slv2_results_size(SLV2Results results) { size_t count = 0; - while (!librdf_query_results_finished(results)) { + while (!slv2_results_finished(results)) { ++count; - librdf_query_results_next(results); - } + slv2_results_next(results); + } return count; } - -librdf_query_results* -slv2_plugin_query(SLV2Plugin plugin, - const char* sparql_str) + +SLV2Results +slv2_plugin_query_sparql(SLV2Plugin plugin, + const char* sparql_str) { slv2_plugin_load_if_necessary(plugin); @@ -157,7 +158,58 @@ slv2_plugin_query(SLV2Plugin plugin, librdf_free_query(query); free(query_str); - return results; + SLV2Results ret = (SLV2Results)malloc(sizeof(struct _SLV2Results)); + ret->world = plugin->world; + ret->rdf_results = results; + + return ret; +} + + +void +slv2_results_free(SLV2Results results) +{ + librdf_free_query_results(results->rdf_results); + free(results); +} + + +bool +slv2_results_finished(SLV2Results results) +{ + return librdf_query_results_finished(results->rdf_results); +} + + +SLV2Value +slv2_results_get_binding_value(SLV2Results results, unsigned index) +{ + return slv2_value_from_librdf_node(results->world, + librdf_query_results_get_binding_value( + results->rdf_results, index)); +} + + +SLV2Value +slv2_results_get_binding_value_by_name(SLV2Results results, const char* name) +{ + return slv2_value_from_librdf_node(results->world, + librdf_query_results_get_binding_value_by_name( + results->rdf_results, name)); +} + + +const char* +slv2_results_get_binding_name(SLV2Results results, unsigned index) +{ + return librdf_query_results_get_binding_name(results->rdf_results, index); +} + + +void +slv2_results_next(SLV2Results results) +{ + librdf_query_results_next(results->rdf_results); } @@ -169,12 +221,12 @@ slv2_plugin_query_variable(SLV2Plugin plugin, { assert(variable < INT_MAX); - librdf_query_results* results = slv2_plugin_query(plugin, sparql_str); + SLV2Results results = slv2_plugin_query_sparql(plugin, sparql_str); SLV2Values ret = slv2_query_get_variable_bindings(plugin->world, results, (int)variable); - librdf_free_query_results(results); + slv2_results_free(results); return ret; } @@ -192,13 +244,13 @@ unsigned slv2_plugin_query_count(SLV2Plugin plugin, const char* sparql_str) { - librdf_query_results* results = slv2_plugin_query(plugin, sparql_str); + SLV2Results results = slv2_plugin_query_sparql(plugin, sparql_str); unsigned ret = 0; if (results) { - ret = slv2_query_count_bindings(results); - librdf_free_query_results(results); + ret = slv2_results_size(results); + slv2_results_free(results); } return ret; diff --git a/src/slv2_internal.h b/src/slv2_internal.h index bc1defe..ecb7255 100644 --- a/src/slv2_internal.h +++ b/src/slv2_internal.h @@ -74,9 +74,6 @@ void slv2_plugin_get_port_float_values(SLV2Plugin p, const char* qname, float* values); -librdf_query_results* slv2_plugin_query(SLV2Plugin plugin, - const char* sparql_str); - /* ********* Plugins ********* */ @@ -229,6 +226,14 @@ SLV2ScalePoint slv2_scale_point_new(SLV2Value value, SLV2Value label); void slv2_scale_point_free(SLV2ScalePoint point); +/* ********* Query Results********* */ + +struct _SLV2Results { + SLV2World world; + librdf_query_results* rdf_results; +}; + + /* ********* Utilities ********* */ char* slv2_strjoin(const char* first, ...); |