A minimalistic Fluent SQL API for Java aimed to resemble the code to your original SQL code.
##Example
- Writing SQL code using string concatentation in Java
String sql =
" WITH LatestOrders AS (" +
" SELECT CustomerName , "+
" SUM ( COUNT ( ID ) ) ," +
" COUNT ( MAX ( n_items ) ) , " +
" Red as color" +
" FROM dbo.Orders" +
" RIGHT JOIN Customers" +
" on Orders.Customer_ID = Customers.ID " +
" LEFT JOIN Persons" +
" ON Persons.name = Customer.name" +
" AND Persons.lastName = Customer.lastName" +
" GROUP BY CustomerID" +
" ) "+
" SELECT "+
" Customers.* , "+
" Orders.OrderTime AS LatestOrderTime , "+
" ( SELECT COUNT ( * ) " +
" FROM dbo.OrderItems " +
" WHERE OrderID IN "+
" ( SELECT ID FROM dbo.Orders WHERE CustomerID = Customers.ID ) "+
" ) AS TotalItemsPurchased "+
" FROM dbo.Customers " +
" INNER JOIN dbo.Orders "+
" USING ID" +
" WHERE "+
" Orders.ID IN ( SELECT ID FROM LatestOrders )"+
" AND Orders.n_items > ? ";
- In fluent SQL:
SQL sql = WITH("LatestOrders" ,
SELECT("CustomerName")
.FN(SUM(COUNT("ID")))
.FN(COUNT(MAX("n_items")))
.FIELD("Red").AS("color")
.FROM("dbo.Orders")
.RIGHT_JOIN("Customers")
.ON("Orders.customer_ID" , "Customers.ID")
.LEFT_JOIN("Persons")
.ON("Persons.name" , "Customer.name")
.AND_ON("Persons.lastName" , "Customer.lastName")
.GROUP_BY("CustomerID")
)
.append(SELECT()
.FIELD("Customers.*")
.FIELD("Orders.OrderTime").AS("LatestOrderTime")
.FIELD(SELECT(COUNT("*"))
.FROM("dbo.OrderItems")
.WHERE("OrderID").IN(
SELECT("ID")
.FROM("dbo.Orders")
.WHERE("CustomerID").EQUAL_TO_FIELD("Customers.ID"))
).AS("TotalItemsPurchased")
.FROM("dbo.Customers")
.INNER_JOIN("dbo.Orders")
.USING("ID")
.WHERE("Orders.ID").IN(SELECT("ID").FROM("LatestOrders"))
.AND("Orders.n_items").GREATER_THAN(0)
);
Breakdown actual = sql.build();
- A clever usage of Java Static methods to write your SQL statement just as what you would expect.
- Easily write your SQL in java.
- It has almost no logic other than appending the SQL statements just like StringBuilder, and appending commas when it is obvious.
- It is not an automatic SQL builder that will automagically transform SQL strings to work accross multi-database platform.
- It is used by programmers who would rather write SQL statements than use ORM that gets in their way.
A SQL breakdown result:
- breakdown.getSql() (String) - the SQL string built
- breakdown.getParameters() (Object[]) - the resulted array of the parameters that is gathered by the SQL builder, with the correct order as they are mentioned in the query This will be used in as parameters in your preparedStatment
- stmt.setObject(i,parameter[i]), in turn SQL injection is already mitigated
- You can use the keyword API
SQL = SELECT("name", "description").FROM("mytable").
keyword(" SUPER AWESOME JOIN ").append("table3")
.ON("column1", "column2");
- You can incorporate it into the SQL.java class
- Simplest Usage:
Copy these 2 files here. Make it as part of your source code, so you can easily modify and add keywords intended for your Database platform.
- Maven project, If you are just using keywords that are already in there!
<dependency>
<groupId>com.ivanceras</groupId>
<artifactId>fluentsql</artifactId>
<version>0.0.6</version>
</dependency>
Take a look at the code in action here
https://news.ycombinator.com/item?id=5956867
https://github.com/ivanceras/fluentsql/tree/master/src/test/java/com/ivanceras/fluent
http://www.jooq.org/ ( This is polished but I wanted CAPSLOCK on the SQL keywords, also it automatically transform SQL statements for each platform )
http://code.google.com/p/squiggle-sql/ ( ..uhmm.. poor choice of words.. )
Apache2