SQL support for Querydsl - enables type-safe SQL query construction in Java
—
QueryDSL SQL provides database-specific SQL dialect support through template classes that handle SQL syntax differences, function mappings, data type handling, and database-specific optimizations for 17+ major databases.
Core template functionality providing the foundation for database-specific implementations.
/**
* Base class for all database-specific SQL templates
*/
public abstract class SQLTemplates {
/**
* Gets the SQL type name for a JDBC type code
* @param code JDBC type code
* @return SQL type name for this database
*/
public String getTypeNameForCode(int code);
/**
* Checks if this database supports SQL arrays
* @return true if arrays are supported
*/
public boolean isArraysSupported();
/**
* Checks if LIMIT/OFFSET clauses are supported
* @return true if LIMIT/OFFSET is supported
*/
public boolean isLimitOffsetSupported();
/**
* Checks if batch operations are supported
* @return true if batch operations are supported
*/
public boolean isBatchSupported();
/**
* Gets the quote character for identifiers
* @return Quote character (usually " or `)
*/
public char getQuoteChar();
/**
* Checks if identifiers should be quoted
* @return true if identifiers need quoting
*/
public boolean isQuoteIdentifiers();
}PostgreSQL-specific template with support for advanced features like arrays, JSON, and custom operators.
/**
* PostgreSQL database templates with advanced feature support
*/
public class PostgreSQLTemplates extends SQLTemplates {
/**
* Creates PostgreSQL templates with default configuration
*/
public PostgreSQLTemplates();
/**
* Creates PostgreSQL templates with custom quote character
* @param quote Character to use for quoting identifiers
*/
public PostgreSQLTemplates(char quote);
/**
* Creates a builder for configuring PostgreSQL templates
* @return Template builder
*/
public static Builder builder();
/**
* Builder for PostgreSQL template configuration
*/
public static class Builder {
public Builder quote(char quote);
public Builder escape(char escape);
public Builder printSchema();
public Builder useSchema(String schema);
public PostgreSQLTemplates build();
}
}Usage Examples:
// Default PostgreSQL templates
SQLTemplates templates = PostgreSQLTemplates.builder().build();
// PostgreSQL with custom configuration
SQLTemplates templates = PostgreSQLTemplates.builder()
.quote('"')
.printSchema()
.useSchema("public")
.build();
// Configuration with templates
Configuration config = new Configuration(templates);
SQLQueryFactory queryFactory = new SQLQueryFactory(config, dataSource);MySQL-specific templates with support for MySQL syntax variations and storage engines.
/**
* MySQL database templates with MySQL-specific optimizations
*/
public class MySQLTemplates extends SQLTemplates {
/**
* Creates MySQL templates with default configuration
*/
public MySQLTemplates();
/**
* Creates MySQL templates with custom quote character
* @param quote Character to use for quoting identifiers
*/
public MySQLTemplates(char quote);
/**
* Creates a builder for configuring MySQL templates
* @return Template builder
*/
public static Builder builder();
}Oracle-specific templates with support for Oracle SQL syntax and PL/SQL features.
/**
* Oracle database templates with Oracle-specific features
*/
public class OracleTemplates extends SQLTemplates {
/**
* Creates Oracle templates with default configuration
*/
public OracleTemplates();
/**
* Creates a builder for configuring Oracle templates
* @return Template builder
*/
public static Builder builder();
}SQL Server templates with support for multiple SQL Server versions and T-SQL features.
/**
* Base SQL Server templates
*/
public class SQLServerTemplates extends SQLTemplates;
/**
* SQL Server 2005 specific templates
*/
public class SQLServer2005Templates extends SQLServerTemplates;
/**
* SQL Server 2008 specific templates with enhanced features
*/
public class SQLServer2008Templates extends SQLServer2005Templates;
/**
* SQL Server 2012 templates with additional T-SQL features
*/
public class SQLServer2012Templates extends SQLServer2008Templates;Templates for additional database systems with their specific SQL dialects.
/**
* H2 database templates
*/
public class H2Templates extends SQLTemplates;
/**
* HSQLDB database templates
*/
public class HSQLDBTemplates extends SQLTemplates;
/**
* Apache Derby templates
*/
public class DerbyTemplates extends SQLTemplates;
/**
* IBM DB2 templates
*/
public class DB2Templates extends SQLTemplates;
/**
* SQLite templates
*/
public class SQLiteTemplates extends SQLTemplates;
/**
* Firebird database templates
*/
public class FirebirdTemplates extends SQLTemplates;
/**
* CUBRID database templates
*/
public class CUBRIDTemplates extends SQLTemplates;
/**
* Teradata database templates
*/
public class TeradataTemplates extends SQLTemplates;Specialized query factories that provide database-specific optimizations and features.
/**
* PostgreSQL-optimized query factory
*/
public class PostgreSQLQueryFactory extends AbstractSQLQueryFactory<PostgreSQLQuery<?>> {
public PostgreSQLQueryFactory(Configuration configuration, DataSource dataSource);
public PostgreSQLQuery<?> query();
}
/**
* MySQL-optimized query factory
*/
public class MySQLQueryFactory extends AbstractSQLQueryFactory<MySQLQuery<?>> {
public MySQLQueryFactory(Configuration configuration, DataSource dataSource);
public MySQLQuery<?> query();
public MySQLReplaceClause replace(RelationalPath<?> path);
}
/**
* Oracle-optimized query factory
*/
public class OracleQueryFactory extends AbstractSQLQueryFactory<OracleQuery<?>> {
public OracleQueryFactory(Configuration configuration, DataSource dataSource);
public OracleQuery<?> query();
}
/**
* SQL Server-optimized query factory
*/
public class SQLServerQueryFactory extends AbstractSQLQueryFactory<SQLServerQuery<?>> {
public SQLServerQueryFactory(Configuration configuration, DataSource dataSource);
public SQLServerQuery<?> query();
}Specialized query implementations with database-specific features and optimizations.
/**
* PostgreSQL query with PostgreSQL-specific features
*/
public class PostgreSQLQuery<T> extends AbstractPostgreSQLQuery<T, PostgreSQLQuery<T>> {
public PostgreSQLQuery(Connection conn, PostgreSQLTemplates templates);
public PostgreSQLQuery<T> forShare();
public PostgreSQLQuery<T> forUpdate();
}
/**
* MySQL query with MySQL-specific features
*/
public class MySQLQuery<T> extends AbstractMySQLQuery<T, MySQLQuery<T>> {
public MySQLQuery(Connection conn, MySQLTemplates templates);
public MySQLQuery<T> straightJoin();
public MySQLQuery<T> forUpdate();
}
/**
* Oracle query with Oracle-specific features
*/
public class OracleQuery<T> extends AbstractOracleQuery<T, OracleQuery<T>> {
public OracleQuery(Connection conn, OracleTemplates templates);
public OracleQuery<T> hint(String hint);
public OracleQuery<T> connectBy(Predicate cond);
}
/**
* SQL Server query with T-SQL features
*/
public class SQLServerQuery<T> extends AbstractSQLServerQuery<T, SQLServerQuery<T>> {
public SQLServerQuery(Connection conn, SQLServerTemplates templates);
public SQLServerQuery<T> tableHints(SQLServerTableHints... hints);
public SQLServerQuery<T> with(String... options);
}Central registry for managing and discovering database-specific templates.
/**
* Registry for database-specific SQL templates
*/
public class SQLTemplatesRegistry {
/**
* Gets templates for a specific database based on connection metadata
* @param connection Database connection
* @return Appropriate SQL templates for the database
*/
public static SQLTemplates getTemplates(Connection connection);
/**
* Gets templates by database product name
* @param productName Database product name
* @return Appropriate SQL templates
*/
public static SQLTemplates getTemplates(String productName);
/**
* Registers custom templates for a database product
* @param productName Database product name
* @param templates SQL templates to register
*/
public static void register(String productName, SQLTemplates templates);
}Usage Examples:
// Automatic template detection
Connection connection = dataSource.getConnection();
SQLTemplates templates = SQLTemplatesRegistry.getTemplates(connection);
Configuration config = new Configuration(templates);
// Manual template selection by product name
SQLTemplates templates = SQLTemplatesRegistry.getTemplates("PostgreSQL");
// Register custom templates
class CustomPostgreSQLTemplates extends PostgreSQLTemplates {
// Custom implementation
}
SQLTemplatesRegistry.register("PostgreSQL", new CustomPostgreSQLTemplates());Specialized features available for specific databases through their template implementations.
/**
* MySQL-specific REPLACE operation
*/
public class MySQLReplaceClause extends AbstractSQLInsertClause<MySQLReplaceClause> {
public MySQLReplaceClause(Connection conn, MySQLTemplates templates, RelationalPath<?> entity);
public <T> MySQLReplaceClause set(Path<T> path, T value);
public long execute();
}
/**
* SQL Server table hints enumeration
*/
public enum SQLServerTableHints {
NOLOCK, READUNCOMMITTED, UPDLOCK, REPEATABLEREAD,
SERIALIZABLE, READCOMMITTED, TABLOCK, TABLOCKX,
PAGLOCK, ROWLOCK, NOWAIT, READPAST, XLOCK, SNAPSHOT
}
/**
* Teradata-specific query band support
*/
public class SetQueryBandClause {
public SetQueryBandClause for(String target);
public SetQueryBandClause set(String key, String value);
public void execute();
}Usage Examples:
// MySQL REPLACE operation
MySQLQueryFactory mysqlFactory = new MySQLQueryFactory(config, dataSource);
mysqlFactory
.replace(qUser)
.set(qUser.name, "John Doe")
.set(qUser.email, "john@example.com")
.execute();
// SQL Server with table hints
SQLServerQueryFactory sqlServerFactory = new SQLServerQueryFactory(config, dataSource);
List<User> users = sqlServerFactory
.selectFrom(qUser)
.tableHints(SQLServerTableHints.NOLOCK)
.fetch();
// Oracle with hints
OracleQueryFactory oracleFactory = new OracleQueryFactory(config, dataSource);
List<User> users = oracleFactory
.selectFrom(qUser)
.hint("/*+ INDEX(user, user_email_idx) */")
.fetch();Configuration options for customizing template behavior and SQL generation.
/**
* Template builder pattern for configuration
*/
public static class TemplateBuilder {
/**
* Sets the quote character for identifiers
* @param quote Quote character
* @return Builder for method chaining
*/
public TemplateBuilder quote(char quote);
/**
* Sets the escape character
* @param escape Escape character
* @return Builder for method chaining
*/
public TemplateBuilder escape(char escape);
/**
* Enables schema printing in SQL
* @return Builder for method chaining
*/
public TemplateBuilder printSchema();
/**
* Sets default schema for operations
* @param schema Schema name
* @return Builder for method chaining
*/
public TemplateBuilder useSchema(String schema);
/**
* Builds the configured templates
* @return Configured SQL templates
*/
public SQLTemplates build();
}Usage Examples:
// Custom PostgreSQL configuration
PostgreSQLTemplates templates = PostgreSQLTemplates.builder()
.quote('`') // Use backticks instead of quotes
.printSchema() // Include schema in SQL
.useSchema("app_schema") // Default schema
.build();
// H2 configuration for testing
H2Templates h2Templates = H2Templates.builder()
.quote('"')
.build();
// SQL Server with custom settings
SQLServerTemplates sqlServerTemplates = SQLServerTemplates.builder()
.quote('[') // Use square brackets
.escape(']')
.build();Install with Tessl CLI
npx tessl i tessl/maven-com-querydsl--querydsl-sql