Changeset 2875 for trunk/slv2/src/query.c
- Timestamp:
- 2011-01-30 13:40:26 (2 years ago)
- File:
-
- 1 edited
-
trunk/slv2/src/query.c (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
trunk/slv2/src/query.c
r2866 r2875 26 26 #include "slv2/collections.h" 27 27 #include "slv2/plugin.h" 28 #include "slv2/query.h"29 28 #include "slv2/util.h" 30 29 #include "slv2_internal.h" 31 32 33 static const char* slv2_query_prefixes =34 "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"35 "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"36 "PREFIX doap: <http://usefulinc.com/ns/doap#>\n"37 "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n"38 "PREFIX lv2: <http://lv2plug.in/ns/lv2core#>\n"39 "PREFIX lv2ev: <http://lv2plug.in/ns/ext/event#>\n";40 41 42 SLV2Values43 slv2_query_get_variable_bindings(SLV2World world,44 SLV2Results results,45 int variable)46 {47 SLV2Values result = NULL;48 49 if (!librdf_query_results_finished(results->rdf_results))50 result = slv2_values_new();51 52 while (!librdf_query_results_finished(results->rdf_results)) {53 librdf_node* node = librdf_query_results_get_binding_value(results->rdf_results, variable);54 55 if (node == NULL) {56 SLV2_ERRORF("Variable %d bound to NULL.\n", variable);57 librdf_query_results_next(results->rdf_results);58 continue;59 }60 61 SLV2Value val = slv2_value_new_librdf_node(world, node);62 if (val)63 raptor_sequence_push(result, val);64 65 librdf_free_node(node);66 librdf_query_results_next(results->rdf_results);67 }68 69 return result;70 }71 72 73 unsigned74 slv2_results_size(SLV2Results results)75 {76 size_t count = 0;77 78 while (!slv2_results_finished(results)) {79 ++count;80 slv2_results_next(results);81 }82 83 return count;84 }85 86 87 SLV2Results88 slv2_plugin_query_sparql(SLV2Plugin plugin,89 const char* sparql_str)90 {91 slv2_plugin_load_if_necessary(plugin);92 93 librdf_uri* base_uri = slv2_value_as_librdf_uri(plugin->plugin_uri);94 95 char* query_str = slv2_strjoin(slv2_query_prefixes, sparql_str, NULL);96 97 librdf_query* query = librdf_new_query(plugin->world->world, "sparql", NULL,98 (const uint8_t*)query_str, base_uri);99 100 if (!query) {101 SLV2_ERRORF("Failed to create query:\n%s", query_str);102 return NULL;103 }104 105 librdf_query_results* results = librdf_query_execute(query, plugin->rdf);106 107 librdf_free_query(query);108 free(query_str);109 110 SLV2Results ret = (SLV2Results)malloc(sizeof(struct _SLV2Results));111 ret->world = plugin->world;112 ret->rdf_results = results;113 114 return ret;115 }116 117 118 void119 slv2_results_free(SLV2Results results)120 {121 librdf_free_query_results(results->rdf_results);122 free(results);123 }124 125 126 bool127 slv2_results_finished(SLV2Results results)128 {129 return librdf_query_results_finished(results->rdf_results);130 }131 132 133 SLV2Value134 slv2_results_get_binding_value(SLV2Results results, unsigned index)135 {136 return slv2_value_new_librdf_node(results->world,137 librdf_query_results_get_binding_value(138 results->rdf_results, index));139 }140 141 142 SLV2Value143 slv2_results_get_binding_value_by_name(SLV2Results results, const char* name)144 {145 return slv2_value_new_librdf_node(results->world,146 librdf_query_results_get_binding_value_by_name(147 results->rdf_results, name));148 }149 150 151 const char*152 slv2_results_get_binding_name(SLV2Results results, unsigned index)153 {154 return librdf_query_results_get_binding_name(results->rdf_results, index);155 }156 157 158 void159 slv2_results_next(SLV2Results results)160 {161 librdf_query_results_next(results->rdf_results);162 }163 164 165 /** Query a single variable */166 SLV2Values167 slv2_plugin_query_variable(SLV2Plugin plugin,168 const char* sparql_str,169 unsigned variable)170 {171 assert(variable < INT_MAX);172 173 SLV2Results results = slv2_plugin_query_sparql(plugin, sparql_str);174 175 SLV2Values ret = slv2_query_get_variable_bindings(plugin->world,176 results, (int)variable);177 178 slv2_results_free(results);179 180 return ret;181 }182 183 184 /** Run a query and count number of matches.185 *186 * More efficient than slv2_plugin_simple_query if you're only interested187 * in the number of results (ie slv2_plugin_num_ports).188 *189 * Note the result of this function is probably meaningless unless the query190 * is a SELECT DISTINCT.191 */192 unsigned193 slv2_plugin_query_count(SLV2Plugin plugin,194 const char* sparql_str)195 {196 SLV2Results results = slv2_plugin_query_sparql(plugin, sparql_str);197 198 unsigned ret = 0;199 200 if (results) {201 ret = slv2_results_size(results);202 slv2_results_free(results);203 }204 205 return ret;206 }207 30 208 31
Note: See TracChangeset
for help on using the changeset viewer.
