Search in sources :

Example 1 with GreaterThan

use of net.sf.jsqlparser.expression.operators.relational.GreaterThan in project Mybatis-PageHelper by pagehelper.

the class SqlServerParser method getPageSelect.

/**
     * 获取一个外层包装的TOP查询
     *
     * @param select
     * @return
     */
protected Select getPageSelect(Select select) {
    SelectBody selectBody = select.getSelectBody();
    if (selectBody instanceof SetOperationList) {
        selectBody = wrapSetOperationList((SetOperationList) selectBody);
    }
    //这里的selectBody一定是PlainSelect
    if (((PlainSelect) selectBody).getTop() != null) {
        throw new PageException("被分页的语句已经包含了Top,不能再通过分页插件进行分页查询!");
    }
    //获取查询列
    List<SelectItem> selectItems = getSelectItems((PlainSelect) selectBody);
    //对一层的SQL增加ROW_NUMBER()
    List<SelectItem> autoItems = new ArrayList<SelectItem>();
    SelectItem orderByColumn = addRowNumber((PlainSelect) selectBody, autoItems);
    //加入自动生成列
    ((PlainSelect) selectBody).addSelectItems(autoItems.toArray(new SelectItem[autoItems.size()]));
    //处理子语句中的order by
    processSelectBody(selectBody, 0);
    //中层子查询
    PlainSelect innerSelectBody = new PlainSelect();
    //PAGE_ROW_NUMBER
    innerSelectBody.addSelectItems(orderByColumn);
    innerSelectBody.addSelectItems(selectItems.toArray(new SelectItem[selectItems.size()]));
    //将原始查询作为内层子查询
    SubSelect fromInnerItem = new SubSelect();
    fromInnerItem.setSelectBody(selectBody);
    fromInnerItem.setAlias(PAGE_TABLE_ALIAS);
    innerSelectBody.setFromItem(fromInnerItem);
    //新建一个select
    Select newSelect = new Select();
    PlainSelect newSelectBody = new PlainSelect();
    //设置top
    Top top = new Top();
    top.setRowCount(Long.MAX_VALUE);
    newSelectBody.setTop(top);
    //设置order by
    List<OrderByElement> orderByElements = new ArrayList<OrderByElement>();
    OrderByElement orderByElement = new OrderByElement();
    orderByElement.setExpression(PAGE_ROW_NUMBER_COLUMN);
    orderByElements.add(orderByElement);
    newSelectBody.setOrderByElements(orderByElements);
    //设置where
    GreaterThan greaterThan = new GreaterThan();
    greaterThan.setLeftExpression(PAGE_ROW_NUMBER_COLUMN);
    greaterThan.setRightExpression(new LongValue(Long.MIN_VALUE));
    newSelectBody.setWhere(greaterThan);
    //设置selectItems
    newSelectBody.setSelectItems(selectItems);
    //设置fromIterm
    SubSelect fromItem = new SubSelect();
    //中层子查询
    fromItem.setSelectBody(innerSelectBody);
    fromItem.setAlias(PAGE_TABLE_ALIAS);
    newSelectBody.setFromItem(fromItem);
    newSelect.setSelectBody(newSelectBody);
    if (isNotEmptyList(select.getWithItemsList())) {
        newSelect.setWithItemsList(select.getWithItemsList());
    }
    return newSelect;
}
Also used : PageException(com.github.pagehelper.PageException) GreaterThan(net.sf.jsqlparser.expression.operators.relational.GreaterThan) LongValue(net.sf.jsqlparser.expression.LongValue)

Aggregations

PageException (com.github.pagehelper.PageException)1 LongValue (net.sf.jsqlparser.expression.LongValue)1 GreaterThan (net.sf.jsqlparser.expression.operators.relational.GreaterThan)1