KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > mysql > MysqlQueryFactory


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.mysql;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.xquark.extractor.algebra.*;
29 import org.xquark.extractor.metadata.MetaDataManager;
30 import org.xquark.extractor.runtime.QueryFactory;
31 import org.xquark.extractor.runtime.Query;
32 import org.xquark.extractor.runtime.SynchronizedResultSet;
33 import org.xquark.extractor.sql.SqlExpression;
34 import org.xquark.xml.xdbc.XMLDBCException;
35 import org.xquark.xquery.metadata.resolver.MetadataAccess;
36 import org.xquark.xquery.parser.XQueryException;
37 import org.xquark.xquery.parser.XQueryExpression;
38
39 public class MysqlQueryFactory extends QueryFactory {
40     private static final String JavaDoc RCSRevision = "$Revision: 1.6 $";
41     private static final String JavaDoc RCSName = "$Name: $";
42
43     private PreEvaluationVisitor _pev;
44     private MysqlRemoveSubQueries _rsv;
45
46     protected TempTableIDGenerator _tmpTableIDProvider;
47     // NOTE: do not set to null since init is performed by super constructor
48

49     public MysqlQueryFactory(MetaDataManager metadataManager, MetadataAccess metadata) throws XMLDBCException {
50         super(metadataManager, metadata);
51     }
52
53     protected void createVisitors() {
54         TypeInterpreter ti = new TypeInterpreter();
55         _genAlgebraVisitor = new GenAlgebraVisitor(ti, _metadataManager, _attIDProvider, _relIDProvider);
56
57         _algebraTypeVisitor = new AlgebraTypeVisitor();
58         _removeProjectVisitor = new RemoveIntermediateProjectVisitor();
59         _brav = new BuildReferredAttributes();
60         _apv = new AnalyzePredicateVisitor();
61
62         _tmpTableIDProvider = new TempTableIDGenerator();
63         _genSqlVisitor = new org.xquark.extractor.mysql.MySqlGenSqlVisitor(_relIDProvider, _tmpTableIDProvider);
64         _genSqlVisitor.setMetadataManager(_metadataManager);
65
66         _pev = new PreEvaluationVisitor();
67         _rsv = new MysqlRemoveSubQueries(_attIDProvider, _relIDProvider);
68     }
69
70     public Query.CompiledExpression compileQuery(XQueryExpression xExpr) throws XMLDBCException {
71         _tmpTableIDProvider.reset();
72         return super.compileQuery(xExpr);
73     }
74
75     protected Query.SQLExecutionInfo compileSingleQDB(XQueryExpression xQuery, boolean keyNeeded)
76     throws XQueryException, XMLDBCException {
77
78         SynchronizedResultSet retVal = null;
79         Expression algebraTree = null;
80         Mapper mapper = null;
81
82         /* Reset counters */
83         _attIDProvider.reset();
84         _relIDProvider.reset();
85
86         /* Algebra tree building */
87         _genAlgebraVisitor.reinit();
88
89         if (keyNeeded) { // Used when Sorted Outer Union is needed to identify rows
90
List JavaDoc souArgList = new ArrayList JavaDoc();
91             souArgList.add(xQuery);
92             _genAlgebraVisitor.noticSortedOuterUnion(souArgList);
93         }
94         xQuery.accept(_genAlgebraVisitor);
95         algebraTree = _genAlgebraVisitor.getAlgebraTree();
96
97         // Clean useless cascaded projection that are systematically generated
98
// because of Qdb intermediate let clauses
99
_removeProjectVisitor.reinit();
100         algebraTree.accept(_removeProjectVisitor);
101
102         // Browse algebra tree to set the _refferedAttributes variable on nodes
103
algebraTree.accept(_brav);
104
105         /* resolve invalid predicates */
106         _apv.reinit();
107         algebraTree.accept(_apv);
108
109         /* Transforms a SELECT or WHERE with aggregation in the select clause into an outer join */
110         _rsv.reinit(algebraTree);
111         algebraTree.accept(_rsv);
112         algebraTree = _rsv.getRewrittenQuery(); // root can be changed
113

114         // TODO: check if batchs could not be used when multiple statements are issued
115

116         /* Sets the type field for every tree node */
117         _algebraTypeVisitor.reinit();
118         algebraTree.accept(_algebraTypeVisitor);
119
120         mapper = algebraTree.getMapper();
121
122         /* Generates sql from semantically supported algebra */
123         _genSqlVisitor.reinit();
124         SqlExpression sql = algebraTree.accept(_genSqlVisitor);
125
126         /* Generate string to be ready for prepared statements execution */
127         sql.generateRequestList();
128         return new Query.SQLExecutionInfo(sql, algebraTree.getMapper());
129     }
130 }
131
Popular Tags