Checkout submodules as necessary MPSPARQL-23
[mp-sparql-moved-to-github.git] / src / sparql.c
index 3744fe6..6fe5e5c 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)
 {
@@ -58,6 +65,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 +111,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 +148,38 @@ 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 't':
+                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;
+                }
+                break;
             case 'd':
                 switch (term->which)
                 {
@@ -210,6 +207,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 +322,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 +381,89 @@ int yaz_sparql_from_rpn_stream(yaz_sparql_t s,
         {
             ;
         }
+        else if (!strncmp(e->pattern, "present", 7))
+        {
+            ;
+        }
+        else if (!strncmp(e->pattern, "uri", 3))
+        {
+            ;
+        }
         else
         {
             errors++;
         }
     }
+    yaz_tok_cfg_destroy(cfg);
+    return errors;
+}
+
+struct sparql_entry *lookup_schema(yaz_sparql_t s, const char *schema)
+{
+    struct sparql_entry *e;
+
+    for (e = s->conf; e; e = e->next)
+    {
+        if (!strncmp(e->pattern, "present.", 8))
+        {
+            if (!schema || !strcmp(e->pattern + 8, schema))
+                break;
+        }
+        if (!strncmp(e->pattern, "uri.", 4))
+        {
+            if (!schema || !strcmp(e->pattern + 4, schema))
+                break;
+        }
+    }
+    return e;
+}
+
+int yaz_sparql_lookup_schema(yaz_sparql_t s, const char *schema)
+{
+    return lookup_schema(s, schema) ? 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 = lookup_schema(s, schema);
+    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,11 +537,51 @@ int yaz_sparql_from_rpn_stream(yaz_sparql_t s,
             pr("\n", client_data);
         }
     }
-    yaz_tok_cfg_destroy(cfg);
-
     return errors ? -1 : r;
 }
 
+void yaz_sparql_explain_indexes( yaz_sparql_t s, WRBUF w, int indent)
+{
+    char indentspace[200]; // must be enough
+    assert(indent<200);
+    int i;
+    for (i=0; i < indent; i++)
+        indentspace[i] = ' ';
+    indentspace[indent] = '\0';
+
+    struct sparql_entry *e;
+    wrbuf_puts(w,indentspace);
+    wrbuf_puts(w,"<indexInfo>\n");
+
+    for (e = s->conf; e; e = e->next)
+    {
+        /*
+        wrbuf_puts(w,"    <FOO>");
+        wrbuf_xmlputs(w, e->pattern );
+        wrbuf_puts(w,"  : ");
+        wrbuf_xmlputs(w, e->value );
+        wrbuf_puts(w,"    </FOO>\n");
+        */
+        if ( strncmp(e->pattern, "index.", 6 ) == 0 )
+        {
+            wrbuf_puts(w,indentspace);
+            wrbuf_puts(w,"  <index>\n");
+            wrbuf_puts(w,indentspace);
+            wrbuf_puts(w,"    <title>");
+            wrbuf_xmlputs(w, e->pattern + 6);
+            wrbuf_puts(w,"</title>\n");
+            wrbuf_puts(w,indentspace);
+            wrbuf_puts(w,"    <map><name>");
+            wrbuf_xmlputs(w, e->pattern + 6);
+            wrbuf_puts(w,"</name></map>\n");
+            wrbuf_puts(w,indentspace);
+            wrbuf_puts(w,"  </index>\n");
+        }
+    }
+    wrbuf_puts(w,indentspace);
+    wrbuf_puts(w,"</indexInfo>\n");
+}
+
 /*
  * Local variables:
  * c-basic-offset: 4