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
public class SqlQueryBuilder
extends AbstractSqlLikeQueryBuilder
implements QueryBuilder, SqlQueryConfiguration.DialectConfiguration
Implementation of 
QueryBuilder that builds SQL queries.- Since:
- 1.0.0
- Author:
- graemerocher
- 
Nested Class SummaryNested classes/interfaces inherited from class io.micronaut.data.model.query.builder.AbstractSqlLikeQueryBuilderAbstractSqlLikeQueryBuilder.CriteriaContext, AbstractSqlLikeQueryBuilder.CriterionHandler<T extends QueryModel.Criterion>, AbstractSqlLikeQueryBuilder.Placeholder, AbstractSqlLikeQueryBuilder.QueryPosition, AbstractSqlLikeQueryBuilder.QueryPropertyPath, AbstractSqlLikeQueryBuilder.QueryState
- 
Field SummaryFieldsModifier and TypeFieldDescriptionstatic final StringThe start of an IN expression.static final Stringstatic final StringFields inherited from class io.micronaut.data.model.query.builder.AbstractSqlLikeQueryBuilderALIAS_REPLACE, ALIAS_REPLACE_QUOTED, AND, AS_CLAUSE, AVG, CLOSE_BRACKET, COMMA, COUNT_DISTINCT, DELETE_CLAUSE, DISTINCT, DOT, EQUALS, EQUALS_FALSE, EQUALS_TRUE, FROM_CLAUSE, FUNCTION_COUNT, GREATER_THAN, GREATER_THAN_OR_EQUALS, IS_EMPTY, IS_NOT_EMPTY, IS_NOT_NULL, IS_NULL, JSON_COLUMN, LESS_THAN, LESS_THAN_OR_EQUALS, LOGICAL_AND, LOGICAL_OR, MAX, MIN, NOT, NOT_EQUALS, OPEN_BRACKET, OR, ORDER_BY_CLAUSE, queryHandlers, SELECT_CLAUSE, SPACE, SUM, UPDATE_CLAUSE, WHERE_CLAUSEFields inherited from interface io.micronaut.data.model.query.builder.QueryBuilderVARIABLE_PATTERN
- 
Constructor SummaryConstructorsConstructorDescriptionDefault constructor.SqlQueryBuilder(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata) Constructor with annotation metadata.SqlQueryBuilder(Dialect dialect) 
- 
Method SummaryModifier and TypeMethodDescriptionClass<? extends Annotation>protected voidappendForUpdate(AbstractSqlLikeQueryBuilder.QueryPosition queryPosition, QueryModel query, StringBuilder queryBuilder) Adds "forUpdate" pisimmistic locking.protected voidappendProjectionRowCount(StringBuilder queryString, String logicalName) Appends a row count projection to the query string.protected voidappendUpdateSetParameter(StringBuilder sb, String alias, PersistentProperty prop, Runnable appendParameter) Appends the SET=? call to the query string.protected StringConvert the literal value to it's SQL representation.@NonNull StringbuildBatchCreateTableStatement(@NonNull PersistentEntity... entities) Builds a batch create tables statement.@NonNull StringbuildBatchDropTableStatement(@NonNull PersistentEntity... entities) Builds a batch drop tables statement.@NonNull String[]buildCreateTableStatements(@NonNull PersistentEntity entity) Builds the create table statement.@NonNull String[]buildDropTableStatements(@NonNull PersistentEntity entity) Builds the drop table statement.@NonNull QueryResultbuildInsert(io.micronaut.core.annotation.AnnotationMetadata repositoryMetadata, PersistentEntity entity) Builds an insert statement for the given entity.protected String[]buildJoin(String alias, JoinPath joinPath, String joinType, StringBuilder target, Map<String, String> appliedJoinPaths, AbstractSqlLikeQueryBuilder.QueryState queryState) Build a join expression for the given alias, association, join type and builder.protected voidbuildJoin(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.@NonNull StringbuildJoinTableInsert(@NonNull PersistentEntity entity, @NonNull Association association) Builds a join table insert statement for a given entity and association.@NonNull QueryResultbuildPagination(@NonNull Pageable pageable) Encode the pageable.protected booleanWhether property path expressions require computation by the implementation.protected voidconcat(StringBuilder writer, Collection<Runnable> partsWriters) dialect()booleanformatParameter(int index) Format the parameter at the given index.protected StringgetAliasName(PersistentEntity entity) Get an alias name for the given entity.getColumnName(PersistentProperty persistentProperty) Get the column name for the given property.Get dialect.protected StringGet the AS keyword to use for table aliases.getTableName(PersistentEntity entity) Get the table name for the given entity.protected booleanisAliasForBatch(PersistentEntity persistentEntity, io.micronaut.core.annotation.AnnotationMetadata annotationMetadata) Should aliases be used in batch statements.protected booleanShould embedded queries by expanded by the implementation.static booleanisForeignKeyWithJoinTable(@NonNull Association association) Is the given association a foreign key reference that requires a join table.protected StringQuote a column name for the dialect.Resolves the join type.voidselectAllColumns(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, PersistentEntity entity, String alias, StringBuilder sb) Selects all columns for the given entity and alias.protected voidselectAllColumns(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, AbstractSqlLikeQueryBuilder.QueryState queryState, StringBuilder queryBuffer) Obtain the string that selects all columns from the entity.protected voidselectAllColumnsFromJoinPaths(AbstractSqlLikeQueryBuilder.QueryState queryState, StringBuilder queryBuffer, Collection<JoinPath> allPaths, @Nullable Map<JoinPath, String> joinAliasOverride) protected GeneratedValue.TypeselectAutoStrategy(PersistentProperty property) Selects the default fallback strategy.booleanWhether projections should be aliased.protected booleanshouldEscape(@NonNull PersistentEntity entity) Whether queries should be escaped for the given entity.booleanWhether FOR UPDATE queries are supported.Methods inherited from class io.micronaut.data.model.query.builder.AbstractSqlLikeQueryBuilderaddCriterionHandler, appendAssociationProjection, appendDeleteClause, appendOrder, appendPropertyRef, appendTransformed, asLiterals, asPath, buildAdditionalWhereClause, buildAdditionalWhereString, buildAdditionalWhereString, buildDelete, buildOrderBy, buildOrderBy, buildQuery, buildSelect, buildUpdate, buildUpdate, buildWhereClause, checkDialectSupportsJsonEntity, getAliasName, getColumnAlias, getDataTransformerReadValue, getDataTransformerWriteValue, getMappedName, getMappedName, getMappedName, getNamingStrategy, getNamingStrategy, getPathOnlyAliasName, getUnescapedTableName, handleSubQuery, isJsonEntity, newBindingContext, resolveWhereForAnnotationMetadata, selectAllColumns, traversePersistentProperties, traversePersistentProperties, traversePersistentProperties, traversePersistentPropertiesMethods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.micronaut.data.model.query.builder.QueryBuilderbuildDelete, buildDelete, buildOrderBy, buildQuery, buildUpdate, buildUpdate, buildUpdate, buildUpdate
- 
Field Details- 
DEFAULT_POSITIONAL_PARAMETER_MARKERThe start of an IN expression.- See Also:
 
