jdbi icon indicating copy to clipboard operation
jdbi copied to clipboard

@Insert-like feature from rooms?

Open gabzim opened this issue 6 years ago • 8 comments

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.

gabzim avatar Apr 05 '18 20:04 gabzim

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.

stevenschlansker avatar Apr 05 '18 20:04 stevenschlansker

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.

gabzim avatar Apr 05 '18 21:04 gabzim

@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).

qualidafial avatar Apr 09 '18 19:04 qualidafial

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

stevenschlansker avatar Apr 09 '18 19:04 stevenschlansker

Right, but those take column names as inputs and produce java names as outputs. We need something in the reverse direction.

qualidafial avatar Apr 09 '18 20:04 qualidafial

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.

stevenschlansker avatar Apr 09 '18 21:04 stevenschlansker

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.

qualidafial avatar Apr 09 '18 22:04 qualidafial

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 :).

dajulia3 avatar Aug 10 '19 12:08 dajulia3