package org.wfc.user.typehandler;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.wfc.user.domain.constant.IdTypeEnum;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class IdTypeEnumTypeHandler extends BaseTypeHandler<IdTypeEnum> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, IdTypeEnum parameter, JdbcType jdbcType)
            throws SQLException {
        ps.setString(i, parameter.name());
    }

    @Override
    public IdTypeEnum getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String name = rs.getString(columnName);
        return name == null ? null : IdTypeEnum.valueOf(name);
    }

    @Override
    public IdTypeEnum getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String name = rs.getString(columnIndex);
        return name == null ? null : IdTypeEnum.valueOf(name);
    }

    @Override
    public IdTypeEnum getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String name = cs.getString(columnIndex);
        return name == null ? null : IdTypeEnum.valueOf(name);
    }
}