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