Introducing Quaere - Language integrated queryies for Java

Published 11 September 07 10:35 PM | andersnoras 

Earlier today, I unveiled the Quaere project during my talk at the JavaZone conference. Quaere is an internal domain specific language that adds a querying syntax reminiscent of SQL to Java applications. The language is modeled with basis in Microsoft’s Language Integrated Query (LINQ) project, which adds similar capabilities to a range of Microsoft .NET languages. The Quaere project allows users to use an internal DSL to filter, enumerate and create projections over a number of collections and other queryable resources using a common, expressive syntax.

Features The Quaere DSL is very flexible and it lets you perform a wide range of queries against any data structure that is an array, or implements the java.lang.Iterable or the org.quaere.Queryable interface. Below is an overview of the querying and other features available through the DSL interface, the underlying query expression model and query engine. See the examples section to gain an understanding of how these features are used.

  • Ability to perform queries against arrays or data structure implementing the Iterable interface.
  • An internal DSL (based on static imports and fluent interfaces) that lets you integrate the query language with regular Java code. No preprocessing or code generation steps are required to use the DSL, simply add a reference to the quaere.jar file (and its dependencies).
  • A large number of querying operators including restriction, selection, projection, set, partitioning, grouping, ordering, quantification, aggregation and conversion operators.
  • Support for lambda expressions.
  • The ability to dynamically define and instantiate anonymous classes.
  • Many new “keywords” for Java 1.5 and later.

Examples To import the DSL into any Java class, simply add the following import to the class: import static org.quaere.DSL.*;

Below is an example of a simple query that selects the numbers less than five from an array of integers:

Integer[] numbers = {5, 4, 1, 3, 9, 8, 6, 7, 2, 0};
Iterable<Integer> lowNums =
from("n").in(numbers).
where(lt("n", 5)).
select("n");

This query uses a projection expression to select the names of all products in the array:


List<Product> products = Arrays.asList(Product.getAllProducts());
Iterable<String> productNames =
from("p").in(products).
select("p.getProductName()");

The following query creates a sequence anonymous class instances with two properties containing the upper and lower case versions of the words in an array. The classes in the upperLowerWords collection will have two strongly typed JavaBean properties:

String[] words = {"aPPLE", "BlUeBeRrY", "cHeRry"};
Iterable<Variant> upperLowerWords =
from("w").in(words).
select(
create(
property("upper", "w.toUpperCase()"),
property("lower", "w.toLowerCase()")
)
);

The following query uses a compound from clause to select all pairs of numbers from two arrays such that the number from numbersA is less than the number from numbersB:

Integer[] numbersA = {0, 2, 4, 5, 6, 8, 9};
Integer[] numbersB = {1, 3, 5, 7, 8};
Iterable<Variant> pairs =
from("a").in(numbersA).
from("b").in(numbersB).
where(lt("a", "b")).
select(
create(
property("a"),
property("b")
)
);

For more examples take a look at the scenario test cases in the snapshot. The project has passing tests for almost all of the examples in Microsoft’s LINQ showcase applications.

License and Contributions The project is licensed under the Apache 2.0 License. The project is not yet hosted by any open source community, and is therefore only available as a downloadable snapshot. The snapshot is available here.

Filed under: ,

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Christian Vest Hansen said on September 12, 2007 3:29 PM:

Hmm.... That looks alrght. I've used a similar technique to implement a functional mini-language in Java.

One thing, though: how far is it, feature wise, when we talk other datasources, such as relational databases, XML structures or *gasp* LDAP?

# andersnoras said on September 13, 2007 1:10 AM:

@Christian;

The snapshot only includes the Quaere for Objects query engine, but I also have a partial implementation of Quaere for Hibernate which I demoed when I gave my talk.

# Anders Norås' Blog said on September 13, 2007 1:40 AM:

Quaere has be elected Java library of the week on the Java Posse podcast. I haven&#8217;t had the time

# aurelije said on September 13, 2007 5:42 AM:

Similar library exist: http://josql.sourceforge.net/

# Noticias externas said on September 13, 2007 9:08 AM:

It&#39;s called Quaere (no idea what the name means), and it looks to be extremely similar in design

# Magnus said on September 13, 2007 10:02 AM:

This looks great... I really needed this in my project. Hope you put this on some sourceforge-like site and keep the project going...

# andersnoras said on September 13, 2007 12:58 PM:

@Magnus;

Thanks. The project will get a permanent home at a open source community. I'm working on this and I reckon it will be sorted in a week or so.

# Carl Rosenberger said on September 13, 2007 4:56 PM:

On a first look Quaere looks like you did some very nice work. Well done!

How much work would be it be to adapt Quaere to another database, let's say to an object database?

Weeks or months?

# andersnoras said on September 14, 2007 3:55 AM:

@Carl;

It took me about a month to write Quaere as it is now with both the DSL and the Quaere for Objects implementation. Quaere for Objects has been a lot of work because I had to write the query engine from scratch. In my talk at JavaZone I showed a proof of concept demo of Quaere for Hibernate, where the Quaere expression trees got translated to Hibernate criteria queries and executed via a Hibernate session. This demo was written in a couple of hours, but it only supported a subset of the language. There is some more info on how Quaere handles extensions in this post: http://andersnoras.com/blogs/anoras/archive/2007/09/13/answering-questions-about-quaere.aspx

The effort required to implement an adapter for a queryable resource depends on what the resources abilities and how many features you want to support. I reckon that it should be fairly easy to support Hibernate, JDO and similar because they already have good criteria APIs, while XML and similar would require more effort.

# TrackBack said on September 14, 2007 4:08 AM:
# TrackBack said on September 14, 2007 4:08 AM:
# TrackBack said on September 14, 2007 4:09 AM:
# Linq-alike in java? Quaere « Ole Morten Amundsen said on September 14, 2007 4:38 AM:

