Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Jdbi should support function arguments similar to consumer arguments #2326

Closed
hgschmie opened this issue Apr 20, 2023 · 2 comments
Closed

Jdbi should support function arguments similar to consumer arguments #2326

hgschmie opened this issue Apr 20, 2023 · 2 comments

Comments

@hgschmie
Copy link
Contributor

We support

@SqlQuery("...") 
@UseRowMapper(SomethingMapper.class)
void foo(Consumer<Something> somethingConsumer); 

and especially

@SqlQuery("...") 
@UseRowMapper(SomethingMapper.class)
void foo(Consumer<Stream<Something>> somethingConsumer); 

for methods today. Jdbi should also support

@SqlQuery("...") 
@UseRowMapper(SomethingMapper.class)
ResultThing foo(Function<Stream<Something>, ResultThing> somethingFunction); 

where the right side of the Function argument must match the return type of the method. This would make some of the current stream/iterator consumption code much simpler:

List<Something> result = new ArrayList();
jdbi.withExtension(SomethingDao.class, dao -> dao.loadAllStuff(stream -> stream.forEach(result::add)));

interface SomethingDao {
    @SqlQuery("...")
    void loadAllStuff(Consumer<Stream<Something>> consumer);
}

would become

List<Something> result = jdbi.withExtension(SomethingDao.class, 
dao -> dao.loadAllStuff(stream -> stream.collect(Collectors.toList())));

interface SomethingDao {
    @SqlQuery("...")
    List<Something> loadAllStuff(Function<Stream<Something>, List<Something>> function);
}

This requires some work in the ResultReturner where the consumer argument detection code is located. It should also enforce the <S, T> T someMethod(Function<S, T> functionArgument) pattern.

@hgschmie
Copy link
Contributor Author

There is a lot of code in SqlCallHandler#createResultTransformer that already does most of the work here. This should be aligned to the code in ResultReturner that deals with all the special cases for the consumers.

Also the SqlCallHandler code should probably not do its own thing for functions and consumers by calling Call#invoke() but use the Call#invoke(Consumer<>) and Call#invoke(Function<>) methods directly

hgschmie added a commit to hgschmie/jdbi that referenced this issue Oct 6, 2023
Similar to `Consumer<...>` arguments, a function argument consumes the
results of a SQL query. The return value from applying the function is
returned by the SQL object method. This is especially useful for
collecting and managing streaming data as the function is called while
the database connection is live.

Addresses jdbi#2326
hgschmie added a commit to hgschmie/jdbi that referenced this issue Oct 6, 2023
Similar to `Consumer<...>` arguments, a function argument consumes the
results of a SQL query. The return value from applying the function is
returned by the SQL object method. This is especially useful for
collecting and managing streaming data as the function is called while
the database connection is live.

Addresses jdbi#2326
hgschmie added a commit to hgschmie/jdbi that referenced this issue Oct 7, 2023
Similar to `Consumer<...>` arguments, a function argument consumes the
results of a SQL query. The return value from applying the function is
returned by the SQL object method. This is especially useful for
collecting and managing streaming data as the function is called while
the database connection is live.

Addresses jdbi#2326
@hgschmie
Copy link
Contributor Author

Shipped in 3.41.x

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

1 participant