+static bool get_result(xmlDoc *doc, Odr_int *sz, Odr_int pos,
+ xmlDoc **ndoc)
+{
+ xmlNode *ptr = xmlDocGetRootElement(doc);
+ xmlNode *q0;
+ Odr_int cur = 0;
+
+ if (ndoc)
+ *ndoc = xmlNewDoc(BAD_CAST "1.0");
+
+ if (ptr->type == XML_ELEMENT_NODE &&
+ !strcmp((const char *) ptr->name, "RDF"))
+ {
+ if (ndoc)
+ {
+ q0 = xmlCopyNode(ptr, 2);
+ xmlDocSetRootElement(*ndoc, q0);
+ }
+ ptr = ptr->children;
+
+ while (ptr && ptr->type != XML_ELEMENT_NODE)
+ ptr = ptr->next;
+ if (ptr && ptr->type == XML_ELEMENT_NODE &&
+ !strcmp((const char *) ptr->name, "Description"))
+ {
+ xmlNode *p = ptr->children;
+
+ while (p && p->type != XML_ELEMENT_NODE)
+ p = p->next;
+ if (p && p->type == XML_ELEMENT_NODE &&
+ !strcmp((const char *) p->name, "type"))
+ { /* SELECT RESULT */
+ for (ptr = ptr->children; ptr; ptr = ptr->next)
+ if (ptr->type == XML_ELEMENT_NODE &&
+ !strcmp((const char *) ptr->name, "solution"))
+ {
+ if (cur++ == pos)
+ {
+ if (ndoc)
+ {
+ xmlNode *q1 = xmlCopyNode(ptr, 1);
+ xmlAddChild(q0, q1);
+ }
+ break;
+ }
+ }
+ }
+ else
+ { /* CONSTRUCT result */
+ for (; ptr; ptr = ptr->next)
+ if (ptr->type == XML_ELEMENT_NODE &&
+ !strcmp((const char *) ptr->name, "Description"))
+ {
+ if (cur++ == pos)
+ {
+ if (ndoc)
+ {
+ xmlNode *q1 = xmlCopyNode(ptr, 1);
+ xmlAddChild(q0, q1);
+ }
+ return true;
+ }
+ }
+ }
+ }
+ }
+ else
+ {
+ for (; ptr; ptr = ptr->next)
+ if (ptr->type == XML_ELEMENT_NODE &&
+ !strcmp((const char *) ptr->name, "sparql"))
+ break;
+ if (ptr)
+ {
+ if (ndoc)
+ {
+ q0 = xmlCopyNode(ptr, 2);
+ xmlDocSetRootElement(*ndoc, q0);
+ }
+ for (ptr = ptr->children; ptr; ptr = ptr->next)
+ if (ptr->type == XML_ELEMENT_NODE &&
+ !strcmp((const char *) ptr->name, "results"))
+ break;
+ }
+ if (ptr)
+ {
+ xmlNode *q1 = 0;
+ if (ndoc)
+ {
+ q1 = xmlCopyNode(ptr, 0);
+ xmlAddChild(q0, q1);
+ }
+ for (ptr = ptr->children; ptr; ptr = ptr->next)
+ if (ptr->type == XML_ELEMENT_NODE &&
+ !strcmp((const char *) ptr->name, "result"))
+ {
+ if (cur++ == pos)
+ {
+ if (ndoc)
+ {
+ xmlNode *q2 = xmlCopyNode(ptr, 1);
+ xmlAddChild(q1, q2);
+ }
+ return true;
+ }
+ }
+ }
+ }
+ if (sz)
+ *sz = cur;
+ return false;
+}
+
+Z_Records *yf::SPARQL::Session::fetch(
+ FrontendSetPtr fset,
+ ODR odr, Odr_oid *preferredRecordSyntax,
+ Z_ElementSetNames *esn,
+ int start, int number, int &error_code, std::string &addinfo,
+ int *number_returned, int *next_position)
+{
+ Z_Records *rec = (Z_Records *) odr_malloc(odr, sizeof(Z_Records));
+ if (esn && esn->which == Z_ElementSetNames_generic &&
+ fset->conf->schema.length())
+ {
+ if (strcmp(esn->u.generic, fset->conf->schema.c_str()))
+ {
+ rec->which = Z_Records_NSD;
+ rec->u.nonSurrogateDiagnostic =
+ zget_DefaultDiagFormat(
+ odr,
+ YAZ_BIB1_SPECIFIED_ELEMENT_SET_NAME_NOT_VALID_FOR_SPECIFIED_,
+ esn->u.generic);
+ return rec;
+ }
+ }
+ rec->which = Z_Records_DBOSD;
+ rec->u.databaseOrSurDiagnostics = (Z_NamePlusRecordList *)
+ odr_malloc(odr, sizeof(Z_NamePlusRecordList));
+ rec->u.databaseOrSurDiagnostics->records = (Z_NamePlusRecord **)
+ odr_malloc(odr, sizeof(Z_NamePlusRecord *) * number);
+ int i;
+ for (i = 0; i < number; i++)
+ {
+ rec->u.databaseOrSurDiagnostics->records[i] = (Z_NamePlusRecord *)
+ odr_malloc(odr, sizeof(Z_NamePlusRecord));
+ Z_NamePlusRecord *npr = rec->u.databaseOrSurDiagnostics->records[i];
+ npr->databaseName = odr_strdup(odr, fset->db.c_str());
+ npr->which = Z_NamePlusRecord_databaseRecord;
+ xmlDoc *ndoc = 0;
+
+ if (!get_result(fset->doc, 0, start - 1 + i, &ndoc))
+ {
+ if (ndoc)
+ xmlFreeDoc(ndoc);
+ break;
+ }
+ xmlNode *ndoc_root = xmlDocGetRootElement(ndoc);
+ if (!ndoc_root)
+ {
+ xmlFreeDoc(ndoc);
+ break;
+ }
+ xmlBufferPtr buf = xmlBufferCreate();
+ xmlNodeDump(buf, ndoc, ndoc_root, 0, 0);
+ npr->u.databaseRecord =
+ z_ext_record_xml(odr, (const char *) buf->content, buf->use);
+ xmlFreeDoc(ndoc);
+ xmlBufferFree(buf);
+ }
+ rec->u.databaseOrSurDiagnostics->num_records = i;
+ *number_returned = i;
+ if (start + number > fset->hits)
+ *next_position = 0;
+ else
+ *next_position = start + number;
+ return rec;
+}
+