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;
123 /* for serverChoice we omit index+relation+structure */
124 if (strcmp(index, "cql.serverChoice"))
126 wrbuf_puts(w, index);
129 if (!strcmp(relation, "exact"))
131 else if (!strcmp(relation, "eq"))
133 else if (!strcmp(relation, "le"))
135 else if (!strcmp(relation, "ge"))
137 /* Missing mapping of not equal, phonetic, stem and relevance */
138 wrbuf_puts(w, relation);
145 if (strcmp(structure, "*"))
148 wrbuf_puts(w, structure);
156 static Odr_int lookup_truncation(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)
165 return *(ae->value.numeric);
168 /* No truncation specified */
172 static int rpn2cql_simple(cql_transform_t ct,
173 void (*pr)(const char *buf, void *client_data),
175 Z_Operand *q, WRBUF w)
177 if (q->which != Z_Operand_APT)
180 return YAZ_BIB1_RESULT_SET_UNSUPP_AS_A_SEARCH_TERM;
184 Z_AttributesPlusTerm *apt = q->u.attributesPlusTerm;
185 Z_Term *term = apt->term;
186 const char *sterm = 0;
188 Odr_int trunc = lookup_truncation(apt->attributes);
193 r = rpn2cql_attr(ct, apt->attributes, w);
200 lterm = term->u.general->len;
201 sterm = (const char *) term->u.general->buf;
204 wrbuf_printf(w, ODR_INT_PRINTF, *term->u.numeric);
206 case Z_Term_characterString:
207 sterm = term->u.characterString;
208 lterm = strlen(sterm);
212 wrbuf_printf(w, "%d", term->which);
213 return YAZ_BIB1_TERM_TYPE_UNSUPP;
216 if (trunc <= 3 || trunc == 100 || trunc == 102 || trunc == 104)
219 for (i = 0 ; i < lterm; i++)
220 if (strchr(" ()=></", sterm[i]))
229 if (trunc == 2 || trunc == 3)
231 for (i = 0; i < lterm; i++)
233 if (sterm[i] == '\\' && i < lterm - 1)
236 if (strchr("*?\"\\", sterm[i]))
238 wrbuf_putc(w, sterm[i]);
240 else if (trunc == 102 && sterm[i] == '.' && sterm[i+1] == '*')
245 else if (trunc == 102 && sterm[i] == '.')
247 else if (trunc == 104 && sterm[i] == '?')
249 else if (trunc == 104 && sterm[i] == '#')
251 else if (strchr("*?\"", sterm[i]))
254 wrbuf_putc(w, sterm[i]);
257 wrbuf_putc(w, sterm[i]);
259 if (trunc == 1 || trunc == 3)
267 wrbuf_printf(w, ODR_INT_PRINTF, trunc);
268 return YAZ_BIB1_UNSUPP_TRUNCATION_ATTRIBUTE;
270 pr(wrbuf_cstr(w), client_data);
276 static int rpn2cql_structure(cql_transform_t ct,
277 void (*pr)(const char *buf, void *client_data),
279 Z_RPNStructure *q, int nested,
282 if (q->which == Z_RPNStructure_simple)
283 return rpn2cql_simple(ct, pr, client_data, q->u.simple, w);
286 Z_Operator *op = q->u.complex->roperator;
287 Z_ProximityOperator *prox;
291 pr("(", client_data);
293 r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s1, 1, w);
299 pr(" and ", client_data);
302 pr(" or ", client_data);
304 case Z_Operator_and_not:
305 pr(" not ", client_data);
307 case Z_Operator_prox: {
308 pr(" prox", client_data);
310 /* No way to express Odr_bool *exclusion -- ignore it */
311 if (prox->distance) {
312 char buf[21]; /* Enough for any 64-bit int */
313 char *op2name[6] = { "<", "<=", "=", ">=", ">","<>" };
314 pr("/distance", client_data);
315 if (!prox->relationType ||
316 *prox->relationType < Z_ProximityOperator_Prox_lessThan ||
317 *prox->relationType > Z_ProximityOperator_Prox_notEqual)
320 return YAZ_BIB1_UNSUPP_SEARCH;
322 pr(op2name[*prox->relationType-1], client_data);
323 sprintf(buf, "%ld", (long) *prox->distance);
324 pr(buf, client_data);
327 if (*prox->ordered) {
328 pr("/ordered", client_data);
330 pr("/unordered", client_data);
333 if (prox->which != Z_ProximityOperator_known ||
334 *prox->u.known != Z_ProxUnit_word) {
335 pr("/unit=", client_data);
336 pr(yaz_prox_unit_name(prox), client_data);
338 pr(" ", client_data);
342 r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s2, 1, w);
344 pr(")", client_data);
349 int cql_transform_rpn2cql_stream_r(cql_transform_t ct,
351 void (*pr)(const char *buf, void *client_data),
355 /* addinfo (w) is used for both addinfo and house-keeping ! */
356 int r = rpn2cql_structure(ct, pr, client_data, q->RPNStructure, 0, addinfo);
358 wrbuf_rewind(addinfo); /* no additional info if no error */
363 int cql_transform_rpn2cql_stream(cql_transform_t ct,
364 void (*pr)(const char *buf, void *client_data),
368 WRBUF w = wrbuf_alloc();
369 int r = cql_transform_rpn2cql_stream_r(ct, w, pr, client_data, q);
371 cql_transform_set_error(ct, r, wrbuf_len(w) ? wrbuf_cstr(w) : 0);
377 int cql_transform_rpn2cql_wrbuf(cql_transform_t ct,
381 return cql_transform_rpn2cql_stream(ct, wrbuf_vp_puts, w, q);
387 * c-file-style: "Stroustrup"
388 * indent-tabs-mode: nil
390 * vim: shiftwidth=4 tabstop=8 expandtab