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
  • 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.
    • shouldEscape

      protected boolean shouldEscape(@NonNull @NonNull 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
    • asLiteral

      protected String asLiteral(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

      @NonNull public @NonNull String buildBatchCreateTableStatement(@NonNull @NonNull 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
    • buildBatchDropTableStatement

      @NonNull public @NonNull String buildBatchDropTableStatement(@NonNull @NonNull 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

      @NonNull public @NonNull String[] buildDropTableStatements(@NonNull @NonNull 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

      @NonNull public @NonNull String buildJoinTableInsert(@NonNull @NonNull PersistentEntity entity, @NonNull @NonNull 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(@NonNull @NonNull 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

      @NonNull public @NonNull String[] buildCreateTableStatements(@NonNull @NonNull PersistentEntity entity)
      Builds the creation 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
    • buildCreateTableStatements

      @NonNull public @NonNull String[] buildCreateTableStatements(PersistentEntity[] entities)
      Builds the creation table statement for 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
    • 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
    • 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

      @NonNull public @NonNull 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
    • getAliasName

      protected String getAliasName(PersistentEntity entity)
      Description copied from class: AbstractSqlLikeQueryBuilder
      Get an alias name for the given entity.
      Overrides:
      getAliasName in class AbstractSqlLikeQueryBuilder
      Parameters:
      entity - The entity
      Returns:
      The 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, 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(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