<defop>
<defop>
allows for the creation of user-defined operations that
contain complex logic or syntax, but can subsequently be expressed as a "standard" 1010data
operation.
Syntax
<defop name="[NAME_OF_NEW_OP]"
[USER_DEFINED_VAR_1]="[VALUE_1]" ...
[USER_DEFINED_VAR_N]="[VALUE_N]">
[SOME_1010DATA_LOGIC]
</defop>
The newly created "operation" would then be called as follows:
<name_of_new_op var="[VALUES]"/>
Attributes
name
- The name of the
<defop>
.The name must begin with an alphabetic character and consist only of alphanumeric characters, digits, and/or underscores. In addition, the name must be unique; that is, no other block in the macro may have the same name.
[USER_DEFINED_VAR_x]
- Accepts a string that will act as a user-defined variable. The variable name must
begin with a lowercase alphabetic character and consist only of lowercase alphanumeric
characters, digits, and/or underscores.
The block element may include any number of additional attributes, which may be used as parameters in the Macro Language code contained within the element. The value of a user-defined variable (e.g.,
my_var
) is substituted wherever a scalar expression containing a reference to it (e.g.,{@my_var}
) appears within the block element.
Example
As an example, it is sometimes necessary to create selection statements with
OR
logic using the OR
operator (|
). For
example, it wouldn't be uncommon to write the following selection operation to select three
accounts, as follows:
<sel value="account=738|account=523|account=709"/>
Of course, the example above is completely functional. But what if the values being
selected on need to change? What if more or fewer need to be selected?
<defop>
allows for creating a more parameterized and programmatic way
to perform common tasks with a single operation call.
In this example, we will create an operation that can select an arbitrary number of values
in the same column of the example table using OR
logic (i.e., if any of the
values are present, the appropriate rows will be selected).
<base table="pub.demo.retail.item"/> <defop name="row_select" rows=""> <set> <sel_expr> <foreach rows="{@rows}" tally="i"> <if test="{@i>1}"> <then>| </then> </if>account='{@rows}' </foreach> </sel_expr> </set> <sel value="{@sel_expr}"/> </defop>
Note that the new operation is called using the following syntax:
<row_select rows="738,523,709"/>
Most importantly, any number of values may be provide to the attribute,
rows
, making this selection logic much more flexible and therefore
useful.
In the example above, the <defop>
may appear anywhere within a macro.
However, it must appear after it is defined.