|
4#
楼主 |
发表于 2010-12-15 15:55:51
|
只看该作者
1647
private SQLQuery makeCountQueryForDynamicFilter(Query sourceQuery,
List<Filter> filters, Map<String, Object> parameters,
Session session)
{
StringBuffer sql = new StringBuffer();
sql.append("select count(*) ");
sql.append(removeSelect(sourceQuery.getQueryString()));
Map<String, Object> params = new HashMap<String, Object>();
setFilters(filters, sql, params);
SQLQuery makedQuery = session.createSQLQuery(sql.toString());
if (!params.isEmpty())
{
setQueryParameter(makedQuery, params);
}
if (null != parameters)
{
setQueryParameter(makedQuery, parameters);
}
return makedQuery;
}
private SQLQuery makeQueryForDynamicTable(Query sourceQuery,
List<Filter> filters, String table, Map<String, Object> parameters,
Session session)
{
StringBuffer sql = new StringBuffer();
sql.append(sourceQuery.getQueryString().replaceAll("tableName", table));
Map<String, Object> params = new HashMap<String, Object>();
if (filters != null && !filters.isEmpty())
{
sql.append(" where 1=1");
for (Filter filter : filters)
{
generatorSQL(sql, params, filter);
}
}
SQLQuery makedQuery = session.createSQLQuery(sql.toString());
if (!params.isEmpty())
{
setQueryParameter(makedQuery, params);
}
if (parameters != null && !parameters.isEmpty())
{
setQueryParameter(makedQuery, parameters);
}
return makedQuery;
}
private void generatorSQL(StringBuffer sql, Map<String, Object> params,
Filter filter)
{
if (filter instanceof EqFilter)
{
sql.append(" and (").append(filter.getProperty()).append(" = :")
.append(filter.getProperty()).append(")");
params.put(filter.getProperty(), filter.getValue());
}
else if (filter instanceof LikeFilter)
{
sql.append(" and (").append(filter.getProperty()).append(" like :")
.append(filter.getProperty()).append(")");
params.put(filter.getProperty(), filter.getValue());
}
else if (filter instanceof NotEqFilter)
{
sql.append(" and (").append(filter.getProperty()).append(" <> :")
.append(filter.getProperty()).append(")");
params.put(filter.getProperty(), filter.getValue());
}
else if (filter instanceof NotLikeFilter)
{
sql.append(" and (").append(filter.getProperty()).append(
" not like :").append(filter.getProperty()).append(")");
params.put(filter.getProperty(), filter.getValue());
}
else if (filter instanceof NullFilter)
{
sql.append(" and (").append(filter.getProperty()).append(
" is null)");
}
else if (filter instanceof NotNullFilter)
{
sql.append(" and (").append(filter.getProperty()).append(
" is not null)");
}
else if (filter instanceof InFilter)
{
sql.append(" and (").append(filter.getProperty()).append(" in (:")
.append(filter.getProperty()).append("))");
params.put(filter.getProperty(), filter.getValue());
}
else if (filter instanceof ORFilter)
{
Filter[] arg = (Filter[]) filter.getValue();
sql.append(" and (").append(makeQueryStringFromFilter(arg[0], ""))
.append(" or ").append(
makeQueryStringFromFilter(arg[1], "1")).append(")");
params.put(arg[0].getProperty(), arg[0].getValue());
params.put(arg[1].getProperty() + "1", arg[1].getValue());
}
else
{
throw new HibernateException("Not supported!");
}
} |
|