+ if (assoc->server && assoc->server->cql_transform)
+ {
+ int srw_errcode = cql2pqf(assoc->encode, srw_req->query.cql,
+ assoc->server->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 %ld",
+ code, pqf_msg, (long) off);
+ srw_error = YAZ_SRW_QUERY_SYNTAX_ERROR;
+ }
+
+ 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,
+ YAZ_SRW_UNSUPP_QUERY_TYPE, 0);
+ }
+ if (rr.query->u.type_1)
+ {
+ rr.stream = assoc->encode;
+ rr.decode = assoc->decode;
+ rr.print = assoc->print;
+ rr.request = req;
+ if ( srw_req->sort.sortKeys )
+ rr.srw_sortKeys = odr_strdup(assoc->encode,
+ srw_req->sort.sortKeys );
+ 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 == YAZ_BIB1_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 (rr.srw_setname)
+ {
+ srw_res->resultSetId =
+ odr_strdup(assoc->encode, rr.srw_setname );
+ srw_res->resultSetIdleTime =
+ odr_intdup(assoc->encode, *rr.srw_setnameIdleTime );
+ }
+
+ if ((rr.hits > 0 && start > rr.hits) || start < 1)
+ {
+ yaz_add_srw_diagnostic(
+ assoc->encode,
+ &srw_res->diagnostics, &srw_res->num_diagnostics,
+ YAZ_SRW_FIRST_RECORD_POSITION_OUT_OF_RANGE, 0);
+ }
+ else if (number > 0)
+ {
+ int i;
+ int ok = 1;
+ if (start + number > rr.hits)
+ number = rr.hits - start + 1;
+
+ /* Call bend_present if defined */
+ if (assoc->init->bend_present)
+ {
+ bend_present_rr *bprr = (bend_present_rr*)
+ odr_malloc (assoc->decode, sizeof(*bprr));
+ bprr->setname = "default";
+ bprr->start = start;
+ bprr->number = number;
+ bprr->format = VAL_TEXT_XML;
+ if (srw_req->recordSchema)
+ {
+ bprr->comp = (Z_RecordComposition *) odr_malloc(assoc->decode,
+ sizeof(*bprr->comp));
+ bprr->comp->which = Z_RecordComp_simple;
+ bprr->comp->u.simple = (Z_ElementSetNames *)
+ odr_malloc(assoc->decode, sizeof(Z_ElementSetNames));
+ bprr->comp->u.simple->which = Z_ElementSetNames_generic;
+ bprr->comp->u.simple->u.generic = srw_req->recordSchema;
+ }
+ else
+ {
+ bprr->comp = 0;
+ }
+ bprr->stream = assoc->encode;
+ bprr->referenceId = 0;
+ bprr->print = assoc->print;
+ bprr->request = req;
+ bprr->association = assoc;
+ bprr->errcode = 0;
+ bprr->errstring = NULL;
+ (*assoc->init->bend_present)(assoc->backend, bprr);
+
+ if (!bprr->request)
+ return;
+ if (bprr->errcode)
+ {
+ srw_error = yaz_diag_bib1_to_srw (bprr->errcode);
+ yaz_add_srw_diagnostic(assoc->encode,
+ &srw_res->diagnostics,
+ &srw_res->num_diagnostics,
+ srw_error, bprr->errstring);
+ ok = 0;
+ }
+ }
+
+ if (ok)
+ {
+ int j = 0;
+ int packing = Z_SRW_recordPacking_string;
+ if (srw_req->recordPacking)
+ {
+ packing =
+ yaz_srw_str_to_pack(srw_req->recordPacking);
+ if (packing == -1)
+ packing = Z_SRW_recordPacking_string;
+ }
+ srw_res->records = (Z_SRW_record *)
+ odr_malloc(assoc->encode,
+ number * sizeof(*srw_res->records));
+
+ srw_res->extra_records = (Z_SRW_extra_record **)
+ odr_malloc(assoc->encode,
+ number*sizeof(*srw_res->extra_records));
+
+ for (i = 0; i<number; i++)
+ {
+ int errcode;
+ const char *addinfo = 0;
+
+ srw_res->records[j].recordPacking = packing;
+ srw_res->records[j].recordData_buf = 0;
+ srw_res->extra_records[j] = 0;
+ yaz_log(YLOG_DEBUG, "srw_bend_fetch %d", i+start);
+ errcode = srw_bend_fetch(assoc, i+start, srw_req,
+ srw_res->records + j,
+ &addinfo);
+ if (errcode)
+ {
+ yaz_add_srw_diagnostic(assoc->encode,
+ &srw_res->diagnostics,
+ &srw_res->num_diagnostics,
+ yaz_diag_bib1_to_srw (errcode),
+ addinfo);
+
+ break;
+ }
+ if (srw_res->records[j].recordData_buf)
+ j++;
+ }
+ srw_res->num_records = j;
+ if (!j)
+ srw_res->records = 0;
+ }
+ }
+ if (rr.estimated_hit_count || rr.partial_resultset)
+ {
+ yaz_add_srw_diagnostic(
+ assoc->encode,
+ &srw_res->diagnostics,
+ &srw_res->num_diagnostics,
+ YAZ_SRW_RESULT_SET_CREATED_WITH_VALID_PARTIAL_RESULTS_AVAILABLE,
+ 0);
+ }
+ }