Merge branch 'master' of ssh://git.indexdata.com:222/home/git/pub/mp-sparql
authorHeikki Levanto <heikki@indexdata.dk>
Wed, 29 Apr 2015 10:26:22 +0000 (12:26 +0200)
committerHeikki Levanto <heikki@indexdata.dk>
Wed, 29 Apr 2015 10:26:22 +0000 (12:26 +0200)
Makefile
bibframe/triplestore.xml
src/Makefile
src/filter_sparql.cpp
src/sparql.c
src/sparql.h
src/test_sparql.c

index 60056c9..6ba1a3a 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -9,7 +9,7 @@ all: $(SUBDIRS)
 $(SUBDIRS):
        $(MAKE) -C $@
 
-clean:
+clean check:
        for d in $(SUBDIRS); do \
                $(MAKE) -C $$d $@; \
        done
index 3ea4914..42cfc26 100644 (file)
       <index type="bf.creator">?work bf:creator ?c .
           ?c bf:label %v FILTER(contains(%v, %s))</index>
       <index type="bf.subject">?work bf:subject ?subject .
-          ?subject bf:label %v FILTER(contains(%v, %s))</index>
+          ?subject bf:label %v FILTER(contains(%v, %s))
+      </index>
+      <uri type="BF-L">
+       CONSTRUCT { ?work ?rel ?subj }
+       WHERE {
+        ?work a bf:Work .
+        ?work ?rel ?subj .
+        ?work ?rel ?obj FILTER( str(?work) = %s )
+        }
+      </uri>
       <!-- TODO: Define a query to return detailed information for each
       schema. For example
       <present shema="BF-L" results="sparql-results" >
index 604d6ff..69d39f0 100644 (file)
@@ -13,8 +13,8 @@ MP_SO := metaproxy_filter_sparql.so
 
 O := filter_sparql.o sparql.o
 
-CXXFLAGS := -DVERSION=\"$(VERSION)\" $(MP_CFLAGS) -fPIC
-CFLAGS := -DVERSION=\"$(VERSION)\" $(MP_CFLAGS) -fPIC
+CXXFLAGS := -DVERSION=\"$(VERSION)\" $(MP_CFLAGS) -fPIC -g -Wall
+CFLAGS := -DVERSION=\"$(VERSION)\" $(MP_CFLAGS) -fPIC -g -Wall
 
 all: $(MP_SO)
 
index 97c8a4d..681575a 100644 (file)
@@ -93,12 +93,18 @@ namespace metaproxy_1 {
             Session(const SPARQL *);
             ~Session();
             void handle_z(Package &package, Z_APDU *apdu);
-            Z_APDU *run_sparql(mp::Package &package,
-                               Z_APDU *apdu_req,
-                               mp::odr &odr,
-                               const char *sparql_query,
-                               ConfPtr conf, FrontendSetPtr fset);
+            Z_APDU *search(mp::Package &package,
+                           Z_APDU *apdu_req,
+                           mp::odr &odr,
+                           const char *sparql_query,
+                           ConfPtr conf, FrontendSetPtr fset);
+            int invoke_sparql(mp::Package &package,
+                              const char *sparql_query,
+                              ConfPtr conf,
+                              WRBUF w);
+
             Z_Records *fetch(
+                Package &package,
                 FrontendSetPtr fset,
                 ODR odr, Odr_oid *preferredRecordSyntax,
                 Z_ElementSetNames *esn,
@@ -291,8 +297,7 @@ void yf::SPARQL::release_session(Package &package) const
     }
 }
 
