1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements RPN to CQL conversion
16 #include <yaz/rpn2cql.h>
17 #include <yaz/xmalloc.h>
18 #include <yaz/diagbib1.h>
19 #include <yaz/z-core.h>
20 #include <yaz/wrbuf.h>
22 static void wrbuf_vputs(const char *buf, void *client_data)
24 wrbuf_write((WRBUF) client_data, buf, strlen(buf));
27 static const char *lookup_index_from_string_attr(Z_AttributeList *attributes)
30 int server_choice = 1;
31 for (j = 0; j < attributes->num_attributes; j++)
33 Z_AttributeElement *ae = attributes->attributes[j];
34 if (*ae->attributeType == 1) /* use attribute */
36 if (ae->which == Z_AttributeValue_complex)
38 Z_ComplexAttribute *ca = ae->value.complex;
40 for (i = 0; i < ca->num_list; i++)
42 Z_StringOrNumeric *son = ca->list[i];
43 if (son->which == Z_StringOrNumeric_string)
47 server_choice = 0; /* not serverChoice because we have use attr */
51 return "cql.serverChoice";
55 static const char *lookup_relation_index_from_attr(Z_AttributeList *attributes)
58 for (j = 0; j < attributes->num_attributes; j++)
60 Z_AttributeElement *ae = attributes->attributes[j];
61 if (*ae->attributeType == 2) /* relation attribute */
63 if (ae->which == Z_AttributeValue_numeric)
65 /* Only support for numeric relation */
66 Odr_int *relation = ae->value.numeric;
67 /* map this numeric to representation in CQL */
70 /* Unsure on whether this is the relation attribute constants? */
71 case Z_ProximityOperator_Prox_lessThan:
73 case Z_ProximityOperator_Prox_lessThanOrEqual:
75 case Z_ProximityOperator_Prox_equal:
77 case Z_ProximityOperator_Prox_greaterThanOrEqual:
79 case Z_ProximityOperator_Prox_greaterThan:
81 case Z_ProximityOperator_Prox_notEqual:
84 /* phonetic is not supported in CQL */
87 /* stem is not supported in CQL */
90 /* relevance is supported in CQL, but not implemented yet */
93 /* Invalid relation */
98 /* Can we have a complex relation value?
99 Should we implement something?
107 static int rpn2cql_attr(cql_transform_t ct,
108 Z_AttributeList *attributes, WRBUF w)
110 const char *relation = cql_lookup_reverse(ct, "relation.", attributes);
111 const char *index = cql_lookup_reverse(ct, "index.", attributes);
112 const char *structure = cql_lookup_reverse(ct, "structure.", attributes);
114 /* if transform (properties) do not match, we'll just use a USE string attribute (bug #2978) */
116 index = lookup_index_from_string_attr(attributes);
118 /* Attempt to fix bug #2978: Look for a relation attribute */
120 relation = lookup_relation_index_from_attr(attributes);
124 cql_transform_set_error(ct,
125 YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, 0);
128 /* for serverChoice we omit index+relation+structure */
129 if (strcmp(index, "cql.serverChoice"))
131 wrbuf_puts(w, index);
134 if (!strcmp(relation, "exact"))
136 else if (!strcmp(relation, "eq"))
138 else if (!strcmp(relation, "le"))
140 else if (!strcmp(relation, "ge"))
142 /* Missing mapping of not equal, phonetic, stem and relevance */
143 wrbuf_puts(w, relation);
150 if (strcmp(structure, "*"))
153 wrbuf_puts(w, structure);
161 static Odr_int lookup_truncation(Z_AttributeList *attributes)
164 for (j = 0; j < attributes->num_attributes; j++)
166 Z_AttributeElement *ae = attributes->attributes[j];
167 if (*ae->attributeType == 5) /* truncation attribute */
169 if (ae->which == Z_AttributeValue_numeric)
170 return *(ae->value.numeric);
173 /* No truncation specified */
177 static int rpn2cql_simple(cql_transform_t ct,
178 void (*pr)(const char *buf, void *client_data),
180 Z_Operand *q, WRBUF w)
183 if (q->which != Z_Operand_APT)
186 cql_transform_set_error(ct, YAZ_BIB1_RESULT_SET_UNSUPP_AS_A_SEARCH_TERM, 0);
190 Z_AttributesPlusTerm *apt = q->u.attributesPlusTerm;
191 Z_Term *term = apt->term;
192 const char *sterm = 0;
196 ret = rpn2cql_attr(ct, apt->attributes, w);
201 lterm = term->u.general->len;
202 sterm = (const char *) term->u.general->buf;
205 wrbuf_printf(w, ODR_INT_PRINTF, *term->u.numeric);
207 case Z_Term_characterString:
208 sterm = term->u.characterString;
209 lterm = strlen(sterm);
213 cql_transform_set_error(ct, YAZ_BIB1_TERM_TYPE_UNSUPP, 0);
220 Odr_int trunc = lookup_truncation(apt->attributes);
222 if (trunc > 3 && trunc != 100)
224 cql_transform_set_error(
225 ct, YAZ_BIB1_UNSUPP_TRUNCATION_ATTRIBUTE, 0);
228 for (i = 0 ; i < lterm; i++)
233 if (trunc == 2 || trunc == 3)
235 wrbuf_write(w, sterm, lterm);
236 if (trunc == 1 || trunc == 3)
242 pr(wrbuf_cstr(w), client_data);
248 static int rpn2cql_structure(cql_transform_t ct,
249 void (*pr)(const char *buf, void *client_data),
251 Z_RPNStructure *q, int nested,
254 if (q->which == Z_RPNStructure_simple)
255 return rpn2cql_simple(ct, pr, client_data, q->u.simple, w);
258 Z_Operator *op = q->u.complex->roperator;
262 pr("(", client_data);
264 r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s1, 1, w);
270 pr(" and ", client_data);
273 pr(" or ", client_data);
275 case Z_Operator_and_not:
276 pr(" not ", client_data);
278 case Z_Operator_prox:
279 cql_transform_set_error(ct, YAZ_BIB1_UNSUPP_SEARCH, 0);
282 r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s2, 1, w);
284 pr(")", client_data);
289 int cql_transform_rpn2cql_stream(cql_transform_t ct,
290 void (*pr)(const char *buf, void *client_data),
295 WRBUF w = wrbuf_alloc();
296 cql_transform_set_error(ct, 0, 0);
297 r = rpn2cql_structure(ct, pr, client_data, q->RPNStructure, 0, w);
303 int cql_transform_rpn2cql_wrbuf(cql_transform_t ct,
307 return cql_transform_rpn2cql_stream(ct, wrbuf_vputs, w, q);
313 * c-file-style: "Stroustrup"
314 * indent-tabs-mode: nil
316 * vim: shiftwidth=4 tabstop=8 expandtab