package com.github.drtrang.typehandlers.type;

import com.github.drtrang.typehandlers.alias.Encrypt;
import com.github.drtrang.typehandlers.util.EncryptUtil;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;

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

/**
 * @author trang
 */
@MappedTypes(Encrypt.class)
public class EncryptHandler extends BaseTypeHandler<String> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType)
            throws SQLException {
        String p = EncryptUtil.encrypt(parameter);
        ps.setString(i, p);
    }

    @Override
    public String getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String r = rs.getString(columnName);
        return EncryptUtil.decrypt(r);
    }

    @Override
    public String getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String r = rs.getString(columnIndex);
        return EncryptUtil.decrypt(r);
    }

    @Override
    public String getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String r = cs.getString(columnIndex);
        return EncryptUtil.decrypt(r);
    }
}

@@ -414,16 +414,133 @@
 n;%0A%0A/**%0A
+ * %E6%8B%A6%E6%88%AA JavaType %E4%B8%BA encrypt %E7%9A%84 SQL%0A * %E6%B3%A8%E6%84%8F%EF%BC%9A%0A *   1. %E5%8A%A0/%E8%A7%A3%E5%AF%86%E7%9A%84%E5%AD%97%E6%AE%B5%E5%8F%AA%E8%BF%87%E6%BB%A4 null %E5%80%BC%EF%BC%8C%E7%A9%BA%E5%AD%97%E7%AC%A6%E4%B8%B2%E4%BE%9D%E7%84%B6%E4%BC%9A%E8%A2%AB%E5%8A%A0/%E8%A7%A3%E5%AF%86%0A *   2. fail fast %E6%A8%A1%E5%BC%8F%EF%BC%8C%E5%BD%93%E5%8A%A0/%E8%A7%A3%E5%AF%86%E5%A4%B1%E8%B4%A5%E6%97%B6%EF%BC%8C%E7%AB%8B%E5%8D%B3%E6%8A%9B%E5%87%BA%E5%BC%82%E5%B8%B8%0A *%0A
  * @auth
@@ -548,16 +548,16 @@
 r trang%0A
-
  */%0A@Map
@@ -805,39 +805,14 @@
     
-String p = EncryptUtil.encrypt(
+// %E5%8F%AA%E8%A6%81 
 para
@@ -816,18 +816,24 @@
 arameter
-);
+ %E9%9D%9E%E7%A9%BA%E9%83%BD%E8%BF%9B%E8%A1%8C%E5%8A%A0%E5%AF%86
 %0A       
@@ -849,17 +849,46 @@
 ring(i, 
-p
+EncryptUtil.encrypt(parameter)
 );%0A    %7D
@@ -1045,32 +1045,51 @@
 ;%0A        return
+ r == null ? null :
  EncryptUtil.dec
@@ -1259,32 +1259,51 @@
 ;%0A        return
+ r == null ? null :
  EncryptUtil.dec
@@ -1489,16 +1489,35 @@
   return
+ r == null ? null :
  Encrypt
@@ -1539,10 +1539,11 @@
 ;%0A    %7D%0A
+%0A
 %7D%0A

fix NPE