1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 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 int server_choice = 1;
53 for (j = 0; j < attributes->num_attributes; j++)
55 Z_AttributeElement *ae = attributes->attributes[j];
56 if (*ae->attributeType == 2) /* relation attribute */
58 if (ae->which == Z_AttributeValue_numeric)
60 /* Only support for numeric relation */
61 Odr_int *relation = ae->value.numeric;
62 /* map this numeric to represetation in cql */
64 /* Unsure on whether this is the relation attribute
66 case Z_ProximityOperator_Prox_lessThan:
68 case Z_ProximityOperator_Prox_lessThanOrEqual:
70 case Z_ProximityOperator_Prox_equal:
72 case Z_ProximityOperator_Prox_greaterThanOrEqual:
74 case Z_ProximityOperator_Prox_greaterThan:
76 case Z_ProximityOperator_Prox_notEqual:
80 /* phonetic is not supported in CQL */
84 /* stem is not supported in CQL */
88 /* relevance is not supported in CQL ?*/
91 /* Invalid relation */
97 /* Can we have a complex relation value?
98 Should we implement something?
102 server_choice = 0; /* Which ServerChoice if relation? */
106 return "cql.serverChoice";
110 static int rpn2cql_attr(cql_transform_t ct,
111 Z_AttributeList *attributes, WRBUF w)
113 const char *relation = cql_lookup_reverse(ct, "relation.", attributes);
114 const char *index = cql_lookup_reverse(ct, "index.", attributes);
115 const char *structure = cql_lookup_reverse(ct, "structure.", attributes);
117 /* if transform (properties) do not match, we'll just use a USE
118 string attribute (bug #2978) */
120 index = lookup_index_from_string_attr(attributes);
122 /* Attempt to fix bug #2978): Look for a relation attribute */
124 relation = lookup_relation_index_from_attr(attributes);
128 cql_transform_set_error(ct,
129 YAZ_BIB1_UNSUPP_USE_ATTRIBUTE, 0);
132 /* for serverChoice we omit index+relation+structure */
133 if (strcmp(index, "cql.serverChoice"))
135 wrbuf_puts(w, index);
138 if (!strcmp(relation, "exact"))
140 else if (!strcmp(relation, "eq"))
142 else if (!strcmp(relation, "le"))
144 else if (!strcmp(relation, "ge"))
146 /* Missing mapping of not equal, phonetic, stem and relevance */
147 wrbuf_puts(w, relation);
154 if (strcmp(structure, "*"))
157 wrbuf_puts(w, structure);
165 static int rpn2cql_simple(cql_transform_t ct,
166 void (*pr)(const char *buf, void *client_data),
168 Z_Operand *q, WRBUF w)
171 if (q->which != Z_Operand_APT)
174 cql_transform_set_error(ct, YAZ_BIB1_RESULT_SET_UNSUPP_AS_A_SEARCH_TERM, 0);
178 Z_AttributesPlusTerm *apt = q->u.attributesPlusTerm;
179 Z_Term *term = apt->term;
180 const char *sterm = 0;
184 ret = rpn2cql_attr(ct, apt->attributes, w);
189 lterm = term->u.general->len;
190 sterm = (const char *) term->u.general->buf;
193 wrbuf_printf(w, ODR_INT_PRINTF, *term->u.numeric);
195 case Z_Term_characterString:
196 sterm = term->u.characterString;
197 lterm = strlen(sterm);
201 cql_transform_set_error(ct, YAZ_BIB1_TERM_TYPE_UNSUPP, 0);
208 for (i = 0 ; i < lterm; i++)
213 wrbuf_write(w, sterm, lterm);
218 pr(wrbuf_cstr(w), client_data);
223 static int rpn2cql_structure(cql_transform_t ct,
224 void (*pr)(const char *buf, void *client_data),
226 Z_RPNStructure *q, int nested,
229 if (q->which == Z_RPNStructure_simple)
230 return rpn2cql_simple(ct, pr, client_data, q->u.simple, w);
233 Z_Operator *op = q->u.complex->roperator;
237 pr("(", client_data);
239 r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s1, 1, w);
245 pr(" and ", client_data);
248 pr(" or ", client_data);
250 case Z_Operator_and_not:
251 pr(" not ", client_data);
253 case Z_Operator_prox:
254 cql_transform_set_error(ct, YAZ_BIB1_UNSUPP_SEARCH, 0);
257 r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s2, 1, w);
259 pr(")", client_data);
264 int cql_transform_rpn2cql_stream(cql_transform_t ct,
265 void (*pr)(const char *buf, void *client_data),
270 WRBUF w = wrbuf_alloc();
271 cql_transform_set_error(ct, 0, 0);
272 r = rpn2cql_structure(ct, pr, client_data, q->RPNStructure, 0, w);
278 int cql_transform_rpn2cql_wrbuf(cql_transform_t ct,
282 return cql_transform_rpn2cql_stream(ct, wrbuf_vputs, w, q);
288 * c-file-style: "Stroustrup"
289 * indent-tabs-mode: nil
291 * vim: shiftwidth=4 tabstop=8 expandtab