YAZ  5.23.1
Data Structures | Macros | Typedefs | Functions
cql.h File Reference

Header with public definitions about CQL. More...

#include <stdio.h>
#include <yaz/nmem.h>
#include <yaz/wrbuf.h>

Go to the source code of this file.

Data Structures

struct  cql_node
 CQL parse tree (node) More...
 
struct  cql_buf_write_info
 Structure used by cql_buf_write_handler. More...
 

Macros

#define CQL_NODE_ST   1
 Node type: search term. More...
 
#define CQL_NODE_BOOL   2
 Node type: boolean. More...
 
#define CQL_NODE_SORT   3
 Node type: sortby single spec. More...
 

Typedefs

typedef struct cql_parserCQL_parser
 CQL parser handle (opaque pointer) More...
 
typedef struct cql_transform_t_cql_transform_t
 CQL transform handle. The transform describes how to convert from CQL to PQF (Type-1 AKA RPN). More...
 

Functions

CQL_parser cql_parser_create (void)
 creates a CQL parser. More...
 
void cql_parser_destroy (CQL_parser cp)
 destroys a CQL parser. More...
 
int cql_parser_string (CQL_parser cp, const char *str)
 parses a CQL query (string) More...
 
int cql_parser_stream (CQL_parser cp, int(*getbyte)(void *client_data), void(*ungetbyte)(int b, void *client_data), void *client_data)
 parses CQL query (query stream) More...
 
int cql_parser_stdio (CQL_parser cp, FILE *f)
 parses CQL query (from FILE) More...
 
void cql_parser_strict (CQL_parser cp, int mode)
 configures strict mode More...
 
void cql_buf_write_handler (const char *b, void *client_data)
 Handler for cql_buf_write_info. More...
 
void cql_node_print (struct cql_node *cn)
 Prints a CQL node and all sub nodes. Hence this function prints the parse tree which is as returned by cql_parser_result. More...
 
struct cql_nodecql_node_mk_sc (NMEM nmem, const char *index, const char *relation, const char *term)
 creates a search clause node (st). More...
 
struct cql_nodecql_apply_prefix (NMEM nmem, struct cql_node *cn, const char *prefix, const char *uri)
 applies a prefix+uri to "unresolved" index and relation URIs. "unresolved" URIs are those nodes where member index_uri / relation_uri is NULL. More...
 
struct cql_nodecql_node_mk_boolean (NMEM nmem, const char *op)
 creates a boolean node. More...
 
struct cql_nodecql_node_mk_sort (NMEM nmem, const char *index, struct cql_node *modifiers)
 creates a sort single spec node. More...
 
void cql_node_destroy (struct cql_node *cn)
 destroys a node and its children. More...
 
struct cql_nodecql_node_dup (NMEM nmem, struct cql_node *cp)
 
struct cql_nodecql_parser_result (CQL_parser cp)
 returns the parse tree of the most recently parsed CQL query. More...
 
struct cql_nodecql_parser_sort_result (CQL_parser cp)
 returns the sortby tree of the most recently parsed CQL query. More...
 
void cql_to_xml (struct cql_node *cn, void(*pr)(const char *buf, void *client_data), void *client_data)
 converts CQL tree to XCQL and writes to user-defined stream More...
 
void cql_to_xml_stdio (struct cql_node *cn, FILE *f)
 converts CQL tree to XCQL and writes to file More...
 
int cql_to_xml_buf (struct cql_node *cn, char *out, int max)
 converts CQL tree to XCQL and writes result to buffer More...
 
int cql_to_ccl (struct cql_node *cn, void(*pr)(const char *buf, void *client_data), void *client_data)
 converts CQL tree to CCL and writes to user-defined stream More...
 
void cql_to_ccl_stdio (struct cql_node *cn, FILE *f)
 converts CQL tree to CCL and writes to file More...
 
int cql_to_ccl_buf (struct cql_node *cn, char *out, int max)
 converts CQL tree to CCL and writes result to buffer More...
 
void cql_fputs (const char *buf, void *client_data)
 stream handle for file (used by cql_to_xml_stdio) More...
 
cql_transform_t cql_transform_create (void)
 creates a CQL transform handle More...
 
