Class SqlQueryBuilder
- java.lang.Object
-
- io.micronaut.data.model.query.builder.AbstractSqlLikeQueryBuilder
-
- io.micronaut.data.model.query.builder.sql.SqlQueryBuilder
-
- All Implemented Interfaces:
QueryBuilder
,java.lang.annotation.Annotation
- Direct Known Subclasses:
CosmosSqlQueryBuilder
public class SqlQueryBuilder extends AbstractSqlLikeQueryBuilder implements QueryBuilder, SqlQueryConfiguration.DialectConfiguration
Implementation ofQueryBuilder
that builds SQL queries.- Since:
- 1.0.0
- Author:
- graemerocher
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class io.micronaut.data.model.query.builder.AbstractSqlLikeQueryBuilder
AbstractSqlLikeQueryBuilder.CriteriaContext, AbstractSqlLikeQueryBuilder.CriterionHandler<T extends QueryModel.Criterion>, AbstractSqlLikeQueryBuilder.Placeholder, AbstractSqlLikeQueryBuilder.QueryPosition, AbstractSqlLikeQueryBuilder.QueryPropertyPath, AbstractSqlLikeQueryBuilder.QueryState
-
-
Field Summary
Fields Modifier and Type Field Description static java.lang.String
DEFAULT_POSITIONAL_PARAMETER_MARKER
The start of an IN expression.static java.lang.String
SQL_SERVER_FOR_UPDATE_CLAUSE
static java.lang.String
STANDARD_FOR_UPDATE_CLAUSE
-
Fields inherited from class io.micronaut.data.model.query.builder.AbstractSqlLikeQueryBuilder
ALIAS_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, 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_CLAUSE
-
Fields inherited from interface io.micronaut.data.model.query.builder.QueryBuilder
IN_VARIABLES_PATTERN, VARIABLE_PATTERN
-
-
Constructor Summary
Constructors Constructor Description SqlQueryBuilder()
Default constructor.SqlQueryBuilder(io.micronaut.core.annotation.AnnotationMetadata annotationMetadata)
Constructor with annotation metadata.SqlQueryBuilder(Dialect dialect)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Class<? extends java.lang.annotation.Annotation>
annotationType()
protected void
appendForUpdate(AbstractSqlLikeQueryBuilder.QueryPosition queryPosition, QueryModel query, java.lang.StringBuilder queryBuilder)
Adds "forUpdate" pisimmistic locking.protected void
appendProjectionRowCount(java.lang.StringBuilder queryString, java.lang.String logicalName)
Appends a row count projection to the query string.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.protected java.lang.String
asLiteral(java.lang.Object value)
Convert the literal value to it's SQL representation.java.lang.String
buildBatchCreateTableStatement(PersistentEntity... entities)
Builds a batch create tables statement.java.lang.String
buildBatchDropTableStatement(PersistentEntity... entities)
Builds a batch drop tables statement.java.lang.String[]
buildCreateTableStatements(PersistentEntity entity)
Builds the create table statement.java.lang.String[]
buildDropTableStatements(PersistentEntity entity)
Builds the drop table statement.QueryResult
buildInsert(io.micronaut.core.annotation.AnnotationMetadata repositoryMetadata, PersistentEntity entity)
Builds an insert statement for the given entity.protected java.lang.String[]
buildJoin(java.lang.String alias, JoinPath joinPath, java.lang.String joinType, java.lang.StringBuilder target, 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.protected void
buildJoin(java.lang.String joinType, java.lang.StringBuilder sb, AbstractSqlLikeQueryBuilder.QueryState queryState, java.util.List<Association> joinAssociationsPath, java.lang.String joinAlias, Association association, PersistentEntity associatedEntity, PersistentEntity associationOwner, java.lang.String currentJoinAlias)
Builds join, adding fields and criteria.java.lang.String
buildJoinTableInsert(PersistentEntity entity, Association association)
Builds a join table insert statement for a given entity and association.QueryResult
buildPagination(Pageable pageable)
Encode the pageable.protected boolean
computePropertyPaths()
Whether property path expressions require computation by the implementation.protected void
concat(java.lang.StringBuilder writer, java.util.Collection<java.lang.Runnable> partsWriters)
Dialect
dialect()
boolean
escapeQueries()
AbstractSqlLikeQueryBuilder.Placeholder
formatParameter(int index)
Format the parameter at the given index.protected java.lang.String
getAliasName(PersistentEntity entity)
Get an alias name for the given entity.java.lang.String
getColumnName(PersistentProperty persistentProperty)
Get the column name for the given property.Dialect
getDialect()
Get dialect.protected java.lang.String
getTableAsKeyword()
Get the AS keyword to use for table aliases.java.lang.String
getTableName(PersistentEntity entity)
Get the table name for the given entity.protected boolean
isAliasForBatch()
Should aliases be used in batch statements.protected boolean
isExpandEmbedded()
Should embedded queries by expanded by the implementation.static boolean
isForeignKeyWithJoinTable(Association association)
Is the given association a foreign key reference that requires a join table.java.lang.String
positionalParameterFormat()
java.lang.String
positionalParameterName()
java.util.regex.Pattern
positionalParameterPattern()
protected java.lang.String
quote(java.lang.String persistedName)
Quote a column name for the dialect.java.lang.String
resolveJoinType(Join.Type jt)
Resolves the join type.void
selectAllColumns(PersistentEntity entity, java.lang.String alias, java.lang.StringBuilder sb)
Selects all columns for the given entity and alias.protected void
selectAllColumns(AbstractSqlLikeQueryBuilder.QueryState queryState, java.lang.StringBuilder queryBuffer)
Obtain the string that selects all columns from the entity.protected void
selectAllColumnsFromJoinPaths(AbstractSqlLikeQueryBuilder.QueryState queryState, java.lang.StringBuilder queryBuffer, java.util.Collection<JoinPath> allPaths, java.util.Map<JoinPath,java.lang.String> joinAliasOverride)
protected GeneratedValue.Type
selectAutoStrategy(PersistentProperty property)
Selects the default fallback strategy.boolean
shouldAliasProjections()
Whether projections should be aliased.protected boolean
shouldEscape(PersistentEntity entity)
Whether queries should be escaped for the given entity.boolean
supportsForUpdate()
Whether FOR UPDATE queries are supported.-
Methods inherited from class io.micronaut.data.model.query.builder.AbstractSqlLikeQueryBuilder
addCriterionHandler, appendAssociationProjection, appendDeleteClause, appendOrder, appendPropertyRef, appendTransformed, asLiterals, asPath, buildDelete, buildOrderBy, buildOrderBy, buildQuery, buildSelect, buildUpdate, buildUpdate, buildWhereClause, getAliasName, getColumnAlias, getDataTransformerReadValue, getDataTransformerWriteValue, getMappedName, getMappedName, getMappedName, getNamingStrategy, getNamingStrategy, getPathOnlyAliasName, getUnescapedTableName, handleSubQuery, newBindingContext, traversePersistentProperties, traversePersistentProperties, traversePersistentProperties, traversePersistentProperties
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface io.micronaut.data.model.query.builder.QueryBuilder
buildDelete, buildDelete, buildOrderBy, buildQuery, buildQuery, buildUpdate, buildUpdate, buildUpdate, buildUpdate
-
-
-
-
Field Detail
-
DEFAULT_POSITIONAL_PARAMETER_MARKER
public static final java.lang.String DEFAULT_POSITIONAL_PARAMETER_MARKER
The start of an IN expression.- See Also:
- Constant Field Values
-
STANDARD_FOR_UPDATE_CLAUSE
public static final java.lang.String STANDARD_FOR_UPDATE_CLAUSE
- See Also:
- Constant Field Values
-
SQL_SERVER_FOR_UPDATE_CLAUSE
public static final java.lang.String SQL_SERVER_FOR_UPDATE_CLAUSE
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
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 Detail
-
getDialect
public Dialect getDialect()
Description copied from class:AbstractSqlLikeQueryBuilder
Get dialect.- Overrides:
getDialect
in classAbstractSqlLikeQueryBuilder
- Returns:
- The dialect being used by the builder.
-
shouldEscape
protected boolean shouldEscape(@NonNull PersistentEntity entity)
Description copied from class:AbstractSqlLikeQueryBuilder
Whether queries should be escaped for the given entity.- Overrides:
shouldEscape
in classAbstractSqlLikeQueryBuilder
- Parameters:
entity
- The entity- Returns:
- True if they should be escaped
-
asLiteral
protected java.lang.String asLiteral(java.lang.Object value)
Description copied from class:AbstractSqlLikeQueryBuilder
Convert the literal value to it's SQL representation.- Overrides:
asLiteral
in classAbstractSqlLikeQueryBuilder
- 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 interfaceQueryBuilder
- 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 classAbstractSqlLikeQueryBuilder
- Returns:
- True if they should
-
buildBatchCreateTableStatement
@NonNull public java.lang.String buildBatchCreateTableStatement(@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 java.lang.String buildBatchDropTableStatement(@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 java.lang.String[] buildDropTableStatements(@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 java.lang.String buildJoinTableInsert(@NonNull PersistentEntity entity, @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
public static boolean isForeignKeyWithJoinTable(@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 java.lang.String[] buildCreateTableStatements(@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
-
getTableAsKeyword
protected java.lang.String getTableAsKeyword()
Description copied from class:AbstractSqlLikeQueryBuilder
Get the AS keyword to use for table aliases.- Overrides:
getTableAsKeyword
in classAbstractSqlLikeQueryBuilder
- Returns:
- The AS keyword if any
-
selectAllColumns
protected void selectAllColumns(AbstractSqlLikeQueryBuilder.QueryState queryState, java.lang.StringBuilder queryBuffer)
Description copied from class:AbstractSqlLikeQueryBuilder
Obtain the string that selects all columns from the entity.- Specified by:
selectAllColumns
in classAbstractSqlLikeQueryBuilder
- Parameters:
queryState
- The query state
-
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)
- Overrides:
selectAllColumnsFromJoinPaths
in classAbstractSqlLikeQueryBuilder
-
selectAllColumns
public void selectAllColumns(PersistentEntity entity, java.lang.String alias, java.lang.StringBuilder sb)
Selects all columns for the given entity and alias.- Specified by:
selectAllColumns
in classAbstractSqlLikeQueryBuilder
- Parameters:
entity
- The entityalias
- The aliassb
- The builder to add the columns
-
resolveJoinType
public java.lang.String resolveJoinType(Join.Type jt)
Description copied from class:AbstractSqlLikeQueryBuilder
Resolves the join type.- Specified by:
resolveJoinType
in classAbstractSqlLikeQueryBuilder
- Parameters:
jt
- The join type- Returns:
- The join type.
-
buildInsert
@NonNull public 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 interfaceQueryBuilder
- Parameters:
repositoryMetadata
- The repository annotation metadataentity
- The entity- Returns:
- The insert statement or null if the implementation doesn't require insert statements
-
buildPagination
@NonNull public QueryResult buildPagination(@NonNull Pageable pageable)
Description copied from interface:QueryBuilder
Encode the pageable.- Specified by:
buildPagination
in interfaceQueryBuilder
- Parameters:
pageable
- The pageable- Returns:
- The encoded query
-
getAliasName
protected java.lang.String getAliasName(PersistentEntity entity)
Description copied from class:AbstractSqlLikeQueryBuilder
Get an alias name for the given entity.- Overrides:
getAliasName
in classAbstractSqlLikeQueryBuilder
- Parameters:
entity
- The entity- Returns:
- The alias name
-
getTableName
public java.lang.String getTableName(PersistentEntity entity)
Description copied from class:AbstractSqlLikeQueryBuilder
Get the table name for the given entity.- Specified by:
getTableName
in classAbstractSqlLikeQueryBuilder
- Parameters:
entity
- The entity- Returns:
- The table name
-
concat
protected void concat(java.lang.StringBuilder writer, java.util.Collection<java.lang.Runnable> partsWriters)
- Overrides:
concat
in classAbstractSqlLikeQueryBuilder
- Parameters:
partsWriters
- The parts writerswriter
- The writer
-
appendUpdateSetParameter
protected void appendUpdateSetParameter(java.lang.StringBuilder sb, java.lang.String alias, PersistentProperty prop, java.lang.Runnable appendParameter)
Description copied from class:AbstractSqlLikeQueryBuilder
Appends the SET=? call to the query string.- Overrides:
appendUpdateSetParameter
in classAbstractSqlLikeQueryBuilder
- Parameters:
sb
- The string builderalias
- The aliasprop
- The propertyappendParameter
- The append parameter action
-
buildJoin
protected java.lang.String[] buildJoin(java.lang.String alias, JoinPath joinPath, java.lang.String joinType, java.lang.StringBuilder target, java.util.Map<java.lang.String,java.lang.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 classAbstractSqlLikeQueryBuilder
- Parameters:
alias
- The aliasjoinPath
- The join pathjoinType
- The join type stringtarget
- The target builderappliedJoinPaths
- The applied joins pathsqueryState
- The query state- Returns:
- An array representing the aliases for each join association in the specified join path
-
buildJoin
protected void buildJoin(java.lang.String joinType, java.lang.StringBuilder sb, AbstractSqlLikeQueryBuilder.QueryState queryState, java.util.List<Association> joinAssociationsPath, java.lang.String joinAlias, Association association, PersistentEntity associatedEntity, PersistentEntity associationOwner, java.lang.String currentJoinAlias)
Builds join, adding fields and criteria.- Parameters:
joinType
- the join typesb
- string builder that join will be added toqueryState
- the query statejoinAssociationsPath
- the list of associationsjoinAlias
- the join aliasassociation
- the associationassociatedEntity
- the associated entityassociationOwner
- the association ownercurrentJoinAlias
- the current join alias
-
quote
protected java.lang.String quote(java.lang.String persistedName)
Quote a column name for the dialect.- Overrides:
quote
in classAbstractSqlLikeQueryBuilder
- Parameters:
persistedName
- The persisted name.- Returns:
- The quoted name
-
getColumnName
public java.lang.String getColumnName(PersistentProperty persistentProperty)
Description copied from class:AbstractSqlLikeQueryBuilder
Get the column name for the given property.- Specified by:
getColumnName
in classAbstractSqlLikeQueryBuilder
- Parameters:
persistentProperty
- The property- Returns:
- The column name
-
appendProjectionRowCount
protected void appendProjectionRowCount(java.lang.StringBuilder queryString, java.lang.String logicalName)
Description copied from class:AbstractSqlLikeQueryBuilder
Appends a row count projection to the query string.- Specified by:
appendProjectionRowCount
in classAbstractSqlLikeQueryBuilder
- Parameters:
queryString
- The query stringlogicalName
- The alias to the table name
-
appendForUpdate
protected void appendForUpdate(AbstractSqlLikeQueryBuilder.QueryPosition queryPosition, QueryModel query, java.lang.StringBuilder queryBuilder)
Description copied from class:AbstractSqlLikeQueryBuilder
Adds "forUpdate" pisimmistic locking.- Overrides:
appendForUpdate
in classAbstractSqlLikeQueryBuilder
- Parameters:
queryPosition
- The query positionquery
- The queryqueryBuilder
- 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 classAbstractSqlLikeQueryBuilder
- Returns:
- True if property path computation is required.
-
isAliasForBatch
protected boolean isAliasForBatch()
Description copied from class:AbstractSqlLikeQueryBuilder
Should aliases be used in batch statements.- Specified by:
isAliasForBatch
in classAbstractSqlLikeQueryBuilder
- 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 classAbstractSqlLikeQueryBuilder
- 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 interfaceQueryBuilder
- Returns:
- True if FOR UPDATE queries are supported
-
dialect
public Dialect dialect()
-
positionalParameterFormat
public java.lang.String positionalParameterFormat()
-
positionalParameterName
public java.lang.String positionalParameterName()
-
positionalParameterPattern
public java.util.regex.Pattern positionalParameterPattern()
- Returns:
- The regex pattern for positional parameters.
-
escapeQueries
public boolean escapeQueries()
-
annotationType
public java.lang.Class<? extends java.lang.annotation.Annotation> annotationType()
- Specified by:
annotationType
in interfacejava.lang.annotation.Annotation
-
-