Checkout submodules as necessary MPSPARQL-23
[mp-sparql-moved-to-github.git] / src / filter_sparql.cpp
index 2ff3cf4..a38567d 100644 (file)
@@ -22,6 +22,7 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 #include <yaz/srw.h>
 #include <yaz/diagbib1.h>
 #include <yaz/match_glob.h>
+#include <yaz/querytowrbuf.h>
 #include <boost/scoped_ptr.hpp>
 #include <boost/thread/mutex.hpp>
 #include <boost/thread/condition.hpp>
@@ -673,18 +674,19 @@ Z_Records *yf::SPARQL::Session::explain_fetch(
     int i;
     for (i = 0; i < number; i++)
     {
-        int idx = start + i - 1;
-        ConfPtr cp = fset->explaindblist[ idx];
-        package.log("sparql", YLOG_LOG, "fetch explain %d:%s", idx, cp->db.c_str() );
+        unsigned int idx = start + i - 1;
+        if ( idx >= fset->explaindblist.size() )
+            break; 
+        ConfPtr cp = fset->explaindblist[idx];
         mp::wrbuf w;
-        wrbuf_puts(w,"<explain xmlns=\"http://explain.z3950.org/dtd/2.0/\">\n");
+        wrbuf_puts(w,"<info>\n");
         wrbuf_puts(w,"  <databaseInfo>\n");
         wrbuf_puts(w,"    <title>");
         wrbuf_xmlputs(w, cp->db.c_str());
         wrbuf_puts(w,"</title>\n");
         wrbuf_puts(w,"  </databaseInfo>\n");
         yaz_sparql_explain_indexes( cp->s, w, 2);
-        wrbuf_puts(w,"</explain>\n");
+        wrbuf_puts(w,"</info>\n");
 
         rec->u.databaseOrSurDiagnostics->records[i] = (Z_NamePlusRecord *)
             odr_malloc(odr, sizeof(Z_NamePlusRecord));
@@ -696,7 +698,7 @@ Z_Records *yf::SPARQL::Session::explain_fetch(
     }
     rec->u.databaseOrSurDiagnostics->num_records = i;
     *number_returned = i;
-    if (start + number > fset->hits)
+    if (start + number > (int)fset->explaindblist.size())
         *next_position = 0;
     else
         *next_position = start + number;
@@ -708,42 +710,36 @@ Z_Records *yf::SPARQL::Session::explain_fetch(
 Z_APDU *yf::SPARQL::Session::explain_search(mp::Package &package,
                            Z_APDU *apdu_req,
                            mp::odr &odr,
-                           const char *sparql_query,
+                           const char *explain_query,
                            FrontendSetPtr fset)
 {
     Z_SearchRequest *req = apdu_req->u.searchRequest;
     Z_APDU *apdu_res = 0;
     //mp::wrbuf w;
 
-    package.log("sparql", YLOG_LOG, "Explain search" );
+    package.log("sparql", YLOG_LOG, "Explain search '%s'", explain_query );
+    const char *term = explain_query + strlen(explain_query);
+    while ( term > explain_query && *term != ' ')
+        term--;
+    term++;
+    if ( ! isalpha( *term) )
+        term=""; // anything non-alpha is taken to mean all 
+                 // Empty string is seen here as two double quotes ""
+                 // so it returns all bases as well
     int numbases = 0;
-    //std::list<std::string> dblist;
     std::list<ConfPtr>::const_iterator it = m_sparql->db_conf.begin();
     m_frontend_sets[req->resultSetName] = fset;
     fset->explaindblist.clear();
     fset->explaindblist.reserve(m_sparql->db_conf.size());
 
     for (; it != m_sparql->db_conf.end(); it++)
-        if ((*it)->schema.length() > 0 )  // searchable db
-        {
+        if ( (*it)->schema.length() > 0  &&  // searchable db
+            (!*term || strcmp(term,(*it)->db.c_str())==0)  )
+        { // and want all, or found the matching one
             numbases++;
             package.log("sparql", YLOG_LOG, "Explain %d: '%s'",
                         numbases, (*it)->db.c_str() );
             fset->explaindblist.push_back(*it);
-/*
-            //yf::SPARQL::Result res;
-            //res.conf = *it;
-            std::string z =
-              "<explain xmlns='http://explain.z3950.org/dtd/2.0/'>"
-                "<databaseInfo>"
-                  "<title>" +
-                    (*it)->db +
-                  "</title>"
-                "</databaseInfo>"
-              "</explain>";
-            //res.doc = xmlParseMemory(z.c_str(), z.size());
-            dblist.push_back(z);
-*/
         }
     int number_returned = 0;
     int next_position = 0;
@@ -788,6 +784,7 @@ Z_APDU *yf::SPARQL::Session::explain_search(mp::Package &package,
     return apdu_res;
 }
 
+
 Z_APDU *yf::SPARQL::Session::search(mp::Package &package,
                                     Z_APDU *apdu_req,
                                     mp::odr &odr,
@@ -958,7 +955,7 @@ void yf::SPARQL::Session::handle_z(mp::Package &package, Z_APDU *apdu_req)
 
             m_frontend_sets.erase(req->resultSetName);
             fset->db = db;
-            if ( db != "explain" )
+            if ( db != "info" )
             {
                 it = m_sparql->db_conf.begin();
                 for (; it != m_sparql->db_conf.end(); it++)
@@ -995,9 +992,10 @@ void yf::SPARQL::Session::handle_z(mp::Package &package, Z_APDU *apdu_req)
             else
             { // The magic "explain" base
                 yaz_log(YLOG_LOG,"About to call explain_search");
-                const char *qry = "query";
+                mp::wrbuf qry;
+                yaz_query_to_wrbuf(qry, req->query);
                 apdu_res = explain_search( package, apdu_req, odr,
-                                           qry, fset);
+                                           qry.c_str(), fset);
                   // TODO - Extract at least a term from the query, and
                   // do some filtering by that
                 yaz_log(YLOG_LOG,"Returned from explain_search");