KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > triactive > jdo > store > SqlDateMapping


1 /*
2  * Copyright 2002 (C) TJDO.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the TJDO License version 1.0.
6  * See the terms of the TJDO License in the documentation provided with this software.
7  *
8  * $Id: SqlDateMapping.java,v 1.6 2003/10/10 22:22:15 pierreg0 Exp $
9  */

10
11 package com.triactive.jdo.store;
12
13 import com.triactive.jdo.PersistenceManager;
14 import java.sql.Date JavaDoc;
15 import java.sql.PreparedStatement JavaDoc;
16 import java.sql.ResultSet JavaDoc;
17 import java.sql.SQLException JavaDoc;
18 import java.sql.Types JavaDoc;
19 import javax.jdo.JDODataStoreException;
20
21
22 public class SqlDateMapping extends ColumnMapping
23 {
24     /* Based on JDBC date escape format: "YYYY-MM-DD". */
25     private static final int DATE_STRING_LENGTH = 10;
26
27     public SqlDateMapping(DatabaseAdapter dba, Class JavaDoc 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 JavaDoc ps, int param, Object JavaDoc 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 JavaDoc)value);
72             else
73                 ps.setString(param, value.toString());
74         }
75         catch (SQLException JavaDoc e)
76         {
77             throw dba.newDataStoreException("Can't set java.sql.Date parameter: value = " + value, e);
78         }
79     }
80
81     protected Date JavaDoc getDate(ResultSet JavaDoc rs, int param)
82     {
83         Date JavaDoc value;
84
85         try
86         {
87             if (typeInfo.dataType == Types.DATE)
88                 value = rs.getDate(param);
89             else
90             {
91                 String JavaDoc s = rs.getString(param);
92
93                 value = s == null ? null : Date.valueOf(s);
94             }
95         }
96         catch (SQLException JavaDoc 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 JavaDoc getObject(PersistenceManager pm, ResultSet JavaDoc rs, int param)
105     {
106         Date JavaDoc 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 JavaDoc value)
115     {
116         return new SqlDateLiteral(qs, this, (Date JavaDoc)value);
117     }
118
119     public SQLExpression newSQLExpression(QueryStatement qs, QueryStatement.QueryColumn qsc, String JavaDoc fieldName)
120     {
121         return new SqlDateExpression(qs, qsc);
122     }
123 }
124
Popular Tags