Class AbstractSqlLikeQueryBuilder

  • All Implemented Interfaces:
    QueryBuilder
    Direct Known Subclasses:
    JpaQueryBuilder, SqlQueryBuilder

    public abstract class AbstractSqlLikeQueryBuilder
    extends java.lang.Object
    implements QueryBuilder
    An abstract class for builders that build SQL-like queries.
    Since:
    1.0.0
    • Constructor Detail

      • AbstractSqlLikeQueryBuilder

        public AbstractSqlLikeQueryBuilder()
    • Method Detail

      • getDialect

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

        protected void asLiterals​(java.lang.StringBuilder sb,
                                  @Nullable
                                  java.lang.Object value)
        Appends values as literals to the sql query builder.
        Parameters:
        sb - the sql string builder
        value - the value to be added
      • asLiteral

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

        protected void concat​(java.lang.StringBuilder writer,
                              java.util.Collection<java.lang.Runnable> partsWriters)
        Parameters:
        writer - The writer
        partsWriters - The parts writers
      • buildQuery

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

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

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

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

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

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

        protected abstract java.lang.String[] buildJoin​(java.lang.String alias,
                                                        JoinPath joinPath,
                                                        java.lang.String joinType,
                                                        java.lang.StringBuilder stringBuilder,
                                                        java.util.Map<java.lang.String,​java.lang.String> appliedJoinPaths,
                                                        AbstractSqlLikeQueryBuilder.QueryState queryState)
        Build a join expression for the given alias, association, join type and builder.
        Parameters:
        alias - The alias
        joinPath - The join path
        joinType - The join type string
        stringBuilder - 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
      • getColumnName

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

        protected abstract void selectAllColumns​(AbstractSqlLikeQueryBuilder.QueryState queryState,
                                                 java.lang.StringBuilder queryBuffer)
        Obtain the string that selects all columns from the entity.
        Parameters:
        queryState - The query state
        queryBuffer -
      • selectAllColumns

        protected abstract void selectAllColumns​(PersistentEntity entity,
                                                 java.lang.String alias,
                                                 java.lang.StringBuilder queryBuffer)
        Selects all columns for the given entity and alias.
        Parameters:
        entity - The entity
        alias - The alias
        queryBuffer - The buffer to append the columns
      • selectAllColumnsFromJoinPaths

        @Internal
        protected void selectAllColumnsFromJoinPaths​(AbstractSqlLikeQueryBuilder.QueryState queryState,
                                                     java.lang.StringBuilder queryBuffer,
                                                     java.util.Collection<JoinPath> allPaths,
                                                     @Nullable
                                                     java.util.Map<JoinPath,​java.lang.String> joinAliasOverride)
      • shouldEscape

        protected boolean shouldEscape​(@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 java.lang.String getTableAsKeyword()
        Get the AS keyword to use for table aliases.
        Returns:
        The AS keyword if any
      • quote

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

        protected void buildSelect​(AbstractSqlLikeQueryBuilder.QueryState queryState,
                                   java.lang.StringBuilder queryString,
                                   java.util.List<QueryModel.Projection> projectionList,
                                   java.lang.String tableAlias,
                                   PersistentEntity entity)
        Build select statement.
        Parameters:
        queryState - the query state
        queryString - the query string builder
        projectionList - projection list (can be empty, then selects all columns)
        tableAlias - the table alias
        entity - the persistent entity
      • appendAssociationProjection

        protected boolean appendAssociationProjection​(AbstractSqlLikeQueryBuilder.QueryState queryState,
                                                      java.lang.StringBuilder queryString,
                                                      PersistentProperty property,
                                                      PersistentPropertyPath propertyPath)
        Appends selection projection for the property which is association.
        Parameters:
        queryState - the query state
        queryString - the query string builder
        property - the persistent property
        propertyPath - the persistent property path
        Returns:
        true if association projection is appended, otherwise false
      • 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 java.lang.String getMappedName​(@NonNull
                                                 NamingStrategy namingStrategy,
                                                 @NonNull
                                                 PersistentProperty property)
        Gets the mapped name from the property using NamingStrategy.
        Parameters:
        namingStrategy - the naming strategy being used
        property - the persistent property
        Returns:
        the mapped name for the property
      • getMappedName

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

        @NonNull
        protected java.lang.String getMappedName​(@NonNull
                                                 NamingStrategy namingStrategy,
                                                 @NonNull
                                                 java.util.List<Association> associations,
                                                 @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
      • appendProjectionRowCount

        protected abstract void appendProjectionRowCount​(java.lang.StringBuilder queryString,
                                                         java.lang.String logicalName)
        Appends a row count projection to the query string.
        Parameters:
        queryString - The query string
        logicalName - The alias to the table name
      • buildWhereClause

        protected void buildWhereClause​(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata,
                                        QueryModel.Junction criteria,
                                        AbstractSqlLikeQueryBuilder.QueryState queryState)
        Builds where clause.
        Parameters:
        annotationMetadata - the annotation metadata for the method
        criteria - the criteria
        queryState - the query state
      • appendForUpdate

        protected void appendForUpdate​(AbstractSqlLikeQueryBuilder.QueryPosition queryPosition,
                                       QueryModel query,
                                       java.lang.StringBuilder queryBuilder)
        Adds "forUpdate" pisimmistic locking.
        Parameters:
        queryPosition - The query position
        query - The query
        queryBuilder - The builder
      • appendPropertyRef

        protected void appendPropertyRef​(java.lang.StringBuilder sb,
                                         AbstractSqlLikeQueryBuilder.QueryPropertyPath propertyPath)
        Appends property to the sql string builder.
        Parameters:
        sb - the sql string builder
        propertyPath - the query property path
      • isExpandEmbedded

        protected boolean isExpandEmbedded()
        Should embedded queries by expanded by the implementation.
        Returns:
        True if they should
      • appendUpdateSetParameter

        protected void appendUpdateSetParameter​(java.lang.StringBuilder sb,
                                                java.lang.String alias,
                                                PersistentProperty prop,
                                                java.lang.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​(java.lang.StringBuilder sb,
                                         java.lang.String transformed,
                                         java.lang.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
                                       io.micronaut.core.annotation.AnnotationMetadata annotationMetadata,
                                       @NonNull
                                       QueryModel query,
                                       @NonNull
                                       java.util.List<java.lang.String> propertiesToUpdate)
        Description copied from interface: QueryBuilder
        Encode the given query into the encoded query instance.
        Specified by:
        buildUpdate in interface QueryBuilder
        Parameters:
        annotationMetadata - The annotation metadata
        query - The query
        propertiesToUpdate - The property names to update
        Returns:
        The encoded query
      • buildUpdate

        public QueryResult buildUpdate​(@NonNull
                                       io.micronaut.core.annotation.AnnotationMetadata annotationMetadata,
                                       @NonNull
                                       QueryModel query,
                                       @NonNull
                                       java.util.Map<java.lang.String,​java.lang.Object> propertiesToUpdate)
        Description copied from interface: QueryBuilder
        Encode the given query into the encoded query instance.
        Specified by:
        buildUpdate in interface QueryBuilder
        Parameters:
        annotationMetadata - The annotation metadata
        query - The query
        propertiesToUpdate - The property names to update
        Returns:
        The encoded query
      • buildDelete

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

        protected abstract boolean isAliasForBatch()
        Should aliases be used in batch statements.
        Returns:
        True if they should
      • appendDeleteClause

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

        @NonNull
        public QueryResult buildOrderBy​(@NonNull
                                        PersistentEntity entity,
                                        @NonNull
                                        Sort sort)
        Description copied from interface: QueryBuilder
        Encode the given query into the encoded query instance.
        Specified by:
        buildOrderBy in interface QueryBuilder
        Parameters:
        entity - The root entity
        sort - The sort
        Returns:
        The encoded query
      • buildOrderBy

        @NonNull
        public QueryResult buildOrderBy​(java.lang.String query,
                                        @NonNull
                                        PersistentEntity entity,
                                        @NonNull
                                        Sort sort)
        Encode the given query into the encoded query instance.
        Parameters:
        query - The query
        entity - The root entity
        sort - The sort
        Returns:
        The encoded query
      • asPath

        protected java.lang.String asPath​(java.util.List<Association> associations,
                                          PersistentProperty property)
        Join associations and property as path.
        Parameters:
        associations - The associations
        property - The property
        Returns:
        joined path
      • traversePersistentProperties

        protected void traversePersistentProperties​(PersistentProperty property,
                                                    java.util.function.BiConsumer<java.util.List<Association>,​PersistentProperty> consumer)
        Traverses persistent properties.
        Parameters:
        property - The property to start traversing from
        consumer - The function to invoke on every property
      • traversePersistentProperties

        protected void traversePersistentProperties​(PersistentEntity persistentEntity,
                                                    java.util.function.BiConsumer<java.util.List<Association>,​PersistentProperty> consumer)
        Traverses persistent properties.
        Parameters:
        persistentEntity - The persistent entity
        consumer - The function to invoke on every property
      • traversePersistentProperties

        protected void traversePersistentProperties​(PersistentEntity persistentEntity,
                                                    boolean includeIdentity,
                                                    boolean includeVersion,
                                                    java.util.function.BiConsumer<java.util.List<Association>,​PersistentProperty> consumer)
        Traverses persistent properties.
        Parameters:
        persistentEntity - The persistent entity
        includeIdentity - Should be identifier included
        includeVersion - Should be version included
        consumer - The function to invoke on every property
      • traversePersistentProperties

        protected void traversePersistentProperties​(java.util.List<Association> associations,
                                                    PersistentProperty property,
                                                    java.util.function.BiConsumer<java.util.List<Association>,​PersistentProperty> consumerProperty)
        Traverses persistent properties.
        Parameters:
        associations - The association list being traversed with the property
        property - The persistent property
        consumerProperty - The function to invoke on every property
      • getDataTransformerReadValue

        protected java.util.Optional<java.lang.String> getDataTransformerReadValue​(java.lang.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 java.util.Optional<java.lang.String> getDataTransformerWriteValue​(java.lang.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 AbstractSqlLikeQueryBuilder.Placeholder formatParameter​(int index)
        Format the parameter at the given index.
        Parameters:
        index - The parameter
        Returns:
        The index
      • resolveJoinType

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

        protected final java.lang.String getColumnAlias​(PersistentProperty property)
        Gets column alias if defined as alias field on MappedProperty annotation on the mapping field.
        Parameters:
        property - the persisent propert
        Returns:
        column alias if defined, otherwise an empty string