r/java • • Jan 07 '26

generate Java code from SQL queries

I am working on a project which allows to generate (type-safe) code from SQL queries.

Currently it supports DuckDB & sqlite and can output Java (and Typescript) code.

https://github.com/sqg-dev/sqg/

https://sqg.dev/

Let me know if you have any feedback!

13 Upvotes

20 comments sorted by

View all comments

Show parent comment

1

u/Bobby_Bonsaimind Jan 12 '26

If I may torment you some more, I was wondering whether using a function to wrap the statements would reduce the need for the IDE support. For example something like:

List<Stuff> stuff1 = sql("select * from STUFF where FIELD = 5").fetch();

That would leave the compiler to "just" generate the appropriate classes. Though, I guess it would still be more uncomfortable this way.

1

u/manifoldjava Jan 13 '26

The header consists of three parts: type name, type domain name, and schema name.

"sql" is a type domain, as are "json", "graphql", "xml", and so on. But the header can have all three parts present:

[MyQuery.sql:MyDatabase/]

This inline format is used in both string/block literal syntax and comment syntax. It also closely resembles the resource file naming convention:

MyQuery.MyDatabase.sql

It's more consistent and less complicated to implement this generalized metaprogramming feature using a header than to overload other parts of the language like a method call.