1 29 30 package com.caucho.amber.type; 31 32 import com.caucho.amber.manager.AmberPersistenceUnit; 33 import com.caucho.bytecode.JClass; 34 import com.caucho.java.JavaWriter; 35 import com.caucho.util.L10N; 36 37 import java.io.IOException ; 38 import java.sql.PreparedStatement ; 39 import java.sql.ResultSet ; 40 import java.sql.SQLException ; 41 import java.sql.Timestamp ; 42 import java.sql.Types ; 43 44 47 public class SqlTimestampType extends Type { 48 private static final L10N L = new L10N(SqlTimestampType.class); 49 50 private static final SqlTimestampType SQL_TIMESTAMP_TYPE = new SqlTimestampType(); 51 52 private SqlTimestampType() 53 { 54 } 55 56 59 public static SqlTimestampType create() 60 { 61 return SQL_TIMESTAMP_TYPE; 62 } 63 64 67 public String getName() 68 { 69 return "java.sql.Timestamp"; 70 } 71 72 75 @Override 76 public boolean isAssignableTo(JClass javaType) 77 { 78 return javaType.isAssignableFrom(java.sql.Timestamp .class); 79 } 80 81 84 public String generateCreateColumnSQL(AmberPersistenceUnit manager, int length, int precision, int scale) 85 { 86 return manager.getCreateColumnSQL(Types.TIMESTAMP, length, precision, scale); 87 } 88 89 92 public int generateLoad(JavaWriter out, String rs, 93 String indexVar, int index) 94 throws IOException 95 { 96 out.print(rs + ".getTimestamp(" + indexVar + " + " + index + ")"); 97 98 return index + 1; 99 } 100 101 104 public void generateSet(JavaWriter out, String pstmt, 105 String index, String value) 106 throws IOException 107 { 108 out.println("if (" + value + " == null)"); 109 out.println(" " + pstmt + ".setNull(" + index + "++, java.sql.Types.TIMESTAMP);"); 110 out.println("else"); 111 out.println(" " + pstmt + ".setTimestamp(" + index + "++, " + value + ");"); 112 } 113 114 117 public void generateSetVersion(JavaWriter out, 118 String pstmt, 119 String index, 120 String value) 121 throws IOException 122 { 123 value = "new java.sql.Timestamp(new java.util.Date().getTime())"; 124 out.println(pstmt + ".setTimestamp(" + index + "++, " + value + ");"); 125 } 126 127 130 public String generateIncrementVersion(String value) 131 throws IOException 132 { 133 return "new java.sql.Timestamp(new java.util.Date().getTime())"; 134 } 135 136 139 public void setParameter(PreparedStatement pstmt, int index, Object value) 140 throws SQLException 141 { 142 pstmt.setTimestamp(index, (Timestamp ) value); 143 } 144 145 148 public Object getObject(ResultSet rs, int index) 149 throws SQLException 150 { 151 return rs.getTimestamp(index); 152 } 153 } 154 | Popular Tags |