SqlBuilder

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

import org.apache.ibatis.jdbc.AbstractSQL;
import java.util.List;

public class SqlBuilder extends AbstractSQL {

    public SqlBuilder SELECT_AS(String column, String alias) {
        return SELECT(column + " AS " + alias);
    }

    public SqlBuilder SELECT_DISTINCT_AS(String column, String alias) {
        return SELECT(String.format("DISTINCT(%s) AS %s", column, alias));
    }

    public SqlBuilder SELECT_COUNT(String columns) {
        return SELECT("COUNT(" + columns + ")");
    }

    public SqlBuilder SELECT_COUNT() {
        return SELECT_COUNT("*");
    }

    public SqlBuilder FROM_AS(String table, String alias) {
        return FROM(table + " AS " + alias);
    }

    public SqlBuilder WHERE_LIKE(String column, String keyword) {
        return WHERE(column + " LIKE " + buildLikeCondition(keyword, true, true));
    }

    public SqlBuilder WHERE_LIKE_LEFT(String column, String keyword) {
        return WHERE(column + " LIKE " + buildLikeCondition(keyword, true, false));
    }

    public SqlBuilder WHERE_LIKE_RIGHT(String column, String keyword) {
        return WHERE(column + " LIKE " + buildLikeCondition(keyword, false, true));
    }

    public SqlBuilder WHERE_IN(String column, List list) {
        return WHERE(column + " IN " + buildArrayCondition(list));
    }

    public SqlBuilder WHERE_NOT_IN(String column, List list) {
        return WHERE(column + " NOT IN " + buildArrayCondition(list));
    }

    public SqlBuilder ORDER_BY_ASC(String columns) {
        return ORDER_BY(columns + " ASC");
    }

    public SqlBuilder ORDER_BY_DESC(String columns) {
        return ORDER_BY(columns + " DESC");
    }

    public String buildSql() {
        return toString();
    }

    public String buildSqlAppendLimit(int pageSize, int pageNum) {
        int offset = (pageNum - 1) * pageSize;
        return toString() + String.format(" LIMIT %d, %d", offset, pageSize) ;
    }

    @Override
    public SqlBuilder getSelf() {
        return this;
    }

    /**
     * 构造数组查询条件,用于"IN"或"NOT IN"条件查询
     * @param list 参数列表,不能为空,仅支持数字或字符串的条件构造
     * @return 构造结果,如(123,456,789)、('aa','bb','cc')
     */
    public static String buildArrayCondition(List list) {
        if (list == null || list.isEmpty()) {
            throw new IllegalArgumentException("args must not be empty");
        }

        StringBuilder arrayCondition = new StringBuilder("(");
        for (Object arg : list) {
            if (arg == null) {
                continue;
            }
            if (arg instanceof Number) {
                // 数字
                arrayCondition.append(arg);
            } else if (arg instanceof String) {
                // 字符串
                arrayCondition.append("'");
                arrayCondition.append(arg);
                arrayCondition.append("'");
            } else {
                // 其他不支持
                throw new IllegalArgumentException("unsupported args");
            }
            arrayCondition.append(',');
        }
        // 替换最后一个“,”为“)”
        arrayCondition.replace(arrayCondition.lastIndexOf(","), arrayCondition.length(), ")");

        return arrayCondition.toString();
    }

    public static String buildLikeCondition(String keyword) {
        return buildLikeCondition(keyword, true, true);
    }

    public static String buildLikeCondition(String keyword, boolean likePre, boolean likePost) {
        if (keyword == null) {
            throw new IllegalArgumentException("keyword must not be null");
        }
        if (!likePre && !likePost) {
            throw new IllegalArgumentException("either likePre or likePost should be true");
        }

        StringBuilder sb = new StringBuilder("CONCAT(");
        if (likePre) {
            sb.append("'%', ");
        }
        sb.append(keyword);
        if (likePost) {
            sb.append(", '%'");
        }
        sb.append(")");

        return sb.toString();
    }

}

转载于:https://my.oschina.net/u/812874/blog/863383

你可能感兴趣的:(SqlBuilder)