1 28 29 package com.caucho.amber.type; 30 31 import com.caucho.amber.AmberException; 32 import com.caucho.java.JavaWriter; 33 import com.caucho.util.L10N; 34 35 import java.io.IOException ; 36 import java.sql.ResultSet ; 37 import java.sql.SQLException ; 38 39 42 public class ClassType extends Type { 43 private static final L10N L = new L10N(ClassType.class); 44 45 private static final ClassType CLASS_TYPE = new ClassType(); 46 47 private ClassType() 48 { 49 } 50 51 54 public static ClassType create() 55 { 56 return CLASS_TYPE; 57 } 58 59 62 public String getName() 63 { 64 return "java.lang.Class"; 65 } 66 67 70 public int generateLoad(JavaWriter out, String rs, 71 String indexVar, int index) 72 throws IOException 73 { 74 out.print("com.caucho.amber.type.ClassType.toClass(" + rs + ".getString(" + indexVar + " + " + index + "))"); 75 76 return index + 1; 77 } 78 79 82 public void generateSet(JavaWriter out, String pstmt, 83 String index, String value) 84 throws IOException 85 { 86 out.println("if (" + value + " == null)"); 87 out.println(" " + pstmt + ".setNull(" + index + "++, java.sql.Types.VARCHAR);"); 88 out.println("else"); 89 out.println(" " + pstmt + ".setString(" + index + "++, " + value + ".getName());"); 90 } 91 92 95 public void generateSetNull(JavaWriter out, String pstmt, String index) 96 throws IOException 97 { 98 out.println(pstmt + ".setNull(" + index + "++, java.sql.Types.VARCHAR);"); 99 } 100 101 104 public static Class toClass(String name) 105 throws SQLException 106 { 107 if (name == null) 108 return null; 109 110 try { 111 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 112 113 return Class.forName(name, false, loader); 114 } catch (ClassNotFoundException e) { 115 throw new AmberException(e); 116 } 117 } 118 119 122 public Object getObject(ResultSet rs, int index) 123 throws SQLException 124 { 125 return toClass(rs.getString(index)); 126 } 127 } 128 | Popular Tags |