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