package com.ces.mybatis.typeHandler;

import com.ces.mybatis.model.*;
import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

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

@MappedTypes({Context.class})
@MappedJdbcTypes({JdbcType.VARCHAR})
public class ContentTypeHandler implements TypeHandler<Context> {

	@Override
    public void setParameter(PreparedStatement preparedStatement, int i, Context context, JdbcType jdbcType) throws SQLException {
        String s = JSONObject.toJSONString(context);
        preparedStatement.setString(i, s);
    }

    @Override
    public Context getResult(ResultSet rs, String columnName) throws SQLException {
        String string = rs.getString(columnName);
        return JSONObject.parseObject(string, Context.class);
    }

    @Override
    public Context getResult(ResultSet rs, int columnIndex) throws SQLException {
        String string = rs.getString(columnIndex);
        return JSONObject.parseObject(string, Context.class);
    }

    @Override
    public Context getResult(CallableStatement cs, int columnIndex) throws SQLException {
        String string = cs.getString(columnIndex);
        return JSONObject.parseObject(string, Context.class);
    }
}
