Class SqlQueryBuilder2
java.lang.Object
io.micronaut.data.model.query.builder.sql.AbstractSqlLikeQueryBuilder2
io.micronaut.data.model.query.builder.sql.SqlQueryBuilder2
- All Implemented Interfaces:
QueryBuilder2
- Direct Known Subclasses:
CosmosSqlQueryBuilder2
Implementation of
AbstractSqlLikeQueryBuilder2.QueryBuilder
that builds SQL queries.- Since:
- 1.0.0
- Author:
- graemerocher, Denis Stepanov
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
The insert query definition.protected class
Nested classes/interfaces inherited from class io.micronaut.data.model.query.builder.sql.AbstractSqlLikeQueryBuilder2
AbstractSqlLikeQueryBuilder2.Placeholder, AbstractSqlLikeQueryBuilder2.QueryBuilder, AbstractSqlLikeQueryBuilder2.QueryPosition, AbstractSqlLikeQueryBuilder2.QueryPropertyPath, AbstractSqlLikeQueryBuilder2.QueryState, AbstractSqlLikeQueryBuilder2.SqlPredicateVisitor
Nested classes/interfaces inherited from interface io.micronaut.data.model.query.builder.QueryBuilder2
QueryBuilder2.BaseQueryDefinition, QueryBuilder2.DeleteQueryDefinition, QueryBuilder2.InsertQueryDefinition, QueryBuilder2.SelectQueryDefinition, QueryBuilder2.UpdateQueryDefinition
-
Field Summary
Modifier and TypeFieldDescriptionstatic final String
The start of an IN expression.static final String
static final String
Fields inherited from class io.micronaut.data.model.query.builder.sql.AbstractSqlLikeQueryBuilder2
ALIAS_REPLACE_QUOTED, AND, AS_CLAUSE, CANNOT_QUERY_ON_ID_WITH_ENTITY_THAT_HAS_NO_ID, CLOSE_BRACKET, COMMA, DISTINCT, DOT, FROM_CLAUSE, LOGICAL_AND, LOGICAL_OR, NOT, OPEN_BRACKET, OR, ORDER_BY_CLAUSE, RETURNING, SELECT_CLAUSE, SPACE, WHERE_CLAUSE
-
Constructor Summary
ConstructorDescriptionDefault constructor.SqlQueryBuilder2
(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata) Constructor with annotation metadata.SqlQueryBuilder2
(Dialect dialect) -
Method Summary
Modifier and TypeMethodDescriptionprotected void
appendForUpdate
(AbstractSqlLikeQueryBuilder2.QueryPosition queryPosition, QueryBuilder2.SelectQueryDefinition definition, StringBuilder queryBuilder) Adds "forUpdate" pessimistic locking.protected void
appendPaginationAndOrder
(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, QueryBuilder2.SelectQueryDefinition definition, boolean pagination, AbstractSqlLikeQueryBuilder2.QueryState queryState) protected void
appendUpdateSetParameter
(StringBuilder sb, String alias, PersistentProperty prop, Runnable appendParameter) Appends the SET=? call to the query string.protected String
Convert the literal value to it's SQL representation.@NonNull String
buildBatchCreateTableStatement
(@NonNull PersistentEntity... entities) Builds a batch create tables statement.@NonNull String
buildBatchDropTableStatement
(@NonNull PersistentEntity... entities) Builds a batch drop tables statement.@NonNull String[]
buildCreateTableStatements
(@NonNull PersistentEntity entity) Builds the creation table statement.@NonNull String[]
buildDropTableStatements
(@NonNull PersistentEntity entity) Builds the drop table statement.@NonNull QueryResult
buildInsert
(io.micronaut.core.annotation.AnnotationMetadata repositoryMetadata, QueryBuilder2.InsertQueryDefinition definition) Builds an insert statement for the given entity.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.@NonNull String
buildJoinTableInsert
(@NonNull PersistentEntity entity, @NonNull Association association) Builds a join table insert statement for a given entity and association.buildSelect
(@NonNull io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, @NonNull QueryBuilder2.SelectQueryDefinition definition) Encode the given query for the passed annotation metadata and query.protected boolean
Whether property path expressions require computation by the implementation.protected SqlQueryBuilder2.SqlSelectionVisitor
createSelectionVisitor
(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, AbstractSqlLikeQueryBuilder2.QueryState queryState, boolean distinct) Create a selection visitor.formatParameter
(int index) Format the parameter at the given index.protected String
getAliasName
(PersistentEntity entity) Get an alias name for the given entity.getColumnName
(PersistentProperty persistentProperty) Get the column name for the given property.Get dialect.protected String
Get the AS keyword to use for table aliases.getTableName
(PersistentEntity entity) Get the table name for the given entity.protected boolean
isAliasForBatch
(PersistentEntity persistentEntity, io.micronaut.core.annotation.AnnotationMetadata annotationMetadata) Should aliases be used in batch statements.static boolean
isForeignKeyWithJoinTable
(@NonNull Association association) Is the given association a foreign key reference that requires a join table.final String
protected String
Quote a column name for the dialect.Resolves the join type.protected GeneratedValue.Type
selectAutoStrategy
(PersistentProperty property) Selects the default fallback strategy.protected boolean
shouldEscape
(@NonNull PersistentEntity entity) Whether queries should be escaped for the given entity.Methods inherited from class io.micronaut.data.model.query.builder.sql.AbstractSqlLikeQueryBuilder2
appendDeleteClause, appendExpression, appendLimitAndOffset, appendOrder, appendPropertyRef, appendTransformed, asPath, asQueryPropertyPath, buildAdditionalWhereClause, buildAdditionalWhereString, buildAdditionalWhereString, buildDelete, buildLimitAndOffset, buildOrderBy, buildPropertyByName, buildQuery, buildSelect, buildUpdate, buildWhereClause, checkDialectSupportsJsonEntity, createPredicateVisitor, getAliasName, getColumnAlias, getDataTransformerReadValue, getDataTransformerWriteValue, getMappedName, getMappedName, getMappedName, getNamingStrategy, getNamingStrategy, getPathOnlyAliasName, getUnescapedTableName, isJsonEntity, newBindingContext, resolveWhereForAnnotationMetadata, traverseEmbedded
-
Field Details
-
DEFAULT_POSITIONAL_PARAMETER_MARKER
The start of an IN expression.- See Also:
-
STANDARD_FOR_UPDATE_CLAUSE
- See Also:
-
SQL_SERVER_FOR_UPDATE_CLAUSE
- See Also:
-
-
Constructor Details
-
SqlQueryBuilder2
@Creator public SqlQueryBuilder2(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata) Constructor with annotation metadata.- Parameters:
annotationMetadata
- The annotation metadata
-
SqlQueryBuilder2
public SqlQueryBuilder2()Default constructor. -
SqlQueryBuilder2
- Parameters:
dialect
- The dialect
-
-
Method Details
-
getDialect
Description copied from class:AbstractSqlLikeQueryBuilder2
Get dialect.- Overrides:
getDialect
in classAbstractSqlLikeQueryBuilder2
- Returns:
- The dialect being used by the builder.
-
shouldEscape
Description copied from class:AbstractSqlLikeQueryBuilder2
Whether queries should be escaped for the given entity.- Overrides:
shouldEscape
in classAbstractSqlLikeQueryBuilder2
- Parameters:
entity
- The entity- Returns:
- True if they should be escaped
-
asLiteral
Description copied from class:AbstractSqlLikeQueryBuilder2
Convert the literal value to it's SQL representation.- Overrides:
asLiteral
in classAbstractSqlLikeQueryBuilder2
- 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 entityassociation
- The association- Returns:
- The join table insert statement
-
isForeignKeyWithJoinTable
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
Description copied from class:AbstractSqlLikeQueryBuilder2
Get the AS keyword to use for table aliases.- Overrides:
getTableAsKeyword
in classAbstractSqlLikeQueryBuilder2
- Returns:
- The AS keyword if any
-
createSelectionVisitor
protected SqlQueryBuilder2.SqlSelectionVisitor createSelectionVisitor(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, AbstractSqlLikeQueryBuilder2.QueryState queryState, boolean distinct) Description copied from class:AbstractSqlLikeQueryBuilder2
Create a selection visitor.- Overrides:
createSelectionVisitor
in classAbstractSqlLikeQueryBuilder2
- Parameters:
annotationMetadata
- The annotation metadataqueryState
- The query statedistinct
- The distinct- Returns:
- The visitor
-
resolveJoinType
Description copied from class:AbstractSqlLikeQueryBuilder2
Resolves the join type.- Specified by:
resolveJoinType
in classAbstractSqlLikeQueryBuilder2
- Parameters:
jt
- The join type- Returns:
- The join type.
-
buildInsert
@NonNull public @NonNull QueryResult buildInsert(io.micronaut.core.annotation.AnnotationMetadata repositoryMetadata, QueryBuilder2.InsertQueryDefinition definition) Description copied from interface:QueryBuilder2
Builds an insert statement for the given entity.- Parameters:
repositoryMetadata
- The repository annotation metadatadefinition
- The definition- Returns:
- The insert statement or null if the implementation doesn't require insert statements
-
getAliasName
Description copied from class:AbstractSqlLikeQueryBuilder2
Get an alias name for the given entity.- Overrides:
getAliasName
in classAbstractSqlLikeQueryBuilder2
- Parameters:
entity
- The entity- Returns:
- The alias name
-
getTableName
Description copied from class:AbstractSqlLikeQueryBuilder2
Get the table name for the given entity.- Specified by:
getTableName
in classAbstractSqlLikeQueryBuilder2
- Parameters:
entity
- The entity- Returns:
- The table name
-
appendUpdateSetParameter
protected void appendUpdateSetParameter(StringBuilder sb, String alias, PersistentProperty prop, Runnable appendParameter) Description copied from class:AbstractSqlLikeQueryBuilder2
Appends the SET=? call to the query string.- Overrides:
appendUpdateSetParameter
in classAbstractSqlLikeQueryBuilder2
- Parameters:
sb
- The string builderalias
- The aliasprop
- The propertyappendParameter
- The append parameter action
-
buildJoin
protected void buildJoin(String joinType, StringBuilder query, AbstractSqlLikeQueryBuilder2.QueryState queryState, PersistentAssociationPath joinAssociation, PersistentEntity associationOwner, String currentJoinAlias, String lastJoinAlias) Description copied from class:AbstractSqlLikeQueryBuilder2
Build a join expression for the given alias, association, join type and builder.- Overrides:
buildJoin
in classAbstractSqlLikeQueryBuilder2
- Parameters:
joinType
- The join typequery
- The query builderqueryState
- The statejoinAssociation
- The associationassociationOwner
- The associated ownercurrentJoinAlias
- The current join aliaslastJoinAlias
- The last join alias
-
quote
Quote a column name for the dialect.- Overrides:
quote
in classAbstractSqlLikeQueryBuilder2
- Parameters:
persistedName
- The persisted name.- Returns:
- The quoted name
-
getColumnName
Description copied from class:AbstractSqlLikeQueryBuilder2
Get the column name for the given property.- Specified by:
getColumnName
in classAbstractSqlLikeQueryBuilder2
- Parameters:
persistentProperty
- The property- Returns:
- The column name
-
appendForUpdate
protected void appendForUpdate(AbstractSqlLikeQueryBuilder2.QueryPosition queryPosition, QueryBuilder2.SelectQueryDefinition definition, StringBuilder queryBuilder) Description copied from class:AbstractSqlLikeQueryBuilder2
Adds "forUpdate" pessimistic locking.- Overrides:
appendForUpdate
in classAbstractSqlLikeQueryBuilder2
- Parameters:
queryPosition
- The query positiondefinition
- The definitionqueryBuilder
- The builder
-
computePropertyPaths
protected boolean computePropertyPaths()Description copied from class:AbstractSqlLikeQueryBuilder2
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 classAbstractSqlLikeQueryBuilder2
- Returns:
- True if property path computation is required.
-
isAliasForBatch
protected boolean isAliasForBatch(PersistentEntity persistentEntity, io.micronaut.core.annotation.AnnotationMetadata annotationMetadata) Description copied from class:AbstractSqlLikeQueryBuilder2
Should aliases be used in batch statements.- Specified by:
isAliasForBatch
in classAbstractSqlLikeQueryBuilder2
- Parameters:
persistentEntity
- the persistent entityannotationMetadata
- the method annotation metadata- Returns:
- True if they should
-
formatParameter
Description copied from class:AbstractSqlLikeQueryBuilder2
Format the parameter at the given index.- Specified by:
formatParameter
in classAbstractSqlLikeQueryBuilder2
- Parameters:
index
- The parameter- Returns:
- The index
-
selectAutoStrategy
Selects the default fallback strategy. For a generated value.- Parameters:
property
- The Persistent property- Returns:
- The generated value
-
positionalParameterFormat
- Returns:
- The positional parameter format
-
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 interfaceQueryBuilder2
- Overrides:
buildSelect
in classAbstractSqlLikeQueryBuilder2
- Parameters:
annotationMetadata
- The annotation metadatadefinition
- The query model- Returns:
- The query result
-
appendPaginationAndOrder
protected void appendPaginationAndOrder(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata, QueryBuilder2.SelectQueryDefinition definition, boolean pagination, AbstractSqlLikeQueryBuilder2.QueryState queryState) - Overrides:
appendPaginationAndOrder
in classAbstractSqlLikeQueryBuilder2
-