Class SqlQueryBuilder

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

Implementation of QueryBuilder that builds SQL queries.
Since:
1.0.0
Author:
graemerocher
  • 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

    • getAnnotationMetadata

      public io.micronaut.core.annotation.AnnotationMetadata getAnnotationMetadata()
      Returns:
      The annotation metadata
    • 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
    • shouldAliasProjections

      public boolean shouldAliasProjections()
      Description copied from interface: QueryBuilder
      Whether projections should be aliased.
      Specified by:
      shouldAliasProjections in interface QueryBuilder
      Returns:
      True if they should
    • isExpandEmbedded

      protected boolean isExpandEmbedded()
      Description copied from class: AbstractSqlLikeQueryBuilder
      Should embedded queries by expanded by the implementation.
      Overrides:
      isExpandEmbedded in class AbstractSqlLikeQueryBuilder
      Returns:
      True if they should
    • 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
    • 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
    • selectAllColumns

      protected void selectAllColumns(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, AbstractSqlLikeQueryBuilder.QueryState queryState, StringBuilder queryBuffer)
      Description copied from class: AbstractSqlLikeQueryBuilder
      Obtain the string that selects all columns from the entity.
      Specified by:
      selectAllColumns in class AbstractSqlLikeQueryBuilder
      Parameters:
      annotationMetadata - The annotation metadata
      queryState - The query state
    • selectAllColumnsFromJoinPaths

      @Internal protected void selectAllColumnsFromJoinPaths(AbstractSqlLikeQueryBuilder.QueryState queryState, StringBuilder queryBuffer, Collection<JoinPath> allPaths, @Nullable @Nullable Map<JoinPath,String> joinAliasOverride)
      Overrides:
      selectAllColumnsFromJoinPaths in class AbstractSqlLikeQueryBuilder
    • selectAllColumns

      public void selectAllColumns(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, PersistentEntity entity, String alias, StringBuilder sb)
      Selects all columns for the given entity and alias.
      Specified by:
      selectAllColumns in class AbstractSqlLikeQueryBuilder
      Parameters:
      annotationMetadata - The annotation metadata
      entity - The entity
      alias - The alias
      sb - The builder to add the columns
    • 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, PersistentEntity entity)
      Description copied from interface: QueryBuilder
      Builds an insert statement for the given entity.
      Specified by:
      buildInsert in interface QueryBuilder
      Parameters:
      repositoryMetadata - The repository annotation metadata
      entity - The entity
      Returns:
      The insert statement or null if the implementation doesn't require insert statements
    • buildInsertReturning

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

      @NonNull public @NonNull QueryResult buildPagination(@NonNull @NonNull Pageable pageable)
      Description copied from interface: QueryBuilder
      Encode the pageable.
      Specified by:
      buildPagination in interface QueryBuilder
      Parameters:
      pageable - The pageable
      Returns:
      The encoded query
    • 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
    • concat

      protected void concat(StringBuilder writer, Collection<Runnable> partsWriters)
      Overrides:
      concat in class AbstractSqlLikeQueryBuilder
      Parameters:
      partsWriters - The parts writers
      writer - The writer
    • 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 String[] buildJoin(String alias, JoinPath joinPath, String joinType, StringBuilder target, Map<String,String> appliedJoinPaths, AbstractSqlLikeQueryBuilder.QueryState queryState)
      Description copied from class: AbstractSqlLikeQueryBuilder
      Build a join expression for the given alias, association, join type and builder.
      Specified by:
      buildJoin in class AbstractSqlLikeQueryBuilder
      Parameters:
      alias - The alias
      joinPath - The join path
      joinType - The join type string
      target - The target builder
      appliedJoinPaths - The applied joins paths
      queryState - The query state
      Returns:
      An array representing the aliases for each join association in the specified join path
    • buildJoin

      protected void buildJoin(String joinType, StringBuilder sb, AbstractSqlLikeQueryBuilder.QueryState queryState, List<Association> joinAssociationsPath, String joinAlias, Association association, PersistentEntity associatedEntity, PersistentEntity associationOwner, String currentJoinAlias)
      Builds join, adding fields and criteria.
      Parameters:
      joinType - the join type
      sb - string builder that join will be added to
      queryState - the query state
      joinAssociationsPath - the list of associations
      joinAlias - the join alias
      association - the association
      associatedEntity - the associated entity
      associationOwner - the association owner
      currentJoinAlias - the current 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
    • appendProjectionRowCount

      protected void appendProjectionRowCount(StringBuilder queryString, String logicalName)
      Description copied from class: AbstractSqlLikeQueryBuilder
      Appends a row count projection to the query string.
      Specified by:
      appendProjectionRowCount in class AbstractSqlLikeQueryBuilder
      Parameters:
      queryString - The query string
      logicalName - The alias to the table name
    • appendProjectionRowCountDistinct

      protected void appendProjectionRowCountDistinct(StringBuilder queryString, AbstractSqlLikeQueryBuilder.QueryState queryState, PersistentEntity entity, io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, String logicalName)
      Description copied from class: AbstractSqlLikeQueryBuilder
      Appends a row count distinct projection to the query string.
      Specified by:
      appendProjectionRowCountDistinct in class AbstractSqlLikeQueryBuilder
      Parameters:
      queryString - The query string
      queryState - The query state
      entity - The persistent entity
      annotationMetadata - The query annotation metadata
      logicalName - The alias to the table name
    • appendForUpdate

      protected void appendForUpdate(AbstractSqlLikeQueryBuilder.QueryPosition queryPosition, QueryModel query, StringBuilder queryBuilder)
      Description copied from class: AbstractSqlLikeQueryBuilder
      Adds "forUpdate" pisimmistic locking.
      Overrides:
      appendForUpdate in class AbstractSqlLikeQueryBuilder
      Parameters:
      queryPosition - The query position
      query - The query
      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
    • supportsForUpdate

      public boolean supportsForUpdate()
      Description copied from interface: QueryBuilder
      Whether FOR UPDATE queries are supported.
      Specified by:
      supportsForUpdate in interface QueryBuilder
      Returns:
      True if FOR UPDATE queries are supported
    • dialect

      public Dialect dialect()
    • positionalParameterFormat

      public String positionalParameterFormat()
    • positionalParameterName

      public String positionalParameterName()
    • positionalParameterPattern

      public Pattern positionalParameterPattern()
      Returns:
      The regex pattern for positional parameters.
    • escapeQueries

      public boolean escapeQueries()
    • annotationType

      public Class<? extends Annotation> annotationType()
      Specified by:
      annotationType in interface Annotation