1 28 29 package com.caucho.amber.type; 30 31 import com.caucho.amber.manager.AmberPersistenceUnit; 32 import com.caucho.bytecode.JClass; 33 import com.caucho.java.JavaWriter; 34 import com.caucho.util.L10N; 35 import com.caucho.util.Log; 36 37 import java.io.IOException ; 38 import java.lang.reflect.Method ; 39 import java.sql.PreparedStatement ; 40 import java.sql.ResultSet ; 41 import java.sql.SQLException ; 42 import java.sql.Types ; 43 import java.util.logging.Level ; 44 import java.util.logging.Logger ; 45 46 49 public class EnumType extends Type { 50 private static final Logger log = Log.open(EnumType.class); 51 private static final L10N L = new L10N(EnumType.class); 52 53 private JClass _beanClass; 54 55 private String _name; 56 57 private boolean _isOrdinal = true; 58 59 60 public EnumType() 61 { 62 } 63 64 67 public JClass getBeanClass() 68 { 69 return _beanClass; 70 } 71 72 75 public void setBeanClass(JClass beanClass) 76 { 77 _beanClass = beanClass; 78 } 79 80 83 public String getName() 84 { 85 return _name; 86 } 87 88 91 public void setName(String name) 92 { 93 _name = name; 94 } 95 96 99 public boolean isNumeric() 100 { 101 return isOrdinal(); 102 } 103 104 107 public boolean isOrdinal() 108 { 109 return _isOrdinal; 110 } 111 112 115 public void setOrdinal(boolean isOrdinal) 116 { 117 _isOrdinal = isOrdinal; 118 } 119 120 123 public Type getForeignType() 124 { 125 return IntegerType.create(); 126 } 127 128 131 public String generateCreateColumnSQL(AmberPersistenceUnit manager, int length, int precision, int scale) 132 { 133 if (_isOrdinal) 134 return manager.getCreateColumnSQL(Types.INTEGER, length, precision, scale); 135 else { 136 if (length == 0) 137 length = 255; 138 139 return "varchar(" + length + ")"; 140 } 141 } 142 143 146 public int generateLoad(JavaWriter out, String rs, 147 String indexVar, int index) 148 throws IOException 149 { 150 if (_isOrdinal) { 151 out.print("(" + getName() + ") com.caucho.amber.type.EnumType.toEnum(" + 152 rs + ".getInt(" + indexVar + " + " + index + "), " + 153 rs + ".wasNull(), "+ 154 getName() + ".values())"); 155 } 156 else { 157 out.print("(" + getName() + ") com.caucho.amber.type.EnumType.toEnum(" + 158 rs + ".getString(" + indexVar + " + " + index + "), " + 159 rs + ".wasNull(), "+ 160 getName() + ".class)"); 161 } 162 163 return index + 1; 164 } 165 166 169 public int generateLoadForeign(JavaWriter out, String rs, 170 String indexVar, int index) 171 throws IOException 172 { 173 if (_isOrdinal) { 174 out.print("(" + getName() + ") com.caucho.amber.type.EnumType.toEnum(" + 175 rs + ".getInt(" + indexVar + " + " + index + "), " + 176 rs + ".wasNull(), "+ 177 getName() + ".values())"); 178 } 179 else { 180 out.print("(" + getName() + ") com.caucho.amber.type.EnumType.toEnum(" + 181 rs + ".getString(" + indexVar + " + " + index + "), " + 182 rs + ".wasNull(), "+ 183 getName() + ".class)"); 184 } 185 186 return index + 1; 187 } 188 189 192 public void generateSet(JavaWriter out, String pstmt, 193 String index, String value) 194 throws IOException 195 { 196 if (_isOrdinal) { 197 out.println("if (" + value + " == null)"); 198 out.println(" " + pstmt + ".setNull(" + index + "++, java.sql.Types.INTEGER);"); 199 out.println("else"); 200 out.println(" " + pstmt + ".setInt(" + index + "++, " + value + ".ordinal());"); 201 } 202 else { 203 if (pstmt.startsWith("query")) 204 out.println(pstmt + ".setString(" + index + "++, " + value + ");"); 205 else 206 out.println("__caucho_setInternalString(" + pstmt + ", " + index + "++, " + value + " == null ? null : " + value + ".toString());"); 207 } 208 } 209 210 213 public void setParameter(PreparedStatement pstmt, int index, Object value) 214 throws SQLException 215 { 216 if (_isOrdinal) { 217 if (value == null) 218 pstmt.setNull(index, Types.INTEGER); 219 else 220 pstmt.setInt(index, ((Enum ) value).ordinal()); 221 } 222 else { 223 if (value == null) 224 pstmt.setNull(index, java.sql.Types.OTHER); 225 else 226 pstmt.setString(index, value.toString()); 227 } 228 } 229 230 233 public String toObject(String value) 234 { 235 return value; 236 } 237 238 241 public String generateCastFromObject(String value) 242 { 243 return value + ".ordinal()"; 244 } 245 246 249 public static Object toEnum(int ordinal, 250 boolean wasNull, 251 Object values[]) 252 { 253 if (wasNull) 254 return null; 255 else 256 return values[ordinal]; 257 } 258 259 262 public static Object toEnum(String name, 263 boolean wasNull, 264 Class cl) 265 { 266 if (wasNull) 267 return null; 268 else 269 return Enum.valueOf(cl, name); 270 } 271 272 275 public Object getObject(ResultSet rs, int index) 276 throws SQLException 277 { 278 if (_isOrdinal) { 279 Object [] values = getValues(); 280 281 if (values == null) 282 return null; 283 284 int v = rs.getInt(index); 285 286 return rs.wasNull() ? null : values[v]; 287 } 288 else { 289 Class cl = getBeanClass().getJavaClass(); 290 291 String name = rs.getString(index); 292 293 return rs.wasNull() ? null : Enum.valueOf(cl, name); 294 } 295 } 296 297 300 public Object toObject(long value) 301 { 302 if (_isOrdinal) { 303 Object [] values = getValues(); 304 305 if (values == null) 306 return null; 307 308 return values[(int) value]; 309 } 310 311 return null; 312 } 313 314 private Object [] getValues() 315 { 316 try { 317 Class cl = getBeanClass().getJavaClass(); 318 319 Method method = cl.getDeclaredMethod("values", null); 320 321 Object object = method.invoke(cl, null); 322 323 return (Object []) object; 324 } catch (Exception e) { 325 log.log(Level.FINE, e.toString(), e); 326 return null; 327 } 328 } 329 } 330 | Popular Tags |