我在使用valuelist时发现它并没有提供针对oracle的分页(没有取当前页的数据集)功能。于是我写了一个关于oracle的adapter
程序如下:
DefaultDynclassOracleAdapter.java
/**
* Copyright (c) 2004 RiseSoft
*
* gf7@163.com
*/
package net.risesoft.risereport.common.adapter.jdbc;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import net.mlw.vlh.ValueListInfo;
import net.risesoft.risereport.common.adapter.jdbc.OracleJdbcAdapter;
import net.mlw.vlh.adapter.jdbc.util.ResultSetMapGenerator;
import dynclass.BeanCreator;
/**
* @see net.mlw.vlh.adapter.jdbc.AbstractDynaJdbcAdapter
* @see net.mlw.vlh.adapter.jdbc.AbstractJdbcAdapter
*
* @author gf
* @version $Revision: 1.1 $ $Date: 2005/06/21 08:18:42 $
*/
public class DefaultDynclassOracleAdapter extends OracleJdbcAdapter
{
/**
* @see net.mlw.vlh.adapter.jdbc.AbstractJdbcAdapter#processResultSet(java.sql.ResultSet)
*/
public List processResultSet(String name, ResultSet result, int numberPerPage, ValueListInfo info) throws SQLException
{
List list = new ArrayList();
ResultSetMapGenerator bg = new ResultSetMapGenerator(result, useName, lowerCase);
for (int i = 0; result.next() && i < numberPerPage; i++)
{
try
{
Map properties = bg.generateMap();
list.add(BeanCreator.createBeanFromMap(properties));
}
catch (Exception e)
{
LOGGER.error(e);
}
}
return list;
}
}
AbstractOracleJdbcAdapter.java
/**
* Copyright (c) 2004 RiseSoft
*
* gf7@163.com
*/
package net.risesoft.risereport.common.adapter.jdbc;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import javax.sql.DataSource;
import net.mlw.vlh.DefaultListBackedValueList;
import net.mlw.vlh.ValueList;
import net.mlw.vlh.ValueListInfo;
import net.mlw.vlh.adapter.AbstractValueListAdapter;
import net.mlw.vlh.adapter.jdbc.objectWrapper.ResultSetDecorator;
import net.mlw.vlh.adapter.jdbc.util.JdbcUtil;
import net.mlw.vlh.adapter.jdbc.util.StandardStatementBuilder;
import net.mlw.vlh.adapter.jdbc.util.StatementBuilder;
import net.mlw.vlh.adapter.util.ObjectValidator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* @see net.mlw.vlh.adapter.jdbc.objectWrapper.ResultSetDecorator
* @author gf
* @version $Revision: 1.2 $ $Date: 2005/07/10 03:58:55 $
*/
public abstract class AbstractOracleJdbcAdapter extends AbstractValueListAdapter
{
/** Commons logger. */
protected static final Log LOGGER = LogFactory
.getLog(AbstractOracleJdbcAdapter.class);
/** The sql.DataSource. * */
protected DataSource dataSource;
/** The sql to execute. * */
protected String sql;
/** The countSql to execute. * */
protected String countSql;
private boolean showSql = true;
/** The StatementBuilder to help generate a sql query. * */
protected StatementBuilder statementBuilder = new StandardStatementBuilder();
/**
* The validator for ResultSet's records.
*/
private ObjectValidator _validator = null;
public AbstractOracleJdbcAdapter()
{
}
/**
* @see net.mlw.vlh.ValueListAdapter#getValueList(java.lang.String,
* net.mlw.vlh.ValueListInfo)
*/
public ValueList getValueList(String name, ValueListInfo info)
{
// System.out.println("start date:"+new Date());
if (info.getSortingColumn() == null)
{
info.setPrimarySortColumn(getDefaultSortColumn());
info.setPrimarySortDirection(getDefaultSortDirectionInteger());
}
if (info.getPagingNumberPer() == Integer.MAX_VALUE)
{
info.setPagingNumberPer(getDefaultNumberPerPage());
}
ResultSet result = null;
Connection connection = null;
PreparedStatement statement = null;
try
{ StringBuffer countQuery=null;
StringBuffer query = new StringBuffer(sql);
connection = dataSource.getConnection();
boolean doSqlPaging = !((getAdapterType() & DO_PAGE) == 2);
//gf分页只读一页数据
if(countSql!=null && countSql.trim().length()>0){
countQuery=new StringBuffer(countSql);
}else{
countQuery=new StringBuffer("select count(*) from(").append(sql).append(")");
}
int pageNumber = info.getPagingPage();
int numberPerPage = (info.getPagingNumberPer() > 0) ? info
.getPagingNumberPer() : getDefaultNumberPerPage();
if (doSqlPaging)
{
int firstRecord = (pageNumber - 1) * numberPerPage;
StringBuffer pagingSelect=new StringBuffer("");
pagingSelect.append("select * from ( select row_.*,rownum rownum_ from ( ");
pagingSelect.append(query.toString());
pagingSelect.append(" ) row_ where rownum < ").append(firstRecord+numberPerPage+1)
.append(" ) where rownum_ > ").append(firstRecord);
query=pagingSelect;
}
statement = statementBuilder.generate(connection, query, info
.getFilters(), doSqlPaging);
//LOGGER.debug(query.toString());
if (showSql)
{
System.out.println("sql: " + query.toString());
}
// System.out.println("1:"+new Date());
result = getResultSet(statement, info);
// System.out.println("2"+new Date());
List list = processResultSet(name, result,
(doSqlPaging) ? numberPerPage : Integer.MAX_VALUE, info);
// System.out.println("3"+new Date()+"countQuery="+countQuery);
statement = statementBuilder.generate(connection, countQuery, info
.getFilters(), doSqlPaging);
// System.out.println("4:"+new Date()+"countQuery="+countQuery);
result=statement.executeQuery();
// System.out.println("5"+new Date());
if (result.next()){
info.setTotalNumberOfEntries(result.getInt(1));
}else{
info.setTotalNumberOfEntries(0);
}
// System.out.println("end date:"+new Date());
return new DefaultListBackedValueList(list, info);
} catch (Exception e)
{
e.printStackTrace();
throw new RuntimeException(e);
} finally
{
JdbcUtil.close(result, statement, connection);
}
}
/**
* @param statement
* @param ValueListInfo This info will be set to validator.
* @return ResultSet (validator is null) or ResultSetDecorator (validator is
* not null)
* @throws SQLException
* @see net.mlw.vlh.adapter.util.ObjectValidator
* @see net.mlw.vlh.adapter.jdbc.objectWrapper.ResultSetDecorator
*/
private ResultSet getResultSet(PreparedStatement statement,
ValueListInfo info) throws SQLException
{
if (_validator == null)
{
return statement.executeQuery();
}
else
{
_validator.setValueListInfo(info);
return new ResultSetDecorator(statement.executeQuery(), _validator);
}
}
/**
* This method takes the result and puts the VOs in the List.
*
* @param result The ResultSet to interate through.
* @param info is ussually constant during this process, you can use it for
* passing additional parameters from controler. (Like in
* DefaultWrapperAdapter)
* @return The List of VOs.
*/
public abstract List processResultSet(String name, ResultSet result,
int numberPerPage, ValueListInfo info) throws SQLException;
/**
* @param dataSource The dataSource to set.
*/
public void setDataSource(DataSource dataSource)
{
this.dataSource = dataSource;
}
/**
* @param sql The sql to set.
*/
public void setSql(String sql)
{
this.sql = sql;
}
/**
* @param countSql The sql to set.
*/
public void setCountSql(String countSql)
{
this.countSql = countSql;
}
/**
* @param statementBuilder The statementBuilder to set.
*/
public void setStatementBuilder(StatementBuilder statementBuilder)
{
this.statementBuilder = statementBuilder;
}
/**
* @param showSql The showSql to set.
*/
public void setShowSql(boolean showSql)
{
this.showSql = showSql;
}
/**
* <p>
* If is set to not null value, it uses a special
* <code>ResultsSetDecorator<code>, that enable or
* disable filtering objects in the final valuelist.
* </p>
* <h4>NOTE:</h4>
* <p>
* It respects the total count of entries that overlap your paged
* list. Simply spoken it supports features such as paging.
* </p>
* @param objectValidator The objectValidator to set.
* The null value means that validator is disabled.
* @see net.mlw.vlh.adapter.util.ObjectValidator
* @see net.mlw.vlh.adapter.jdbc.objectWrapper.ResultSetDecorator
*/
public void setValidator(ObjectValidator objectValidator)
{
this._validator = objectValidator;
}
}
OracleJdbcAdapter.java
/**
* Copyright (c) 2004 RiseSoft
*
* gf7@163.com
*/
package net.risesoft.risereport.common.adapter.jdbc;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* This adapter handles the standard functionality of creating a query and
* execution it...
*
* net.mlw.vlh.adapter.jdbc.AbstractJdbcAdapter
*
* @author Matthew L. Wilson, Andrej Zachar
* @version $Revision: 1.1 $ $Date: 2005/06/21 08:18:42 $
*/
public abstract class OracleJdbcAdapter extends AbstractOracleJdbcAdapter
{
/** Commons logger. */
protected static final Log LOGGER = LogFactory
.getLog(OracleJdbcAdapter.class);
/** Sets weather the name of the column, or the alias of the column is used. * */
protected boolean useName = false;
protected boolean lowerCase = false;
public OracleJdbcAdapter()
{
}
/**
* Sets weather the name of the column, or the alias of the column is used.
* For example:
* <p>
* SELECT X as Y from dual; X = name Y = alias
* </p>
*
* @param useName
* true: use the name of the column false: use the name of the
* alias
*/
public void setUseName(boolean useName)
{
this.useName = useName;
}
/**
* Sets weather the name of the column should be lowecase;
*
* @param useName
*/
public void setLowerCase(boolean lowerCase)
{
this.lowerCase = lowerCase;
}
}