Class AbstractSqlLikeQueryBuilder2

java.lang.Object
io.micronaut.data.model.query.builder.sql.AbstractSqlLikeQueryBuilder2
All Implemented Interfaces:
QueryBuilder2
Direct Known Subclasses:
JpaQueryBuilder2, SqlQueryBuilder2

@Internal public abstract class AbstractSqlLikeQueryBuilder2 extends Object implements QueryBuilder2
An abstract class for builders that build SQL-like queries.
Since:
1.0.0
Author:
graemerocher, Denis Stepanov
  • Field Details

  • Constructor Details

    • AbstractSqlLikeQueryBuilder2

      public AbstractSqlLikeQueryBuilder2()
  • Method Details

    • getDialect

      protected Dialect getDialect()
      Get dialect.
      Returns:
      dialect
    • traverseEmbedded

      protected boolean traverseEmbedded()
      Returns:
      True if embedded properties should be traversed
    • asLiteral

      @NonNull protected @NonNull String asLiteral(@Nullable @Nullable Object value)
      Convert the literal value to it's SQL representation.
      Parameters:
      value - The literal value
      Returns:
      converter value
    • asQueryPropertyPath

      protected final AbstractSqlLikeQueryBuilder2.QueryPropertyPath asQueryPropertyPath(String tableAlias, PersistentProperty persistentProperty)
    • buildSelect

      public QueryResult buildSelect(@NonNull @NonNull io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, @NonNull @NonNull QueryBuilder2.SelectQueryDefinition definition)
      Description copied from interface: QueryBuilder2
      Encode the given query for the passed annotation metadata and query.
      Specified by:
      buildSelect in interface QueryBuilder2
      Parameters:
      annotationMetadata - The annotation metadata
      definition - The query model
      Returns:
      The query result
    • getTableName

      protected abstract String getTableName(PersistentEntity entity)
      Get the table name for the given entity.
      Parameters:
      entity - The entity
      Returns:
      The table name
    • getUnescapedTableName

      protected String getUnescapedTableName(PersistentEntity entity)
      Get the table name for the given entity.
      Parameters:
      entity - The entity
      Returns:
      The table name
    • getAliasName

      protected String getAliasName(PersistentEntity entity)
      Get an alias name for the given entity.
      Parameters:
      entity - The entity
      Returns:
      The alias name
    • getAliasName

      public String getAliasName(JoinPath joinPath)
      Get the alias name.
      Parameters:
      joinPath - The join path
      Returns:
      The alias
    • getPathOnlyAliasName

      @NonNull protected @NonNull String getPathOnlyAliasName(JoinPath joinPath)
      Get the alias name for just the join path.
      Parameters:
      joinPath - The join path
      Returns:
      The alias
    • buildJoin

      protected void buildJoin(String joinType, StringBuilder query, AbstractSqlLikeQueryBuilder2.QueryState queryState, PersistentAssociationPath joinAssociation, PersistentEntity associationOwner, String currentJoinAlias, String lastJoinAlias)
      Build a join expression for the given alias, association, join type and builder.
      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
    • getColumnName

      protected abstract String getColumnName(PersistentProperty persistentProperty)
      Get the column name for the given property.
      Parameters:
      persistentProperty - The property
      Returns:
      The column name
    • shouldEscape

      protected boolean shouldEscape(@NonNull @NonNull PersistentEntity entity)
      Whether queries should be escaped for the given entity.
      Parameters:
      entity - The entity
      Returns:
      True if they should be escaped
    • getTableAsKeyword

      protected String getTableAsKeyword()
      Get the AS keyword to use for table aliases.
      Returns:
      The AS keyword if any
    • quote

      protected String quote(String persistedName)
      Quote a column name for the dialect.
      Parameters:
      persistedName - The persisted name.
      Returns:
      The quoted name
    • buildSelect

      protected void buildSelect(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, AbstractSqlLikeQueryBuilder2.QueryState queryState, jakarta.persistence.criteria.Selection<?> selection, boolean distinct)
      Build select statement.
      Parameters:
      annotationMetadata - the annotation metadata
      queryState - the query state
      selection - projection list (can be empty, then selects all columns)
      distinct - is distinct selection
    • createSelectionVisitor

      protected AbstractSqlLikeQueryBuilder2.SqlSelectionVisitor createSelectionVisitor(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, AbstractSqlLikeQueryBuilder2.QueryState queryState, boolean distinct)
      Create a selection visitor.
      Parameters:
      annotationMetadata - The annotation metadata
      queryState - The query state
      distinct - The distinct
      Returns:
      The visitor
    • getNamingStrategy

      protected NamingStrategy getNamingStrategy(PersistentPropertyPath propertyPath)
      Gets NamingStrategy for the property path. Subclasses might override and potentially provide different strategy in some cases.
      Parameters:
      propertyPath - the property path representation
      Returns:
      naming strategy for the property path
    • getNamingStrategy

      protected NamingStrategy getNamingStrategy(PersistentEntity entity)
      Gets NamingStrategy for the entity. Subclasses might override and potentially provide different strategy in some cases.
      Parameters:
      entity - the persistent entity
      Returns:
      naming strategy for the entity
    • getMappedName

      @NonNull protected @NonNull String getMappedName(@NonNull @NonNull NamingStrategy namingStrategy, @NonNull @NonNull Association association)
      Gets the mapped name from the association using NamingStrategy.
      Parameters:
      namingStrategy - the naming strategy being used
      association - the association
      Returns:
      the mapped name for the association
    • getMappedName

      @NonNull protected @NonNull String getMappedName(@NonNull @NonNull NamingStrategy namingStrategy, @NonNull @NonNull List<Association> associations, @NonNull @NonNull PersistentProperty property)
      Gets the mapped name from for the list of associations and property using NamingStrategy.
      Parameters:
      namingStrategy - the naming strategy
      associations - the association list
      property - the property
      Returns:
      the mappen name for the list of associations and property using given naming strategy
    • getMappedName

      @NonNull protected @NonNull String getMappedName(@NonNull @NonNull NamingStrategy namingStrategy, @NonNull @NonNull PersistentPropertyPath propertyPath)
      Gets the mapped name from for the list of associations and property using NamingStrategy.
      Parameters:
      namingStrategy - the naming strategy
      propertyPath - the property path
      Returns:
      the mappen name for the list of associations and property using given naming strategy
    • buildWhereClause

      protected void buildWhereClause(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, jakarta.persistence.criteria.Predicate predicate, AbstractSqlLikeQueryBuilder2.QueryState queryState)
      Builds where clause.
      Parameters:
      annotationMetadata - the annotation metadata for the method
      predicate - the predicate
      queryState - the query state
    • createPredicateVisitor

      protected AbstractSqlLikeQueryBuilder2.SqlPredicateVisitor createPredicateVisitor(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, AbstractSqlLikeQueryBuilder2.QueryState queryState)
      Create a predicate visitor.
      Parameters:
      annotationMetadata - The annotation metadata
      queryState - The query state
      Returns:
      The visitor
    • buildAdditionalWhereClause

      protected String buildAdditionalWhereClause(AbstractSqlLikeQueryBuilder2.QueryState queryState, io.micronaut.core.annotation.AnnotationMetadata annotationMetadata)
      Builds additional where clause if there is Where annotation on the entity.
      Parameters:
      queryState - the query state
      annotationMetadata - the annotation metadata
      Returns:
      where clause if there was Where annotation on the entity (or joins for JPA implementation)
    • buildAdditionalWhereString

      protected String buildAdditionalWhereString(String alias, PersistentEntity entity, io.micronaut.core.annotation.AnnotationMetadata annotationMetadata)
      Builds WHERE clause for the entity and given alias if IgnoreWhere is not present.
      Parameters:
      alias - the entity alias
      entity - the entity
      annotationMetadata - the entity metadata
      Returns:
      the WHERE clause
    • buildAdditionalWhereString

      protected final String buildAdditionalWhereString(JoinPath joinPath, io.micronaut.core.annotation.AnnotationMetadata annotationMetadata)
      Builds WHERE clause based on Where annotation on the metadata.
      Parameters:
      joinPath - the join path
      annotationMetadata - the annotation metadata
      Returns:
      WHERE clause if Where annotation is declared and IgnoreWhere is not present, otherwise empty string
    • resolveWhereForAnnotationMetadata

      protected final String resolveWhereForAnnotationMetadata(String alias, io.micronaut.core.annotation.AnnotationMetadata annotationMetadata)
      Resolves where clause if there is Where annotation on the entity.
      Parameters:
      alias - the entity alias
      annotationMetadata - the entity annotation metadata
      Returns:
      where clause with entity alias if entity has declared where annotation
    • appendOrder

      protected void appendOrder(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, QueryBuilder2.SelectQueryDefinition definition, AbstractSqlLikeQueryBuilder2.QueryState queryState)
      Appends order to the query.
      Parameters:
      annotationMetadata - the annotation metadata
      definition - the query model
      queryState - the query state
    • appendForUpdate

      protected void appendForUpdate(AbstractSqlLikeQueryBuilder2.QueryPosition queryPosition, QueryBuilder2.SelectQueryDefinition definition, StringBuilder queryBuilder)
      Adds "forUpdate" pessimistic locking.
      Parameters:
      queryPosition - The query position
      definition - The definition
      queryBuilder - The builder
    • appendUpdateSetParameter

      protected void appendUpdateSetParameter(StringBuilder sb, String alias, PersistentProperty prop, Runnable appendParameter)
      Appends the SET=? call to the query string.
      Parameters:
      sb - The string builder
      alias - The alias
      prop - The property
      appendParameter - The append parameter action
    • appendTransformed

      protected void appendTransformed(StringBuilder sb, String transformed, Runnable appendParameter)
      Appends custom query part.
      Parameters:
      sb - The string builder
      transformed - The transformed query part
      appendParameter - The append parameter action
    • computePropertyPaths

      protected abstract boolean computePropertyPaths()
      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.
      Returns:
      True if property path computation is required.
    • buildUpdate

      public QueryResult buildUpdate(@NonNull @NonNull io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, @NonNull @NonNull QueryBuilder2.UpdateQueryDefinition definition)
      Description copied from interface: QueryBuilder2
      Encode the given query into the encoded query instance.
      Specified by:
      buildUpdate in interface QueryBuilder2
      Parameters:
      annotationMetadata - The annotation metadata
      definition - The definition
      Returns:
      The encoded query
    • buildDelete

      public QueryResult buildDelete(@NonNull @NonNull io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, @NonNull @NonNull QueryBuilder2.DeleteQueryDefinition definition)
      Description copied from interface: QueryBuilder2
      Encode the given query into the encoded query instance.
      Specified by:
      buildDelete in interface QueryBuilder2
      Parameters:
      annotationMetadata - The annotation metadata
      definition - The query definition
      Returns:
      The encoded query
    • isAliasForBatch

      protected abstract boolean isAliasForBatch(PersistentEntity persistentEntity, io.micronaut.core.annotation.AnnotationMetadata annotationMetadata)
      Should aliases be used in batch statements.
      Parameters:
      persistentEntity - the persistent entity
      annotationMetadata - the method annotation metadata
      Returns:
      True if they should
    • appendDeleteClause

      @NonNull protected @NonNull StringBuilder appendDeleteClause(StringBuilder queryString)
      Append the delete clause.
      Parameters:
      queryString - The query string
      Returns:
      The delete clause
    • buildOrderBy

      @NonNull public @NonNull QueryResult buildOrderBy(String query, @NonNull @NonNull PersistentEntity entity, @NonNull @NonNull io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, @NonNull @NonNull Sort sort, boolean nativeQuery)
      Encode the given query into the encoded query instance.
      Parameters:
      query - The query
      entity - The root entity
      annotationMetadata - The annotation metadata
      sort - The sort
      nativeQuery - Whether the query is native query, in which case sort field names will be supplied by the user and not verified
      Returns:
      The encoded query
    • buildPropertyByName

      public String buildPropertyByName(@NonNull @NonNull String propertyName, @NonNull @NonNull String query, @NonNull @NonNull PersistentEntity entity, @NonNull @NonNull io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, boolean nativeQuery)
      Encode the given property retrieval into a query instance. For example, property name might be encoded as `person_.name` using its path and table's alias.
      Parameters:
      propertyName - The name of the property
      query - The query
      entity - The root entity
      annotationMetadata - The annotation metadata
      nativeQuery - Whether the query is native query, in which case the property name will be supplied by the user and not verified
      Returns:
      The encoded query
    • asPath

      protected static String asPath(List<Association> associations, PersistentProperty property)
      Join associations and property as path.
      Parameters:
      associations - The associations
      property - The property
      Returns:
      joined path
    • newBindingContext

      protected BindingParameter.BindingContext newBindingContext(@Nullable @Nullable PersistentPropertyPath ref)
      Creates new binding parameter context.
      Parameters:
      ref - the persistent property reference
      Returns:
      new binding parameter context
    • getDataTransformerReadValue

      protected Optional<String> getDataTransformerReadValue(String alias, PersistentProperty prop)
      Returns transformed value if the data transformer id defined.
      Parameters:
      alias - query table alias
      prop - a property
      Returns:
      optional transformed value
    • getDataTransformerWriteValue

      protected Optional<String> getDataTransformerWriteValue(String alias, PersistentProperty prop)
      Returns transformed value if the data transformer id defined.
      Parameters:
      alias - query table alias
      prop - a property
      Returns:
      optional transformed value
    • formatParameter

      protected abstract AbstractSqlLikeQueryBuilder2.Placeholder formatParameter(int index)
      Format the parameter at the given index.
      Parameters:
      index - The parameter
      Returns:
      The index
    • resolveJoinType

      public abstract String resolveJoinType(Join.Type jt)
      Resolves the join type.
      Parameters:
      jt - The join type
      Returns:
      The join type.
    • getColumnAlias

      protected final String getColumnAlias(PersistentProperty property)
      Gets column alias if defined as alias field on MappedProperty annotation on the mapping field.
      Parameters:
      property - the persistent property
      Returns:
      column alias if defined, otherwise an empty string
    • checkDialectSupportsJsonEntity

      protected void checkDialectSupportsJsonEntity(PersistentEntity entity)
      If and when EntityRepresentation annotation with JSON type is used for the repository method but dialect does not support JSON entity representations this will throw IllegalArgumentException.
      Parameters:
      entity - the persistent entity
    • isJsonEntity

      protected boolean isJsonEntity(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, PersistentEntity entity)
      Checks whether EntityRepresentation annotation with JSON type is used for the repository method. If current dialect does not support handling JSON entity representations, IllegalArgumentException is thrown.
      Parameters:
      annotationMetadata - the annotation metadata
      entity - the persistent entity
      Returns:
      true if EntityRepresentation annotation with JSON type is used for the repository method
    • appendExpression

      protected final void appendExpression(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, StringBuilder query, AbstractSqlLikeQueryBuilder2.QueryState queryState, jakarta.persistence.criteria.Expression<?> expression, boolean isProjection)
    • appendPropertyRef

      protected final void appendPropertyRef(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, StringBuilder query, AbstractSqlLikeQueryBuilder2.QueryState queryState, PersistentPropertyPath pp, boolean isProjection)