From a0884c6c35bc62160b6ce6100a4e8ad2d15ab937 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Thu, 30 Apr 2015 11:20:41 +0200 Subject: [PATCH] Allow include of db definitions MPSPARQL-15 --- src/filter_sparql.cpp | 21 ++++++++++++++++++++- src/sparql.c | 7 +++++++ src/sparql.h | 3 +++ src/test_sparql.c | 8 +++++++- 4 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/filter_sparql.cpp b/src/filter_sparql.cpp index 34759e5..f168571 100644 --- a/src/filter_sparql.cpp +++ b/src/filter_sparql.cpp @@ -177,6 +177,24 @@ void yf::SPARQL::configure(const xmlNode *xmlnode, bool test_only, conf->uri = mp::xml::get_text(attr->children); else if (!strcmp((const char *) attr->name, "schema")) conf->schema = mp::xml::get_text(attr->children); + else if (!strcmp((const char *) attr->name, "include")) + { + std::string name = mp::xml::get_text(attr->children); + std::list::const_iterator it = db_conf.begin(); + while (1) + if (it == db_conf.end()) + { + throw mp::filter::FilterException( + "include db not found: " + name); + } + else if (name.compare((*it)->db) == 0) + { + yaz_sparql_include(s, (*it)->s); + break; + } + else + it++; + } else throw mp::filter::FilterException( "Bad attribute " + std::string((const char *) @@ -766,7 +784,8 @@ void yf::SPARQL::Session::handle_z(mp::Package &package, Z_APDU *apdu_req) fset->db = db; it = m_sparql->db_conf.begin(); for (; it != m_sparql->db_conf.end(); it++) - if (yaz_match_glob((*it)->db.c_str(), db.c_str())) + if ((*it)->schema.length() > 0 + && yaz_match_glob((*it)->db.c_str(), db.c_str())) { mp::wrbuf addinfo_wr; mp::wrbuf sparql_wr; diff --git a/src/sparql.c b/src/sparql.c index 3905347..c738e5d 100644 --- a/src/sparql.c +++ b/src/sparql.c @@ -37,6 +37,13 @@ void yaz_sparql_destroy(yaz_sparql_t s) nmem_destroy(s->nmem); } +void yaz_sparql_include(yaz_sparql_t s, yaz_sparql_t u) +{ + struct sparql_entry *e = u->conf; + for (; e; e = e->next) + yaz_sparql_add_pattern(s, e->pattern, e->value); +} + int yaz_sparql_add_pattern(yaz_sparql_t s, const char *pattern, const char *value) { diff --git a/src/sparql.h b/src/sparql.h index 7d2da77..907ff66 100644 --- a/src/sparql.h +++ b/src/sparql.h @@ -46,6 +46,9 @@ int yaz_sparql_from_uri_wrbuf(yaz_sparql_t s, WRBUF addinfo, WRBUF w, YAZ_EXPORT int yaz_sparql_lookup_schema(yaz_sparql_t s, const char *schema); +YAZ_EXPORT +void yaz_sparql_include(yaz_sparql_t s, yaz_sparql_t u); + YAZ_END_CDECL #endif diff --git a/src/test_sparql.c b/src/test_sparql.c index 718f9f7..963667f 100644 --- a/src/test_sparql.c +++ b/src/test_sparql.c @@ -372,6 +372,7 @@ static void tst1(void) static void tst2(void) { yaz_sparql_t s = yaz_sparql_create(); + yaz_sparql_t s2 = yaz_sparql_create(); yaz_sparql_add_pattern(s, "prefix", "rdf: http://www.w3.org/1999/02/22-rdf-syntax-ns"); @@ -445,10 +446,14 @@ static void tst2(void) " OPTIONAL { ?inst bf:heldBy ?lib } .\n" " ?work bf:creator/bf:label ?v0 " "FILTER(contains(?v0, \"london\"))\n" + + "}\n")); + yaz_sparql_include(s2, s); + YAZ_CHECK(test_query( - s, "@or @and @attr 1=bf.creator a @attr 1=bf.title b @attr 1=bf.title c", + s2, "@or @and @attr 1=bf.creator a @attr 1=bf.title b @attr 1=bf.title c", "PREFIX rdf: \n" "PREFIX bf: \n" "PREFIX gs: \n" @@ -473,6 +478,7 @@ static void tst2(void) "}\n" )); + yaz_sparql_destroy(s2); yaz_sparql_destroy(s); } -- 1.7.10.4