| 1 |
/* |
| 2 |
* Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0, |
| 3 |
* and the EPL 1.0 (https://h2database.com/html/license.html). |
| 4 |
* Initial Developer: H2 Group |
| 5 |
*/ |
| 6 |
package org.h2.schema; |
| 7 |
|
| 8 |
import java.sql.Connection; |
| 9 |
import java.sql.SQLException; |
| 10 |
|
| 11 |
import org.h2.api.Aggregate; |
| 12 |
import org.h2.api.AggregateFunction; |
| 13 |
import org.h2.engine.DbObject; |
| 14 |
import org.h2.engine.SessionLocal; |
| 15 |
import org.h2.message.DbException; |
| 16 |
import org.h2.message.Trace; |
| 17 |
import org.h2.util.JdbcUtils; |
| 18 |
import org.h2.util.StringUtils; |
| 19 |
import org.h2.value.DataType; |
| 20 |
import org.h2.value.TypeInfo; |
| 21 |
|
| 22 |
/** |
| 23 |
* Represents a user-defined aggregate function. |
| 24 |
*/ |
| 25 |
public final class UserAggregate extends UserDefinedFunction { |
| 26 |
|
| 27 |
private Class<?> javaClass; |
| 28 |
|
| 29 |
public UserAggregate(Schema schema, int id, String name, String className, |
| 30 |
boolean force) { |
| 31 |
super(schema, id, name, Trace.FUNCTION); |
| 32 |
this.className = className; |
| 33 |
if (!force) { |
| 34 |
getInstance(); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
public Aggregate getInstance() { |
| 39 |
if (javaClass == null) { |
| 40 |
javaClass = JdbcUtils.loadUserClass(className); |
| 41 |
} |
| 42 |
Object obj; |
| 43 |
try { |
| 44 |
obj = javaClass.getDeclaredConstructor().newInstance(); |
| 45 |
Aggregate agg; |
| 46 |
if (obj instanceof Aggregate) { |
| 47 |
agg = (Aggregate) obj; |
| 48 |
} else { |
| 49 |
agg = new AggregateWrapper((AggregateFunction) obj); |
| 50 |
} |
| 51 |
return agg; |
| 52 |
} catch (Exception e) { |
| 53 |
throw DbException.convert(e); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
@Override |
| 58 |
public String getDropSQL() { |
| 59 |
StringBuilder builder = new StringBuilder("DROP AGGREGATE IF EXISTS "); |
| 60 |
return getSQL(builder, DEFAULT_SQL_FLAGS).toString(); |
| 61 |
} |
| 62 |
|
| 63 |
@Override |
| 64 |
public String getCreateSQL() { |
| 65 |
StringBuilder builder = new StringBuilder("CREATE FORCE AGGREGATE "); |
| 66 |
getSQL(builder, DEFAULT_SQL_FLAGS).append(" FOR "); |
| 67 |
return StringUtils.quoteStringSQL(builder, className).toString(); |
| 68 |
} |
| 69 |
|
| 70 |
@Override |
| 71 |
public int getType() { |
| 72 |
return DbObject.AGGREGATE; |
| 73 |
} |
| 74 |
|
| 75 |
@Override |
| 76 |
public synchronized void removeChildrenAndResources(SessionLocal session) { |
| 77 |
database.removeMeta(session, getId()); |
| 78 |
className = null; |
| 79 |
javaClass = null; |
| 80 |
invalidate(); |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Wrap {@link AggregateFunction} in order to behave as |
| 85 |
* {@link org.h2.api.Aggregate} |
| 86 |
**/ |
| 87 |
private static class AggregateWrapper implements Aggregate { |
| 88 |
private final AggregateFunction aggregateFunction; |
| 89 |
|
| 90 |
AggregateWrapper(AggregateFunction aggregateFunction) { |
| 91 |
this.aggregateFunction = aggregateFunction; |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public void init(Connection conn) throws SQLException { |
| 96 |
aggregateFunction.init(conn); |
| 97 |
} |
| 98 |
|
| 99 |
@Override |
| 100 |
public int getInternalType(int[] inputTypes) throws SQLException { |
| 101 |
int[] sqlTypes = new int[inputTypes.length]; |
| 102 |
for (int i = 0; i < inputTypes.length; i++) { |
| 103 |
sqlTypes[i] = DataType.convertTypeToSQLType(TypeInfo.getTypeInfo(inputTypes[i])); |
| 104 |
} |
| 105 |
return DataType.convertSQLTypeToValueType(aggregateFunction.getType(sqlTypes)); |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public void add(Object value) throws SQLException { |
| 110 |
aggregateFunction.add(value); |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public Object getResult() throws SQLException { |
| 115 |
return aggregateFunction.getResult(); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
} |