Allow include of db definitions MPSPARQL-15
authorAdam Dickmeiss <adam@indexdata.dk>
Thu, 30 Apr 2015 09:20:41 +0000 (11:20 +0200)
committerAdam Dickmeiss <adam@indexdata.dk>
Thu, 30 Apr 2015 09:20:41 +0000 (11:20 +0200)
src/filter_sparql.cpp
src/sparql.c
src/sparql.h
src/test_sparql.c

index 34759e5..f168571 100644 (file)
@@ -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<ConfPtr>::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;
index 3905347..c738e5d 100644 (file)
@@ -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)
 {
index 7d2da77..907ff66 100644 (file)
@@ -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
index 718f9f7..963667f 100644 (file)
@@ -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: <http://www.w3.org/1999/02/22-rdf-syntax-ns>\n"
                   "PREFIX bf: <http://bibframe.org/vocab/>\n"
                   "PREFIX gs: <http://gs.com/panorama/domain-model>\n"
@@ -473,6 +478,7 @@ static void tst2(void)
                   "}\n"
                   ));
 
+    yaz_sparql_destroy(s2);
     yaz_sparql_destroy(s);
 }