Skip to content

Instantly share code, notes, and snippets.

@taktos
Last active December 27, 2015 04:09
Show Gist options
  • Save taktos/7265337 to your computer and use it in GitHub Desktop.
Save taktos/7265337 to your computer and use it in GitHub Desktop.
DBFlute で MySQL のDATE型をJoda-TimeのLocalDateにマッピングする

DBFlute で MySQL のDATE型をJoda-TimeのLocalDateにマッピングする

やること

  • Java
  • TnAbstractValueType を継承したValueTypeを作り、java.sql.Dateとorg.joda.time.LocalDateの変換を実装する
  • DBFluteInitializerを継承し、prologue()をオーバーライドして、DBFluteConfig#registerBasicValueType() でLocalDateに対するValueTypeを登録する
  • dfprop
  • typeMappingMap.dfprop で、DATEをorg.joda.time.LocalDateにマッピングする
  • littleAdjustmentMap.dfpropのextendedDBFluteInitializerClassに、拡張したDBFluteInitializerを設定する
  • includeQueryMap.dfpropでFromToとDateFromToの生成を抑制する(FromToがjava.util.Dateを使ってしまいコンパイルエラーになるため)

ValueType

public class JodaDateType extends TnAbstractValueType {

    public JodaDateType() {
        super(Types.DATE);
    }

    @Override
    public Object getValue(final ResultSet rs, final int index) throws SQLException {
        final Date date = rs.getDate(index);
        return date == null ? null : LocalDate.fromDateFields(date);
    }

    @Override
    public Object getValue(final ResultSet rs, final String columnName) throws SQLException {
        final Date date = rs.getDate(columnName);
        return date == null ? null : LocalDate.fromDateFields(date);
    }

    @Override
    public Object getValue(final CallableStatement cs, final int index) throws SQLException {
        final Date date = cs.getDate(index);
        return date == null ? null : LocalDate.fromDateFields(date);
    }

    @Override
    public Object getValue(final CallableStatement cs, final String parameterName) throws SQLException {
        final Date date = cs.getDate(parameterName);
        return date == null ? null : LocalDate.fromDateFields(date);
    }

    @Override
    public void bindValue(final Connection conn, final PreparedStatement ps, final int index, final Object value) throws SQLException {
        if (value == null) {
            this.setNull(ps, index);
        } else {
            ps.setDate(index, this.toSqlDate(value));
        }

    }

    @Override
    public void bindValue(final Connection conn, final CallableStatement cs, final String parameterName, final Object value) throws SQLException {
        if (value == null) {
            this.setNull(cs, parameterName);
        } else {
            cs.setDate(parameterName, this.toSqlDate(value));
        }
    }

    private java.sql.Date toSqlDate(final Object value) {
        assert value != null;

        if (value instanceof ReadablePartial) {
            return new java.sql.Date(((ReadablePartial) value).toDateTime(null).withTimeAtStartOfDay().getMillis());
        } else if (value instanceof ReadableInstant) {
            return new java.sql.Date(((ReadableInstant) value).getMillis());
        }

        return DfTypeUtil.toSqlDate(value);
    }
}

DBFluteInitializerの継承

public class ExtendedDBFluteInitializer extends DBFluteInitializer {
    private static final ValueType DATE_TYPE = new JodaDateType();

    public ExtendedDBFluteInitializer(final DataSource dataSource) {
        super(dataSource);
    }

    @Override
    protected void prologue() {
        super.prologue();
        final DBFluteConfig config = DBFluteConfig.getInstance();
        config.registerBasicValueType(LocalDate.class, DATE_TYPE);
    }
}

typeMappingMap.dfprop

map:{
    ; NUMERIC = $$AutoMapping$$ ; DECIMAL = $$AutoMapping$$
    ; DATE = org.joda.time.LocalDate
}

littleAdjustmentMap.dfprop

...
    ; extendedDBFluteInitializerClass = com.example.ExtendedDBFluteInitializer
...

includeQueryMap.dfprop

...
    ; Date = map:{
        ; NotEqual = map:{}
        ; InScope = map:{}
        ; NotInScope = map:{}
        ; FromTo = map:{}
        ; DateFromTo = map:{}
    }
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment