summaryrefslogtreecommitdiffstats
path: root/src/query.c
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2006-06-06 20:20:33 +0000
committerDavid Robillard <d@drobilla.net>2006-06-06 20:20:33 +0000
commit7fd4168fe8581e46f4ee35cc182db6220b6eed04 (patch)
tree403d603debb6304193f60c2adda8a53863c45e04 /src/query.c
parente9a163310bc7b0a607d89ed5cb70c6bba99e919d (diff)
downloadlilv-7fd4168fe8581e46f4ee35cc182db6220b6eed04.tar.gz
lilv-7fd4168fe8581e46f4ee35cc182db6220b6eed04.tar.bz2
lilv-7fd4168fe8581e46f4ee35cc182db6220b6eed04.zip
Moved libslv2 into it's own subdirectory
git-svn-id: http://svn.drobilla.net/lad/libslv2@4 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'src/query.c')
-rw-r--r--src/query.c123
1 files changed, 123 insertions, 0 deletions
diff --git a/src/query.c b/src/query.c
new file mode 100644
index 0000000..2030735
--- /dev/null
+++ b/src/query.c
@@ -0,0 +1,123 @@
+/* LibSLV2
+ * Copyright (C) 2006 Dave Robillard <drobilla@connect.carleton.ca>
+ *
+ * This library is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by the Free
+ * Software Foundation; either version 2 of the License, or (at your option)
+ * any later version.
+ *
+ * This library is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include <util.h>
+#include <stdlib.h>
+#include <assert.h>
+#include <slv2/plugin.h>
+#include <slv2/query.h>
+
+
+unsigned char*
+slv2_query_header(const SLV2Plugin* p)
+{
+ const unsigned char* plugin_uri = slv2_plugin_get_uri(p);
+ const unsigned char* data_file_url = slv2_plugin_get_data_url(p);
+
+ unsigned char* query_string = ustrjoin(U(
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n"
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> \n"
+ "PREFIX doap: <http://usefulinc.com/ns/doap#> \n"
+ "PREFIX lv2: <http://lv2plug.in/ontology#> \n"
+ "PREFIX plugin: <"), plugin_uri, U("> \n"),
+ U("PREFIX data: <"), data_file_url, U("> \n\n"), 0);
+
+ return query_string;
+}
+
+
+unsigned char*
+slv2_query_lang_filter(const uchar* variable)
+{
+ uchar* result = NULL;
+ uchar* const lang = (uchar*)getenv("LANG");
+ if (lang) {
+ // FILTER( LANG(?value) = "en" || LANG(?value) = "" )
+ result = ustrjoin(
+ //U("FILTER (lang(?value) = \""), lang, U("\")\n"), 0);
+ U("FILTER( lang(?value) = \""), lang,
+ U("\" || lang(?value) = \"\" )\n"), 0);
+ }
+
+ return result;
+}
+
+
+rasqal_query_results*
+slv2_plugin_run_query(const SLV2Plugin* p,
+ const uchar* first, ...)
+{
+ va_list args_list;
+ va_start(args_list, first);
+ va_list args_copy;
+ va_copy(args_copy, args_list);
+
+ /* FIXME: Too much unecessary allocation */
+ uchar* header = slv2_query_header(p);
+ uchar* args_str = vstrjoin(first, args_copy);
+ uchar* query_str = ustrjoin(header, args_str, 0);
+
+ assert(p);
+ assert(query_str);
+
+ rasqal_query *rq = rasqal_new_query("sparql", NULL);
+
+ //printf("Query: \n%s\n\n", query_str);
+
+ rasqal_query_prepare(rq, query_str, NULL);
+ rasqal_query_results* results = rasqal_query_execute(rq);
+
+ rasqal_free_query(rq);
+
+ free(query_str);
+ free(args_str);
+ free(header);
+
+ return results;
+}
+
+
+SLV2Property
+slv2_query_get_results(rasqal_query_results* results)
+{
+ struct _Property* result = NULL;
+
+ if (rasqal_query_results_get_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, U("value"));
+ assert(literal != NULL);
+
+ // Add value on to the array. Yes, this is disgusting.
+ result->num_values++;
+ // FIXME LEAK:
+ result->values = realloc(result->values, result->num_values * sizeof(char*));
+ result->values[result->num_values-1] = ustrdup(rasqal_literal_as_string(literal));
+
+ rasqal_query_results_next(results);
+ }
+
+ return result;
+}
+