com.huifu.mybatis.handler.LikeTypeHandler.java Source code

Java tutorial

Introduction

Here is the source code for com.huifu.mybatis.handler.LikeTypeHandler.java

Source

/*
 * Copyright 2001-2012 Software Engineering Center Chinese Academy of Sciences.
 * All rights reserved. SEC PROPRIETARY/CONFIDENTIAL. Use is subject to license
 * terms.
 */
package com.huifu.mybatis.handler;

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

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import com.huifu.base.LogFactory;

/**
 * ????. ??%xxxx%?. ???Oracle10g,SqlServer2005
 * 
 * <pre>
 * ?:
 * 
 * where ECTABLE_COLUMN_DETAIL.DISPLAY_NAME like  #{displayName,javaType=Like,jdbcType=VARCHAR} escape '\'
 * </pre>
 * 
 * @author zx.
 * @since 1.0
 * @version 1.0
 */
public class LikeTypeHandler extends BaseTypeHandler<String> {

    /**
     * ? %
     */
    static final String WILDCARD_PERCENT_SIGN = "%";
    /**
     * ? _
     */
    static final String WILDCARD_UNDERLINE = "_";

    /**
     * ? [
     */
    static final String WILDCARD_LEFT_BRACKETS = "[";

    /**
     * ? ]
     */
    static final String WILDCARD_RIGHT_BRACKETS = "]";

    /**
     *  \
     */
    static final String ECSAPE_SLASH = "\\";

    @Override
    public String getNullableResult(ResultSet arg0, String arg1) throws SQLException {
        return arg0.getString(arg1);
    }

    @Override
    public String getNullableResult(CallableStatement arg0, int arg1) throws SQLException {
        return arg0.getString(arg1);
    }

    @Override
    public String getNullableResult(ResultSet arg0, int arg1) throws SQLException {
        return arg0.getString(arg1);
    }

    @Override
    public void setNonNullParameter(PreparedStatement arg0, int arg1, String arg2, JdbcType arg3)
            throws SQLException {
        String likeParam = arg2;
        // ?
        likeParam = StringUtils.replace(likeParam, getEscape(), getEscape() + getEscape());
        // ?%
        likeParam = StringUtils.replace(likeParam, WILDCARD_PERCENT_SIGN, getEscape() + WILDCARD_PERCENT_SIGN);
        // ?_
        likeParam = StringUtils.replace(likeParam, WILDCARD_UNDERLINE, getEscape() + WILDCARD_UNDERLINE);

        // TODO [] .
        // ?[
        // likeParam = StringUtils.replace(likeParam, WILDCARD_LEFT_BRACKETS,
        // getEscape() + WILDCARD_LEFT_BRACKETS);
        // ?]
        // likeParam = StringUtils.replace(likeParam, WILDCARD_RIGHT_BRACKETS,
        // getEscape() + WILDCARD_RIGHT_BRACKETS);

        // ??%
        likeParam = WILDCARD_PERCENT_SIGN + likeParam + WILDCARD_PERCENT_SIGN;
        LogFactory.LOGGER_FRAMEWORK.debug("like param :" + likeParam);
        arg0.setString(arg1, likeParam);
        // arg0.setString(arg1, likeParam + " escape '\\'");
    }

    String getEscape() {
        return ECSAPE_SLASH;
    }

}