6 ms·
In the java-world there are libraries like JDBI, which makes it possible to write interfaces with SQL-annotations and have serialization, connection setup etc.
by fn1 6y ago
In the java-world there are libraries like JDBI, which makes it possible to write interfaces with SQL-annotations and have serialization, connection setup etc. done for you:
public interface UserDao {
@SqlUpdate("CREATE TABLE user (id INTEGER PRIMARY KEY, name VARCHAR)")
void createTable();
@SqlUpdate("INSERT INTO user(id, name) VALUES (?, ?)")
void insertPositional(int id, String name);
@SqlQuery("SELECT * FROM user ORDER BY name")
@RegisterBeanMapper(User.class)
List<User> listUsers();
}
This is great because it's explicit. No hidden queries.
- shock-value 6y agoAnother great option in Java is jOOQ, which lets you write type-safe and potentially composable queries such as: context .update(User.USER) .set(User.USER.NAME, userName) .where(User.USER.ID.eq(userId)) .execute()
- alex-lx 6y agojOOQ and its DSL is good, however IMO it's more readable using raw SQL (by using `context.fetchInto` and its variants) than to using DSL when deal with complex query.
- jwdunne 6y agoI’ve noticed this in other contexts too. For small queries with straight forward joins, a query builder is nice and readable. But for larger, more complex queries, I found putting the query into its own file was best for readability.
- lukaseder 6y agoWhy not just create views and query those with jOOQ, then?
- alex-lx 6y agohi, @lukaseder, Thank you for creating jOOQ. I don't have much experience on views, and some called `Best Practice` forbid to use that because it's hard to maintain. What do you think about that.
- JulianWasTaken 6y agoIn Python this would look like sqlalchemy's query layer (which is great).