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 javax.persistence.TemporalType; 37 import java.io.IOException ; 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 UtilDateType extends Type { 47 private static final L10N L = new L10N(UtilDateType.class); 48 49 public static final UtilDateType 50 TEMPORAL_DATE_TYPE = new UtilDateType(TemporalType.DATE); 51 public static final UtilDateType 52 TEMPORAL_TIME_TYPE = new UtilDateType(TemporalType.TIME); 53 public static final UtilDateType 54 TEMPORAL_TIMESTAMP_TYPE = new UtilDateType(TemporalType.TIMESTAMP); 55 56 private TemporalType _temporalType; 57 58 private UtilDateType(TemporalType temporalType) 59 { 60 _temporalType = temporalType; 61 } 62 63 66 public static UtilDateType create() 67 { 68 return TEMPORAL_TIMESTAMP_TYPE; 69 } 70 71 74 public String getName() 75 { 76 return "java.util.Date"; 77 } 78 79 82 @Override 83 public boolean isAssignableTo(JClass javaType) 84 { 85 return javaType.isAssignableFrom(java.util.Date .class); 86 } 87 88 91 public String generateCreateColumnSQL(AmberPersistenceUnit manager, 92 int length, 93 int precision, 94 int scale) 95 { 96 return manager.getCreateColumnSQL(Types.TIMESTAMP, length, precision, scale); 97 } 98 99 102 public int generateLoad(JavaWriter out, 103 String rs, 104 String indexVar, 105 int index) 106 throws IOException 107 { 108 out.print("com.caucho.amber.type.UtilDateType.toDate(" + rs + ".getTimestamp(" + indexVar + " + " + index + "))"); 109 110 return index + 1; 111 } 112 113 116 public void generateSet(JavaWriter out, 117 String pstmt, 118 String index, 119 String value) 120 throws IOException 121 { 122 out.println("if (" + value + " == null)"); 123 out.println(" " + pstmt + ".setNull(" + index + "++, java.sql.Types.TIMESTAMP);"); 124 out.println("else"); 125 out.println(" " + pstmt + ".setTimestamp(" + index + "++, new java.sql.Timestamp(" + value + ".getTime()));"); 126 } 127 128 131 public static java.util.Date toDate(java.sql.Timestamp time) 132 throws SQLException 133 { 134 return time; 135 } 136 137 140 public Object getObject(ResultSet rs, int index) 141 throws SQLException 142 { 143 java.sql.Timestamp date = rs.getTimestamp(index); 144 145 if (date == null) 146 return null; 147 else 148 return new java.util.Date (date.getTime()); 149 } 150 151 154 public void setParameter(PreparedStatement pstmt, 155 int index, 156 Object value) 157 throws SQLException 158 { 159 switch (_temporalType) { 160 case DATE: 161 pstmt.setObject(index, value, Types.DATE); 162 break; 163 164 case TIME: 165 pstmt.setObject(index, value, Types.TIME); 166 break; 167 168 default: 169 pstmt.setObject(index, value, Types.TIMESTAMP); 170 break; 171 } 172 } 173 } 174 | Popular Tags |