use of org.nutz.dao.pager.Pager in project nutz by nutzam.
the class Sqlserver2005JdbcExpert method formatQuery.
@Override
public void formatQuery(Sql sql) {
Pager pager = sql.getContext().getPager();
// 需要进行分页
if (null != pager && pager.getPageNumber() > 0) {
// TODO XXX 这个写法灰常暴力!!But , it works!!!! 期待更好的写法
if (!sql.getSourceSql().toUpperCase().startsWith("SELECT "))
// 以免出错.
return;
String xSql = sql.getSourceSql().substring(6);
String pre = String.format("select * from(select row_number()over(order by __tc__)__rn__,* from(select top %d 0 __tc__, ", pager.getOffset() + pager.getPageSize());
String last = String.format(")t)tt where __rn__ > %d", pager.getOffset());
sql.setSourceSql(pre + xSql + last);
}
}
use of org.nutz.dao.pager.Pager in project nutz by nutzam.
the class Db2JdbcExpert method formatQuery.
public void formatQuery(Pojo pojo) {
Pager pager = pojo.getContext().getPager();
// 需要进行分页
if (null != pager && pager.getPageNumber() > 0) {
// 之前插入
pojo.insertFirst(Pojos.Items.wrap("SELECT * FROM (" + "SELECT ROW_NUMBER() OVER() AS ROWNUM, " + "T.* FROM ("));
// 之后插入
pojo.append(Pojos.Items.wrapf(") T) AS A WHERE ROWNUM BETWEEN %d AND %d", pager.getOffset() + 1, pager.getOffset() + pager.getPageSize()));
}
}
use of org.nutz.dao.pager.Pager in project nutz by nutzam.
the class CustomizedSqlsTest method test_cnd_pager.
@Test
public void test_cnd_pager() {
pojos.init();
Sql sql = Sqls.create("select * from t_pet $condition");
sql.setCondition(Cnd.where("name", "=", "wendal"));
Pager pager = dao.createPager(1, 20);
sql.setPager(pager);
dao.execute(sql);
}
use of org.nutz.dao.pager.Pager in project nutz by nutzam.
the class NutDao method createPager.
public Pager createPager(int pageNumber, int pageSize) {
Pager pager = new Pager();
pager.setPageNumber(pageNumber);
pager.setPageSize(pageSize);
return pager;
}
use of org.nutz.dao.pager.Pager in project nutz by nutzam.
the class NutDaoExecutor method _runSelect.
private void _runSelect(Connection conn, DaoStatement st) throws SQLException {
Object[][] paramMatrix = st.getParamMatrix();
// -------------------------------------------------
// 以下代码,就为了该死的游标分页!!
// -------------------------------------------------
int startRow = -1;
int lastRow = -1;
if (st.getContext().getResultSetType() == ResultSet.TYPE_SCROLL_INSENSITIVE) {
Pager pager = st.getContext().getPager();
if (pager != null) {
startRow = pager.getOffset();
lastRow = pager.getOffset() + pager.getPageSize();
}
}
// -------------------------------------------------
// 生成 Sql 语句
String sql = st.toPreparedStatement();
// 打印调试信息
ResultSet rs = null;
Statement stat = null;
try {
// 木有参数,直接运行
if (null == paramMatrix || paramMatrix.length == 0 || paramMatrix[0].length == 0) {
stat = conn.createStatement(st.getContext().getResultSetType(), ResultSet.CONCUR_READ_ONLY);
if (lastRow > 0)
// 游标分页,现在总行数
stat.setMaxRows(lastRow);
if (st.getContext().getFetchSize() != 0)
stat.setFetchSize(st.getContext().getFetchSize());
rs = stat.executeQuery(sql);
} else // 有参数,用缓冲语句
{
// 打印调试信息
if (paramMatrix.length > 1) {
if (log.isWarnEnabled())
log.warnf("Drop last %d rows parameters for:\n%s", paramMatrix.length - 1, st);
}
// 准备运行语句
ValueAdaptor[] adaptors = st.getAdaptors();
// 创建语句并设置参数
stat = conn.prepareStatement(sql, st.getContext().getResultSetType(), ResultSet.CONCUR_READ_ONLY);
if (lastRow > 0)
stat.setMaxRows(lastRow);
if (st.getContext().getFetchSize() != 0)
stat.setFetchSize(st.getContext().getFetchSize());
for (int i = 0; i < paramMatrix[0].length; i++) {
adaptors[i].set((PreparedStatement) stat, paramMatrix[0][i], i + 1);
}
rs = ((PreparedStatement) stat).executeQuery();
}
if (startRow > 0)
rs.absolute(startRow);
// 执行回调
st.onAfter(conn, rs, stat);
} finally {
Daos.safeClose(stat, rs);
}
// 打印更详细的调试信息
if (log.isTraceEnabled())
log.trace("...DONE");
}
Aggregations