Search in sources :

Example 16 with Aggregate

use of org.h2.api.Aggregate in project h2database by h2database.

the class CreateAggregate method update.

@Override
public int update() {
    session.commit(true);
    session.getUser().checkAdmin();
    Database db = session.getDatabase();
    if (db.findAggregate(name) != null || schema.findFunction(name) != null) {
        if (!ifNotExists) {
            throw DbException.get(ErrorCode.FUNCTION_ALIAS_ALREADY_EXISTS_1, name);
        }
    } else {
        int id = getObjectId();
        UserAggregate aggregate = new UserAggregate(db, id, name, javaClassMethod, force);
        db.addDatabaseObject(session, aggregate);
    }
    return 0;
}
Also used : UserAggregate(org.h2.engine.UserAggregate) Database(org.h2.engine.Database)

Example 17 with Aggregate

use of org.h2.api.Aggregate in project h2database by h2database.

the class JavaAggregate method getValue.

@Override
public Value getValue(Session session) {
    HashMap<Expression, Object> group = select.getCurrentGroup();
    if (group == null) {
        throw DbException.get(ErrorCode.INVALID_USE_OF_AGGREGATE_FUNCTION_1, getSQL());
    }
    try {
        Aggregate agg = (Aggregate) group.get(this);
        if (agg == null) {
            agg = getInstance();
        }
        Object obj = agg.getResult();
        if (obj == null) {
            return ValueNull.INSTANCE;
        }
        return DataType.convertToValue(session, obj, dataType);
    } catch (SQLException e) {
        throw DbException.convert(e);
    }
}
Also used : SQLException(java.sql.SQLException) UserAggregate(org.h2.engine.UserAggregate) Aggregate(org.h2.api.Aggregate)

Example 18 with Aggregate

use of org.h2.api.Aggregate in project h2database by h2database.

the class JavaAggregate method getInstance.

private Aggregate getInstance() throws SQLException {
    Aggregate agg = userAggregate.getInstance();
    agg.init(userConnection);
    return agg;
}
Also used : UserAggregate(org.h2.engine.UserAggregate) Aggregate(org.h2.api.Aggregate)

Example 19 with Aggregate

use of org.h2.api.Aggregate in project h2database by h2database.

the class JavaAggregate method optimize.

@Override
public Expression optimize(Session session) {
    userConnection = session.createConnection(false);
    int len = args.length;
    argTypes = new int[len];
    for (int i = 0; i < len; i++) {
        Expression expr = args[i];
        args[i] = expr.optimize(session);
        int type = expr.getType();
        argTypes[i] = type;
    }
    try {
        Aggregate aggregate = getInstance();
        dataType = aggregate.getInternalType(argTypes);
    } catch (SQLException e) {
        throw DbException.convert(e);
    }
    if (filterCondition != null) {
        filterCondition = filterCondition.optimize(session);
    }
    return this;
}
Also used : SQLException(java.sql.SQLException) UserAggregate(org.h2.engine.UserAggregate) Aggregate(org.h2.api.Aggregate)

Example 20 with Aggregate

use of org.h2.api.Aggregate in project h2database by h2database.

the class TestFunctions method testAggregateType.

private void testAggregateType() throws SQLException {
    deleteDb("functions");
    Connection conn = getConnection("functions");
    Statement stat = conn.createStatement();
    stat.execute("CREATE AGGREGATE SIMPLE_MEDIAN FOR \"" + MedianStringType.class.getName() + "\"");
    stat.execute("CREATE AGGREGATE IF NOT EXISTS SIMPLE_MEDIAN FOR \"" + MedianStringType.class.getName() + "\"");
    ResultSet rs = stat.executeQuery("SELECT SIMPLE_MEDIAN(X) FROM SYSTEM_RANGE(1, 9)");
    rs.next();
    assertEquals("5", rs.getString(1));
    rs = stat.executeQuery("SELECT SIMPLE_MEDIAN(X) FILTER (WHERE X > 2) FROM SYSTEM_RANGE(1, 9)");
    rs.next();
    assertEquals("6", rs.getString(1));
    conn.close();
    if (config.memory) {
        return;
    }
    conn = getConnection("functions");
    stat = conn.createStatement();
    stat.executeQuery("SELECT SIMPLE_MEDIAN(X) FROM SYSTEM_RANGE(1, 9)");
    DatabaseMetaData meta = conn.getMetaData();
    rs = meta.getProcedures(null, null, "SIMPLE_MEDIAN");
    assertTrue(rs.next());
    assertFalse(rs.next());
    rs = stat.executeQuery("SCRIPT");
    boolean found = false;
    while (rs.next()) {
        String sql = rs.getString(1);
        if (sql.contains("SIMPLE_MEDIAN")) {
            found = true;
        }
    }
    assertTrue(found);
    stat.execute("DROP AGGREGATE SIMPLE_MEDIAN");
    stat.execute("DROP AGGREGATE IF EXISTS SIMPLE_MEDIAN");
    conn.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) CallableStatement(java.sql.CallableStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SimpleResultSet(org.h2.tools.SimpleResultSet) DatabaseMetaData(java.sql.DatabaseMetaData)

Aggregations

UserAggregate (org.h2.engine.UserAggregate)8 Expression (org.h2.expression.Expression)6 ValueExpression (org.h2.expression.ValueExpression)6 Aggregate (org.h2.api.Aggregate)5 Connection (java.sql.Connection)4 ResultSet (java.sql.ResultSet)4 SQLException (java.sql.SQLException)4 Statement (java.sql.Statement)4 SimpleResultSet (org.h2.tools.SimpleResultSet)4 ValueString (org.h2.value.ValueString)4 CallableStatement (java.sql.CallableStatement)3 PreparedStatement (java.sql.PreparedStatement)3 ConditionInSelect (org.h2.expression.ConditionInSelect)3 Column (org.h2.table.Column)3 IndexColumn (org.h2.table.IndexColumn)3 DatabaseMetaData (java.sql.DatabaseMetaData)2 AlterTableAddConstraint (org.h2.command.ddl.AlterTableAddConstraint)2 AlterTableAlterColumn (org.h2.command.ddl.AlterTableAlterColumn)2 DropFunctionAlias (org.h2.command.ddl.DropFunctionAlias)2 DropSchema (org.h2.command.ddl.DropSchema)2