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