use of com.orientechnologies.orient.core.sql.functions.OSQLFunctionAbstract in project orientdb by orientechnologies.
the class SQLFunctionsTest method queryCustomFunction.
@Test
public void queryCustomFunction() {
OSQLEngine.getInstance().registerFunction("bigger", new OSQLFunctionAbstract("bigger", 2, 2) {
@Override
public String getSyntax() {
return "bigger(<first>, <second>)";
}
@Override
public Object execute(Object iThis, OIdentifiable iCurrentRecord, Object iCurrentResult, final Object[] iParams, OCommandContext iContext) {
if (iParams[0] == null || iParams[1] == null)
// CHECK BOTH EXPECTED PARAMETERS
return null;
if (!(iParams[0] instanceof Number) || !(iParams[1] instanceof Number))
// EXCLUDE IT FROM THE RESULT SET
return null;
// USE DOUBLE TO AVOID LOSS OF PRECISION
final double v1 = ((Number) iParams[0]).doubleValue();
final double v2 = ((Number) iParams[1]).doubleValue();
return Math.max(v1, v2);
}
});
List<ODocument> result = database.command(new OSQLSynchQuery<ODocument>("select from Account where bigger(id,1000) = 1000")).execute();
Assert.assertTrue(result.size() != 0);
for (ODocument d : result) {
Assert.assertTrue((Integer) d.field("id") <= 1000);
}
OSQLEngine.getInstance().unregisterFunction("bigger");
}
Aggregations