cql_transform_t cql_transform_open_FILE (FILE *f)
 creates a CQL transform handle from am opened file handle More...
 
cql_transform_t cql_transform_open_fname (const char *fname)
 creates a CQL transform handle from a file More...
 
int cql_transform_define_pattern (cql_transform_t ct, const char *pattern, const char *value)
 defines CQL transform pattern More...
 
void cql_transform_close (cql_transform_t ct)
 destroys a CQL transform handle More...
 
int cql_transform (cql_transform_t ct, struct cql_node *cn, void(*pr)(const char *buf, void *client_data), void *client_data)
 tranforms PQF given a CQL tree (NOT re-entrant) More...
 
int cql_transform_r (cql_transform_t ct, struct cql_node *cn, WRBUF addinfo, void(*pr)(const char *buf, void *client_data), void *client_data)
 tranforms PQF given a CQL tree (re-entrant) More...
 
int cql_transform_FILE (cql_transform_t ct, struct cql_node *cn, FILE *f)
 transforms PQF given a CQL tree from FILE (not re-entrant) More...
 
int cql_transform_buf (cql_transform_t ct, struct cql_node *cn, char *out, int max)
 transforms PQF given a CQL tree from buffer (not re-entrant) More...
 
int cql_transform_error (cql_transform_t ct, const char **addinfo)
 returns additional information for last transform More...
 
void cql_transform_set_error (cql_transform_t ct, int error, const char *addinfo)
 sets error and addinfo for transform More...
 
const char * cql_strerror (int code)
 returns the CQL message corresponding to a given error code. More...
 
const char * cql_uri (void)
 returns the standard CQL context set URI. More...
 
int cql_strcmp (const char *s1, const char *s2)
 compares two CQL strings (ala strcmp) More...
 
int cql_strncmp (const char *s1, const char *s2, size_t n)
 compares two CQL strings (ala strncmp) More...
 
int cql_sortby_to_sortkeys (struct cql_node *cn, void(*pr)(const char *buf, void *client_data), void *client_data)
 converts CQL sortby to sortkeys (ala versions 1.1) More...
 
int cql_sortby_to_sortkeys_buf (struct cql_node *cn, char *out, int max)
 converts CQL sortby to sortkeys .. More...
 

Detailed Description

Header with public definitions about CQL.

Definition in file cql.h.

Macro Definition Documentation

#define CQL_NODE_BOOL   2

Node type: boolean.

Definition at line 113 of file cql.h.

Referenced by cql_apply_prefix(), cql_node_destroy(), cql_node_dup(), cql_node_mk_boolean(), cql_to_ccl_r(), cql_to_xml_r(), and emit_node().

#define CQL_NODE_SORT   3

Node type: sortby single spec.

Definition at line 115 of file cql.h.

Referenced by cql_apply_prefix(), cql_node_destroy(), cql_node_dup(), cql_node_mk_sort(), cql_sortby_to_sortkeys(), cql_to_ccl_r(), cql_to_xml_r(), and emit_node().

#define CQL_NODE_ST   1

Node type: search term.

Definition at line 111 of file cql.h.

Referenced by bool(), cql_apply_prefix(), cql_node_destroy(), cql_node_dup(), cql_node_mk_sc(), cql_to_ccl_r(), cql_to_xml_r(), emit_node(), and emit_term().

Typedef Documentation

typedef struct cql_parser* CQL_parser

CQL parser handle (opaque pointer)

Definition at line 41 of file cql.h.

CQL transform handle. The transform describes how to convert from CQL to PQF (Type-1 AKA RPN).

Definition at line 292 of file cql.h.

Function Documentation

struct cql_node* cql_apply_prefix ( NMEM  nmem,
struct cql_node cn,
const char *  prefix,
const char *  uri 
)

applies a prefix+uri to "unresolved" index and relation URIs. "unresolved" URIs are those nodes where member index_uri / relation_uri is NULL.

Definition at line 110 of file cqlutil.c.

References cql_node::boolean, cql_apply_prefix(), CQL_NODE_BOOL, CQL_NODE_SORT, CQL_NODE_ST, cql_strncmp(), nmem_strdup(), cql_node::sort, cql_node::st, cql_node::u, and cql_node::which.

