KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > runtime > LookAheadJdbcResultSet


1 /*
2  * This file belongs to the XQuark distribution.
3  * Copyright (C) 2003 Universite de Versailles Saint-Quentin.
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307.
18  * You can also get it at http://www.gnu.org/licenses/lgpl.html
19  *
20  * For more information on this software, see http://www.xquark.org.
21  */

22
23 package org.xquark.extractor.runtime;
24
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.ResultSetMetaData JavaDoc;
27 import java.sql.SQLException JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.xquark.extractor.algebra.Mapper;
32 import org.xquark.extractor.common.Debug;
33 import org.xquark.extractor.common.MessageLibrary;
34 import org.xquark.extractor.metadata.ExtractorMappingInfo;
35 import org.xquark.jdbc.typing.DBMSInfo;
36 import org.xquark.schema.validation.ValidationContextProvider;
37 import org.xquark.xquery.parser.XQueryException;
38 import org.xquark.xquery.typing.QAtomicSerializer;
39
40 /**
41  * A look ahead JDBC result set implementation used by {@link org.xquark.extractor.runtime.SynchronizedResultSet}.
42  * The look ahead is needed to be able to detect changes of skolem IDs.
43  */

44 class LookAheadJdbcResultSet {
45     private static final String JavaDoc RCSRevision = "$Revision: 1.3 $";
46     private static final String JavaDoc RCSName = "$Name: $";
47
48     private static final int BOR = 0; /* begin of resultset*/
49     private static final int BUFFER = 1;
50     private static final int RESULTSET = 2;
51
52     private ResultSet JavaDoc _resultset;
53     /** Buffer for an entire row **/
54     private String JavaDoc[] _buffer;
55     private Map JavaDoc _attributeMap = new HashMap JavaDoc();
56     private ColumnInfo[] _attributes; // because some drivers (SQLServer) don't like fetching out of the regular order....
57

58     private boolean _bufferNotEmpty;
59     private boolean _resultSetNotEmpty;
60     private int _cursor;
61     static private ValidationContextProvider _validationContext = new ValidationContextProvider() {
62         public String JavaDoc getNamespaceURI(String JavaDoc prefix) {
63             return null;
64         }
65         public String JavaDoc getPrefix(String JavaDoc uri) {
66             return null;
67         }
68         public String JavaDoc getDocumentBase() {
69             return null;
70         }
71         public Map JavaDoc getNotationDeclarations() {
72             return null;
73         }
74     };
75     private QAtomicSerializer qSerializer = new QAtomicSerializer(_validationContext);
76
77     public LookAheadJdbcResultSet(ResultSet JavaDoc resultset, Mapper mapper, DBMSInfo info) throws SQLException JavaDoc, XQueryException {
78         _resultset = resultset;
79
80         ResultSetMetaData JavaDoc rsm = _resultset.getMetaData();
81
82         int columnCount = rsm.getColumnCount();
83         _buffer = new String JavaDoc[columnCount];
84         _attributes = new ColumnInfo[columnCount];
85         String JavaDoc columnName = null;
86         ColumnInfo ci = null;
87         for (int i = 0; i < columnCount; i++) {
88             columnName = rsm.getColumnName(i + 1);
89             ci = new ColumnInfo(i + 1, mapper.findMapItem(columnName).getMappingInfo(info.getTypeMap()));
90             _attributeMap.put(columnName, ci);
91             _attributes[i] = ci;
92         }
93
94         _bufferNotEmpty = _resultset.next();
95         if (_bufferNotEmpty) {
96             refreshBuffer();
97             _resultSetNotEmpty = _resultset.next();
98         }
99         _cursor = BOR;
100     }
101
102     public boolean next() throws SQLException JavaDoc, XQueryException {
103         boolean retVal;
104         if (BOR == _cursor) {
105             _cursor = BUFFER;
106         } else {
107             Debug.assertTrue(BUFFER == _cursor, "BUFFER == _cursor");
108             if (_resultSetNotEmpty) {
109                 refreshBuffer();
110                 _resultSetNotEmpty = _resultset.next();
111             } else {
112                 _resultset.close();
113                 _resultset = null;
114                 _bufferNotEmpty = false;
115             }
116         }
117         return _bufferNotEmpty;
118     }
119
120     public boolean hasNext() {
121         boolean retVal = false;
122
123         if (BUFFER == _cursor) {
124             retVal = _resultSetNotEmpty;
125         } else if (BOR == _cursor) {
126             retVal = _bufferNotEmpty;
127         } else {
128             Debug.assertTrue(false, MessageLibrary.getMessage("IE_ERR"));
129         }
130
131         return retVal;
132     }
133
134     public boolean lookAhead() {
135         boolean retVal = false;
136         switch (_cursor) {
137             case BOR :
138                 retVal = _bufferNotEmpty;
139                 if (retVal) {
140                     _cursor = BUFFER;
141                 }
142                 break;
143             case BUFFER :
144                 retVal = _resultSetNotEmpty;
145                 if (retVal) {
146                     _cursor = RESULTSET;
147                 }
148                 break;
149             default :
150                 org.xquark.extractor.common.Debug.assertTrue(false, "Logic error");
151         }
152
153         return retVal;
154     }
155
156     public boolean goBack() {
157         boolean retVal = false;
158         switch (_cursor) {
159             case BUFFER :
160                 _cursor = BOR;
161                 retVal = true;
162                 break;
163             case RESULTSET :
164                 _cursor = BUFFER;
165                 retVal = true;
166                 break;
167             case BOR :
168                 retVal = false;
169                 break;
170             default :
171                 org.xquark.extractor.common.Debug.assertTrue(false, "Logic error");
172         }
173
174         return retVal;
175     }
176
177     public String JavaDoc getString(String JavaDoc name) throws SQLException JavaDoc {
178         String JavaDoc retVal = null;
179         ColumnInfo columnInfo = (ColumnInfo) _attributeMap.get(name);
180         if (BUFFER == _cursor) {
181             retVal = getStringFromBuffer(columnInfo.getIndex());
182         } else {
183             try {
184                 ExtractorMappingInfo mappingInfo = columnInfo.getMappingInfo();
185                 if (mappingInfo == null) // pseudo-id
186
retVal = _resultset.getString(columnInfo.getIndex());
187                 else
188                     retVal = mappingInfo.getParameter(_resultset, columnInfo.getIndex(), qSerializer, _validationContext);
189             } catch (XQueryException e) {
190             }
191         }
192         return retVal;
193     }
194
195     private void refreshBuffer() throws SQLException JavaDoc, XQueryException {
196         ExtractorMappingInfo mappingInfo = null;
197
198         for (int i = 0; i < _attributes.length; i++) {
199             mappingInfo = _attributes[i].getMappingInfo();
200             if (mappingInfo == null) { // pseudo-id
201
_buffer[i] = _resultset.getString(_attributes[i].getIndex());
202             } else {
203                 _buffer[i] = mappingInfo.getParameter(_resultset, _attributes[i].getIndex(), qSerializer, _validationContext);
204             }
205         }
206     }
207
208     private String JavaDoc getStringFromBuffer(int index) {
209         String JavaDoc retVal = null;
210         if (index > 0)
211             retVal = _buffer[index - 1];
212         return retVal;
213     }
214
215     /** @todo this function is to be removed after debug */
216     public ResultSet JavaDoc getSqlResultSet() {
217         return _resultset;
218     }
219
220     public void close() {
221         if (_resultset != null) {
222             try {
223                 _resultset.close();
224                 _resultset = null;
225             } catch (SQLException JavaDoc ex) {
226             }
227         }
228     }
229
230     private class ColumnInfo {
231         private int index = 0;
232         private ExtractorMappingInfo mappingInfo = null;
233
234         public ColumnInfo(int index, ExtractorMappingInfo mappingInfo) {
235             super();
236             this.index = index;
237             this.mappingInfo = mappingInfo;
238         }
239         public int getIndex() {
240             return index;
241         }
242
243         public ExtractorMappingInfo getMappingInfo() {
244             return mappingInfo;
245         }
246
247     }
248 }
249
Popular Tags