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.sql.PreparedStatement ; 37 import java.sql.ResultSet ; 38 import java.sql.SQLException ; 39 import java.sql.Types ; 40 41 44 public class PrimitiveIntType extends PrimitiveType { 45 private static final L10N L = new L10N(PrimitiveIntType.class); 46 47 private static final PrimitiveIntType INT_TYPE = new PrimitiveIntType(); 48 49 private PrimitiveIntType() 50 { 51 } 52 53 56 public static PrimitiveIntType create() 57 { 58 return INT_TYPE; 59 } 60 61 64 public String getName() 65 { 66 return "int"; 67 } 68 69 72 public boolean isNumeric() 73 { 74 return true; 75 } 76 77 80 public Type getForeignType() 81 { 82 return IntegerType.create(); 83 } 84 85 88 public String generateCreateColumnSQL(AmberPersistenceUnit manager, int length, int precision, int scale) 89 { 90 return manager.getCreateColumnSQL(Types.INTEGER, length, precision, scale); 91 } 92 93 96 public int generateLoad(JavaWriter out, String rs, 97 String indexVar, int index) 98 throws IOException 99 { 100 out.print(rs + ".getInt(" + indexVar + " + " + index + ")"); 101 102 return index + 1; 103 } 104 105 108 public int generateLoadForeign(JavaWriter out, String rs, 109 String indexVar, int index) 110 throws IOException 111 { 112 out.print("com.caucho.amber.type.PrimitiveIntType.toForeignInt(" + 113 rs + ".getInt(" + indexVar + " + " + index + "), " + 114 rs + ".wasNull())"); 115 116 return index + 1; 117 } 118 119 122 public void generateSet(JavaWriter out, String pstmt, 123 String index, String value) 124 throws IOException 125 { 126 out.println(pstmt + ".setInt(" + index + "++, " + value + ");"); 127 } 128 129 132 public void generateSetNull(JavaWriter out, String pstmt, String index) 133 throws IOException 134 { 135 out.println(pstmt + ".setNull(" + index + "++, java.sql.Types.INTEGER);"); 136 } 137 138 141 public void generateSetVersion(JavaWriter out, 142 String pstmt, 143 String index, 144 String value) 145 throws IOException 146 { 147 out.println(pstmt + ".setInt(" + index + "++, " + value + " + 1);"); 148 } 149 150 153 public String toObject(String value) 154 { 155 return "new Integer(" + value + ")"; 156 } 157 158 161 public String generateCastFromObject(String value) 162 { 163 return "((Number) " + value + ").intValue()"; 164 } 165 166 169 public static Integer toForeignInt(int value, boolean wasNull) 170 { 171 if (wasNull || value == 0) 173 return null; 174 else 175 return new Integer (value); 176 } 177 178 181 public Object getObject(ResultSet rs, int index) 182 throws SQLException 183 { 184 int v = rs.getInt(index); 185 186 return rs.wasNull() ? null : new Integer (v); 187 } 188 189 192 public Object toObject(long value) 193 { 194 return new Integer ((int) value); 195 } 196 197 200 public void setParameter(PreparedStatement pstmt, int index, Object value) 201 throws SQLException 202 { 203 if (value instanceof Number ) 204 pstmt.setString(index, value.toString()); 205 else 206 throw new IllegalArgumentException ("Invalid argument for setParameter."); 207 } 208 } 209 | Popular Tags |