001: /*
002: * Copyright 2002 (C) TJDO.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the TJDO License version 1.0.
006: * See the terms of the TJDO License in the documentation provided with this software.
007: *
008: * $Id: SqlDateMapping.java,v 1.6 2003/10/10 22:22:15 pierreg0 Exp $
009: */
010:
011: package com.triactive.jdo.store;
012:
013: import com.triactive.jdo.PersistenceManager;
014: import java.sql.Date;
015: import java.sql.PreparedStatement;
016: import java.sql.ResultSet;
017: import java.sql.SQLException;
018: import java.sql.Types;
019: import javax.jdo.JDODataStoreException;
020:
021: public class SqlDateMapping extends ColumnMapping {
022: /* Based on JDBC date escape format: "YYYY-MM-DD". */
023: private static final int DATE_STRING_LENGTH = 10;
024:
025: public SqlDateMapping(DatabaseAdapter dba, Class type) {
026: super (dba, type);
027:
028: initTypeInfo();
029: }
030:
031: public SqlDateMapping(Column col) {
032: super (col);
033:
034: col.checkPrimitive();
035:
036: initTypeInfo();
037: }
038:
039: public SqlDateMapping(ClassBaseTable table, int relativeFieldNumber) {
040: this (table.newColumn(relativeFieldNumber));
041: }
042:
043: protected TypeInfo getTypeInfo() {
044: return dba.getTypeInfo(new int[] { Types.DATE, Types.CHAR });
045: }
046:
047: protected void initTypeInfo() {
048: super .initTypeInfo();
049:
050: if (col != null && typeInfo.dataType == Types.CHAR) {
051: col.setFixedLength(DATE_STRING_LENGTH);
052: col.checkString();
053: }
054: }
055:
056: public void setObject(PersistenceManager pm, PreparedStatement ps,
057: int param, Object value) {
058: try {
059: if (value == null)
060: ps.setNull(param, typeInfo.dataType);
061: else if (typeInfo.dataType == Types.DATE)
062: ps.setDate(param, (Date) value);
063: else
064: ps.setString(param, value.toString());
065: } catch (SQLException e) {
066: throw dba.newDataStoreException(
067: "Can't set java.sql.Date parameter: value = "
068: + value, e);
069: }
070: }
071:
072: protected Date getDate(ResultSet rs, int param) {
073: Date value;
074:
075: try {
076: if (typeInfo.dataType == Types.DATE)
077: value = rs.getDate(param);
078: else {
079: String s = rs.getString(param);
080:
081: value = s == null ? null : Date.valueOf(s);
082: }
083: } catch (SQLException e) {
084: throw dba.newDataStoreException(
085: "Can't get java.sql.Date result: param = " + param,
086: e);
087: }
088:
089: return value;
090: }
091:
092: public Object getObject(PersistenceManager pm, ResultSet rs,
093: int param) {
094: Date value = getDate(rs, param);
095:
096: if (value == null)
097: return null;
098: else
099: return value;
100: }
101:
102: public SQLExpression newSQLLiteral(QueryStatement qs, Object value) {
103: return new SqlDateLiteral(qs, this , (Date) value);
104: }
105:
106: public SQLExpression newSQLExpression(QueryStatement qs,
107: QueryStatement.QueryColumn qsc, String fieldName) {
108: return new SqlDateExpression(qs, qsc);
109: }
110: }
|