Search in sources :

Example 1 with PageException

use of com.github.pagehelper.PageException in project Mybatis-PageHelper by pagehelper.

the class SqlServerParser method getOrderByElements.

/**
 * 获取新的排序列表
 *
 * @param plainSelect 原始查询
 * @param autoItems   生成的新查询要素
 * @return 新的排序列表
 */
protected List<OrderByElement> getOrderByElements(PlainSelect plainSelect, List<SelectItem> autoItems) {
    List<OrderByElement> orderByElements = plainSelect.getOrderByElements();
    ListIterator<OrderByElement> iterator = orderByElements.listIterator();
    OrderByElement orderByElement;
    // 非 `*` 且 非 `t.*` 查询列集合
    Map<String, SelectExpressionItem> selectMap = new HashMap<String, SelectExpressionItem>();
    // 别名集合
    Set<String> aliases = new HashSet<String>();
    // 是否包含 `*` 查询列
    boolean allColumns = false;
    // `t.*` 查询列的表名集合
    Set<String> allColumnsTables = new HashSet<String>();
    for (SelectItem item : plainSelect.getSelectItems()) {
        if (item instanceof SelectExpressionItem) {
            SelectExpressionItem expItem = (SelectExpressionItem) item;
            selectMap.put(expItem.getExpression().toString(), expItem);
            Alias alias = expItem.getAlias();
            if (alias != null) {
                aliases.add(alias.getName());
            }
        } else if (item instanceof AllColumns) {
            allColumns = true;
        } else if (item instanceof AllTableColumns) {
            allColumnsTables.add(((AllTableColumns) item).getTable().getName());
        }
    }
    // 开始遍历 OrderByElement 列表
    int aliasNo = 1;
    while (iterator.hasNext()) {
        orderByElement = iterator.next();
        Expression expression = orderByElement.getExpression();
        SelectExpressionItem selectExpressionItem = selectMap.get(expression.toString());
        if (selectExpressionItem != null) {
            // OrderByElement 在查询列表中
            Alias alias = selectExpressionItem.getAlias();
            if (alias != null) {
                // 查询列含有别名时用查询列别名
                iterator.set(cloneOrderByElement(orderByElement, alias.getName()));
            } else {
                // 查询列不包含别名
                if (expression instanceof Column) {
                    // 查询列为普通列,这时因为列在嵌套查询外时名称中不包含表名,故去除排序列的表名引用
                    // 例(仅为解释此处逻辑,不代表最终分页结果):
                    // SELECT TEST.A FROM TEST ORDER BY TEST.A
                    // -->
                    // SELECT A FROM (SELECT TEST.A FROM TEST) ORDER BY A
                    ((Column) expression).setTable(null);
                } else {
                    // 而为列增加别名是非常简单的,故此要求排序复杂列必须使用别名
                    throw new PageException("列 \"" + expression + "\" 需要定义别名");
                }
            }
        } else {
            // OrderByElement 不在查询列表中,需要自动生成一个查询列
            if (expression instanceof Column) {
                // OrderByElement 为普通列
                Table table = ((Column) expression).getTable();
                if (table == null) {
                    // 表名为空
                    if (allColumns || (allColumnsTables.size() == 1 && plainSelect.getJoins() == null) || aliases.contains(((Column) expression).getColumnName())) {
                        // 此时排序列其实已经包含在查询列表中了,不需做任何操作
                        continue;
                    }
                } else {
                    // 表名不为空
                    String tableName = table.getName();
                    if (allColumns || allColumnsTables.contains(tableName)) {
                        // 包含`*`查询列 或者 包含特定的`t.*`列
                        // 此时排序列其实已经包含在查询列表中了,只需去除排序列的表名引
                        ((Column) expression).setTable(null);
                        continue;
                    }
                }
            }
            // 将排序列加入查询列中
            String aliasName = PAGE_COLUMN_ALIAS_PREFIX + aliasNo++;
            SelectExpressionItem item = new SelectExpressionItem();
            item.setExpression(expression);
            item.setAlias(new Alias(aliasName));
            autoItems.add(item);
            iterator.set(cloneOrderByElement(orderByElement, aliasName));
        }
    }
    return orderByElements;
}
Also used : PageException(com.github.pagehelper.PageException) Table(net.sf.jsqlparser.schema.Table) Expression(net.sf.jsqlparser.expression.Expression) Column(net.sf.jsqlparser.schema.Column) Alias(net.sf.jsqlparser.expression.Alias)

