KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > progress > ProgressQueryFactory


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.progress;
24
25 import java.sql.SQLException JavaDoc;
26 import java.sql.Statement JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29
30 import org.xquark.extractor.algebra.*;
31 import org.xquark.extractor.metadata.MetaDataManager;
32 import org.xquark.extractor.runtime.QueryFactory;
33 import org.xquark.extractor.runtime.Query;
34 import org.xquark.extractor.runtime.SynchronizedResultSet;
35 import org.xquark.extractor.sql.SqlExpression;
36 import org.xquark.xml.xdbc.XMLDBCException;
37 import org.xquark.xquery.metadata.resolver.MetadataAccess;
38 import org.xquark.xquery.parser.XQueryException;
39 import org.xquark.xquery.parser.XQueryExpression;
40
41 public class ProgressQueryFactory extends QueryFactory {
42     private static final String JavaDoc RCSRevision = "$Revision: 1.5 $";
43     private static final String JavaDoc RCSName = "$Name: $";
44
45     private PreEvaluationVisitor _pev;
46
47     public ProgressQueryFactory(MetaDataManager metadataManager, MetadataAccess metadata) throws XMLDBCException {
48         super(metadataManager, metadata);
49     }
50
51     protected void createVisitors() {
52         TypeInterpreter ti = new TypeInterpreter();
53         _genAlgebraVisitor = new GenAlgebraVisitor(ti, _metadataManager, _attIDProvider, _relIDProvider);
54
55         _algebraTypeVisitor = new AlgebraTypeVisitor();
56         _removeProjectVisitor = new RemoveIntermediateProjectVisitor();
57         _brav = new BuildReferredAttributes();
58         _apv = new AnalyzePredicateVisitor();
59
60         _genSqlVisitor = new org.xquark.extractor.progress.ProgressGenSqlVisitor(_relIDProvider);
61         _genSqlVisitor.setMetadataManager(_metadataManager);
62
63         _pev = new PreEvaluationVisitor();
64     }
65
66     protected Query.SQLExecutionInfo compileSingleQDB(XQueryExpression xQuery, boolean keyNeeded)
67     throws XQueryException, XMLDBCException {
68         //Trace.enter(this, "executeSingleQDB(XQueryExpression xQuery, boolean keyNeeded)");
69

70         SynchronizedResultSet retVal = null;
71         Expression algebraTree = null;
72         Mapper mapper = null;
73
74         /* Reset counters */
75         _attIDProvider.reset();
76         _relIDProvider.reset();
77
78         /* Algebra tree building */
79         _genAlgebraVisitor.reinit();
80
81         if (keyNeeded) { // Used when Sorted Outer Union is needed to identify rows
82
List JavaDoc souArgList = new ArrayList JavaDoc();
83             souArgList.add(xQuery);
84             _genAlgebraVisitor.noticSortedOuterUnion(souArgList);
85         }
86         xQuery.accept(_genAlgebraVisitor);
87         algebraTree = _genAlgebraVisitor.getAlgebraTree();
88
89         // INFO : YS types of visitors : GenAlgebraVisitor (a ParserVisitor impl),
90
// AlgebraVisitor, GenSQLVisitor
91
_removeProjectVisitor.reinit();
92         algebraTree.accept(_removeProjectVisitor);
93
94         algebraTree.accept(_brav);
95
96         /* resolve invalid predicates */
97         _apv.reinit();
98         algebraTree.accept(_apv);
99
100         /* Transforms a select with aggregation in the select clause into an outer
101          * join on the first table and a join between the first and the aggregation
102          * table (maybe because Progress does not support group by in OJ ?)
103          */

104         algebraTree.accept(new RemoveNestedAggregation(_attIDProvider, _relIDProvider));
105
106         _algebraTypeVisitor.reinit();
107         algebraTree.accept(_algebraTypeVisitor);
108
109         _pev.setGenSqlVisitor(_genSqlVisitor);
110         try { // TODO: why temp tables should be created here ?
111
Statement JavaDoc jdbcStatement = _stmt.createJdbcStatement();
112             _pev.setJdbcStatement(jdbcStatement);
113             jdbcStatement.close();
114         }
115         catch (SQLException JavaDoc ex) {
116             throw new XMLDBCException(ex.getMessage(), ex);
117         }
118         algebraTree.accept(_pev);
119
120         mapper = algebraTree.getMapper();
121
122         _genSqlVisitor.reinit();
123         SqlExpression sql = algebraTree.accept(_genSqlVisitor);
124
125         /* Generate string to be ready for prepared statements execution */
126         sql.generateRequestList();
127         return new Query.SQLExecutionInfo(sql, algebraTree.getMapper());
128     }
129 }
130
Popular Tags