1 28 29 package com.caucho.amber.type; 30 31 import com.caucho.java.JavaWriter; 32 import com.caucho.util.L10N; 33 34 import javax.persistence.TemporalType; 35 import java.io.IOException ; 36 import java.sql.PreparedStatement ; 37 import java.sql.ResultSet ; 38 import java.sql.SQLException ; 39 import java.sql.Timestamp ; 40 import java.sql.Types ; 41 import java.util.Calendar ; 42 43 46 public class CalendarType extends Type { 47 private static final L10N L = new L10N(CalendarType.class); 48 49 public static final CalendarType 50 TEMPORAL_DATE_TYPE = new CalendarType(TemporalType.DATE); 51 public static final CalendarType 52 TEMPORAL_TIME_TYPE = new CalendarType(TemporalType.TIME); 53 public static final CalendarType 54 TEMPORAL_TIMESTAMP_TYPE = new CalendarType(TemporalType.TIMESTAMP); 55 56 private TemporalType _temporalType; 57 58 private CalendarType(TemporalType temporalType) 59 { 60 _temporalType = temporalType; 61 } 62 63 66 public static CalendarType create() 67 { 68 return TEMPORAL_TIMESTAMP_TYPE; 69 } 70 71 74 public String getName() 75 { 76 return "java.util.Calendar"; 77 } 78 79 82 public int generateLoad(JavaWriter out, String rs, 83 String indexVar, int index) 84 throws IOException 85 { 86 out.print("com.caucho.amber.type.CalendarType.toCalendar(" + rs + ".getTimestamp(" + indexVar + " + " + index + "))"); 87 88 return index + 1; 89 } 90 91 94 public void generateSet(JavaWriter out, String pstmt, 95 String index, String value) 96 throws IOException 97 { 98 out.println("if (" + value + " == null)"); 99 out.println(" " + pstmt + ".setNull(" + index + "++, java.sql.Types.TIMESTAMP);"); 100 out.println("else"); 101 out.println(" " + pstmt + ".setTimestamp(" + index + "++, new java.sql.Timestamp(" + value + ".getTimeInMillis()));"); 102 } 103 104 107 public static Calendar toCalendar(java.util.Date time) 108 throws SQLException 109 { 110 if (time == null) 111 return null; 112 else { 113 Calendar cal = Calendar.getInstance(); 114 cal.setTime(time); 115 116 return cal; 117 } 118 } 119 120 123 public Object getObject(ResultSet rs, int index) 124 throws SQLException 125 { 126 java.sql.Timestamp time = rs.getTimestamp(index); 127 128 if (time == null) 129 return null; 130 else { 131 Calendar cal = Calendar.getInstance(); 132 cal.setTime(time); 133 134 return cal; 135 } 136 } 137 138 141 public void setParameter(PreparedStatement pstmt, 142 int index, 143 Object value) 144 throws SQLException 145 { 146 Timestamp timestamp 147 = new Timestamp (((Calendar ) value).getTimeInMillis()); 148 149 switch (_temporalType) { 150 case DATE: 151 pstmt.setObject(index, timestamp, Types.DATE); 152 break; 153 154 case TIME: 155 pstmt.setObject(index, timestamp, Types.TIME); 156 break; 157 158 default: 159 pstmt.setObject(index, timestamp, Types.TIMESTAMP); 160 break; 161 } 162 } 163 } 164 | Popular Tags |