Example 2 with PageException

use of com.github.pagehelper.PageException in project Mybatis-PageHelper by pagehelper.

the class SqlServerParser method convertToPageSql.

/**
 * 转换为分页语句
 *
 * @param sql
 * @param offset
 * @param limit
 * @return
 */
public String convertToPageSql(String sql, Integer offset, Integer limit) {
    // 解析SQL
    Statement stmt;
    try {
        stmt = CCJSqlParserUtil.parse(sql);
    } catch (Throwable e) {
        throw new PageException("不支持该SQL转换为分页查询!", e);
    }
    if (!(stmt instanceof Select)) {
        throw new PageException("分页语句必须是Select查询!");
    }
    // 获取分页查询的select
    Select pageSelect = getPageSelect((Select) stmt);
    String pageSql = pageSelect.toString();
    // 缓存移到外面了,所以不替换参数
    if (offset != null) {
        pageSql = pageSql.replace(START_ROW, String.valueOf(offset));
    }
    if (limit != null) {
        pageSql = pageSql.replace(PAGE_SIZE, String.valueOf(limit));
    }
    return pageSql;
}
Also used : PageException(com.github.pagehelper.PageException) Statement(net.sf.jsqlparser.statement.Statement)

Example 3 with PageException

use of com.github.pagehelper.PageException in project Mybatis-PageHelper by pagehelper.

the class PageObjectUtil method getPageFromObject.

/**
 * 对象中获取分页参数
 *
 * @param params
 * @return
 */
public static <T> Page<T> getPageFromObject(Object params, boolean required) {
    if (params == null) {
        throw new PageException("无法获取分页查询参数!");
    }
    if (params instanceof IPage) {
        IPage pageParams = (IPage) params;
        Page page = null;
        if (pageParams.getPageNum() != null && pageParams.getPageSize() != null) {
            page = new Page(pageParams.getPageNum(), pageParams.getPageSize());
        }
        if (StringUtil.isNotEmpty(pageParams.getOrderBy())) {
            if (page != null) {
                page.setOrderBy(pageParams.getOrderBy());
            } else {
                page = new Page();
                page.setOrderBy(pageParams.getOrderBy());
                page.setOrderByOnly(true);
            }
        }
        return page;
    }
    int pageNum;
    int pageSize;
    MetaObject paramsObject = null;
    if (hasRequest && requestClass.isAssignableFrom(params.getClass())) {
        try {
            paramsObject = MetaObjectUtil.forObject(getParameterMap.invoke(params, new Object[] {}));
        } catch (Exception e) {
        // 忽略
        }
    } else {
        paramsObject = MetaObjectUtil.forObject(params);
    }
    if (paramsObject == null) {
        throw new PageException("分页查询参数处理失败!");
    }
    Object orderBy = getParamValue(paramsObject, "orderBy", false);
    boolean hasOrderBy = false;
    if (orderBy != null && orderBy.toString().length() > 0) {
        hasOrderBy = true;
    }
    try {
        Object _pageNum = getParamValue(paramsObject, "pageNum", required);
        Object _pageSize = getParamValue(paramsObject, "pageSize", required);
        if (_pageNum == null || _pageSize == null) {
            if (hasOrderBy) {
                Page page = new Page();
                page.setOrderBy(orderBy.toString());
                page.setOrderByOnly(true);
                return page;
            }
            return null;
        }
        pageNum = Integer.parseInt(String.valueOf(_pageNum));
        pageSize = Integer.parseInt(String.valueOf(_pageSize));
    } catch (NumberFormatException e) {
        throw new PageException("分页参数不是合法的数字类型!", e);
    }
    Page page = new Page(pageNum, pageSize);
    // count查询
    Object _count = getParamValue(paramsObject, "count", false);
    if (_count != null) {
        page.setCount(Boolean.valueOf(String.valueOf(_count)));
    }
    // 排序
    if (hasOrderBy) {
        page.setOrderBy(orderBy.toString());
    }
    // 分页合理化
    Object reasonable = getParamValue(paramsObject, "reasonable", false);
    if (reasonable != null) {
        page.setReasonable(Boolean.valueOf(String.valueOf(reasonable)));
    }
    // 查询全部
    Object pageSizeZero = getParamValue(paramsObject, "pageSizeZero", false);
    if (pageSizeZero != null) {
        page.setPageSizeZero(Boolean.valueOf(String.valueOf(pageSizeZero)));
    }
    return page;
}
Also used : PageException(com.github.pagehelper.PageException) IPage(com.github.pagehelper.IPage) MetaObject(org.apache.ibatis.reflection.MetaObject) IPage(com.github.pagehelper.IPage) Page(com.github.pagehelper.Page) MetaObject(org.apache.ibatis.reflection.MetaObject) PageException(com.github.pagehelper.PageException)

