1 28 29 package com.caucho.amber.type; 30 31 import com.caucho.amber.manager.AmberPersistenceUnit; 32 import com.caucho.java.JavaWriter; 33 import com.caucho.util.L10N; 34 35 import java.io.IOException ; 36 import java.math.BigDecimal ; 37 import java.math.BigInteger ; 38 import java.sql.PreparedStatement ; 39 import java.sql.ResultSet ; 40 import java.sql.SQLException ; 41 import java.sql.Types ; 42 43 46 public class BigIntegerType extends Type { 47 private static final L10N L = new L10N(BigIntegerType.class); 48 49 private BigIntegerType() 50 { 51 } 52 53 56 public static BigIntegerType create() 57 { 58 return new BigIntegerType(); 59 } 60 61 64 public String getName() 65 { 66 return "java.math.BigInteger"; 67 } 68 69 72 public boolean isNumeric() 73 { 74 return true; 75 } 76 77 80 public int generateLoad(JavaWriter out, String rs, 81 String indexVar, int index) 82 throws IOException 83 { 84 out.print(rs + ".getBigDecimal(" + indexVar + " + " + index + ")"); 85 out.print(" == null || " + rs + ".wasNull() ? null : "); 86 out.print(rs + ".getBigDecimal(" + indexVar + " + " + index + ").toBigInteger()"); 87 88 return index + 1; 89 } 90 91 94 public void generateSet(JavaWriter out, String pstmt, 95 String index, String value) 96 throws IOException 97 { 98 out.println("if (" + value + " == null)"); 99 out.println(" " + pstmt + ".setNull(" + index + "++, java.sql.Types.DECIMAL);"); 100 out.println("else"); 101 out.println(" " + pstmt + ".setBigDecimal(" + index + "++, new java.math.BigDecimal(" + value + "));"); 102 } 103 104 107 public void setParameter(PreparedStatement pstmt, int index, Object value) 108 throws SQLException 109 { 110 if (value == null) 111 pstmt.setNull(index, java.sql.Types.DECIMAL); 112 else 113 pstmt.setBigDecimal(index, new BigDecimal ((BigInteger ) value)); 114 } 115 116 119 public Object getObject(ResultSet rs, int index) 120 throws SQLException 121 { 122 BigDecimal v = rs.getBigDecimal(index); 123 124 if (rs.wasNull()) 125 return null; 126 127 return v.toBigInteger(); 128 } 129 130 public String generateCreateColumnSQL(AmberPersistenceUnit manager, 131 int length, 132 int precision, 133 int scale) 134 { 135 return manager.getCreateColumnSQL(Types.NUMERIC, length, precision, scale); 136 } 137 } 138 | Popular Tags |