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 PrimitiveShortType extends PrimitiveType { 45 private static final L10N L = new L10N(PrimitiveShortType.class); 46 47 private static final PrimitiveShortType SHORT_TYPE = new PrimitiveShortType(); 48 49 private PrimitiveShortType() 50 { 51 } 52 53 56 public static PrimitiveShortType create() 57 { 58 return SHORT_TYPE; 59 } 60 61 64 public String getName() 65 { 66 return "short"; 67 } 68 69 72 public boolean isNumeric() 73 { 74 return true; 75 } 76 77 80 public Type getForeignType() 81 { 82 return ShortType.create(); 83 } 84 85 88 public String generateCreateColumnSQL(AmberPersistenceUnit manager, int length, int precision, int scale) 89 { 90 return manager.getCreateColumnSQL(Types.SMALLINT, 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 + ".getShort(" + indexVar + " + " + index + ")"); 101 102 return index + 1; 103 } 104 105 108 public void generateSet(JavaWriter out, String pstmt, 109 String index, String value) 110 throws IOException 111 { 112 out.println(pstmt + ".setShort(" + index + "++, " + value + ");"); 113 } 114 115 118 public void generateSetNull(JavaWriter out, String pstmt, String index) 119 throws IOException 120 { 121 out.println(pstmt + ".setNull(" + index + "++, java.sql.Types.SMALLINT);"); 122 } 123 124 127 public void generateSetVersion(JavaWriter out, 128 String pstmt, 129 String index, 130 String value) 131 throws IOException 132 { 133 out.println(pstmt + ".setShort(" + index + "++, " + value + " + 1);"); 134 } 135 136 139 public String toObject(String value) 140 { 141 return "new Short(" + value + ")"; 142 } 143 144 147 public String generateCastFromObject(String value) 148 { 149 return "((Number) " + value + ").shortValue()"; 150 } 151 152 155 public Object getObject(ResultSet rs, int index) 156 throws SQLException 157 { 158 short v = rs.getShort(index); 159 160 return rs.wasNull() ? null : new Short (v); 161 } 162 163 166 public Object toObject(long value) 167 { 168 return new Short ((short) value); 169 } 170 171 174 public void setParameter(PreparedStatement pstmt, int index, Object value) 175 throws SQLException 176 { 177 if (value instanceof Number ) 178 pstmt.setString(index, value.toString()); 179 else 180 throw new IllegalArgumentException ("Invalid argument for setParameter."); 181 } 182 } 183 | Popular Tags |