-
Notifications
You must be signed in to change notification settings - Fork 0
Computing Functions Auxiliary
Decode | Provide switch-case-default functionality |
Ifnn | Return value, if checked value is not null. Short synonym for function IfNotNull |
IfNotNull | Return value, if checked value is not null |
Length | Returns the length of the object, or -1 if not applicable |
Nvl | Return substitute if value is null |
Provide switch-case-default and case-when-default functionality.
decode(expression, caseValue, returnValue [, caseValue, returnValue] [, default])
expression
- The value to compare
caseValue
- The value that is compared against expression
returnValue
- The value returned, if expression is equal to caseValue
default
- Optional. If no matches are found, the DECODE function will return default.
Result value with type as returnValue
or default
If expression is a Collection, then comparison will run for every value in collection and result will be a collection.
Map<String, Object> testValues = new LinkedHashMap<>();
testValues.put("a", 1);
testValues.put("b", 2);
int res = engine.calc(" decode(.a, 1, 111, 2, 222, 3, 333, 5, 555) ", testValues, Integer.class);
assertEquals(111, res);
int res = engine.calc(" decode(.b, 1, 111, 2, 222, 3, 333, 5, 555) ", testValues, Integer.class);
assertEquals(222, res);
int res = engine.calc(" decode(.no, 1, 111, 2, 222, 555) ", testValues, Integer.class);
assertEquals(555, res);
String str = engine.calc(" 'aaa' + decode(null, '', ' ' + 'bbb') ", testValues, String.class);
assertEquals("aaa", str);
@Test
public void run08() {
Map<String, Object> map = new LinkedHashMap<String, Object>(){{
put("fieldA", 10);
put("fieldB", 20);
put("fieldC", 30);
}};
Formula formula = new Formula("decode(true, .fieldA=10, 'resultA', .fieldB=20, 'resultB')");
String str = engine.calc(formula, map, String.class);
assertEquals("resultA", str);
formula = new Formula("decode(true, .fieldA=11, 'resultA', .fieldB=20, 'resultB')");
str = engine.calc(formula, map, String.class);
assertEquals("resultB", str);
}
Return value, if checked value is not null.
ifNotNull(expressionForCheck, expressionValue)
ifnn(expressionForCheck, expressionValue)
expressionForCheck
- The expression whose result is checked for null.
expressionValue
- The expression that will be the result if the expressionForCheck
is not null.
Result value with type as expressionValue
String str = engine.calc(" 'aaa' + ifNotNull(null, ' ' + 'bbb') ", String.class);
assertEquals("aaa", str);
String str = engine.calc(" 'aaa' + ifnn('not null', ' ' + 'bbb' ", String.class);
assertEquals("aaa bbb", str);
Returns the length of the object, or -1 if not applicable.
value
path or collection where required to count the number of rows
Length of the object, type : long
int len = engine.calc(" length(null) ", Integer.class);
assertEquals(0, len);
len = engine.calc(" length('abcdf') ", Integer.class);
assertEquals(5, len);
len = engine.calc(" length(1) ", Integer.class);
assertEquals(1, len);
len = engine.calc(" length(123) ", Integer.class);
assertEquals(3, len);
len = engine.calc(" length((1,2,3,4,5)) ", Integer.class);
assertEquals(5, len);
Map<String, Integer> map = new HashMap<>();
map.put("1", 1);
map.put("2", 2);
Map<String, Object> data = new HashMap<>();
data.put("map", map);
len = engine.calc(" length(.map) ", data, Integer.class);
assertEquals(2, len);
Return substitute if value is null.
value
The value to test to null.
substitute
The value to be used if value
is null.
Result value with type as value
or substitute
String str = engine.calc(" 'aaa' + nvl(null, ' ' + 'bbb') ", String.class);
assertEquals("aaa bbb", str);
str = engine.calc(" 'aaa ' + nvl('no null', ' ' + 'bbb') ", String.class);
assertEquals("aaa no null", str);
- Home
- Extendable Query Language
- Computing
- Simple Usage
- Own Value Extraction
- Functions
- What is Type
- Known Types
- Add Own Type
- Data conversion
- Generate SQL Query
- Find files