SQL support for Querydsl - enables type-safe SQL query construction in Java
npx @tessl/cli install tessl/maven-com-querydsl--querydsl-sql@5.1.0QueryDSL SQL provides a fluent, type-safe API for building and executing SQL queries in Java. It eliminates SQL string concatenation by offering a programmatic query construction interface that supports complex queries, joins, subqueries, and database-specific optimizations while maintaining compile-time type safety.
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-sql</artifactId>
<version>5.1.0</version>
</dependency>import com.querydsl.sql.SQLQueryFactory;
import com.querydsl.sql.SQLQuery;
import com.querydsl.sql.Configuration;
import com.querydsl.sql.SQLTemplates;
import com.querydsl.sql.PostgreSQLTemplates;
import com.querydsl.core.Tuple;import com.querydsl.sql.*;
import com.querydsl.sql.postgresql.PostgreSQLTemplates;
import javax.sql.DataSource;
// Setup configuration with database-specific templates
SQLTemplates templates = PostgreSQLTemplates.builder().build();
Configuration configuration = new Configuration(templates);
// Create query factory
SQLQueryFactory queryFactory = new SQLQueryFactory(configuration, dataSource);
// Build and execute queries
List<String> names = queryFactory
.select(qUser.name)
.from(qUser)
.where(qUser.active.isTrue())
.fetch();
// DML operations
long count = queryFactory
.update(qUser)
.set(qUser.lastLogin, LocalDateTime.now())
.where(qUser.id.eq(userId))
.execute();QueryDSL SQL is built around several key components:
SQLQueryFactory) for creating queries and DML operationsSQLQuery, AbstractSQLQuery) for constructing type-safe queriesCore query building functionality for SELECT statements with type-safe joins, conditions, grouping, and ordering. Supports complex queries including subqueries, CTEs, and window functions.
public SQLQuery<?> query();
public <T> SQLQuery<T> select(Expression<T> expr);
public SQLQuery<Tuple> select(Expression<?>... exprs);
public <T> SQLQuery<T> selectFrom(RelationalPath<T> expr);Database-specific SQL dialect support with optimized templates for PostgreSQL, MySQL, Oracle, SQL Server, and 13+ other databases. Templates handle SQL syntax differences, function mappings, and database-specific features.
public abstract class SQLTemplates {
public String getTypeNameForCode(int code);
public boolean isArraysSupported();
public boolean isLimitOffsetSupported();
}
public class PostgreSQLTemplates extends SQLTemplates;
public class MySQLTemplates extends SQLTemplates;
public class OracleTemplates extends SQLTemplates;Type-safe Data Manipulation Language operations including INSERT, UPDATE, DELETE, and MERGE with batch processing support and automatic key generation.
public SQLInsertClause insert(RelationalPath<?> path);
public SQLUpdateClause update(RelationalPath<?> path);
public SQLDeleteClause delete(RelationalPath<?> path);
public SQLMergeClause merge(RelationalPath<?> path);Comprehensive type mapping system between Java and SQL types with support for custom types, enums, arrays, and database-specific types like UUID, JSON, and XML.
public interface Type<T> {
T getValue(ResultSet rs, int startIndex) throws SQLException;
void setValue(PreparedStatement st, int startIndex, T value) throws SQLException;
int[] getSQLTypes();
Class<T> getReturnedClass();
}Flexible configuration system for customizing SQL generation, type mappings, naming strategies, schema mappings, and execution listeners.
public class Configuration {
public Configuration(SQLTemplates templates);
public void register(String table, String property, Path<?> path);
public void registerType(String typeName, Type<?> type);
public void addListener(SQLListener listener);
}Comprehensive expression builder for SQL functions, operators, and database-specific features including window functions, aggregate functions, and conditional expressions.
public class SQLExpressions {
public static <T> SimpleExpression<T> constant(T value);
public static StringExpression concat(Expression<String>... args);
public static DateTimeExpression<Date> currentTimestamp();
public static WindowFunction<Long> rowNumber();
}public interface SQLCommonQuery<Q> {
List<Tuple> fetch();
<T> List<T> fetch();
Tuple fetchOne();
<T> T fetchOne();
long fetchCount();
}
public interface SQLCommonQueryFactory<Q, D, U, I, M> {
Q query();
D delete(RelationalPath<?> path);
U update(RelationalPath<?> path);
I insert(RelationalPath<?> path);
M merge(RelationalPath<?> path);
}public interface RelationalPath<T> extends Path<T> {
SchemaAndTable getSchemaAndTable();
Collection<? extends Path<?>> getColumns();
PrimaryKey<T> getPrimaryKey();
}
public class SchemaAndTable {
public SchemaAndTable(String schema, String table);
public String getSchema();
public String getTable();
}public class SQLBindings {
public String getSQL();
public List<Object> getBindings();
}
public interface Union<T> {
List<T> fetch();
T fetchOne();
Union<T> orderBy(OrderSpecifier<?>... o);
}