Search in sources :

Example 1 with LteParam

use of com.baidu.unbiz.common.genericdao.param.LteParam in project disconf by knightliao.

the class QueryGenerator method appendMatches.

public void appendMatches(Collection<Match> matches, StringBuilder sb, List<Object> params) {
    for (Match match : matches) {
        Object value = match.getValue();
        String column = match.getColumn();
        if (value == null) {
            sb.append(column).append(" is null");
        } else if (value instanceof LikeParam) {
            sb.append(column).append(" like ?");
            params.add('%' + ((LikeParam) value).getWord() + '%');
        } else if (value instanceof BetweenParam) {
            BetweenParam bp = (BetweenParam) value;
            Object start = bp.getStart();
            Object end = bp.getEnd();
            if (start == null && end == null) {
                sb.append("1!=1");
            } else if (start == null) {
                sb.append(column).append(" <= ?");
                params.add(end);
            } else if (end == null) {
                sb.append(column).append(" >= ?");
                params.add(start);
            } else {
                sb.append(column).append(" between ? and ?");
                params.add(start);
                params.add(end);
            }
        } else if (value instanceof GteParam) {
            sb.append(column).append(" >= ?");
            params.add(((GteParam) value).getValue());
        } else if (value instanceof GreaterThanParam) {
            sb.append(column).append(" > ?");
            params.add(((GreaterThanParam) value).getValue());
        } else if (value instanceof LteParam) {
            sb.append(column).append(" <= ?");
            params.add(((LteParam) value).getValue());
        } else if (value instanceof LessThanParam) {
            sb.append(column).append(" < ?");
            params.add(((LessThanParam) value).getValue());
        } else if (value instanceof ExpressionParam) {
            sb.append('(').append(column).append(')');
        } else {
            // 如果是取反的参数
            boolean reverse = value instanceof NotParam;
            if (reverse) {
                value = ((NotParam) value).getValue();
            }
            if (value == null && reverse) {
                sb.append(column).append(" is not null");
            } else if (value instanceof Collection<?>) {
                Collection<?> coll = (Collection<?>) value;
                if (coll.size() == 0) {
                    sb.append(reverse ? "1=1" : "1!=1");
                } else if (coll.size() == 1) {
                    sb.append(column).append(reverse ? "!=?" : "=?");
                    params.add(coll.iterator().next());
                } else {
                    sb.append(column).append(reverse ? " not in " : " in");
                    sb.append(connectObjects(coll));
                    params.addAll(coll);
                }
            } else if (value.getClass().isArray()) {
                Object[] array = (Object[]) value;
                if (array.length == 0) {
                    sb.append(reverse ? "1=1" : "1!=1");
                } else if (array.length == 1) {
                    sb.append(column).append(reverse ? "!=?" : "=?");
                    params.add(array[0]);
                } else {
                    sb.append(column).append(reverse ? " not in " : " in");
                    sb.append(connectObjects(Arrays.asList(array)));
                    for (Object o : array) {
                        params.add(o);
                    }
                }
            } else {
                sb.append(column).append(reverse ? "!=?" : "=?");
                params.add(value);
            }
        }
        sb.append(" and ");
    }
    sb.delete(sb.length() - 5, sb.length());
}
Also used : GteParam(com.baidu.unbiz.common.genericdao.param.GteParam) GreaterThanParam(com.baidu.unbiz.common.genericdao.param.GreaterThanParam) LikeParam(com.baidu.unbiz.common.genericdao.param.LikeParam) LteParam(com.baidu.unbiz.common.genericdao.param.LteParam) NotParam(com.baidu.unbiz.common.genericdao.param.NotParam) ExpressionParam(com.baidu.unbiz.common.genericdao.param.ExpressionParam) Match(com.baidu.unbiz.common.genericdao.operator.Match) BetweenParam(com.baidu.unbiz.common.genericdao.param.BetweenParam) LessThanParam(com.baidu.unbiz.common.genericdao.param.LessThanParam) Collection(java.util.Collection) BaseObject(com.github.knightliao.apollo.db.bo.BaseObject)

Aggregations

Match (com.baidu.unbiz.common.genericdao.operator.Match)1 BetweenParam (com.baidu.unbiz.common.genericdao.param.BetweenParam)1 ExpressionParam (com.baidu.unbiz.common.genericdao.param.ExpressionParam)1 GreaterThanParam (com.baidu.unbiz.common.genericdao.param.GreaterThanParam)1 GteParam (com.baidu.unbiz.common.genericdao.param.GteParam)1 LessThanParam (com.baidu.unbiz.common.genericdao.param.LessThanParam)1 LikeParam (com.baidu.unbiz.common.genericdao.param.LikeParam)1 LteParam (com.baidu.unbiz.common.genericdao.param.LteParam)1 NotParam (com.baidu.unbiz.common.genericdao.param.NotParam)1 BaseObject (com.github.knightliao.apollo.db.bo.BaseObject)1 Collection (java.util.Collection)1