1 /* This file is part of Metaproxy.
2 Copyright (C) Index Data
4 Metaproxy is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
9 Metaproxy is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <metaproxy/package.hpp>
20 #include <metaproxy/util.hpp>
23 #include <yaz/diagbib1.h>
24 #include <yaz/match_glob.h>
25 #include <boost/scoped_ptr.hpp>
26 #include <boost/thread/mutex.hpp>
27 #include <boost/thread/condition.hpp>
28 #include <boost/algorithm/string.hpp>
33 namespace mp = metaproxy_1;
34 namespace yf = mp::filter;
36 namespace metaproxy_1 {
38 class SPARQL : public Base {
45 typedef boost::shared_ptr<Session> SessionPtr;
46 typedef boost::shared_ptr<Conf> ConfPtr;
48 typedef boost::shared_ptr<FrontendSet> FrontendSetPtr;
49 typedef std::map<std::string,FrontendSetPtr> FrontendSets;
53 void process(metaproxy_1::Package & package) const;
54 void configure(const xmlNode * ptr, bool test_only,
56 SessionPtr get_session(Package &package, Z_APDU **apdu) const;
57 void release_session(Package &package) const;
58 boost::scoped_ptr<Rep> m_p;
59 std::list<ConfPtr> db_conf;
71 boost::condition m_cond_session_ready;
73 std::map<mp::Session,SessionPtr> m_clients;
75 class SPARQL::Result {
80 friend class FrontendSet;
85 class SPARQL::FrontendSet {
90 std::list<Result> results;
91 std::vector<ConfPtr> explaindblist;
93 class SPARQL::Session {
95 Session(const SPARQL *);
97 void handle_z(Package &package, Z_APDU *apdu);
98 Z_APDU *search(mp::Package &package,
101 const char *sparql_query,
103 FrontendSetPtr fset);
104 Z_APDU *explain_search(mp::Package &package,
107 const char *sparql_query,
108 FrontendSetPtr fset);
109 int invoke_sparql(mp::Package &package,
110 const char *sparql_query,
116 ODR odr, Odr_oid *preferredRecordSyntax,
117 Z_ElementSetNames *esn,
118 int start, int number, int &error_code, std::string &addinfo,
119 int *number_returned, int *next_position);
120 Z_Records *explain_fetch(
123 ODR odr, Odr_oid *preferredRecordSyntax,
124 Z_ElementSetNames *esn,
125 int start, int number, int &error_code, std::string &addinfo,
126 int *number_returned, int *next_position);
129 bool m_support_named_result_sets;
130 FrontendSets m_frontend_sets;
131 const SPARQL *m_sparql;
136 yf::SPARQL::Result::~Result()
142 yf::SPARQL::Result::Result()
147 yf::SPARQL::SPARQL() : m_p(new Rep)
151 yf::SPARQL::~SPARQL()
155 void yf::SPARQL::configure(const xmlNode *xmlnode, bool test_only,
158 const xmlNode *ptr = xmlnode->children;
161 for (; ptr; ptr = ptr->next)
163 if (ptr->type != XML_ELEMENT_NODE)
165 if (!strcmp((const char *) ptr->name, "defaults"))
167 const struct _xmlAttr *attr;
168 for (attr = ptr->properties; attr; attr = attr->next)
170 if (!strcmp((const char *) attr->name, "uri"))
171 uri = mp::xml::get_text(attr->children);
173 throw mp::filter::FilterException(
174 "Bad attribute " + std::string((const char *)
178 else if (!strcmp((const char *) ptr->name, "db"))
180 yaz_sparql_t s = yaz_sparql_create();
181 ConfPtr conf(new Conf);
185 const struct _xmlAttr *attr;
186 for (attr = ptr->properties; attr; attr = attr->next)
188 if (!strcmp((const char *) attr->name, "path"))
189 conf->db = mp::xml::get_text(attr->children);
190 else if (!strcmp((const char *) attr->name, "uri"))
191 conf->uri = mp::xml::get_text(attr->children);
192 else if (!strcmp((const char *) attr->name, "schema"))
193 conf->schema = mp::xml::get_text(attr->children);
194 else if (!strcmp((const char *) attr->name, "include"))
196 std::vector<std::string> dbs;
197 std::string db = mp::xml::get_text(attr->children);
198 boost::split(dbs, db, boost::is_any_of(" \t"));
200 for (i = 0; i < dbs.size(); i++)
202 if (dbs[i].length() == 0)
204 std::list<ConfPtr>::const_iterator it = db_conf.begin();
206 if (it == db_conf.end())
208 throw mp::filter::FilterException(
209 "include db not found: " + dbs[i]);
211 else if (dbs[i].compare((*it)->db) == 0)
213 yaz_sparql_include(s, (*it)->s);
221 throw mp::filter::FilterException(
222 "Bad attribute " + std::string((const char *)
225 xmlNode *p = ptr->children;
226 for (; p; p = p->next)
228 if (p->type != XML_ELEMENT_NODE)
230 std::string name = (const char *) p->name;
231 const struct _xmlAttr *attr;
232 for (attr = p->properties; attr; attr = attr->next)
234 if (!strcmp((const char *) attr->name, "type"))
237 name.append(mp::xml::get_text(attr->children));
240 throw mp::filter::FilterException(
241 "Bad attribute " + std::string((const char *)
244 std::string value = mp::xml::get_text(p);
245 if (yaz_sparql_add_pattern(s, name.c_str(), value.c_str()))
247 throw mp::filter::FilterException(
248 "Bad SPARQL config " + name);
251 if (!conf->uri.length())
253 throw mp::filter::FilterException("Missing uri");
255 if (!conf->db.length())
257 throw mp::filter::FilterException("Missing path");
259 db_conf.push_back(conf);
263 throw mp::filter::FilterException
265 + std::string((const char *) ptr->name)
266 + " in sparql filter");
271 yf::SPARQL::Conf::~Conf()
273 yaz_sparql_destroy(s);
276 yf::SPARQL::Session::Session(const SPARQL *sparql) :
278 m_support_named_result_sets(false),
283 yf::SPARQL::Session::~Session()
287 yf::SPARQL::SessionPtr yf::SPARQL::get_session(Package & package,
292 Z_GDU *gdu = package.request().get();
294 boost::mutex::scoped_lock lock(m_p->m_mutex);
296 std::map<mp::Session,SPARQL::SessionPtr>::iterator it;
298 if (gdu && gdu->which == Z_GDU_Z3950)
299 *apdu = gdu->u.z3950;
305 it = m_p->m_clients.find(package.session());
306 if (it == m_p->m_clients.end())
308 if (!it->second->m_in_use)
310 it->second->m_in_use = true;
313 m_p->m_cond_session_ready.wait(lock);
318 // new Z39.50 session ..
319 SessionPtr p(new Session(this));
320 m_p->m_clients[package.session()] = p;
324 void yf::SPARQL::release_session(Package &package) const
326 boost::mutex::scoped_lock lock(m_p->m_mutex);
327 std::map<mp::Session,SessionPtr>::iterator it;
329 it = m_p->m_clients.find(package.session());
330 if (it != m_p->m_clients.end())
332 it->second->m_in_use = false;
334 if (package.session().is_closed())
335 m_p->m_clients.erase(it);
336 m_p->m_cond_session_ready.notify_all();
340 static bool get_result(xmlDoc *doc, Odr_int *sz, Odr_int pos, xmlDoc **ndoc)
342 xmlNode *ptr = xmlDocGetRootElement(doc);
347 *ndoc = xmlNewDoc(BAD_CAST "1.0");
349 if (ptr->type == XML_ELEMENT_NODE &&
350 !strcmp((const char *) ptr->name, "RDF"))
354 q0 = xmlCopyNode(ptr, 2);
355 xmlDocSetRootElement(*ndoc, q0);
359 while (ptr && ptr->type != XML_ELEMENT_NODE)
361 if (ptr && ptr->type == XML_ELEMENT_NODE &&
362 !strcmp((const char *) ptr->name, "Description"))
364 xmlNode *p = ptr->children;
366 while (p && p->type != XML_ELEMENT_NODE)
368 if (p && p->type == XML_ELEMENT_NODE &&
369 !strcmp((const char *) p->name, "type"))
370 { /* SELECT RESULT */
371 for (ptr = ptr->children; ptr; ptr = ptr->next)
372 if (ptr->type == XML_ELEMENT_NODE &&
373 !strcmp((const char *) ptr->name, "solution"))
379 xmlNode *q1 = xmlCopyNode(ptr, 1);
387 { /* CONSTRUCT result */
388 for (; ptr; ptr = ptr->next)
389 if (ptr->type == XML_ELEMENT_NODE &&
390 !strcmp((const char *) ptr->name, "Description"))
396 xmlNode *q1 = xmlCopyNode(ptr, 1);
407 for (; ptr; ptr = ptr->next)
408 if (ptr->type == XML_ELEMENT_NODE &&
409 !strcmp((const char *) ptr->name, "sparql"))
415 q0 = xmlCopyNode(ptr, 2);
416 xmlDocSetRootElement(*ndoc, q0);
418 for (ptr = ptr->children; ptr; ptr = ptr->next)
419 if (ptr->type == XML_ELEMENT_NODE &&
420 !strcmp((const char *) ptr->name, "results"))
428 q1 = xmlCopyNode(ptr, 0);
431 for (ptr = ptr->children; ptr; ptr = ptr->next)
432 if (ptr->type == XML_ELEMENT_NODE &&
433 !strcmp((const char *) ptr->name, "result"))
439 xmlNode *q2 = xmlCopyNode(ptr, 1);
452 Z_Records *yf::SPARQL::Session::fetch(
455 ODR odr, Odr_oid *preferredRecordSyntax,
456 Z_ElementSetNames *esn,
457 int start, int number, int &error_code, std::string &addinfo,
458 int *number_returned, int *next_position)
460 Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
461 std::list<Result>::iterator it = fset->results.begin();
462 const char *schema = 0;
463 bool uri_lookup = false;
464 bool fetch_logged = false;
465 if (esn && esn->which == Z_ElementSetNames_generic)
466 schema = esn->u.generic;
468 for (; it != fset->results.end(); it++)
470 if (yaz_sparql_lookup_schema(it->conf->s, schema))
475 if (!schema || !strcmp(esn->u.generic, it->conf->schema.c_str()))
478 if (it == fset->results.end())
480 rec->which = Z_Records_NSD;
481 rec->u.nonSurrogateDiagnostic =
482 zget_DefaultDiagFormat(
484 YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_,
488 rec->which = Z_Records_DBOSD;
489 rec->u.databaseOrSurDiagnostics = (Z_NamePlusRecordList *)
490 odr_malloc(odr, sizeof(Z_NamePlusRecordList));
491 rec->u.databaseOrSurDiagnostics->records = (Z_NamePlusRecord **)
492 odr_malloc(odr, sizeof(Z_NamePlusRecord *) * number);
494 for (i = 0; i < number; i++)
496 rec->u.databaseOrSurDiagnostics->records[i] = (Z_NamePlusRecord *)
497 odr_malloc(odr, sizeof(Z_NamePlusRecord));
498 Z_NamePlusRecord *npr = rec->u.databaseOrSurDiagnostics->records[i];
499 npr->databaseName = odr_strdup(odr, fset->db.c_str());
500 npr->which = Z_NamePlusRecord_databaseRecord;
503 if (!get_result(it->doc, 0, start - 1 + i, &ndoc))
509 xmlNode *ndoc_root = xmlDocGetRootElement(ndoc);
518 xmlNode *n = ndoc_root;
521 if (n->type == XML_ELEMENT_NODE)
523 if (!strcmp((const char *) n->name, "uri") ||
524 !strcmp((const char *) n->name, "bnode") )
526 uri = mp::xml::get_text(n->children);
536 rec->which = Z_Records_NSD;
537 rec->u.nonSurrogateDiagnostic =
538 zget_DefaultDiagFormat(
540 YAZ_BIB1_SYSTEM_ERROR_IN_PRESENTING_RECORDS, 0);
546 mp::wrbuf addinfo, query, w;
547 int error = yaz_sparql_from_uri_wrbuf(it->conf->s,
549 uri.c_str(), schema);
553 { // Log the fetch query only once
554 package.log("sparql", YLOG_LOG,
555 "fetch query: for %s \n%s",
556 uri.c_str(), query.c_str() );
561 package.log("sparql", YLOG_LOG,
562 "fetch uri:%s", uri.c_str() );
564 error = invoke_sparql(package, query.c_str(),
569 rec->which = Z_Records_NSD;
570 rec->u.nonSurrogateDiagnostic =
571 zget_DefaultDiagFormat(
574 addinfo.len() ? addinfo.c_str() : 0);
578 npr->u.databaseRecord =
579 z_ext_record_xml(odr, w.c_str(), w.len());
584 xmlBufferPtr buf = xmlBufferCreate();
585 xmlNodeDump(buf, ndoc, ndoc_root, 0, 0);
586 yaz_log(YLOG_LOG, "record %s %.*s", uri_lookup ? "uri" : "normal",
587 (int) buf->use, (const char *) buf->content);
588 npr->u.databaseRecord =
589 z_ext_record_xml(odr, (const char *) buf->content, buf->use);
594 rec->u.databaseOrSurDiagnostics->num_records = i;
595 *number_returned = i;
596 if (start + number > fset->hits)
599 *next_position = start + number;
603 int yf::SPARQL::Session::invoke_sparql(mp::Package &package,
604 const char *sparql_query,
608 Package http_package(package.session(), package.origin());
611 http_package.copy_filter(package);
612 Z_GDU *gdu = z_get_HTTP_Request_uri(odr, conf->uri.c_str(), 0, 1);
614 z_HTTP_header_add(odr, &gdu->u.HTTP_Request->headers,
615 "Content-Type", "application/x-www-form-urlencoded");
616 z_HTTP_header_add(odr, &gdu->u.HTTP_Request->headers,
617 "Accept", "application/sparql-results+xml,"
618 "application/rdf+xml");
619 const char *names[2];
622 const char *values[1];
623 values[0] = sparql_query;
625 yaz_array_to_uri(&path, odr, (char **) names, (char **) values);
627 gdu->u.HTTP_Request->content_buf = path;
628 gdu->u.HTTP_Request->content_len = strlen(path);
630 yaz_log(YLOG_DEBUG, "sparql: HTTP request\n%s", sparql_query);
632 http_package.request() = gdu;
635 Z_GDU *gdu_resp = http_package.response().get();
637 if (!gdu_resp || gdu_resp->which != Z_GDU_HTTP_Response)
639 wrbuf_puts(w, "no HTTP response from backend");
640 return YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
642 else if (gdu_resp->u.HTTP_Response->code != 200)
644 Z_HTTP_Response *resp = gdu_resp->u.HTTP_Response;
645 wrbuf_printf(w, "sparql: HTTP error %d from backend",
647 package.log("sparql", YLOG_LOG,
648 "HTTP error %d from backend ",
650 package.log("sparql", YLOG_LOG,
651 "%.*s" , resp->content_len, resp->content_buf );
652 return YAZ_BIB1_TEMPORARY_SYSTEM_ERROR;
654 Z_HTTP_Response *resp = gdu_resp->u.HTTP_Response;
655 wrbuf_write(w, resp->content_buf, resp->content_len);
659 Z_Records *yf::SPARQL::Session::explain_fetch(
662 ODR odr, Odr_oid *preferredRecordSyntax,
663 Z_ElementSetNames *esn,
664 int start, int number, int &error_code, std::string &addinfo,
665 int *number_returned, int *next_position)
667 Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
668 rec->which = Z_Records_DBOSD;
669 rec->u.databaseOrSurDiagnostics = (Z_NamePlusRecordList *)
670 odr_malloc(odr, sizeof(Z_NamePlusRecordList));
671 rec->u.databaseOrSurDiagnostics->records = (Z_NamePlusRecord **)
672 odr_malloc(odr, sizeof(Z_NamePlusRecord *) * number);
674 for (i = 0; i < number; i++)
676 unsigned int idx = start + i - 1;
677 if ( idx >= fset->explaindblist.size() )
679 ConfPtr cp = fset->explaindblist[idx];
681 wrbuf_puts(w,"<info>\n");
682 wrbuf_puts(w," <databaseInfo>\n");
683 wrbuf_puts(w," <title>");
684 wrbuf_xmlputs(w, cp->db.c_str());
685 wrbuf_puts(w,"</title>\n");
686 wrbuf_puts(w," </databaseInfo>\n");
687 yaz_sparql_explain_indexes( cp->s, w, 2);
688 wrbuf_puts(w,"</info>\n");
690 rec->u.databaseOrSurDiagnostics->records[i] = (Z_NamePlusRecord *)
691 odr_malloc(odr, sizeof(Z_NamePlusRecord));
692 Z_NamePlusRecord *npr = rec->u.databaseOrSurDiagnostics->records[i];
693 npr->databaseName = odr_strdup(odr, fset->db.c_str());
694 npr->which = Z_NamePlusRecord_databaseRecord;
695 npr->u.databaseRecord =
696 z_ext_record_xml(odr, w.buf(), w.len() );
698 rec->u.databaseOrSurDiagnostics->num_records = i;
699 *number_returned = i;
700 if (start + number > (int)fset->explaindblist.size())
703 *next_position = start + number;
709 Z_APDU *yf::SPARQL::Session::explain_search(mp::Package &package,
712 const char *sparql_query,
715 Z_SearchRequest *req = apdu_req->u.searchRequest;
716 Z_APDU *apdu_res = 0;
719 package.log("sparql", YLOG_LOG, "Explain search" );
721 //std::list<std::string> dblist;
722 std::list<ConfPtr>::const_iterator it = m_sparql->db_conf.begin();
723 m_frontend_sets[req->resultSetName] = fset;
724 fset->explaindblist.clear();
725 fset->explaindblist.reserve(m_sparql->db_conf.size());
727 for (; it != m_sparql->db_conf.end(); it++)
728 if ((*it)->schema.length() > 0 ) // searchable db
731 package.log("sparql", YLOG_LOG, "Explain %d: '%s'",
732 numbases, (*it)->db.c_str() );
733 fset->explaindblist.push_back(*it);
735 int number_returned = 0;
736 int next_position = 0;
737 Z_Records *records = 0;
742 const char *element_set_name = 0;
743 mp::util::piggyback_sr(req, numbases, number, &element_set_name);
746 Z_ElementSetNames *esn;
748 if (number > *req->smallSetUpperBound)
749 esn = req->mediumSetElementSetNames;
751 esn = req->smallSetElementSetNames;
752 records = explain_fetch(package, fset,
753 odr, req->preferredRecordSyntax, esn,
762 apdu_res = odr.create_searchResponse(
763 apdu_req, error_code, addinfo.c_str());
767 apdu_res = odr.create_searchResponse(apdu_req, 0, 0);
768 Z_SearchResponse *resp = apdu_res->u.searchResponse;
769 *resp->resultCount = numbases;
770 *resp->numberOfRecordsReturned = number_returned;
771 *resp->nextResultSetPosition = next_position;
772 resp->records = records;
778 Z_APDU *yf::SPARQL::Session::search(mp::Package &package,
781 const char *sparql_query,
782 ConfPtr conf, FrontendSetPtr fset)
784 Z_SearchRequest *req = apdu_req->u.searchRequest;
785 Z_APDU *apdu_res = 0;
788 package.log("sparql", YLOG_LOG,
789 "search query:\n%s", sparql_query );
791 int error = invoke_sparql(package, sparql_query, conf, w);
794 apdu_res = odr.create_searchResponse(apdu_req, error,
800 xmlDocPtr doc = xmlParseMemory(w.c_str(), w.len());
803 apdu_res = odr.create_searchResponse(
805 YAZ_BIB1_TEMPORARY_SYSTEM_ERROR,
806 "invalid XML from backendbackend");
811 Z_Records *records = 0;
812 int number_returned = 0;
813 int next_position = 0;
819 fset->results.push_back(result);
820 yaz_log(YLOG_DEBUG, "saving sparql result xmldoc=%p", doc);
822 get_result(result.doc, &fset->hits, -1, 0);
823 m_frontend_sets[req->resultSetName] = fset;
828 const char *element_set_name = 0;
829 mp::util::piggyback_sr(req, fset->hits, number, &element_set_name);
832 Z_ElementSetNames *esn;
834 if (number > *req->smallSetUpperBound)
835 esn = req->mediumSetElementSetNames;
837 esn = req->smallSetElementSetNames;
838 records = fetch(package, fset,
839 odr, req->preferredRecordSyntax, esn,
848 odr.create_searchResponse(
849 apdu_req, error_code, addinfo.c_str());
854 odr.create_searchResponse(apdu_req, 0, 0);
855 Z_SearchResponse *resp = apdu_res->u.searchResponse;
856 *resp->resultCount = fset->hits;
857 *resp->numberOfRecordsReturned = number_returned;
858 *resp->nextResultSetPosition = next_position;
859 resp->records = records;
866 void yf::SPARQL::Session::handle_z(mp::Package &package, Z_APDU *apdu_req)
869 Z_APDU *apdu_res = 0;
870 if (apdu_req->which == Z_APDU_initRequest)
872 apdu_res = odr.create_initResponse(apdu_req, 0, 0);
873 Z_InitRequest *req = apdu_req->u.initRequest;
874 Z_InitResponse *resp = apdu_res->u.initResponse;
876 resp->implementationName = odr_strdup(odr, "sparql");
877 if (ODR_MASK_GET(req->options, Z_Options_namedResultSets))
878 m_support_named_result_sets = true;
880 static const int masks[] = {
881 Z_Options_search, Z_Options_present,
882 Z_Options_namedResultSets, -1
884 for (i = 0; masks[i] != -1; i++)
885 if (ODR_MASK_GET(req->options, masks[i]))
886 ODR_MASK_SET(resp->options, masks[i]);
887 static const int versions[] = {
893 for (i = 0; versions[i] != -1; i++)
894 if (ODR_MASK_GET(req->protocolVersion, versions[i]))
895 ODR_MASK_SET(resp->protocolVersion, versions[i]);
898 *resp->preferredMessageSize = *req->preferredMessageSize;
899 *resp->maximumRecordSize = *req->maximumRecordSize;
901 else if (apdu_req->which == Z_APDU_close)
903 apdu_res = odr.create_close(apdu_req,
904 Z_Close_finished, 0);
905 package.session().close();
907 else if (apdu_req->which == Z_APDU_searchRequest)
909 Z_SearchRequest *req = apdu_req->u.searchRequest;
911 FrontendSets::iterator fset_it =
912 m_frontend_sets.find(req->resultSetName);
913 if (fset_it != m_frontend_sets.end())
915 // result set already exist
916 // if replace indicator is off: we return diagnostic if
917 // result set already exist.
918 if (*req->replaceIndicator == 0)
921 odr.create_searchResponse(
923 YAZ_BIB1_RESULT_SET_EXISTS_AND_REPLACE_INDICATOR_OFF,
925 package.response() = apdu;
927 m_frontend_sets.erase(fset_it);
929 if (req->query->which != Z_Query_type_1)
931 apdu_res = odr.create_searchResponse(
932 apdu_req, YAZ_BIB1_QUERY_TYPE_UNSUPP, 0);
934 else if (req->num_databaseNames != 1)
936 apdu_res = odr.create_searchResponse(
938 YAZ_BIB1_ACCESS_TO_SPECIFIED_DATABASE_DENIED, 0);
942 std::string db = req->databaseNames[0];
943 std::list<ConfPtr>::const_iterator it;
944 FrontendSetPtr fset(new FrontendSet);
946 m_frontend_sets.erase(req->resultSetName);
950 it = m_sparql->db_conf.begin();
951 for (; it != m_sparql->db_conf.end(); it++)
952 if ((*it)->schema.length() > 0
953 && yaz_match_glob((*it)->db.c_str(), db.c_str()))
955 mp::wrbuf addinfo_wr;
958 yaz_sparql_from_rpn_wrbuf((*it)->s,
959 addinfo_wr, sparql_wr,
960 req->query->u.type_1);
963 apdu_res = odr.create_searchResponse(
965 addinfo_wr.len() ? addinfo_wr.c_str() : 0);
969 Z_APDU *apdu_1 = search(package, apdu_req, odr,
970 sparql_wr.c_str(), *it,
978 apdu_res = odr.create_searchResponse(
979 apdu_req, YAZ_BIB1_DATABASE_DOES_NOT_EXIST, db.c_str());
983 { // The magic "explain" base
984 yaz_log(YLOG_LOG,"About to call explain_search");
985 const char *qry = "query";
986 apdu_res = explain_search( package, apdu_req, odr,
988 // TODO - Extract at least a term from the query, and
989 // do some filtering by that
990 yaz_log(YLOG_LOG,"Returned from explain_search");
994 else if (apdu_req->which == Z_APDU_presentRequest)
996 Z_PresentRequest *req = apdu_req->u.presentRequest;
997 FrontendSets::iterator fset_it =
998 m_frontend_sets.find(req->resultSetId);
999 if (fset_it == m_frontend_sets.end())
1002 odr.create_presentResponse(
1003 apdu_req, YAZ_BIB1_SPECIFIED_RESULT_SET_DOES_NOT_EXIST,
1005 package.response() = apdu_res;
1008 int number_returned = 0;
1009 int next_position = 0;
1011 std::string addinfo;
1012 Z_ElementSetNames *esn = 0;
1013 if (req->recordComposition)
1015 if (req->recordComposition->which == Z_RecordComp_simple)
1016 esn = req->recordComposition->u.simple;
1020 odr.create_presentResponse(
1022 YAZ_BIB1_ONLY_A_SINGLE_ELEMENT_SET_NAME_SUPPORTED,
1024 package.response() = apdu_res;
1029 if ( fset_it->second->explaindblist.size() > 0 )
1030 records = explain_fetch(
1033 odr, req->preferredRecordSyntax, esn,
1034 *req->resultSetStartPoint, *req->numberOfRecordsRequested,
1035 error_code, addinfo,
1042 odr, req->preferredRecordSyntax, esn,
1043 *req->resultSetStartPoint, *req->numberOfRecordsRequested,
1044 error_code, addinfo,
1050 odr.create_presentResponse(apdu_req, error_code,
1056 odr.create_presentResponse(apdu_req, 0, 0);
1057 Z_PresentResponse *resp = apdu_res->u.presentResponse;
1058 resp->records = records;
1059 *resp->numberOfRecordsReturned = number_returned;
1060 *resp->nextResultSetPosition = next_position;
1065 apdu_res = odr.create_close(apdu_req,
1066 Z_Close_protocolError,
1067 "sparql: unhandled APDU");
1068 package.session().close();
1072 package.response() = apdu_res;
1075 void yf::SPARQL::process(mp::Package &package) const
1078 SessionPtr p = get_session(package, &apdu);
1081 p->handle_z(package, apdu);
1085 release_session(package);
1088 static mp::filter::Base* filter_creator()
1090 return new mp::filter::SPARQL;
1094 struct metaproxy_1_filter_struct metaproxy_1_filter_sparql = {
1105 * c-file-style: "Stroustrup"
1106 * indent-tabs-mode: nil
1108 * vim: shiftwidth=4 tabstop=8 expandtab