summaryrefslogtreecommitdiffstats
path: root/src/query.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2009-02-14 00:48:45 +0000
committerDavid Robillard <d@drobilla.net>2009-02-14 00:48:45 +0000
commit6f8391945483823638c031d9f31c3f412e6276d2 (patch)
tree9589f437d79e204d49f0045056809f254656f7d0 /src/query.c
parentd9b5f38ffb9c0d0a6841368e7d52a71b9819d087 (diff)
downloadlilv-6f8391945483823638c031d9f31c3f412e6276d2.tar.gz
lilv-6f8391945483823638c031d9f31c3f412e6276d2.tar.bz2
lilv-6f8391945483823638c031d9f31c3f412e6276d2.zip
New generic query API.
List presets in lv2_inspect. git-svn-id: http://svn.drobilla.net/lad/trunk/slv2@1932 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'src/query.c')
-rw-r--r--src/query.c102
1 files changed, 77 insertions, 25 deletions
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;