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;
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)
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;
}
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"))
{
;
}
+ else if (!strncmp(e->pattern, "uri", 3))
+ {
+ ;
+ }
else
{
errors++;
}
}
+ yaz_tok_cfg_destroy(cfg);
+ return errors;
+}
+
+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"))
pr("\n", client_data);
}
}
- yaz_tok_cfg_destroy(cfg);
-
return errors ? -1 : r;
}
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();
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"