/**
* 通用查询,返回所有符合查询条件的列
* */
public OperResult queryHistory(String table_name,int table_unit,String id_col_name,String time_col_name,List<String> keys,Date startTime,Date endTime,int interval)
{
OperResult result = new OperResult();
if(tableName2Type.get(table_name)==null)//检查输入的表名是否合法
{
result.setFailed("table "+table_name+" is not exist!");
return result;
}
if(tableName2Type.get(table_name)==TABLE_TIMING&&interval<=0)//判断表插入类型与选择的间隔时间是否一致
{
result.setFailed("table "+table_name+" is inserted periodically, plz input correct interval time!");
return result;
}
if(tableName2Type.get(table_name)==TABLE_EMITTING&&interval>0)//判断表插入类型与选择的间隔时间是否一致
{
result.setFailed("table "+table_name+" is inserted emitted, plz input correct interval time!");
return result;
}
List<LazyDynaBean> resultData = new ArrayList<LazyDynaBean>();
for(String id:keys){
long id_key = 0;
try
{
id_key = Long.parseLong(id);
}
catch (NumberFormatException e)
{
System.out.println("ID:"+id+"'s format is error!");
continue;//如果输入的id类型转化出错时不处理,相当于没有查询到该id的数据;逻辑上是正确的
}
// 按时间间隔获取数据与否只是此处组织查询语句有区别,只要在sql_ping_with_str()函数中区分即可
String with_sql_str = sql_ping_with_str(interval,startTime,endTime,table_name,time_col_name,id_col_name,id_key,table_unit);
if(with_sql_str!=null)//有可能没有找到待查询的表
{
String query_sql_str = sql_ping_query_str(table_name);
String execute_sql_str = with_sql_str + query_sql_str;
System.out.println(execute_sql_str);
/**!
* 通过每行都进行Meta信息的处理来转成结果。
*/
// List<LazyDynaBean> records = jdbcTemplate.query(execute_sql_str, new RowMapper<LazyDynaBean>() {
// public LazyDynaBean mapRow(ResultSet rs, int rowNum)
// throws SQLException {
// return DataBaseUtility.convertResultSet2LazyDynaBean(rs);
// }
// });
/**!
* 通过先得到Meta信息,然后遍历结果
*/
List<LazyDynaBean> records = null;
try{//有可能用户调接口的时候会出错列名,需要异常处理
records = jdbcTemplate.query(execute_sql_str, new ResultSetExtractor<List<LazyDynaBean>>(){
// public LazyDynaBean mapRow(ResultSet rs, int rowNum)
// throws SQLException {
// return DataBaseUtility.convertResultSet2LazyDynaBean(rs);
// }
@Override
public List<LazyDynaBean> extractData(ResultSet rs)
throws SQLException, DataAccessException {
return DataBaseUtility.convertResultSet2LazyDynaBeanList(rs);
}
});
}
catch(BadSqlGrammarException e)
{
e.printStackTrace();
result.setFailed("Bad column name!");
return result;
}
resultData.addAll(records);
}
}
result.setSucceed();
result.setData(resultData);
return result;
}