PingBack from http://olemortenamundsen.wordpress.com/2007/09/14/linq-alike-in-java-quaere/

# LINQ for Java « Justin Rudd’s Drivel said on September 14, 2007 3:33 PM:

PingBack from http://justinrudd.wordpress.com/2007/09/14/linq-for-java/

# LINQ desde Java » Innova Desarrollos informáticos said on September 15, 2007 1:10 AM:

PingBack from http://www.balearsinnovacio.com/blog/?p=232

# 窃听器 said on September 16, 2007 1:54 PM:

Good article, the author thanks!

# Marco Russo said on September 16, 2007 2:43 PM:

LINQ non è ancora stato rilasciato (lo sarà con .NET Framework 3.5 tra pochi mesi) e già si cominciano

# Stefano Paluello's Blog said on September 20, 2007 7:07 AM:

Quaere - Nasce Linq for Java

# Trond Aarø said on September 21, 2007 12:36 AM:

Nice work Anders! Good luck.

I think the language extension is the most interesting part of the LINQ prosject in .NET. Much of the fuzz about the ORM-part is shadowing this.

The declarativ language for working with collections of object is great!

# Unilever Centre for Molecular Informatics, Cambridge - petermr’s blog » Blog Archive » Semantic web : the scream! said on September 24, 2007 2:17 PM:

PingBack from http://wwmm.ch.cam.ac.uk/blogs/murrayrust/?p=634

# Anders Norås' Blog said on September 25, 2007 3:10 AM:

Even if InfoQ broke the news last week , I'm very pleased to announce that Quaere gotten a proper home

# Anders Norås' Blog said on September 25, 2007 4:26 AM:

When I was writing the early spikes for the Quaere project, I wrote a blog post entitled "The Poor Man's

# Stephan Schmidt said on September 27, 2007 4:31 AM:

Looks great. I currently use JXPath to find code violations in my Reposita Meaxure code analysis tool like this.

http://meaxure.reposita.org/

"classes/constructors[@params > 10]"

Quaere looks like a great alternative way to describe convention violations. I'll give it a try. Thanks.

Peace

-stephan

--

Stephan Schmidt :: stephan@reposita.org

Reposita Open Source - Monitor your software development

http://www.reposita.org

Blog at http://stephan.reposita.org - No signal. No noise.

# Norman Sasono said on October 16, 2007 8:49 PM:

Anders,

I guess Quaere cannot be compared to Microsoft's LINQ. Something very fundamental is missing.

Microsoft's LINQ is language enhancements, not just adding new API/library. One gotta touch the language design & compiler. Not just building another API/library.

Quaere still uses string literals as query criteria & projection. While LINQ in C# uses Lambda Expression, a higher order functional programming style. It ensures type checking during compile time.

So, the real LINQ for Java is something that must touch Java language design & compilers. Not just API/library.

Again, LINQ is language enhancements (language & compilers), not just API/library.

# Creating a fluent interface for Google Collections at Stephans Blog said on October 17, 2007 12:43 AM:

PingBack from http://stephan.reposita.org/archives/2007/10/17/creating-a-fluent-interface-for-google-collections/

# Pedro Furlanetto said on October 30, 2007 6:09 PM:

I think you are misusing the term DSL. DSL stands for a domain specific language. Your project don't add any new things to Java language, just new classes. It's a API not a DSL.

BTW, nice work. Do you know this project: http://jga.sourceforge.net/ ? Seens to fit very well with yours.

# andersnoras said on October 31, 2007 7:02 AM:

@Pedro;

IMHO this depends on your definition of a DSL. IMHO, Quaere is an internal / embedded DSL which uses Java's features to give the user the impression of using a domain specific language. Read this blog post for a more thorough discussion http://andersnoras.com/blogs/anoras/archive/2007/07/15/is-it-a-bird-is-it-a-plane-it-s-a-dsl.aspx

# Norman Sasono said on December 5, 2007 2:30 AM:

So, Anders, any update on this project?

How would you add compile time type checking instead of using string literals for criteria & projection?

As long as it still uses string literals, Quaere is not that close to Microsoft's LINQ.

# Anders Norås' Blog said on January 30, 2008 12:51 PM:

Today it is exactly one year since I pick up on blogging after a long break. To celebrate, I&#8217;ll

# pH » Carnaval? said on February 2, 2008 1:58 AM:

PingBack from http://www.astro.ufsc.br/~henrique/2008/02/02/carnaval/

# Blog del CIIN said on March 2, 2008 3:56 AM:

Como sabéis, una de las grandes novedades de Visual Studio 2008 y de .NET Framework 3.5 es el Language

# Extensibilidad de LINQ: LINQ Flavours! « Pasi??n por la tecnolog??a… said on March 2, 2008 4:02 AM:

PingBack from http://jcgonzalezmartin.wordpress.com/2008/03/02/extensibilidad-de-linq-linq-flavours/

# Oyun said on April 28, 2008 2:57 PM:

very thans good

# Data Access discussed at Xcalia » Blog Archive » LINQ for Java said on May 5, 2008 6:10 AM:

PingBack from http://blog.xcalia.com/erix/2008/05/05/linq-for-java/

# Lightweight SQL Interfaces for Java « The Sustainable Software Workshop said on June 11, 2008 4:52 AM:

PingBack from http://sustainablesoftware.wordpress.com/2008/06/11/lightweight-sql-interfaces-for-java/

# Cheapest cialis. said on July 25, 2008 12:00 PM:

Liquid cialis. Cialis.

Leave a Comment

(required) 
(optional)
(required) 
Enter the code you see below