1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 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
39 /** \brief CQL parser handle (opaque pointer) */
40 typedef struct cql_parser *CQL_parser;
42 /** \brief creates a CQL parser.
45 Returns CQL parser or NULL if parser could not be created.
48 CQL_parser cql_parser_create(void);
50 /** \brief destroys a CQL parser.
53 This function does nothing if NULL if received.
56 void cql_parser_destroy(CQL_parser cp);
58 /** \brief parses a CQL query (string)
65 int cql_parser_string(CQL_parser cp, const char *str);
67 /** \brief parses CQL query (query stream)
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
75 This function is similar to cql_parser_string but takes a
76 functions to read each query character from a stream.
78 The functions pointers getbytes, ungetbyte are similar to
79 that known from stdios getc, ungetc.
82 int cql_parser_stream(CQL_parser cp,
83 int (*getbyte)(void *client_data),
84 void (*ungetbyte)(int b, void *client_data),
87 /** \brief parses CQL query (from FILE)
89 \param f file where query is read from
93 This function is similar to cql_parser_string but reads from
94 stdio FILE handle instead.
97 int cql_parser_stdio(CQL_parser cp, FILE *f);
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
106 /** \brief CQL parse tree (node)
112 /** which == CQL_NODE_ST */
116 /** CQL index URI or NULL if no URI */
122 /** relation URL or NULL if no relation URI) */
124 /** relation modifiers */
125 struct cql_node *modifiers;
127 struct cql_node *extra_terms;
129 /** which == CQL_NODE_BOOL */
131 /** operator name "and", "or", ... */
134 struct cql_node *left;
136 struct cql_node *right;
137 /** modifiers (NULL for no list) */
138 struct cql_node *modifiers;
140 /** which == CQL_NODE_SORT */
144 struct cql_node *next;
145 /** modifiers (NULL for no list) */
146 struct cql_node *modifiers;
148 struct cql_node *search;
153 /** \brief Private structure that describes the CQL properties (profile)
155 struct cql_properties;
157 /** \brief Structure used by cql_buf_write_handler
159 struct cql_buf_write_info {
165 /** \brief Handler for cql_buf_write_info
168 void cql_buf_write_handler(const char *b, void *client_data);
170 /** \brief Prints a CQL node and all sub nodes.
171 Hence this function prints the parse tree which is as returned by
175 void cql_node_print(struct cql_node *cn);
177 /** \brief creates a search clause node (st). */
179 struct cql_node *cql_node_mk_sc(NMEM nmem, const char *index,
180 const char *relation, const char *term);
182 /** \brief applies a prefix+uri to "unresolved" index and relation URIs.
183 "unresolved" URIs are those nodes where member index_uri / relation_uri
187 struct cql_node *cql_apply_prefix(NMEM nmem, struct cql_node *cn,
188 const char *prefix, const char *uri);
190 /** \brief creates a boolean node. */
192 struct cql_node *cql_node_mk_boolean(NMEM nmem, const char *op);
194 /** \brief creates a sort single spec node. */
196 struct cql_node *cql_node_mk_sort(NMEM nmem, const char *index,
197 struct cql_node *modifiers);
199 /** \brief destroys a node and its children. */
201 void cql_node_destroy(struct cql_node *cn);
203 /** duplicates a node (returns a copy of supplied node) . */
205 struct cql_node *cql_node_dup (NMEM nmem, struct cql_node *cp);
207 /** \brief returns the parse tree of the most recently parsed CQL query.
209 \returns CQL node or NULL for failure
212 struct cql_node *cql_parser_result(CQL_parser cp);
214 /** \brief returns the sortby tree of the most recently parsed CQL query.
216 \returns CQL node or NULL for failure
219 struct cql_node *cql_parser_sort_result(CQL_parser cp);
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
227 void cql_to_xml(struct cql_node *cn,
228 void (*pr)(const char *buf, void *client_data),
230 /** \brief converts CQL tree to XCQL and writes to file
231 \param cn CQL node (tree)
235 void cql_to_xml_stdio(struct cql_node *cn, FILE *f);
237 /** \brief converts CQL tree to XCQL and writes result to buffer
238 \param cn CQL node (tree)
240 \param max size of buffer (max chars to write)
241 \returns length of resulting buffer
244 int cql_to_xml_buf(struct cql_node *cn, char *out, int max);
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
252 int cql_to_ccl(struct cql_node *cn,
253 void (*pr)(const char *buf, void *client_data),
256 /** \brief converts CQL tree to CCL and writes to file
257 \param cn CQL node (tree)
261 void cql_to_ccl_stdio(struct cql_node *cn, FILE *f);
263 /** \brief converts CQL tree to CCL and writes result to buffer
264 \param cn CQL node (tree)
266 \param max size of buffer (max chars to write)
268 \retval -1 conversion error
269 \retval -2 buffer too small (truncated)
272 int cql_to_ccl_buf(struct cql_node *cn, char *out, int max);
274 /** \brief stream handle for file (used by cql_to_xml_stdio) */
276 void cql_fputs(const char *buf, void *client_data);
278 /** \brief CQL transform handle.
279 The transform describes how to convert from CQL to PQF (Type-1 AKA RPN).
281 typedef struct cql_transform_t_ *cql_transform_t;
283 /** \brief creates a CQL transform handle
284 \returns transform handle or NULL for failure
287 cql_transform_t cql_transform_create(void);
289 /** \brief creates a CQL transform handle from am opened file handle
290 \param f file where transformation spec is read
291 \returns transform handle or NULL for failure
293 The transformation spec is read from a FILE handle which is assumed
297 cql_transform_t cql_transform_open_FILE (FILE *f);
299 /** \brief creates a CQL transform handle from a file
300 \param fname name of where transformation spec is read
301 \returns transform handle or NULL for failure
304 cql_transform_t cql_transform_open_fname(const char *fname);
307 /** \brief defines CQL transform pattern
308 \param ct CQL transform handle
309 \param pattern pattern string
310 \param value pattern value
311 \returns 0 for succes; -1 for failure
314 int cql_transform_define_pattern(cql_transform_t ct, const char *pattern,
319 /** \brief destroys a CQL transform handle
320 \param ct CQL transform handle
323 void cql_transform_close(cql_transform_t ct);
325 /** \brief tranforms PQF given a CQL tree
326 \param ct CQL transform handle
327 \param cn CQL node tree
328 \param pr print function
329 \param client_data data to be passed to pr
333 The result is written to a user-defined stream.
336 int cql_transform(cql_transform_t ct,
338 void (*pr)(const char *buf, void *client_data),
341 /** \brief transforms PQF given a CQL tree (from FILE)
342 \param ct CQL transform handle
344 \param f FILE where output is written
346 \retval !=0 failure (error code)
348 The result is written to a file specified by FILE handle (which must
349 be opened for writing.
352 int cql_transform_FILE(cql_transform_t ct,
353 struct cql_node *cn, FILE *f);
355 /** \brief transforms PQF given a CQL tree (from FILE)
356 \param ct CQL transform handle
358 \param out buffer for output
359 \param max maximum bytes for output (size of buffer)
361 \retval !=0 failure (error code)
364 int cql_transform_buf(cql_transform_t ct,
365 struct cql_node *cn, char *out, int max);
367 /** \brief returns additional information for last transform
368 \param ct CQL transform handle
369 \param addinfo additional info (result)
373 int cql_transform_error(cql_transform_t ct, const char **addinfo);
375 /** \brief sets error and addinfo for transform
376 \param ct CQL transform handle
377 \param error error code
378 \param addinfo additional info
381 void cql_transform_set_error(cql_transform_t ct, int error, const char *addinfo);
383 /** \brief returns the CQL message corresponding to a given error code.
384 \param code error code
385 \returns text message
388 const char *cql_strerror(int code);
390 /** \brief returns the standard CQL context set URI.
391 \returns CQL URI string
394 const char *cql_uri(void);
396 /** \brief compares two CQL strings (ala strcmp)
399 \returns comparison value
400 Compares two CQL strings (for relations, operators, etc)
401 (unfortunately defined as case-insensitive unlike XML etc)
404 int cql_strcmp(const char *s1, const char *s2);
406 /** \brief compares two CQL strings (ala strncmp)
410 \returns comparison value
411 Compares two CQL strings at most n bytes
412 (unfortunately defined as case-insensitive unlike XML etc)
415 int cql_strncmp(const char *s1, const char *s2, size_t n);
417 /** \brief converts CQL sortby to sortkeys (ala versions 1.1)
419 \param pr print function
420 \param client_data data to be passed to pr function
422 This will take CQL_NODE_SORT entries and conver them to
424 path,schema,ascending,caseSensitive,missingValue
427 One for each sort keys. Where
429 path is string index for sorting
431 schema is schema for sort index
433 ascending is a boolean (0=false, 1=true). Default is true.
435 caseSensitive is a boolean. Default is false.
437 missingValue is a string and one of 'abort', 'highValue', 'lowValue',
438 or 'omit'. Default is 'highValue'.
441 http://www.loc.gov/standards/sru/sru1-1archive/search-retrieve-operation.html#sort
444 int cql_sortby_to_sortkeys(struct cql_node *cn,
445 void (*pr)(const char *buf, void *client_data),
448 /** \brief converts CQL sortby to sortkeys ..
450 \param out result buffer
451 \param max size of buffer (allocated)
456 int cql_sortby_to_sortkeys_buf(struct cql_node *cn, char *out, int max);
465 * c-file-style: "Stroustrup"
466 * indent-tabs-mode: nil
468 * vim: shiftwidth=4 tabstop=8 expandtab