Example 4 with PageException

use of com.github.pagehelper.PageException in project Mybatis-PageHelper by pagehelper.

the class PageAutoDialect method instanceDialect.

/**
 * 初始化 helper
 *
 * @param dialectClass
 * @param properties
 */
public static AbstractHelperDialect instanceDialect(String dialectClass, Properties properties) {
    AbstractHelperDialect dialect;
    if (StringUtil.isEmpty(dialectClass)) {
        throw new PageException("使用 PageHelper 分页插件时,必须设置 helper 属性");
    }
    try {
        Class sqlDialectClass = resloveDialectClass(dialectClass);
        if (AbstractHelperDialect.class.isAssignableFrom(sqlDialectClass)) {
            dialect = (AbstractHelperDialect) sqlDialectClass.newInstance();
        } else {
            throw new PageException("使用 PageHelper 时,方言必须是实现 " + AbstractHelperDialect.class.getCanonicalName() + " 接口的实现类!");
        }
    } catch (Exception e) {
        throw new PageException("初始化 helper [" + dialectClass + "]时出错:" + e.getMessage(), e);
    }
    dialect.setProperties(properties);
    return dialect;
}
Also used : PageException(com.github.pagehelper.PageException) AbstractHelperDialect(com.github.pagehelper.dialect.AbstractHelperDialect) SQLException(java.sql.SQLException) PageException(com.github.pagehelper.PageException)

Example 5 with PageException

use of com.github.pagehelper.PageException in project Mybatis-PageHelper by pagehelper.

the class PageAutoDialect method initDialect.

/**
     * 初始化 helper
     *
     * @param dialectClass
     * @param properties
     */
private AbstractHelperDialect initDialect(String dialectClass, Properties properties) {
    AbstractHelperDialect dialect;
    if (StringUtil.isEmpty(dialectClass)) {
        throw new PageException("使用 PageHelper 分页插件时,必须设置 helper 属性");
    }
    try {
        Class sqlDialectClass = resloveDialectClass(dialectClass);
        if (AbstractHelperDialect.class.isAssignableFrom(sqlDialectClass)) {
            dialect = (AbstractHelperDialect) sqlDialectClass.newInstance();
        } else {
            throw new PageException("使用 PageHelper 时,方言必须是实现 " + AbstractHelperDialect.class.getCanonicalName() + " 接口的实现类!");
        }
    } catch (Exception e) {
        throw new PageException("初始化 helper [" + dialectClass + "]时出错:" + e.getMessage(), e);
    }
    dialect.setProperties(properties);
    return dialect;
}
Also used : PageException(com.github.pagehelper.PageException) AbstractHelperDialect(com.github.pagehelper.dialect.AbstractHelperDialect) SQLException(java.sql.SQLException) PageException(com.github.pagehelper.PageException)

Aggregations

PageException (com.github.pagehelper.PageException)9 AbstractHelperDialect (com.github.pagehelper.dialect.AbstractHelperDialect)3 SQLException (java.sql.SQLException)2 Alias (net.sf.jsqlparser.expression.Alias)2 Statement (net.sf.jsqlparser.statement.Statement)2 IPage (com.github.pagehelper.IPage)1 Page (com.github.pagehelper.Page)1 DataSource (javax.sql.DataSource)1 Expression (net.sf.jsqlparser.expression.Expression)1 LongValue (net.sf.jsqlparser.expression.LongValue)1 GreaterThan (net.sf.jsqlparser.expression.operators.relational.GreaterThan)1 Column (net.sf.jsqlparser.schema.Column)1 Table (net.sf.jsqlparser.schema.Table)1 MetaObject (org.apache.ibatis.reflection.MetaObject)1