Adapt query to different databases with JDBI

53 views
Skip to first unread message

Mattia Poli

unread,
Nov 25, 2020, 11:37:44 AM11/25/20
to jDBI
Hi everyone, i have to execute the same query for Postgres, MySql, SqlServer and Oracle.

in some table i have column named uid , so in some databases i can execute easily :
"SELECT * FROM table WHERE uid = :uid".
In oracle instead , in order to use uid i have to escape it, so the column name must be "uid".
Indeed the query must be something like : 
"SELECT * FROM table WHERE \"uid\" = :uid"

Is it possible somehow to tell jdbi to do this out of the box? 
Maybe with sqlStatementFactory or JDBI plugin i don't know.

Thanks for heping me.

Matthew Hall

unread,
Nov 27, 2020, 12:35:13 PM11/27/20
to jd...@googlegroups.com
I suggest using a template engine like Freemarker or StringTemplate, so you can put all variants of each statement in the same SQL templates, and use defined attributes to switch between database types.

Many queries will be the same for all databases and require no template directives at all. You can define a single attribute e.g. "dbType" and switch off that attribute in the template when needed.


--
You received this message because you are subscribed to the Google Groups "jDBI" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jdbi+uns...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/jdbi/f0a4bbd2-4357-47a7-bbb6-168af37b5687n%40googlegroups.com.

Mattia Poli

unread,
Nov 30, 2020, 5:07:53 AM11/30/20
to jDBI
yes in fact in these days i saw that too.

Thanks
Reply all
Reply to author
Forward
0 new messages