Friday, January 16, 2015

Functional, fluent JDBC - FluentJdbc

Some projects relying on a relational database do just fine using a high-level ORM, some have need for more low-level control over the queries. Using JDBC directly for application development can be really cumbersome. Not because it's a bad API, the problem is it's so low-level it's really more suitable for tool- than application development .
 
There are many abstraction layer libraries over JDBC that add the much needed syntax sugar. Even so, sometimes I find it difficult to find a suitable one for the applications I work on. Either because they constrain in some way or have other unwanted properties (like transitive dependencies).

In some cases I implemented abstractions of my own and a pattern has started to emerge. I decided to collect those ideas and make it a library for easier reuse. A library that makes the most common SQL querying operations as simple, declarative as possible, is light-weight, has no dependencies. Most similar libraries don't make use of a fluent API which makes way for some interesting possibilities in terms of both readability and flexibility. Also I wanted to put some emphasis on easy integration to popular persistence / transaction management tools since the goal is to complement them, not necessarily replace them. As a result it blends quite well with things like JPA, Spring Data, Guice Persist, JEE, etc.

Some of FluentJdbc 's features: 
  • execution of select/insert/update/delete/alter/... statements as one-liners
  • parameter mapping (named, positional, supporting java.time, Optional, extension for custom types)
  • access to generated keys
  • automatic result -> pojo mapping
  • transaction handling
  • big data (streaming select / insert / update)
Check out FluentJdbc's github page for usage details. The artifact is up in maven central, if you want to give it a shot:
<dependency>
    <groupid>org.codejargon</groupid>
    <artifactid>fluentjdbc</artifactid>
    <version>1.3</version>
</dependency>
Note: java 8 only

No comments:

Post a Comment