| 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.api; |
| 7 |
|
| 8 |
import java.sql.Connection; |
| 9 |
import java.sql.SQLException; |
| 10 |
|
| 11 |
/** |
| 12 |
* A user-defined aggregate function needs to implement this interface. |
| 13 |
* The class must be public and must have a public non-argument constructor. |
| 14 |
*/ |
| 15 |
public interface Aggregate { |
| 16 |
|
| 17 |
/** |
| 18 |
* This method is called when the aggregate function is used. |
| 19 |
* A new object is created for each invocation. |
| 20 |
* |
| 21 |
* @param conn a connection to the database |
| 22 |
* @throws SQLException on SQL exception |
| 23 |
*/ |
| 24 |
default void init(Connection conn) throws SQLException { |
| 25 |
// Do nothing by default |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* This method must return the H2 data type, {@link org.h2.value.Value}, |
| 30 |
* of the aggregate function, given the H2 data type of the input data. |
| 31 |
* The method should check here if the number of parameters |
| 32 |
* passed is correct, and if not it should throw an exception. |
| 33 |
* |
| 34 |
* @param inputTypes the H2 data type of the parameters, |
| 35 |
* @return the H2 data type of the result |
| 36 |
* @throws SQLException if the number/type of parameters passed is incorrect |
| 37 |
*/ |
| 38 |
int getInternalType(int[] inputTypes) throws SQLException; |
| 39 |
|
| 40 |
/** |
| 41 |
* This method is called once for each row. |
| 42 |
* If the aggregate function is called with multiple parameters, |
| 43 |
* those are passed as array. |
| 44 |
* |
| 45 |
* @param value the value(s) for this row |
| 46 |
* @throws SQLException on failure |
| 47 |
*/ |
| 48 |
void add(Object value) throws SQLException; |
| 49 |
|
| 50 |
/** |
| 51 |
* This method returns the computed aggregate value. This method must |
| 52 |
* preserve previously added values and must be able to reevaluate result if |
| 53 |
* more values were added since its previous invocation. |
| 54 |
* |
| 55 |
* @return the aggregated value |
| 56 |
* @throws SQLException on failure |
| 57 |
*/ |
| 58 |
Object getResult() throws SQLException; |
| 59 |
|
| 60 |
} |