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 /* Bug 2878: Currently only support left and right truncation. Specific check for this */
162 static int checkForTruncation(int flag, Z_AttributeList *attributes)
165 for (j = 0; j < attributes->num_attributes; j++)
167 Z_AttributeElement *ae = attributes->attributes[j];
168 if (*ae->attributeType == 5) /* truncation attribute */
170 if (ae->which == Z_AttributeValue_numeric)
172 Odr_int truncation = *(ae->value.numeric);
173 /* This logic only works for Left, right and both. eg. 1,2,3 */
175 return ((int) truncation & flag);
177 /* Complex: Shouldn't happen */
180 /* No truncation or unsupported */
184 static int checkForLeftTruncation(Z_AttributeList *attributes) {
185 return checkForTruncation(2, attributes);
188 static int checkForRightTruncation(Z_AttributeList *attributes) {
189 return checkForTruncation(1, attributes);
192 static int rpn2cql_simple(cql_transform_t ct,
193 void (*pr)(const char *buf, void *client_data),
195 Z_Operand *q, WRBUF w)
198 if (q->which != Z_Operand_APT)
201 cql_transform_set_error(ct, YAZ_BIB1_RESULT_SET_UNSUPP_AS_A_SEARCH_TERM, 0);
205 Z_AttributesPlusTerm *apt = q->u.attributesPlusTerm;
206 Z_Term *term = apt->term;
207 const char *sterm = 0;
211 ret = rpn2cql_attr(ct, apt->attributes, w);
216 lterm = term->u.general->len;
217 sterm = (const char *) term->u.general->buf;
220 wrbuf_printf(w, ODR_INT_PRINTF, *term->u.numeric);
222 case Z_Term_characterString:
223 sterm = term->u.characterString;
224 lterm = strlen(sterm);
228 cql_transform_set_error(ct, YAZ_BIB1_TERM_TYPE_UNSUPP, 0);
235 for (i = 0 ; i < lterm; i++)
240 /* Bug 2878: Check and add Truncation */
241 if (checkForLeftTruncation(apt->attributes))
243 wrbuf_write(w, sterm, lterm);
244 /* Bug 2878: Check and add Truncation */
245 if (checkForRightTruncation(apt->attributes))
251 pr(wrbuf_cstr(w), client_data);
257 static int rpn2cql_structure(cql_transform_t ct,
258 void (*pr)(const char *buf, void *client_data),
260 Z_RPNStructure *q, int nested,
263 if (q->which == Z_RPNStructure_simple)
264 return rpn2cql_simple(ct, pr, client_data, q->u.simple, w);
267 Z_Operator *op = q->u.complex->roperator;
271 pr("(", client_data);
273 r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s1, 1, w);
279 pr(" and ", client_data);
282 pr(" or ", client_data);
284 case Z_Operator_and_not:
285 pr(" not ", client_data);
287 case Z_Operator_prox:
288 cql_transform_set_error(ct, YAZ_BIB1_UNSUPP_SEARCH, 0);
291 r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s2, 1, w);
293 pr(")", client_data);
298 int cql_transform_rpn2cql_stream(cql_transform_t ct,
299 void (*pr)(const char *buf, void *client_data),
304 WRBUF w = wrbuf_alloc();
305 cql_transform_set_error(ct, 0, 0);
306 r = rpn2cql_structure(ct, pr, client_data, q->RPNStructure, 0, w);
312 int cql_transform_rpn2cql_wrbuf(cql_transform_t ct,
316 return cql_transform_rpn2cql_stream(ct, wrbuf_vputs, w, q);
322 * c-file-style: "Stroustrup"
323 * indent-tabs-mode: nil
325 * vim: shiftwidth=4 tabstop=8 expandtab