2 * Copyright (C) 1995-2005, Index Data ApS
5 * $Id: querytowrbuf.c,v 1.3 2006-01-20 14:44:55 adam Exp $
10 * \brief Query to WRBUF (to strings)
16 #include <yaz/logrpn.h>
17 #include <yaz/querytowrbuf.h>
19 static void yaz_term_to_wrbuf(WRBUF b, const char *term, int len)
22 for (i = 0; i < len; i++)
23 if (strchr(" \"{", term[i]))
26 wrbuf_printf(b, "%.*s ", len, term);
30 for (i = 0; i<len; i++)
34 wrbuf_putc(b, term[i]);
36 wrbuf_printf(b, "\" ");
40 static void yaz_attribute_element_to_wrbuf(WRBUF b,
41 const Z_AttributeElement *element)
45 char *sep = ""; /* optional space after attrset name */
46 if (element->attributeSet)
49 attrset = oid_getentbyoid (element->attributeSet);
50 setname = attrset->desc;
53 switch (element->which)
55 case Z_AttributeValue_numeric:
56 wrbuf_printf(b,"@attr %s%s%d=%d ", setname, sep,
57 *element->attributeType, *element->value.numeric);
59 case Z_AttributeValue_complex:
60 wrbuf_printf(b,"@attr %s%s\"%d=", setname, sep,
61 *element->attributeType);
62 for (i = 0; i<element->value.complex->num_list; i++)
66 if (element->value.complex->list[i]->which ==
67 Z_StringOrNumeric_string)
68 wrbuf_printf (b, "%s",
69 element->value.complex->list[i]->u.string);
70 else if (element->value.complex->list[i]->which ==
71 Z_StringOrNumeric_numeric)
72 wrbuf_printf (b, "%d",
73 *element->value.complex->list[i]->u.numeric);
75 wrbuf_printf(b, "\" ");
78 wrbuf_printf (b, "@attr 1=unknown ");
82 static const char *complex_op_name(const Z_Operator *op)
90 case Z_Operator_and_not:
95 return "unknown complex operator";
99 static void yaz_apt_to_wrbuf(WRBUF b, const Z_AttributesPlusTerm *zapt)
101 int num_attributes = zapt->attributes->num_attributes;
103 for (i = 0; i<num_attributes; i++)
104 yaz_attribute_element_to_wrbuf(b,zapt->attributes->attributes[i]);
106 switch (zapt->term->which)
109 yaz_term_to_wrbuf(b, (const char *)zapt->term->u.general->buf,
110 zapt->term->u.general->len);
112 case Z_Term_characterString:
113 wrbuf_printf(b, "@term string ");
114 yaz_term_to_wrbuf(b, zapt->term->u.characterString,
115 strlen(zapt->term->u.characterString));
118 wrbuf_printf(b, "@term numeric %d ", *zapt->term->u.numeric);
121 wrbuf_printf(b, "@term null x");
124 wrbuf_printf(b, "@term null unknown%d ", zapt->term->which);
128 static void yaz_rpnstructure_to_wrbuf(WRBUF b, const Z_RPNStructure *zs)
130 if (zs->which == Z_RPNStructure_complex)
132 Z_Operator *op = zs->u.complex->roperator;
133 wrbuf_printf(b, "@%s ", complex_op_name(op) );
134 if (op->which== Z_Operator_prox)
136 if (!op->u.prox->exclusion)
138 else if (*op->u.prox->exclusion)
143 wrbuf_printf(b, " %d %d %d ", *op->u.prox->distance,
144 *op->u.prox->ordered,
145 *op->u.prox->relationType);
147 switch(op->u.prox->which)
149 case Z_ProximityOperator_known:
152 case Z_ProximityOperator_private:
156 wrbuf_printf(b, "%d", op->u.prox->which);
158 if (op->u.prox->u.known)
159 wrbuf_printf(b, " %d ", *op->u.prox->u.known);
161 wrbuf_printf(b, " 0 ");
163 yaz_rpnstructure_to_wrbuf(b,zs->u.complex->s1);
164 yaz_rpnstructure_to_wrbuf(b,zs->u.complex->s2);
166 else if (zs->which == Z_RPNStructure_simple)
168 if (zs->u.simple->which == Z_Operand_APT)
169 yaz_apt_to_wrbuf(b, zs->u.simple->u.attributesPlusTerm);
170 else if (zs->u.simple->which == Z_Operand_resultSetId)
172 wrbuf_printf(b, "@set ");
173 yaz_term_to_wrbuf(b, zs->u.simple->u.resultSetId,
174 strlen(zs->u.simple->u.resultSetId));
177 wrbuf_printf (b, "(unknown simple structure)");
180 wrbuf_puts(b, "(unknown structure)");
183 void yaz_rpnquery_to_wrbuf(WRBUF b, const Z_RPNQuery *rpn)
188 attrset = oid_getentbyoid (rpn->attributeSetId);
191 ast = attrset->value;
192 wrbuf_printf(b, "@attrset %s ", attrset->desc);
194 yaz_rpnstructure_to_wrbuf(b, rpn->RPNStructure);
198 void yaz_query_to_wrbuf(WRBUF b, const Z_Query *q)
205 case Z_Query_type_101:
206 wrbuf_printf(b,"RPN: ");
207 yaz_rpnquery_to_wrbuf(b, q->u.type_1);
210 wrbuf_printf(b, "CCL: %.*s", q->u.type_2->len, q->u.type_2->buf);
212 case Z_Query_type_100:
213 wrbuf_printf(b, "Z39.58: %.*s", q->u.type_100->len,
216 case Z_Query_type_104:
217 if (q->u.type_104->which == Z_External_CQL)
218 wrbuf_printf(b, "CQL: %s", q->u.type_104->u.cql);
220 wrbuf_printf(b,"Unknown type 104 query %d", q->u.type_104->which);
224 void yaz_scan_to_wrbuf(WRBUF b, const Z_AttributesPlusTerm *zapt,
227 /* should print attr set here */
228 yaz_apt_to_wrbuf(b, zapt);
232 void wrbuf_scan_term(WRBUF b, const Z_AttributesPlusTerm *zapt, oid_value ast)
234 yaz_apt_to_wrbuf(b, zapt);
238 void wrbuf_put_zquery(WRBUF b, const Z_Query *q)
240 yaz_query_to_wrbuf(b, q);
247 * indent-tabs-mode: nil
249 * vim: shiftwidth=4 tabstop=8 expandtab