use of com.github.pagehelper.PageException in project Mybatis-PageHelper by pagehelper.
the class PageAutoDialect method getDialect.
/**
* 根据 jdbcUrl 获取数据库方言
*
* @param ms
* @return
*/
private AbstractHelperDialect getDialect(MappedStatement ms) {
//改为对dataSource做缓存
DataSource dataSource = ms.getConfiguration().getEnvironment().getDataSource();
String url = getUrl(dataSource);
if (urlDialectMap.containsKey(url)) {
return urlDialectMap.get(url);
}
try {
lock.lock();
if (urlDialectMap.containsKey(url)) {
return urlDialectMap.get(url);
}
if (StringUtil.isEmpty(url)) {
throw new PageException("无法自动获取jdbcUrl,请在分页插件中配置dialect参数!");
}
String dialectStr = fromJdbcUrl(url);
if (dialectStr == null) {
throw new PageException("无法自动获取数据库类型,请通过 helperDialect 参数指定!");
}
AbstractHelperDialect dialect = initDialect(dialectStr, properties);
urlDialectMap.put(url, dialect);
return dialect;
} finally {
lock.unlock();
}
}
use of com.github.pagehelper.PageException 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.setExpression(new LongValue(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;
}
use of com.github.pagehelper.PageException in project Mybatis-PageHelper by pagehelper.
the class SqlServerParser method wrapSetOperationList.
/**
* 包装SetOperationList
*
* @param setOperationList
* @return
*/
protected SelectBody wrapSetOperationList(SetOperationList setOperationList) {
// 获取最后一个plainSelect
SelectBody setSelectBody = setOperationList.getSelects().get(setOperationList.getSelects().size() - 1);
if (!(setSelectBody instanceof PlainSelect)) {
throw new PageException("目前无法处理该SQL,您可以将该SQL发送给abel533@gmail.com协助作者解决!");
}
PlainSelect plainSelect = (PlainSelect) setSelectBody;
PlainSelect selectBody = new PlainSelect();
List<SelectItem> selectItems = getSelectItems(plainSelect);
selectBody.setSelectItems(selectItems);
// 设置fromIterm
SubSelect fromItem = new SubSelect();
fromItem.setSelectBody(setOperationList);
fromItem.setAlias(new Alias(WRAP_TABLE));
selectBody.setFromItem(fromItem);
// order by
if (isNotEmptyList(plainSelect.getOrderByElements())) {
selectBody.setOrderByElements(plainSelect.getOrderByElements());
plainSelect.setOrderByElements(null);
}
return selectBody;
}
use of com.github.pagehelper.PageException in project Mybatis-PageHelper by pagehelper.
the class OrderByParser method converToOrderBySql.
/**
* convert to order by sql
*
* @param sql
* @param orderBy
* @return
*/
public static String converToOrderBySql(String sql, String orderBy) {
// 解析SQL
Statement stmt = null;
try {
stmt = CCJSqlParserUtil.parse(sql);
Select select = (Select) stmt;
SelectBody selectBody = select.getSelectBody();
// 处理body-去最外层order by
List<OrderByElement> orderByElements = extraOrderBy(selectBody);
String defaultOrderBy = PlainSelect.orderByToString(orderByElements);
if (defaultOrderBy.indexOf('?') != -1) {
throw new PageException("原SQL[" + sql + "]中的order by包含参数,因此不能使用OrderBy插件进行修改!");
}
// 新的sql
sql = select.toString();
} catch (Throwable e) {
log.warn("处理排序失败: " + e + ",降级为直接拼接 order by 参数");
}
return sql + " order by " + orderBy;
}
Aggregations