Querying SQL/JDBC sources
This chapter describes the query type generation and querying functionality of the SQL module.
Creating the Querydsl query types
To get started export your schema into Querydsl query types like this :
This declares that the database schema is to be mirrored into the com.myproject.domain package in the src/main/java folder.
The generated types have the table name transformed to mixed case as the class name and a similar mixed case transformation
applied to the columns which are available as property paths in the query type.
In addition to this primary key and foreign key constraints are provided as fields which can be used for compact join declarations ... TODO
Maven integration
This functionality is also available as a Maven plugin. The presented example can be declared like this in the POM :
com.mysema.querydsl
querydsl-maven-plugin
${querydsl.version}
export
org.apache.derby.jdbc.EmbeddedDriver
jdbc:derby:target/demoDB;create=true
com.myproject.domain
${project.basedir}/target/generated-sources/java
org.apache.derby
derby
${derby.version}
]]>
Use the goal test-export to add the targetFolder as a test compile source root
instead of a compile source root.
Querying
Querying with Querydsl SQL is as simple as this :
lastNames = query.from(customer)
.where(customer.firstName.eq("Bob"))
.list(customer.lastName);
]]>
which is transformed into the following sql query, assuming that the related table name is
customer and the columns first_name and
last_name :
Internally Querydsl SQL uses PreparedStatements, though.
Querydsl uses SQL dialects to customize the SQL serialization needed for different relational databases. The
available dialects are :
com.mysema.query.sql.DerbyTemplates
tested with version 10.5.3
com.mysema.query.sql.HSQLDBTemplates
tested with version 1.8.0.7
com.mysema.query.sql.H2Templates
tested with H2 1.2.133
com.mysema.query.sql.MySQLTemplates
tested with MySQL CE 5.1
com.mysema.query.sql.OracleTemplates
tested with Oracle 10g XE
com.mysema.query.sql.PostgresTemplates
tested with Postgres 8.4
com.mysema.query.sql.SQLServerTemplates
tested with SQL Server 2008
General usage
Use the the cascading methods of the SQLQuery interface like this
from : Define the query sources here.
innerJoin, join, leftJoin, fullJoin, on : Define join elements using these constructs.
For the join methods the first argument is the join source and the second the target (alias).
where : Define the query filters, either in varargs form separated via commas or
cascaded via the and-operator.
groupBy : Define the group by arguments in varargs form.
having : Define the having filter of the "group by" grouping as an varags array of
EBoolean expressions.
orderBy : Define the ordering of the result as an varargs array of order expressions.
Use asc() and desc() on numeric, string and other comparable expression to access the OrderSpecifier instances.
limit, offset, restrict : Define the paging of the result. Limit for max results,
offset for skipping rows and restrict for defining both in one call.
Ordering
The syntax for declaring ordering is
which is equivalent to the following native SQL
SELECT c.first_name, c.last_name
FROM customer c
ORDER BY c.last_name ASC, c.first_name ASC
Grouping
Grouping can be done in the following form
which is equivalent to the following native SQL
SELECT c.last_name
FROM customer c
GROUP BY c.last_name
Union queries
TODO
Query extension support
Custom query extensions to support engine specific syntax can be created by subclassing AbstractSQLQuery and adding flagging methods like
in the given MySQLQuery example :
{
public MySQLQuery(Connection conn) {
this(conn, new MySQLTemplates(), new DefaultQueryMetadata());
}
public MySQLQuery(Connection conn, SQLTemplates templates) {
this(conn, templates, new DefaultQueryMetadata());
}
protected MySQLQuery(Connection conn, SQLTemplates templates, QueryMetadata metadata) {
super(conn, new Configuration(templates), metadata);
}
public MySQLQuery bigResult(){
return addFlag(Position.AFTER_SELECT, "SQL_BIG_RESULT ");
}
public MySQLQuery bufferResult(){
return addFlag(Position.AFTER_SELECT, "SQL_BUFFER_RESULT ");
}
// ...
}
]]>
The flags are custom SQL snippets that can be inserted at specific points in the serialization.
The supported positions are the enums of the com.mysema.query.QueryFlag.Position enum class.
Using DDL commands
CREATE TABLE commands can be used in fluent form via the CreateTableClause. Here are some examples :
The constructor of CreateTableClause takes the connection, the templates and the table name. The rest is declared via
column, primaryKey and foreignKey invocations.
Here are the corresponding CREATE TABLE clauses as they are executed.
Using Data manipulation commands
All the DMLClause implementation in the Querydsl SQL module take three parameters, the Connection, the SQLTemplates instance
used in the queries and the main entity the DMLClause is bound to.
Insert examples :
Update examples :
Delete examples :
Batch support in DML clauses
Querydsl SQL supports usage of JDBC batch updates through the DML APIs. If you have consecutive DML calls with a similar structure,
you can bundle the the calls via addBatch() usage into one DMLClause. See the examples how it works for UPDATE, DELETE and INSERT.
Bean class generation
TODO
User types
TODO