Search in sources :

Example 1 with Pager

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);
    }
}
Also used : Pager(org.nutz.dao.pager.Pager)

Example 2 with Pager

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()));
    }
}
Also used : Pager(org.nutz.dao.pager.Pager)

Example 3 with Pager

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);
}
Also used : Pager(org.nutz.dao.pager.Pager) NutSql(org.nutz.dao.impl.sql.NutSql) Sql(org.nutz.dao.sql.Sql) Test(org.junit.Test)

Example 4 with Pager

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;
}
Also used : Pager(org.nutz.dao.pager.Pager)

Example 5 with 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");
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CallableStatement(java.sql.CallableStatement) DaoStatement(org.nutz.dao.sql.DaoStatement) Pager(org.nutz.dao.pager.Pager) ValueAdaptor(org.nutz.dao.jdbc.ValueAdaptor) ResultSet(java.sql.ResultSet)

Aggregations

Pager (org.nutz.dao.pager.Pager)11 Test (org.junit.Test)2 Sql (org.nutz.dao.sql.Sql)2 CallableStatement (java.sql.CallableStatement)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 NutSql (org.nutz.dao.impl.sql.NutSql)1 ValueAdaptor (org.nutz.dao.jdbc.ValueAdaptor)1 DaoStatement (org.nutz.dao.sql.DaoStatement)1 PItem (org.nutz.dao.sql.PItem)1 Pet (org.nutz.dao.test.meta.Pet)1 PojoSql (org.nutz.dao.test.meta.issue1074.PojoSql)1 Issue1163Pet (org.nutz.dao.test.meta.issue1163.Issue1163Pet)1