pers.clare.hisql.service.SQLNextService Maven / Gradle / Ivy
The newest version!
package pers.clare.hisql.service;
import pers.clare.hisql.exception.HiSqlException;
import pers.clare.hisql.page.Next;
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.util.List;
import java.util.Map;
public abstract class SQLNextService extends SQLQueryService {
public static final Pagination DefaultPagination = Pagination.of(0, 20);
public Pagination getPagination(Pagination pagination) {
return pagination == null ? DefaultPagination : pagination;
}
protected Pagination toPagination(Sort sort) {
if (sort == null) {
return DefaultPagination;
} else {
return Pagination.of(DefaultPagination.getPage(), DefaultPagination.getSize(), sort.getSorts());
}
}
protected String buildPaginationSQL(Pagination pagination, String sql) {
return getPaginationMode().buildPaginationSQL(pagination, sql);
}
protected Next toNext(Pagination pagination, List list) {
return Next.of(pagination.getPage(), pagination.getSize(), list);
}
public Next next(
Class clazz
, String sql
, Object... parameters
) {
return doNext(clazz, sql, null, parameters);
}
public Next next(
Class clazz
, String sql
, Sort sort
, Object... parameters
) {
return doNext(clazz, sql, toPagination(sort), parameters);
}
public Next next(
Class clazz
, String sql
, Pagination pagination
, Object... parameters
) {
return doNext(clazz, sql, pagination, parameters);
}
private Next doNext(
Class clazz
, String sql
, Pagination pagination
, Object... parameters
) {
pagination = getPagination(pagination);
if (pagination.getSize() == 0) return Next.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 toNext(pagination, list);
} catch (HiSqlException e) {
throw e;
} catch (Exception e) {
throw new HiSqlException(executeSql, e);
} finally {
closeConnection(connection);
}
}
public Next