- 
STANDARD_FOR_UPDATE_CLAUSE- See Also:
 
- 
SQL_SERVER_FOR_UPDATE_CLAUSE- See Also:
 
 
- 
- 
Constructor Details- 
SqlQueryBuilder@Creator public SqlQueryBuilder(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata) Constructor with annotation metadata.- Parameters:
- annotationMetadata- The annotation metadata
 
- 
SqlQueryBuilderpublic SqlQueryBuilder()Default constructor.
- 
SqlQueryBuilder- Parameters:
- dialect- The dialect
 
 
- 
- 
Method Details- 
getDialectDescription copied from class:AbstractSqlLikeQueryBuilderGet dialect.- Overrides:
- getDialectin class- AbstractSqlLikeQueryBuilder
- Returns:
- The dialect being used by the builder.
 
- 
shouldEscapeDescription copied from class:AbstractSqlLikeQueryBuilderWhether queries should be escaped for the given entity.- Overrides:
- shouldEscapein class- AbstractSqlLikeQueryBuilder
- Parameters:
- entity- The entity
- Returns:
- True if they should be escaped
 
- 
asLiteralDescription copied from class:AbstractSqlLikeQueryBuilderConvert the literal value to it's SQL representation.- Overrides:
- asLiteralin class- AbstractSqlLikeQueryBuilder
- Parameters:
- value- The literal value
- Returns:
- converter value
 
- 
shouldAliasProjectionspublic boolean shouldAliasProjections()Description copied from interface:QueryBuilderWhether projections should be aliased.- Specified by:
- shouldAliasProjectionsin interface- QueryBuilder
- Returns:
- True if they should
 
- 
isExpandEmbeddedprotected boolean isExpandEmbedded()Description copied from class:AbstractSqlLikeQueryBuilderShould embedded queries by expanded by the implementation.- Overrides:
- isExpandEmbeddedin 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
 
- 
isForeignKeyWithJoinTableIs 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 create 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
 
- 
getTableAsKeywordDescription copied from class:AbstractSqlLikeQueryBuilderGet the AS keyword to use for table aliases.- Overrides:
- getTableAsKeywordin class- AbstractSqlLikeQueryBuilder
- Returns:
- The AS keyword if any
 
- 
selectAllColumnsprotected void selectAllColumns(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, AbstractSqlLikeQueryBuilder.QueryState queryState, StringBuilder queryBuffer) Description copied from class:AbstractSqlLikeQueryBuilderObtain the string that selects all columns from the entity.- Specified by:
- selectAllColumnsin 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:
- selectAllColumnsFromJoinPathsin class- AbstractSqlLikeQueryBuilder
 
- 
selectAllColumnspublic 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:
- selectAllColumnsin class- AbstractSqlLikeQueryBuilder
- Parameters:
- annotationMetadata- The annotation metadata
- entity- The entity
- alias- The alias
- sb- The builder to add the columns
 