-static bool get_result(xmlDoc *doc, Odr_int *sz, Odr_int pos,
-                       xmlDoc **ndoc)
+static bool get_result(xmlDoc *doc, Odr_int *sz, Odr_int pos, xmlDoc **ndoc)
 {
     xmlNode *ptr = xmlDocGetRootElement(doc);
     xmlNode *q0;
@@ -405,6 +410,7 @@ static bool get_result(xmlDoc *doc, Odr_int *sz, Odr_int pos,
 }
 
 Z_Records *yf::SPARQL::Session::fetch(
+    Package &package,
     FrontendSetPtr fset,
     ODR odr, Odr_oid *preferredRecordSyntax,
     Z_ElementSetNames *esn,
@@ -413,26 +419,31 @@ Z_Records *yf::SPARQL::Session::fetch(
 {
     Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
     std::list<Result>::iterator it = fset->results.begin();
-    if (esn && esn->which == Z_ElementSetNames_generic && esn->u.generic)
+    const char *schema = 0;
+    bool uri_lookup = false;
+    if (esn && esn->which == Z_ElementSetNames_generic)
+        schema = esn->u.generic;
+
+    for (; it != fset->results.end(); it++)
     {
-        for (; it != fset->results.end(); it++)
-        {
-            yaz_log(YLOG_LOG, "checking xmldoc=%p schema=%s user-schema=%s",
-                    it->doc, it->conf->schema.c_str(), esn->u.generic);
-            if (!strcmp(esn->u.generic, it->conf->schema.c_str()))
-                break;
-        }
-        if (it == fset->results.end())
+        if (schema && !strcmp(esn->u.generic, it->conf->schema.c_str()))
+            break;
+        if (yaz_sparql_lookup_schema(it->conf->s, schema))
         {
-            rec->which = Z_Records_NSD;
-            rec->u.nonSurrogateDiagnostic =
-                zget_DefaultDiagFormat(
-                    odr,
-                    YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_,
-                    esn->u.generic);
-            return rec;
+            uri_lookup = true;
+            break;
         }
     }
+    if (it == fset->results.end())
+    {
+        rec->which = Z_Records_NSD;
+        rec->u.nonSurrogateDiagnostic =
+            zget_DefaultDiagFormat(
+                odr,
+                YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_,
+                schema);
+        return rec;
+    }
     rec->which = Z_Records_DBOSD;
     rec->u.databaseOrSurDiagnostics = (Z_NamePlusRecordList *)
         odr_malloc(odr, sizeof(Z_NamePlusRecordList));
@@ -460,12 +471,72 @@ Z_Records *yf::SPARQL::Session::fetch(
             xmlFreeDoc(ndoc);
             break;
         }
-        xmlBufferPtr buf = xmlBufferCreate();
-        xmlNodeDump(buf, ndoc, ndoc_root, 0, 0);
-        npr->u.databaseRecord =
-            z_ext_record_xml(odr, (const char *) buf->content, buf->use);
+        if (uri_lookup)
+        {
+            std::string uri;
+            xmlNode *n = ndoc_root;
+            while (n)
+            {
+                if (n->type == XML_ELEMENT_NODE)
+                {
+                    if (!strcmp((const char *) n->name, "uri"))
+                    {
+                        uri = mp::xml::get_text(n->children);
+
+                    }
+                    n = n->children;
+                }
+                else
+                    n = n->next;
+            }
+            if (!uri.length())
+            {
+                rec->which = Z_Records_NSD;
+                rec->u.nonSurrogateDiagnostic =
+                    zget_DefaultDiagFormat(
+                        odr,
+                        YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS, 0);
+                xmlFreeDoc(ndoc);
+                return rec;
+            }
+            else
+            {
+                mp::wrbuf addinfo, query, w;
+                int error = yaz_sparql_from_uri_wrbuf(it->conf->s,
+                                                      addinfo, query,
+                                                      uri.c_str(), schema);
+                if (!error)
+                {
+                    yaz_log(YLOG_LOG, "query=%s", query.c_str());
+                    error = invoke_sparql(package, query.c_str(),
+                                          it->conf, w);
+                }
+                if (error)
+                {
+                    rec->which = Z_Records_NSD;
+                    rec->u.nonSurrogateDiagnostic =
+                        zget_DefaultDiagFormat(
+                            odr,
+                            error,
+                            addinfo.len() ? addinfo.c_str() : 0);
+                    xmlFreeDoc(ndoc);
+                    return rec;
+                }
+                npr->u.databaseRecord =
+                    z_ext_record_xml(odr, w.c_str(), w.len());
+            }
+        }
+        else
+        {
+            xmlBufferPtr buf = xmlBufferCreate();
+            xmlNodeDump(buf, ndoc, ndoc_root, 0, 0);
+            yaz_log(YLOG_LOG, "record %s %.*s", uri_lookup ? "uri" : "normal",
+                    (int) buf->use, (const char *) buf->content);
+            npr->u.databaseRecord =
+                z_ext_record_xml(odr, (const char *) buf->content, buf->use);
+            xmlBufferFree(buf);
+        }
         xmlFreeDoc(ndoc);
-        xmlBufferFree(buf);
     }
     rec->u.databaseOrSurDiagnostics->num_records = i;
     *number_returned = i;
@@ -476,14 +547,13 @@ Z_Records *yf::SPARQL::Session::fetch(
     return rec;
 }
 
-Z_APDU *yf::SPARQL::Session::run_sparql(mp::Package &package,
-                                        Z_APDU *apdu_req,
-                                        mp::odr &odr,
-                                        const char *sparql_query,
-                                        ConfPtr conf, FrontendSetPtr fset)
+int yf::SPARQL::Session::invoke_sparql(mp::Package &package,
+                                       const char *sparql_query,
+                                       ConfPtr conf,
+                                       WRBUF w)
 {
-    Z_SearchRequest *req = apdu_req->u.searchRequest;
     Package http_package(package.session(), package.origin());
+    mp::odr odr;
 
     http_package.copy_filter(package);
     Z_GDU *gdu = z_get_HTTP_Request_uri(odr, conf->uri.c_str(), 0, 1);
@@ -510,32 +580,50 @@ Z_APDU *yf::SPARQL::Session::run_sparql(mp::Package &package,
     http_package.move();
 
     Z_GDU *gdu_resp = http_package.response().get();
-    Z_APDU *apdu_res = 0;
+
     if (!gdu_resp || gdu_resp->which != Z_GDU_HTTP_Response)
     {
-        yaz_log(YLOG_LOG, "sparql: no HTTP response");
-        apdu_res = odr.create_searchResponse(apdu_req,
-                                             YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
-                                             "no HTTP response from backend");
+        wrbuf_puts(w, "no HTTP response from backend");
+        return YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
     }
     else if (gdu_resp->u.HTTP_Response->code != 200)
     {
-        mp::wrbuf w;
-
         wrbuf_printf(w, "sparql: HTTP error %d from backend",
                      gdu_resp->u.HTTP_Response->code);
-        apdu_res = odr.create_searchResponse(apdu_req,
-                                             YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
-                                             w.c_str());
+        return YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
+    }
+    Z_HTTP_Response *resp = gdu_resp->u.HTTP_Response;
+    wrbuf_write(w, resp->content_buf, resp->content_len);
+    return 0;
+}
+
+Z_APDU *yf::SPARQL::Session::search(mp::Package &package,
+                                    Z_APDU *apdu_req,
+                                    mp::odr &odr,
+                                    const char *sparql_query,
+                                    ConfPtr conf, FrontendSetPtr fset)
+{
+    Z_SearchRequest *req = apdu_req->u.searchRequest;
+    Z_APDU *apdu_res = 0;
+    mp::wrbuf w;
+
+    int error = invoke_sparql(package, sparql_query, conf, w);
+    if (error)
+    {
+        apdu_res = odr.create_searchResponse(apdu_req, error,
+                                             w.len() ?
+                                             w.c_str() : 0);
     }
     else
     {
-        Z_HTTP_Response *resp = gdu_resp->u.HTTP_Response;
-        xmlDocPtr doc = xmlParseMemory(resp->content_buf, resp->content_len);
+        xmlDocPtr doc = xmlParseMemory(w.c_str(), w.len());
         if (!doc)
-            apdu_res = odr.create_searchResponse(apdu_req,
-                                             YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
-                                             "invalid XML from backendbackend");
+        {
+            apdu_res = odr.create_searchResponse(
+                apdu_req,
+                YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
+                "invalid XML from backendbackend");
+        }
         else
         {
             Result result;
@@ -566,7 +654,7 @@ Z_APDU *yf::SPARQL::Session::run_sparql(mp::Package &package,
                     esn = req->mediumSetElementSetNames;
                 else
                     esn = req->smallSetElementSetNames;
-                records = fetch(fset,
+                records = fetch(package, fset,
                                 odr, req->preferredRecordSyntax, esn,
                                 1, number,
                                 error_code, addinfo,
@@ -680,8 +768,8 @@ void yf::SPARQL::Session::handle_z(mp::Package &package, Z_APDU *apdu_req)
             for (; it != m_sparql->db_conf.end(); it++)
                 if (yaz_match_glob((*it)->db.c_str(), db.c_str()))
                 {
-                    WRBUF addinfo_wr = wrbuf_alloc();
-                    WRBUF sparql_wr = wrbuf_alloc();
+                    mp::wrbuf addinfo_wr;
+                    mp::wrbuf sparql_wr;
                     int error =
                         yaz_sparql_from_rpn_wrbuf((*it)->s,
                                                   addinfo_wr, sparql_wr,
@@ -690,19 +778,16 @@ void yf::SPARQL::Session::handle_z(mp::Package &package, Z_APDU *apdu_req)
                     {
                         apdu_res = odr.create_searchResponse(
                             apdu_req, error,
-                            wrbuf_len(addinfo_wr) ?
-                            wrbuf_cstr(addinfo_wr) : 0);
+                            addinfo_wr.len() ? addinfo_wr.c_str() : 0);
                     }
                     else
                     {
-                        Z_APDU *apdu_1 = run_sparql(package, apdu_req, odr,
-                                                    wrbuf_cstr(sparql_wr), *it,
-                                                    fset);
+                        Z_APDU *apdu_1 = search(package, apdu_req, odr,
+                                                sparql_wr.c_str(), *it,
+                                                fset);
                         if (!apdu_res)
                             apdu_res = apdu_1;
                     }
-                    wrbuf_destroy(addinfo_wr);
-                    wrbuf_destroy(sparql_wr);
                 }
             if (apdu_res == 0)
             {
@@ -746,6 +831,7 @@ void yf::SPARQL::Session::handle_z(mp::Package &package, Z_APDU *apdu_req)
             }
         }
         Z_Records *records = fetch(
+            package,
             fset_it->second,
             odr, req->preferredRecordSyntax, esn,
             *req->resultSetStartPoint, *req->numberOfRecordsRequested,
index 3744fe6..e9987a1 100644 (file)
@@ -58,6 +58,13 @@ int yaz_sparql_from_rpn_wrbuf(yaz_sparql_t s, WRBUF addinfo, WRBUF w,
     return yaz_sparql_from_rpn_stream(s, addinfo, wrbuf_vp_puts, w, q);
 }
 
+int yaz_sparql_from_uri_wrbuf(yaz_sparql_t s, WRBUF addinfo, WRBUF w,
+                              const char *uri, const char *schema)
+{
+    return yaz_sparql_from_uri_stream(s, addinfo, wrbuf_vp_puts, w, uri,
+                                      schema);
+}
+
 static Odr_int lookup_attr_numeric(Z_AttributeList *attributes, int type)
 {
     int j;
@@ -97,60 +104,11 @@ static const char *lookup_attr_string(Z_AttributeList *attributes, int type)
     return 0;
 }
 
-static int apt(yaz_sparql_t s, WRBUF addinfo, WRBUF res, WRBUF vars,
-               Z_AttributesPlusTerm *q, int indent, int *var_no)
+static int z_term(yaz_sparql_t s, WRBUF addinfo, WRBUF res, WRBUF vars,
+                  struct sparql_entry *e, const char *use_var,
+                  Z_Term *term, int indent, int *var_no)
 {
-    Z_Term *term = q->term;
-    Odr_int v = lookup_attr_numeric(q->attributes, 1);
-    struct sparql_entry *e = 0;
     const char *cp;
-    const char *use_var = 0;
-    int i;
-
-    wrbuf_puts(res, "  ");
-    for (i = 0; i < indent; i++)
-        wrbuf_puts(res, " ");
-    if (v)
-    {
-        for (e = s->conf; e; e = e->next)
-        {
-            if (!strncmp(e->pattern, "index.", 6))
-            {
-                char *end = 0;
-                Odr_int w = odr_strtol(e->pattern + 6, &end, 10);
-
-                if (end && *end == '\0' && v == w)
-                    break;
-            }
-        }
-        if (!e)
-        {
-            wrbuf_printf(addinfo, ODR_INT_PRINTF, v);
-            return YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
-        }
-    }
-    else
-    {
-        const char *index_name = lookup_attr_string(q->attributes, 1);
-        if (!index_name)
-            index_name = "any";
-        for (e = s->conf; e; e = e->next)
-        {
-            if (!strncmp(e->pattern, "index.", 6))
-            {
-                if (!strcmp(e->pattern + 6, index_name))
-                    break;
-            }
-        }
-        if (!e)
-        {
-            wrbuf_puts(addinfo, index_name);
-            return YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
-        }
-    }
-    assert(e);
-    wrbuf_rewind(addinfo);
-
     for (cp = e->value; *cp; cp++)
     {
         if (strchr(" \t\r\n\f", *cp) && !use_var)
@@ -183,6 +141,23 @@ static int apt(yaz_sparql_t s, WRBUF addinfo, WRBUF res, WRBUF vars,
                 }
                 wrbuf_puts(addinfo, "\"");
                 break;
+            case 'u':
+                wrbuf_puts(addinfo, "<");
+                switch (term->which)
+                {
+                case Z_Term_general:
+                    wrbuf_json_write(addinfo,
+                                term->u.general->buf, term->u.general->len);
+                    break;
+                case Z_Term_numeric:
+                    wrbuf_printf(addinfo, ODR_INT_PRINTF, *term->u.numeric);
+                    break;
+                case Z_Term_characterString:
+                    wrbuf_json_puts(addinfo, term->u.characterString);
+                    break;
+                }
+                wrbuf_puts(addinfo, ">");
+                break;
             case 'd':
                 switch (term->which)
                 {
@@ -210,6 +185,62 @@ static int apt(yaz_sparql_t s, WRBUF addinfo, WRBUF res, WRBUF vars,
             wrbuf_putc(addinfo, *cp);
     }
     wrbuf_puts(res, wrbuf_cstr(addinfo));
+    return 0;
+}
+
+static int apt(yaz_sparql_t s, WRBUF addinfo, WRBUF res, WRBUF vars,
+               Z_AttributesPlusTerm *q, int indent, int *var_no)
+{
+    Odr_int v = lookup_attr_numeric(q->attributes, 1);
+    struct sparql_entry *e = 0;
+    const char *use_var = 0;
+    int i;
+
+    wrbuf_puts(res, "  ");
+    for (i = 0; i < indent; i++)
+        wrbuf_puts(res, " ");
+    if (v)
+    {
+        for (e = s->conf; e; e = e->next)
+        {
+            if (!strncmp(e->pattern, "index.", 6))
+            {
+                char *end = 0;
+                Odr_int w = odr_strtol(e->pattern + 6, &end, 10);
+
+                if (end && *end == '\0' && v == w)
+                    break;
+            }
+        }
+        if (!e)
+        {
+            wrbuf_printf(addinfo, ODR_INT_PRINTF, v);
+            return YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
+        }
+    }
+    else
+    {
+        const char *index_name = lookup_attr_string(q->attributes, 1);
+        if (!index_name)
+            index_name = "any";
+        for (e = s->conf; e; e = e->next)
+        {
+            if (!strncmp(e->pattern, "index.", 6))
+            {
+                if (!strcmp(e->pattern + 6, index_name))
+                    break;
+            }
+        }
+        if (!e)
+        {
+            wrbuf_puts(addinfo, index_name);
+            return YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
+        }
+    }
+    assert(e);
+    wrbuf_rewind(addinfo);
+
+    z_term(s, addinfo, res, vars, e, use_var, q->term, indent, var_no);
     (*var_no)++;
     return 0;
 }
@@ -269,17 +300,15 @@ static int rpn_structure(yaz_sparql_t s, WRBUF addinfo,
     return 0;
 }
 
-int yaz_sparql_from_rpn_stream(yaz_sparql_t s,
-                               WRBUF addinfo,
-                               void (*pr)(const char *buf,
-                                          void *client_data),
-                               void *client_data,
-                               Z_RPNQuery *q)
+static int emit_prefixes(yaz_sparql_t s,
+                          WRBUF addinfo,
+                          void (*pr)(const char *buf,
+                                     void *client_data),
+                          void *client_data)
 {
     struct sparql_entry *e;
     yaz_tok_cfg_t cfg = yaz_tok_cfg_create();
-    int r = 0, errors = 0;
-
+    int errors = 0;
     for (e = s->conf; e; e = e->next)
     {
         if (!strcmp(e->pattern, "prefix"))
@@ -330,11 +359,88 @@ int yaz_sparql_from_rpn_stream(yaz_sparql_t s,
         {
             ;
         }
+        else if (!strncmp(e->pattern, "uri", 3))
+        {
+            ;
+        }
         else
         {
             errors++;
         }
     }
+    yaz_tok_cfg_destroy(cfg);
+    return errors;
+}
+
+int yaz_sparql_lookup_schema(yaz_sparql_t s, const char *schema)
+{
+    struct sparql_entry *e;
+
+    for (e = s->conf; e; e = e->next)
+    {
+        if (!schema && !strcmp(e->pattern, "uri"))
+            break;
+        else if (schema && !strncmp(e->pattern, "uri.", 4))
+        {
+            if (!strcmp(e->pattern + 4, schema))
+                break;
+        }
+    }
+    return e ? 1 : 0;
+}
+
+int yaz_sparql_from_uri_stream(yaz_sparql_t s,
+                               WRBUF addinfo,
+                               void (*pr)(const char *buf, void *client_data),
+                               void *client_data,
+                               const char *uri, const char *schema)
+{
+    int r = 0, errors = emit_prefixes(s, addinfo, pr, client_data);
+    struct sparql_entry *e;
+
+    for (e = s->conf; e; e = e->next)
+    {
+        if (!schema && !strcmp(e->pattern, "uri"))
+            break;
+        else if (schema && !strncmp(e->pattern, "uri.", 4))
+        {
+            if (!strcmp(e->pattern + 4, schema))
+                break;
+        }
+    }
+    if (!e)
+        errors++;
+    if (!errors)
+    {
+        WRBUF res = wrbuf_alloc();
+        WRBUF vars = wrbuf_alloc();
+        int var_no = 0;
+        Z_Term term;
+
+        term.which = Z_Term_characterString;
+        term.u.characterString = (char *) uri;
+        r = z_term(s, addinfo, res, vars, e, 0, &term, 0, &var_no);
+        if (!r)
+        {
+            pr(wrbuf_cstr(res), client_data);
+            pr("\n", client_data);
+        }
+        wrbuf_destroy(res);
+        wrbuf_destroy(vars);
+    }
+    return errors ? -1 : r;
+}
+
+int yaz_sparql_from_rpn_stream(yaz_sparql_t s,
+                               WRBUF addinfo,
+                               void (*pr)(const char *buf,
+                                          void *client_data),
+                               void *client_data,
+                               Z_RPNQuery *q)
+{
+    int r = 0, errors = emit_prefixes(s, addinfo, pr, client_data);
+    struct sparql_entry *e;
+
     for (e = s->conf; e; e = e->next)
     {
         if (!strcmp(e->pattern, "form"))
@@ -408,8 +514,6 @@ int yaz_sparql_from_rpn_stream(yaz_sparql_t s,
             pr("\n", client_data);
         }
     }
-    yaz_tok_cfg_destroy(cfg);
-
     return errors ? -1 : r;
 }
 
index 257b3e3..7d2da77 100644 (file)
@@ -31,6 +31,21 @@ YAZ_EXPORT
 int yaz_sparql_from_rpn_wrbuf(yaz_sparql_t s, WRBUF addinfo, WRBUF w,
                               Z_RPNQuery *q);
 
+
+YAZ_EXPORT
+int yaz_sparql_from_uri_stream(yaz_sparql_t s,
+                               WRBUF addinfo,
+                               void (*pr)(const char *buf, void *client_data),
+                               void *client_data,
+                               const char *uri, const char *schema);
+
+YAZ_EXPORT
+int yaz_sparql_from_uri_wrbuf(yaz_sparql_t s, WRBUF addinfo, WRBUF w,
+                              const char *uri, const char *schema);
+
+YAZ_EXPORT
+int yaz_sparql_lookup_schema(yaz_sparql_t s, const char *schema);
+
 YAZ_END_CDECL
 
 #endif
index 3590f9b..4bd27a0 100644 (file)
@@ -63,6 +63,51 @@ static int test_query(yaz_sparql_t s, const char *pqf, const char *expect)
     return ret;
 }
 
+static int test_uri(yaz_sparql_t s, const char *uri, const char *schema,
+                    const char *expect)
+{
+    int ret = 0;
+    WRBUF addinfo = wrbuf_alloc();
+    WRBUF w = wrbuf_alloc();
+
+    int r = yaz_sparql_from_uri_wrbuf(s, addinfo, w, uri, schema);
+    if (expect)
+    {
+        if (!r)
+        {
+            if (!strcmp(expect, wrbuf_cstr(w)))
+                ret = 1;
+            else
+            {
+                yaz_log(YLOG_WARN, "test_sparql: uri=%s", uri);
+                yaz_log(YLOG_WARN, " expect: %s", expect);
+                yaz_log(YLOG_WARN, " got:    %s", wrbuf_cstr(w));
+            }
+        }
+        else
+        {
+            yaz_log(YLOG_WARN, "test_sparql: uri=%s", uri);
+            yaz_log(YLOG_WARN, " expect: %s", expect);
+            yaz_log(YLOG_WARN, " got error: %d:%s", r, wrbuf_cstr(addinfo));
+        }
+    }
+    else
+    {
+        if (r)
+            ret = 1;
+        else
+        {
+            yaz_log(YLOG_WARN, "test_sparql: uri=%s", uri);
+            yaz_log(YLOG_WARN, " expect error");
+            yaz_log(YLOG_WARN, " got:    %s", wrbuf_cstr(w));
+        }
+    }
+    wrbuf_destroy(w);
+    wrbuf_destroy(addinfo);
+    return ret;
+}
+
+
 static void tst1(void)
 {
     yaz_sparql_t s = yaz_sparql_create();
@@ -73,10 +118,7 @@ static void tst1(void)
                            "bf: <http://bibframe.org/vocab/>");
     yaz_sparql_add_pattern(s, "prefix",
                            "gs: http://gs.com/panorama/domain-model");
-    yaz_sparql_add_pattern(s, "field.title", "?title");
-    yaz_sparql_add_pattern(s, "field.author", "?author");
-    yaz_sparql_add_pattern(s, "field.description", "?description");
-    yaz_sparql_add_pattern(s, "field.instanceTitle", "?ititle");
+    yaz_sparql_add_pattern(s, "form", "SELECT ?title ?author ?description ?ititle");
     yaz_sparql_add_pattern(s, "criteria", "?work a bf:Work");
     yaz_sparql_add_pattern(s, "criteria", "?work bf:workTitle/bf:titleValue ?title");
     yaz_sparql_add_pattern(s, "criteria", "?work bf:creator/bf:label ?author");
@@ -107,12 +149,20 @@ static void tst1(void)
     yaz_sparql_add_pattern(s, "index.bf.targetAudience",
                            "?work bf:targetAudience %s");
     yaz_sparql_add_pattern(s, "index.bf.isbn", "?inst bf:ISBN %s");
+
+    yaz_sparql_add_pattern(s, "uri.full", "SELECT ?sub ?rel WHERE ?work = %u");
+
+    YAZ_CHECK(test_uri(s, "http://x/y", "full",
+                       "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"
+                       "SELECT ?sub ?rel WHERE ?work = <http://x/y>\n"));
+
     YAZ_CHECK(test_query(
                   s, "@attr 1=bf.title computer",
                   "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"
-                  "\n"
                   "SELECT ?title ?author ?description ?ititle\n"
                   "WHERE {\n"
                   "  ?work a bf:Work .\n"
@@ -132,7 +182,6 @@ static void tst1(void)
                   "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"
-                  "\n"
                   "SELECT ?title ?author ?description ?ititle\n"
                   "WHERE {\n"
                   "  ?work a bf:Work .\n"
@@ -152,7 +201,6 @@ static void tst1(void)
                   "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"
-                  "\n"
                   "SELECT ?title ?author ?description ?ititle\n"
                   "WHERE {\n"
                   "  ?work a bf:Work .\n"
@@ -173,7 +221,6 @@ static void tst1(void)
                   "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"
-                  "\n"
                   "SELECT ?title ?author ?description ?ititle\n"
                   "WHERE {\n"
                   "  ?work a bf:Work .\n"
@@ -198,7 +245,6 @@ static void tst1(void)
                   "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"
-                  "\n"
                   "SELECT ?title ?author ?description ?ititle\n"
                   "WHERE {\n"
                   "  ?work a bf:Work .\n"
@@ -228,7 +274,6 @@ static void tst1(void)
                   "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"
-                  "\n"
                   "SELECT ?title ?author ?description ?ititle\n"
                   "WHERE {\n"
                   "  ?work a bf:Work .\n"
@@ -255,7 +300,6 @@ static void tst1(void)
                   "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"
-                  "\n"
                   "SELECT ?title ?author ?description ?ititle\n"
                   "WHERE {\n"
                   "  ?work a bf:Work .\n"
@@ -280,7 +324,6 @@ static void tst1(void)
                   "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"
-                  "\n"
                   "SELECT ?title ?author ?description ?ititle\n"
                   "WHERE {\n"
                   "  ?work a bf:Work .\n"
@@ -301,7 +344,6 @@ static void tst1(void)
                   "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"
-                  "\n"
                   "SELECT ?title ?author ?description ?ititle\n"
                   "WHERE {\n"
                   "  ?work a bf:Work .\n"
@@ -329,10 +371,7 @@ static void tst2(void)
                            "bf: <http://bibframe.org/vocab/>");
     yaz_sparql_add_pattern(s, "prefix",
                            "gs: http://gs.com/panorama/domain-model");
-    yaz_sparql_add_pattern(s, "field.title", "?title");
-    yaz_sparql_add_pattern(s, "field.author", "?author");
-    yaz_sparql_add_pattern(s, "field.description", "?description");
-    yaz_sparql_add_pattern(s, "field.instanceTitle", "?ititle");
+    yaz_sparql_add_pattern(s, "form", "SELECT ?title ?author ?description ?ititle");
     yaz_sparql_add_pattern(s, "criteria", "?work a bf:Work");
     yaz_sparql_add_pattern(s, "criteria", "?work bf:workTitle/bf:titleValue ?title");
     yaz_sparql_add_pattern(s, "criteria", "?work bf:creator/bf:label ?author");
@@ -368,7 +407,6 @@ static void tst2(void)
                   "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"
-                  "\n"
                   "SELECT ?title ?author ?description ?ititle\n"
                   "WHERE {\n"
                   "  ?work a bf:Work .\n"
@@ -388,7 +426,6 @@ static void tst2(void)
                   "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"
-                  "\n"
                   "SELECT ?title ?author ?description ?ititle\n"
                   "WHERE {\n"
                   "  ?work a bf:Work .\n"
@@ -407,7 +444,6 @@ static void tst2(void)
                   "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"
-                  "\n"
                   "SELECT ?title ?author ?description ?ititle\n"
                   "WHERE {\n"
                   "  ?work a bf:Work .\n"