Referenced by cql_apply_prefix(), cql_transform_r(), and yyparse().

void cql_buf_write_handler ( const char *  b,
void *  client_data 
)
void cql_fputs ( const char *  buf,
void *  client_data 
)

stream handle for file (used by cql_to_xml_stdio)

Definition at line 18 of file cqlutil.c.

Referenced by cql_to_ccl_stdio(), cql_to_xml_stdio(), and cql_transform_FILE().

void cql_node_destroy ( struct cql_node cn)
struct cql_node* cql_node_dup ( NMEM  nmem,
struct cql_node cp 
)

duplicates a node (returns a copy of supplied node) .

Definition at line 24 of file cqlutil.c.

References cql_node::boolean, CQL_NODE_BOOL, cql_node_dup(), cql_node_mk_boolean(), cql_node_mk_sc(), cql_node_mk_sort(), CQL_NODE_SORT, CQL_NODE_ST, nmem_strdup(), cql_node::sort, cql_node::st, cql_node::u, and cql_node::which.

Referenced by cql_node_dup(), and yyparse().

struct cql_node* cql_node_mk_boolean ( NMEM  nmem,
const char *  op 
)

creates a boolean node.

Definition at line 78 of file cqlutil.c.

References cql_node::boolean, CQL_NODE_BOOL, nmem_malloc(), nmem_strdup(), cql_node::u, and cql_node::which.

Referenced by cql_node_dup(), and yyparse().

struct cql_node* cql_node_mk_sc ( NMEM  nmem,
const char *  index,
const char *  relation,
const char *  term 
)

creates a search clause node (st).

Definition at line 55 of file cqlutil.c.

References CQL_NODE_ST, nmem_malloc(), nmem_strdup(), cql_node::st, cql_node::u, and cql_node::which.

Referenced by cql_node_dup(), and yyparse().

struct cql_node* cql_node_mk_sort ( NMEM  nmem,
const char *  index,
struct cql_node modifiers 
)

creates a sort single spec node.

Definition at line 91 of file cqlutil.c.

References CQL_NODE_SORT, cql_node::modifiers, nmem_malloc(), nmem_strdup(), cql_node::sort, cql_node::u, and cql_node::which.

Referenced by cql_node_dup(), and yyparse().

void cql_node_print ( struct cql_node cn)

Prints a CQL node and all sub nodes. Hence this function prints the parse tree which is as returned by cql_parser_result.

CQL_parser cql_parser_create ( void  )

creates a CQL parser.

Returns
CCL parser

Returns CQL parser or NULL if parser could not be created.

Definition at line 1971 of file cql.c.

References cql_parser::client_data, cql_parser::getbyte, cql_parser::last_error, cql_parser::last_pos, cql_parser::nmem, nmem_create(), cql_parser::strict, cql_parser::top, cql_parser::ungetbyte, and xmalloc.

Referenced by cql2pqf().

void cql_parser_destroy ( CQL_parser  cp)

destroys a CQL parser.

Parameters
cpCQL parser

This function does nothing if NULL if received.

Definition at line 1986 of file cql.c.

References cql_node_destroy(), cql_parser::nmem, nmem_destroy(), cql_parser::top, and xfree.

Referenced by cql2pqf().

struct cql_node* cql_parser_result ( CQL_parser  cp)

returns the parse tree of the most recently parsed CQL query.

Parameters
cpCQL parser
Returns
CQL node or NULL for failure

Definition at line 1993 of file cql.c.

References cql_parser::top.

Referenced by cql2pqf().

struct cql_node* cql_parser_sort_result ( CQL_parser  cp)

returns the sortby tree of the most recently parsed CQL query.

Parameters
cpCQL parser
Returns
CQL node or NULL for failure
int cql_parser_stdio ( CQL_parser  cp,
FILE *  f 
)

parses CQL query (from FILE)

Parameters
cpCQL parser
ffile where query is read from
Return values
0success
!=0failure

This function is similar to cql_parser_string but reads from stdio FILE handle instead.

Definition at line 34 of file cqlstdio.c.

References cql_parser_stream(), getbyte_stream(), and ungetbyte_stream().

