| 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 |
* <p> |
| 15 |
* Please note this interface only has limited support for data types. |
| 16 |
* If you need data types that don't have a corresponding SQL type |
| 17 |
* (for example GEOMETRY), then use the {@link Aggregate} interface. |
| 18 |
* </p> |
| 19 |
*/ |
| 20 |
public interface AggregateFunction { |
| 21 |
|
| 22 |
/** |
| 23 |
* This method is called when the aggregate function is used. |
| 24 |
* A new object is created for each invocation. |
| 25 |
* |
| 26 |
* @param conn a connection to the database |
| 27 |
* @throws SQLException on SQL exception |
| 28 |
*/ |
| 29 |
default void init(Connection conn) throws SQLException { |
| 30 |
// Do nothing by default |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* This method must return the SQL type of the method, given the SQL type of |
| 35 |
* the input data. The method should check here if the number of parameters |
| 36 |
* passed is correct, and if not it should throw an exception. |
| 37 |
* |
| 38 |
* @param inputTypes the SQL type of the parameters, {@link java.sql.Types} |
| 39 |
* @return the SQL type of the result |
| 40 |
* @throws SQLException on failure |
| 41 |
*/ |
| 42 |
int getType(int[] inputTypes) throws SQLException; |
| 43 |
|
| 44 |
/** |
| 45 |
* This method is called once for each row. |
| 46 |
* If the aggregate function is called with multiple parameters, |
| 47 |
* those are passed as array. |
| 48 |
* |
| 49 |
* @param value the value(s) for this row |
| 50 |
* @throws SQLException on failure |
| 51 |
*/ |
| 52 |
void add(Object value) throws SQLException; |
| 53 |
|
| 54 |
/** |
| 55 |
* This method returns the computed aggregate value. This method must |
| 56 |
* preserve previously added values and must be able to reevaluate result if |
| 57 |
* more values were added since its previous invocation. |
| 58 |
* |
| 59 |
* @return the aggregated value |
| 60 |
* @throws SQLException on failure |
| 61 |
*/ |
| 62 |
Object getResult() throws SQLException; |
| 63 |
|
| 64 |
} |