jdbi icon indicating copy to clipboard operation
jdbi copied to clipboard

Attribute isn't defined error

Open erikjara opened this issue 6 years ago • 3 comments

Hi everyone! I'm doing a query with StringTemplate4 like this:

    @SqlUpdate("UPDATE users " +
            "<if(first_name)>first_name = :first_name,<endif>" +
            "<if(last_name)>last_name = :last_name,<endif>" +
            "updated_at = :now WHERE user_id=:id")
    @RegisterConstructorMapper(User::class)
    @DefineNamedBindings @UseStringTemplateEngine @Timestamped @GetGeneratedKeys
    fun update(@Bind("id") id: Int, @BindMap map: Map<String, Any>): User

All runs flawless, but if I don't send "first_name" key, I get this message in console: attribute first_name isn't defined

Of course, I researched the reason and we need set a new STErrorListener to delete the message, inside this STGroup: https://github.com/jdbi/jdbi/blob/f3173087f9fbfd3a9c722fe9218fafbea506decd/stringtemplate4/src/main/java/org/jdbi/v3/stringtemplate4/StringTemplateEngine.java#L27

Here's any plan to include this configuration in JDBI? Thanks!

erikjara avatar Jul 09 '19 19:07 erikjara

Bindings and defined attributes are separate elements. Bindings are used for data and attributes are for templating.

We have a @DefineBinds annotation that probably does what you want.

qualidafial avatar Jul 09 '19 19:07 qualidafial

@qualidafial can you send me the anchor link please? I can't find it in docs. BTW, if I remove @DefineNamedBindings my code doesn't work :(

erikjara avatar Jul 09 '19 20:07 erikjara

Ok, I didn't read carefully enough the first time. DefineNamedBindings is the annotation name I was looking for.

I think we could accommodate a config option to let you configure an STGroup

qualidafial avatar Jul 09 '19 22:07 qualidafial

Hello Guys,

this ticket is really old and the number of reactions is very low. Therefore, I doubt that there is an urgent need to fix the problem (besides, in the end it is only messy log-noise).

I think we could accommodate a config option to let you configure an STGroup

I guess there is still no such config option. Is there a good way to inject that necessary STErrorListener? The solution offered in this StackOverflow answer is too vague (at least for me).

mieseprem avatar Mar 22 '23 15:03 mieseprem

@mieseprem , please check out #2304 and see if that helps you

stevenschlansker avatar Mar 22 '23 22:03 stevenschlansker

Hello @stevenschlansker ! Thanks for having an eye in this ❤️

case NO_SUCH_ATTRIBUTE:
    if (!ctx.getConfig(StringTemplates.class).failOnMissingAttribute()) {
        break;
    }

This part will - as long I don't configure to fail on missing attributes - silently ignore missing attributes. That would indeed mitigate the unnecessary log-noise.

mieseprem avatar Mar 23 '23 15:03 mieseprem