KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > xquark > extractor > algebra > PreEvaluationVisitor


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.algebra;
24
25 import java.math.BigDecimal JavaDoc;
26 import java.sql.*;
27
28 import org.xquark.extractor.common.Debug;
29 import org.xquark.extractor.common.SqlWrapperException;
30 import org.xquark.extractor.sql.AbstractGenSqlVisitor;
31 import org.xquark.extractor.sql.SqlExpression;
32
33 public class PreEvaluationVisitor extends DefaultCompleteVisitor {
34
35     private static final String JavaDoc RCSRevision = "$Revision: 1.5 $";
36     private static final String JavaDoc RCSName = "$Name: $";
37
38
39     AbstractGenSqlVisitor _genSqlVisitor = null;
40     Statement _jdbcStatement = null;
41
42     public PreEvaluationVisitor() {
43     }
44
45     public PreEvaluationVisitor(AbstractGenSqlVisitor genSqlVisitor, Statement jdbcStatement) {
46         setGenSqlVisitor(genSqlVisitor);
47         setJdbcStatement(jdbcStatement);
48     }
49
50     public void setGenSqlVisitor(AbstractGenSqlVisitor genSqlVisitor) {
51         _genSqlVisitor = genSqlVisitor;
52     }
53
54     public void setJdbcStatement(Statement jdbcStatement) {
55         _jdbcStatement = jdbcStatement;
56     }
57
58     public void visit(TempValue arg) throws SqlWrapperException {
59         super.visit(arg);
60
61         if (null == arg.getValue()) {
62             /* evaluate this subtree*/
63             try {
64                 Expression subTree = arg.getOperand();
65                 SqlTypeAtom sqlType = (SqlTypeAtom)subTree.getType();
66
67                 _genSqlVisitor.reinit();
68                 SqlExpression sql = subTree.accept(_genSqlVisitor);
69                 ResultSet sqlResultSet = sql.Execute(_jdbcStatement);
70
71                 sqlResultSet.next();
72
73                 Literal litValue = null;
74
75                 Object JavaDoc value = sqlResultSet.getObject(1);
76                 if (null == value) {
77                     litValue = new LitNull();
78                 }
79                 else if (value instanceof Integer JavaDoc) {
80                     litValue = new LitInteger((Integer JavaDoc)value);
81                 }
82                 else if (value instanceof BigDecimal JavaDoc) {
83                     litValue = new LitDecimal((BigDecimal JavaDoc)value);
84                 }
85                 else if (value instanceof Float JavaDoc) {
86                     litValue = new LitFloat((Float JavaDoc)value);
87                 }
88                 else if (value instanceof Double JavaDoc) {
89                     litValue = new LitDouble((Double JavaDoc)value);
90                 }
91                 else if (value instanceof String JavaDoc) {
92                     litValue = new LitString((String JavaDoc)value);
93                 }
94                 else if (value instanceof Date) {
95                     litValue = new LitDate((Date)value);
96                 }
97                 else {
98                     Debug.nyi("creating a literal for " + value.getClass().getName());
99                 }
100
101                 arg.setValue(litValue);
102
103                 sqlResultSet.close();
104             }
105             catch (SQLException ex) {
106                 throw new SqlWrapperException(ex.getMessage(), ex);
107             }
108         }
109     }
110 }
111
Popular Tags