1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 Index Data
3 * See the file LICENSE for details.
8 * \brief Implements Z39.50 Query Printing
15 #include <yaz/logrpn.h>
16 #include <yaz/oid_db.h>
18 static const char *relToStr(int v)
23 case 1: str = "Less than"; break;
24 case 2: str = "Less than or equal"; break;
25 case 3: str = "Equal"; break;
26 case 4: str = "Greater or equal"; break;
27 case 5: str = "Greater than"; break;
28 case 6: str = "Not equal"; break;
29 case 100: str = "Phonetic"; break;
30 case 101: str = "Stem"; break;
31 case 102: str = "Relevance"; break;
32 case 103: str = "AlwaysMatches"; break;
37 static void attrStr (int type, int value, char *str)
47 rstr = relToStr(value);
49 sprintf(str, "relation=%s", rstr);
51 sprintf(str, "relation=%d", value);
57 sprintf(str, "position=First in field");
60 sprintf(str, "position=First in any subfield");
63 sprintf(str, "position=Any position in field");
66 sprintf(str, "position");
73 sprintf(str, "structure=Phrase");
76 sprintf(str, "structure=Word");
79 sprintf(str, "structure=Key");
82 sprintf(str, "structure=Year");
85 sprintf(str, "structure=Date");
88 sprintf(str, "structure=Word list");
91 sprintf(str, "structure=Date (un)");
94 sprintf(str, "structure=Name (norm)");
97 sprintf(str, "structure=Name (un)");
100 sprintf(str, "structure=Structure");
103 sprintf(str, "structure=urx");
106 sprintf(str, "structure=free-form-text");
109 sprintf(str, "structure=document-text");
112 sprintf(str, "structure=local-number");
115 sprintf(str, "structure=string");
118 sprintf(str, "structure=numeric string");
121 sprintf(str, "structure");
128 sprintf(str, "truncation=Right");
131 sprintf(str, "truncation=Left");
134 sprintf(str, "truncation=Left&right");
137 sprintf(str, "truncation=Do not truncate");
140 sprintf(str, "truncation=Process #");
143 sprintf(str, "truncation=re-1");
146 sprintf(str, "truncation=re-2");
149 sprintf(str, "truncation=CCL");
152 sprintf(str, "truncation");
159 sprintf(str, "completeness=Incomplete subfield");
162 sprintf(str, "completeness=Complete subfield");
165 sprintf(str, "completeness=Complete field");
168 sprintf(str, "completeness");
173 sprintf(str + strlen(str), " (%d=%d)", type, value);
175 sprintf(str, "%d=%d", type, value);
179 * zlog_attributes: print attributes of term
181 static void zlog_attributes(Z_AttributesPlusTerm *t, int depth,
182 const Odr_oid *ast, int loglevel)
186 int num_attributes = t->attributes->num_attributes;
188 for (of = 0; of < num_attributes; of++)
190 char attset_name_buf[OID_STR_MAX];
191 const char *attset_name = 0;
192 Z_AttributeElement *element;
193 element = t->attributes->attributes[of];
194 if (element->attributeSet)
196 attset_name = yaz_oid_to_string_buf(element->attributeSet,
201 switch (element->which)
203 case Z_AttributeValue_numeric:
204 attrStr (*element->attributeType,
205 *element->value.numeric, str);
206 yaz_log (loglevel, "%*.0s%s %s", depth, "", attset_name, str);
208 case Z_AttributeValue_complex:
209 yaz_log (loglevel, "%*.0s%s attributeType=" ODR_INT_PRINTF
211 depth, "", attset_name, *element->attributeType);
212 for (i = 0; i<element->value.complex->num_list; i++)
214 if (element->value.complex->list[i]->which ==
215 Z_StringOrNumeric_string)
216 yaz_log (loglevel, "%*.0s string: '%s'", depth, "",
217 element->value.complex->list[i]->u.string);
218 else if (element->value.complex->list[i]->which ==
219 Z_StringOrNumeric_numeric)
220 yaz_log (loglevel, "%*.0s numeric: '" ODR_INT_PRINTF
222 *element->value.complex->list[i]->u.numeric);
226 yaz_log (loglevel, "%.*s%s attribute unknown",
227 depth, "", attset_name);
232 static char *complex_op_name(Z_Operator *op)
240 case Z_Operator_and_not:
242 case Z_Operator_prox:
245 return "unknown complex operator";
249 static char *prox_unit_name(Z_ProximityOperator *op)
251 if (op->which!=Z_ProximityOperator_known)
255 case Z_ProxUnit_character: return "character";
256 case Z_ProxUnit_word: return "word";
257 case Z_ProxUnit_sentence: return "sentence";
258 case Z_ProxUnit_paragraph: return "paragraph";
259 case Z_ProxUnit_section: return "section";
260 case Z_ProxUnit_chapter: return "chapter";
261 case Z_ProxUnit_document: return "document";
262 case Z_ProxUnit_element: return "element";
263 case Z_ProxUnit_subelement: return "subelement";
264 case Z_ProxUnit_elementType: return "elementType";
265 case Z_ProxUnit_byte: return "byte";
266 default: return "unknown";
270 static void zlog_structure(Z_RPNStructure *zs, int depth,
271 const Odr_oid *ast, int loglevel)
273 if (zs->which == Z_RPNStructure_complex)
275 Z_Operator *op = zs->u.complex->roperator;
280 case Z_Operator_and_not:
281 yaz_log (loglevel, "%*.0s %s", depth, "", complex_op_name(op) );
283 case Z_Operator_prox:
284 yaz_log (loglevel, "%*.0s prox excl=%s dist=" ODR_INT_PRINTF
287 depth, "", op->u.prox->exclusion ?
288 (*op->u.prox->exclusion ? "T" : "F") : "N",
289 *op->u.prox->distance,
290 *op->u.prox->ordered ? "T" : "F",
291 relToStr(*op->u.prox->relationType),
292 prox_unit_name(op->u.prox) );
295 yaz_log (loglevel, "%*.0s unknown complex", depth, "");
298 zlog_structure (zs->u.complex->s1, depth+2, ast, loglevel);
299 zlog_structure (zs->u.complex->s2, depth+2, ast, loglevel);
301 else if (zs->which == Z_RPNStructure_simple)
303 if (zs->u.simple->which == Z_Operand_APT)
305 Z_AttributesPlusTerm *zapt = zs->u.simple->u.attributesPlusTerm;
307 switch (zapt->term->which)
310 yaz_log (loglevel, "%*.0s term '%.*s' (general)", depth, "",
311 zapt->term->u.general->len,
312 zapt->term->u.general->buf);
314 case Z_Term_characterString:
315 yaz_log (loglevel, "%*.0s term '%s' (string)", depth, "",
316 zapt->term->u.characterString);
319 yaz_log (loglevel, "%*.0s term '" ODR_INT_PRINTF
320 "' (numeric)", depth, "",
321 *zapt->term->u.numeric);
324 yaz_log (loglevel, "%*.0s term (null)", depth, "");
327 yaz_log (loglevel, "%*.0s term (not general)", depth, "");
329 zlog_attributes(zapt, depth+2, ast, loglevel);
331 else if (zs->u.simple->which == Z_Operand_resultSetId)
333 yaz_log (loglevel, "%*.0s set '%s'", depth, "",
334 zs->u.simple->u.resultSetId);
337 yaz_log (loglevel, "%*.0s unknown simple structure", depth, "");
340 yaz_log (loglevel, "%*.0s unknown structure", depth, "");
343 void log_rpn_query_level (int loglevel, Z_RPNQuery *rpn)
345 zlog_structure(rpn->RPNStructure, 0, rpn->attributeSetId, loglevel);
348 void log_rpn_query(Z_RPNQuery *rpn)
350 log_rpn_query_level(YLOG_LOG, rpn);
353 void log_scan_term_level(int loglevel,
354 Z_AttributesPlusTerm *zapt, const Odr_oid *ast)
359 if (zapt->term->which == Z_Term_general)
361 yaz_log (loglevel, "%*.0s term '%.*s' (general)", depth, "",
362 zapt->term->u.general->len, zapt->term->u.general->buf);
365 yaz_log (loglevel, "%*.0s term (not general)", depth, "");
366 zlog_attributes(zapt, depth+2, ast, loglevel);
369 void log_scan_term(Z_AttributesPlusTerm *zapt, const Odr_oid *ast)
371 log_scan_term_level (YLOG_LOG, zapt, ast);
374 void yaz_log_zquery_level (int loglevel, Z_Query *q)
380 case Z_Query_type_1: case Z_Query_type_101:
381 log_rpn_query_level (loglevel, q->u.type_1);
384 yaz_log(loglevel, "CCL: %.*s", q->u.type_2->len, q->u.type_2->buf);
386 case Z_Query_type_100:
387 yaz_log(loglevel, "Z39.58: %.*s", q->u.type_100->len,
390 case Z_Query_type_104:
391 if (q->u.type_104->which == Z_External_CQL)
392 yaz_log (loglevel, "CQL: %s", q->u.type_104->u.cql);
396 void yaz_log_zquery (Z_Query *q)
398 yaz_log_zquery_level(YLOG_LOG, q);
404 * c-file-style: "Stroustrup"
405 * indent-tabs-mode: nil
407 * vim: shiftwidth=4 tabstop=8 expandtab