Search in sources :

Example 1 with Match

use of com.baidu.unbiz.common.genericdao.operator.Match 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)

Example 2 with Match

use of com.baidu.unbiz.common.genericdao.operator.Match in project disconf by knightliao.

the class ConfigDaoImpl method getConfigList.

/**
     *
     */
@Override
public List<Config> getConfigList(Long appId, Long envId, String version, Boolean hasValue) {
    List<Match> matchs = new ArrayList<Match>();
    matchs.add(new Match(Columns.APP_ID, appId));
    matchs.add(new Match(Columns.ENV_ID, envId));
    matchs.add(new Match(Columns.VERSION, version));
    matchs.add(new Match(Columns.STATUS, Constants.STATUS_NORMAL));
    if (hasValue) {
        return find(matchs, new ArrayList<Order>());
    } else {
        return findColumns(matchs, new String[] { Columns.CONFIG_ID, Columns.TYPE, Columns.NAME, Columns.CREATE_TIME, Columns.UPDATE_TIME, Columns.STATUS, Columns.APP_ID, Columns.ENV_ID, Columns.VERSION });
    }
}
Also used : Order(com.baidu.unbiz.common.genericdao.operator.Order) ArrayList(java.util.ArrayList) Match(com.baidu.unbiz.common.genericdao.operator.Match)

Example 3 with Match

use of com.baidu.unbiz.common.genericdao.operator.Match in project disconf by knightliao.

the class ConfigDaoImpl method getConfigList.

/**
     *
     */
@Override
public DaoPageResult<Config> getConfigList(Long appId, Long envId, String version, Page page) {
    DaoPage daoPage = DaoUtils.daoPageAdapter(page);
    List<Match> matchs = new ArrayList<Match>();
    matchs.add(new Match(Columns.APP_ID, appId));
    matchs.add(new Match(Columns.ENV_ID, envId));
    matchs.add(new Match(Columns.VERSION, version));
    matchs.add(new Match(Columns.STATUS, Constants.STATUS_NORMAL));
    return page2(matchs, daoPage);
}
Also used : ArrayList(java.util.ArrayList) DaoPage(com.baidu.ub.common.db.DaoPage) Match(com.baidu.unbiz.common.genericdao.operator.Match)

Example 4 with Match

use of com.baidu.unbiz.common.genericdao.operator.Match in project disconf by knightliao.

the class QueryGenerator method getKeyMatches.

/**
     * 获取主键匹配的条件
     *
     * @param id
     *
     * @return 上午9:45:33 created by Darwin(Tianxin)
     */
public List<Match> getKeyMatches(KEY id) {
    Set<String> keyColumn = orMapping.getKeyColumn();
    List<Match> matches = new ArrayList<Match>(keyColumn.size());
    for (String column : keyColumn) {
        Object value = getKeyColumnValue(column, id);
        matches.add(new Match(column, value));
    }
    return matches;
}
Also used : ArrayList(java.util.ArrayList) BaseObject(com.github.knightliao.apollo.db.bo.BaseObject) Match(com.baidu.unbiz.common.genericdao.operator.Match)

Example 5 with Match

use of com.baidu.unbiz.common.genericdao.operator.Match in project disconf by knightliao.

the class QueryGenerator method getUpdateQuery.

public List<Query> getUpdateQuery(List<ENTITY> entities) {
    List<Query> queries = new ArrayList<Query>(entities.size());
    Set<String> modifiableColumns = orMapping.getModifiableColumns();
    for (ENTITY entity : entities) {
        List<Match> matches = getKeyMatches(entity.getId());
        List<Modify> modifies = new ArrayList<Modify>(modifiableColumns.size());
        for (String column : modifiableColumns) {
            Object value = getColumnValue(column, entity);
            modifies.add(new Modify(column, value));
        }
        queries.add(getUpdateQuery(modifies, matches));
    }
    return queries;
}
Also used : Query(com.baidu.unbiz.common.genericdao.operator.Query) ArrayList(java.util.ArrayList) Modify(com.baidu.unbiz.common.genericdao.operator.Modify) BaseObject(com.github.knightliao.apollo.db.bo.BaseObject) Match(com.baidu.unbiz.common.genericdao.operator.Match)

Aggregations

Match (com.baidu.unbiz.common.genericdao.operator.Match)5 ArrayList (java.util.ArrayList)4 BaseObject (com.github.knightliao.apollo.db.bo.BaseObject)3 DaoPage (com.baidu.ub.common.db.DaoPage)1 Modify (com.baidu.unbiz.common.genericdao.operator.Modify)1 Order (com.baidu.unbiz.common.genericdao.operator.Order)1 Query (com.baidu.unbiz.common.genericdao.operator.Query)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 Collection (java.util.Collection)1