int cql_parser_stream ( CQL_parser  cp,
int(*)(void *client_data)  getbyte,
void(*)(int b, void *client_data)  ungetbyte,
void *  client_data 
)

parses CQL query (query stream)

Parameters
cpCQL parser
getbytefunction which reads one character from stream
ungetbytefunction which unreads one character from stream
client_datadata to be passed to stream functions
Return values
0success
!=0failure

This function is similar to cql_parser_string but takes a functions to read each query character from a stream.

The functions pointers getbytes, ungetbyte are similar to that known from stdios getc, ungetc.

Definition at line 1955 of file cql.c.

References cql_parser::client_data, cql_node_destroy(), cql_parse(), cql_parser::getbyte, cql_parser::nmem, nmem_reset(), cql_parser::top, and cql_parser::ungetbyte.

Referenced by cql_parser_stdio(), and cql_parser_string().

void cql_parser_strict ( CQL_parser  cp,
int  mode 
)

configures strict mode

Parameters
cpCQL parser
mode1=enable strict mode, 0=disable strict mode

This function is similar to cql_parser_string but reads from stdio FILE handle instead.

Definition at line 1998 of file cql.c.

References cql_parser::strict.

int cql_parser_string ( CQL_parser  cp,
const char *  str 
)

parses a CQL query (string)

Parameters
cpCQL parser
strCQL string
Return values
0success
!=0failure

Definition at line 35 of file cqlstring.c.

References cql_parser_stream(), getbuf(), cql_buf_info::off, cql_buf_info::str, and ungetbuf().

Referenced by cql2pqf().

int cql_sortby_to_sortkeys ( struct cql_node cn,
void(*)(const char *buf, void *client_data)  pr,
void *  client_data 
)

converts CQL sortby to sortkeys (ala versions 1.1)

Parameters
cnCQL tree
prprint function
client_datadata to be passed to pr function

This will take CQL_NODE_SORT entries and conver them to

path,schema,ascending,caseSensitive,missingValue items..

One for each sort keys. Where

path is string index for sorting

schema is schema for sort index

ascending is a boolean (0=false, 1=true). Default is true.

caseSensitive is a boolean. Default is false.

missingValue is a string and one of 'abort', 'highValue', 'lowValue', or 'omit'. Default is 'highValue'.

See also http://www.loc.gov/standards/sru/sru-1-1.html#sort

Definition at line 83 of file cql_sortkeys.c.

References CQL_NODE_SORT, cql_sort_modifiers(), pr_n(), cql_node::sort, cql_node::u, and cql_node::which.

Referenced by cql_sortby_to_sortkeys_buf().

int cql_sortby_to_sortkeys_buf ( struct cql_node cn,
char *  out,
int  max 
)

converts CQL sortby to sortkeys ..

Parameters
cnCQL tree
outresult buffer
maxsize of buffer (allocated)
Return values
0OK
-1ERROR

Definition at line 120 of file cql_sortkeys.c.

References cql_buf_write_info::buf, cql_buf_write_handler(), cql_sortby_to_sortkeys(), cql_buf_write_info::max, and cql_buf_write_info::off.

Referenced by cql2pqf().

int cql_strcmp ( const char *  s1,
const char *  s2 
)

compares two CQL strings (ala strcmp)

Parameters
s1string 1
s2string 2
Returns
comparison value Compares two CQL strings (for relations, operators, etc) (unfortunately defined as case-insensitive unlike XML etc)

Definition at line 194 of file cqlutil.c.

Referenced by cql_lookup_property(), cql_transform_r(), emit_node(), and yylex().

const char* cql_strerror ( int  code)

returns the CQL message corresponding to a given error code.

Parameters
codeerror code
Returns
text message

Definition at line 20 of file cqlstrer.c.

References cql_buf_write_info::buf.

Referenced by cql2pqf().

int cql_strncmp ( const char *  s1,
const char *  s2,
size_t  n 
)

compares two CQL strings (ala strncmp)

Parameters
s1string 1
s2string 2
nsize
Returns
comparison value Compares two CQL strings at most n bytes (unfortunately defined as case-insensitive unlike XML etc)

Definition at line 210 of file cqlutil.c.

Referenced by cql_apply_prefix(), and cql_transform_r().

