148a9e0c7552dcd5ac117628a986e569025f001a
[yaz-moved-to-github.git] / include / yaz / cql.h
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) 1995-2011 Index Data.
3  * All rights reserved.
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  *
7  *     * Redistributions of source code must retain the above copyright
8  *       notice, this list of conditions and the following disclaimer.
9  *     * Redistributions in binary form must reproduce the above copyright
10  *       notice, this list of conditions and the following disclaimer in the
11  *       documentation and/or other materials provided with the distribution.
12  *     * Neither the name of Index Data nor the names of its contributors
13  *       may be used to endorse or promote products derived from this
14  *       software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27
28 /** \file cql.h
29     \brief Header with public definitions about CQL.
30 */
31
32 #ifndef CQL_H_INCLUDED
33 #define CQL_H_INCLUDED
34 #include <stdio.h>
35 #include <yaz/nmem.h>
36
37 YAZ_BEGIN_CDECL
38
39 /** \brief CQL parser handle (opaque pointer) */
40 typedef struct cql_parser *CQL_parser;
41
42 /** \brief creates a CQL parser.
43     \returns CCL parser
44     
45     Returns CQL parser or NULL if parser could not be created.
46  */
47 YAZ_EXPORT 
48 CQL_parser cql_parser_create(void);
49
50 /** \brief destroys a CQL parser.
51     \param cp CQL parser
52
53     This function does nothing if NULL if received.
54  */
55 YAZ_EXPORT 
56 void cql_parser_destroy(CQL_parser cp);
57
58 /** \brief parses a CQL query (string)
59     \param cp CQL parser
60     \param str CQL string
61     \retval 0 success
62     \retval !=0 failure
63  */
64 YAZ_EXPORT 
65 int cql_parser_string(CQL_parser cp, const char *str);
66
67 /** \brief parses CQL query (query stream)
68     \param cp CQL parser
69     \param getbyte function which reads one character from stream
70     \param ungetbyte function which unreads one character from stream
71     \param client_data data to be passed to stream functions
72     \retval 0 success
73     \retval !=0 failure
74     
75     This function is similar to cql_parser_string but takes a
76     functions to read each query character from a stream.
77     
78     The functions pointers getbytes, ungetbyte are similar to
79     that known from stdios getc, ungetc.
80 */
81 YAZ_EXPORT 
82 int cql_parser_stream(CQL_parser cp,
83                       int (*getbyte)(void *client_data),
84                       void (*ungetbyte)(int b, void *client_data),
85                       void *client_data);
86
87 /** \brief parses CQL query (from FILE)
88     \param cp CQL parser
89     \param f file where query is read from
90     \retval 0 success
91     \retval !=0 failure
92     
93     This function is similar to cql_parser_string but reads from 
94     stdio FILE handle instead.
95 */
96 YAZ_EXPORT
97 int cql_parser_stdio(CQL_parser cp, FILE *f);
98
99 /** \brief Node type: search term */
100 #define CQL_NODE_ST 1
101 /** \brief Node type: boolean */
102 #define CQL_NODE_BOOL 2
103 /** \brief Node type: sortby single spec */
104 #define CQL_NODE_SORT 3
105
106 /** \brief CQL parse tree (node)
107  */
108 struct cql_node {
109     /** node type */
110     int which;
111     union {
112         /** which == CQL_NODE_ST */
113         struct {
114             /** CQL index */
115             char *index;
116             /** CQL index URI or NULL if no URI */
117             char *index_uri;
118             /** Search term */
119             char *term;
120             /** relation */
121             char *relation;
122             /** relation URL or NULL if no relation URI) */
123             char *relation_uri;
124             /** relation modifiers */
125             struct cql_node *modifiers;
126             /** term list */
127             struct cql_node *extra_terms;
128         } st;
129         /** which == CQL_NODE_BOOL */
130         struct {
131             /** operator name "and", "or", ... */
132             char *value;
133             /** left operand */
134             struct cql_node *left;
135             /** right operand */ 
136             struct cql_node *right;
137             /** modifiers (NULL for no list) */
138             struct cql_node *modifiers;
139         } boolean;
140         /** which == CQL_NODE_SORT */
141         struct {
142             char *index;
143             /** next spec */
144             struct cql_node *next;
145             /** modifiers (NULL for no list) */
146             struct cql_node *modifiers;
147             /** search node */
148             struct cql_node *search;
149         } sort;
150     } u;
151 };
152
153 /** \brief Private structure that describes the CQL properties (profile)
154  */
155 struct cql_properties;
156
157 /** \brief Structure used by cql_buf_write_handler
158  */
159 struct cql_buf_write_info {
160     int max;
161     int off;
162     char *buf;
163 };
164
165 /** \brief Handler for cql_buf_write_info
166  */
167 YAZ_EXPORT
168 void cql_buf_write_handler(const char *b, void *client_data);
169
170 /** \brief Prints a CQL node and all sub nodes.
171     Hence this function prints the parse tree which is as returned by
172     cql_parser_result.
173 */
174 YAZ_EXPORT
175 void cql_node_print(struct cql_node *cn);
176
177 /** \brief creates a search clause node (st). */
178 YAZ_EXPORT
179 struct cql_node *cql_node_mk_sc(NMEM nmem, const char *index,
180                                 const char *relation, const char *term);
181
182 /** \brief applies a prefix+uri to "unresolved" index and relation URIs.
183     "unresolved" URIs are those nodes where member index_uri / relation_uri
184     is NULL.
185 */
186 YAZ_EXPORT
187 struct cql_node *cql_apply_prefix(NMEM nmem, struct cql_node *cn,
188                                   const char *prefix, const char *uri);
189
190 /** \brief creates a boolean node. */
191 YAZ_EXPORT
192 struct cql_node *cql_node_mk_boolean(NMEM nmem, const char *op);
193
194 /** \brief creates a sort single spec node. */
195 YAZ_EXPORT
196 struct cql_node *cql_node_mk_sort(NMEM nmem, const char *index,
197     struct cql_node *modifiers);
198
199 /** \brief destroys a node and its children. */
200 YAZ_EXPORT
201 void cql_node_destroy(struct cql_node *cn);
202
203 /** duplicates a node (returns a copy of supplied node) . */
204 YAZ_EXPORT
205 struct cql_node *cql_node_dup (NMEM nmem, struct cql_node *cp);
206
207 /** \brief returns the parse tree of the most recently parsed CQL query.
208     \param cp CQL parser
209     \returns CQL node or NULL for failure
210 */
211 YAZ_EXPORT
212 struct cql_node *cql_parser_result(CQL_parser cp);
213
214 /** \brief returns the sortby tree of the most recently parsed CQL query.
215     \param cp CQL parser
216     \returns CQL node or NULL for failure
217 */
218 YAZ_EXPORT
219 struct cql_node *cql_parser_sort_result(CQL_parser cp);
220
221 /** \brief converts CQL tree to XCQL and writes to user-defined stream
222     \param cn CQL node (tree)
223     \param pr print function
224     \param client_data data to be passed to pr function
225  */
226 YAZ_EXPORT
227 void cql_to_xml(struct cql_node *cn, 
228                 void (*pr)(const char *buf, void *client_data),
229                 void *client_data);
230 /** \brief converts CQL tree to XCQL and writes to file
231     \param cn CQL node (tree)
232     \param f file handle
233  */
234 YAZ_EXPORT
235 void cql_to_xml_stdio(struct cql_node *cn, FILE *f);
236
237 /** \brief converts CQL tree to XCQL and writes result to buffer
238     \param cn CQL node (tree)
239     \param out buffer
240     \param max size of buffer (max chars to write)
241     \returns length of resulting buffer
242  */
243 YAZ_EXPORT
244 int cql_to_xml_buf(struct cql_node *cn, char *out, int max);
245
246 /** \brief converts CQL tree to CCL and writes to user-defined stream
247     \param cn CQL node (tree)
248     \param pr print function
249     \param client_data data to be passed to pr function
250  */
251 YAZ_EXPORT
252 int cql_to_ccl(struct cql_node *cn, 
253                void (*pr)(const char *buf, void *client_data),
254                void *client_data);
255
256 /** \brief converts CQL tree to CCL and writes to file
257     \param cn CQL node (tree)
258     \param f file handle
259  */
260 YAZ_EXPORT
261 void cql_to_ccl_stdio(struct cql_node *cn, FILE *f);
262
263 /** \brief converts CQL tree to CCL and writes result to buffer
264     \param cn CQL node (tree)
265     \param out buffer
266     \param max size of buffer (max chars to write)
267     \returns length of resulting buffer
268  */
269 YAZ_EXPORT
270 int cql_to_ccl_buf(struct cql_node *cn, char *out, int max);
271
272 /** \brief stream handle for file (used by cql_to_xml_stdio) */
273 YAZ_EXPORT
274 void cql_fputs(const char *buf, void *client_data);
275
276 /** \brief CQL transform handle.
277     The transform describes how to convert from CQL to PQF (Type-1 AKA RPN).
278 */
279 typedef struct cql_transform_t_ *cql_transform_t;
280
281 /** \brief creates a CQL transform handle
282     \returns transform handle or NULL for failure
283 */
284 YAZ_EXPORT
285 cql_transform_t cql_transform_create(void);
286
287 /** \brief creates a CQL transform handle from am opened file handle
288     \param f file where transformation spec is read
289     \returns transform handle or NULL for failure
290
291     The transformation spec is read from a FILE handle which is assumed
292     opened for reading.
293 */
294 YAZ_EXPORT
295 cql_transform_t cql_transform_open_FILE (FILE *f);
296
297 /** \brief creates a CQL transform handle from a file
298     \param fname name of where transformation spec is read
299     \returns transform handle or NULL for failure
300 */
301 YAZ_EXPORT
302 cql_transform_t cql_transform_open_fname(const char *fname);
303
304
305 /** \brief defines CQL transform pattern
306     \param ct CQL transform handle
307     \param pattern pattern string
308     \param value pattern value
309     \returns 0 for succes; -1 for failure
310 */
311 YAZ_EXPORT
312 int cql_transform_define_pattern(cql_transform_t ct, const char *pattern,
313                                  const char *value);
314     
315
316
317 /** \brief destroys a CQL transform handle
318     \param ct CQL transform handle
319  */
320 YAZ_EXPORT
321 void cql_transform_close(cql_transform_t ct);
322
323 /** \brief tranforms PQF given a CQL tree
324     \param ct CQL transform handle
325     \param cn CQL node tree
326     \param pr print function
327     \param client_data data to be passed to pr
328     \retval 0 success
329     \retval != 0 error
330
331     The result is written to a user-defined stream.
332 */
333 YAZ_EXPORT
334 int cql_transform(cql_transform_t ct,
335                   struct cql_node *cn,
336                   void (*pr)(const char *buf, void *client_data),
337                   void *client_data);
338
339 /** \brief transforms PQF given a CQL tree (from FILE)
340     \param ct CQL transform handle
341     \param cn CQL tree
342     \param f FILE where output is written
343     \retval 0 success
344     \retval !=0 failure (error code)
345
346     The result is written to a file specified by FILE handle (which must
347     be opened for writing.
348 */
349 YAZ_EXPORT
350 int cql_transform_FILE(cql_transform_t ct,
351                        struct cql_node *cn, FILE *f);
352
353 /** \brief transforms PQF given a CQL tree (from FILE)
354     \param ct CQL transform handle
355     \param cn CQL tree
356     \param out buffer for output
357     \param max maximum bytes for output (size of buffer)
358     \retval 0 success
359     \retval !=0 failure (error code)
360  */
361 YAZ_EXPORT
362 int cql_transform_buf(cql_transform_t ct,
363                       struct cql_node *cn, char *out, int max);
364
365 /** \brief returns additional information for last transform
366     \param ct CQL transform handle
367     \param addinfo additional info (result)
368     \returns error code
369  */
370 YAZ_EXPORT
371 int cql_transform_error(cql_transform_t ct, const char **addinfo);
372
373 /** \brief sets error and addinfo for transform
374     \param ct CQL transform handle
375     \param error error code
376     \param addinfo additional info
377  */
378 YAZ_EXPORT
379 void cql_transform_set_error(cql_transform_t ct, int error, const char *addinfo);
380
381 /** \brief returns the CQL message corresponding to a given error code.
382     \param code error code
383     \returns text message
384 */
385 YAZ_EXPORT
386 const char *cql_strerror(int code);
387
388 /** \brief returns the standard CQL context set URI.
389     \returns CQL URI string
390 */
391 YAZ_EXPORT
392 const char *cql_uri(void);
393
394 /** \brief compares two CQL strings (ala strcmp)
395     \param s1 string 1
396     \param s2 string 2
397     \returns comparison value
398     Compares two CQL strings (for relations, operators, etc)
399     (unfortunately defined as case-insensitive unlike XML etc)
400 */
401 YAZ_EXPORT
402 int cql_strcmp(const char *s1, const char *s2);
403
404 /** \brief compares two CQL strings (ala strncmp)
405     \param s1 string 1
406     \param s2 string 2
407     \param n size
408     \returns comparison value
409     Compares two CQL strings at most n bytes
410     (unfortunately defined as case-insensitive unlike XML etc)
411  */
412 YAZ_EXPORT
413 int cql_strncmp(const char *s1, const char *s2, size_t n);
414
415 /** \brief converts CQL sortby to sortkeys (ala versions 1.1)
416     \param cn CQL tree
417     \param pr print function
418     \param client_data data to be passed to pr function
419     
420     This will take CQL_NODE_SORT entries and conver them to
421
422     path,schema,ascending,caseSensitive,missingValue
423     items..
424
425     One for each sort keys. Where
426
427     path is string index for sorting
428
429     schema is schema for sort index
430
431     ascending is a boolean (0=false, 1=true). Default is true.
432
433     caseSensitive is a boolean. Default is false.
434
435     missingValue is a string and one of 'abort', 'highValue', 'lowValue',
436     or 'omit'. Default is 'highValue'.
437
438     See also
439     http://www.loc.gov/standards/sru/sru1-1archive/search-retrieve-operation.html#sort
440 */
441 YAZ_EXPORT
442 int cql_sortby_to_sortkeys(struct cql_node *cn,
443                            void (*pr)(const char *buf, void *client_data),
444                            void *client_data);
445
446 /** \brief converts CQL sortby to sortkeys .. 
447     \param cn CQL tree
448     \param out result buffer
449     \param max size of buffer (allocated)
450     \retval 0 OK
451     \retval -1 ERROR
452 */
453 YAZ_EXPORT
454 int cql_sortby_to_sortkeys_buf(struct cql_node *cn, char *out, int max);
455
456 YAZ_END_CDECL
457
458 #endif
459 /* CQL_H_INCLUDED */
460 /*
461  * Local variables:
462  * c-basic-offset: 4
463  * c-file-style: "Stroustrup"
464  * indent-tabs-mode: nil
465  * End:
466  * vim: shiftwidth=4 tabstop=8 expandtab
467  */
468