`
gtssgtss
  • 浏览: 17333 次
  • 性别: Icon_minigender_1
  • 来自: 福州
社区版块
存档分类
最新评论

重构题(1) -- 问题来临

SQL 
阅读更多
public abstract class SimplePagingQueryJdbcTemplate{
	
	public static final Pattern fromPattern = Pattern.compile("from",Pattern.CASE_INSENSITIVE);
	
	public static final String countSQLHead = "select count(*) from ";
	
	public static final Pattern orderByPattern = Pattern.compile("order(\\s)+by",Pattern.CASE_INSENSITIVE);
	
	public static final Pattern selectPattern = Pattern.compile("select",Pattern.CASE_INSENSITIVE);
	
	@Resource
	JdbcTemplate jdbcTemplate;
	
	public List <Map<String,Object>> query(SimpleQuery simpleQuery,Pager pager){
		String querySQL = simpleQuery.getQuerySQL();
		Object args = simpleQuery.getArgs();
		long count = count(simpleQuery);
		pager.setTotalObjects(count);
		if(count<=pager.getFirstResult()){
			return new ArrayList<Map<String,Object>>();
		}
		String pagingQuerySQL = createPagingQuerySQL(querySQL, pager.getFirstResult(), pager.getPageSize());
		if(args == null){
			return jdbcTemplate.queryForList(pagingQuerySQL);
		}else{			
			return jdbcTemplate.queryForList(pagingQuerySQL, args);
		}
	}
	
	private long count(SimpleQuery simpleQuery){
		String countSQL = simpleQuery.getCountSQL();
		Object args = simpleQuery.getArgs();
		if(countSQL == null){
			countSQL = createCountSQL(simpleQuery.getQuerySQL());
		}
		if(args == null){
			return jdbcTemplate.queryForLong(countSQL);
		}else{
			return jdbcTemplate.queryForLong(countSQL, args);
		}
	}
	
	private String createCountSQL(String querySQL){
		String[] SQLParts = fromPattern.split(querySQL, 2);
		if(SQLParts.length!=2){
			throw new IllegalArgumentException("The SQL query maybe miss the word \"from\".");
		}
		String countSQLbody = cutLastOrderBy(SQLParts[1])[0];		
		return countSQLHead+countSQLbody;
	}
	
	protected String[] cutLastOrderBy(String sql){
		Matcher matcher = orderByPattern.matcher(sql);
		if(!matcher.find()){
			return new String[]{sql,null};
		}
		int	orderByIndex = matcher.start();		
		while(matcher.find()){
			orderByIndex = matcher.start();
		}
		return new String[] {sql.substring(0,orderByIndex),sql.substring(orderByIndex)};
	}
	
	
	protected abstract String createPagingQuerySQL(String querySQL, long firstResult, int maxResult);
}


public interface Pager {

    long getFirstResult();

    long getPageNumber();

    int getPageSize();

    long getTotalObjects();

    long getTotalPages();

    void setFirstResult(long firstResult);

    void setPageNumber(long pageNumber);

    void setPageSize(int pageSize);

    void setTotalObjects(long totalObjects);

    void setTotalPages(long totalPages);

}


public interface SimpleQuery {

    Object getArgs();

    String getCountSQL();

    String getQuerySQL();

    void setArgs(Object... args);

    void setCountSQL(String countSQL);

    void setQuerySQL(String querySQL);

}



在对象村的春天软件公司,员工们使用着一个简单的分页功能...有一天,老板发话:"我们的查询功能,返回的是List <Map<String,Object>> 类型,客户现在觉得不满意,要求我们增加返回包装好的对象的List,正好我们有RowMapper接口,这个功能应该不难实现."于是,员工们准备向系统添加功能了.
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics