Srtring Templates experiment for JDBC

John Hendrikx hjohn at xs4all.nl
Wed Jun 12 14:43:48 UTC 2024


Hi Amber list,

I know the discussion on String Templates has come a bit to a halt.  I 
just wanted to add my perspective after developing a library which uses 
the JDK 22 String Templates.  I found the processor syntax, although a 
bit unintuitive at first, quite a nice fit for handling SQL queries with 
a thin wrapper around JDBC.  The processor and current transactional 
scope can be nicely wrapped into a single reference, so you hardly 
notice there is a processor involved.

Where previously I may have written:

       try (Transaction tx = db.beginTransaction()) {
           List<Row> rows = tx.perform("SELECT * FROM employees WHERE a = 
?", a).toList();
       }

I now write:

       try (Transaction tx = db.beginTransaction()) {
           List<Row> rows = tx."SELECT * FROM employees WHERE a = 
\{a}".toList();
       }

Where the Transaction class implements the processor.

I've been reading the chatter on the experts list, and am especially 
weary of solution that would not allow for an empty template. For 
example, it should be easily possible to have a template without 
parameters without having to change to a different syntax:

        int count = tx."SELECT COUNT(*) FROM employees".asInt().get();

As this may evolve to for example:

        int count = tx."SELECT COUNT(*) FROM employees WHERE archived = 
\{archived}".asInt().get();

(Or vice versa)

For those interested in how I've been using templates, the library is 
found here: https://github.com/hjohn/TemplatedJDBC -- I bit the bullet 
and converted a lot of other (internal) projects to use this syntax, and 
I found it to work quite well.  I'm looking forward to seeing more 
progress in this area and to test the next iteration of String Templates 
when it becomes available.

--John
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.openjdk.org/pipermail/amber-dev/attachments/20240612/9884a37d/attachment.htm>


More information about the amber-dev mailing list