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