Object Values Api

The Object Values API is the third query type that NeoDatis ODB supports.

The Object Values API is a very powerful API to let one retrieve values of object attributes instead of the full object itself!

Sometime, you just need to retrieve the name of a user (to build a combo, for example), and traditional Object Databases force you to retrieve the whole object. NeoDatis ODB let you retrieve the attributes you need without needing instancianting a single object. This leverage a very fast and powerfull API for building reports for example.

Check a single code snippet to check how it work:

ODB odb = open("values.odb");
odb.store(new User("user1","email1",new Profile("profile name",new Function("f111"))));
odb.close();
 
odb = open("values.odb");
Values values = odb.getValues(new ValuesCriteriaQuery(User.class).field("name"));
 
IObjectValues ov = values.nextValues();
System.out.println("user1", ov.getByAlias("name"));
System.out.println("user1", ov.getByIndex(0));
odb.close();

As a user have a profile and the profile has a name, you may want to retrieve the name of the profile directly, like this:

Values values = odb.getValues(new ValuesCriteriaQuery(User.class).field("name").field("profile.name"));

Object Values API provides more features to query object values like:

  • sum
  • average
  • min
  • max
  • count
  • group by
  • Custom function

Sum

Values values = odb.getValues(new ValuesCriteriaQuery(TestClass.class).sum("int1","sum of int1")

Average

Values values = odb.getValues(new ValuesCriteriaQuery(TestClass.class).avg("int1","average of int1")

Min & Max

Values values = odb.getValues(new ValuesCriteriaQuery(TestClass.class)
       .min("int1","min of int1")
       .max("int1","max of int1");

Count

Values values = odb.getValues(new ValuesCriteriaQuery(TestClass.class).count("my count");

Custom function

If you need a specific function, you can always implement you own Action do compute what you need. This is done by extending the
CustomQueryFieldAction class.

Here is a simple implementation example:

package org.neodatis.odb.test.query.values;
 
import java.math.BigDecimal;
 
import org.neodatis.odb.core.OID;
import org.neodatis.odb.core.io.AttributeValuesMap;
import org.neodatis.odb.core.query.values.CustomQueryFieldAction;
import org.neodatis.odb.core.query.values.ValuesUtil;
 
public class TestCustomQueryFieldAction extends CustomQueryFieldAction {
 
    private BigDecimal myValue;
 
    public TestCustomQueryFieldAction() {
        this.myValue = new BigDecimal(0);
    }
 
    public void execute(final OID oid, final AttributeValuesMap values) {
        Number n = ValuesUtil.convert((Number) values.get(attributeName));
        myValue = myValue.add(new BigDecimal(n.toString()).multiply(new BigDecimal(2)));
    }
 
    public Object getValue() {
        return myValue;
    }
 
    public boolean isMultiRow() {
        return false;
    }
 
    public void start() {
        // Nothing to do 
    }
    public void end() {
        // Nothing to do
 
    }
}

And here is how you use this custom funcion:
ICustomQueryFieldAction custom = new TestCustomQueryFieldAction();
Values values = odb.getValues(new ValuesCriteriaQuery(TestClass.class)
    .custom("int1", "custom of int1",custom));
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License