UUID type support for @Bind sql query of jdbi library

 Jdbi library is quite popular and used a lot to connect to different DBs (more about it in official doc). 

And it supports a lot of types as request parameters in query requests, but not UUID (for MS SQL at least).

Sad. But a solution exists (it's even documented:). To do this, we have to do a little...

First of all, create a new class:

 public class UUIDArgumentFactory extends AbstractArgumentFactory<UUID> {
    UUIDArgumentFactory() {
        super(Types.VARCHAR);
    }
 
    @Override
    protected Argument build(UUID value, ConfigRegistry config) {
                               .setString(position, value.toString());
    }
}
        return (position, statement, ctx) -> statement

And use it: 

...

jdbi.registerArgument(new UUIDArgumentFactory());

jdbi.open();

... 

 

And that's it! Profit👍

Now we could use UUID in our requests like:

@SqlQuery("SELECT * FROM [db].[Users] WITH (nolock) " +
"WHERE UserID=:Id ORDER BY CreatedDate desc")
@RegisterFieldMapper(UserDTO.class)
UserDTO getUserById(@Bind("Id") UUID searchId);

 

No more .toString() for cast UUID to String in query call.💗