Version 5.7.2
[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     {
120         wrbuf_rewind(w);
121         return YAZ_BIB1_UNSUPP_USE_ATTRIBUTE;
122     }
123     /* for serverChoice we omit index+relation+structure */
124     if (strcmp(index, "cql.serverChoice"))
125     {
126         wrbuf_puts(w, index);
127         if (relation)
128         {
129             if (!strcmp(relation, "exact"))
130                 relation = "==";
131             else if (!strcmp(relation, "eq"))
132                 relation = "=";
133             else if (!strcmp(relation, "le"))
134                 relation = "<=";
135             else if (!strcmp(relation, "ge"))
136                 relation = ">=";
137             /* Missing mapping of not equal, phonetic, stem and relevance */
138             wrbuf_puts(w, relation);
139         }
140         else
141             wrbuf_puts(w, "=");
142
143         if (structure)
144         {
145             if (strcmp(structure, "*"))
146             {
147                 wrbuf_puts(w, "/");
148                 wrbuf_puts(w, structure);
149                 wrbuf_puts(w, " ");
150             }
151         }
152     }
153     return 0;
154 }
155
156 static Odr_int lookup_truncation(Z_AttributeList *attributes)
157 {
158     int j;
159     for (j = 0; j < attributes->num_attributes; j++)
160     {
161         Z_AttributeElement *ae = attributes->attributes[j];
162         if (*ae->attributeType == 5) /* truncation attribute */
163         {
164             if (ae->which == Z_AttributeValue_numeric)
165                 return *(ae->value.numeric);
166         }
167     }
168     /* No truncation specified */
169     return 0;
170 }
171
172 static int rpn2cql_simple(cql_transform_t ct,
173                           void (*pr)(const char *buf, void *client_data),
174                           void *client_data,
175                           Z_Operand *q, WRBUF w)
176 {
177     if (q->which != Z_Operand_APT)
178     {
179         wrbuf_rewind(w);
180         return YAZ_BIB1_RESULT_SET_UNSUPP_AS_A_SEARCH_TERM;
181     }
182     else
183     {
184         Z_AttributesPlusTerm *apt = q->u.attributesPlusTerm;
185         Z_Term *term = apt->term;
186         const char *sterm = 0;
187         size_t lterm = 0;
188         Odr_int trunc = lookup_truncation(apt->attributes);
189         size_t i;
190         int r;
191
192         wrbuf_rewind(w);
193         r = rpn2cql_attr(ct, apt->attributes, w);
194         if (r)
195             return r;
196
197         switch (term->which)
198         {
199         case Z_Term_general:
200             lterm = term->u.general->len;
201             sterm = (const char *) term->u.general->buf;
202             break;
203         case Z_Term_numeric:
204             wrbuf_printf(w, ODR_INT_PRINTF, *term->u.numeric);
205             break;
206         case Z_Term_characterString:
207             sterm = term->u.characterString;
208             lterm = strlen(sterm);
209             break;
210         default:
211             wrbuf_rewind(w);
212             wrbuf_printf(w, "%d", term->which);
213             return YAZ_BIB1_TERM_TYPE_UNSUPP;
214         }
215
216         if (trunc <= 3 || trunc == 100 || trunc == 102 || trunc == 104)
217         {
218             int quote_it = 0;
219             for (i = 0 ; i < lterm; i++)
220                 if (strchr(" ()=></", sterm[i]))
221                 {
222                     quote_it = 1;
223                     break;
224                 }
225             if (lterm == 0)
226                 quote_it = 1;
227             if (quote_it)
228                 wrbuf_puts(w, "\"");
229             if (trunc == 2 || trunc == 3)
230                 wrbuf_puts(w, "*");
231             for (i = 0; i < lterm; i++)
232             {
233                 if (sterm[i] == '\\' && i < lterm - 1)
234                 {
235                     i++;
236                     if (strchr("*?\"\\", sterm[i]))
237                         wrbuf_putc(w, '\\');
238                     wrbuf_putc(w, sterm[i]);
239                 }
240                 else if (trunc == 102 && sterm[i] == '.' && sterm[i+1] == '*')
241                 {
242                     wrbuf_putc(w, '*');
243                     i++;
244                 }
245                 else if (trunc == 102 && sterm[i] == '.')
246                     wrbuf_putc(w, '?');
247                 else if (trunc == 104 && sterm[i] == '?')
248                     wrbuf_putc(w, '*');
249                 else if (trunc == 104 && sterm[i] == '#')
250                     wrbuf_putc(w, '?');
251                 else if (strchr("*?\"", sterm[i]))
252                 {
253                     wrbuf_putc(w, '\\');
254                     wrbuf_putc(w, sterm[i]);
255                 }
256                 else
257                     wrbuf_putc(w, sterm[i]);
258             }
259             if (trunc == 1 || trunc == 3)
260                 wrbuf_puts(w, "*");
261             if (quote_it)
262                 wrbuf_puts(w, "\"");
263         }
264         else
265         {
266             wrbuf_rewind(w);
267             wrbuf_printf(w, ODR_INT_PRINTF, trunc);
268             return YAZ_BIB1_UNSUPP_TRUNCATION_ATTRIBUTE;
269         }
270         pr(wrbuf_cstr(w), client_data);
271     }
272     return 0;
273 }
274
275
276 static int rpn2cql_structure(cql_transform_t ct,
277                              void (*pr)(const char *buf, void *client_data),
278                              void *client_data,
279                              Z_RPNStructure *q, int nested,
280                              WRBUF w)
281 {
282     if (q->which == Z_RPNStructure_simple)
283         return rpn2cql_simple(ct, pr, client_data, q->u.simple, w);
284     else
285     {
286         Z_Operator *op = q->u.complex->roperator;
287         Z_ProximityOperator *prox;
288         int r;
289
290         if (nested)
291             pr("(", client_data);
292
293         r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s1, 1, w);
294         if (r)
295             return r;
296         switch (op->which)
297         {
298         case  Z_Operator_and:
299             pr(" and ", client_data);
300             break;
301         case  Z_Operator_or:
302             pr(" or ", client_data);
303             break;
304         case  Z_Operator_and_not:
305             pr(" not ", client_data);
306             break;
307         case  Z_Operator_prox: {
308             pr(" prox", client_data);
309             prox = op->u.prox;
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)
318                 {
319                     wrbuf_rewind(w);
320                     return YAZ_BIB1_UNSUPP_SEARCH;
321                 }
322                 pr(op2name[*prox->relationType-1], client_data);
323                 sprintf(buf, "%ld", (long) *prox->distance);
324                 pr(buf, client_data);
325             }
326             if (prox->ordered) {
327                 if (*prox->ordered) {
328                     pr("/ordered", client_data);
329                 } else {
330                     pr("/unordered", client_data);
331                 }
332             }
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);
337             }
338             pr(" ", client_data);
339             break;
340         }
341         }
342         r = rpn2cql_structure(ct, pr, client_data, q->u.complex->s2, 1, w);
343         if (nested)
344             pr(")", client_data);
345         return r;
346     }
347 }
348
349 int cql_transform_rpn2cql_stream_r(cql_transform_t ct,
350                                    WRBUF addinfo,
351                                    void (*pr)(const char *buf, void *client_data),
352                                    void *client_data,
353                                    Z_RPNQuery *q)
354 {
355     /* addinfo (w) is used for both addinfo and house-keeping ! */
356     int r = rpn2cql_structure(ct, pr, client_data, q->RPNStructure, 0, addinfo);
357     if (!r)
358         wrbuf_rewind(addinfo); /* no additional info if no error */
359     return r;
360 }
361
362
363 int cql_transform_rpn2cql_stream(cql_transform_t ct,
364                                  void (*pr)(const char *buf, void *client_data),
365                                  void *client_data,
366                                  Z_RPNQuery *q)
367 {
368     WRBUF w = wrbuf_alloc();
369     int r = cql_transform_rpn2cql_stream_r(ct, w, pr, client_data, q);
370     if (r)
371         cql_transform_set_error(ct, r, wrbuf_len(w) ? wrbuf_cstr(w) : 0);
372     wrbuf_destroy(w);
373     return r;
374 }
375
376
377 int cql_transform_rpn2cql_wrbuf(cql_transform_t ct,
378                                 WRBUF w,
379                                 Z_RPNQuery *q)
380 {
381     return cql_transform_rpn2cql_stream(ct, wrbuf_vp_puts, w, q);
382 }
383
384 /*
385  * Local variables:
386  * c-basic-offset: 4
387  * c-file-style: "Stroustrup"
388  * indent-tabs-mode: nil
389  * End:
390  * vim: shiftwidth=4 tabstop=8 expandtab
391  */
392