int cql_to_ccl ( struct cql_node cn,
void(*)(const char *buf, void *client_data)  pr,
void *  client_data 
)

converts CQL tree to CCL and writes to user-defined stream

Parameters
cnCQL node (tree)
prprint function
client_datadata to be passed to pr function

Definition at line 249 of file cql2ccl.c.

References cql_to_ccl_r().

Referenced by cql_to_ccl_buf(), and cql_to_ccl_stdio().

int cql_to_ccl_buf ( struct cql_node cn,
char *  out,
int  max 
)

converts CQL tree to CCL and writes result to buffer

Parameters
cnCQL node (tree)
outbuffer
maxsize of buffer (max chars to write)
Return values
0OK
-1conversion error
-2buffer too small (truncated)

Definition at line 261 of file cql2ccl.c.

References cql_buf_write_info::buf, cql_buf_write_handler(), cql_to_ccl(), cql_buf_write_info::max, and cql_buf_write_info::off.

void cql_to_ccl_stdio ( struct cql_node cn,
FILE *  f 
)

converts CQL tree to CCL and writes to file

Parameters
cnCQL node (tree)
ffile handle

Definition at line 256 of file cql2ccl.c.

References cql_fputs(), and cql_to_ccl().

void cql_to_xml ( struct cql_node cn,
void(*)(const char *buf, void *client_data)  pr,
void *  client_data 
)

converts CQL tree to XCQL and writes to user-defined stream

Parameters
cnCQL node (tree)
prprint function
client_datadata to be passed to pr function

Definition at line 239 of file xcqlutil.c.

References cql_to_xml_r().

Referenced by cql_to_xml_buf(), and cql_to_xml_stdio().

int cql_to_xml_buf ( struct cql_node cn,
char *  out,
int  max 
)

converts CQL tree to XCQL and writes result to buffer

Parameters
cnCQL node (tree)
outbuffer
maxsize of buffer (max chars to write)
Returns
length of resulting buffer

Definition at line 264 of file xcqlutil.c.

References cql_buf_write_info::buf, cql_buf_write_handler(), cql_to_xml(), cql_buf_write_info::max, and cql_buf_write_info::off.

void cql_to_xml_stdio ( struct cql_node cn,
FILE *  f 
)

converts CQL tree to XCQL and writes to file

Parameters
cnCQL node (tree)
ffile handle

Definition at line 246 of file xcqlutil.c.

References cql_fputs(), and cql_to_xml().

int cql_transform ( cql_transform_t  ct,
struct cql_node cn,
void(*)(const char *buf, void *client_data)  pr,
void *  client_data 
)

tranforms PQF given a CQL tree (NOT re-entrant)

Parameters
ctCQL transform handle
cnCQL node tree
prprint function
client_datadata to be passed to pr
Return values
0success
!=0 error

The result is written to a user-defined stream.

Definition at line 987 of file cqltransform.c.

References cql_transform_r(), cql_transform_set_error(), wrbuf_alloc(), wrbuf_cstr(), and wrbuf_destroy().

Referenced by cql2pqf(), cql_transform_buf(), and cql_transform_FILE().

int cql_transform_buf ( cql_transform_t  ct,
struct cql_node cn,
char *  out,
int  max 
)

transforms PQF given a CQL tree from buffer (not re-entrant)

Parameters
ctCQL transform handle
cnCQL tree
outbuffer for output
maxmaximum bytes for output (size of buffer)
Return values
0success
!=0failure (error code)

Definition at line 1003 of file cqltransform.c.

References cql_buf_write_info::buf, cql_buf_write_handler(), cql_transform(), cql_transform_set_error(), cql_buf_write_info::max, cql_buf_write_info::off, and YAZ_SRW_TOO_MANY_CHARS_IN_QUERY.

void cql_transform_close ( cql_transform_t  ct)
cql_transform_t cql_transform_create ( void  )

creates a CQL transform handle

Returns
transform handle or NULL for failure

Definition at line 53 of file cqltransform.c.

References cql_transform_t_::addinfo, cql_transform_t_::entry, cql_transform_t_::error, cql_transform_t_::nmem, nmem_create(), cql_transform_t_::tok_cfg, wrbuf_alloc(), xmalloc, and yaz_tok_cfg_create().

