2 * Copyright (C) 1995-2005, Index Data ApS
5 * $Id: querytowrbuf.c,v 1.4 2006-04-19 10:05:03 adam Exp $
8 /** \file querytowrbuf.c
9 \brief Query to WRBUF (to strings)
15 #include <yaz/logrpn.h>
16 #include <yaz/querytowrbuf.h>
18 static void yaz_term_to_wrbuf(WRBUF b, const char *term, int len)
21 for (i = 0; i < len; i++)
22 if (strchr(" \"{", term[i]))
25 wrbuf_printf(b, "%.*s ", len, term);
29 for (i = 0; i<len; i++)
33 wrbuf_putc(b, term[i]);
35 wrbuf_printf(b, "\" ");
39 static void yaz_attribute_element_to_wrbuf(WRBUF b,
40 const Z_AttributeElement *element)
44 char *sep = ""; /* optional space after attrset name */
45 if (element->attributeSet)
48 attrset = oid_getentbyoid (element->attributeSet);
49 setname = attrset->desc;
52 switch (element->which)
54 case Z_AttributeValue_numeric:
55 wrbuf_printf(b,"@attr %s%s%d=%d ", setname, sep,
56 *element->attributeType, *element->value.numeric);
58 case Z_AttributeValue_complex:
59 wrbuf_printf(b,"@attr %s%s\"%d=", setname, sep,
60 *element->attributeType);
61 for (i = 0; i<element->value.complex->num_list; i++)
65 if (element->value.complex->list[i]->which ==
66 Z_StringOrNumeric_string)
67 wrbuf_printf (b, "%s",
68 element->value.complex->list[i]->u.string);
69 else if (element->value.complex->list[i]->which ==
70 Z_StringOrNumeric_numeric)
71 wrbuf_printf (b, "%d",
72 *element->value.complex->list[i]->u.numeric);
74 wrbuf_printf(b, "\" ");
77 wrbuf_printf (b, "@attr 1=unknown ");
81 static const char *complex_op_name(const Z_Operator *op)
89 case Z_Operator_and_not:
94 return "unknown complex operator";
98 static void yaz_apt_to_wrbuf(WRBUF b, const Z_AttributesPlusTerm *zapt)
100 int num_attributes = zapt->attributes->num_attributes;
102 for (i = 0; i<num_attributes; i++)
103 yaz_attribute_element_to_wrbuf(b,zapt->attributes->attributes[i]);
105 switch (zapt->term->which)
108 yaz_term_to_wrbuf(b, (const char *)zapt->term->u.general->buf,
109 zapt->term->u.general->len);
111 case Z_Term_characterString:
112 wrbuf_printf(b, "@term string ");
113 yaz_term_to_wrbuf(b, zapt->term->u.characterString,
114 strlen(zapt->term->u.characterString));
117 wrbuf_printf(b, "@term numeric %d ", *zapt->term->u.numeric);
120 wrbuf_printf(b, "@term null x");
123 wrbuf_printf(b, "@term null unknown%d ", zapt->term->which);
127 static void yaz_rpnstructure_to_wrbuf(WRBUF b, const Z_RPNStructure *zs)
129 if (zs->which == Z_RPNStructure_complex)
131 Z_Operator *op = zs->u.complex->roperator;
132 wrbuf_printf(b, "@%s ", complex_op_name(op) );
133 if (op->which== Z_Operator_prox)
135 if (!op->u.prox->exclusion)
137 else if (*op->u.prox->exclusion)
142 wrbuf_printf(b, " %d %d %d ", *op->u.prox->distance,
143 *op->u.prox->ordered,
144 *op->u.prox->relationType);
146 switch(op->u.prox->which)
148 case Z_ProximityOperator_known:
151 case Z_ProximityOperator_private:
155 wrbuf_printf(b, "%d", op->u.prox->which);
157 if (op->u.prox->u.known)
158 wrbuf_printf(b, " %d ", *op->u.prox->u.known);
160 wrbuf_printf(b, " 0 ");
162 yaz_rpnstructure_to_wrbuf(b,zs->u.complex->s1);
163 yaz_rpnstructure_to_wrbuf(b,zs->u.complex->s2);
165 else if (zs->which == Z_RPNStructure_simple)
167 if (zs->u.simple->which == Z_Operand_APT)
168 yaz_apt_to_wrbuf(b, zs->u.simple->u.attributesPlusTerm);
169 else if (zs->u.simple->which == Z_Operand_resultSetId)
171 wrbuf_printf(b, "@set ");
172 yaz_term_to_wrbuf(b, zs->u.simple->u.resultSetId,
173 strlen(zs->u.simple->u.resultSetId));
176 wrbuf_printf (b, "(unknown simple structure)");
179 wrbuf_puts(b, "(unknown structure)");
182 void yaz_rpnquery_to_wrbuf(WRBUF b, const Z_RPNQuery *rpn)
187 attrset = oid_getentbyoid (rpn->attributeSetId);
190 ast = attrset->value;
191 wrbuf_printf(b, "@attrset %s ", attrset->desc);
193 yaz_rpnstructure_to_wrbuf(b, rpn->RPNStructure);
197 void yaz_query_to_wrbuf(WRBUF b, const Z_Query *q)
204 case Z_Query_type_101:
205 wrbuf_printf(b,"RPN: ");
206 yaz_rpnquery_to_wrbuf(b, q->u.type_1);
209 wrbuf_printf(b, "CCL: %.*s", q->u.type_2->len, q->u.type_2->buf);
211 case Z_Query_type_100:
212 wrbuf_printf(b, "Z39.58: %.*s", q->u.type_100->len,
215 case Z_Query_type_104:
216 if (q->u.type_104->which == Z_External_CQL)
217 wrbuf_printf(b, "CQL: %s", q->u.type_104->u.cql);
219 wrbuf_printf(b,"Unknown type 104 query %d", q->u.type_104->which);
223 void yaz_scan_to_wrbuf(WRBUF b, const Z_AttributesPlusTerm *zapt,
226 /* should print attr set here */
227 yaz_apt_to_wrbuf(b, zapt);
231 void wrbuf_scan_term(WRBUF b, const Z_AttributesPlusTerm *zapt, oid_value ast)
233 yaz_apt_to_wrbuf(b, zapt);
237 void wrbuf_put_zquery(WRBUF b, const Z_Query *q)
239 yaz_query_to_wrbuf(b, q);
246 * indent-tabs-mode: nil
248 * vim: shiftwidth=4 tabstop=8 expandtab