- yaz_log(YLOG_DEBUG, "srw_bend_search");
- if (!assoc->init)
- {
- yaz_log(YLOG_DEBUG, "srw_bend_init");
- if (!srw_bend_init(assoc))
- {
- srw_error = 3; /* assume Authentication error */
-
- yaz_add_srw_diagnostic(assoc->encode, &srw_res->diagnostics,
- &srw_res->num_diagnostics, 1, 0);
- yaz_log(log_request, "Search SRW: backend init failed");
- return;
- }
- }
-
- rr.setname = "default";
- rr.replace_set = 1;
- rr.num_bases = 1;
- rr.basenames = &srw_req->database;
- rr.referenceId = 0;
-
- rr.query = (Z_Query *) odr_malloc (assoc->decode, sizeof(*rr.query));
-
- if (srw_req->query_type == Z_SRW_query_type_cql)
- {
- ext = (Z_External *) odr_malloc(assoc->decode, sizeof(*ext));
- ext->direct_reference = odr_getoidbystr(assoc->decode,
- "1.2.840.10003.16.2");
- ext->indirect_reference = 0;
- ext->descriptor = 0;
- ext->which = Z_External_CQL;
- ext->u.cql = srw_req->query.cql;
- querystr = srw_req->query.cql;
- querytype = "CQL";
-
- rr.query->which = Z_Query_type_104;
- rr.query->u.type_104 = ext;
+ srw_bend_init(assoc, &srw_res->diagnostics, &srw_res->num_diagnostics);
+ if (srw_req->sort_type != Z_SRW_sort_type_none)
+ yaz_add_srw_diagnostic(assoc->encode, &srw_res->diagnostics,
+ &srw_res->num_diagnostics, 80, 0);
+ else if (srw_res->num_diagnostics == 0 && assoc->init)
+ {
+ bend_search_rr rr;
+ rr.setname = "default";
+ rr.replace_set = 1;
+ rr.num_bases = 1;
+ rr.basenames = &srw_req->database;
+ rr.referenceId = 0;
+
+ rr.query = (Z_Query *) odr_malloc (assoc->decode, sizeof(*rr.query));
+ rr.query->u.type_1 = 0;
+
+ if (srw_req->query_type == Z_SRW_query_type_cql)
+ {
+ if (assoc->cql_transform)
+ {
+ int srw_errcode = cql2pqf(assoc->encode, srw_req->query.cql,
+ assoc->cql_transform, rr.query);
+ if (srw_errcode)
+ {
+ yaz_add_srw_diagnostic(assoc->encode,
+ &srw_res->diagnostics,
+ &srw_res->num_diagnostics,
+ srw_errcode, 0);
+ }
+ }
+ else
+ {
+ /* CQL query to backend. Wrap it - Z39.50 style */
+ ext = (Z_External *) odr_malloc(assoc->decode, sizeof(*ext));
+ ext->direct_reference = odr_getoidbystr(assoc->decode,
+ "1.2.840.10003.16.2");
+ ext->indirect_reference = 0;
+ ext->descriptor = 0;
+ ext->which = Z_External_CQL;
+ ext->u.cql = srw_req->query.cql;
+
+ rr.query->which = Z_Query_type_104;
+ rr.query->u.type_104 = ext;
+ }
+ }
+ else if (srw_req->query_type == Z_SRW_query_type_pqf)
+ {
+ Z_RPNQuery *RPNquery;
+ YAZ_PQF_Parser pqf_parser;
+
+ pqf_parser = yaz_pqf_create ();
+
+ RPNquery = yaz_pqf_parse (pqf_parser, assoc->decode,
+ srw_req->query.pqf);
+ if (!RPNquery)
+ {
+ const char *pqf_msg;
+ size_t off;
+ int code = yaz_pqf_error (pqf_parser, &pqf_msg, &off);
+ yaz_log(log_requestdetail, "Parse error %d %s near offset %d",
+ code, pqf_msg, off);
+ srw_error = 10;
+ }
+
+ rr.query->which = Z_Query_type_1;
+ rr.query->u.type_1 = RPNquery;
+
+ yaz_pqf_destroy (pqf_parser);
+ }
+ else
+ {
+ yaz_add_srw_diagnostic(assoc->encode, &srw_res->diagnostics,
+ &srw_res->num_diagnostics, 11, 0);
+ }
+ if (rr.query->u.type_1)
+ {
+ rr.stream = assoc->encode;
+ rr.decode = assoc->decode;
+ rr.print = assoc->print;
+ rr.request = req;
+ rr.association = assoc;
+ rr.fd = 0;
+ rr.hits = 0;
+ rr.errcode = 0;
+ rr.errstring = 0;
+ rr.search_info = 0;
+ yaz_log_zquery_level(log_requestdetail,rr.query);
+
+ (assoc->init->bend_search)(assoc->backend, &rr);
+ if (rr.errcode)
+ {
+ if (rr.errcode == 109) /* database unavailable */
+ {
+ *http_code = 404;
+ }
+ else
+ {
+ srw_error = yaz_diag_bib1_to_srw (rr.errcode);
+ yaz_add_srw_diagnostic(assoc->encode,
+ &srw_res->diagnostics,
+ &srw_res->num_diagnostics,
+ srw_error, rr.errstring);
+ }
+ }
+ else
+ {
+ int number = srw_req->maximumRecords ? *srw_req->maximumRecords : 0;
+ int start = srw_req->startRecord ? *srw_req->startRecord : 1;
+
+ yaz_log(log_requestdetail, "Request to pack %d+%d out of %d",
+ start, number, rr.hits);
+
+ srw_res->numberOfRecords = odr_intdup(assoc->encode, rr.hits);
+ if (number > 0)
+ {
+ int i;
+
+ if (start > rr.hits)
+ {
+ yaz_add_srw_diagnostic(assoc->encode, &srw_res->diagnostics,
+ &srw_res->num_diagnostics,
+ 61, 0);
+ }
+ else
+ {
+ int j = 0;
+ int packing = Z_SRW_recordPacking_string;
+ if (start + number > rr.hits)
+ number = rr.hits - start + 1;
+ if (srw_req->recordPacking &&
+ !strcmp(srw_req->recordPacking, "xml"))
+ packing = Z_SRW_recordPacking_XML;
+ srw_res->records = (Z_SRW_record *)
+ odr_malloc(assoc->encode,
+ number * sizeof(*srw_res->records));
+ for (i = 0; i<number; i++)
+ {
+ int errcode;
+
+ srw_res->records[j].recordPacking = packing;
+ srw_res->records[j].recordData_buf = 0;
+ yaz_log(YLOG_DEBUG, "srw_bend_fetch %d", i+start);
+ errcode = srw_bend_fetch(assoc, i+start, srw_req,
+ srw_res->records + j);
+ if (errcode)
+ {
+ yaz_add_srw_diagnostic(assoc->encode,
+ &srw_res->diagnostics,
+ &srw_res->num_diagnostics,
+ yaz_diag_bib1_to_srw (errcode),
+ rr.errstring);
+
+ break;
+ }
+ if (srw_res->records[j].recordData_buf)
+ j++;
+ }
+ srw_res->num_records = j;
+ if (!j)
+ srw_res->records = 0;
+ }
+ }
+ }
+ }