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