Class SqlQueryBuilder

java.lang.Object
io.micronaut.data.model.query.builder.sql.AbstractSqlLikeQueryBuilder
io.micronaut.data.model.query.builder.sql.SqlQueryBuilder
All Implemented Interfaces:
QueryBuilder
Direct Known Subclasses:
CosmosSqlQueryBuilder

@Internal public class SqlQueryBuilder extends AbstractSqlLikeQueryBuilder
Implementation of AbstractSqlLikeQueryBuilder.QueryBuilder that builds SQL queries.
Since:
1.0.0
Author:
graemerocher, Denis Stepanov
  • Field Details

  • Constructor Details

    • SqlQueryBuilder

      @Creator public SqlQueryBuilder(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata)
      Constructor with annotation metadata.
      Parameters:
      annotationMetadata - The annotation metadata
    • SqlQueryBuilder

      public SqlQueryBuilder()
      Default constructor.
    • SqlQueryBuilder

      public SqlQueryBuilder(Dialect dialect)
      Parameters:
      dialect - The dialect
    • SqlQueryBuilder

      public SqlQueryBuilder(Dialect dialect, @Nullable String dialectVersion)
      Parameters:
      dialect - The dialect
      dialectVersion - The target dialect version
      Since:
      5.2
  • Method Details

    • getDialect

      public Dialect getDialect()
      Description copied from class: AbstractSqlLikeQueryBuilder
      Get dialect.
      Overrides:
      getDialect in class AbstractSqlLikeQueryBuilder
      Returns:
      The dialect being used by the builder.
    • supportsNullOrdering

      protected boolean supportsNullOrdering()
      Description copied from class: AbstractSqlLikeQueryBuilder
      Whether the dialect understands the standard NULLS FIRST and NULLS LAST suffixes on an ORDER BY item. Dialects that do not get an equivalent CASE expression sorted on first.
      Overrides:
      supportsNullOrdering in class AbstractSqlLikeQueryBuilder
      Returns:
      Whether the suffixes are supported
    • isDialectVersionAtLeast

      public boolean isDialectVersionAtLeast(String requiredVersion)
      Overrides:
      isDialectVersionAtLeast in class AbstractSqlLikeQueryBuilder
      Parameters:
      requiredVersion - The required target dialect version
      Returns:
      Whether the target dialect version meets the requirement
      Since:
      5.2
    • getDialectVersion

      public @Nullable String getDialectVersion()
      Overrides:
      getDialectVersion in class AbstractSqlLikeQueryBuilder
      Returns:
      The normalized target dialect version, or null when none is configured.
      Since:
      5.2
    • getDialectOptions

      protected SqlDialectOptions getDialectOptions()
      Overrides:
      getDialectOptions in class AbstractSqlLikeQueryBuilder
      Returns:
      the resolved dialect options for this builder
    • shouldEscape

      protected boolean shouldEscape(PersistentEntity entity)
      Description copied from class: AbstractSqlLikeQueryBuilder
      Whether queries should be escaped for the given entity.
      Overrides:
      shouldEscape in class AbstractSqlLikeQueryBuilder
      Parameters:
      entity - The entity
      Returns:
      True if they should be escaped
    • normalizeAlias

      protected String normalizeAlias(String alias)
      Description copied from class: AbstractSqlLikeQueryBuilder
      Normalize a generated alias for dialect-specific identifier requirements.
      Overrides:
      normalizeAlias in class AbstractSqlLikeQueryBuilder
      Parameters:
      alias - The generated alias
      Returns:
      The normalized alias
    • asLiteral

      protected String asLiteral(@Nullable Object value)
      Description copied from class: AbstractSqlLikeQueryBuilder
      Convert the literal value to it's SQL representation.
      Overrides:
      asLiteral in class AbstractSqlLikeQueryBuilder
      Parameters:
      value - The literal value
      Returns:
      converter value
    • buildBatchCreateTableStatement

      public String buildBatchCreateTableStatement(PersistentEntity... entities)
      Builds a batch create tables statement. Designed for testing and not production usage. For production a SQL migration tool such as Flyway or Liquibase is recommended.
      Parameters:
      entities - the entities
      Returns:
      The table
    • buildBatchCreateTableStatement

      public String buildBatchCreateTableStatement(List<DefinitionProvider> columnDefinitionProviders, PersistentEntity... entities)
      Builds a batch create tables statement. Designed for testing and not production usage. For production a SQL migration tool such as Flyway or Liquibase is recommended.
      Parameters:
      columnDefinitionProviders - the list of SqlColumnDefinitionProvider
      entities - the entities
      Returns:
      The table
    • buildBatchDropTableStatement

      public String buildBatchDropTableStatement(PersistentEntity... entities)
      Builds a batch drop tables statement. Designed for testing and not production usage. For production a SQL migration tool such as Flyway or Liquibase is recommended.
      Parameters:
      entities - the entities
      Returns:
      The table
    • buildDropTableStatements

      public String[] buildDropTableStatements(PersistentEntity entity)
      Builds the drop table statement. Designed for testing and not production usage. For production a SQL migration tool such as Flyway or Liquibase is recommended.
      Parameters:
      entity - The entity
      Returns:
      The tables for the give entity
    • buildJoinTableInsert

      public String buildJoinTableInsert(PersistentEntity entity, Association association)
      Builds a join table insert statement for a given entity and association.
      Parameters:
      entity - The entity
      association - The association
      Returns:
      The join table insert statement
    • isForeignKeyWithJoinTable

      public static boolean isForeignKeyWithJoinTable(Association association)
      Is the given association a foreign key reference that requires a join table.
      Parameters:
      association - The association.
      Returns:
      True if it is.
    • buildCreateTableStatements

      public String[] buildCreateTableStatements(PersistentEntity entity)
      Builds a set of CREATE TABLE statements for the given entity.

      This method is public and the class is non-final; therefore it can be overridden. If you override it, ensure you preserve the following behavior expected by callers:

      • Return a non-null String[] containing SQL statements in execution order.
      • Respect the current getDialect() and escaping rules (see shouldEscape(PersistentEntity)).
      • Keep special handling for JsonView entities (Oracle-only) consistent with the base implementation.
      Parameters:
      entity - The entity
      Returns:
      The CREATE TABLE statements
    • buildCreateTableStatements

      public String[] buildCreateTableStatements(PersistentEntity entity, List<DefinitionProvider> definitionProviders)
      Builds a set of CREATE TABLE statements for the given entity.
      Parameters:
      entity - The entity
      definitionProviders - The definition providers
      Returns:
      The CREATE TABLE statements
    • buildCreateTableStatements

      public final String[] buildCreateTableStatements(PersistentEntity... entities)
      Builds the create table statements for a collection of entities. Designed for testing and not production usage. For production a SQL migration tool such as Flyway or Liquibase is recommended.
      Parameters:
      entities - The collection of entities
      Returns:
      The tables for the given entities
    • buildCreateTableStatements

      public final String[] buildCreateTableStatements(PersistentEntity[] entities, Dialect dialect)
    • buildCreateTableStatements

      public final String[] buildCreateTableStatements(List<DefinitionProvider> definitionProviders, PersistentEntity[] entities, Dialect dialect)
      Builds the create table statements for a collection of entities.
      Parameters:
      definitionProviders - The definition providers
      entities - The collection of entities
      dialect - The dialect
      Returns:
      The tables for the given entities
    • getTableAsKeyword

      protected String getTableAsKeyword()
      Description copied from class: AbstractSqlLikeQueryBuilder
      Get the AS keyword to use for table aliases.
      Overrides:
      getTableAsKeyword in class AbstractSqlLikeQueryBuilder
      Returns:
      The AS keyword if any
    • createSelectionVisitor

      protected SqlQueryBuilder.SqlSelectionVisitor createSelectionVisitor(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, AbstractSqlLikeQueryBuilder.QueryState queryState, boolean distinct)
      Description copied from class: AbstractSqlLikeQueryBuilder
      Create a selection visitor.
      Overrides:
      createSelectionVisitor in class AbstractSqlLikeQueryBuilder
      Parameters:
      annotationMetadata - The annotation metadata
      queryState - The query state
      distinct - The distinct
      Returns:
      The visitor
    • createReturningSelectionVisitor

      protected AbstractSqlLikeQueryBuilder.ReturningSelectionVisitor createReturningSelectionVisitor(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, AbstractSqlLikeQueryBuilder.QueryState queryState, boolean distinct)
      Description copied from class: AbstractSqlLikeQueryBuilder
      Creates a visitor for handling the RETURNING clause in an UPDATE/DELETE statement. This method is used to generate the necessary SQL for the RETURNING clause when executing an UPDATE or DELETE query with a RETURNING clause.
      Overrides:
      createReturningSelectionVisitor in class AbstractSqlLikeQueryBuilder
      Parameters:
      annotationMetadata - The annotation metadata associated with the query.
      queryState - The current state of the query being built.
      distinct - Whether the query is marked as DISTINCT.
      Returns:
      A visitor that can handle the RETURNING clause.
    • resolveJoinType

      public String resolveJoinType(Join.Type jt)
      Description copied from class: AbstractSqlLikeQueryBuilder
      Resolves the join type.
      Specified by:
      resolveJoinType in class AbstractSqlLikeQueryBuilder
      Parameters:
      jt - The join type
      Returns:
      The join type.
    • buildInsert

      public QueryResult buildInsert(io.micronaut.core.annotation.AnnotationMetadata repositoryMetadata, QueryBuilder.InsertQueryDefinition definition)
      Description copied from interface: QueryBuilder
      Builds an insert statement for the given entity.
      Parameters:
      repositoryMetadata - The repository annotation metadata
      definition - The definition
      Returns:
      The insert statement or null if the implementation doesn't require insert statements
    • buildUpsert

      public QueryResult buildUpsert(io.micronaut.core.annotation.AnnotationMetadata repositoryMetadata, QueryBuilder.UpsertQueryDefinition definition)
      Description copied from interface: QueryBuilder
      Builds an upsert statement for the given entity.
      Parameters:
      repositoryMetadata - The repository annotation metadata
      definition - The definition
      Returns:
      The upsert statement
    • getRawAliasName

      protected String getRawAliasName(PersistentEntity entity)
      Description copied from class: AbstractSqlLikeQueryBuilder
      Get the unnormalized alias name for the given entity.
      Overrides:
      getRawAliasName in class AbstractSqlLikeQueryBuilder
      Parameters:
      entity - The entity
      Returns:
      The raw alias name
    • getTableName

      public String getTableName(PersistentEntity entity)
      Description copied from class: AbstractSqlLikeQueryBuilder
      Get the table name for the given entity.
      Specified by:
      getTableName in class AbstractSqlLikeQueryBuilder
      Parameters:
      entity - The entity
      Returns:
      The table name
    • appendUpdateSetParameter

      protected void appendUpdateSetParameter(StringBuilder sb, @Nullable String alias, PersistentProperty prop, Runnable appendParameter)
      Description copied from class: AbstractSqlLikeQueryBuilder
      Appends the SET=? call to the query string.
      Overrides:
      appendUpdateSetParameter in class AbstractSqlLikeQueryBuilder
      Parameters:
      sb - The string builder
      alias - The alias
      prop - The property
      appendParameter - The append parameter action
    • buildJoin

      protected void buildJoin(@Nullable String joinType, StringBuilder query, AbstractSqlLikeQueryBuilder.QueryState queryState, PersistentAssociationPath joinAssociation, PersistentEntity associationOwner, String currentJoinAlias, String lastJoinAlias)
      Description copied from class: AbstractSqlLikeQueryBuilder
      Build a join expression for the given alias, association, join type and builder.
      Overrides:
      buildJoin in class AbstractSqlLikeQueryBuilder
      Parameters:
      joinType - The join type
      query - The query builder
      queryState - The state
      joinAssociation - The association
      associationOwner - The associated owner
      currentJoinAlias - The current join alias
      lastJoinAlias - The last join alias
    • quote

      protected String quote(String persistedName, boolean supportsDynamicValues)
      Description copied from class: AbstractSqlLikeQueryBuilder
      Quote a persisted name (schema, table or column name) for the dialect.
      Overrides:
      quote in class AbstractSqlLikeQueryBuilder
      Parameters:
      persistedName - The persisted name.
      supportsDynamicValues - Whether persisted name supports dynamic values. Schema and table can have dynamic value (like ${config.entry}) and columns can't.
      Returns:
      The quoted name
    • getColumnName

      public String getColumnName(PersistentProperty persistentProperty)
      Description copied from class: AbstractSqlLikeQueryBuilder
      Get the column name for the given property.
      Specified by:
      getColumnName in class AbstractSqlLikeQueryBuilder
      Parameters:
      persistentProperty - The property
      Returns:
      The column name
    • appendForUpdate

      protected void appendForUpdate(AbstractSqlLikeQueryBuilder.QueryPosition queryPosition, QueryBuilder.SelectQueryDefinition definition, StringBuilder queryBuilder)
      Description copied from class: AbstractSqlLikeQueryBuilder
      Adds "forUpdate" pessimistic locking.
      Overrides:
      appendForUpdate in class AbstractSqlLikeQueryBuilder
      Parameters:
      queryPosition - The query position
      definition - The definition
      queryBuilder - The builder
    • computePropertyPaths

      protected boolean computePropertyPaths()
      Description copied from class: AbstractSqlLikeQueryBuilder
      Whether property path expressions require computation by the implementation. In a certain query dialects property paths are supported (such as JPA-QL where you can do select foo.bar) whilst for explicit SQL queries paths like this have to be computed into aliases / column name references.
      Specified by:
      computePropertyPaths in class AbstractSqlLikeQueryBuilder
      Returns:
      True if property path computation is required.
    • isAliasForBatch

      protected boolean isAliasForBatch(PersistentEntity persistentEntity, io.micronaut.core.annotation.AnnotationMetadata annotationMetadata)
      Description copied from class: AbstractSqlLikeQueryBuilder
      Should aliases be used in batch statements.
      Specified by:
      isAliasForBatch in class AbstractSqlLikeQueryBuilder
      Parameters:
      persistentEntity - the persistent entity
      annotationMetadata - the method annotation metadata
      Returns:
      True if they should
    • formatParameter

      public AbstractSqlLikeQueryBuilder.Placeholder formatParameter(int index)
      Description copied from class: AbstractSqlLikeQueryBuilder
      Format the parameter at the given index.
      Specified by:
      formatParameter in class AbstractSqlLikeQueryBuilder
      Parameters:
      index - The parameter
      Returns:
      The index
    • selectAutoStrategy

      protected GeneratedValue.Type selectAutoStrategy(PersistentProperty property)
      Selects the default fallback strategy. For a generated value.
      Parameters:
      property - The Persistent property
      Returns:
      The generated value
    • positionalParameterFormat

      public final String positionalParameterFormat()
      Returns:
      The positional parameter format
    • appendLimitAndOrder

      protected void appendLimitAndOrder(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, QueryBuilder.SelectQueryDefinition definition, boolean appendLimit, boolean appendOrder, AbstractSqlLikeQueryBuilder.QueryState queryState)
      Overrides:
      appendLimitAndOrder in class AbstractSqlLikeQueryBuilder