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 void wrbuf_vputs(const char *buf, void *client_data)
23 wrbuf_write((WRBUF) client_data, buf, strlen(buf));
26 static const char *lookup_index_from_string_attr(Z_AttributeList *attributes)
29 int server_choice = 1;
30 for (j = 0; j < attributes->num_attributes; j++)
32 Z_AttributeElement *ae = attributes->attributes[j];
33 if (*ae->attributeType == 1) /* use attribute */
35 if (ae->which == Z_AttributeValue_complex)
37 Z_ComplexAttribute *ca = ae->value.complex;
39 for (i = 0; i < ca->num_list; i++)
41 Z_StringOrNumeric *son = ca->list[i];
42 if (son->which == Z_StringOrNumeric_string)
46 server_choice = 0; /* not serverChoice because we have use attr */
50 return "cql.serverChoice";
54 static const char *lookup_relation_index_from_attr(Z_AttributeList *attributes)
57 for (j = 0; j < attributes->num_attributes; j++)
59 Z_AttributeElement *ae = attributes->attributes[j];
60 if (*ae->attributeType == 2) /* relation attribute */
62 if (ae->which == Z_AttributeValue_numeric)
64 /* Only support for numeric relation */
65 Odr_int *relation = ae->value.numeric;
66 /* map this numeric to representation in CQL */
69 /* Unsure on whether this is the relation attribute constants? */
70 case Z_ProximityOperator_Prox_lessThan:
72 case Z_ProximityOperator_Prox_lessThanOrEqual:
74 case Z_ProximityOperator_Prox_equal:
76 case Z_ProximityOperator_Prox_greaterThanOrEqual:
78 case Z_ProximityOperator_Prox_greaterThan:
80 case Z_ProximityOperator_Prox_notEqual:
83 /* phonetic is not supported in CQL */
86 /* stem is not supported in CQL */
89 /* relevance is supported in CQL, but not implemented yet */
92 /* Invalid relation */
97 /* Can we have a complex relation value?
98 Should we implement something?
106 static int rpn2cql_attr(cql_transform_t ct,
107 Z_AttributeList *attributes, WRBUF w)
109 const char *relation = cql_lookup_reverse(ct, "relation.", attributes);
110 const char *index = cql_lookup_reverse(ct, "index.", attributes);
111 const char *structure = cql_lookup_reverse(ct, "structure.", attributes);
113 /* if transform (properties) do not match, we'll just use a USE string attribute (bug #2978) */
115 index = lookup_index_from_string_attr(attributes);
117 /* Attempt to fix bug #2978: Look for a relation attribute */
119 relation = lookup_relation_index_from_attr(attributes);
123 cql_transform_set_error(ct,
124 YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, 0);
127 /* for serverChoice we omit index+relation+structure */
128 if (strcmp(index, "cql.serverChoice"))
130 wrbuf_puts(w, index);
133 if (!strcmp(relation, "exact"))
135 else if (!strcmp(relation, "eq"))
137 else if (!strcmp(relation, "le"))
139 else if (!strcmp(relation, "ge"))
141 /* Missing mapping of not equal, phonetic, stem and relevance */
142 wrbuf_puts(w, relation);
149 if (strcmp(structure, "*"))
152 wrbuf_puts(w, structure);
160 /* Bug 2878: Currently only support left and right truncation. Specific check for this */
161 static int checkForTruncation(int flag, 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)
171 int truncation = *(ae->value.numeric);
172 /* This logic only works for Left, right and both. eg. 1,2,3 */
174 return (int) (truncation & flag);
176 /* Complex: Shouldn't happen */
179 /* No truncation or unsupported */
183 static int checkForLeftTruncation(Z_AttributeList *attributes) {
184 return checkForTruncation(1, attributes);
187 static int checkForRightTruncation(Z_AttributeList *attributes) {
188 return checkForTruncation(2, attributes);
191 static int rpn2cql_simple(cql_transform_t ct,
192 void (*pr)(const char *buf, void *client_data),
194 Z_Operand *q, WRBUF w)
197 if (q->which != Z_Operand_APT)
200 cql_transform_set_error(ct, YAZ_BIB1_RESULT_SET_UNSUPP_AS_A_SEARCH_TERM, 0);
204 Z_AttributesPlusTerm *apt = q->u.attributesPlusTerm;
205 Z_Term *term = apt->term;
206 const char *sterm = 0;
210 ret = rpn2cql_attr(ct, apt->attributes, w);
215 lterm = term->u.general->len;
216 sterm = (const char *) term->u.general->buf;
219 wrbuf_printf(w, ODR_INT_PRINTF, *term->u.numeric);
221 case Z_Term_characterString:
222 sterm = term->u.characterString;
223 lterm = strlen(sterm);
227 cql_transform_set_error(ct, YAZ_BIB1_TERM_TYPE_UNSUPP, 0);
234 for (i = 0 ; i < lterm; i++)
239 /* Bug 2878: Check and add Truncation */
240 if (checkForLeftTruncation(apt->attributes))
242 wrbuf_write(w, sterm, lterm);
243 /* Bug 2878: Check and add Truncation */
244 if (checkForRightTruncation(apt->attributes))
250 pr(wrbuf_cstr(w), client_data);
256 static int rpn2cql_structure(cql_transform_t ct,
257 void (*pr)(const char *buf, void *client_data),
259 Z_RPNStructure *q, int nested,
262 if (q->which == Z_RPNStructure_simple)
263 return rpn2cql_simple(ct, pr, client_data, q->u.simple, w);
266 Z_Operator *op = q->u.complex->roperator;
270 pr("(", client_data);
272 r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s1, 1, w);
278 pr(" and ", client_data);
281 pr(" or ", client_data);
283 case Z_Operator_and_not:
284 pr(" not ", client_data);
286 case Z_Operator_prox:
287 cql_transform_set_error(ct, YAZ_BIB1_UNSUPP_SEARCH, 0);
290 r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s2, 1, w);
292 pr(")", client_data);
297 int cql_transform_rpn2cql_stream(cql_transform_t ct,
298 void (*pr)(const char *buf, void *client_data),
303 WRBUF w = wrbuf_alloc();
304 cql_transform_set_error(ct, 0, 0);
305 r = rpn2cql_structure(ct, pr, client_data, q->RPNStructure, 0, w);
311 int cql_transform_rpn2cql_wrbuf(cql_transform_t ct,
315 return cql_transform_rpn2cql_stream(ct, wrbuf_vputs, w, q);
321 * c-file-style: "Stroustrup"
322 * indent-tabs-mode: nil
324 * vim: shiftwidth=4 tabstop=8 expandtab