Remove // comment
[idzebra-moved-to-github.git] / perl / zebra_perl.c
1 #if 0
2 #include "zebraapi.h"
3 #endif
4
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8
9 #include <yaz/yaz-util.h>
10 #include <yaz/proto.h>
11 #include <yaz/log.h>
12 #include <yaz/cql.h>
13 #include <yaz/pquery.h>
14
15 #include "zebra_perl.h"
16 #include <data1.h>
17
18 NMEM handles;
19
20 void init (void) {
21   nmem_init ();
22   yaz_log_init_prefix ("ZebraPerl");
23   yaz_log (LOG_LOG, "Zebra API initialized");
24 }
25
26 void DESTROY (void) {
27   nmem_exit ();
28   yaz_log (LOG_LOG, "Zebra API destroyed");
29 }   
30
31 /* Logging facilities from yaz */
32 void logLevel (int level) {
33   yaz_log_init_level(level);
34 }
35  
36 void logFile (const char *fname) {
37   yaz_log_init_file(fname);
38 }
39
40 void logMsg (int level, const char *message) {
41   logf(level, "%s", message);
42 }
43
44 /* debug tool for data1... maybe should be moved to data1. 
45    perl can't really pass filehandles */
46 void data1_print_tree(data1_handle dh, data1_node *n) {
47   data1_pr_tree(dh, n, stdout);
48 }
49
50 /* ---------------------------------------------------------------------------
51   Record retrieval 
52   2 phase retrieval - I didn't manage to return array of blessed references
53   to wrapped structures... it's feasible, but I'll need some time 
54   / pop - 2002-11-17
55 */
56
57 void record_retrieve(RetrievalObj *ro,
58                      ODR stream,
59                      RetrievalRecord *res,
60                      int pos) 
61 {
62   int i = pos - 1;
63
64
65   RetrievalRecordBuf *buf = 
66     (RetrievalRecordBuf *) odr_malloc(stream, sizeof(*buf));  
67
68   res->errCode    = ro->records[i].errCode;
69   if (ro->records[i].errString) {
70     res->errString  = odr_strdup(stream, ro->records[i].errString);
71   } else {
72     res->errString = "";
73   }
74   res->position   = ro->records[i].position;
75   res->base       = ro->records[i].base;
76   res->format     = (char *) 
77     yaz_z3950_oid_value_to_str(ro->records[i].format, CLASS_RECSYN); 
78   res->buf        = buf;
79   res->buf->len   = ro->records[i].len;
80   res->buf->buf   = ro->records[i].buf;
81   res->score      = ro->records[i].score;
82   res->sysno      = ro->records[i].sysno;
83
84 }
85
86
87
88 /* most of the code here was copied from yaz-client */
89 void records_retrieve(ZebraHandle zh,
90                       ODR stream,
91                       const char *setname,
92                       const char *a_eset, 
93                       const char *a_schema,
94                       const char *a_format,
95                       int from,
96                       int to,
97                       RetrievalObj *res) 
98 {
99   static enum oid_value recordsyntax = VAL_SUTRS;
100   static enum oid_value schema = VAL_NONE;
101   static Z_ElementSetNames *elementSetNames = 0; 
102   static Z_RecordComposition compo;
103   static Z_ElementSetNames esn;
104   static char what[100];
105   int i;
106   int oid[OID_SIZE];
107
108   compo.which = -1;
109
110   if (from < 1) from = 1;
111   if (from > to) to = from;
112   res->noOfRecords = to - from + 1;
113
114   res->records = odr_malloc (stream, 
115                              sizeof(*res->records) * (res->noOfRecords));  
116
117   for (i = 0; i<res->noOfRecords; i++) res->records[i].position = from+i;
118
119   if (!a_eset || !*a_eset) {
120     elementSetNames = 0;
121   } else {
122     strcpy(what, a_eset);
123     esn.which = Z_ElementSetNames_generic;
124     esn.u.generic = what;
125     elementSetNames = &esn;
126   }
127
128   if (!a_schema || !*a_schema) {
129     schema = VAL_NONE;
130   } else {
131     schema = oid_getvalbyname (a_schema);
132     if (schema == VAL_NONE) {
133       logf(LOG_WARN,"unknown schema '%s'",a_schema);
134     }
135   }
136
137   
138   if (!a_format || !*a_format) {
139     recordsyntax = VAL_SUTRS;
140   } else {
141     recordsyntax = oid_getvalbyname (a_format);
142     if (recordsyntax == VAL_NONE) {
143       logf(LOG_WARN,"unknown record syntax '%s', using SUTRS",a_schema);
144       recordsyntax = VAL_SUTRS;
145     }
146   }
147
148   if (schema != VAL_NONE) {
149     oident prefschema;
150
151     prefschema.proto = PROTO_Z3950;
152     prefschema.oclass = CLASS_SCHEMA;
153     prefschema.value = schema;
154     
155     compo.which = Z_RecordComp_complex;
156     compo.u.complex = (Z_CompSpec *)
157       odr_malloc(stream, sizeof(*compo.u.complex));
158     compo.u.complex->selectAlternativeSyntax = (bool_t *) 
159       odr_malloc(stream, sizeof(bool_t));
160     *compo.u.complex->selectAlternativeSyntax = 0;
161     
162     compo.u.complex->generic = (Z_Specification *)
163       odr_malloc(stream, sizeof(*compo.u.complex->generic));
164     compo.u.complex->generic->which = Z_Schema_oid;
165     compo.u.complex->generic->schema.oid = (Odr_oid *)
166       odr_oiddup(stream, oid_ent_to_oid(&prefschema, oid));
167     if (!compo.u.complex->generic->schema.oid)
168       {
169         /* OID wasn't a schema! Try record syntax instead. */
170         prefschema.oclass = CLASS_RECSYN;
171         compo.u.complex->generic->schema.oid = (Odr_oid *)
172           odr_oiddup(stream, oid_ent_to_oid(&prefschema, oid));
173       }
174     if (!elementSetNames)
175       compo.u.complex->generic->elementSpec = 0;
176     else
177       {
178         compo.u.complex->generic->elementSpec = (Z_ElementSpec *)
179           odr_malloc(stream, sizeof(Z_ElementSpec));
180         compo.u.complex->generic->elementSpec->which =
181           Z_ElementSpec_elementSetName;
182         compo.u.complex->generic->elementSpec->u.elementSetName =
183           elementSetNames->u.generic;
184       }
185     compo.u.complex->num_dbSpecific = 0;
186     compo.u.complex->dbSpecific = 0;
187     compo.u.complex->num_recordSyntax = 0;
188     compo.u.complex->recordSyntax = 0;
189   } 
190   else if (elementSetNames) {
191     compo.which = Z_RecordComp_simple;
192     compo.u.simple = elementSetNames;
193   }
194
195   if (compo.which == -1) {
196     api_records_retrieve (zh, stream, setname, 
197                             NULL, 
198                             recordsyntax,
199                             res->noOfRecords, res->records);
200   } else {
201     api_records_retrieve (zh, stream, setname, 
202                             &compo,
203                             recordsyntax,
204                             res->noOfRecords, res->records);
205   }
206
207 }
208  
209 int zebra_cql2pqf (cql_transform_t ct, 
210                    const char *query, char *res, int len) {
211   
212   int status;
213   const char *addinfo = "";
214   CQL_parser cp = cql_parser_create();
215
216   if (status = cql_parser_string(cp, query)) {
217     cql_parser_destroy(cp);
218     return (status);
219   }
220
221   if (cql_transform_buf(ct, cql_parser_result(cp), res, len)) {
222     status = cql_transform_error(ct, &addinfo);
223     logf (LOG_WARN,"Transform error %d %s\n", status, addinfo ? addinfo : "");
224     cql_parser_destroy(cp);
225     return (status);
226   }
227
228   cql_parser_destroy(cp);
229   return (0);
230 }
231
232 void zebra_scan_PQF (ZebraHandle zh,
233                      ScanObj *so,
234                      ODR stream,
235                      const char *pqf_query)
236 {
237   Z_AttributesPlusTerm *zapt;
238   Odr_oid *attrsetid;
239   const char* oidname;
240   oid_value attributeset;
241   ZebraScanEntry *entries;
242   int i, class;
243
244   logf(LOG_DEBUG,  
245        "scan req: pos:%d, num:%d, partial:%d", 
246        so->position, so->num_entries, so->is_partial);
247
248   zapt = p_query_scan (stream, PROTO_Z3950, &attrsetid, pqf_query);
249
250   oidname = yaz_z3950oid_to_str (attrsetid, &class); 
251   logf (LOG_DEBUG, "Attributreset: %s", oidname);
252   attributeset = oid_getvalbyname(oidname);
253
254   if (!zapt) {
255     logf (LOG_WARN, "bad query %s\n", pqf_query);
256     odr_reset (stream);
257     return;
258   }
259
260   so->entries = (scanEntry *)
261     odr_malloc (stream, sizeof(so->entries) * (so->num_entries));
262
263
264   zebra_scan (zh, stream, zapt, attributeset, 
265               &so->position, &so->num_entries, 
266               (ZebraScanEntry **) &so->entries, &so->is_partial);
267
268   logf(LOG_DEBUG, 
269        "scan res: pos:%d, num:%d, partial:%d", 
270        so->position, so->num_entries, so->is_partial);
271 }
272
273 scanEntry *getScanEntry(ScanObj *so, int pos) {
274   return (&so->entries[pos-1]);
275 }
276