1 10 11 package com.triactive.jdo.store; 12 13 import com.triactive.jdo.PersistenceManager; 14 import java.math.BigInteger ; 15 import java.sql.PreparedStatement ; 16 import java.sql.ResultSet ; 17 import java.sql.SQLException ; 18 import java.sql.Types ; 19 import javax.jdo.JDODataStoreException; 20 21 22 public class IntegerMapping extends ColumnMapping 23 { 24 private static final int INT_MAX_DECIMAL_DIGITS = 10; 25 26 public IntegerMapping(DatabaseAdapter dba, Class type) 27 { 28 super(dba, type); 29 30 initTypeInfo(); 31 } 32 33 public IntegerMapping(Column col) 34 { 35 super(col); 36 37 col.checkPrimitive(); 38 39 initTypeInfo(); 40 } 41 42 public IntegerMapping(ClassBaseTable table, int relativeFieldNumber) 43 { 44 this(table.newColumn(relativeFieldNumber)); 45 } 46 47 protected TypeInfo getTypeInfo() 48 { 49 return dba.getTypeInfo(new int[] { Types.INTEGER, Types.DECIMAL }); 50 } 51 52 protected void initTypeInfo() 53 { 54 super.initTypeInfo(); 55 56 60 if (col != null && typeInfo.dataType == Types.DECIMAL) 61 { 62 col.setMinimumPrecision(INT_MAX_DECIMAL_DIGITS); 63 col.checkDecimal(); 64 } 65 } 66 67 public void setInt(PersistenceManager pm, PreparedStatement ps, int param, int value) 68 { 69 try 70 { 71 ps.setInt(param, value); 72 } 73 catch (SQLException e) 74 { 75 throw dba.newDataStoreException("Can't set int parameter: value = " + value, e); 76 } 77 } 78 79 public int getInt(PersistenceManager pm, ResultSet rs, int param) 80 { 81 int value; 82 83 try 84 { 85 value = rs.getInt(param); 86 87 if (rs.wasNull()) 88 throw new NullValueException("Illegal null value in column " + col); 89 } 90 catch (SQLException e) 91 { 92 throw dba.newDataStoreException("Can't get int result: param = " + param, e); 93 } 94 95 return value; 96 } 97 98 public void setObject(PersistenceManager pm, PreparedStatement ps, int param, Object value) 99 { 100 try 101 { 102 if (value == null) 103 ps.setNull(param, typeInfo.dataType); 104 else 105 ps.setInt(param, ((Integer )value).intValue()); 106 } 107 catch (SQLException e) 108 { 109 throw dba.newDataStoreException("Can't set Integer parameter: value = " + value, e); 110 } 111 } 112 113 public Object getObject(PersistenceManager pm, ResultSet rs, int param) 114 { 115 Object value; 116 117 try 118 { 119 int i = rs.getInt(param); 120 value = rs.wasNull() ? null : new Integer (i); 121 } 122 catch (SQLException e) 123 { 124 throw dba.newDataStoreException("Can't get Integer result: param = " + param, e); 125 } 126 127 return value; 128 } 129 130 public SQLExpression newSQLLiteral(QueryStatement qs, Object value) 131 { 132 return new IntegerLiteral(qs, BigInteger.valueOf(((Integer )value).longValue())); 133 } 134 135 public SQLExpression newSQLExpression(QueryStatement qs, QueryStatement.QueryColumn qsc, String fieldName) 136 { 137 return new NumericExpression(qs, qsc); 138 } 139 } 140 | Popular Tags |