1 10 11 package com.triactive.jdo.store; 12 13 import com.triactive.jdo.PersistenceManager; 14 import java.sql.Date ; 15 import java.sql.PreparedStatement ; 16 import java.sql.ResultSet ; 17 import java.sql.SQLException ; 18 import java.sql.Types ; 19 import javax.jdo.JDODataStoreException; 20 21 22 public class SqlDateMapping extends ColumnMapping 23 { 24 25 private static final int DATE_STRING_LENGTH = 10; 26 27 public SqlDateMapping(DatabaseAdapter dba, Class type) 28 { 29 super(dba, type); 30 31 initTypeInfo(); 32 } 33 34 public SqlDateMapping(Column col) 35 { 36 super(col); 37 38 col.checkPrimitive(); 39 40 initTypeInfo(); 41 } 42 43 public SqlDateMapping(ClassBaseTable table, int relativeFieldNumber) 44 { 45 this(table.newColumn(relativeFieldNumber)); 46 } 47 48 protected TypeInfo getTypeInfo() 49 { 50 return dba.getTypeInfo(new int[] { Types.DATE, Types.CHAR }); 51 } 52 53 protected void initTypeInfo() 54 { 55 super.initTypeInfo(); 56 57 if (col != null && typeInfo.dataType == Types.CHAR) 58 { 59 col.setFixedLength(DATE_STRING_LENGTH); 60 col.checkString(); 61 } 62 } 63 64 public void setObject(PersistenceManager pm, PreparedStatement ps, int param, Object value) 65 { 66 try 67 { 68 if (value == null) 69 ps.setNull(param, typeInfo.dataType); 70 else if (typeInfo.dataType == Types.DATE) 71 ps.setDate(param, (Date )value); 72 else 73 ps.setString(param, value.toString()); 74 } 75 catch (SQLException e) 76 { 77 throw dba.newDataStoreException("Can't set java.sql.Date parameter: value = " + value, e); 78 } 79 } 80 81 protected Date getDate(ResultSet rs, int param) 82 { 83 Date value; 84 85 try 86 { 87 if (typeInfo.dataType == Types.DATE) 88 value = rs.getDate(param); 89 else 90 { 91 String s = rs.getString(param); 92 93 value = s == null ? null : Date.valueOf(s); 94 } 95 } 96 catch (SQLException e) 97 { 98 throw dba.newDataStoreException("Can't get java.sql.Date result: param = " + param, e); 99 } 100 101 return value; 102 } 103 104 public Object getObject(PersistenceManager pm, ResultSet rs, int param) 105 { 106 Date value = getDate(rs, param); 107 108 if (value == null) 109 return null; 110 else 111 return value; 112 } 113 114 public SQLExpression newSQLLiteral(QueryStatement qs, Object value) 115 { 116 return new SqlDateLiteral(qs, this, (Date )value); 117 } 118 119 public SQLExpression newSQLExpression(QueryStatement qs, QueryStatement.QueryColumn qsc, String fieldName) 120 { 121 return new SqlDateExpression(qs, qsc); 122 } 123 } 124 | Popular Tags |