|
Prototype: |
LINQExpand4Java |
|
Last updated: |
2009-05-22 |
|
Authors: |
Miguel Garcia, http://www.sts.tu-harburg.de/people/mi.garcia/
Kaichuan Wen, http://www.wen-k.com
|
| Introduction: |
It is an open problem in software development how best to integrate OO programming languages and database query languages. A seamless solution requires (a) mapping between an OO type system and a DB schema language; and (b) compile-time well-formedness checking of queries, queries which appear nested in OO programs.
A recent contribution by Microsoft addressing (a) and (b) is the LINQ set of technologies, to enable data access (from .Net languages) over a variety of data sources in a typesafe manner.
As of now, there is no similar mechanism for Java, given that JDBC or O/R mapping queries are handled by a Java compiler as uninterpreted strings (which thus may result in runtime errors when shipped to the DBMS).
Our open-source LINQExpand4Java prototype automates some of the tasks for (b) above, i.e. it can expand LINQ queries (provided by the developer in a Java source file) into the Abstract Syntax Tree needed for shipping to a (LINQ-aware) DBMS. The underlying approach is in general applicable to other data query mechanisms as well (XQuery, JPQL, to name a few).
|
|
The user perspective: |
From a user perspective, given an input LINQ query in a Java source file as depicted in Figure 1,
running LINQExpand4Java as part of a build (performed by an ANT task, or by an IDE) results in the source code shown in Figure 2.
The first statement in the generated output (LINQ.expanded("original query")) allows changing the input query, and have it re-generated.
In case the input query cannot be parsed or is ill-formed, the normal error reporting mechanism of the Java compiler is used by LINQExpand4Java
to provide feedback to that effect (not shown).
 |
Figure 1: Before query expansion (intput framed in rectangle)
 |
Figure 2: After query expansion (intput framed in rectangle)
The user perspective alone does not provide insight about the transformation from LINQ textual syntax into SQO operators that any LINQ-processing tool
needs to rely on, and that our code provides in a reusable manner.
|
|
Architecture of the Prototype: |
The prototype comprises two Java projects (the compiler plugin itself and a project with tests only), which can be imported into the Eclipse workspace (Import > Archive File).
One block performs the different phases of transformations and expansion from textual LINQ query to bunches of Java statements. The other part is the compiler plug-in that performs the AST rewriting of the Java source file.
The transformers and expanders involve classes residing in the following packages: grammar,
linqtextual, transform, transparentId, sqo and functionalSyntax.
Classes in package linqtextual constitute a metamodel representing each grammar productions of LINQ queries in textual syntax.
A Concrete Syntax Tree (CST) based on this metamodel is built by the parser (in package grammar) generated with ANTLR.
The CST is then transformed into sequence of invocation of Standard Query Operators (SQO) and further translated into a purely functional form.
The compiler plug-in makes use of the mechanism of annotation processing based on JSR 269.
An annotation type @plugin.Linq is defined in package plugin.
The expander then visits all the methods decorated with this annotation, and replaces the statement
LINQ.expand("query comes here")
with statements that generate corresponding instances in functional syntax form that are ready for shipping to a DBMS for evaluation.
|
|
Installation and How to use: |
Right now the prototype performs the first part of compile-time processing,
namely expanding a LINQ query into the Java statements needed to build its Abstract Syntax Tree
(additional desirable functionality is described below under Future Work).
In order to use LINQExpand4Java, please follow these instructions:
- Download and import the .jar file into the Eclipse Java project that contains LINQ queries.
- In one of your Java files, add a method and decorate it with this annotation:
@plugin.Linq("query")
where the string "query" is the query you would like to expand.
- This method should have
void as its return type, and the following as its last statement:
plugin.marker.LINQ.expand("query");
Because this statement will be then replaced with a bunch of statements ending with a return statement,
any other statements after LINQ.expand("query") would become unreachable,
which leads to a compilation error. The previous return type void will be replaced with the type of the return value by
LINQExpand4Java.
- Run
javac on your source files, specifying plugin.LinqExpanderProcessor as an annotation processor.
If you launch the Java compiler in command line, the command will look like:
javac -processor plugin.LinqExpanderProcessor -printsource -d gen Example.java
The option -printsource is optional yet helpful, in order to see the rewritten Java source file.
Special care has to be taken to redirect the output that would otherwise overwrite the original one.
Compiling with Eclipse or another IDE is similar, in which case one should include these options in the compiler argument.
|
| Future Work: |
As of now, an expanded query is not shipped to any DBMS for evaluation.
We are also working on a type-checker (based on JSR-308) to determine for example that the arguments given to a query conform to expected types.
This kind of semantic analysis cannot be easily performed with compiler-plugins based on JSR-269
(such as LINQExpand4Java), which operate on parse-tree view (as opposed to AST-view of the input source code).
This functionality is still work-in-progress, yet these aspects are complementary (not a replacement) to the functionality already available.
|
| Useful links: |
|
| License: |
/*******************************************************************************
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Miguel Garcia, http://www.sts.tu-harburg.de/people/mi.garcia
* Kaichuan Wen, http://www.wen-k.com/
*******************************************************************************/
|
| Download (binary): |
LinqExpand4Java.jar
|
| Download (sources): |
LinqExpand4Java20090522.zip
ToStatementTest20090522.zip
|
| Publication: |
Garcia M.
Compiler plugins can handle nested languages: AST-level expansion of LINQ queries for Java
Abstract
Paper
BibTeX
Slides
Poster
Proceedings of ICOODB 2009, 2nd International Conference on Object Databases, Zurich, Switzerland, July 2009.
|
|
|