SLV2Values slv2_query_get_variable_bindings(SLV2World world, SLV2Results results, int variable) { SLV2Values result = NULL; if (!librdf_query_results_finished(results->rdf_results)) result = slv2_values_new(); 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->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->rdf_results); } return result; }
RdfStorePrivate::ResultMap RdfStorePrivate::ExecuteTupleQuery(const std::string& query) const { RdfStorePrivate::ResultMap resultMap; const std::string completeQuery = this->PrependPrefixes(query); librdf_query* rdfQuery = librdf_new_query(m_World, "sparql", nullptr, (const unsigned char*) completeQuery.c_str(), nullptr); if (!rdfQuery) return resultMap; librdf_query_results* results = librdf_query_execute(rdfQuery, m_Model); if (!results) { librdf_free_query(rdfQuery); return resultMap; } if (!librdf_query_results_is_bindings(results)) { librdf_free_query_results(results); librdf_free_query(rdfQuery); return resultMap; } while (!librdf_query_results_finished(results)) { int count = librdf_query_results_get_bindings_count(results); for (int i = 0; i < count; ++i) { const char *name = librdf_query_results_get_binding_name(results, i); if (!name) continue; std::string key = name; std::list<RdfNode> list; if(!resultMap[key].empty()) { list = resultMap[key]; } librdf_node *node = librdf_query_results_get_binding_value(results, i); list.push_back(LibRdfNodeToRdfNode(node)); resultMap[key] = list; } librdf_query_results_next(results); } librdf_free_query_results(results); librdf_free_query(rdfQuery); return resultMap; }
/** * @brief Executes the given delete query * @attention DELETE-Statement not supported in Redland, so the Query has to be SELECT with < ?s ?p ?o > statements! * These statements are removed out of the storage. * * * @param query The query to execute. */ bool RDF_ExecDiscardQuery(unsigned char* query) { librdf_query* rdfquery; librdf_query_results* results; rdfquery = librdf_new_query(world, "sparql", NULL, query, NULL); results = librdf_model_query_execute(model, rdfquery); librdf_free_query(rdfquery); //Get all statements from the select query and remove them from storage while(!librdf_query_results_finished(results)) { const char **names = NULL; librdf_node* values[10]; if(librdf_query_results_get_bindings(results, &names, values)) { break; } librdf_node* subject_tmp = values[0]; librdf_node* predicate_tmp = values[1]; librdf_node* object_tmp = values[2]; librdf_statement* statement_tmp = librdf_new_statement_from_nodes(world, subject_tmp, predicate_tmp, object_tmp); librdf_storage_remove_statement(storage, statement_tmp); librdf_free_node(subject_tmp); librdf_free_node(predicate_tmp); librdf_free_node(object_tmp); librdf_free_statement(statement_tmp); int i = 0; for (i = 0; i < 10; i++) { librdf_free_node(values[i]); } librdf_query_results_next(results); free(names); } if (results == NULL) { printf("Failed to execute discard query!\n"); } librdf_free_query_results(results); return true; }
void slv2_port_get_range(SLV2Plugin p, SLV2Port port, SLV2Value* def, SLV2Value* min, SLV2Value* max) { if (def) *def = NULL; if (min) *min = NULL; if (max) *max = NULL; char* query = slv2_strjoin( "SELECT DISTINCT ?def ?min ?max WHERE {\n" "<", slv2_value_as_uri(p->plugin_uri), "> lv2:port ?port .\n" "?port lv2:symbol \"", slv2_value_as_string(port->symbol), "\".\n", "OPTIONAL { ?port lv2:default ?def }\n", "OPTIONAL { ?port lv2:minimum ?min }\n", "OPTIONAL { ?port lv2:maximum ?max }\n", "\n}", NULL); librdf_query_results* results = slv2_plugin_query(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); if (def && def_node && !*def) *def = slv2_value_new_librdf_node(p->world, def_node); if (min && min_node && !*min) *min = slv2_value_new_librdf_node(p->world, min_node); if (max && max_node && !*max) *max = slv2_value_new_librdf_node(p->world, max_node); if ((!def || *def) && (!min || *min) && (!max || *max)) break; librdf_query_results_next(results); } librdf_free_query_results(results); free(query); }
static int query_results(request_rec *r, librdf_world *w, librdf_model *m, char *query) { int ret = 0; librdf_query *q = NULL; librdf_query_results *qr = NULL; if ((q = librdf_new_query(w, "sparql", NULL, (unsigned char *)query, NULL)) != NULL) { if ((qr = librdf_query_execute(q, m)) != NULL) { for (; !librdf_query_results_finished(qr); librdf_query_results_next(qr)); ret = librdf_query_results_get_count(qr); ap_log_rerror(APLOG_MARK, APLOG_WARNING, 0, r, "[ACL] [query] %d results, sparql: %s", ret, query); librdf_free_query_results(qr); } else ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "librdf_query_execute returned NULL"); librdf_free_query(q); } else ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, "librdf_new_query returned NULL"); return ret; }
SLV2ScalePoints slv2_port_get_scale_points(SLV2Plugin p, SLV2Port port) { char* query = slv2_strjoin( "SELECT DISTINCT ?value ?label WHERE {\n" "<", slv2_value_as_uri(p->plugin_uri), "> lv2:port ?port .\n" "?port lv2:symbol \"", slv2_value_as_string(port->symbol), "\" ;\n", " lv2:scalePoint ?point .\n" "?point rdf:value ?value ;\n" " rdfs:label ?label .\n" "\n} ORDER BY ?value", NULL); librdf_query_results* results = slv2_plugin_query(p, query); SLV2ScalePoints ret = NULL; if (!librdf_query_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); raptor_sequence_push(ret, slv2_scale_point_new(value, label)); librdf_query_results_next(results); } librdf_free_query_results(results); free(query); assert(!ret || slv2_values_size(ret) > 0); return ret; }
void slv2_results_next(SLV2Results results) { librdf_query_results_next(results->rdf_results); }
UT_Error IE_Imp_OpenDocument::_handleRDFStreams () { #ifndef WITH_REDLAND return UT_OK; #else UT_Error error = UT_OK; UT_DEBUGMSG(("IE_Imp_OpenDocument::_handleRDFStreams()\n")); // // DEBUG. // { // PD_DocumentRDFHandle rdf = getDoc()->getDocumentRDF(); // PD_DocumentRDFMutationHandle m = rdf->createMutation(); // m->add( PD_URI("http://www.example.com/foo#bar" ), // PD_URI("http://www.example.com/foo#bar" ), // PD_Object("http://www.example.com/foo#bar" ) ); // m->commit(); // rdf->dumpModel("added foo"); // } RDFArguments args; librdf_model* model = args.model; // check if we can load a manifest.rdf file GsfInput* pRdfManifest = gsf_infile_child_by_name(m_pGsfInfile, "manifest.rdf"); if (pRdfManifest) { UT_DEBUGMSG(("IE_Imp_OpenDocument::_handleRDFStreams() have manifest.rdf\n")); error = _loadRDFFromFile( pRdfManifest, "manifest.rdf", &args ); g_object_unref (G_OBJECT (pRdfManifest)); if (error != UT_OK) return error; } // find other RDF/XML files referenced in the manifest const char* query_string = "" "prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> \n" "prefix odf: <http://docs.oasis-open.org/opendocument/meta/package/odf#> \n" "prefix odfcommon: <http://docs.oasis-open.org/opendocument/meta/package/common#> \n" "select ?subj ?fileName \n" " where { \n" " ?subj rdf:type odf:MetaDataFile . \n" " ?subj odfcommon:path ?fileName \n" " } \n"; librdf_uri* base_uri = 0; librdf_query* query = librdf_new_query( args.world, "sparql", 0, (unsigned char*)query_string, base_uri ); librdf_query_results* results = librdf_query_execute( query, model ); if( !results ) { // Query failed is a failure to execute the SPARQL, // in which case there might be results but we couldn't find them UT_DEBUGMSG(("IE_Imp_OpenDocument::_handleRDFStreams() SPARQL query to find auxillary RDF/XML files failed! q:%p\n", query )); error = UT_ERROR; } else { UT_DEBUGMSG(("IE_Imp_OpenDocument::_handleRDFStreams() aux RDF/XML file count:%d\n", librdf_query_results_get_count( results ))); // parse auxillary RDF/XML files too for( ; !librdf_query_results_finished( results ) ; librdf_query_results_next( results )) { librdf_node* fnNode = librdf_query_results_get_binding_value_by_name ( results, "fileName" ); UT_DEBUGMSG(("_handleRDFStreams() fnNode:%p\n", fnNode )); std::string fn = toString(fnNode); UT_DEBUGMSG(("_handleRDFStreams() loading auxilary RDF/XML file from:%s\n", fn.c_str())); GsfInput* pAuxRDF = gsf_infile_child_by_name(m_pGsfInfile, fn.c_str()); if (pAuxRDF) { error = _loadRDFFromFile( pAuxRDF, fn.c_str(), &args ); g_object_unref (G_OBJECT (pAuxRDF)); if( error != UT_OK ) break; } else { UT_ASSERT_HARMLESS(UT_SHOULD_NOT_HAPPEN); return UT_ERROR; } } librdf_free_query_results( results ); } librdf_free_query( query ); UT_DEBUGMSG(("_handleRDFStreams() error:%d model.sz:%d\n", error, librdf_model_size( model ))); if( error != UT_OK ) { return error; } // convert the redland model into native AbiWord RDF triples { PD_DocumentRDFHandle rdf = getDoc()->getDocumentRDF(); PD_DocumentRDFMutationHandle m = rdf->createMutation(); librdf_statement* statement = librdf_new_statement( args.world ); librdf_stream* stream = librdf_model_find_statements( model, statement ); while (!librdf_stream_end(stream)) { librdf_statement* current = librdf_stream_get_object( stream ); int objectType = PD_Object::OBJECT_TYPE_URI; std::string xsdType = ""; if( librdf_node_is_blank( librdf_statement_get_object( current ))) { objectType = PD_Object::OBJECT_TYPE_BNODE; } if( librdf_node_is_literal( librdf_statement_get_object( current ))) { objectType = PD_Object::OBJECT_TYPE_LITERAL; if( librdf_uri* u = librdf_node_get_literal_value_datatype_uri( librdf_statement_get_object( current ))) { xsdType = toString(u); } } if( DEBUG_RDF_IO ) { UT_DEBUGMSG(("_handleRDFStreams() adding s:%s p:%s o:%s rotv:%d otv:%d ots:%s\n", toString( librdf_statement_get_subject( current )).c_str(), toString( librdf_statement_get_predicate( current )).c_str(), toString( librdf_statement_get_object( current )).c_str(), librdf_node_get_type(librdf_statement_get_object( current )), objectType, xsdType.c_str() )); } m->add( PD_URI( toString( librdf_statement_get_subject( current ))), PD_URI( toString( librdf_statement_get_predicate( current ))), PD_Object( toString( librdf_statement_get_object( current )), objectType, xsdType )); // m->add( PD_URI( toString( librdf_statement_get_subject( current ))), // PD_URI( toString( librdf_statement_get_predicate( current ))), // PD_Object( toString( librdf_statement_get_object( current )), // objectType, // xsdType )); librdf_stream_next(stream); } librdf_free_stream( stream ); librdf_free_statement( statement ); // m->add( PD_URI("http://www.example.com/foo#bar" ), // PD_URI("http://www.example.com/foo#bar" ), // PD_Object("http://www.example.com/foo#bar" ) ); m->commit(); } if( DEBUG_RDF_IO ) { getDoc()->getDocumentRDF()->dumpModel("Loaded RDF from ODF file"); } return error; #endif }
static int validate_webid(request_rec *request, const char *subjAltName, char *pkey_n, unsigned int pkey_e_i) { int r = HTTP_UNAUTHORIZED; librdf_world *rdf_world = NULL; librdf_storage *rdf_storage = NULL; librdf_model *rdf_model = NULL; librdf_query *rdf_query = NULL; librdf_query_results *rdf_query_results = NULL; rdf_world = librdf_new_world(); if (rdf_world != NULL) { librdf_world_open(rdf_world); librdf_world_set_logger(rdf_world, (void *)request, ap_rdf_log_proxy); rdf_storage = librdf_new_storage(rdf_world, "uri", subjAltName, NULL); if (rdf_storage != NULL) { rdf_model = librdf_new_model(rdf_world, rdf_storage, NULL); } else ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, request, "WebID: librdf_new_storage returned NULL"); } if (rdf_model != NULL) { char *c_query = apr_psprintf(request->pool, SPARQL_WEBID, subjAltName); ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, request, "WebID: SPARQL query = %s", c_query); rdf_query = librdf_new_query(rdf_world, "sparql", NULL, (unsigned char*)c_query, NULL); } else { ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, request, "WebID: librdf_new_query returned NULL"); } if (rdf_query != NULL) { rdf_query_results = librdf_query_execute(rdf_query, rdf_model); if (rdf_query_results != NULL) { for (; r != OK && librdf_query_results_finished(rdf_query_results)==0; librdf_query_results_next(rdf_query_results)) { librdf_node *m_node, *e_node; unsigned char *rdf_mod; unsigned char *rdf_exp; if (r != OK && NULL != (m_node = librdf_query_results_get_binding_value_by_name(rdf_query_results, "m")) && NULL != (e_node = librdf_query_results_get_binding_value_by_name(rdf_query_results, "e"))) { if (librdf_node_get_type(m_node) == LIBRDF_NODE_TYPE_LITERAL && librdf_node_get_type(e_node) == LIBRDF_NODE_TYPE_LITERAL) { rdf_mod = librdf_node_get_literal_value(m_node); rdf_exp = librdf_node_get_literal_value(e_node); ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, request, "WebID: modulus = %s", rdf_mod); ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, request, "WebID: exponent = %s", rdf_exp); if (rdf_exp != NULL && apr_strtoi64((char*)rdf_exp, NULL, 10) == pkey_e_i && matches_pkey(rdf_mod, pkey_n)) r = OK; librdf_free_node(m_node); librdf_free_node(e_node); } } } librdf_free_query_results(rdf_query_results); } else ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, request, "WebID: librdf_query_execute returned NULL"); librdf_free_query(rdf_query); } else ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, request, "WebID: librdf_new_query returned NULL"); if (rdf_model) librdf_free_model(rdf_model); if (rdf_storage) librdf_free_storage(rdf_storage); if (rdf_world) librdf_free_world(rdf_world); return r; }
void run_query2(librdf_world* world, librdf_model* model, const std::string& q) { librdf_stream* strm; std::cout << "** Query: " << q << std::endl; librdf_uri* uri1 = librdf_new_uri(world, (const unsigned char*) "http://bunchy.org"); if (uri1 == 0) throw std::runtime_error("Couldn't parse URI"); librdf_uri* uri2 = librdf_new_uri(world, (const unsigned char*) "http://bunchy.org"); if (uri2 == 0) throw std::runtime_error("Couldn't parse URI"); librdf_query* qry = librdf_new_query(world, "sparql", uri1, (const unsigned char*) q.c_str(), uri2); librdf_free_uri(uri1); librdf_free_uri(uri2); if (qry == 0) throw std::runtime_error("Couldn't parse query."); librdf_query_results* results = librdf_query_execute(qry, model); if (results == 0) throw std::runtime_error("Couldn't execute query"); librdf_free_query(qry); std::cout << "--------------------------------------------------------" << std::endl; while (true) { if (librdf_query_results_finished(results)) break; int bcount = librdf_query_results_get_bindings_count(results); if (bcount < 0) throw std::runtime_error("Couldn't get query bindings count"); for(int i = 0; i < bcount; i++) { if (i != 0) std::cout << " "; librdf_node* value = librdf_query_results_get_binding_value(results, i); if (value == 0) throw std::runtime_error("Couldn't get results binding"); output_node(value); librdf_free_node(value); } std::cout << std::endl; librdf_query_results_next(results); } std::cout << "--------------------------------------------------------" << std::endl; librdf_free_query_results(results); }
int print_query_results(librdf_world* world, librdf_model* model, librdf_query_results *results) { int i; char *name; librdf_stream* stream; librdf_serializer* serializer; const char *query_graph_serializer_syntax_name="rdfxml"; if(librdf_query_results_is_bindings(results)) { fprintf(stdout, ": Query returned bindings results:\n"); while(!librdf_query_results_finished(results)) { fputs("result: [", stdout); for(i=0; i<librdf_query_results_get_bindings_count(results); i++) { librdf_node *value=librdf_query_results_get_binding_value(results, i); name=(char*)librdf_query_results_get_binding_name(results, i); if(i>0) fputs(", ", stdout); fprintf(stdout, "%s=", name); if(value) { librdf_node_print(value, stdout); librdf_free_node(value); } else fputs("NULL", stdout); } fputs("]\n", stdout); librdf_query_results_next(results); } fprintf(stdout, ": Query returned %d results\n", librdf_query_results_get_count(results)); } else if(librdf_query_results_is_boolean(results)) { fprintf(stdout, ": Query returned boolean result: %s\n", librdf_query_results_get_boolean(results) ? "true" : "false"); } else if(librdf_query_results_is_graph(results)) { librdf_storage* tmp_storage; librdf_model* tmp_model; tmp_storage=librdf_new_storage(world, NULL, NULL, NULL); tmp_model=librdf_new_model(world, tmp_storage, NULL); fprintf(stdout, ": Query returned graph result:\n"); stream=librdf_query_results_as_stream(results); if(!stream) { fprintf(stderr, ": Failed to get query results graph\n"); return -1; } librdf_model_add_statements(tmp_model, stream); librdf_free_stream(stream); fprintf(stdout, ": Total %d triples\n", librdf_model_size(model)); serializer=librdf_new_serializer(world, query_graph_serializer_syntax_name, NULL, NULL); if(!serializer) { fprintf(stderr, ": Failed to create serializer type %s\n", query_graph_serializer_syntax_name); return -1; } librdf_serializer_serialize_model_to_file_handle(serializer, stdout, NULL, tmp_model); librdf_free_serializer(serializer); librdf_free_model(tmp_model); librdf_free_storage(tmp_storage); } else { fprintf(stdout, ": Query returned unknown result format\n"); return -1; } return 0; }