Add new operator into your data model

Each data model contains a lot of default operators. However, sometimes you may need to add a new operator to your model.

Let's suppose we need “within last N days” operator which words for date/time fields, takes a numeric operand (N) and returns true if specified date is within last N days starting from today.

Here are the instructions:

1) Open your model file

Run Data Model Editor utility (DME) and open your model (.xml) file (Model | Open… menu item).

2) Add new operator

Go to Operators tab and select “Operators | Add Operator” menu item. New operator with default ID “NewOp” will appear at the bottom of the list of operators.

Data Model Editor - add new operator

After that, we need to define all properties for a new operator.

3) Setting operator's properties

ID: "DateWithinLastNDays"

This is mostly an internal property. EasyQuery will use to reference this operator in attributes and query conditions.

Caption: "Last N days"

This value will be shown to users in a drop-down list when they select operator

Display format:

{expr1} [[is within last]] {expr2} days

This field defines how your operator (actually the whole condition with this operator) will look in a condition row in query panel. The part within [[ and ]] will be clickable.

Expression:

{expr1} > DATEADD(day, -{expr2}, GETDATE())

The most important property for an operator. It defines the result expression which will come into generated SQL statement. and parts specify the operands. The first operand is always an attribute, second - some value entered by a user.

Applied types: "Date, DateTime"

Since this operator can be used only with Date and DateTime attributes - we uncheck all options in “Applied types” field except these two.

Here is how Operators tab in DME looks like after all changes described above:

Data Model Editor - operator properties

4) Setting operand's properties

Each operator has one or more operands. By default, all operands have the same kind and data type, but this preset can be changed. In our case, we need the second operand to be numeric (integer if more precisely) data type. To set it - just open “Operands” tab, select “Default operand” item in the list and choose “Int” in “Data type” combo box. All other operand properties let be left with their default values.

N.B.: By default “data type” property has “Auto” value. It means that this operand will have the same type as attribute selected for the left part of the condition. It's useful for operators which can be applied to attributes with different data types (e.g. “is equal to”).

Data Model Editor - operand properties

5) Associate new operator with attribute(s)

Finally, we need to associate new operator with one or more attributes for which it may be used in query conditions. To do it:

  • open “Entities” tab
  • select necessary attribute
  • open its “Operators” tab
  • click on “Add” button, check new operator and click OK.

Data Model Editor - associate operator with attributes

6) Done!

Finally, save your model and run the project which uses this model to test new operator:

Data Model Editor - new operator in the query panel