jdbi
jdbi copied to clipboard
@Insert-like feature from rooms?
One thing I keep finding in my code is that for insert
statements (and for a total update
), I almost always end up just listing the fields that the bean has, even when I can bind the whole object.
Is it possible at all to implement a feature that will just generate a simple insert
query? is it outside the scope of the project? Now I don't mean complex hierarchies with multiple inserts etc, but just doing a bit of reflection and creating the insert query from the bean that is bound.
I think that's starting to exceed the scope of the project. If you build something that is both very nice and simple and straightforward, we might consider it as an add-on module, but until then we want to keep SQL generation outside of the control of jdbi. You should be able to prototype it as a JdbiPlugin
hopefully.
Yeah @stevenschlansker, I completely understand and that's something I like about jdbi, it doesn't try to be an ORM. In this particular scenario I thought maybe the requirement is so minimal (just listing fields for an insert), that it might bend the rules a little bit. I'll try to give it a shot and see if it looks simple enough to be considered.
@arg20 Some hints to give you a place to start if you want to attempt this:
SQL Objects use SqlLocator
to determine the SQL statement to run for each method. The default SQL locator pulls the SQL from annotations like @SqlUpdate
, but this can be overridden via getConfig(SqlObjects.class).setSqlLocator(yourSqlLocator)
, or via configuring annotations like @UseClasspathSqlLocator
.
I've only seen beans mentioned, but a general solution would ideally play nice with @BindBean
, @BindFields
, or @BindMethods
.
We do provide the @ColumnName
annotation in Jdbi core, but it might be nice if folks could configure a column naming strategy (e.g. convert camelCase
Java names to snake_case
column names).
we've got at least some things hooked in with column name strategies:
https://github.com/jdbi/jdbi/blob/master/core/src/main/java/org/jdbi/v3/core/mapper/reflect/SnakeCaseColumnNameMatcher.java
but of course it's far from complete
Right, but those take column names as inputs and produce java names as outputs. We need something in the reverse direction.
Not necessarily -- you could scan the db column names and the bean property names, and run it through this. Maybe we do need to add it and I'm not against it if so, just would like it to dovetail in with this existing support if possible.
I guess I didn't consider scanning the database metadata.
That's probably reasonable--provided we do some caching to avoid incurring that cost on every insert/update.
This feature would be great... has anyone started implementing this? If not, I might give it a shot.
@qualidafial is the guidance you provided still relevant for the latest version of jdbi? I haven't really dug into the internals, I'm only a happy user so far :).