1 29 30 package com.caucho.amber.type; 31 32 import com.caucho.amber.manager.AmberPersistenceUnit; 33 import com.caucho.java.JavaWriter; 34 import com.caucho.util.L10N; 35 36 import java.io.IOException ; 37 import java.sql.PreparedStatement ; 38 import java.sql.ResultSet ; 39 import java.sql.SQLException ; 40 import java.sql.Types ; 41 42 45 public class DoubleType extends Type { 46 private static final L10N L = new L10N(DoubleType.class); 47 48 private static final DoubleType DOUBLE_TYPE = new DoubleType(); 49 50 private DoubleType() 51 { 52 } 53 54 57 public static DoubleType create() 58 { 59 return DOUBLE_TYPE; 60 } 61 62 65 public String getName() 66 { 67 return "java.lang.Double"; 68 } 69 70 73 public boolean isNumeric() 74 { 75 return true; 76 } 77 78 81 public String generateCreateColumnSQL(AmberPersistenceUnit manager, int length, int precision, int scale) 82 { 83 return manager.getCreateColumnSQL(Types.DOUBLE, length, precision , scale); 84 } 85 86 89 public int generateLoad(JavaWriter out, String rs, 90 String indexVar, int index) 91 throws IOException 92 { 93 out.print("com.caucho.amber.type.DoubleType.toDouble(" + 94 rs + ".getDouble(" + indexVar + " + " + index + "), " + 95 rs + ".wasNull())"); 96 97 return index + 1; 98 } 99 100 103 public void generateSet(JavaWriter out, String pstmt, 104 String index, String value) 105 throws IOException 106 { 107 out.println("if (" + value + " == null)"); 108 out.println(" " + pstmt + ".setNull(" + index + "++, java.sql.Types.DOUBLE);"); 109 out.println("else"); 110 out.println(" " + pstmt + ".setDouble(" + index + "++, " + 111 value + ".doubleValue());"); 112 } 113 114 117 public void generateSetNull(JavaWriter out, String pstmt, 118 String index) 119 throws IOException 120 { 121 out.println(pstmt + ".setNull(" + index + "++, java.sql.Types.DOUBLE);"); 122 } 123 124 127 public static Double toDouble(double value, boolean wasNull) 128 { 129 if (wasNull) 130 return null; 131 else 132 return new Double (value); 133 } 134 135 138 public void setParameter(PreparedStatement pstmt, int index, Object value) 139 throws SQLException 140 { 141 if (value == null) 142 pstmt.setNull(index, Types.DOUBLE); 143 else 144 pstmt.setDouble(index, ((Double ) value).doubleValue()); 145 } 146 147 150 public Object getObject(ResultSet rs, int index) 151 throws SQLException 152 { 153 double value = rs.getDouble(index); 154 155 return rs.wasNull() ? null : new Double (value); 156 } 157 } 158 | Popular Tags |