01: package simpleorm.core;
02:
03: import simpleorm.properties.*;
04: import java.sql.ResultSet;
05:
06: /** Represents Date field meta data. */
07:
08: public class SFieldDate extends SFieldScalar {
09: public SFieldDate(SRecordMeta meta, String columnName,
10: SPropertyValue[] pvals) {
11: super (meta, columnName, pvals);
12: }
13:
14: public SFieldDate(SRecordMeta meta, String columnName) {
15: this (meta, columnName, new SPropertyValue[0]);
16: }
17:
18: public SFieldDate(SRecordMeta meta, String columnName,
19: SPropertyValue pval) {
20: this (meta, columnName, new SPropertyValue[] { pval });
21: }
22:
23: public SFieldDate(SRecordMeta meta, String columnName,
24: SPropertyValue pval1, SPropertyValue pval2) {
25: this (meta, columnName, new SPropertyValue[] { pval1, pval2 });
26: }
27:
28: /** Abstract specializer. Clone this key field to
29: be a foreign key
30: to <code>rmeta</code> of the same type.*/
31: SFieldMeta makeForeignKey(SRecordMeta rmeta, String prefix,
32: SPropertyValue[] pvals) {
33: return new SFieldDate(rmeta, (prefix == null ? "" : prefix)
34: + getString(SCon.SCOLUMN_NAME), pvals);
35: }
36:
37: Object queryFieldValue(ResultSet rs, int sqlIndex) throws Exception {
38: java.sql.Date res = rs.getDate(sqlIndex);
39: if (rs.wasNull()) // ie. last operation!
40: return null;
41: else
42: return res;
43: }
44:
45: Object convertToField(Object raw) throws Exception {
46: if (raw instanceof java.sql.Date)
47: return (java.sql.Date) raw;
48: if (raw == null)
49: return null; // Follows JDBC
50: if (raw instanceof String)
51: return java.sql.Date.valueOf((String) raw);
52: if (raw instanceof java.util.Date)
53: return new java.sql.Date(((java.util.Date) raw).getTime());
54:
55: throw new SException.Data("Cannot convert " + raw + " to Date.");
56: }
57:
58: /** Specializes SFieldMeta. */
59: String defaultDataType() {
60: return "DATE";
61: }
62: }
|