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,
24 Odr_int *numeric_value)
27 int server_choice = 1;
28 for (j = 0; j < attributes->num_attributes; j++)
30 Z_AttributeElement *ae = attributes->attributes[j];
31 if (*ae->attributeType == 1) /* use attribute */
33 if (ae->which == Z_AttributeValue_complex)
35 Z_ComplexAttribute *ca = ae->value.complex;
37 for (i = 0; i < ca->num_list; i++)
39 Z_StringOrNumeric *son = ca->list[i];
40 if (son->which == Z_StringOrNumeric_string)
44 else if (ae->which == Z_AttributeValue_numeric)
46 *numeric_value = *ae->value.numeric;
48 server_choice = 0; /* not serverChoice because we have use attr */
52 return "cql.serverChoice";
56 static const char *lookup_relation_index_from_attr(Z_AttributeList *attributes)
59 for (j = 0; j < attributes->num_attributes; j++)
61 Z_AttributeElement *ae = attributes->attributes[j];
62 if (*ae->attributeType == 2) /* relation attribute */
64 if (ae->which == Z_AttributeValue_numeric)
66 /* Only support for numeric relation */
67 Odr_int *relation = ae->value.numeric;
68 /* map this numeric to representation in CQL */
71 /* Unsure on whether this is the relation attribute constants? */
72 case Z_ProximityOperator_Prox_lessThan:
74 case Z_ProximityOperator_Prox_lessThanOrEqual:
76 case Z_ProximityOperator_Prox_equal:
78 case Z_ProximityOperator_Prox_greaterThanOrEqual:
80 case Z_ProximityOperator_Prox_greaterThan:
82 case Z_ProximityOperator_Prox_notEqual:
85 /* phonetic is not supported in CQL */
88 /* stem is not supported in CQL */
91 /* relevance is supported in CQL, but not implemented yet */
94 /* Invalid relation */
99 /* Can we have a complex relation value?
100 Should we implement something?
108 static int rpn2cql_attr(cql_transform_t ct,
109 Z_AttributeList *attributes, WRBUF w)
111 const char *relation = cql_lookup_reverse(ct, "relation.", attributes);
112 const char *index = cql_lookup_reverse(ct, "index.", attributes);
113 const char *structure = cql_lookup_reverse(ct, "structure.", attributes);
115 /* if transform (properties) do not match, we'll fall back
116 to string or report numeric attribute error */
119 Odr_int use_attribute = -1;
120 index = lookup_index_from_string_attr(attributes, &use_attribute);
124 if (use_attribute != -1)
125 wrbuf_printf(w, ODR_INT_PRINTF, use_attribute);
126 return YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
130 relation = lookup_relation_index_from_attr(attributes);
134 else if (!strcmp(relation, "exact"))
136 else if (!strcmp(relation, "eq"))
138 else if (!strcmp(relation, "le"))
140 else if (!strcmp(relation, "ge"))
143 if (strcmp(index, "cql.serverChoice") || strcmp(relation, "=")
144 || (structure && strcmp(structure, "*")))
146 wrbuf_puts(w, index);
148 wrbuf_puts(w, relation);
151 if (structure && strcmp(structure, "*"))
154 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)
182 if (q->which != Z_Operand_APT)
185 return YAZ_BIB1_RESULT_SET_UNSUPP_AS_A_SEARCH_TERM;
189 Z_AttributesPlusTerm *apt = q->u.attributesPlusTerm;
190 Z_Term *term = apt->term;
191 const char *sterm = 0;
193 Odr_int trunc = lookup_truncation(apt->attributes);
198 r = rpn2cql_attr(ct, apt->attributes, w);
205 lterm = term->u.general->len;
206 sterm = (const char *) term->u.general->buf;
209 wrbuf_printf(w, ODR_INT_PRINTF, *term->u.numeric);
211 case Z_Term_characterString:
212 sterm = term->u.characterString;
213 lterm = strlen(sterm);
217 wrbuf_printf(w, "%d", term->which);
218 return YAZ_BIB1_TERM_TYPE_UNSUPP;
221 if (trunc <= 3 || trunc == 100 || trunc == 102 || trunc == 104)
224 for (i = 0 ; i < lterm; i++)
225 if (strchr(" ()=></", sterm[i]))
234 if (trunc == 2 || trunc == 3)
236 for (i = 0; i < lterm; i++)
238 if (sterm[i] == '\\' && i < lterm - 1)
241 if (strchr("*?\"\\", sterm[i]))
243 wrbuf_putc(w, sterm[i]);
245 else if (trunc == 102 && sterm[i] == '.' && sterm[i+1] == '*')
250 else if (trunc == 102 && sterm[i] == '.')
252 else if (trunc == 104 && sterm[i] == '?')
254 else if (trunc == 104 && sterm[i] == '#')
256 else if (strchr("*?\"", sterm[i]))
259 wrbuf_putc(w, sterm[i]);
262 wrbuf_putc(w, sterm[i]);
264 if (trunc == 1 || trunc == 3)
272 wrbuf_printf(w, ODR_INT_PRINTF, trunc);
273 return YAZ_BIB1_UNSUPP_TRUNCATION_ATTRIBUTE;
275 pr(wrbuf_cstr(w), client_data);
281 static int rpn2cql_structure(cql_transform_t ct,
282 void (*pr)(const char *buf, void *client_data),
284 Z_RPNStructure *q, int nested,
287 if (q->which == Z_RPNStructure_simple)
288 return rpn2cql_simple(ct, pr, client_data, q->u.simple, w);
291 Z_Operator *op = q->u.complex->roperator;
292 Z_ProximityOperator *prox;
296 pr("(", client_data);
298 r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s1, 1, w);
304 pr(" and ", client_data);
307 pr(" or ", client_data);
309 case Z_Operator_and_not:
310 pr(" not ", client_data);
312 case Z_Operator_prox: {
313 pr(" prox", client_data);
315 /* No way to express Odr_bool *exclusion -- ignore it */
316 if (prox->distance) {
317 char buf[21]; /* Enough for any 64-bit int */
318 char *op2name[6] = { "<", "<=", "=", ">=", ">","<>" };
319 pr("/distance", client_data);
320 if (!prox->relationType ||
321 *prox->relationType < Z_ProximityOperator_Prox_lessThan ||
322 *prox->relationType > Z_ProximityOperator_Prox_notEqual)
325 return YAZ_BIB1_UNSUPP_SEARCH;
327 pr(op2name[*prox->relationType-1], client_data);
328 sprintf(buf, "%ld", (long) *prox->distance);
329 pr(buf, client_data);
332 if (*prox->ordered) {
333 pr("/ordered", client_data);
335 pr("/unordered", client_data);
338 if (prox->which != Z_ProximityOperator_known ||
339 *prox->u.known != Z_ProxUnit_word) {
340 pr("/unit=", client_data);
341 pr(yaz_prox_unit_name(prox), client_data);
343 pr(" ", client_data);
347 r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s2, 1, w);
349 pr(")", client_data);
354 int cql_transform_rpn2cql_stream_r(cql_transform_t ct,
356 void (*pr)(const char *buf, void *client_data),
360 /* addinfo (w) is used for both addinfo and house-keeping ! */
361 int r = rpn2cql_structure(ct, pr, client_data, q->RPNStructure, 0, addinfo);
363 wrbuf_rewind(addinfo); /* no additional info if no error */
368 int cql_transform_rpn2cql_stream(cql_transform_t ct,
369 void (*pr)(const char *buf, void *client_data),
373 WRBUF w = wrbuf_alloc();
374 int r = cql_transform_rpn2cql_stream_r(ct, w, pr, client_data, q);
376 cql_transform_set_error(ct, r, wrbuf_len(w) ? wrbuf_cstr(w) : 0);
382 int cql_transform_rpn2cql_wrbuf(cql_transform_t ct,
386 return cql_transform_rpn2cql_stream(ct, wrbuf_vp_puts, w, q);
392 * c-file-style: "Stroustrup"
393 * indent-tabs-mode: nil
395 * vim: shiftwidth=4 tabstop=8 expandtab