Return null if a record cannot be retrieved.
[yaz4j-moved-to-github.git] / src / main / java / org / yaz4j / ResultSet.java
1 package org.yaz4j;
2
3 import org.yaz4j.exception.ZoomException;
4 import org.yaz4j.jni.SWIGTYPE_p_ZOOM_record_p;
5 import org.yaz4j.jni.SWIGTYPE_p_ZOOM_resultset_p;
6 import org.yaz4j.jni.yaz4jlib;
7
8 public class ResultSet {
9     //for GC refcount
10     private Connection conn;
11     private SWIGTYPE_p_ZOOM_resultset_p resultSet;
12     private long size = 0;
13     private boolean disposed = false;
14
15     ResultSet(SWIGTYPE_p_ZOOM_resultset_p resultSet, Connection conn) {
16         this.resultSet = resultSet;
17         size = yaz4jlib.ZOOM_resultset_size(this.resultSet);
18         this.conn = conn;
19     }
20
21     @Override
22     public void finalize() {
23         this._dispose();
24     }
25
26     /**
27      * Read option by name.
28      * @param name option name
29      * @return option value
30      */
31     public String option(String name) {
32       return yaz4jlib.ZOOM_resultset_option_get(resultSet, name);
33     }
34
35     /**
36      * Write option with a given name.
37      * @param name option name
38      * @param value option value
39      * @return result set (self) for chainability
40      */
41     public ResultSet option(String name, String value) {
42       yaz4jlib.ZOOM_resultset_option_set(resultSet, name, value);
43       return this;
44     }
45
46     public Record getRecord(int index) throws ZoomException {
47       SWIGTYPE_p_ZOOM_record_p record = 
48         yaz4jlib.ZOOM_resultset_record(resultSet, index);
49       //may be out of range or unsupported syntax
50       if (record == null) {
51         return null;
52       }
53       int errorCode = yaz4jlib.ZOOM_record_error(record, null, null, null);
54       if (errorCode != 0) throw new ZoomException("Record excpetion, code " + errorCode);
55       return new Record(record, this);
56     }
57
58     public long getSize() {
59         return size;
60     }
61
62     void _dispose() {
63         if (!disposed) {
64             yaz4jlib.ZOOM_resultset_destroy(resultSet);
65             resultSet = null;
66             conn = null;
67             disposed = true;
68         }
69     }
70 }