mirror of
https://github.com/sqlite/sqlite.git
synced 2025-07-30 19:03:16 +03:00
Add Java tests which ensure that xFinal() is called with no aggregate context when the result set is empty.
FossilOrigin-Name: 7ac8c66a6b62d6bb345e8b9957a26de463019ae7d30ff442f19482c3a6827fc7
This commit is contained in:
@ -58,6 +58,9 @@ public abstract class SQLFunction {
|
||||
ValueHolder<T> which can be used to modify that state directly
|
||||
without requiring that the client update the underlying map's
|
||||
entry.
|
||||
|
||||
T must be of a type which can be legally stored as a value in
|
||||
java.util.HashMap<KeyType,T>.
|
||||
*/
|
||||
public ValueHolder<T> getAggregateState(sqlite3_context cx, T initialValue){
|
||||
ValueHolder<T> rc = map.get(cx.getAggregateContext());
|
||||
|
@ -482,6 +482,10 @@ public class Tester1 {
|
||||
|
||||
private static void testUdfAggregate(){
|
||||
final sqlite3 db = createNewDb();
|
||||
final ValueHolder<Boolean> xFinalNull =
|
||||
// To confirm that xFinal() is called with no aggregate state
|
||||
// when the corresponding result set is empty.
|
||||
new ValueHolder<>(false);
|
||||
SQLFunction func = new SQLFunction.Aggregate<Integer>(){
|
||||
@Override
|
||||
public void xStep(sqlite3_context cx, sqlite3_value args[]){
|
||||
@ -490,8 +494,12 @@ public class Tester1 {
|
||||
@Override
|
||||
public void xFinal(sqlite3_context cx){
|
||||
final Integer v = this.takeAggregateState(cx);
|
||||
if(null == v) sqlite3_result_null(cx);
|
||||
else sqlite3_result_int(cx, v);
|
||||
if(null == v){
|
||||
xFinalNull.value = true;
|
||||
sqlite3_result_null(cx);
|
||||
}else{
|
||||
sqlite3_result_int(cx, v);
|
||||
}
|
||||
}
|
||||
};
|
||||
execSql(db, "CREATE TABLE t(a); INSERT INTO t(a) VALUES(1),(2),(3)");
|
||||
@ -506,6 +514,7 @@ public class Tester1 {
|
||||
affirm( 6 == v );
|
||||
++n;
|
||||
}
|
||||
affirm( false == xFinalNull.value );
|
||||
sqlite3_reset(stmt);
|
||||
// Ensure that the accumulator is reset...
|
||||
n = 0;
|
||||
@ -529,7 +538,11 @@ public class Tester1 {
|
||||
affirm( 12 == c1 );
|
||||
}
|
||||
affirm( 1 == n );
|
||||
affirm( false == xFinalNull.value );
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
execSql(db, "SELECT myfunc(1) WHERE 0");
|
||||
affirm( true == xFinalNull.value );
|
||||
sqlite3_close(db);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user