package cn.***.api.mappers.handler;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import cn.***.api.model.WechatTemplate;
import oracle.jdbc.OracleConnection;
import oracle.sql.ARRAY;
import oracle.sql.ArrayDescriptor;
import oracle.sql.STRUCT;
import oracle.sql.StructDescriptor;
public class WechatTemplateArrayTypeHandler extends BaseTypeHandler {
private final StringORA_OBJ_TYP="TYP_WECHAT_TEMPLATE";
private final String ORA_TAB_TYP="TAB_WECHAT_TEMPLATE";
@Override
public void setNonNullParameter(PreparedStatement ps,int i, Object o, JdbcType jdbcType) throws SQLException {
Connectionconn=null;
try{
if(null!=o) {
@SuppressWarnings("unchecked")
List list= (ArrayList)o;
conn=ps.getConnection();
if(conn.isWrapperFor(OracleConnection.class)) {
conn=conn.unwrap(OracleConnection.class);
ARRAYarray= getArray(conn,ORA_OBJ_TYP,ORA_TAB_TYP,list);
ps.setArray(i,array);
}
}
}catch(ClassNotFoundException e) {
e.printStackTrace();
}catch(Exception e) {
e.printStackTrace();
}
}
@Override
public Object
getNullableResult(ResultSet rs, String columnName)throwsSQLException {
return null;
}
@Override
public Object getNullableResult(ResultSet rs,int columnIndex)throws SQLException {
return null;
}
@Override
public Object getNullableResult(CallableStatement cs,int columnIndex)throws SQLException {
retur nnull;
}
private ARRAY getArray(Connection con, String OracleObj, String Oraclelist, List listData)
throws Exception{
ARRAY array=null;
ArrayDescriptor desc= ArrayDescriptor.createDescriptor(Oraclelist,con);
STRUCT[] structs=new STRUCT[listData.size()];
if(list Data!=null&&listData.size() > 0) {
StructDescriptor structdesc=new StructDescriptor(OracleObj,con);
for(int i= 0;i < listData.size(); i++) {
Object[]result= {listData.get(i).getId(),listData.get(i).getTemplateId(),
listData.get(i).getTitle(),listData.get(i).getPrimaryIndustry(),
listData.get(i).getDeputyIndustry(),listData.get(i).getParamNumber(),
listData.get(i).getContent(),listData.get(i).getExample(),listData.get(i).getStatus() };
structs[i] =newSTRUCT(structdesc,con,result);
}
array=new ARRAY(desc,con,structs);
}else{
array=new ARRAY(desc,con,structs);
}
return array;
}
}