예제 #1
0
  @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");
  }
 @Test
 public void testCustomFunction() {
   OSQLFunction f = OSQLEngine.getInstance().getInlineFunction("containsValueSubstrings");
   String syntax = f.getSyntax();
   assertTrue(syntax.startsWith("containsValueSubstrings"));
 }