CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-querydsl--querydsl-sql

SQL support for Querydsl - enables type-safe SQL query construction in Java

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

QueryDSL SQL

QueryDSL 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.

Package Information

  • Package Name: com.querydsl:querydsl-sql
  • Package Type: maven
  • Language: Java
  • Installation:
    <dependency>
      <groupId>com.querydsl</groupId>
      <artifactId>querydsl-sql</artifactId>
      <version>5.1.0</version>
    </dependency>

Core Imports

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;

Basic Usage

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();

Architecture

QueryDSL SQL is built around several key components:

  • Query Factory: Central entry point (SQLQueryFactory) for creating queries and DML operations
  • Query Builders: Fluent interfaces (SQLQuery, AbstractSQLQuery) for constructing type-safe queries
  • SQL Templates: Database-specific dialect handling for 17+ databases with optimized SQL generation
  • Type System: Comprehensive type mapping between Java and SQL types with custom type support
  • DML Operations: Type-safe insert, update, delete, and merge operations with batch support
  • Configuration System: Flexible configuration for listeners, type mappings, and schema customization

Capabilities

Query Construction

Core 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);

Query Construction

Database Templates

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;

Database Templates

DML Operations

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);

DML Operations

Type System

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();
}

Type System

Configuration and Customization

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);
}

Configuration

SQL Expressions and Functions

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();
}

SQL Expressions

Types

Core Query Interfaces

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);
}

Path and Metadata

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();
}

Query Result Handling

public class SQLBindings {
    public String getSQL();
    public List<Object> getBindings();
}

public interface Union<T> {
    List<T> fetch();
    T fetchOne();
    Union<T> orderBy(OrderSpecifier<?>... o);
}
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
mavenpkg:maven/com.querydsl/querydsl-sql@5.1.x
Publish Source
CLI
Badge
tessl/maven-com-querydsl--querydsl-sql badge