<switch>
<switch>
creates a set of conditional statements in which each
condition triggers a specific response from the system.
Description
<switch>
may be used only within
a <block>
.The <switch>
construct is an extended for of the
<if>
/<then>
construct that makes it easier to
contain conditional logic in the same construct (e.g., using a single
<switch>
instead of many
<if>
/<then>
) statements.
<switch>
has two different versions of its syntax that can be used. The
first is bound to a scalar variable using the on
attribute in the opening
<switch>
tag. The second form allows for comparisons to an arbitrary
number of scalar variables, or other expressions that resolve to 0
or
1
(otherwise known as a boolean expression).
Syntax
<switch on="[VAR]"> <case when="{[CONDITION_1]}"> [CONTENTS_1] </case> <case when="{[CONDITION_2]}"> [CONTENTS_2] </case> ... <else> [CONTENTS_0] </else> </switch>
This expands to [CONTENTS_1] if [VAR] is [CONDITION_1], [CONTENTS_2] if [VAR] is [CONDITION_2], and so on, or [CONTENTS_0] if [VAR] does not match any of the cases.
<else>
clause may be omitted, in which case nothing is
inserted if there is no match.Alternate syntax
<switch> <case when="{[VAR_1][COMPARISON][CONDITION_1]}"> [CONTENTS_1] </case> <case when="{[VAR_2][COMPARISON][CONDITION_2]}"> [CONTENTS_2] </case> <case when="{[BOOLEAN_EXPRESSION]}"> [CONTENTS_3] </case> ... <else> [CONTENTS_0] </else> </switch>
Each of the [CONDITION_1], [CONDITION_2], ... must be
0 or 1. In this version, the first case with when="1"
is inserted. If no
condition is matched, [CONTENTS_0] is inserted.
<else>
clause may be omitted, in which case nothing is
inserted if there is no match.Attributes
on
on
specifies the value or variable that each<case>
permutation will compare to its specific conditions. Can be any value or may be omitted.
Example: <switch>
Form 1
In this example, the base table is pub.demo.retail.item
, and the objective
is to select any one store in the table or all stores.
<base table="pub.demo.retail.item"/> <defblock name="storeselect" type="store" store="2"> <switch on="{@type}"> <case when="all"> <note>all stores selected</note> </case> <case when="store"> <sel value="store={@store}"/> </case> <else> <signal msg="Please make a selection"/> </else> </switch> </defblock> <insert block="storeselect" type="store" store="1"/>
<switch>
Form 2
The second form of the <switch>
statement is ideal for use in cases
when the behavior of the query is dependent on more than a single variable, such as basic
error checking. In this form, the on
attribute is omitted, as follows:
<base table="pub.demo.retail.item"/> <defblock name="testswitchformtwo" store_select="" sku_select="" account_select=""> <switch> <case when="{@store_select=''}"> <signal msg="You must select a store value"/> </case> <case when="{@sku_select=''}"> <signal msg="You must select a product"/> </case> <case when="{@account_select=''}"> <signal msg="You must select an account"/> </case> <else> <sel value="store={@store_select} & sku={@sku_select} & account={@account_select}"/> </else> </switch> </defblock> <insert block="testswitchformtwo" store_select="1" sku_select="366" account_select="957"/>