1 2 12 package com.versant.core.jdbc.sql.conv; 13 14 import com.versant.core.jdbc.JdbcConverter; 15 import com.versant.core.jdbc.JdbcTypeRegistry; 16 import com.versant.core.jdbc.metadata.JdbcColumn; 17 18 import javax.jdo.JDOFatalDataStoreException; import java.sql.*; 20 import java.util.Date ; 21 22 26 public class DateTimestampConverter extends JdbcConverterBase { 27 28 private static final boolean useNanos; 29 30 static { 31 32 String v = System.getProperty("java.version"); 33 useNanos = v.startsWith("1.3"); 34 35 36 } 37 38 public static class Factory extends NoArgJdbcConverterFactory { 39 40 private DateTimestampConverter converter; 41 42 46 public JdbcConverter createJdbcConverter(JdbcColumn col, Object args, 47 JdbcTypeRegistry jdbcTypeRegistry) { 48 if (converter == null) converter = new DateTimestampConverter(); 49 return converter; 50 } 51 52 } 53 54 60 public Object get(ResultSet rs, int index, JdbcColumn col) 61 throws SQLException, JDOFatalDataStoreException { 62 Timestamp t = rs.getTimestamp(index); 63 if (t == null) return null; 64 if (useNanos) { 65 return new Date (t.getTime() + (t.getNanos() / 1000000)); 66 } else { 67 return new Date (t.getTime()); 68 } 69 } 70 71 77 public void set(PreparedStatement ps, int index, JdbcColumn col, 78 Object value) 79 throws SQLException, JDOFatalDataStoreException { 80 if (value == null) { 81 ps.setNull(index, col.jdbcType); 82 } else { 83 Date d = (Date )value; 84 if (col.jdbcType == Types.DATE) { 85 ps.setDate(index, new java.sql.Date (d.getTime())); 86 } else { 87 ps.setTimestamp(index, new Timestamp(d.getTime())); 88 } 89 } 90 } 91 92 96 public Class getValueType() { 97 return Date .class; 98 } 99 } 100 101 | Popular Tags |