📄 queryexecutor.java
字号:
/*
* Created on 2004-4-1
*
*/
package com.esimple.service.query;
import java.util.*;
import com.esimple.service.query.config.*;
import com.esimple.service.query.handler.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.sql.*;
import com.esimple.service.dict.DictManager;
import javax.sql.DataSource;
/**
* @author steven
*
*/
public class QueryExecutor {
protected Log logger = LogFactory.getLog(this.getClass());
private static ArrayList handlers = new ArrayList();
private DictManager dictManager;
private DataSource dataSource;
public void setDictManager(DictManager dictManager){
this.dictManager = dictManager;
}
public DictManager getDictManager(){
return this.dictManager;
}
public void setDataSource(DataSource dataSource){
this.dataSource= dataSource;
}
static{
handlers.add(new BasicSqlHandler());
handlers.add(new VarHandler());
handlers.add(new AccessRuleHandler());
handlers.add(new PageHandler());
}
public String toSql(QueryConfig query,String role,HashMap context) throws Exception{
String sql="";
for(int i=0;i< handlers.size();i++){
sql =((SqlHandler)handlers.get(i)).toSqlString(query,sql,role,context);
logger.debug("common gen sql:"+sql);
}
logger.debug("common gen sql:"+sql);
return sql;
}
public int getRowCount(QueryConfig query,String role,HashMap context) throws Exception{
String sql = getRowCountSql(query,role,context);
int count = 0;
Connection conn =null;
Statement stat =null;
ResultSet rs =null;
try{
conn = dataSource.getConnection();
stat = conn.createStatement();
rs = stat.executeQuery( sql );
if ( rs.next() ){
count = rs.getInt("rowCount");
}
}catch(Exception e){
throw e;
}finally{
try{
if( rs != null )rs.close();
if( stat != null) stat.close();
if( conn != null) conn.close();
}catch(Exception ee){
ee.printStackTrace();
}
}
return count;
}
/**
* @param query
* @param role
* @param context
* @return
*/
private String getRowCountSql(QueryConfig query, String role, HashMap context)
throws Exception{
String sql="";
for(int i=0;i< handlers.size()-1;i++){
sql =((SqlHandler)handlers.get(i)).toSqlString(query,sql,role,context);
}
sql = "select count(*) rowCount from ( "
+ sql +") rownum_" ;
logger.debug("row Count sql:"+sql);
return sql;
}
public List execute(QueryConfig query,String role,HashMap context) throws Exception{
String sql = toSql(query,role,context);
ArrayList list = new ArrayList();
ArrayList result = query.getResults();
Connection conn =null;
Statement stat =null;
ResultSet rs =null;
try{
conn = dataSource.getConnection();
stat = conn.createStatement();
rs = stat.executeQuery( sql );
while ( rs.next() ){
QueryResultConfig rscf;
HashMap row = processRow(result, rs);
list.add(row);
}
}catch(Exception e){
throw e;
}finally{
try{
if( rs != null )rs.close();
if( stat != null) stat.close();
if( conn != null) conn.close();
}catch(Exception ee){
ee.printStackTrace();
}
}
return list;
}
private HashMap processRow(ArrayList result, ResultSet rs)
throws SQLException {
HashMap row = new HashMap();
for( int i=0;i<result.size();i++){
QueryResultConfig rscf = (QueryResultConfig)result.get(i);
if( rscf.getType().equalsIgnoreCase("float") ){
String format = rscf.getFormat();
if( format == null ) format= "#,###";
double value = rs.getDouble(rscf.getAlias());
String formatResult = (new java.text.DecimalFormat(format)).format(value);
row.put(rscf.getAlias(),formatResult);
}else if( rscf.getType().equalsIgnoreCase("long") ){
String format = rscf.getFormat();
if( format == null ) format= "####";
long value = rs.getLong(rscf.getAlias());
String formatResult = (new java.text.DecimalFormat(format)).format(value);
row.put(rscf.getAlias(),formatResult);
}else if( rscf.getType().equalsIgnoreCase("date") ){
String format = rscf.getFormat();
if( format == null ) format= "yyyyMMdd";
java.sql.Date value = rs.getDate(rscf.getAlias());
String formatResult = "";
if( value != null ){
formatResult = (new java.text.SimpleDateFormat(format)).format(value);
}
row.put(rscf.getAlias(),formatResult);
}else if( rscf.getType().equalsIgnoreCase("dict") ){
String format = rscf.getFormat();
String value = rs.getString(rscf.getAlias());
value=dictManager.getDictContent(format,value);
row.put(rscf.getAlias(),value);
}else{
logger.debug( rscf.getAlias() );
String value = rs.getString( rscf.getAlias() );
row.put(rscf.getAlias(),value);
}
}
return row;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -