zoom 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,116 @@
1
+ /*
2
+ * Copyright (C) 2005 Laurent Sansonetti <lrz@chopine.be>
3
+ *
4
+ * This library is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU Lesser General Public
6
+ * License as published by the Free Software Foundation; either
7
+ * version 2.1 of the License, or (at your option) any later version.
8
+ *
9
+ * This library is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ * Lesser General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU Lesser General Public
15
+ * License along with this library; if not, write to the Free Software
16
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
+ */
18
+
19
+ #include "rbzoom.h"
20
+
21
+ /* Class: ZOOM::Query
22
+ * Search queries.
23
+ */
24
+ static VALUE cZoomQuery;
25
+
26
+ static VALUE
27
+ rbz_query_make (ZOOM_query query)
28
+ {
29
+ return query != NULL
30
+ ? Data_Wrap_Struct (cZoomQuery,
31
+ NULL,
32
+ ZOOM_query_destroy,
33
+ query)
34
+ : Qnil;
35
+ }
36
+
37
+ ZOOM_query
38
+ rbz_query_get (VALUE obj)
39
+ {
40
+ ZOOM_query query;
41
+
42
+ Data_Get_Struct (obj, struct ZOOM_query_p, query);
43
+ assert (query != NULL);
44
+
45
+ return query;
46
+ }
47
+
48
+ /*
49
+ * Class method: new_prefix(prefix)
50
+ * prefix: PQF notation.
51
+ *
52
+ * Creates a RPN query using the given PQF notation.
53
+ *
54
+ * Returns: a newly created ZOOM::Query object.
55
+ */
56
+ static VALUE
57
+ rbz_query_new_prefix (VALUE self, VALUE prefix)
58
+ {
59
+ ZOOM_query query;
60
+
61
+ query = ZOOM_query_create ();
62
+ ZOOM_query_prefix (query, RVAL2CSTR (prefix));
63
+
64
+ return rbz_query_make (query);
65
+ }
66
+
67
+ /*
68
+ * Class method: new_cql(prefix)
69
+ * prefix: CQL notation.
70
+ *
71
+ * Creates a CQL query using the given CQL notation.
72
+ *
73
+ * Returns: a newly created ZOOM::Query object.
74
+ */
75
+ static VALUE
76
+ rbz_query_new_cql (VALUE self, VALUE cql)
77
+ {
78
+ ZOOM_query query;
79
+
80
+ query = ZOOM_query_create ();
81
+ ZOOM_query_cql (query, RVAL2CSTR (cql));
82
+
83
+ return rbz_query_make (query);
84
+ }
85
+
86
+ /*
87
+ * Class method: new_sort_by(criteria)
88
+ * criteria: a sort criteria.
89
+ *
90
+ * Creates a sort query from the YAZ sorting notation.
91
+ *
92
+ * Returns: a newly created ZOOM::Query object.
93
+ */
94
+ static VALUE
95
+ rbz_query_new_sort_by (VALUE self, VALUE criteria)
96
+ {
97
+ ZOOM_query query;
98
+
99
+ query = ZOOM_query_create ();
100
+ ZOOM_query_sortby (rbz_query_get (self), RVAL2CSTR (criteria));
101
+
102
+ return rbz_query_make (query);
103
+ }
104
+
105
+ void
106
+ Init_zoom_query (VALUE mZoom)
107
+ {
108
+ VALUE c;
109
+
110
+ c = rb_define_class_under (mZoom, "Query", rb_cObject);
111
+ rb_define_singleton_method (c, "new_prefix", rbz_query_new_prefix, 1);
112
+ rb_define_singleton_method (c, "new_cql", rbz_query_new_cql, 1);
113
+ rb_define_singleton_method (c, "new_sort_by", rbz_query_new_sort_by, 1);
114
+
115
+ cZoomQuery = c;
116
+ }
@@ -0,0 +1,176 @@
1
+ /*
2
+ * Copyright (C) 2005 Laurent Sansonetti <lrz@chopine.be>
3
+ *
4
+ * This library is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU Lesser General Public
6
+ * License as published by the Free Software Foundation; either
7
+ * version 2.1 of the License, or (at your option) any later version.
8
+ *
9
+ * This library is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ * Lesser General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU Lesser General Public
15
+ * License along with this library; if not, write to the Free Software
16
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
+ */
18
+
19
+ #include "rbzoom.h"
20
+
21
+ /* Class: ZOOM::Record
22
+ * A record object is a retrieval record on the client side - created from
23
+ * result sets.
24
+ */
25
+ static VALUE cZoomRecord;
26
+
27
+ VALUE
28
+ rbz_record_make (ZOOM_record record)
29
+ {
30
+ return record != NULL
31
+ ? Data_Wrap_Struct (cZoomRecord,
32
+ NULL,
33
+ ZOOM_record_destroy,
34
+ record)
35
+ : Qnil;
36
+ }
37
+
38
+ static ZOOM_record
39
+ rbz_record_get (VALUE obj)
40
+ {
41
+ ZOOM_record record;
42
+
43
+ Data_Get_Struct (obj, struct ZOOM_record_p, record);
44
+ assert (record != NULL);
45
+
46
+ return record;
47
+ }
48
+
49
+ static char _type [128];
50
+
51
+ static const char *
52
+ rbz_record_type (const char *form, int argc, VALUE *argv)
53
+ {
54
+ VALUE charset_from;
55
+ VALUE charset_to;
56
+
57
+ if (argc == 0)
58
+ return form;
59
+
60
+ rb_scan_args (argc, argv, "11", &charset_from, &charset_to);
61
+
62
+ memset (_type, 0, sizeof _type);
63
+
64
+ if (NIL_P (charset_to))
65
+ snprintf (_type, sizeof _type, "%s; charset=%s", form,
66
+ RVAL2CSTR (charset_from));
67
+ else
68
+ snprintf (_type, sizeof _type, "%s; charset=%s,%s", form,
69
+ RVAL2CSTR (charset_from), RVAL2CSTR (charset_to));
70
+
71
+ return _type;
72
+ }
73
+
74
+ /*
75
+ * Method: database(charset_from=nil, charset_to=nil)
76
+ * charset_from: the name of the charset to convert from (optional).
77
+ * charset_to: the name of the charset to convert to (optional).
78
+ *
79
+ * Returns: the database name of the record.
80
+ */
81
+ static VALUE
82
+ rbz_record_database (int argc, VALUE *argv, VALUE self)
83
+ {
84
+ return CSTR2RVAL (ZOOM_record_get (rbz_record_get (self),
85
+ rbz_record_type ("database", argc, argv),
86
+ NULL));
87
+ }
88
+
89
+ /*
90
+ * Method: syntax(charset_from=nil, charset_to=nil)
91
+ * charset_from: the name of the charset to convert from (optional).
92
+ * charset_to: the name of the charset to convert to (optional).
93
+ *
94
+ * Returns: the symbolic transfer syntax name of the record.
95
+ */
96
+ static VALUE
97
+ rbz_record_syntax (int argc, VALUE *argv, VALUE self)
98
+ {
99
+ return CSTR2RVAL (ZOOM_record_get (rbz_record_get (self),
100
+ rbz_record_type ("syntax", argc, argv),
101
+ NULL));
102
+ }
103
+
104
+ /*
105
+ * Method: render(charset_from=nil, charset_to=nil)
106
+ * charset_from: the name of the charset to convert from (optional).
107
+ * charset_to: the name of the charset to convert to (optional).
108
+ *
109
+ * Returns: a display friendly description of the record.
110
+ */
111
+ static VALUE
112
+ rbz_record_render (int argc, VALUE *argv, VALUE self)
113
+ {
114
+ return CSTR2RVAL (ZOOM_record_get (rbz_record_get (self),
115
+ rbz_record_type ("render", argc, argv),
116
+ NULL));
117
+ }
118
+
119
+ /*
120
+ * Method: xml(charset_from=nil, charset_to=nil)
121
+ * charset_from: the name of the charset to convert from (optional).
122
+ * charset_to: the name of the charset to convert to (optional).
123
+ *
124
+ * Returns an XML description of the record. SRW/SRU and Z39.50 records with
125
+ * transfer syntax XML are returned verbatim. MARC records are returned in
126
+ * MARCXML (converted from ISO2709 to MARCXML by YAZ). GRS-1 and OPAC records are
127
+ * not supported for this form.
128
+ *
129
+ * Returns: an XML description of the record.
130
+ */
131
+ static VALUE
132
+ rbz_record_xml (int argc, VALUE *argv, VALUE self)
133
+ {
134
+ return CSTR2RVAL (ZOOM_record_get (rbz_record_get (self),
135
+ rbz_record_type ("xml", argc, argv),
136
+ NULL));
137
+ }
138
+
139
+ /*
140
+ * Method: raw(charset_from=nil, charset_to=nil)
141
+ * charset_from: the name of the charset to convert from (optional).
142
+ * charset_to: the name of the charset to convert to (optional).
143
+ *
144
+ * Does not change the leader on character conversion.
145
+ *
146
+ * MARC records are returned in ISO2709.
147
+ * GRS-1 and OPAC records are not supported for this form.
148
+ *
149
+ * Returns: an ISO2709 record.
150
+ */
151
+
152
+ static VALUE
153
+ rbz_record_raw (int argc, VALUE *argv, VALUE self)
154
+ {
155
+ return CSTR2RVAL (ZOOM_record_get (rbz_record_get (self),
156
+ rbz_record_type ("raw", argc, argv),
157
+ NULL));
158
+ }
159
+
160
+ void
161
+ Init_zoom_record (VALUE mZoom)
162
+ {
163
+ VALUE c;
164
+
165
+ c = rb_define_class_under (mZoom, "Record", rb_cObject);
166
+ rb_undef_method (CLASS_OF (c), "new");
167
+
168
+ rb_define_method (c, "database", rbz_record_database, -1);
169
+ rb_define_method (c, "syntax", rbz_record_syntax, -1);
170
+ rb_define_method (c, "render", rbz_record_render, -1);
171
+ rb_define_alias (c, "to_s", "render");
172
+ rb_define_method (c, "xml", rbz_record_xml, -1);
173
+ rb_define_method (c, "raw", rbz_record_raw, -1);
174
+
175
+ cZoomRecord = c;
176
+ }
@@ -0,0 +1,222 @@
1
+ /*
2
+ * Copyright (C) 2005 Laurent Sansonetti <lrz@chopine.be>
3
+ *
4
+ * This library is free software; you can redistribute it and/or
5
+ * modify it under the terms of the GNU Lesser General Public
6
+ * License as published by the Free Software Foundation; either
7
+ * version 2.1 of the License, or (at your option) any later version.
8
+ *
9
+ * This library is distributed in the hope that it will be useful,
10
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12
+ * Lesser General Public License for more details.
13
+ *
14
+ * You should have received a copy of the GNU Lesser General Public
15
+ * License along with this library; if not, write to the Free Software
16
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17
+ */
18
+
19
+ #include "rbzoom.h"
20
+
21
+ /* Class: ZOOM::ResultSet
22
+ * The result set object is a container for records returned from a target.
23
+ */
24
+ static VALUE cZoomResultSet;
25
+
26
+ VALUE
27
+ rbz_resultset_make (ZOOM_resultset resultset)
28
+ {
29
+ return resultset != NULL
30
+ ? Data_Wrap_Struct (cZoomResultSet,
31
+ NULL,
32
+ ZOOM_resultset_destroy,
33
+ resultset)
34
+ : Qnil;
35
+ }
36
+
37
+ static ZOOM_resultset
38
+ rbz_resultset_get (VALUE obj)
39
+ {
40
+ ZOOM_resultset resultset;
41
+
42
+ Data_Get_Struct (obj, struct ZOOM_resultset_p, resultset);
43
+ assert (resultset != NULL);
44
+
45
+ return resultset;
46
+ }
47
+
48
+ /*
49
+ * Method: set_option(key, value)
50
+ * key: the name of the option, as a string.
51
+ * value: the value of this option (as a string, integer or boolean).
52
+ *
53
+ * Sets an option on the result set.
54
+ *
55
+ * Returns: self.
56
+ */
57
+ static VALUE
58
+ rbz_resultset_set_option (VALUE self, VALUE key, VALUE val)
59
+ {
60
+ ZOOM_resultset_option_set (rbz_resultset_get (self),
61
+ RVAL2CSTR (key),
62
+ RVAL2CSTR (rb_obj_as_string (val)));
63
+
64
+ return self;
65
+ }
66
+
67
+ /*
68
+ * Method: get_option(key)
69
+ * key: the name of the option, as a string.
70
+ *
71
+ * Gets the value of a result set's option.
72
+ *
73
+ * Returns: the value of the given option, as a string, integer or boolean.
74
+ */
75
+ static VALUE
76
+ rbz_resultset_get_option (VALUE self, VALUE key)
77
+ {
78
+ const char *value;
79
+
80
+ value = ZOOM_resultset_option_get (rbz_resultset_get (self),
81
+ RVAL2CSTR (key));
82
+
83
+ return zoom_option_value_to_ruby_value (value);
84
+ }
85
+
86
+ /* Method: size
87
+ * Returns: the number of hits.
88
+ */
89
+ static VALUE
90
+ rbz_resultset_size (VALUE self)
91
+ {
92
+ return INT2NUM (ZOOM_resultset_size (rbz_resultset_get (self)));
93
+ }
94
+
95
+ /*
96
+ * Method: [](key)
97
+ * key: either an integer, a range or an interval of 2 integers.
98
+ *
99
+ * Retrieves one or many records from the result set, according to the given
100
+ * key.
101
+ *
102
+ * # Gets the first record.
103
+ * rset[0]
104
+ * # Gets the first, second and third records.
105
+ * rset[1..3]
106
+ * # Gets three records starting from the second one.
107
+ * rset[2, 3]
108
+ *
109
+ * Returns: one or many references to ZOOM::Record objects.
110
+ */
111
+ static VALUE
112
+ rbz_resultset_index (int argc, VALUE *argv, VALUE self)
113
+ {
114
+ ZOOM_record record;
115
+ VALUE ary;
116
+ size_t begin;
117
+ size_t count;
118
+ size_t i;
119
+
120
+ if (argc == 1) {
121
+ VALUE arg = argv [0];
122
+
123
+ if (TYPE (arg) == T_FIXNUM || TYPE (arg) == T_BIGNUM) {
124
+ record = ZOOM_resultset_record (rbz_resultset_get (self),
125
+ NUM2LONG (arg));
126
+ return record != NULL
127
+ ? rbz_record_make (ZOOM_record_clone (record))
128
+ : Qnil;
129
+ }
130
+
131
+ if (CLASS_OF (arg) == rb_cRange) {
132
+ begin = NUM2LONG (rb_funcall (arg, rb_intern ("begin"), 0));
133
+ count = NUM2LONG (rb_funcall (arg, rb_intern ("end"), 0));
134
+ count -= begin;
135
+ }
136
+ else
137
+ rb_raise (rb_eArgError,
138
+ "Invalid argument of type %s (not Numeric or Range)",
139
+ rb_class2name (CLASS_OF (arg)));
140
+ }
141
+ else {
142
+ VALUE rb_begin;
143
+ VALUE rb_count;
144
+
145
+ rb_scan_args (argc, argv, "2", &rb_begin, &rb_count);
146
+
147
+ begin = NUM2LONG (rb_begin);
148
+ count = NUM2LONG (rb_count);
149
+ }
150
+
151
+ ary = rb_ary_new ();
152
+ if (count == 0)
153
+ return ary;
154
+
155
+ for (i = 0; i < count; i++) {
156
+ record = ZOOM_resultset_record (rbz_resultset_get (self),
157
+ begin + i);
158
+ if (record != NULL)
159
+ rb_ary_push (ary, rbz_record_make (ZOOM_record_clone (record)));
160
+ }
161
+ return ary;
162
+ }
163
+
164
+ /*
165
+ * Method: records
166
+ *
167
+ * Lists the records inside the result set.
168
+ *
169
+ * Returns: an array of ZOOM::Record objects.
170
+ */
171
+ static VALUE
172
+ rbz_resultset_records (VALUE self)
173
+ {
174
+ VALUE argv [2];
175
+
176
+ argv [0] = INT2FIX (0);
177
+ argv [1] = rbz_resultset_size (self);
178
+
179
+ return rbz_resultset_index (2, argv, self);
180
+ }
181
+
182
+ /*
183
+ * Method: each_record { |record| ... }
184
+ *
185
+ * Parses the records inside the result set and call the given block for each
186
+ * record, passing a reference to a ZOOM::Record object as parameter.
187
+ *
188
+ * Returns: self.
189
+ */
190
+ static VALUE
191
+ rbz_resultset_each_record (VALUE self)
192
+ {
193
+ rb_ary_each (rbz_resultset_records (self));
194
+ return self;
195
+ }
196
+
197
+ void
198
+ Init_zoom_resultset (VALUE mZoom)
199
+ {
200
+ VALUE c;
201
+
202
+ c = rb_define_class_under (mZoom, "ResultSet", rb_cObject);
203
+ rb_undef_method (CLASS_OF (c), "new");
204
+ rb_define_method (c, "set_option", rbz_resultset_set_option, 2);
205
+ rb_define_method (c, "get_option", rbz_resultset_get_option, 1);
206
+
207
+ define_zoom_option (c, "start");
208
+ define_zoom_option (c, "count");
209
+ define_zoom_option (c, "presentChunk");
210
+ define_zoom_option (c, "elementSetName");
211
+ define_zoom_option (c, "preferredRecordSyntax");
212
+ define_zoom_option (c, "schema");
213
+ define_zoom_option (c, "setname");
214
+
215
+ rb_define_method (c, "size", rbz_resultset_size, 0);
216
+ rb_define_alias (c, "length", "size");
217
+ rb_define_method (c, "records", rbz_resultset_records, 0);
218
+ rb_define_method (c, "each_record", rbz_resultset_each_record, 0);
219
+ rb_define_method (c, "[]", rbz_resultset_index, -1);
220
+
221
+ cZoomResultSet = c;
222
+ }