jaqu.html 10.4 KB
Newer Older
1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2
<!--
Thomas Mueller's avatar
Thomas Mueller committed
3
Copyright 2004-2009 H2 Group. Multiple-Licensed under the H2 License, Version 1.0,
4
and under the Eclipse Public License, Version 1.0
5
(http://h2database.com/html/license.html).
6 7 8 9 10 11
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" />
12
<!-- [search] { -->
13 14 15
<script type="text/javascript" src="navigation.js"></script>
</head><body onload="frameMe();">
<table class="content"><tr class="content"><td class="content"><div class="contentDiv">
16
<!-- } -->
17 18 19 20 21

<h1>JaQu</h1>

<h2>What is JaQu</h2>
<p>
22
JaQu stands for Java Query and allows to access databases using pure Java.
23
JaQu provides a fluent interface (or internal DSL) for building SQL statements.
Thomas Mueller's avatar
Thomas Mueller committed
24
JaQu replaces SQL, JDBC, and persistence frameworks such as Hibernate.
25
JaQu is something like LINQ for Java (LINQ stands for "language integrated query" and is a
26 27
Microsoft .NET technology). The following JaQu code:
</p>
28
<pre class="notranslate">
29
Product p = new Product();
30
List&lt;Product&gt; soldOutProducts =
Thomas Mueller's avatar
Thomas Mueller committed
31
    db.from(p).where(p.unitsInStock).is(0).select();
32 33 34 35
</pre>
<p>
stands for the SQL statement:
</p>
36
<pre class="notranslate">
37
SELECT * FROM PRODUCTS P
38 39 40
WHERE P.UNITS_IN_STOCK = 0
</pre>

Thomas Mueller's avatar
Thomas Mueller committed
41
<h2>Advantages and Differences to Other Data Access Tools</h2>
42
<p>
43
Unlike SQL, JaQu can be easily integrated in Java applications. Because JaQu is pure Java,
Thomas Mueller's avatar
Thomas Mueller committed
44
auto-complete in the IDE and Javadoc and are supported. Type checking is performed by the compiler.
45
JaQu fully protects against SQL injection.
Thomas Mueller's avatar
Thomas Mueller committed
46 47
</p>
<p>
Thomas Mueller's avatar
Thomas Mueller committed
48
JaQu is much smaller than persistence frameworks such as Hibernate.
49
Unlike iBatis and Hibernate, no XML or annotation based configuration is required;
Thomas Mueller's avatar
Thomas Mueller committed
50 51 52
instead the configuration (if required at all) is done in pure Java, in the application itself.
</p>
<p>
53
JaQu does not require or contain any data caching mechanism. Like JDBC and iBatis,
Thomas Mueller's avatar
Thomas Mueller committed
54
JaQu provides full control over when and what SQL statements are executed.
55 56
</p>

57 58
<h3>Restrictions</h3>
<p>
Thomas Mueller's avatar
Thomas Mueller committed
59 60
Primitive types (eg. boolean, int, long, double) are not supported. Use
Boolean, Integer, Long, and Double instead.
61 62
</p>

63 64 65
<h3>Why in Java?</h3>
<p>
Most people use Java in their application. Mixing Java and another language (for example Scala or Groovy)
Thomas Mueller's avatar
Thomas Mueller committed
66
in the same application is complicated: you would need to split the application and database code.
67 68 69 70
</p>

<h2>Current State</h2>
<p>
71 72
Currently, JaQu is only tested with the H2 database. The API may change in future versions.
JaQu is not part of the h2 jar file, however the source code is included in H2, under:
73 74 75 76 77
</p>
<ul><li>src/test/org/h2/test/jaqu/* (samples and tests)
</li><li>src/tools/org/h2/jaqu/* (framework)
</li></ul>

78 79
<h2>Building the JaQu library</h2>
<p>
80
To create the JaQu jar file, run: <code class="notranslate">build jarJaqu</code>. This will create the file <code class="notranslate">bin/h2jaqu.jar</code>.
81 82
</p>

83 84 85
<h2>Requirements</h2>
<p>
JaQu requires Java 1.5. Annotations are not need.
86
Currently, JaQu is only tested with the H2 database engine, however in theory it should
87 88 89 90
work with any database that supports the JDBC API.
</p>

<h2>Example Code</h2>
91
<pre class="notranslate">
92 93 94 95 96 97 98
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 {
Thomas Mueller's avatar
Thomas Mueller committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    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();
124
        List&lt;Product&gt; soldOutProducts =
Thomas Mueller's avatar
Thomas Mueller committed
125 126 127 128 129 130 131
            db.from(p).
            where(p.unitsInStock).is(0).
            orderBy(p.productId).select();
    }

    private void testWhereSimple3() throws Exception {
        Product p = new Product();
132
        List&lt;Product&gt; expensiveInStockProducts =
Thomas Mueller's avatar
Thomas Mueller committed
133 134 135 136 137 138 139 140
            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();
141
        List&lt;Customer&gt; waCustomers =
Thomas Mueller's avatar
Thomas Mueller committed
142 143 144 145 146 147 148
            db.from(c).
            where(c.region).is("WA").
            select();
    }

    private void testSelectSimple2() throws Exception {
        Product p = new Product();
149
        List&lt;String&gt; productNames =
Thomas Mueller's avatar
Thomas Mueller committed
150 151 152 153 154 155 156 157 158 159 160 161
            db.from(p).
            orderBy(p.productId).select(p.productName);
    }

    public static class ProductPrice {
        public String productName;
        public String category;
        public Double price;
    }

    private void testAnonymousTypes3() throws Exception {
        final Product p = new Product();
162
        List&lt;ProductPrice&gt; productInfos =
Thomas Mueller's avatar
Thomas Mueller committed
163
            db.from(p).orderBy(p.productId).
Thomas Mueller's avatar
Thomas Mueller committed
164
            select(new ProductPrice() {{
Thomas Mueller's avatar
Thomas Mueller committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
                    productName = p.productName;
                    category = p.category;
                    price = p.unitPrice;
            }});
    }

    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();
180
        List&lt;CustOrder&gt; orders =
Thomas Mueller's avatar
Thomas Mueller committed
181 182 183 184
            db.from(c).
            innerJoin(o).on(c.customerId).is(o.customerId).
            where(o.total).smaller(new BigDecimal("500.00")).
            orderBy(1).
Thomas Mueller's avatar
Thomas Mueller committed
185
            select(new CustOrder() {{
Thomas Mueller's avatar
Thomas Mueller committed
186 187 188 189 190 191 192 193
                customerId = c.customerId;
                orderId = o.orderId;
                total = o.total;
            }});
    }

    private void testLength() throws Exception {
        Product p = new Product();
194
        List&lt;Integer&gt; lengths =
Thomas Mueller's avatar
Thomas Mueller committed
195
            db.from(p).
Thomas Mueller's avatar
Thomas Mueller committed
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
            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();
212
        List&lt;ProductGroup&gt; list =
Thomas Mueller's avatar
Thomas Mueller committed
213 214 215
            db.from(p).
            groupBy(p.category).
            orderBy(1).
Thomas Mueller's avatar
Thomas Mueller committed
216
            select(new ProductGroup() {{
Thomas Mueller's avatar
Thomas Mueller committed
217 218 219 220
                category = p.category;
                productCount = count();
            }});
    }
221 222 223 224

}
</pre>

225 226
<h2>Configuration</h2>
<p>
Thomas Mueller's avatar
Thomas Mueller committed
227
JaQu does not require any configuration when using the default mapping.
228
To define table indices, or if you want to map a class to a table with a different name,
229 230 231
or a field to a column with another name, create a function called 'define' in the data class.
Example:
</p>
232
<pre class="notranslate">
233 234 235 236 237 238 239
public class Product implements Table {

    public Integer productId;
    public String productName;
    public String category;
    public Double unitPrice;
    public Integer unitsInStock;
240

241 242 243 244 245
    public void define() {
        tableName("Product");
        primaryKey(productId);
        index(productName, category);
    }
Thomas Mueller's avatar
Thomas Mueller committed
246 247

}
248 249
</pre>
<p>
250
The method 'define()' contains the mapping definition. It is called once
251 252 253
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).
Thomas Mueller's avatar
Thomas Mueller committed
254 255 256
Because the definition is written in regular Java, the configuration can depend on the environment.
This is not possible using annotations.
Unlike XML mapping configuration, the configuration is integrated in the class itself.
257 258
</p>

259 260 261 262 263 264
<h2>Natural Syntax</h2>
<p>The plan is to support more natural (pure Java) syntax in conditions.
To do that, the condition class is de-compiled to a SQL condition.
A proof of concept decompiler is included (but it doesn't work yet).
The planned syntax is:
</p>
265
<pre class="notranslate">
266 267 268 269 270 271 272 273 274 275 276 277 278
long count = db.from(co).
    where(new Filter() { public boolean where() {
        return co.id == x
            &amp;&amp; co.name.equals(name)
            &amp;&amp; co.value == new BigDecimal("1")
            &amp;&amp; co.amount == 1L
            &amp;&amp; co.birthday.before(new java.util.Date())
            &amp;&amp; co.created.before(java.sql.Timestamp.valueOf("2005-05-05 05:05:05"))
            &amp;&amp; co.time.before(java.sql.Time.valueOf("23:23:23"));
        } }).selectCount();
</pre>

<h2>Other Ideas</h2>
279 280
<p>
This project has just been started, and nothing is fixed yet.
Thomas Mueller's avatar
Thomas Mueller committed
281
Some ideas for what to implement are:
282
</p>
283 284
<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).
285 286 287 288 289
</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>
290
<p class="notranslate">
291
<a href="http://code.google.com/p/dreamsource-orm">Dreamsource ORM</a><br />
292 293
<a href="http://incubator.apache.org/empire-db/empiredb/empiredb.htm">Empire-db</a><br />
<a href="http://www.jequel.de">JEQUEL: Java Embedded QUEry Language</a><br />
294
<a href="http://joist.ws">Joist</a><br />
295 296
<a href="http://josql.sourceforge.net">JoSQL</a><br />
<a href="http://code.google.com/p/liquidform">LIQUidFORM</a><br />
297
<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 />
298 299 300
<a href="http://quaere.codehaus.org/">Quaere</a><br />
<a href="http://source.mysema.com/display/querydsl/Querydsl">Querydsl</a><br />
<a href="https://squill.dev.java.net">Squill</a><br />
301 302
</p>

303 304
<!-- [close] { --></div></td></tr></table><!-- } --><!-- analytics --></body></html>