1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) Index Data.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
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.
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.
29 \brief Header with public definitions about CQL.
32 #ifndef CQL_H_INCLUDED
33 #define CQL_H_INCLUDED
36 #include <yaz/wrbuf.h>
40 /** \brief CQL parser handle (opaque pointer) */
41 typedef struct cql_parser *CQL_parser;
43 /** \brief creates a CQL parser.
46 Returns CQL parser or NULL if parser could not be created.
49 CQL_parser cql_parser_create(void);
51 /** \brief destroys a CQL parser.
54 This function does nothing if NULL if received.
57 void cql_parser_destroy(CQL_parser cp);
59 /** \brief parses a CQL query (string)
66 int cql_parser_string(CQL_parser cp, const char *str);
68 /** \brief parses CQL query (query stream)
70 \param getbyte function which reads one character from stream
71 \param ungetbyte function which unreads one character from stream
72 \param client_data data to be passed to stream functions
76 This function is similar to cql_parser_string but takes a
77 functions to read each query character from a stream.
79 The functions pointers getbytes, ungetbyte are similar to
80 that known from stdios getc, ungetc.
83 int cql_parser_stream(CQL_parser cp,
84 int (*getbyte)(void *client_data),
85 void (*ungetbyte)(int b, void *client_data),
88 /** \brief parses CQL query (from FILE)
90 \param f file where query is read from
94 This function is similar to cql_parser_string but reads from
95 stdio FILE handle instead.
98 int cql_parser_stdio(CQL_parser cp, FILE *f);
100 /** \brief configures strict mode
102 \param mode 1=enable strict mode, 0=disable strict mode
104 This function is similar to cql_parser_string but reads from
105 stdio FILE handle instead.
108 void cql_parser_strict(CQL_parser cp, int mode);
110 /** \brief Node type: search term */
111 #define CQL_NODE_ST 1
112 /** \brief Node type: boolean */
113 #define CQL_NODE_BOOL 2
114 /** \brief Node type: sortby single spec */
115 #define CQL_NODE_SORT 3
117 /** \brief CQL parse tree (node)
123 /** which == CQL_NODE_ST */
127 /** CQL index URI or NULL if no URI */
133 /** relation URL or NULL if no relation URI) */
135 /** relation modifiers */
136 struct cql_node *modifiers;
138 struct cql_node *extra_terms;
140 /** which == CQL_NODE_BOOL */
142 /** operator name "and", "or", ... */
145 struct cql_node *left;
147 struct cql_node *right;
148 /** modifiers (NULL for no list) */
149 struct cql_node *modifiers;
151 /** which == CQL_NODE_SORT */
155 struct cql_node *next;
156 /** modifiers (NULL for no list) */
157 struct cql_node *modifiers;
159 struct cql_node *search;
164 /** \brief Private structure that describes the CQL properties (profile)
166 struct cql_properties;
168 /** \brief Structure used by cql_buf_write_handler
170 struct cql_buf_write_info {
176 /** \brief Handler for cql_buf_write_info
179 void cql_buf_write_handler(const char *b, void *client_data);
181 /** \brief Prints a CQL node and all sub nodes.
182 Hence this function prints the parse tree which is as returned by
186 void cql_node_print(struct cql_node *cn);
188 /** \brief creates a search clause node (st). */
190 struct cql_node *cql_node_mk_sc(NMEM nmem, const char *index,
191 const char *relation, const char *term);
193 /** \brief applies a prefix+uri to "unresolved" index and relation URIs.
194 "unresolved" URIs are those nodes where member index_uri / relation_uri
198 struct cql_node *cql_apply_prefix(NMEM nmem, struct cql_node *cn,
199 const char *prefix, const char *uri);
201 /** \brief creates a boolean node. */
203 struct cql_node *cql_node_mk_boolean(NMEM nmem, const char *op);
205 /** \brief creates a sort single spec node. */
207 struct cql_node *cql_node_mk_sort(NMEM nmem, const char *index,
208 struct cql_node *modifiers);
210 /** \brief destroys a node and its children. */
212 void cql_node_destroy(struct cql_node *cn);
214 /** duplicates a node (returns a copy of supplied node) . */
216 struct cql_node *cql_node_dup (NMEM nmem, struct cql_node *cp);
218 /** \brief returns the parse tree of the most recently parsed CQL query.
220 \returns CQL node or NULL for failure
223 struct cql_node *cql_parser_result(CQL_parser cp);
225 /** \brief returns the sortby tree of the most recently parsed CQL query.
227 \returns CQL node or NULL for failure
230 struct cql_node *cql_parser_sort_result(CQL_parser cp);
232 /** \brief converts CQL tree to XCQL and writes to user-defined stream
233 \param cn CQL node (tree)
234 \param pr print function
235 \param client_data data to be passed to pr function
238 void cql_to_xml(struct cql_node *cn,
239 void (*pr)(const char *buf, void *client_data),
241 /** \brief converts CQL tree to XCQL and writes to file
242 \param cn CQL node (tree)
246 void cql_to_xml_stdio(struct cql_node *cn, FILE *f);
248 /** \brief converts CQL tree to XCQL and writes result to buffer
249 \param cn CQL node (tree)
251 \param max size of buffer (max chars to write)
252 \returns length of resulting buffer
255 int cql_to_xml_buf(struct cql_node *cn, char *out, int max);
257 /** \brief converts CQL tree to CCL and writes to user-defined stream
258 \param cn CQL node (tree)
259 \param pr print function
260 \param client_data data to be passed to pr function
263 int cql_to_ccl(struct cql_node *cn,
264 void (*pr)(const char *buf, void *client_data),
267 /** \brief converts CQL tree to CCL and writes to file
268 \param cn CQL node (tree)
272 void cql_to_ccl_stdio(struct cql_node *cn, FILE *f);
274 /** \brief converts CQL tree to CCL and writes result to buffer
275 \param cn CQL node (tree)
277 \param max size of buffer (max chars to write)
279 \retval -1 conversion error
280 \retval -2 buffer too small (truncated)
283 int cql_to_ccl_buf(struct cql_node *cn, char *out, int max);
285 /** \brief stream handle for file (used by cql_to_xml_stdio) */
287 void cql_fputs(const char *buf, void *client_data);
289 /** \brief CQL transform handle.
290 The transform describes how to convert from CQL to PQF (Type-1 AKA RPN).
292 typedef struct cql_transform_t_ *cql_transform_t;
294 /** \brief creates a CQL transform handle
295 \returns transform handle or NULL for failure
298 cql_transform_t cql_transform_create(void);
300 /** \brief creates a CQL transform handle from am opened file handle
301 \param f file where transformation spec is read
302 \returns transform handle or NULL for failure
304 The transformation spec is read from a FILE handle which is assumed
308 cql_transform_t cql_transform_open_FILE (FILE *f);
310 /** \brief creates a CQL transform handle from a file
311 \param fname name of where transformation spec is read
312 \returns transform handle or NULL for failure
315 cql_transform_t cql_transform_open_fname(const char *fname);
318 /** \brief defines CQL transform pattern
319 \param ct CQL transform handle
320 \param pattern pattern string
321 \param value pattern value
322 \returns 0 for succes; -1 for failure
325 int cql_transform_define_pattern(cql_transform_t ct, const char *pattern,
330 /** \brief destroys a CQL transform handle
331 \param ct CQL transform handle
334 void cql_transform_close(cql_transform_t ct);
336 /** \brief tranforms PQF given a CQL tree (NOT re-entrant)
337 \param ct CQL transform handle
338 \param cn CQL node tree
339 \param pr print function
340 \param client_data data to be passed to pr
344 The result is written to a user-defined stream.
347 int cql_transform(cql_transform_t ct,
349 void (*pr)(const char *buf, void *client_data),
352 /** \brief tranforms PQF given a CQL tree (re-entrant)
353 \param ct CQL transform handle
354 \param cn CQL node tree
355 \param addinfo additional information (if error)
356 \param pr print function
357 \param client_data data to be passed to pr
359 \retval != 0 error code
361 The result is written to a user-defined stream.
363 int cql_transform_r(cql_transform_t ct, struct cql_node *cn,
365 void (*pr)(const char *buf, void *client_data),
368 /** \brief transforms PQF given a CQL tree from FILE (not re-entrant)
369 \param ct CQL transform handle
371 \param f FILE where output is written
373 \retval !=0 failure (error code)
375 The result is written to a file specified by FILE handle (which must
376 be opened for writing.
379 int cql_transform_FILE(cql_transform_t ct,
380 struct cql_node *cn, FILE *f);
382 /** \brief transforms PQF given a CQL tree from buffer (not re-entrant)
383 \param ct CQL transform handle
385 \param out buffer for output
386 \param max maximum bytes for output (size of buffer)
388 \retval !=0 failure (error code)
391 int cql_transform_buf(cql_transform_t ct,
392 struct cql_node *cn, char *out, int max);
394 /** \brief returns additional information for last transform
395 \param ct CQL transform handle
396 \param addinfo additional info (result)
400 int cql_transform_error(cql_transform_t ct, const char **addinfo);
402 /** \brief sets error and addinfo for transform
403 \param ct CQL transform handle
404 \param error error code
405 \param addinfo additional info
408 void cql_transform_set_error(cql_transform_t ct, int error, const char *addinfo);
410 /** \brief returns the CQL message corresponding to a given error code.
411 \param code error code
412 \returns text message
415 const char *cql_strerror(int code);
417 /** \brief returns the standard CQL context set URI.
418 \returns CQL URI string
421 const char *cql_uri(void);
423 /** \brief compares two CQL strings (ala strcmp)
426 \returns comparison value
427 Compares two CQL strings (for relations, operators, etc)
428 (unfortunately defined as case-insensitive unlike XML etc)
431 int cql_strcmp(const char *s1, const char *s2);
433 /** \brief compares two CQL strings (ala strncmp)
437 \returns comparison value
438 Compares two CQL strings at most n bytes
439 (unfortunately defined as case-insensitive unlike XML etc)
442 int cql_strncmp(const char *s1, const char *s2, size_t n);
444 /** \brief converts CQL sortby to sortkeys (ala versions 1.1)
446 \param pr print function
447 \param client_data data to be passed to pr function
449 This will take CQL_NODE_SORT entries and conver them to
451 path,schema,ascending,caseSensitive,missingValue
454 One for each sort keys. Where
456 path is string index for sorting
458 schema is schema for sort index
460 ascending is a boolean (0=false, 1=true). Default is true.
462 caseSensitive is a boolean. Default is false.
464 missingValue is a string and one of 'abort', 'highValue', 'lowValue',
465 or 'omit'. Default is 'highValue'.
468 http://www.loc.gov/standards/sru/sru1-1archive/search-retrieve-operation.html#sort
471 int cql_sortby_to_sortkeys(struct cql_node *cn,
472 void (*pr)(const char *buf, void *client_data),
475 /** \brief converts CQL sortby to sortkeys ..
477 \param out result buffer
478 \param max size of buffer (allocated)
483 int cql_sortby_to_sortkeys_buf(struct cql_node *cn, char *out, int max);
492 * c-file-style: "Stroustrup"
493 * indent-tabs-mode: nil
495 * vim: shiftwidth=4 tabstop=8 expandtab