<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <!-- Copyright 2004-2008 H2 Group. Multiple-Licensed under the H2 License, Version 1.0, and under the Eclipse Public License, Version 1.0 (http://h2database.com/html/license.html). Initial Developer: H2 Group --> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head><meta http-equiv="Content-Type" content="text/html;charset=utf-8" /><title> JaQu </title><link rel="stylesheet" type="text/css" href="stylesheet.css" /> <!-- [search] { --> <script type="text/javascript" src="navigation.js"></script> </head><body onload="frameMe();"> <table class="content"><tr class="content"><td class="content"><div class="contentDiv"> <!-- } --> <h1>JaQu</h1> <h2>What is JaQu</h2> <p> JaQu stands for Java Query and allows to access databases using pure Java. JaQu provides a fluent interface (or internal DSL) for building SQL statements. JaQu replaces SQL, JDBC, and object/relation frameworks such as Hibernate. JaQu is something like LINQ for Java (LINQ stands for "language integrated query" and is a Microsoft .NET technology). The following JaQu code: </p> <pre> Product p = new Product(); List<Product> soldOutProducts = db.from(p).where(p.unitsInStock).is(0).select(); </pre> <p> stands for the SQL statement: </p> <pre> SELECT * FROM PRODUCTS P WHERE P.UNITS_IN_STOCK = 0 </pre> <h2>Advantages and Differences to other Data Access Tools</h2> <p> Unlike SQL, JaQu can be easily integrated in Java applications. Because JaQu is pure Java, Javadoc and auto-complete are supported. Type checking is performed by the compiler. JaQu fully protects against SQL injection. </p> <p> JaQu is much smaller than object/relation mapping tools such as Hibernate. Unlike iBatis and Hibernate, no XML or annotation based configuration is required; instead the configuration (if required at all) is done in pure Java, in the application itself. </p> <p> JaQu does not require or contain any data caching mechanism. Like JDBC and iBatis, JaQu provides full control over when and what SQL statements are executed. </p> <h3>Restrictions</h3> <p> Primitive types (eg. boolean, int, long, double) are not supported. Instead, Boolean, Integer, Long, and Double must be used. </p> <h3>Why in Java?</h3> <p> Most people use Java in their application. Mixing Java and another language (for example Scala or Groovy) in the same application is complicated. It would be required to split the code to access the database and the application code. </p> <h2>Current State</h2> <p> JaQu is not yet stable, and not part of the h2.jar file. However the source code is included in H2, under: </p> <ul><li>src/test/org/h2/test/jaqu/* (samples and tests) </li><li>src/tools/org/h2/jaqu/* (framework) </li></ul> <h2>Building the JaQu library</h2> <p> To create the JaQu jar file, run: <code>build jarJaqu</code>. This will create the file <code>bin/h2jaqu.jar</code>. </p> <h2>Requirements</h2> <p> JaQu requires Java 1.5. Annotations are not need. Currently, JaQu is only tested with the H2 database engine, however in theory it should work with any database that supports the JDBC API. </p> <h2>Example Code</h2> <pre> package org.h2.test.jaqu; import java.math.BigDecimal; import java.util.List; import org.h2.jaqu.Db; import static org.h2.jaqu.Function.*; public class Test { Db db; public static void main(String[] args) throws Exception { new SamplesTest().test(); } public void test() throws Exception { db = Db.open("jdbc:h2:mem:", "sa", "sa"); db.insertAll(Product.getProductList()); db.insertAll(Customer.getCustomerList()); db.insertAll(Order.getOrderList()); testLength(); testCount(); testGroup(); testSelectManyCompoundFrom2(); testWhereSimple4(); testSelectSimple2(); testAnonymousTypes3(); testWhereSimple2(); testWhereSimple3(); db.close(); } private void testWhereSimple2() throws Exception { Product p = new Product(); List<Product> soldOutProducts = db.from(p). where(p.unitsInStock).is(0). orderBy(p.productId).select(); } private void testWhereSimple3() throws Exception { Product p = new Product(); List<Product> expensiveInStockProducts = db.from(p). where(p.unitsInStock).bigger(0). and(p.unitPrice).bigger(3.0). orderBy(p.productId).select(); } private void testWhereSimple4() throws Exception { Customer c = new Customer(); List<Customer> waCustomers = db.from(c). where(c.region).is("WA"). select(); } private void testSelectSimple2() throws Exception { Product p = new Product(); List<String> productNames = db.from(p). orderBy(p.productId).select(p.productName); List<Product> products = Product.getProductList(); } public static class ProductPrice { public String productName; public String category; public Double price; } private void testAnonymousTypes3() throws Exception { final Product p = new Product(); List<ProductPrice> productInfos = db.from(p).orderBy(p.productId). select(new ProductPrice() { { productName = p.productName; category = p.category; price = p.unitPrice; }}); List<Product> products = Product.getProductList(); } public static class CustOrder { public String customerId; public Integer orderId; public BigDecimal total; } private void testSelectManyCompoundFrom2() throws Exception { final Customer c = new Customer(); final Order o = new Order(); List<CustOrder> orders = db.from(c). innerJoin(o).on(c.customerId).is(o.customerId). where(o.total).smaller(new BigDecimal("500.00")). orderBy(1). select(new CustOrder() { { customerId = c.customerId; orderId = o.orderId; total = o.total; }}); } private void testLength() throws Exception { Product p = new Product(); List<Integer> lengths = db.from(p). where(length(p.productName)).smaller(10). orderBy(1). selectDistinct(length(p.productName)); } private void testCount() throws Exception { long count = db.from(new Product()).selectCount(); } public static class ProductGroup { public String category; public Long productCount; } private void testGroup() throws Exception { final Product p = new Product(); List<ProductGroup> list = db.from(p). groupBy(p.category). orderBy(1). select(new ProductGroup() { { category = p.category; productCount = count(); }}); } } </pre> <h2>Configuration</h2> <p> JaQu does not require any kind of configuration is you want to use the default mapping. To define table indices, or if you want to map a class to a table with a different name, or a field to a column with another name, create a function called 'define' in the data class. Example: </p> <pre> public class Product implements Table { public Integer productId; public String productName; public String category; public Double unitPrice; public Integer unitsInStock; public void define() { tableName("Product"); primaryKey(productId); index(productName, category); } </pre> <p> The method 'define()' contains the mapping definition. It is called once when the class is used for the first time. Like annotations, the mapping is defined in the class itself. Unlike when using annotations, the compiler can check the syntax even for multi-column objects (multi-column indexes, multi-column primary keys and so on). This solution is very flexible because the definition is written in regular Java code: Unlike when using annotations, your code can select the right configuration depending on the environment if required. Unlike XML mapping configuration, the configuration is integrated in the class itself. </p> <h2>Ideas</h2> <p> This project has just been started, and nothing is fixed yet. Some ideas for what to implement include: </p> <ul><li>Support queries on collections (instead of using a database). </li><li>Provide API level compatibility with JPA (so that JaQu can be used as an extension of JPA). </li><li>Internally use a JPA implementation (for example Hibernate) instead of SQL directly. </li><li>Use PreparedStatements and cache them. </li></ul> <h2>Related Projects</h2> <p> <a href="http://www.jequel.de/">JEQUEL: Java Embedded QUEry Language</a><br /> <a href="http://quaere.codehaus.org/">Quaere</a><br /> <a href="http://svn.quaere.codehaus.org/browse/~raw,r=76/quaere/trunk/Quaere/src/test/java/org/quaere/alias/test/SamplesTest.java">Quaere (Alias implementation)</a><br /> <a href="http://josql.sourceforge.net/">JoSQL</a><br /> <a href="http://groups.google.de/group/jlinq">Google Group about adding LINQ features to Java</a><br /> </p> <!-- [close] { --></div></td></tr></table><!-- } --><!-- analytics --></body></html>