cql_transform_rpn2cql_stream back to error in cql_transform_t
[yaz-moved-to-github.git] / src / rpn2cql.c
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
5 /**
6  * \file
7  * \brief Implements RPN to CQL conversion
8  */
9 #if HAVE_CONFIG_H
10 #include <config.h>
11 #endif
12
13 #include <assert.h>
14 #include <stdlib.h>
15 #include <string.h>
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() */
22
23 static const char *lookup_index_from_string_attr(Z_AttributeList *attributes)
24 {
25     int j;
26     int server_choice = 1;
27     for (j = 0; j < attributes->num_attributes; j++)
28     {
29         Z_AttributeElement *ae = attributes->attributes[j];
30         if (*ae->attributeType == 1) /* use attribute */
31         {
32             if (ae->which == Z_AttributeValue_complex)
33             {
34                 Z_ComplexAttribute *ca = ae->value.complex;
35                 int i;
36                 for (i = 0; i < ca->num_list; i++)
37                 {
38                     Z_StringOrNumeric *son = ca->list[i];
39                     if (son->which == Z_StringOrNumeric_string)
40                         return son->u.string;
41                 }
42             }
43             server_choice = 0; /* not serverChoice because we have use attr */
44         }
45     }
46     if (server_choice)
47         return "cql.serverChoice";
48     return 0;
49 }
50
51 static const char *lookup_relation_index_from_attr(Z_AttributeList *attributes)
52 {
53     int j;
54     for (j = 0; j < attributes->num_attributes; j++)
55     {
56         Z_AttributeElement *ae = attributes->attributes[j];
57         if (*ae->attributeType == 2) /* relation attribute */
58         {
59             if (ae->which == Z_AttributeValue_numeric)
60             {
61                 /* Only support for numeric relation */
62                 Odr_int *relation = ae->value.numeric;
63                 /* map this numeric to representation in CQL */
64                 switch (*relation)
65                 {
66                     /* Unsure on whether this is the relation attribute constants? */
67                 case Z_ProximityOperator_Prox_lessThan:
68                     return "<";
69                 case Z_ProximityOperator_Prox_lessThanOrEqual:
70                     return "<=";
71                 case Z_ProximityOperator_Prox_equal:
72                     return "=";
73                 case Z_ProximityOperator_Prox_greaterThanOrEqual:
74                     return ">=";
75                 case Z_ProximityOperator_Prox_greaterThan:
76                     return ">";
77                 case Z_ProximityOperator_Prox_notEqual:
78                     return "<>";
79                 case 100:
80                     /* phonetic is not supported in CQL */
81                     return 0;
82                 case 101:
83                     /* stem is not supported in CQL */
84                     return 0;
85                 case 102:
86                     /* relevance is supported in CQL, but not implemented yet */
87                     return 0;
88                 default:
89                     /* Invalid relation */
90                     return 0;
91                 }
92             }
93             else {
94                 /*  Can we have a complex relation value?
95                     Should we implement something?
96                 */
97             }
98         }
99     }
100     return "=";
101 }
102
103 static int rpn2cql_attr(cql_transform_t ct,
104                         Z_AttributeList *attributes, WRBUF w)
105 {
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);
109
110     /* if transform (properties) do not match, we'll just use a USE string attribute (bug #2978) */
111     if (!index)
112         index = lookup_index_from_string_attr(attributes);
113
114     /* Attempt to fix bug #2978: Look for a relation attribute */
115     if (!relation)
116         relation = lookup_relation_index_from_attr(attributes);
117
118     if (!index)
119         return YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
120     /* for serverChoice we omit index+relation+structure */
121     if (strcmp(index, "cql.serverChoice"))
122     {
123         wrbuf_puts(w, index);
124         if (relation)
125         {
126             if (!strcmp(relation, "exact"))
127                 relation = "==";
128             else if (!strcmp(relation, "eq"))
129                 relation = "=";
130             else if (!strcmp(relation, "le"))
131                 relation = "<=";
132             else if (!strcmp(relation, "ge"))
133                 relation = ">=";
134             /* Missing mapping of not equal, phonetic, stem and relevance */
135             wrbuf_puts(w, relation);
136         }
137         else
138             wrbuf_puts(w, "=");
139
140         if (structure)
141         {
142             if (strcmp(structure, "*"))
143             {
144                 wrbuf_puts(w, "/");
145                 wrbuf_puts(w, structure);
146                 wrbuf_puts(w, " ");
147             }
148         }
149     }
150     return 0;
151 }
152
153 static Odr_int lookup_truncation(Z_AttributeList *attributes)
154 {
155     int j;
156     for (j = 0; j < attributes->num_attributes; j++)
157     {
158         Z_AttributeElement *ae = attributes->attributes[j];
159         if (*ae->attributeType == 5) /* truncation attribute */
160         {
161             if (ae->which == Z_AttributeValue_numeric)
162                 return *(ae->value.numeric);
163         }
164     }
165     /* No truncation specified */
166     return 0;
167 }
168
169 static int rpn2cql_simple(cql_transform_t ct,
170                           void (*pr)(const char *buf, void *client_data),
171                           void *client_data,
172                           Z_Operand *q, WRBUF w)
173 {
174     if (q->which != Z_Operand_APT)
175         return YAZ_BIB1_RESULT_SET_UNSUPP_AS_A_SEARCH_TERM;
176     else
177     {
178         Z_AttributesPlusTerm *apt = q->u.attributesPlusTerm;
179         Z_Term *term = apt->term;
180         const char *sterm = 0;
181         size_t lterm = 0;
182         Odr_int trunc = lookup_truncation(apt->attributes);
183         size_t i;
184         int r;
185
186         wrbuf_rewind(w);
187         r = rpn2cql_attr(ct, apt->attributes, w);
188         if (r)
189             return r;
190
191         switch (term->which)
192         {
193         case Z_Term_general:
194             lterm = term->u.general->len;
195             sterm = (const char *) term->u.general->buf;
196             break;
197         case Z_Term_numeric:
198             wrbuf_printf(w, ODR_INT_PRINTF, *term->u.numeric);
199             break;
200         case Z_Term_characterString:
201             sterm = term->u.characterString;
202             lterm = strlen(sterm);
203             break;
204         default:
205             return YAZ_BIB1_TERM_TYPE_UNSUPP;
206         }
207
208         if (trunc <= 3 || trunc == 100 || trunc == 102 || trunc == 104)
209         {
210             int quote_it = 0;
211             for (i = 0 ; i < lterm; i++)
212                 if (strchr(" ()=></", sterm[i]))
213                 {
214                     quote_it = 1;
215                     break;
216                 }
217             if (lterm == 0)
218                 quote_it = 1;
219             if (quote_it)
220                 wrbuf_puts(w, "\"");
221             if (trunc == 2 || trunc == 3)
222                 wrbuf_puts(w, "*");
223             for (i = 0; i < lterm; i++)
224             {
225                 if (sterm[i] == '\\' && i < lterm - 1)
226                 {
227                     i++;
228                     if (strchr("*?\"\\", sterm[i]))
229                         wrbuf_putc(w, '\\');
230                     wrbuf_putc(w, sterm[i]);
231                 }
232                 else if (trunc == 102 && sterm[i] == '.' && sterm[i+1] == '*')
233                 {
234                     wrbuf_putc(w, '*');
235                     i++;
236                 }
237                 else if (trunc == 102 && sterm[i] == '.')
238                     wrbuf_putc(w, '?');
239                 else if (trunc == 104 && sterm[i] == '?')
240                     wrbuf_putc(w, '*');
241                 else if (trunc == 104 && sterm[i] == '#')
242                     wrbuf_putc(w, '?');
243                 else if (strchr("*?\"", sterm[i]))
244                 {
245                     wrbuf_putc(w, '\\');
246                     wrbuf_putc(w, sterm[i]);
247                 }
248                 else
249                     wrbuf_putc(w, sterm[i]);
250             }
251             if (trunc == 1 || trunc == 3)
252                 wrbuf_puts(w, "*");
253             if (quote_it)
254                 wrbuf_puts(w, "\"");
255         }
256         else
257         {
258             return YAZ_BIB1_UNSUPP_TRUNCATION_ATTRIBUTE;
259         }
260         pr(wrbuf_cstr(w), client_data);
261     }
262     return 0;
263 }
264
265
266 static int rpn2cql_structure(cql_transform_t ct,
267                              void (*pr)(const char *buf, void *client_data),
268                              void *client_data,
269                              Z_RPNStructure *q, int nested,
270                              WRBUF w)
271 {
272     if (q->which == Z_RPNStructure_simple)
273         return rpn2cql_simple(ct, pr, client_data, q->u.simple, w);
274     else
275     {
276         Z_Operator *op = q->u.complex->roperator;
277         Z_ProximityOperator *prox;
278         int r;
279
280         if (nested)
281             pr("(", client_data);
282
283         r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s1, 1, w);
284         if (r)
285             return r;
286         switch (op->which)
287         {
288         case  Z_Operator_and:
289             pr(" and ", client_data);
290             break;
291         case  Z_Operator_or:
292             pr(" or ", client_data);
293             break;
294         case  Z_Operator_and_not:
295             pr(" not ", client_data);
296             break;
297         case  Z_Operator_prox: {
298             pr(" prox", client_data);
299             prox = op->u.prox;
300             /* No way to express Odr_bool *exclusion -- ignore it */
301             if (prox->distance) {
302                 char buf[21]; /* Enough for any 64-bit int */
303                 char *op2name[6] = { "<", "<=", "=", ">=", ">","<>" };
304                 pr("/distance", client_data);
305                 if (!prox->relationType ||
306                     *prox->relationType < Z_ProximityOperator_Prox_lessThan ||
307                     *prox->relationType > Z_ProximityOperator_Prox_notEqual)
308                 {
309                     return YAZ_BIB1_UNSUPP_SEARCH;
310                 }
311                 pr(op2name[*prox->relationType-1], client_data);
312                 sprintf(buf, "%ld", (long) *prox->distance);
313                 pr(buf, client_data);
314             }
315             if (prox->ordered) {
316                 if (*prox->ordered) {
317                     pr("/ordered", client_data);
318                 } else {
319                     pr("/unordered", client_data);
320                 }
321             }
322             if (prox->which != Z_ProximityOperator_known ||
323                 *prox->u.known != Z_ProxUnit_word) {
324                     pr("/unit=", client_data);
325                     pr(yaz_prox_unit_name(prox), client_data);
326             }
327             pr(" ", client_data);
328             break;
329         }
330         }
331         r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s2, 1, w);
332         if (nested)
333             pr(")", client_data);
334         return r;
335     }
336 }
337
338 int cql_transform_rpn2cql_stream(cql_transform_t ct,
339                                  void (*pr)(const char *buf, void *client_data),
340                                  void *client_data,
341                                  Z_RPNQuery *q)
342 {
343     int r;
344     WRBUF w = wrbuf_alloc();
345     r = rpn2cql_structure(ct, pr, client_data, q->RPNStructure, 0, w);
346     if (r)
347         cql_transform_set_error(ct, r, 0);
348     wrbuf_destroy(w);
349     return r;
350 }
351
352
353 int cql_transform_rpn2cql_wrbuf(cql_transform_t ct,
354                                 WRBUF w,
355                                 Z_RPNQuery *q)
356 {
357     return cql_transform_rpn2cql_stream(ct, wrbuf_vp_puts, w, q);
358 }
359
360 /*
361  * Local variables:
362  * c-basic-offset: 4
363  * c-file-style: "Stroustrup"
364  * indent-tabs-mode: nil
365  * End:
366  * vim: shiftwidth=4 tabstop=8 expandtab
367  */
368