package com.mybatis.handle;



import com.mybatis.model.Context;
import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;

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 extends BaseTypeHandler<Context> {

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

    @Override
    public Context getNullableResult(ResultSet resultSet, String s) throws SQLException {
        String string = resultSet.getString(s);
        return JSONObject.parseObject(string, Context.class);
    }

    @Override
    public Context getNullableResult(ResultSet resultSet, int i) throws SQLException {
        String string = resultSet.getString(i);
        return JSONObject.parseObject(string, Context.class);
    }

    @Override
    public Context getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        String string = callableStatement.getString(i);
        return JSONObject.parseObject(string, Context.class);

    }
}
