YAZ  5.23.1
querytowrbuf.c
Go to the documentation of this file.
1 /* This file is part of the YAZ toolkit.
2  * Copyright (C) Index Data
3  * See the file LICENSE for details.
4  */
8 #if HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11 
12 #include <stdio.h>
13 #include <assert.h>
14 
15 #include <yaz/logrpn.h>
16 #include <yaz/querytowrbuf.h>
17 #include <yaz/oid_db.h>
18 
19 void yaz_encode_pqf_term(WRBUF b, const char *term, int len)
20 {
21  int i;
22  for (i = 0; i < len; i++)
23  if (strchr(" \"{", term[i]))
24  break;
25  if (len > 0 && i == len)
26  {
27  if (term[0] == '@')
28  wrbuf_putc(b, '\\');
29  for (i = 0; i < len; i++)
30  {
31  if (term[i] == '\\')
32  wrbuf_putc(b, '\\');
33  wrbuf_putc(b, term[i]);
34  }
35  }
36  else
37  {
38  wrbuf_putc(b, '"');
39  for (i = 0; i < len; i++)
40  {
41  if (term[i] == '"' || term[i] == '\\')
42  wrbuf_putc(b, '\\');
43  wrbuf_putc(b, term[i]);
44  }
45  wrbuf_putc(b, '"');
46  }
47  wrbuf_putc(b, ' ');
48 }
49 
51  const Z_AttributeElement *element)
52 {
53  int i;
54 
55  wrbuf_puts(b, "@attr ");
56  if (element->attributeSet)
57  {
58  char oid_name_str[OID_STR_MAX];
59  const char *setname = yaz_oid_to_string_buf(element->attributeSet,
60  0, oid_name_str);
61  if (setname)
62  {
63  wrbuf_puts(b, setname);
64  wrbuf_puts(b, " ");
65  }
66  }
67  wrbuf_printf(b, ODR_INT_PRINTF "=", *element->attributeType);
68  switch (element->which)
69  {
72  break;
74  for (i = 0; i < element->value.complex->num_list; i++)
75  {
76  if (i)
77  wrbuf_puts(b, ",");
78  if (element->value.complex->list[i]->which ==
80  wrbuf_puts(b, element->value.complex->list[i]->u.string);
81  else if (element->value.complex->list[i]->which ==
84  *element->value.complex->list[i]->u.numeric);
85  }
86  break;
87  default:
88  wrbuf_puts(b, "@attr 1=unknown");
89  }
90  wrbuf_puts(b, " ");
91 }
92 
93 static const char *complex_op_name(const Z_Operator *op)
94 {
95  switch (op->which)
96  {
97  case Z_Operator_and:
98  return "and";
99  case Z_Operator_or:
100  return "or";
101  case Z_Operator_and_not:
102  return "not";
103  case Z_Operator_prox:
104  return "prox";
105  default:
106  return "unknown complex operator";
107  }
108 }
109 
111 {
112  int i;
113  for (i = 0; i < al->num_attributes; i++)
115 }
116 
117 static void yaz_apt_to_wrbuf(WRBUF b, const Z_AttributesPlusTerm *zapt)
118 {
120  switch (zapt->term->which)
121  {
122  case Z_Term_general:
123  yaz_encode_pqf_term(b, (const char *)zapt->term->u.general->buf,
124  zapt->term->u.general->len);
125  break;
127  wrbuf_puts(b, "@term string ");
129  strlen(zapt->term->u.characterString));
130  break;
131  case Z_Term_numeric:
132  wrbuf_printf(b, "@term numeric " ODR_INT_PRINTF " ",
133  *zapt->term->u.numeric);
134  break;
135  case Z_Term_null:
136  wrbuf_puts(b, "@term null x");
137  break;
138  default:
139  wrbuf_printf(b, "@term null unknown%d ", zapt->term->which);
140  }
141 }
142 
144 {
145  if (zs->which == Z_RPNStructure_complex)
146  {
147  Z_Operator *op = zs->u.complex->roperator;
148  wrbuf_printf(b, "@%s ", complex_op_name(op) );
149  if (op->which== Z_Operator_prox)
150  {
151  if (!op->u.prox->exclusion)
152  wrbuf_putc(b, 'n');
153  else if (*op->u.prox->exclusion)
154  wrbuf_putc(b, '1');
155  else
156  wrbuf_putc(b, '0');
157 
158  wrbuf_printf(b, " " ODR_INT_PRINTF " %d "
159  ODR_INT_PRINTF " ", *op->u.prox->distance,
160  *op->u.prox->ordered,
161  *op->u.prox->relationType);
162 
163  switch(op->u.prox->which)
164  {
166  wrbuf_putc(b, 'k');
167  break;
169  wrbuf_putc(b, 'p');
170  break;
171  default:
172  wrbuf_printf(b, "%d", op->u.prox->which);
173  }
174  if (op->u.prox->u.known)
175  wrbuf_printf(b, " " ODR_INT_PRINTF " ", *op->u.prox->u.known);
176  else
177  wrbuf_printf(b, " 0 ");
178  }
181  }
182  else if (zs->which == Z_RPNStructure_simple)
183  {
184  if (zs->u.simple->which == Z_Operand_APT)
186  else if (zs->u.simple->which == Z_Operand_resultSetId)
187  {
188  wrbuf_printf(b, "@set ");
190  strlen(zs->u.simple->u.resultSetId));
191  }
192  else
193  wrbuf_puts(b, "(unknown simple structure)");
194  }
195  else
196  wrbuf_puts(b, "(unknown structure)");
197 }
198 
200 {
201  if (rpn->attributeSetId)
202  {
203  char oid_name_str[OID_STR_MAX];
204  const char *oid_name = yaz_oid_to_string_buf(rpn->attributeSetId,
205  0, oid_name_str);
206  if (oid_name)
207  wrbuf_printf(b, "@attrset %s ", oid_name);
208  }
210  wrbuf_chop_right(b);
211 }
212 
214 {
215  assert(q);
216  assert(b);
217  switch (q->which)
218  {
219  case Z_Query_type_1:
220  case Z_Query_type_101:
221  wrbuf_puts(b,"RPN ");
223  break;
224  case Z_Query_type_2:
225  wrbuf_puts(b, "CCL ");
226  wrbuf_write(b, (const char *) q->u.type_2->buf, q->u.type_2->len);
227  break;
228  case Z_Query_type_100:
229  wrbuf_puts(b, "Z39.58 ");
230  wrbuf_write(b, (const char *) q->u.type_100->buf, q->u.type_100->len);
231  break;
232  case Z_Query_type_104:
233  if (q->u.type_104->which == Z_External_CQL)
234  {
235  wrbuf_puts(b, "CQL ");
236  wrbuf_puts(b, q->u.type_104->u.cql);
237  }
238  else
239  wrbuf_printf(b,"UNKNOWN type 104 query %d", q->u.type_104->which);
240  }
241 }
242 
244  const Odr_oid *attrbute_set)
245 {
246  /* should print attr set here */
247  wrbuf_puts(b, "RPN ");
248  yaz_apt_to_wrbuf(b, zapt);
249 }
250 
251 void wrbuf_diags(WRBUF b, int num_diagnostics, Z_DiagRec **diags)
252 {
253  /* we only dump the first diag - that keeps the log cleaner. */
254  wrbuf_puts(b," ERROR ");
255  if (diags[0]->which != Z_DiagRec_defaultFormat)
256  wrbuf_puts(b,"(diag not in default format?)");
257  else
258  {
259  Z_DefaultDiagFormat *e = diags[0]->u.defaultFormat;
260  if (e->condition)
262  else
263  wrbuf_puts(b, "?? ");
265  wrbuf_puts(b, e->u.v2Addinfo);
266  else if ((e->which==Z_DefaultDiagFormat_v3Addinfo) && (e->u.v3Addinfo))
267  wrbuf_puts(b, e->u.v3Addinfo);
268  wrbuf_puts(b, " ");
269  }
270 }
271 
273 {
274  int i;
275  for (i = 0; i < fl->num; i++)
276  {
277  Z_FacetField *ff = fl->elements[i];
278 
279  if (i)
280  wrbuf_puts(w, ", ");
282  }
283 }
284 
285 /*
286  * Local variables:
287  * c-basic-offset: 4
288  * c-file-style: "Stroustrup"
289  * indent-tabs-mode: nil
290  * End:
291  * vim: shiftwidth=4 tabstop=8 expandtab
292  */
293 
Z_Operand * simple
Definition: z-core.h:502
Odr_int * condition
Definition: z-core.h:743
#define Z_AttributeValue_numeric
Definition: z-core.h:586
Odr_int * distance
Definition: z-core.h:593
int which
Definition: z-core.h:510
char * buf
Definition: odr.h:101
#define Z_DefaultDiagFormat_v3Addinfo
Definition: z-core.h:749
Z_AttributeList * attributes
Definition: z-facet-1.h:36
#define Z_Term_general
Definition: z-core.h:547
Odr_int * numeric
Definition: z-core.h:1322
Z_Complex * complex
Definition: z-core.h:503
void yaz_scan_to_wrbuf(WRBUF b, const Z_AttributesPlusTerm *zapt, const Odr_oid *attrbute_set)
Definition: querytowrbuf.c:243
#define Z_AttributeValue_complex
Definition: z-core.h:587
Z_RPNStructure * s2
Definition: z-core.h:495
#define Z_Query_type_1
Definition: z-core.h:479
#define Z_External_CQL
Definition: prt-ext.h:93
Query to WRBUF (to strings)
Z_ProximityOperator * prox
Definition: z-core.h:564
Odr_int * numeric
Definition: z-core.h:584
#define Z_DiagRec_defaultFormat
Definition: z-core.h:736
Z_ProxUnit * known
Definition: z-core.h:604
const char * yaz_oid_to_string_buf(const Odr_oid *oid, oid_class *oclass, char *buf)
maps any OID to string (named or dot-notation)
Definition: oid_db.c:99
#define Z_DefaultDiagFormat_v2Addinfo
Definition: z-core.h:748
void wrbuf_puts(WRBUF b, const char *buf)
appends C-string to WRBUF
Definition: wrbuf.c:89
Z_RPNStructure * RPNStructure
Definition: z-core.h:490
Odr_bool * exclusion
Definition: z-core.h:592
#define Z_Operator_or
Definition: z-core.h:566
#define Z_Operator_prox
Definition: z-core.h:568
short Odr_oid
Definition: oid_util.h:42
#define Z_Query_type_101
Definition: z-core.h:482
void wrbuf_write(WRBUF b, const char *buf, size_t size)
append constant size buffer to WRBUF
Definition: wrbuf.c:68
Odr_oct * type_100
Definition: z-core.h:474
string buffer
Definition: wrbuf.h:42
Z_AttributesPlusTerm * attributesPlusTerm
Definition: z-core.h:512
Z_AttributeElement ** attributes
Definition: z-core.h:533
Z_External * type_104
Definition: z-core.h:477
#define Z_Query_type_104
Definition: z-core.h:484
void wrbuf_diags(WRBUF b, int num_diagnostics, Z_DiagRec **diags)
Definition: querytowrbuf.c:251
Z_AttributeList * attributes
Definition: z-core.h:522
Z_RPNQuery * type_1
Definition: z-core.h:472
union Z_Operator::@52 u
#define Z_RPNStructure_complex
Definition: z-core.h:505
Odr_oct * general
Definition: z-core.h:539
Z_AttributeSetId * attributeSet
Definition: z-core.h:580
Header for OID database.
void yaz_facet_list_to_wrbuf(WRBUF w, const Z_FacetList *fl)
Definition: querytowrbuf.c:272
union Z_External::@30 u
int which
Definition: prt-ext.h:63
#define Z_StringOrNumeric_numeric
Definition: z-core.h:1324
Odr_bool * ordered
Definition: z-core.h:594
Odr_int * numeric
Definition: z-core.h:540
Z_ResultSetId * resultSetId
Definition: z-core.h:513
int which
Definition: z-core.h:537
#define Z_Term_numeric
Definition: z-core.h:548
static void yaz_attribute_element_to_wrbuf(WRBUF b, const Z_AttributeElement *element)
Definition: querytowrbuf.c:50
union Z_DefaultDiagFormat::@60 u
union Z_DiagRec::@59 u
Header for Z39.50 Query Printing.
union Z_AttributeElement::@53 value
int which
Definition: z-core.h:469
#define Z_StringOrNumeric_string
Definition: z-core.h:1323
void wrbuf_printf(WRBUF b, const char *fmt,...)
writes printf result to WRBUF
Definition: wrbuf.c:178
#define Z_Query_type_100
Definition: z-core.h:481
#define Z_Operand_resultSetId
Definition: z-core.h:516
char * v2Addinfo
Definition: z-core.h:746
Z_InternationalString * v3Addinfo
Definition: z-core.h:747
static void yaz_rpnstructure_to_wrbuf(WRBUF b, const Z_RPNStructure *zs)
Definition: querytowrbuf.c:143
union Z_Query::@48 u
Odr_int * attributeType
Definition: z-core.h:581
static void yaz_attributes_to_wrbuf(WRBUF w, const Z_AttributeList *al)
Definition: querytowrbuf.c:110
#define OID_STR_MAX
Definition: oid_util.h:40
Odr_oct * type_2
Definition: z-core.h:473
#define ODR_INT_PRINTF
Definition: odr.h:49
#define Z_Operator_and
Definition: z-core.h:565
int which
Definition: z-core.h:559
void wrbuf_chop_right(WRBUF b)
chips traling blanks away from WRBUF
Definition: wrbuf.c:130
#define Z_Query_type_2
Definition: z-core.h:480
void yaz_query_to_wrbuf(WRBUF b, const Z_Query *q)
Definition: querytowrbuf.c:213
#define Z_Operand_APT
Definition: z-core.h:515
#define Z_Term_null
Definition: z-core.h:554
union Z_RPNStructure::@49 u
#define wrbuf_putc(b, c)
Definition: wrbuf.h:268
Z_InternationalString * string
Definition: z-core.h:1321
Z_FacetField ** elements
Definition: z-facet-1.h:32
union Z_StringOrNumeric::@75 u
Z_StringOrNumeric ** list
Definition: z-core.h:574
Z_DefaultDiagFormat * defaultFormat
Definition: z-core.h:734
void yaz_encode_pqf_term(WRBUF b, const char *term, int len)
Definition: querytowrbuf.c:19
static const char * complex_op_name(const Z_Operator *op)
Definition: querytowrbuf.c:93
#define Z_Term_characterString
Definition: z-core.h:549
int num_attributes
Definition: z-core.h:532
#define Z_ProximityOperator_known
Definition: z-core.h:606
Z_ComplexAttribute * complex
Definition: z-core.h:585
#define Z_Operator_and_not
Definition: z-core.h:567
void yaz_rpnquery_to_wrbuf(WRBUF b, const Z_RPNQuery *rpn)
Definition: querytowrbuf.c:199
#define Z_RPNStructure_simple
Definition: z-core.h:504
Z_InternationalString * cql
Definition: prt-ext.h:138
union Z_Operand::@50 u
#define Z_ProximityOperator_private
Definition: z-core.h:607
static void yaz_apt_to_wrbuf(WRBUF b, const Z_AttributesPlusTerm *zapt)
Definition: querytowrbuf.c:117
Z_Operator * roperator
Definition: z-core.h:496
Z_InternationalString * characterString
Definition: z-core.h:541
Z_AttributeSetId * attributeSetId
Definition: z-core.h:489
Odr_int * relationType
Definition: z-core.h:601
union Z_ProximityOperator::@54 u
Z_RPNStructure * s1
Definition: z-core.h:494
union Z_Term::@51 u
int len
Definition: odr.h:102