1 22 23 package org.xquark.extractor.runtime; 24 25 import java.sql.ResultSet ; 26 import java.sql.ResultSetMetaData ; 27 import java.sql.SQLException ; 28 import java.util.HashMap ; 29 import java.util.Map ; 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 44 class LookAheadJdbcResultSet { 45 private static final String RCSRevision = "$Revision: 1.3 $"; 46 private static final String RCSName = "$Name: $"; 47 48 private static final int BOR = 0; 49 private static final int BUFFER = 1; 50 private static final int RESULTSET = 2; 51 52 private ResultSet _resultset; 53 54 private String [] _buffer; 55 private Map _attributeMap = new HashMap (); 56 private ColumnInfo[] _attributes; 58 private boolean _bufferNotEmpty; 59 private boolean _resultSetNotEmpty; 60 private int _cursor; 61 static private ValidationContextProvider _validationContext = new ValidationContextProvider() { 62 public String getNamespaceURI(String prefix) { 63 return null; 64 } 65 public String getPrefix(String uri) { 66 return null; 67 } 68 public String getDocumentBase() { 69 return null; 70 } 71 public Map getNotationDeclarations() { 72 return null; 73 } 74 }; 75 private QAtomicSerializer qSerializer = new QAtomicSerializer(_validationContext); 76 77 public LookAheadJdbcResultSet(ResultSet resultset, Mapper mapper, DBMSInfo info) throws SQLException , XQueryException { 78 _resultset = resultset; 79 80 ResultSetMetaData rsm = _resultset.getMetaData(); 81 82 int columnCount = rsm.getColumnCount(); 83 _buffer = new String [columnCount]; 84 _attributes = new ColumnInfo[columnCount]; 85 String 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 , 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 getString(String name) throws SQLException { 178 String 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) 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 , XQueryException { 196 ExtractorMappingInfo mappingInfo = null; 197 198 for (int i = 0; i < _attributes.length; i++) { 199 mappingInfo = _attributes[i].getMappingInfo(); 200 if (mappingInfo == null) { _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 getStringFromBuffer(int index) { 209 String retVal = null; 210 if (index > 0) 211 retVal = _buffer[index - 1]; 212 return retVal; 213 } 214 215 216 public ResultSet 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 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 |