可选的where子句jasper报告 [英] optional where clause jasper reports

查看:126
本文介绍了可选的where子句jasper报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试此查询,但没有成功。

I am trying this query but without success.

SELECT name, phone_office, billing_address_city, billing_address_street, billing_address_country 
FROM accounts
WHERE ($P!{EmployeeID} is null or assigned_user_id = $P!{EmployeeID})
ORDER BY billing_address_country, billing_address_city

此网址将按EmployeeID进行过滤,工作正常:

This url will filter by EmployeeID and works fine:

.../flow.html?_flowId=viewReportFlow&reportUnit=/reports/samples/EmployeeAccounts&EmployeeID=sarah_id

但是,当我删除EmployeeID参数时,我想删除过滤器在哪里。所以应该显示所有结果。

But when i remove the EmployeeID parameter i want to remove the filter where. So all results should be shown.

.../flow.html?_flowId=viewReportFlow&reportUnit=/reports/samples/EmployeeAccounts

我的问题是,在sql查询中传递可选where的正确方法是什么。

My question is, what is the correct way of passing an optional where in sql query.

推荐答案

好的,让我们看看样本。

Ok, let see the sample.

例如我们有一个查询:

SELECT id, city, street FROM address WHERE city=$P{inputParamCity} ORDER BY city

但我们的 inputParamCity 可以是未定义。在这种情况下,我们得到一个错误:

But our inputParamCity can be undefined. In this case we got an error:

Error filling print... Error preparing statement for executing the report query : 
SELECT id, city, street FROM address WHERE city=? ORDER BY city

我们如何修理它?

这很简单 - 我们可以使用默认表达式添加另一个参数,如下所示:

How we can fix it?
It is very simple - we can add another parameter with default expression like this:

<parameter name="whereClause" class="java.lang.String" isForPrompting="false">
    <defaultValueExpression><![CDATA[()$P{inputParamCity} == null || $P{inputParamCity}.isEmpty()) ? "1=1" : "city='" + $P{inputParamCity} + "'"]]></defaultValueExpression>
</parameter>

- 如果 inputParamCity 参数未定义为fake子句将使用1 = 1,在其他情况下,将应用 city 字段的过滤器。

-if the inputParamCity parameter is undefined the "fake" clause "1=1" will be used, in other case the filter by city field will be applied.

当然我们必须修改查询表达式 - 使用这个新参数。在这种情况下,我们的查询表达式将是:

And of course we have to modify the query expression - to use this new parameter. Our query expression in this case will be:

<queryString>
    <![CDATA[SELECT id, city, street FROM address WHERE $P!{whereClause} ORDER BY city]]>
</queryString>



样本



jrxml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="optional_where_clause" language="groovy" pageWidth="595" pageHeight="842" columnWidth="555" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20" uuid="d3648644-0087-4dfc-ac6d-87e82d9bb33e">
    <parameter name="inputParamCity" class="java.lang.String"/>
    <parameter name="whereClause" class="java.lang.String" isForPrompting="false">
        <defaultValueExpression><![CDATA[($P{inputParamCity} == null || $P{inputParamCity}.isEmpty()) ? "1=1" : "city='" + $P{inputParamCity} + "'"]]></defaultValueExpression>
    </parameter>
    <queryString>
        <![CDATA[SELECT id, city, street FROM address WHERE $P!{whereClause} ORDER BY city]]>
    </queryString>
    <field name="ID" class="java.lang.Integer"/>
    <field name="CITY" class="java.lang.String"/>
    <field name="STREET" class="java.lang.String"/>
    <detail>
        <band height="20" splitType="Stretch">
            <textField>
                <reportElement uuid="c2a80b99-e087-4839-8e77-841edd899255" x="0" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{ID}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="0aafcfd6-60f7-4272-8e7d-0aa77507204b" x="100" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{CITY}]]></textFieldExpression>
            </textField>
            <textField>
                <reportElement uuid="c8726513-8250-43ec-bafc-003e81094c27" x="200" y="0" width="100" height="20"/>
                <textElement/>
                <textFieldExpression><![CDATA[$F{STREET}]]></textFieldExpression>
            </textField>
        </band>
    </detail>
</jasperReport>

如果使用未定义的 inputParamCity 参数(未设置该值)结果将是:

In case using undefined inputParamCity parameter (the value is not set) the result will be:

在这种情况下,引擎使用的查询是:

In this case the query was used by engine is:

SELECT id, city, street FROM address WHERE 1=1 ORDER BY city

例如,如果我们为 inputParamCity 参数设置值 Chicago ,结果将为:

If we set, for example, the value Chicago for inputParamCity parameter the result will be:

In在这种情况下,引擎使用的查询是:

In this case the query was used by engine is:

SELECT id, city, street FROM address WHERE city='Chicago' ORDER BY city






注意:

您可以修改 whereClause 参数的表达式和查询表达式。例如,您可以将 WHERE 关键字从查询表达式移动到参数的表达式,以防止使用子句1 = 1

You can modify the whereClause parameter's expression and the query expression. For example, you can move WHERE keyword from query expression to parameter's expression to prevent using the fake clause "1=1"

这篇关于可选的where子句jasper报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