KickJava   Java API By Example, From Geeks To Geeks.

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


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.mysql.sql.SqlCreateTempTableAs;
30 import org.xquark.extractor.runtime.IDProvider;
31 import org.xquark.extractor.sql.*;
32
33 public final class MySqlGenSqlVisitor extends AbstractGenSqlVisitor {
34
35     private static final String JavaDoc RCSRevision = "$Revision: 1.7 $";
36     private static final String JavaDoc RCSName = "$Name: $";
37
38     private IDProvider _tmpTableIDProvider;
39
40     public MySqlGenSqlVisitor(IDProvider relIDProvider, IDProvider tmpTableIDProvider) {
41         super((SqlFactory) MySqlFactory.getInstance(), relIDProvider);
42         _tmpTableIDProvider = tmpTableIDProvider;
43     }
44
45     public void reinit() {
46         super.reinit();
47     }
48
49     public SqlExpression visit(UnOpAggregate arg) {
50         //Trace.enter(this, "visit(UnOpAggregate arg)");
51

52         SqlExpression retVal = visit((UnOpProject) arg);
53
54         //Trace.exit(this, "visit(UnOpAggregate arg)");
55
return retVal;
56     }
57
58     // TODO: generate also subquery for sorts...
59

60     public SqlExpression visit(UnOpProject arg) {
61         //Trace.enter(this, "visit(UnOpProject arg)");
62
SqlExpression retVal;
63
64         SqlSelect outerExpression = null;
65         if (!_nestStack.isEmpty())
66             outerExpression = (SqlSelect) _nestStack.firstElement();
67
68         SqlSelect select = _factory.createSelect(arg);
69         _nestStack.push(select);
70
71         retVal = selectAddSelectClause(select, arg); // After potential attributes renaming
72

73         /* if _nestStack not empty (subquery) create a temporary table (supposed
74          * to remain only in FROMs */

75         if (outerExpression != null) {
76             String JavaDoc tempTableName = getTemporaryTableName();
77             SqlCreateTempTableAs sCreateTempTable = new SqlCreateTempTableAs(tempTableName, select);
78             outerExpression.appendPrecedent(sCreateTempTable);
79             retVal = _factory.createTable(tempTableName);
80         }
81         _nestStack.pop();
82
83         //Trace.exit(this, "visit(UnOpProject arg)");
84
return retVal;
85     }
86
87     protected SqlSelect selectAddFromClause(SqlSelect select, BinOpOuterJoin arg) {
88         //Trace.enter(this, "selectAddFromClause (SqlSelect select, BinOpOuterJoin arg)", arg);
89

90         Expression[] relations = { arg.getLeftOperand(), arg.getRightOperand()};
91         SqlExpression[] sOprnd = new SqlExpression[2];
92
93         for (int i = 0; i < relations.length; i++) {
94             // NOTE: a list of if (instanceof) used to check algebra integrity have
95
// been removed because these tests make visitor automatic type switch worthless...
96
sOprnd[i] = relations[i].accept(this);
97         }
98
99         /* Outer join predicate creation */
100         List JavaDoc aPredicateList = arg.getPredicateList();
101         List JavaDoc sPredicateList = null;
102         if (null != aPredicateList) {
103             /* translate predicates */
104             sPredicateList = new ArrayList JavaDoc();
105             Expression aPredicate = null;
106             org.xquark.extractor.sql.SqlExpression sPredicate = null;
107
108             for (int i = 0; i < aPredicateList.size(); i++) {
109                 aPredicate = (Expression) aPredicateList.get(i);
110                 sPredicate = aPredicate.accept(this);
111                 sPredicateList.add(sPredicate);
112             }
113         }
114
115         /* Outer join clause creation */
116         org.xquark.extractor.sql.SqlExpression retVal = new org.xquark.extractor.sql.SqlBinOpOuterJoin(sOprnd[0], sOprnd[1], arg.getJoinType(), sPredicateList);
117
118         select.addFromClause(retVal);
119
120         //Trace.exit(this, "selectAddFromClause (SqlSelect select, BinOpOuterJoin arg)", arg);
121
return select;
122     }
123
124     protected String JavaDoc getTemporaryTableName() {
125         return TMP_TABLE_PREFIX + _tmpTableIDProvider.getID();
126     }
127 }
128
Popular Tags