Merge branch 'master' of /home/quinn/proj/pazpar2
authorAdam Dickmeiss <adam@indexdata.dk>
Sun, 27 Jul 2008 16:01:14 +0000 (16:01 +0000)
committerAdam Dickmeiss <adam@indexdata.dk>
Sun, 27 Jul 2008 16:01:14 +0000 (16:01 +0000)
1  2 
src/client.c

diff --combined src/client.c
@@@ -58,6 -58,7 +58,7 @@@ Foundation, Inc., 51 Franklin St, Fift
  #include <yaz/oid_db.h>
  #include <yaz/diagbib1.h>
  #include <yaz/snprintf.h>
+ #include <yaz/rpn2cql.h>
  
  #define USE_TIMING 0
  #if USE_TIMING
@@@ -80,6 -81,7 +81,7 @@@ struct client 
      struct connection *connection;
      struct session *session;
      char *pquery; // Current search
+     char *cqlquery; // used for SRU targets only
      int hits;
      int records;
      int setno;
@@@ -311,6 -313,7 +313,6 @@@ static int nativesyntax_to_type(struct 
          }
          else
              return -1;
 -        yaz_log(YLOG_LOG, "Returned type %s", type);
          return 0;
      }
      return -1;
@@@ -377,6 -380,8 +379,6 @@@ void client_record_response(struct clie
          ZOOM_record rec = 0;
          const char *msg, *addinfo;
          
 -        yaz_log(YLOG_LOG, "show_raw=%p show_raw->active=%d",
 -                cl->show_raw, cl->show_raw ? cl->show_raw->active : 0);
          if (cl->show_raw && cl->show_raw->active)
          {
              if ((rec = ZOOM_resultset_record(resultset,
@@@ -436,6 -441,7 +438,7 @@@ void client_start_search(struct client 
      const char *opt_elements = session_setting_oneval(sdb, PZ_ELEMENTS);
      const char *opt_requestsyn = session_setting_oneval(sdb, PZ_REQUESTSYNTAX);
      const char *opt_maxrecs = session_setting_oneval(sdb, PZ_MAXRECS);
+     const char *opt_sru = session_setting_oneval(sdb, PZ_SRU);
  
      assert(link);
  
          ZOOM_connection_option_set(link, "piggyback", "1");
      if (*opt_queryenc)
          ZOOM_connection_option_set(link, "rpnCharset", opt_queryenc);
-     if (*opt_elements)
+     if (*opt_sru && *opt_elements)
+         ZOOM_connection_option_set(link, "schema", opt_elements);
+     else if (*opt_elements)
          ZOOM_connection_option_set(link, "elementSetName", opt_elements);
      if (*opt_requestsyn)
          ZOOM_connection_option_set(link, "preferredRecordSyntax", opt_requestsyn);
  
      ZOOM_connection_option_set(link, "presentChunk", "20");
  
-     rs = ZOOM_connection_search_pqf(link, cl->pquery);
+     if (cl->cqlquery)
+     {
+         ZOOM_query q = ZOOM_query_create();
+         ZOOM_query_cql(q, cl->cqlquery);
+         rs = ZOOM_connection_search(link, q);
+     }
+     else
+         rs = ZOOM_connection_search_pqf(link, cl->pquery);
      connection_set_resultset(co, rs);
      connection_continue(co);
  }
@@@ -483,6 -498,7 +495,7 @@@ struct client *client_create(void
      else
          r = xmalloc(sizeof(struct client));
      r->pquery = 0;
+     r->cqlquery = 0;
      r->database = 0;
      r->connection = 0;
      r->session = 0;
@@@ -511,6 -527,7 +524,7 @@@ void client_destroy(struct client *c
              cc->next = c->next;
      }
      xfree(c->pquery);
+     xfree(c->cqlquery);
  
      if (c->connection)
          connection_release(c->connection);
@@@ -564,13 -581,38 +578,38 @@@ static CCL_bibset prepare_cclmap(struc
      return res;
  }
  
+ // returns a xmalloced CQL query corresponding to the pquery in client
+ static char *make_cqlquery(struct client *cl)
+ {
+     cql_transform_t cqlt = cql_transform_create();
+     Z_RPNQuery *zquery;
+     char *r;
+     WRBUF wrb = wrbuf_alloc();
+     int status;
+     zquery = p_query_rpn(global_parameters.odr_out, cl->pquery);
+     if ((status = cql_transform_rpn2cql_wrbuf(cqlt, wrb, zquery)))
+     {
+         yaz_log(YLOG_WARN, "failed to generate CQL query, code=%d", status);
+         return 0;
+     }
+     r = xstrdup(wrbuf_cstr(wrb));
+     wrbuf_destroy(wrb);
+     odr_reset(global_parameters.odr_out); // releases the zquery
+     cql_transform_close(cqlt);
+     return r;
+ }
  // Parse the query given the settings specific to this client
  int client_parse_query(struct client *cl, const char *query)
  {
      struct session *se = client_get_session(cl);
+     struct session_database *sdb = client_get_database(cl);
      struct ccl_rpn_node *cn;
      int cerror, cpos;
      CCL_bibset ccl_map = prepare_cclmap(cl);
+     const char *sru = session_setting_oneval(sdb, PZ_SRU);
  
      if (!ccl_map)
          return -1;
      xfree(cl->pquery);
      cl->pquery = xstrdup(wrbuf_cstr(se->wrbuf));
  
+     xfree(cl->cqlquery);
+     if (*sru)
+     {
+         if (!(cl->cqlquery = make_cqlquery(cl)))
+             return -1;
+     }
+     else
+         cl->cqlquery = 0;
      if (!se->relevance)
      {
          // Initialize relevance structure with query terms