Referenced by cql_transform_open_FILE(), and solr_transform_create().

int cql_transform_define_pattern ( cql_transform_t  ct,
const char *  pattern,
const char *  value 
)

defines CQL transform pattern

Parameters
ctCQL transform handle
patternpattern string
valuepattern value
Returns
0 for succes; -1 for failure

Definition at line 204 of file cqltransform.c.

References cql_transform_parse_tok_line(), cql_transform_t_::tok_cfg, yaz_tok_cfg_single_tokens(), yaz_tok_parse_buf(), and yaz_tok_parse_destroy().

Referenced by solr_transform_define_pattern().

int cql_transform_error ( cql_transform_t  ct,
const char **  addinfo 
)

returns additional information for last transform

Parameters
ctCQL transform handle
addinfoadditional info (result)
Returns
error code

Definition at line 1027 of file cqltransform.c.

References cql_transform_t_::addinfo, cql_transform_t_::error, wrbuf_cstr(), and wrbuf_len.

Referenced by cql2pqf(), and solr_transform_error().

int cql_transform_FILE ( cql_transform_t  ct,
struct cql_node cn,
FILE *  f 
)

transforms PQF given a CQL tree from FILE (not re-entrant)

Parameters
ctCQL transform handle
cnCQL tree
fFILE where output is written
Return values
0success
!=0failure (error code)

The result is written to a file specified by FILE handle (which must be opened for writing.

Definition at line 998 of file cqltransform.c.

References cql_fputs(), and cql_transform().

cql_transform_t cql_transform_open_FILE ( FILE *  f)

creates a CQL transform handle from am opened file handle

Parameters
ffile where transformation spec is read
Returns
transform handle or NULL for failure

The transformation spec is read from a FILE handle which is assumed opened for reading.

Definition at line 215 of file cqltransform.c.

References cql_transform_close(), cql_transform_create(), cql_transform_parse_tok_line(), cql_prop_entry::pattern, cql_transform_t_::tok_cfg, xfree, xstrdup, yaz_tok_cfg_single_tokens(), YAZ_TOK_EOF, yaz_tok_move(), yaz_tok_parse_buf(), yaz_tok_parse_destroy(), yaz_tok_parse_string(), and YAZ_TOK_STRING.

Referenced by cql_transform_open_fname(), and solr_transform_open_FILE().

cql_transform_t cql_transform_open_fname ( const char *  fname)

creates a CQL transform handle from a file

Parameters
fnamename of where transformation spec is read
Returns
transform handle or NULL for failure

Definition at line 276 of file cqltransform.c.

References cql_transform_open_FILE(), and cql_prop_entry::value.

Referenced by cql2pqf(), solr_transform_open_fname(), and xml_config_read().

int cql_transform_r ( cql_transform_t  ct,
struct cql_node cn,
WRBUF  addinfo,
void(*)(const char *buf, void *client_data)  pr,
void *  client_data 
)

tranforms PQF given a CQL tree (re-entrant)

Parameters
ctCQL transform handle
cnCQL node tree
addinfoadditional information (if error)
prprint function
client_datadata to be passed to pr
Return values
0success
!=0 error code

The result is written to a user-defined stream.

Definition at line 966 of file cqltransform.c.

References cql_apply_prefix(), cql_strcmp(), cql_strncmp(), emit_node(), cql_transform_t_::entry, cql_prop_entry::next, nmem_create(), nmem_destroy(), cql_prop_entry::pattern, and cql_prop_entry::value.

Referenced by cql_transform().

void cql_transform_set_error ( cql_transform_t  ct,
int  error,
const char *  addinfo 
)

sets error and addinfo for transform

Parameters
ctCQL transform handle
errorerror code
addinfoadditional info

Definition at line 1033 of file cqltransform.c.

References cql_transform_t_::addinfo, cql_transform_t_::error, wrbuf_puts(), and wrbuf_rewind().

Referenced by cql_transform(), cql_transform_buf(), cql_transform_rpn2cql_stream(), and solr_transform_set_error().

const char* cql_uri ( void  )

returns the standard CQL context set URI.

Returns
CQL URI string

Definition at line 105 of file cqlutil.c.

Referenced by emit_node().