KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > fetch > FetchResultImp


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc.fetch;
13
14 import com.versant.core.common.BindingSupportImpl;
15 import com.versant.core.common.Debug;
16
17 import java.sql.ResultSet JavaDoc;
18 import java.sql.PreparedStatement JavaDoc;
19 import java.sql.SQLException JavaDoc;
20
21 /**
22  * This maintains information about a FetchSpec used during a fetch operation.
23  * All the fields of the FetchSpec and FetchOp's themselves are read only
24  * once the spec has been prepared. This makes it possible to use the same
25  * FetchSpec from different threads simultaneously.
26  */

27 public class FetchResultImp implements FetchResult {
28
29     private FetchSpec spec;
30     private String JavaDoc sql;
31     private PreparedStatement JavaDoc ps;
32     private ResultSet JavaDoc rs;
33     private boolean scrollable;
34     private boolean haveRow;
35     private Object JavaDoc[] opData;
36
37     public FetchResultImp(FetchSpec spec, PreparedStatement JavaDoc ps, String JavaDoc sql,
38             boolean scrollable) {
39         this.spec = spec;
40         this.ps = ps;
41         this.sql = sql;
42         this.scrollable = scrollable;
43         opData = new Object JavaDoc[spec.getFetchOpCount()];
44     }
45
46     public void execute() {
47         if (rs != null) {
48             throw BindingSupportImpl.getInstance().internal(
49                     "query has already been executed");
50         }
51         checkClosed();
52         try {
53             rs = ps.executeQuery();
54         } catch (Exception JavaDoc e) {
55             throw spec.getSqlDriver().mapException(e,
56                     "Error executing query: " + e + "\nSQL:\n" + sql,
57                     true);
58         }
59     }
60
61     public void cancel() {
62         if (rs != null) {
63             try {
64                 ps.cancel();
65             } catch (SQLException JavaDoc e) {
66                 throw spec.getSqlDriver().mapException(e, e + "\nSQL:\n" + sql,
67                         true);
68             }
69         }
70     }
71
72     public void close() {
73         if (isClosed()) {
74             return;
75         }
76         if (rs != null) {
77             try {
78                 rs.close();
79             } catch (Exception JavaDoc e) {
80                 // ignore
81
}
82             rs = null;
83         }
84         try {
85             ps.close();
86         } catch (SQLException JavaDoc e) {
87             // ignore
88
}
89         ps = null;
90         spec.fetchResultClosed(this);
91         opData = null;
92     }
93
94     public boolean isClosed() {
95         return ps == null;
96     }
97
98     public boolean hasNext() {
99         if (ps == null) {
100             return false;
101         }
102         if (rs == null) {
103             execute();
104         }
105         try {
106             if (haveRow || (haveRow = rs.next())) {
107                 return true;
108             } else {
109                 close();
110                 return false;
111             }
112         } catch (Exception JavaDoc e) {
113             throw spec.getSqlDriver().mapException(e, e.toString(), true);
114         }
115     }
116
117     public Object JavaDoc next() {
118         if (haveRow || hasNext()) {
119             Object JavaDoc row = spec.createRow(this);
120             haveRow = false;
121             return row;
122         } else {
123             throw BindingSupportImpl.getInstance().internal("no more rows: " +
124                     this);
125         }
126     }
127
128     public void remove() {
129         throw new UnsupportedOperationException JavaDoc();
130     }
131
132     public boolean isScrollable() {
133         return scrollable;
134     }
135
136     private void checkClosed() {
137         if (ps == null) {
138             throw BindingSupportImpl.getInstance().internal(
139                     "FetchResult has been closed");
140         }
141     }
142
143     public int size() {
144         if (Debug.DEBUG) {
145             checkScrollable();
146         }
147         try {
148             if (rs == null) {
149                 execute();
150             }
151             if (rs.last()) {
152                 return rs.getRow();
153             } else {
154                 return 0;
155             }
156         } catch (Exception JavaDoc e) {
157             throw spec.getSqlDriver().mapException(e, e.toString(), true);
158         }
159     }
160
161     public Object JavaDoc get(int index) {
162         if (Debug.DEBUG) {
163             checkScrollable();
164         }
165         try {
166             if (rs == null) {
167                 execute();
168             }
169             if (rs.absolute(index + 1)) {
170                 return spec.createRow(this);
171             } else {
172                 throw BindingSupportImpl.getInstance().internal(
173                         "invalud index " + index + ": " + this);
174             }
175         } catch (Exception JavaDoc e) {
176             throw spec.getSqlDriver().mapException(e, e.toString(), true);
177         }
178     }
179
180     private void checkScrollable() {
181         if (!scrollable) {
182             throw BindingSupportImpl.getInstance().internal("not scrollable: " +
183                     this);
184         }
185     }
186
187     /**
188      * Get data for op.
189      */

190     public Object JavaDoc getData(FetchOp op) {
191         if (Debug.DEBUG) {
192             checkOp(op);
193         }
194         return opData[op.getIndex()];
195     }
196
197     /**
198      * Set data object for op.
199      */

200     public void setData(FetchOp op, Object JavaDoc data) {
201         if (Debug.DEBUG) {
202             checkOp(op);
203         }
204         opData[op.getIndex()] = data;
205     }
206
207     private void checkOp(FetchOp op) {
208         if (op == null) {
209             throw BindingSupportImpl.getInstance().internal(
210                     "op is null: " + this);
211         }
212         if (op.getSpec() != spec) {
213             throw BindingSupportImpl.getInstance().internal(
214                     "spec mismatch: " + this + ", op.getSpec() = " +
215                     op.getSpec() + ", op " + op);
216         }
217     }
218
219     public String JavaDoc toString() {
220         return "FetchResult 0x" +
221                 Integer.toHexString(System.identityHashCode(this)) +
222                 (isClosed() ? " CLOSED" : "") +
223                 " spec " + spec;
224     }
225
226     public ResultSet JavaDoc getResultSet() {
227         return rs;
228     }
229
230     public FetchSpec getFetchSpec() {
231         return spec;
232     }
233
234     public PreparedStatement JavaDoc getPreparedStatement() {
235         return ps;
236     }
237
238 }
239
240
Popular Tags