1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2011 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements CQL to XCQL conversion.
19 static int cql_to_ccl_r(struct cql_node *cn,
20 void (*pr)(const char *buf, void *client_data),
23 static void pr_term(struct cql_node *cn,
24 void (*pr)(const char *buf, void *client_data),
29 pr("\"", client_data);
30 pr(cn->u.st.term, client_data);
31 pr("\"", client_data);
32 if (cn->u.st.extra_terms)
34 cn = cn->u.st.extra_terms;
38 static int node(struct cql_node *cn,
39 void (*pr)(const char *buf, void *client_data),
42 const char *ccl_field = 0;
43 const char *split_op = 0;
44 const char *ccl_rel = 0;
45 const char *rel = cn->u.st.relation;
47 if (cn->u.st.index && strcmp(cn->u.st.index,
49 ccl_field = cn->u.st.index;
53 else if (!strcmp(rel, "<") || !strcmp(rel, "<=")
54 || !strcmp(rel, ">") || !strcmp(rel, ">=")
55 || !strcmp(rel, "<>") || !strcmp(rel, "="))
57 else if (!strcmp(rel, "all"))
62 else if (!strcmp(rel, "any"))
67 else if (!strcmp(rel, "==") || !strcmp(rel, "adj"))
73 /* unsupported relation */
78 if (ccl_field && ccl_rel)
80 pr(ccl_field, client_data);
81 pr(ccl_rel, client_data);
83 pr_term(cn, pr, client_data);
87 const char *cp = cn->u.st.term;
93 if (ccl_field && ccl_rel)
95 pr(ccl_field, client_data);
96 pr(ccl_rel, client_data);
98 while (*cp && *cp != ' ')
110 pr(" ", client_data);
111 pr(split_op, client_data);
112 pr(" ", client_data);
119 static int bool(struct cql_node *cn,
120 void (*pr)(const char *buf, void *client_data),
125 pr("(", client_data);
126 r = cql_to_ccl_r(cn->u.boolean.left, pr, client_data);
130 pr(" ", client_data);
131 pr(cn->u.boolean.value, client_data);
132 pr(" ", client_data);
134 r = cql_to_ccl_r(cn->u.boolean.right, pr, client_data);
135 pr(")", client_data);
139 static int cql_to_ccl_r(struct cql_node *cn,
140 void (*pr)(const char *buf, void *client_data),
149 return node(cn, pr, client_data);
151 return bool(cn, pr, client_data);
153 return cql_to_ccl_r(cn->u.sort.search, pr, client_data);
158 int cql_to_ccl(struct cql_node *cn,
159 void (*pr)(const char *buf, void *client_data),
162 return cql_to_ccl_r(cn, pr, client_data);
165 void cql_to_ccl_stdio(struct cql_node *cn, FILE *f)
167 cql_to_ccl(cn, cql_fputs, f);
170 int cql_to_ccl_buf(struct cql_node *cn, char *out, int max)
172 struct cql_buf_write_info info;
177 r = cql_to_ccl(cn, cql_buf_write_handler, &info);
179 info.buf[info.off] = '\0';
181 return -2; /* buffer overflow */
188 * c-file-style: "Stroustrup"
189 * indent-tabs-mode: nil
191 * vim: shiftwidth=4 tabstop=8 expandtab