- 
resolveJoinTypeDescription copied from class:AbstractSqlLikeQueryBuilderResolves the join type.- Specified by:
- resolveJoinTypein 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:QueryBuilderBuilds an insert statement for the given entity.- Specified by:
- buildInsertin 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
 
- 
buildPaginationDescription copied from interface:QueryBuilderEncode the pageable.- Specified by:
- buildPaginationin interface- QueryBuilder
- Parameters:
- pageable- The pageable
- Returns:
- The encoded query
 
- 
getAliasNameDescription copied from class:AbstractSqlLikeQueryBuilderGet an alias name for the given entity.- Overrides:
- getAliasNamein class- AbstractSqlLikeQueryBuilder
- Parameters:
- entity- The entity
- Returns:
- The alias name
 
- 
getTableNameDescription copied from class:AbstractSqlLikeQueryBuilderGet the table name for the given entity.- Specified by:
- getTableNamein class- AbstractSqlLikeQueryBuilder
- Parameters:
- entity- The entity
- Returns:
- The table name
 
- 
concat- Overrides:
- concatin class- AbstractSqlLikeQueryBuilder
- Parameters:
- partsWriters- The parts writers
- writer- The writer
 
- 
appendUpdateSetParameterprotected void appendUpdateSetParameter(StringBuilder sb, String alias, PersistentProperty prop, Runnable appendParameter) Description copied from class:AbstractSqlLikeQueryBuilderAppends the SET=? call to the query string.- Overrides:
- appendUpdateSetParameterin class- AbstractSqlLikeQueryBuilder
- Parameters:
- sb- The string builder
- alias- The alias
- prop- The property
- appendParameter- The append parameter action
 
- 
buildJoinprotected String[] buildJoin(String alias, JoinPath joinPath, String joinType, StringBuilder target, Map<String, String> appliedJoinPaths, AbstractSqlLikeQueryBuilder.QueryState queryState) Description copied from class:AbstractSqlLikeQueryBuilderBuild a join expression for the given alias, association, join type and builder.- Specified by:
- buildJoinin 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
 
- 
buildJoinprotected 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
 
- 
quoteQuote a column name for the dialect.- Overrides:
- quotein class- AbstractSqlLikeQueryBuilder
- Parameters:
- persistedName- The persisted name.
- Returns:
- The quoted name
 
- 
getColumnNameDescription copied from class:AbstractSqlLikeQueryBuilderGet the column name for the given property.- Specified by:
- getColumnNamein class- AbstractSqlLikeQueryBuilder
- Parameters:
- persistentProperty- The property
- Returns:
- The column name
 
- 
appendProjectionRowCountDescription copied from class:AbstractSqlLikeQueryBuilderAppends a row count projection to the query string.- Specified by:
- appendProjectionRowCountin class- AbstractSqlLikeQueryBuilder
- Parameters:
- queryString- The query string
- logicalName- The alias to the table name
 
- 
appendForUpdateprotected void appendForUpdate(AbstractSqlLikeQueryBuilder.QueryPosition queryPosition, QueryModel query, StringBuilder queryBuilder) Description copied from class:AbstractSqlLikeQueryBuilderAdds "forUpdate" pisimmistic locking.- Overrides:
- appendForUpdatein class- AbstractSqlLikeQueryBuilder
- Parameters:
- queryPosition- The query position
- query- The query
- queryBuilder- The builder
 
- 
computePropertyPathsprotected boolean computePropertyPaths()Description copied from class:AbstractSqlLikeQueryBuilderWhether 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:
- computePropertyPathsin class- AbstractSqlLikeQueryBuilder
- Returns:
- True if property path computation is required.
 
- 
isAliasForBatchprotected boolean isAliasForBatch(PersistentEntity persistentEntity, io.micronaut.core.annotation.AnnotationMetadata annotationMetadata) Description copied from class:AbstractSqlLikeQueryBuilderShould aliases be used in batch statements.- Specified by:
- isAliasForBatchin class- AbstractSqlLikeQueryBuilder
- Parameters:
- persistentEntity- the persistent entity
- annotationMetadata- the method annotation metadata
- Returns:
- True if they should
 
- 
formatParameterDescription copied from class:AbstractSqlLikeQueryBuilderFormat the parameter at the given index.- Specified by:
- formatParameterin class- AbstractSqlLikeQueryBuilder
- Parameters:
- index- The parameter
- Returns:
- The index
 
- 
selectAutoStrategySelects the default fallback strategy. For a generated value.- Parameters:
- property- The Persistent property
- Returns:
- The generated value
 
- 
supportsForUpdatepublic boolean supportsForUpdate()Description copied from interface:QueryBuilderWhether FOR UPDATE queries are supported.- Specified by:
- supportsForUpdatein interface- QueryBuilder
- Returns:
- True if FOR UPDATE queries are supported
 
- 
dialect
- 
positionalParameterFormat
- 
positionalParameterName
- 
positionalParameterPattern- Returns:
- The regex pattern for positional parameters.
 
- 
escapeQueriespublic boolean escapeQueries()
- 
annotationType- Specified by:
- annotationTypein interface- Annotation
 
 
-