Request report with parameters

一笑奈何 提交于 2019-12-11 19:39:59

问题


I want to create a report with JasperReports with multiple parameters, the report is generated correctly when the user passes all the parameters but nothing is generated when one parameter is missed i use this request

  SELECT
     t.*,
     u."name" AS username,
     c."name" AS componentName,
     s."designation" AS statusName,
     pr."name" AS priorityName,
     p."name" AS projectName
FROM
     "component" c INNER JOIN "ticket" t ON c."id" = t."component_id"
     INNER JOIN "personne" u ON t."personne_id" = u."id"
     INNER JOIN "status" s ON t."status_id" = s."id"
     INNER JOIN "priority" pr ON t."priority_id" = pr."id"
     INNER JOIN "project" p ON c."project_id" = p."id"
WHERE
     pr.name = $P{priority}
and u.login = $P{userLogin}
and s.designation = $P{status}
and t.creation_date between $P{start} and $P{end}
and c.name = $P{componenet}

Please can you help me to generate the report even where there is one parameter missed?


回答1:


do you want outer joins? or you may choose a structure like this:

and ( u.login = $P{userLogin} or $P{userLogin} is null )

for each line




回答2:


You can set the default values for every parameter or use this query:-

     SELECT
 t.*,
 u."name" AS username,
 c."name" AS componentName,
 s."designation" AS statusName,
 pr."name" AS priorityName,
 p."name" AS projectName
FROM
 "component" c INNER JOIN "ticket" t ON c."id" = t."component_id"
 INNER JOIN "personne" u ON t."personne_id" = u."id"
 INNER JOIN "status" s ON t."status_id" = s."id"
 INNER JOIN "priority" pr ON t."priority_id" = pr."id"
 INNER JOIN "project" p ON c."project_id" = p."id"
WHERE
 (pr.name = $P{priority} or $P{priority}  is null)
  and (u.login = $P{userLogin} or $P{userLogin}  is null)
  and (s.designation = $P{status}  or $P{status} is null )
  and t.creation_date between $P{start}  and $P{end}
  and (c.name = $P{componenet} or $P{componenet}  is null)



回答3:


You can control the where clause in java class before generate the report.

1.In java u can check whether the parameter is null or not, then do small validation such as

StringBuffer sb = new StringBuffer();
if(StringUtils.isNotEmpty(priority)){
        sb.append(" AND pr.name = "+priority);
    }

2.do for every other condition for u.login, s.designation

3.after that u can pass reportParam.put("sqlQuery", sb.toString());

4.the in ireport simply change your query

WHERE
pr.name = $P{priority}
and u.login = $P{userLogin}
and s.designation = $P{status}
and t.creation_date between $P{start} and $P{end}
and c.name = $P{componenet}

to

WHERE 1=1 $P!{sqlQuery}

now you dont need to worry about the null, since it will ignore the condition in java class.



来源:https://stackoverflow.com/questions/17663860/request-report-with-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!