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 FloatType extends Type { 46 private static final L10N L = new L10N(FloatType.class); 47 48 private static final FloatType FLOAT_TYPE = new FloatType(); 49 50 private FloatType() 51 { 52 } 53 54 57 public static FloatType create() 58 { 59 return FLOAT_TYPE; 60 } 61 62 65 public String getName() 66 { 67 return "java.lang.Float"; 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.REAL, 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.FloatType.toFloat(" + 94 rs + ".getFloat(" + 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.REAL);"); 109 out.println("else"); 110 out.println(" " + pstmt + ".setString(" + index + "++, " + 114 value + ".toString());"); 115 } 116 117 120 public void generateSetNull(JavaWriter out, String pstmt, 121 String index) 122 throws IOException 123 { 124 out.println(pstmt + ".setNull(" + index + "++, java.sql.Types.REAL);"); 125 } 126 127 130 public static Float toFloat(float value, boolean wasNull) 131 { 132 if (wasNull) 133 return null; 134 else 135 return new Float (value); 136 } 137 138 141 public Object getObject(ResultSet rs, int index) 142 throws SQLException 143 { 144 float value = rs.getFloat(index); 145 146 return rs.wasNull() ? null : new Float (value); 147 } 148 149 152 public void setParameter(PreparedStatement pstmt, int index, Object value) 153 throws SQLException 154 { 155 if (value == null) 156 pstmt.setNull(index, Types.FLOAT); 157 else 158 pstmt.setFloat(index, ((Float ) value).floatValue()); 159 } 160 } 161 | Popular Tags |