You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ibatis.apache.org by "李义冬 (JIRA)" <ib...@incubator.apache.org> on 2006/07/27 03:47:14 UTC

[jira] Commented: (IBATIS-327) suggest: PaginatedDataList getCount() Implements

    [ http://issues.apache.org/jira/browse/IBATIS-327?page=comments#action_12423752 ] 
            
李义冬 commented on IBATIS-327:
----------------------------

public class PaginateSql implements Sql{
    private boolean count;
    private Sql sql;

    public PaginateSql(Sql sql) {
        this.sql = sql;
    }

    public String getSql(RequestScope request, Object parameterObject) {
        if (count) {
            return sql.getSql(request, parameterObject);
        } else {
            StringBuffer selSql = new StringBuffer("select count(*) from (");
            selSql.append(sql.getSql(request, parameterObject));  
            selSql.append(")");
            count = true;
            return selSql.toString();
        }
    }

    public ParameterMap getParameterMap(RequestScope request, Object parameterObject) {
        return sql.getParameterMap(request, parameterObject); 
    }

    public ResultMap getResultMap(RequestScope request, Object parameterObject) {
        return sql.getResultMap(request, parameterObject); 
    }

    public void cleanup(RequestScope request) {
        sql.cleanup(request);  
    }
}


> suggest: PaginatedDataList getCount() Implements
> ------------------------------------------------
>
>                 Key: IBATIS-327
>                 URL: http://issues.apache.org/jira/browse/IBATIS-327
>             Project: iBatis for Java
>          Issue Type: Wish
>          Components: SQL Maps
>         Environment: SqlMapExecutor.queryForPaginatedList(String id, Object paramObject, int pageSize)
> to PaginatedList, But i cann't obtain the total of query
>            Reporter: 李义冬
>
> <select id="getUser resultMap="user-result-List">
>         select * from user where sex = #sex#
> </select>
> I know, "select count(*) from (select * from user where sex = ?) so, add method getCount() to PaginatedList.java
>      public int getCount() {
>         if (count == -1) {
>             SqlMapClientImpl sqlMapClient = (SqlMapClientImpl) sqlMapExecutor;
>             GeneralStatement statement = (GeneralStatement) sqlMapClient.getMappedStatement(statementName);
>               RequestScope requestScope = new RequestScope();  //  i cann't obtains RequestScope from requestPool.
>             statement.initRequest(requestScope);
>             Sql sql = new PaginateSql(statement.getSql());
>             String selSql = sql.getSql(requestScope, parameterObject);
>             ParameterMap parameterMap = sql.getParameterMap(requestScope, parameterObject);
>             Object[] obj = parameterMap.getParameterObjectValues(requestScope, parameterObject);
>             Connection con = null;
>             PreparedStatement ps = null;
>             ResultSet result = null;
>             try {
>                 con = sqlMapClient.getDataSource().getConnection();
>                 ps = con.prepareStatement(selSql);
>                 parameterMap.setParameters(requestScope, ps, obj);
>                 result = ps.executeQuery();
>                 while (result.next()) {
>                     count = result.getInt(1);
>                 }
>             } catch (SQLException e) {
>                 e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
>             } finally {
>                 try {
>                     result.close();
>                 } catch (SQLException e) {
>                     // ignore
>                 }
>                 try {
>                     ps.close();
>                 } catch (SQLException e) {
>                     // ignore
>                 }
>                 try {
>                     con.close();
>                 } catch (SQLException e) {
>                     // ignore
>                 }
>             }
>         }
>         return count;
>     }
> I am very superficial to the framework understanding, this code is shoddy, but it can obtains count.  I thought you understand my meaning, anticipates your reply

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira