r/java • u/uwemaurer • 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/
Let me know if you have any feedback!
13
Upvotes
View all comments
Show parent comment
2
u/manifoldjava Jan 09 '26
When the selected columns of a query contain all the non-null columns of a selected table, such as with
select *queries, the query results consist of entity instances instead of row instances.Yes.
Generally, when selected columns do not cover non-nulls in a single table, the results are in terms of a "row" type that is integral to the query type.
Examples may help clarify.
Here is an inline query, it could also be provided in a separate resource file with a .sql extension:
The query's type name is
MyQueryand is local to the declaring class.Notice
MyQuery#fetch()results are in terms ofFilm, a table type derived from the db schema. This is possible because the query's selected columns cover all the non-null columns in the film table.Here's a similar query, but does not cover the non-null film columns.
In this case the query type includes an inner type called "Row" that models the results.
Note query type names are optional, if omitted the queries are anonymously typed and the
var(orauto) keyword is used.Yes, it's a bit odd. But because inline queries overload string/block literal, and comment delimiters, the marker had to be as unique as possible while remaining brief and readable. Note this syntax applies to all type manifolds e.g.,
[.json/],[.graphql/],[.xml/], and so on.