1 22 23 package org.xquark.extractor.algebra; 24 25 import java.math.BigDecimal ; 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 RCSRevision = "$Revision: 1.5 $"; 36 private static final String 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 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 value = sqlResultSet.getObject(1); 76 if (null == value) { 77 litValue = new LitNull(); 78 } 79 else if (value instanceof Integer ) { 80 litValue = new LitInteger((Integer )value); 81 } 82 else if (value instanceof BigDecimal ) { 83 litValue = new LitDecimal((BigDecimal )value); 84 } 85 else if (value instanceof Float ) { 86 litValue = new LitFloat((Float )value); 87 } 88 else if (value instanceof Double ) { 89 litValue = new LitDouble((Double )value); 90 } 91 else if (value instanceof String ) { 92 litValue = new LitString((String )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 |