ESQL Short Course
ESQL Short Course
ESQL Short Course
ESQL
• Extends the constructs of the SQL language to define the behavior of
nodes in a message flow
– Test, calculate, and manipulate fields in logical message
– Reference message header fields
– Change field properties
• Based upon standard SQL 3
Examples:
RETURN Body.Msg.Name = 'IBM';
SET OutputRoot.XML.Msg.Name = 'IBM';
SET OutputRoot.XML.Msg.Name=InputBody."First.Name";
<OrderMsg>
<OrderData Nr=”300524” Date=”2001-11-05”/>
<Customer Nr=”123456” FirstName=”Andrew” LastName=”Smith” />
<OrderItem Nr=”111” Price=”27.95” InStock=”100”>
<Quantity>2</Quantity></OrderItem>
<OrderItem Nr=”222” Price=”8765.12”>
<Quantity>2</Quantity></OrderItem>
<OrderItem Nr=”333” Price=”3.75” InStock=”456”>
<Quantity>10</Quantity>
<Discount>3</Discount></OrderItem></OrderMsg>
Some ESQL syntax
• ESQL keywords not case-sensitive
SET set SeT.....
• CorrelationNames and field references are case-sensitive
OutputRoot, InputBody.Msg.field .....
• ‘Character constants’ and literals enclosed with single quotation
marks (‘ ’)
• “Field references” enclosed with double quotation marks (“ ”) if:
– Special characters in name, such as a blank space
– Name is an ESQL reserved word
• Statement delimiter: semicolon (;)
• Comments
one line: -- (two dashes)
block: /*..comment here ..*/
• FLOAT
1.23e-45 6.7890E2 (64bit)
SQL_C_DOUBLE
• INTEGER, INT
+123 -890 0x123ab 0X3B
64-bit 2-complement form
SQL_C_LONG
• BIT
b'10110'
• BLOB
x'3C493E'
8-bit bytes, 2 digits per byte
SQL_C_BINARY
• CHARACTER
'ABC '='ABC'
SQL_C_CHAR
• TIME
TIME '23:09:01.123456'
SQL_C_TIME
• TIMESTAMP
TIMESTAMP '2001-10-03
23:09:01.123456'
SQL_C_TIMESTAMP
• GMTTIME, GMTTIMESTAMP
Greenwich Mean Time
• INTERVAL
either YEAR and MONTH
variable length
or DAY HOUR MINUTE SECOND
fixed length
INTERVAL '90' MINUTE
INTERVAL '1-06' YEAR TO MONTH
• BOOLEAN
TRUE, FALSE, UNKNOWN (NULL)
SQL_C_BIT
Examples:
DECLARE i INTEGER CARDINALITY(InputRoot.*[]);
SET rowCust.{'Customer-'||C} = InputBody.Customer[<2];
SET OutputRoot.MRM.addr:addressDetails.addr:postcode = 'ZZ01
4WW';
SET OutputRoot.XMLNSC.A.(XMLNSC.Attribute)B = 3;
ESQL operators
• Arithmetic
+ -
numeric, datetime, intervals
* /
numeric, intervals
unary (negation) -
numeric, intervals
concatenate ||
strings
Order of precedence
• Logical
AND, OR, NOT
Boolean
• Comparison
< > <= >= <> =
BETWEEN
3 BETWEEN 1 AND 10
IN
'A' IN (1,'ABC',123.4,'A')
LIKE
multiple characters: %
'ABC' LIKE 'A%' true
single character: _
'ABC' LIKE 'A_' false
IS, IS NOT
test for NULLs
NULL, UNKNOWN, TRUE, FALSE
myVar IS NOT NULL
Deleting and reordering fields
• Delete a field or structure
– Delete and free memory; useful for large messages
DELETE FIELD OutputRoot.XMLNSC.OrderMsg.Customer;
DECLARE myRef REFERENCE TO
OutputRoot.XMLNSC.OrderMsg.OrderItem[1];
DELETE NEXTSIBLING OF myRef;
– Set to NULL
SET OutputRoot.XMLNSC.OrderMsg.OrderItem[2]=NULL;
– Detach (cut and paste)
DETACH myRef;
CREATE
• Create new fields (tags) or attributes
– At any point in message
– A scalar field
SET OutputRoot=InputRoot;
CREATE FIELD OutputRoot.XMLNSC.OrderMsg.Customer.AnAttr
TYPE XMLNSC.Attribute VALUE 'C';
DECLARE myRef REFERENCE TO OutputRoot.XMLNSC;
CREATE PREVIOUSSIBLING OF myREF DOMAIN 'MQRFH2';
– Or a structure (per dupli cation)
DECLARE myRef REFERENCE TO OutputRoot.XMLNSC.OrderMsg;
CREATE FIRSTCHILD OF myRef
FROM InputBody.OrderMsg.OrderItem[3];
NULL value
• NULL is distinct state
– Not 0 or ' '
– SQL_NULL_DATA
– All ESQL datatypes (except REFERENCE)
• If value is
– Unknown
– Undefined
– Uninitialized
• General rule: If any operand is NULL then value is NULL
• Use NULL to delete field or structure
Example:
SET OutputRoot.XMLNSC.Msg.Field=NULL;
SET OutputRoot.XMLNSC.Msg.F2= InputBody.Msg.Unknown;
CAST: Transform into other data type
• CAST can convert character string into BLOB or BIT and vice versa,
if CCSID and ENCODING specified
>--EXISTS--(--ListExpression--)-->>
>--SINGULAR--(--ListExpression--)-->>
>--CARDINALITY--(--ListExpression--)-->>
>--THE--(--ListExpression--)-->>
IF statement
IF InputBody.Message.Department = 'Sales‘
THEN
SET OutputRoot.XMLNSC.Msg.Type = 'S';
SET OutputRoot.XMLNSC.Msg.DeptNo = '4711';
ELSE
SET OutputRoot.XMLNSC.Msg.Type = 'X';
SET OutputRoot.XMLNSC.Msg.DeptNo = '9999';
END IF;
CASE function
• Like ?: Expression in C, but more powerful
• Simple form: 1:n test
SET OutputRoot.XMLNSC.Msg.Type = CASE InputBody.Msg.Department
WHEN 'Sales' THEN 'S'
WHEN 'Distribution' THEN 'D'
ELSE 'X'
END;
• Specialized: NULLIF
SET OutputRoot.XMLNSC.Msg.CustNo =
NULLIF(InputBody.Msg.Custno,'007')
CASE statement
• Like SWITCH statement in C, but more powerful
• Evaluates and executes multiple statements
• Simple form: 1:n test
CASE InputBody.Msg.Department
WHEN 'Sales' THEN SET OutputRoot.XMLNSC.Msg.Type = 'S';
WHEN 'Distribution' THEN CALL handleDistribution();
ELSE
CALL handleUnknown();
RETURN;
END CASE;
WHILE
Use a WHILE loop to repeat a sequence of statements, for example:
DECLARE I INTEGER 1;
WHILE I <= 10 DO
SET OutputRoot.XMLNSC.Msg.MYARRAY[I] = I;
SET I = I + 1;
END WHILE;
Procedures
• Subroutines with IN, OUT, and INOUT parameters and optional RETURNS value
– Can be used recursively
– Language can be ESQL, Java or database (stored procedure)
333 Grapes
222 Oranges
111 Apples
ESQL:
SET OutputRoot.XMLNSC.OrderMsg.OrderItem[]=
(SELECT I.Nr, D.DESCR AS Description
FROM InputBody.OrderMsg.OrderItem[] as I,
Database.PRODUCTS as D
WHERE I.Nr = D.PRODNO);
Input Message:
<OrderMsg>
<OrderItem>
<Nr>111</Nr>
<Price>27.95</Price>
<Quantity>2</Quantity>
</OrderItem>
<OrderItem Discount="3">
<Nr>333</Nr>
<Price>100.75</Price>
<Quantity>1</Quantity>
</OrderItem>
</OrderMsg>
Output Message:
<OrderMsg>
<OrderItem>
<Nr>111</Nr>
<Description>Apples</Description>
</OrderItem>
<OrderItem>
<Nr>333</Nr>
<Description>Grapes</Description>
</OrderItem>
</OrderMsg>
Typical node ESQL
• Compute, Filter, and Database nodes are associated with one and
only one ESQL module
– Must be module for the type of node
CREATE FILTER MODULE <MyFilterModule>.......
– Modules can be reused by other nodes
• Each module contains a function called Main which is the entry point
at which the processing of the node starts
• RETURN value controls message propagation to output terminals
– Filter node
return TRUE; return FALSE; return UNKNOWN;
Propagates to respective terminal
RETURN;
Propagates to FAILURE terminal
Variables
• Scalar and fixed data type
• Tree structures (ROW and REFERENCE)
>-DECLARE-+----Name-----+--+----------+-DataType-+--
+---------------+-->>
+--<<--,--<<--+ +-CONSTANT-+ |+-InitialValueExpr-+
+--NAMESPACE----------+
+--NAME---------------+
DECLARE var, I INTEGER 1;
DECLARE addr NAMESPACE ‘http://www.ibm.com/address’;
DECLARE course CONSTANT CHAR ‘WM663’;
Variable scope, lifetime, and sharing
• Scope: Where the variable appears
Examples: node, flow, execution group, broker
SET OutputRoot.Properties.MessageFormat='XML1';
PROPAGATE DELETE NONE;
SET OutputRoot.Properties.MessageFormat='Text1';
RETURN TRUE;
END;
ESQL Database
Insertion:
INSERT INTO Database.DSN2.Schema2.PRICEDATA
(PRODUCT_NAME, ITEM_PRICE, STATUS)
VALUES (Body.Message.ProductID,
Body.Message.UnitPrice,'OPEN');
PASSTHRU example
• PASSTHRU function to issue complex SELECTS
Example:
SET OutputRoot.XMLNSC.Msg.Results[]= PASSTHRU(
'SELECT WORKDEPT, MAX(SALARY) AS MaxSalary
FROM schema1.EMPLOYEE
GROUP BY WORKDEPT
HAVING MAX(SALARY) > ?'
TO Database.SAMPLE
VALUES(InputBody.Msg.Salary));
THE (SELECT …)
• SELECT returns a list
SET OutputRoot.XML.Msg.Name[]=
(SELECT E.LASTNAME
from Database.EMPLOYEE as E);
Msg
Name LASTNAME Haas
Name LASTNAME Thompson
SELECT (ITEM …)
• SELECT (ITEM..) gets value only
– Without the ITEM keyword, the database table column name or XML tag will
be used as message tag
– Can be overridden by AS keyword in SELECT statement
SET
OutputRoot.XML.Msg.Name[]=
(SELECT E.LASTNAME from
Database.EMPLOYEE as E);
Msg
Name LASTNAME Haas
Name LASTNAME Thompson
SET
OutputRoot.XML.Msg.Name[]=
(SELECT ITEM E.LASTNAME from
Database.EMPLOYEE as E);
Msg
Name Haas
Name Thompson