Search in sources :

Example 1 with OSQLFunctionAbstract

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");
}
Also used : OSQLFunctionAbstract(com.orientechnologies.orient.core.sql.functions.OSQLFunctionAbstract) OCommandContext(com.orientechnologies.orient.core.command.OCommandContext) OSQLSynchQuery(com.orientechnologies.orient.core.sql.query.OSQLSynchQuery) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) Test(org.testng.annotations.Test)

Aggregations

OCommandContext (com.orientechnologies.orient.core.command.OCommandContext)1 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 OSQLFunctionAbstract (com.orientechnologies.orient.core.sql.functions.OSQLFunctionAbstract)1 OSQLSynchQuery (com.orientechnologies.orient.core.sql.query.OSQLSynchQuery)1 Test (org.testng.annotations.Test)1