For me it works fine now:

package code.lib.h2;

public class First implements org.h2.api.AggregateFunction {
    int count = 0;
    Object result;
    Object 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;
        Object value = objects[0];
        Object sort = objects[1];
        if (count == 0) {
            result = value;
            order = sort;
        } else {
            if (((java.lang.Comparable<Object>) sort).compareTo((java.lang.
Comparable<Object>) order) < 0) {
                result = value;
                order = sort;
            }
        }
        count++;
    }

    @Override
    public Object getResult() throws java.sql.SQLException {
        return result;
    }
}

package code.lib.h2;

public class Last implements org.h2.api.AggregateFunction {
    int count = 0;
    Object result;
    Object 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;
        Object value = objects[0];
        Object sort = objects[1];
        if (count == 0) {
            result = value;
            order = sort;
        } else {
            if (((java.lang.Comparable<Object>) sort).compareTo((java.lang.
Comparable<Object>) order) >= 0) {
                result = value;
                order = sort;
            }
        }
        count++;
    }

    @Override
    public Object getResult() throws java.sql.SQLException {
        return result;
    }
}

and in the H2 console:

DROP AGGREGATE IF EXISTS FIRST;
CREATE AGGREGATE FIRST FOR "code.lib.h2.First";

DROP AGGREGATE IF EXISTS LAST;
CREATE AGGREGATE LAST FOR "code.lib.h2.Last";

Suggestions for better code are welcome! (I'm no Java programmer, normally 
I use Scala.)


-- 
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