KickJava   Java API By Example, From Geeks To Geeks.

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


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.SQLException JavaDoc;
27 import java.sql.Statement JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.List JavaDoc;
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 JavaDoc KEY_NAME = "key_name";
46
47     private static final String JavaDoc RCSRevision = "$Revision: 1.8 $";
48     private static final String JavaDoc 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 JavaDoc selections = null;
55     protected XMLResultSet _currentResultSet = null;
56     protected Statement JavaDoc _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     /**
76      * Access method for the _connection property.
77      * @return the current value of the _connection property
78      */

79     public XMLConnection getConnection() {
80         return _connection;
81     }
82
83     public Statement JavaDoc createJdbcStatement() throws SQLException JavaDoc {
84         Statement JavaDoc 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     /* XDBC API IMPLEMENTATION */
95     /*********************************************************************/
96     
97     public boolean execute(String JavaDoc query) throws XMLDBCException {
98         if (_currentResultSet != null)
99             _currentResultSet.close();
100         _currentResultSet = executeXQuery(query);
101         return true;
102     }
103
104     public boolean execute(String JavaDoc query, int queryType) throws XMLDBCException {
105         return execute(query);
106     }
107
108     public XMLResultSet executeQuery(String JavaDoc query) throws XMLDBCException {
109         execute(query);
110         return _currentResultSet;
111     }
112
113     public XMLResultSet executeQuery(String JavaDoc query, int queryType) throws XMLDBCException {
114         return executeQuery(query);
115     }
116
117     public XMLDocumentSet executeDocumentQuery(String JavaDoc query) throws XMLDBCException {
118         throw new XMLDBCNotSupportedException("executeDocumentQuery not yet supported");
119     }
120
121     // Utility for LS
122
public XQueryModule parseXQuery(String JavaDoc 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 JavaDoc baseURI) { _queryFactory.setBaseURI(baseURI);}
135
136     public String JavaDoc 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 JavaDoc e) { /* no op */ }
152             }
153             _connection.statementClosed(this);
154             _isClosed = true;
155         }
156     }
157
158     public boolean isClosed() throws XMLDBCException {
159         return _isClosed;
160     }
161
162     /*********************************************************************/
163     /* INTERNAL QUERY EXECUTION */
164     /*********************************************************************/
165     
166     /**
167      * Execute the Xquery request and return an XML document as result.
168      *
169      * @param xquery the query to execute.
170      * @return the XML result document.
171      */

172     protected XMLResultSet executeXQuery(String JavaDoc 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 JavaDoc 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 JavaDoc childResultSetList = new ArrayList JavaDoc();
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 JavaDoc 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         // create a statement for every Qdb (one current ResultSet per statement in JDBC)
244
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 JavaDoc 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 JavaDoc ex) {}
266         }
267
268         return retVal;
269     }
270
271     protected void finalize() throws Exception JavaDoc {
272         close();
273     }
274 }
275
Popular Tags