Towards 2.1.44. Bump copyright year.
[yaz-moved-to-github.git] / include / yaz / cql.h
1 /*
2  * Copyright (c) 1995-2007, 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 /* $Id: cql.h,v 1.17 2007-01-03 08:42:14 adam Exp $ */
28
29 /** \file cql.h
30     \brief Header with public definitions about CQL.
31 */
32
33 #ifndef CQL_H_INCLUDED
34 #define CQL_H_INCLUDED
35 #include <stdio.h>
36 #include <yaz/nmem.h>
37
38 YAZ_BEGIN_CDECL
39
40 /** CQL parser handle */
41 typedef struct cql_parser *CQL_parser;
42
43 /**
44  * Creates a CQL parser.
45  * Returns CQL parser handle or NULL if parser could not be created.
46  */
47 YAZ_EXPORT 
48 CQL_parser cql_parser_create(void);
49
50 /**
51  * Destroys a 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 /**
59  * Parses a CQL string query.
60  *
61  * Returns 0 if on success; non-zero (error code) on failure.
62  */
63 YAZ_EXPORT 
64 int cql_parser_string(CQL_parser cp, const char *str);
65
66 /**
67  * Parses a CQL query - streamed query.
68  *
69  * This function is similar to cql_parser_string but takes a
70  * functions to read each query character from a stream.
71  *
72  * The functions pointers getbytes, ungetbyte are similar to
73  * that known from stdios getc, ungetc.
74  *
75  * Returns 0 if on success; non-zero (error code) on failure.
76  */
77 YAZ_EXPORT 
78 int cql_parser_stream(CQL_parser cp,
79                       int (*getbyte)(void *client_data),
80                       void (*ungetbyte)(int b, void *client_data),
81                       void *client_data);
82
83 /**
84  * Parses a CQL query from a FILE handle.
85  *
86  * This function is similar to cql_parser_string but reads from 
87  * stdio FILE handle instead.
88  *
89  * Returns 0 if on success; non-zero (error code) on failure.
90  */
91 YAZ_EXPORT
92 int cql_parser_stdio(CQL_parser cp, FILE *f);
93
94 /**
95  * The node in a CQL parse tree. 
96  */
97 #define CQL_NODE_ST 1
98 #define CQL_NODE_BOOL 2
99 struct cql_node {
100     /** node type */
101     int which;
102     union {
103         /** which == CQL_NODE_ST */
104         struct {
105             /** CQL index */
106             char *index;
107             /** CQL index URI or NULL if no URI */
108             char *index_uri;
109             /** Search term */
110             char *term;
111             /** relation */
112             char *relation;
113             /** relation URL or NULL if no relation URI) */
114             char *relation_uri;
115             /** relation modifiers */
116             struct cql_node *modifiers;
117         } st;
118         /** which == CQL_NODE_BOOL */
119         struct {
120             /** operator name "and", "or", ... */
121             char *value;
122             /** left operand */
123             struct cql_node *left;
124             /** right operand */ 
125             struct cql_node *right;
126             /** modifiers (NULL for no list) */
127             struct cql_node *modifiers;
128         } boolean;
129     } u;
130 };
131
132 /**
133  * Private structure that describes the CQL properties (profile)
134  */
135 struct cql_properties;
136
137 /**
138  * Structure used by cql_buf_write_handlre
139  */
140 struct cql_buf_write_info {
141     int max;
142     int off;
143     char *buf;
144 };
145
146 /**
147  * Handler for cql_buf_write_info *
148  */
149 YAZ_EXPORT
150 void cql_buf_write_handler (const char *b, void *client_data);
151
152 /**
153  * Prints a CQL node and all sub nodes. Hence this function
154  * prints the parse tree which is as returned by cql_parser_result.
155  */
156 YAZ_EXPORT
157 void cql_node_print(struct cql_node *cn);
158
159 /**
160  * This function creates a search clause node (st).
161  */
162 YAZ_EXPORT
163 struct cql_node *cql_node_mk_sc(NMEM nmem, const char *index,
164                                 const char *relation, const char *term);
165
166 /**
167  * This function applies a prefix+uri to "unresolved" index and relation
168  * URIs.
169  *
170  * "unresolved" URIs are those nodes where member index_uri / relation_uri
171  * is NULL.
172  */
173 YAZ_EXPORT
174 struct cql_node *cql_apply_prefix(NMEM nmem, struct cql_node *cn,
175                                   const char *prefix, const char *uri);
176
177 /**
178  * This function creates a boolean node.
179  */
180 YAZ_EXPORT
181 struct cql_node *cql_node_mk_boolean(NMEM nmem, const char *op);
182
183 /**
184  * Destroys a node and its children.
185  */
186 YAZ_EXPORT
187 void cql_node_destroy(struct cql_node *cn);
188
189 /**
190  * Duplicate a node (returns a copy of supplied node) .
191  */
192 YAZ_EXPORT
193 struct cql_node *cql_node_dup (NMEM nmem, struct cql_node *cp);
194
195 /**
196  * This function returns the parse tree of the most recently parsed
197  * CQL query.
198  *
199  * The function returns NULL if most recently parse failed.
200  */
201 YAZ_EXPORT
202 struct cql_node *cql_parser_result(CQL_parser cp);
203
204 /**
205  * This function converts a CQL node tree to XCQL and writes the
206  * resulting XCQL to a user-defined output stream.
207  */
208 YAZ_EXPORT
209 void cql_to_xml(struct cql_node *cn, 
210                 void (*pr)(const char *buf, void *client_data),
211                 void *client_data);
212 /**
213  * This function converts a CQL node tree to XCQL and writes the
214  * resulting XCQL to a FILE handle (stdio) 
215  */
216 YAZ_EXPORT
217 void cql_to_xml_stdio(struct cql_node *cn, FILE *f);
218
219 /**
220  * This function converts a CQL node tree to XCQL and writes
221  * the resulting XCQL to a buffer 
222  */
223 YAZ_EXPORT
224 int cql_to_xml_buf(struct cql_node *cn, char *out, int max);
225
226 /**
227  * Utility function that prints to a FILE.
228  */
229 YAZ_EXPORT
230 void cql_fputs(const char *buf, void *client_data);
231
232 /**
233  * The CQL transform handle. The transform describes how to
234  * convert from CQL to PQF (Type-1 AKA RPN).
235  */
236 typedef struct cql_transform_t_ *cql_transform_t;
237
238 /**
239  * Creates a CQL transform handle. The transformation spec is read from
240  * a FILE handle (which is assumed opened in read mode).
241  */
242 YAZ_EXPORT
243 cql_transform_t cql_transform_open_FILE (FILE *f);
244
245 /**
246  * Creates a CQL transform handle. The transformation spec is read from
247  * a file with the filename given.
248  */
249 YAZ_EXPORT
250 cql_transform_t cql_transform_open_fname(const char *fname);
251
252 /**
253  * Destroys a CQL transform handle.
254  */
255 YAZ_EXPORT
256 void cql_transform_close(cql_transform_t ct);
257
258 /**
259  * Performs a CQL transform to PQF given a CQL node tree and a CQL
260  * transformation handle. The result is written to a user-defined stream.
261  */
262 YAZ_EXPORT
263 void cql_transform_pr(cql_transform_t ct,
264                       struct cql_node *cn,
265                       void (*pr)(const char *buf, void *client_data),
266                       void *client_data);
267
268 /**
269  * Performs a CQL transform to PQF given a CQL node tree and a CQL
270  * transformation handle. The result is written to a file specified by
271  * FILE handle (which must be opened for writing).
272  */
273 YAZ_EXPORT
274 int cql_transform_FILE(cql_transform_t ct,
275                        struct cql_node *cn, FILE *f);
276
277 /**
278  * Performs a CQL transform to PQF given a CQL node tree and a CQL
279  * transformation handle. The result is written to a buffer. 
280  */
281 YAZ_EXPORT
282 int cql_transform_buf(cql_transform_t ct,
283                       struct cql_node *cn, char *out, int max);
284 /**
285  * Returns error code and additional information from last transformation.
286  * Performs a CQL transform given a CQL node tree and a CQL transformation.
287  */
288 YAZ_EXPORT
289 int cql_transform_error(cql_transform_t ct, const char **addinfo);
290
291 /**
292  * Returns the CQL message corresponding to a given error code.
293  */
294 YAZ_EXPORT
295 const char *cql_strerror(int code);
296
297 /**
298  * Returns the standard CQL context set URI.
299  */
300 YAZ_EXPORT
301 const char *cql_uri(void);
302
303 /**
304  * Compares two CQL strings (for relations, operators, etc)
305  * (unfortunately defined as case-insensitive unlike XML etc)
306  */
307 YAZ_EXPORT
308 int cql_strcmp(const char *s1, const char *s2);
309
310 /**
311  * Compares two CQL strings at most n bytes
312  * (unfortunately defined as case-insensitive unlike XML etc)
313  */
314 YAZ_EXPORT
315 int cql_strncmp(const char *s1, const char *s2, size_t n);
316
317 YAZ_END_CDECL
318
319 #endif
320 /* CQL_H_INCLUDED */
321 /*
322  * Local variables:
323  * c-basic-offset: 4
324  * indent-tabs-mode: nil
325  * End:
326  * vim: shiftwidth=4 tabstop=8 expandtab
327  */
328