Hi!

I found out that you can implement a user-defined aggregate function with 
TWO parameter, e.g. first with the value searched for and the second for 
ordering, e.g.

SELECT FIRST(name, id) and an implementation for Integer for value and sort 
type like this:

package myh2;

public class First implements org.h2.api.AggregateFunction {
  int count = 0;
  int result;
  int order;

    @Override
    public void init(java.sql.Connection cnctn) throws java.sql.SQLException 
{
        // I ignored this
    }

    @Override
    public int getType(int[] ints) throws java.sql.SQLException {
       if (ints.length != 2) {
           throw new java.sql.SQLException("The aggregate function FIRST 
must have 2 arguments.");
       }
       return ints[0];
    }

    @Override
    public void add(Object o) throws java.sql.SQLException {
        Object[] objects = (Object[]) o;
        int value = (Integer) objects[0];
        int sort = (Integer) objects[1];
        //int sort = (java.lang.Comparable<Integer>) objects[1];
        if (count == 0) {
            result = value;
            order = sort;
        } else {
            if (sort <= order) {
                result = value;
                order = sort;
            }
        }
        count++;
    }

    @Override
    public Object getResult() throws java.sql.SQLException {
        return result;
    }
}
This may be a bad-looking code, sorry, but it should work like that.
In this example the type for the search value and sorting is hard coded as 
Integer. Now I look for a more general solution.

-- 
You received this message because you are subscribed to the Google Groups "H2 
Database" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/h2-database.
For more options, visit https://groups.google.com/d/optout.

Reply via email to