1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2010 Index Data
3 * See the file LICENSE for details.
8 * \brief Implements RPN to CQL conversion
15 #include <yaz/rpn2cql.h>
16 #include <yaz/xmalloc.h>
17 #include <yaz/diagbib1.h>
18 #include <yaz/z-core.h>
19 #include <yaz/wrbuf.h>
21 static const char *lookup_index_from_string_attr(Z_AttributeList *attributes)
24 int server_choice = 1;
25 for (j = 0; j < attributes->num_attributes; j++)
27 Z_AttributeElement *ae = attributes->attributes[j];
28 if (*ae->attributeType == 1) /* use attribute */
30 if (ae->which == Z_AttributeValue_complex)
32 Z_ComplexAttribute *ca = ae->value.complex;
34 for (i = 0; i < ca->num_list; i++)
36 Z_StringOrNumeric *son = ca->list[i];
37 if (son->which == Z_StringOrNumeric_string)
41 server_choice = 0; /* not serverChoice because we have use attr */
45 return "cql.serverChoice";
49 static const char *lookup_relation_index_from_attr(Z_AttributeList *attributes)
52 for (j = 0; j < attributes->num_attributes; j++)
54 Z_AttributeElement *ae = attributes->attributes[j];
55 if (*ae->attributeType == 2) /* relation attribute */
57 if (ae->which == Z_AttributeValue_numeric)
59 /* Only support for numeric relation */
60 Odr_int *relation = ae->value.numeric;
61 /* map this numeric to representation in CQL */
64 /* Unsure on whether this is the relation attribute constants? */
65 case Z_ProximityOperator_Prox_lessThan:
67 case Z_ProximityOperator_Prox_lessThanOrEqual:
69 case Z_ProximityOperator_Prox_equal:
71 case Z_ProximityOperator_Prox_greaterThanOrEqual:
73 case Z_ProximityOperator_Prox_greaterThan:
75 case Z_ProximityOperator_Prox_notEqual:
78 /* phonetic is not supported in CQL */
81 /* stem is not supported in CQL */
84 /* relevance is supported in CQL, but not implemented yet */
87 /* Invalid relation */
92 /* Can we have a complex relation value?
93 Should we implement something?
101 static int rpn2cql_attr(cql_transform_t ct,
102 Z_AttributeList *attributes, WRBUF w)
104 const char *relation = cql_lookup_reverse(ct, "relation.", attributes);
105 const char *index = cql_lookup_reverse(ct, "index.", attributes);
106 const char *structure = cql_lookup_reverse(ct, "structure.", attributes);
108 /* if transform (properties) do not match, we'll just use a USE string attribute (bug #2978) */
110 index = lookup_index_from_string_attr(attributes);
112 /* Attempt to fix bug #2978: Look for a relation attribute */
114 relation = lookup_relation_index_from_attr(attributes);
118 cql_transform_set_error(ct,
119 YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, 0);
122 /* for serverChoice we omit index+relation+structure */
123 if (strcmp(index, "cql.serverChoice"))
125 wrbuf_puts(w, index);
128 if (!strcmp(relation, "exact"))
130 else if (!strcmp(relation, "eq"))
132 else if (!strcmp(relation, "le"))
134 else if (!strcmp(relation, "ge"))
136 /* Missing mapping of not equal, phonetic, stem and relevance */
137 wrbuf_puts(w, relation);
144 if (strcmp(structure, "*"))
147 wrbuf_puts(w, structure);
155 /* Bug 2878: Currently only support left and right truncation. Specific check for this */
156 static int checkForTruncation(int flag, Z_AttributeList *attributes)
159 for (j = 0; j < attributes->num_attributes; j++)
161 Z_AttributeElement *ae = attributes->attributes[j];
162 if (*ae->attributeType == 5) /* truncation attribute */
164 if (ae->which == Z_AttributeValue_numeric)
166 int truncation = *(ae->value.numeric);
167 /* This logic only works for Left, right and both. eg. 1,2,3 */
169 return (int) (truncation & flag);
171 /* Complex: Shouldn't happen */
174 /* No truncation or unsupported */
178 static int checkForLeftTruncation(Z_AttributeList *attributes) {
179 return checkForTruncation(1, attributes);
182 static int checkForRightTruncation(Z_AttributeList *attributes) {
183 return checkForTruncation(2, attributes);
186 static int rpn2cql_simple(cql_transform_t ct,
187 void (*pr)(const char *buf, void *client_data),
189 Z_Operand *q, WRBUF w)
192 if (q->which != Z_Operand_APT)
195 cql_transform_set_error(ct, YAZ_BIB1_RESULT_SET_UNSUPP_AS_A_SEARCH_TERM, 0);
199 Z_AttributesPlusTerm *apt = q->u.attributesPlusTerm;
200 Z_Term *term = apt->term;
201 const char *sterm = 0;
205 ret = rpn2cql_attr(ct, apt->attributes, w);
210 lterm = term->u.general->len;
211 sterm = (const char *) term->u.general->buf;
214 wrbuf_printf(w, ODR_INT_PRINTF, *term->u.numeric);
216 case Z_Term_characterString:
217 sterm = term->u.characterString;
218 lterm = strlen(sterm);
222 cql_transform_set_error(ct, YAZ_BIB1_TERM_TYPE_UNSUPP, 0);
229 for (i = 0 ; i < lterm; i++)
234 /* Bug 2878: Check and add Truncation */
235 if (checkForLeftTruncation(apt->attributes))
237 wrbuf_write(w, sterm, lterm);
238 /* Bug 2878: Check and add Truncation */
239 if (checkForRightTruncation(apt->attributes))
245 pr(wrbuf_cstr(w), client_data);
251 static int rpn2cql_structure(cql_transform_t ct,
252 void (*pr)(const char *buf, void *client_data),
254 Z_RPNStructure *q, int nested,
257 if (q->which == Z_RPNStructure_simple)
258 return rpn2cql_simple(ct, pr, client_data, q->u.simple, w);
261 Z_Operator *op = q->u.complex->roperator;
265 pr("(", client_data);
267 r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s1, 1, w);
273 pr(" and ", client_data);
276 pr(" or ", client_data);
278 case Z_Operator_and_not:
279 pr(" not ", client_data);
281 case Z_Operator_prox:
282 cql_transform_set_error(ct, YAZ_BIB1_UNSUPP_SEARCH, 0);
285 r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s2, 1, w);
287 pr(")", client_data);
292 int cql_transform_rpn2cql_stream(cql_transform_t ct,
293 void (*pr)(const char *buf, void *client_data),
298 WRBUF w = wrbuf_alloc();
299 cql_transform_set_error(ct, 0, 0);
300 r = rpn2cql_structure(ct, pr, client_data, q->RPNStructure, 0, w);
306 int cql_transform_rpn2cql_wrbuf(cql_transform_t ct,
310 return cql_transform_rpn2cql_stream(ct, wrbuf_vputs, w, q);
316 * c-file-style: "Stroustrup"
317 * indent-tabs-mode: nil
319 * vim: shiftwidth=4 tabstop=8 expandtab