pers.clare.hisql.service.SQLPageService Maven / Gradle / Ivy
The newest version!
package pers.clare.hisql.service;
import pers.clare.hisql.exception.HiSqlException;
import pers.clare.hisql.page.Page;
import pers.clare.hisql.page.Pagination;
import pers.clare.hisql.page.Sort;
import pers.clare.hisql.util.ConnectionUtil;
import pers.clare.hisql.util.ResultSetUtil;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
public abstract class SQLPageService extends SQLNextService {
public Page page(
Class clazz
, String sql
, Object... parameters
) {
return doPage(clazz, sql, null, parameters);
}
public Page page(
Class clazz
, String sql
, Sort sort
, Object... parameters
) {
return doPage(clazz, sql, toPagination(sort), parameters);
}
public Page page(
Class clazz
, String sql
, Pagination pagination
, Object... parameters
) {
return doPage(clazz, sql, pagination, parameters);
}
private Page doPage(
Class clazz
, String sql
, Pagination pagination
, Object... parameters
) {
pagination = getPagination(pagination);
if (pagination.getSize() == 0) return Page.empty(pagination);
String executeSql = buildPaginationSQL(pagination, sql);
Connection connection = null;
try {
connection = getConnection();
List list = ResultSetUtil.toList(getResultSetConverter(), ConnectionUtil.query(connection, executeSql, parameters), clazz);
return toPage(pagination, list, connection, sql, parameters);
} catch (HiSqlException e) {
throw e;
} catch (Exception e) {
throw new HiSqlException(executeSql, e);
} finally {
closeConnection(connection);
}
}
public Page