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 36 import java.io.IOException ; 37 import java.sql.Date ; 38 import java.sql.PreparedStatement ; 39 import java.sql.ResultSet ; 40 import java.sql.SQLException ; 41 import java.sql.Types ; 42 43 46 public class SqlDateType extends Type { 47 private static final L10N L = new L10N(SqlDateType.class); 48 49 private static final SqlDateType SQL_DATE_TYPE = new SqlDateType(); 50 51 private SqlDateType() 52 { 53 } 54 55 58 public static SqlDateType create() 59 { 60 return SQL_DATE_TYPE; 61 } 62 63 66 public String getName() 67 { 68 return "java.sql.Date"; 69 } 70 71 74 @Override 75 public boolean isAssignableTo(JClass javaType) 76 { 77 return javaType.isAssignableFrom(java.sql.Date .class); 78 } 79 80 83 public String generateCreateColumnSQL(AmberPersistenceUnit manager, int length, int precision, int scale) 84 { 85 return manager.getCreateColumnSQL(Types.DATE, length, precision, scale); 86 } 87 88 91 public int generateLoad(JavaWriter out, String rs, 92 String indexVar, int index) 93 throws IOException 94 { 95 out.print(rs + ".getDate(" + indexVar + " + " + index + ")"); 96 97 return index + 1; 98 } 99 100 103 public void generateSet(JavaWriter out, String pstmt, 104 String index, String value) 105 throws IOException 106 { 107 out.println("if (" + value + " == null)"); 108 out.println(" " + pstmt + ".setNull(" + index + "++, java.sql.Types.DATE);"); 109 out.println("else"); 110 out.println(" " + pstmt + ".setDate(" + index + "++, " + value + ");"); 111 } 112 113 116 public void setParameter(PreparedStatement pstmt, int index, Object value) 117 throws SQLException 118 { 119 pstmt.setDate(index, (Date ) value); 120 } 121 122 125 public Object getObject(ResultSet rs, int index) 126 throws SQLException 127 { 128 return rs.getDate(index); 129 } 130 } 131 | Popular Tags |