1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 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>
21 #include <yaz/logrpn.h> /* For yaz_prox_unit_name() */
23 static const char *lookup_index_from_string_attr(Z_AttributeList *attributes)
26 int server_choice = 1;
27 for (j = 0; j < attributes->num_attributes; j++)
29 Z_AttributeElement *ae = attributes->attributes[j];
30 if (*ae->attributeType == 1) /* use attribute */
32 if (ae->which == Z_AttributeValue_complex)
34 Z_ComplexAttribute *ca = ae->value.complex;
36 for (i = 0; i < ca->num_list; i++)
38 Z_StringOrNumeric *son = ca->list[i];
39 if (son->which == Z_StringOrNumeric_string)
43 server_choice = 0; /* not serverChoice because we have use attr */
47 return "cql.serverChoice";
51 static const char *lookup_relation_index_from_attr(Z_AttributeList *attributes)
54 for (j = 0; j < attributes->num_attributes; j++)
56 Z_AttributeElement *ae = attributes->attributes[j];
57 if (*ae->attributeType == 2) /* relation attribute */
59 if (ae->which == Z_AttributeValue_numeric)
61 /* Only support for numeric relation */
62 Odr_int *relation = ae->value.numeric;
63 /* map this numeric to representation in CQL */
66 /* Unsure on whether this is the relation attribute constants? */
67 case Z_ProximityOperator_Prox_lessThan:
69 case Z_ProximityOperator_Prox_lessThanOrEqual:
71 case Z_ProximityOperator_Prox_equal:
73 case Z_ProximityOperator_Prox_greaterThanOrEqual:
75 case Z_ProximityOperator_Prox_greaterThan:
77 case Z_ProximityOperator_Prox_notEqual:
80 /* phonetic is not supported in CQL */
83 /* stem is not supported in CQL */
86 /* relevance is supported in CQL, but not implemented yet */
89 /* Invalid relation */
94 /* Can we have a complex relation value?
95 Should we implement something?
103 static int rpn2cql_attr(cql_transform_t ct,
104 Z_AttributeList *attributes, WRBUF w)
106 const char *relation = cql_lookup_reverse(ct, "relation.", attributes);
107 const char *index = cql_lookup_reverse(ct, "index.", attributes);
108 const char *structure = cql_lookup_reverse(ct, "structure.", attributes);
110 /* if transform (properties) do not match, we'll just use a USE string attribute (bug #2978) */
112 index = lookup_index_from_string_attr(attributes);
114 /* Attempt to fix bug #2978: Look for a relation attribute */
116 relation = lookup_relation_index_from_attr(attributes);
121 return YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
125 else if (!strcmp(relation, "exact"))
127 else if (!strcmp(relation, "eq"))
129 else if (!strcmp(relation, "le"))
131 else if (!strcmp(relation, "ge"))
134 if (strcmp(index, "cql.serverChoice") || strcmp(relation, "=")
135 || (structure && strcmp(structure, "*")))
137 wrbuf_puts(w, index);
139 wrbuf_puts(w, relation);
142 if (structure && strcmp(structure, "*"))
145 wrbuf_puts(w, structure);
152 static Odr_int lookup_truncation(Z_AttributeList *attributes)
155 for (j = 0; j < attributes->num_attributes; j++)
157 Z_AttributeElement *ae = attributes->attributes[j];
158 if (*ae->attributeType == 5) /* truncation attribute */
160 if (ae->which == Z_AttributeValue_numeric)
161 return *(ae->value.numeric);
164 /* No truncation specified */
168 static int rpn2cql_simple(cql_transform_t ct,
169 void (*pr)(const char *buf, void *client_data),
171 Z_Operand *q, WRBUF w)
173 if (q->which != Z_Operand_APT)
176 return YAZ_BIB1_RESULT_SET_UNSUPP_AS_A_SEARCH_TERM;
180 Z_AttributesPlusTerm *apt = q->u.attributesPlusTerm;
181 Z_Term *term = apt->term;
182 const char *sterm = 0;
184 Odr_int trunc = lookup_truncation(apt->attributes);
189 r = rpn2cql_attr(ct, apt->attributes, w);
196 lterm = term->u.general->len;
197 sterm = (const char *) term->u.general->buf;
200 wrbuf_printf(w, ODR_INT_PRINTF, *term->u.numeric);
202 case Z_Term_characterString:
203 sterm = term->u.characterString;
204 lterm = strlen(sterm);
208 wrbuf_printf(w, "%d", term->which);
209 return YAZ_BIB1_TERM_TYPE_UNSUPP;
212 if (trunc <= 3 || trunc == 100 || trunc == 102 || trunc == 104)
215 for (i = 0 ; i < lterm; i++)
216 if (strchr(" ()=></", sterm[i]))
225 if (trunc == 2 || trunc == 3)
227 for (i = 0; i < lterm; i++)
229 if (sterm[i] == '\\' && i < lterm - 1)
232 if (strchr("*?\"\\", sterm[i]))
234 wrbuf_putc(w, sterm[i]);
236 else if (trunc == 102 && sterm[i] == '.' && sterm[i+1] == '*')
241 else if (trunc == 102 && sterm[i] == '.')
243 else if (trunc == 104 && sterm[i] == '?')
245 else if (trunc == 104 && sterm[i] == '#')
247 else if (strchr("*?\"", sterm[i]))
250 wrbuf_putc(w, sterm[i]);
253 wrbuf_putc(w, sterm[i]);
255 if (trunc == 1 || trunc == 3)
263 wrbuf_printf(w, ODR_INT_PRINTF, trunc);
264 return YAZ_BIB1_UNSUPP_TRUNCATION_ATTRIBUTE;
266 pr(wrbuf_cstr(w), client_data);
272 static int rpn2cql_structure(cql_transform_t ct,
273 void (*pr)(const char *buf, void *client_data),
275 Z_RPNStructure *q, int nested,
278 if (q->which == Z_RPNStructure_simple)
279 return rpn2cql_simple(ct, pr, client_data, q->u.simple, w);
282 Z_Operator *op = q->u.complex->roperator;
283 Z_ProximityOperator *prox;
287 pr("(", client_data);
289 r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s1, 1, w);
295 pr(" and ", client_data);
298 pr(" or ", client_data);
300 case Z_Operator_and_not:
301 pr(" not ", client_data);
303 case Z_Operator_prox: {
304 pr(" prox", client_data);
306 /* No way to express Odr_bool *exclusion -- ignore it */
307 if (prox->distance) {
308 char buf[21]; /* Enough for any 64-bit int */
309 char *op2name[6] = { "<", "<=", "=", ">=", ">","<>" };
310 pr("/distance", client_data);
311 if (!prox->relationType ||
312 *prox->relationType < Z_ProximityOperator_Prox_lessThan ||
313 *prox->relationType > Z_ProximityOperator_Prox_notEqual)
316 return YAZ_BIB1_UNSUPP_SEARCH;
318 pr(op2name[*prox->relationType-1], client_data);
319 sprintf(buf, "%ld", (long) *prox->distance);
320 pr(buf, client_data);
323 if (*prox->ordered) {
324 pr("/ordered", client_data);
326 pr("/unordered", client_data);
329 if (prox->which != Z_ProximityOperator_known ||
330 *prox->u.known != Z_ProxUnit_word) {
331 pr("/unit=", client_data);
332 pr(yaz_prox_unit_name(prox), client_data);
334 pr(" ", client_data);
338 r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s2, 1, w);
340 pr(")", client_data);
345 int cql_transform_rpn2cql_stream_r(cql_transform_t ct,
347 void (*pr)(const char *buf, void *client_data),
351 /* addinfo (w) is used for both addinfo and house-keeping ! */
352 int r = rpn2cql_structure(ct, pr, client_data, q->RPNStructure, 0, addinfo);
354 wrbuf_rewind(addinfo); /* no additional info if no error */
359 int cql_transform_rpn2cql_stream(cql_transform_t ct,
360 void (*pr)(const char *buf, void *client_data),
364 WRBUF w = wrbuf_alloc();
365 int r = cql_transform_rpn2cql_stream_r(ct, w, pr, client_data, q);
367 cql_transform_set_error(ct, r, wrbuf_len(w) ? wrbuf_cstr(w) : 0);
373 int cql_transform_rpn2cql_wrbuf(cql_transform_t ct,
377 return cql_transform_rpn2cql_stream(ct, wrbuf_vp_puts, w, q);
383 * c-file-style: "Stroustrup"
384 * indent-tabs-mode: nil
386 * vim: shiftwidth=4 tabstop=8 expandtab