1 29 30 package com.caucho.amber.type; 31 32 import com.caucho.amber.entity.EntityItem; 33 import com.caucho.amber.manager.AmberConnection; 34 import com.caucho.amber.manager.AmberPersistenceUnit; 35 import com.caucho.bytecode.JClass; 36 import com.caucho.config.ConfigException; 37 import com.caucho.java.JavaWriter; 38 import com.caucho.util.L10N; 39 40 import java.io.IOException ; 41 import java.sql.PreparedStatement ; 42 import java.sql.ResultSet ; 43 import java.sql.SQLException ; 44 import java.sql.Types ; 45 46 49 abstract public class Type { 50 private static final L10N L = new L10N(Type.class); 51 52 55 abstract public String getName(); 56 57 60 public boolean isBoolean() 61 { 62 return false; 63 } 64 65 68 public boolean isNumeric() 69 { 70 return false; 71 } 72 73 76 public String getJavaTypeName() 77 { 78 return getName(); 79 } 80 81 84 public int getColumnCount() 85 { 86 return 1; 87 } 88 89 92 public void init() 93 throws ConfigException 94 { 95 } 96 97 100 public Type getForeignType() 101 { 102 return this; 103 } 104 105 108 public String getForeignTypeName() 109 { 110 return getForeignType().getJavaTypeName(); 111 } 112 113 116 public boolean isAssignableTo(JClass javaType) 117 { 118 return true; 119 } 120 121 124 public String generateCreateColumnSQL(AmberPersistenceUnit manager, int length, int precision, int scale) 125 { 126 if (length == 0) 127 length = 255; 128 129 return manager.getCreateColumnSQL(Types.VARCHAR, length, precision, scale); 130 } 131 132 135 public int generateLoad(JavaWriter out, String rs, 136 String indexVar, int index) 137 throws IOException 138 { 139 throw new UnsupportedOperationException (getClass().getName()); 140 } 141 142 145 public int generateLoad(JavaWriter out, String rs, 146 String indexVar, int index, 147 JClass targetType) 148 throws IOException 149 { 150 String i = indexVar + " + " + index; 151 152 if ("java.lang.Byte".equals(targetType.getName())) 153 out.print("new Byte((byte) " + rs + ".getInt(" + i + "))"); 154 else if ("java.lang.Short".equals(targetType.getName())) 155 out.print("new Short((short) " + rs + ".getInt(" + i + "))"); 156 else if ("java.lang.Integer".equals(targetType.getName())) { 157 out.print("new Integer(" + rs + ".getInt(" + i + "))"); 159 } 160 else if ("java.lang.Long".equals(targetType.getName())) 161 out.print("new Long(" + rs + ".getLong(" + i + "))"); 162 else if ("java.lang.Float".equals(targetType.getName())) 163 out.print("new Float((float) " + rs + ".getDouble(" + i + "))"); 164 else if ("java.lang.Double".equals(targetType.getName())) 165 out.print("new Double(" + rs + ".getDouble(" + i + "))"); 166 else if ("java.lang.String".equals(targetType.getName())) 167 out.print(rs + ".getString(" + i + ")"); 168 else { 169 out.print("(" + targetType.getName() + ") "); 170 out.print(rs + ".getObject(" + i + ")"); 171 } 172 173 return index + 1; 174 } 175 176 179 public int generateLoadForeign(JavaWriter out, String rs, 180 String indexVar, int index) 181 throws IOException 182 { 183 return getForeignType().generateLoad(out, rs, indexVar, index); 184 } 185 186 189 public void generateSet(JavaWriter out, String pstmt, 190 String index, String value) 191 throws IOException 192 { 193 throw new UnsupportedOperationException (getClass().getName()); 194 } 195 196 199 public void generateSetVersion(JavaWriter out, 200 String pstmt, 201 String index, 202 String value) 203 throws IOException 204 { 205 throw new UnsupportedOperationException (getClass().getName()); 206 } 207 208 211 public String generateIncrementVersion(String value) 212 throws IOException 213 { 214 return value + " + 1"; 215 } 216 217 220 public void generateSetNull(JavaWriter out, String pstmt, String index) 221 throws IOException 222 { 223 generateSet(out, pstmt, index, null); 224 } 225 226 229 public void setParameter(PreparedStatement pstmt, int index, Object value) 230 throws SQLException 231 { 232 pstmt.setObject(index, value); 233 } 234 235 238 public Object getObject(ResultSet rs, int index) 239 throws SQLException 240 { 241 return rs.getObject(index); 242 } 243 244 247 public EntityItem findItem(AmberConnection aConn, ResultSet rs, int index) 248 throws SQLException 249 { 250 throw new UnsupportedOperationException (getClass().getName()); 251 } 252 253 256 public Object getObject(AmberConnection aConn, ResultSet rs, int index) 257 throws SQLException 258 { 259 return getObject(rs, index); 260 } 261 262 265 public String toObject(String value) 266 { 267 return value; 268 } 269 270 273 public String fromObject(String value) 274 { 275 return generateCastFromObject(value); 276 } 277 278 281 public Object toObject(long value) 282 { 283 return new Long (value); 284 } 285 286 289 public String generateCastFromObject(String value) 290 { 291 return "((" + getName() + ") " + value + ")"; 292 } 293 294 297 public String generateEquals(String a, String b) 298 { 299 return a + ".equals(" + b + ")"; 300 } 301 302 305 public String generateIsNull(String value) 306 { 307 return "(" + value + " == " + generateNull() + ")"; 308 } 309 310 313 public String generateNull() 314 { 315 return "null"; 316 } 317 318 321 public boolean isAutoIncrement() 322 { 323 return false; 324 } 325 } 326 | Popular Tags |