1 22 23 package org.xquark.extractor.runtime; 24 25 import java.sql.ResultSet ; 26 import java.sql.SQLException ; 27 import java.sql.Statement ; 28 import java.util.ArrayList ; 29 import java.util.HashMap ; 30 import java.util.List ; 31 32 import org.apache.commons.logging.Log; 33 import org.apache.commons.logging.LogFactory; 34 import org.xquark.extractor.ExtractorConnection; 35 import org.xquark.extractor.common.MessageLibrary; 36 import org.xquark.extractor.metadata.MetaDataManager; 37 import org.xquark.extractor.sql.StatementInfo; 38 import org.xquark.xml.xdbc.*; 39 import org.xquark.xquery.parser.XQueryException; 40 import org.xquark.xquery.parser.XQueryModule; 41 import org.xquark.xquery.xdbc.XDBCResultSetInterface; 42 import org.xquark.xquery.xdbc.XResultSetImpl; 43 44 public class ExtractorStatement implements XMLStatementInternal { 45 final static String KEY_NAME = "key_name"; 46 47 private static final String RCSRevision = "$Revision: 1.8 $"; 48 private static final String RCSName = "$Name: $"; 49 50 private static Log log = LogFactory.getLog(ExtractorStatement.class); 51 52 protected ExtractorConnection _connection; 53 protected MetaDataManager _metadataManager = null; 54 protected HashMap selections = null; 55 protected XMLResultSet _currentResultSet = null; 56 protected Statement _stmt = null; 57 protected boolean _hasResultSet = false; 58 protected boolean _isClosed = false; 59 protected int _id; 60 protected QueryFactory _queryFactory; 61 62 public ExtractorStatement(ExtractorConnection connection, 63 int stmtId, 64 QueryFactory queryFactory, 65 MetaDataManager metadataManager 66 ) 67 throws XMLDBCException { 68 _connection = connection; 69 _metadataManager = metadataManager; 70 _id = stmtId; 71 _queryFactory = queryFactory; 72 _queryFactory.setStatement(this); 73 } 74 75 79 public XMLConnection getConnection() { 80 return _connection; 81 } 82 83 public Statement createJdbcStatement() throws SQLException { 84 Statement retVal = null; 85 if (_connection.getExtractor().useScrollableResultSets()) 86 retVal = _connection.getJdbcConnection().createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); 87 else 88 retVal = _connection.getJdbcConnection().createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); 89 90 return retVal; 91 } 92 93 94 95 96 97 public boolean execute(String query) throws XMLDBCException { 98 if (_currentResultSet != null) 99 _currentResultSet.close(); 100 _currentResultSet = executeXQuery(query); 101 return true; 102 } 103 104 public boolean execute(String query, int queryType) throws XMLDBCException { 105 return execute(query); 106 } 107 108 public XMLResultSet executeQuery(String query) throws XMLDBCException { 109 execute(query); 110 return _currentResultSet; 111 } 112 113 public XMLResultSet executeQuery(String query, int queryType) throws XMLDBCException { 114 return executeQuery(query); 115 } 116 117 public XMLDocumentSet executeDocumentQuery(String query) throws XMLDBCException { 118 throw new XMLDBCNotSupportedException("executeDocumentQuery not yet supported"); 119 } 120 121 public XQueryModule parseXQuery(String xquery) throws XMLDBCException { 123 return _queryFactory.parseXQuery(xquery); 124 } 125 126 public XMLResultSet getResultSet() throws XMLDBCException { 127 return _currentResultSet; 128 } 129 130 public XMLDocumentSet getDocumentSet() throws XMLDBCException { 131 throw new XMLDBCNotSupportedException("executeDocumentQuery not yet supported"); 132 } 133 134 public void setBaseURI(String baseURI) { _queryFactory.setBaseURI(baseURI);} 135 136 public String getBaseURI() { return _queryFactory.getBaseURI();} 137 138 public int getStatementId() { return _id;} 139 140 public void close() throws XMLDBCException { 141 if (!_isClosed) { 142 _metadataManager = null; 143 selections = null; 144 if (null != _currentResultSet) { 145 _currentResultSet.close(); 146 _currentResultSet = null; 147 } 148 if (_stmt != null) { 149 try { 150 _stmt.close(); 151 } catch (SQLException e) { } 152 } 153 _connection.statementClosed(this); 154 _isClosed = true; 155 } 156 } 157 158 public boolean isClosed() throws XMLDBCException { 159 return _isClosed; 160 } 161 162 163 164 165 166 172 protected XMLResultSet executeXQuery(String xquery) throws XMLDBCException { 173 if (isClosed()) 174 throw new XMLDBCException(MessageLibrary.getMessage("ST_CLOSED")); 175 176 return runQuery(_queryFactory.createCompiledQuery(xquery).getCompiledQuery()); 177 } 178 179 protected XMLResultSet runQuery(List compiledQuery) throws XMLDBCException { 180 181 XMLResultSet retVal = null; 182 183 if (compiledQuery.size() == 1) 184 retVal = runQuery((Query.CompiledExpression) compiledQuery.get(0)); 185 else if (compiledQuery.size()> 1) 186 retVal = new XResultSetUnion(compiledQuery, this); 187 188 return retVal; 189 } 190 191 public XMLResultSet runQuery(Query.CompiledExpression cExpr) throws XMLDBCException { 192 try { 193 return new XResultSetImpl( 194 cExpr.sqlInfoTree == null ? null : runQueryTree(cExpr.sqlInfoTree), 195 cExpr.expr.getQMEM(), 196 cExpr.expr.getIdVarList(), 197 this 198 ); 199 } 200 catch (XQueryException e) { 201 throw new XMLDBCException(e.getMessage(), e); 202 } 203 } 204 205 private XDBCResultSetInterface runQueryTree(Query.SQLExecutionInfo jdbcInfo) 206 throws XMLDBCException, XQueryException { 207 XDBCResultSetInterface rs = null; 208 209 List childResultSetList = new ArrayList (); 210 if (jdbcInfo.childrenQueries != null) 211 for (int i = 0; i < jdbcInfo.childrenQueries.size(); i++) { 212 childResultSetList.add(runQueryTree((Query.SQLExecutionInfo) jdbcInfo.childrenQueries.get(i))); 213 } 214 215 if (jdbcInfo.isUnion()) 216 rs = new UnionResultSet(childResultSetList); 217 else { 218 rs = executeSQL(jdbcInfo); 219 SynchronizedResultSet srs = (SynchronizedResultSet) rs; 220 srs.setSlaveList(childResultSetList); 221 if (jdbcInfo.idCount >= 0) 222 srs.setReconstructionIdCount(jdbcInfo.idCount); 223 224 if (jdbcInfo.idPosition != null) 225 srs.setIdPosition(jdbcInfo.idPosition); 226 } 227 return rs; 228 } 229 230 protected SynchronizedResultSet executeSQL(Query.SQLExecutionInfo sqlInfo) 231 throws XQueryException, XMLDBCException { 232 233 List requestList = sqlInfo.sql.getRequestList(); 234 235 if (log.isDebugEnabled()) { 236 log.debug("=================< SQL >================="); 237 for (int i = 0; i < requestList.size(); i++) 238 log.debug(((StatementInfo)requestList.get(i)).sStmt); 239 log.debug("=================< MAPPER >================="); 240 log.debug(sqlInfo.mapper.pprint()); 241 } 242 243 SynchronizedResultSet retVal = null; 245 try { 246 retVal = new SynchronizedResultSet( 247 requestList, 248 sqlInfo.mapper, 249 createJdbcStatement(), 250 _metadataManager.getDbmsInfo() 251 ); 252 } 253 catch (XMLDBCException ex) { 254 throw ex; 255 } 256 catch (SQLException ex) { 257 throw new XMLDBCException(ex.getMessage(), ex); 258 } 259 finally { 260 try { 261 if (_stmt == null) 262 _stmt = createJdbcStatement(); 263 sqlInfo.sql.cleanAll(_stmt); 264 } 265 catch (SQLException ex) {} 266 } 267 268 return retVal; 269 } 270 271 protected void finalize() throws Exception { 272 close(); 273 } 274 } 275 | Popular Tags |