Class FluentSelect<T, S extends FluentSelect<T,S>>

java.lang.Object
org.apache.cayenne.query.FluentSelect<T,S>
All Implemented Interfaces:
Query, Select<T>
Direct Known Subclasses:
ColumnSelect, ObjectSelect

public abstract class FluentSelect<T, S extends FluentSelect<T,S>> extends Object implements Select<T>
Base class for ObjectSelect and ColumnSelect
Since:
4.0
  • Field Details

    • entityType

      protected Class<?> entityType
    • entityName

      protected String entityName
    • dbEntityName

      protected String dbEntityName
    • where

      protected Expression where
    • having

      protected Expression having
    • havingExpressionIsActive

      protected boolean havingExpressionIsActive
    • orderings

      protected Collection<Ordering> orderings
    • distinct

      protected boolean distinct
    • metaData

      protected org.apache.cayenne.query.ObjectSelectMetadata metaData
  • Constructor Details

    • FluentSelect

      protected FluentSelect()
  • Method Details

    • createMetadata

      protected abstract org.apache.cayenne.query.ObjectSelectMetadata createMetadata()
    • getBaseMetaData

      protected abstract org.apache.cayenne.query.ObjectSelectMetadata getBaseMetaData()
    • resolveRoot

      protected Object resolveRoot(EntityResolver resolver)
    • entityType

      public S entityType(Class<?> entityType)
      Sets the type of the entity to fetch without changing the return type of the query.
      Returns:
      this object
    • entityName

      public S entityName(String entityName)
      Sets the ObjEntity name to fetch without changing the return type of the query. This form is most often used for generic entities that don't map to a distinct class.
      Returns:
      this object
    • dbEntityName

      public S dbEntityName(String dbEntityName)
      Sets the DbEntity name to fetch without changing the return type of the query. This form is most often used for generic entities that don't map to a distinct class.
      Returns:
      this object
    • where

      public S where(Expression expression)
      Appends a qualifier expression of this query. An equivalent to and(Expression...) that can be used a syntactic sugar.
      Returns:
      this object
    • where

      public S where(String expressionString, Object... parameters)
      Appends a qualifier expression of this query, using provided expression String and an array of position parameters. This is an equivalent to calling "and".
      Returns:
      this object
    • byId

      public S byId(Object id)
      Appends a qualifier matching a single object of the root entity by its id. The id can be one of:
      • a scalar, for entities with a single-column PK, e.g. byId(1);
      • a Map of PK column names to values, e.g. byId(Map.of("KEY1", "x", "KEY2", "y")), which is the form to use for a compound PK;
      • an ObjectId of the root entity, matched by its id snapshot.
      This is a shorthand for where(Expression) with one of the "id" expressions of the root entity "self" property, so it combines with any other query settings. Like where(..), it is AND'ed to any qualifier set previously.
      Parameters:
      id - a scalar, a Map or an ObjectId
      Since:
      5.0
    • byIds

      public S byIds(Object... ids)
      Appends a qualifier matching any of the objects of the root entity with the given ids. Each id can be a scalar, a Map of PK column names to values or an ObjectId, and the forms can be mixed. Scalars are combined into a single IN, the rest into an OR of per-id matches. An empty array matches nothing.
      Parameters:
      ids - scalars, Maps or ObjectIds
      Returns:
      this object
      Since:
      5.0
    • byIds

      public S byIds(Collection<?> ids)
      Appends a qualifier matching any of the objects of the root entity with the given ids. Each id can be a scalar, a Map of PK column names to values or an ObjectId, and the forms can be mixed. Scalars are combined into a single IN, the rest into an OR of per-id matches. An empty collection matches nothing.
      Parameters:
      ids - scalars, Maps or ObjectIds
      Returns:
      this object
      Since:
      5.0
    • and

      public S and(Expression... expressions)
      AND's provided expressions to the existing WHERE clause expression.
      Returns:
      this object
    • and

      public S and(Collection<Expression> expressions)
      AND's provided expressions to the existing WHERE clause expression.
      Returns:
      this object
    • joinExpression

      protected S joinExpression(Collection<Expression> expressions, Function<Collection<Expression>, Expression> joiner)
    • or

      public S or(Expression... expressions)
      OR's provided expressions to the existing WHERE clause expression.
      Returns:
      this object
    • or

      public S or(Collection<Expression> expressions)
      OR's provided expressions to the existing WHERE clause expression.
      Returns:
      this object
    • orderBy

      public S orderBy(String property)
      Add an ascending ordering on the given property. If there is already an ordering on this query then add this ordering with a lower priority.
      Parameters:
      property - the property to sort on
      Returns:
      this object
    • orderBy

      public S orderBy(String property, SortOrder sortOrder)
      Add an ordering on the given property. If there is already an ordering on this query then add this ordering with a lower priority.
      Parameters:
      property - the property to sort on
      sortOrder - the direction of the ordering
      Returns:
      this object
    • orderBy

      public S orderBy(Ordering... orderings)
      Add one or more orderings to this query.
      Returns:
      this object
    • orderBy

      public S orderBy(Collection<Ordering> orderings)
      Adds a list of orderings to this query.
      Returns:
      this object
    • prefetch

      public S prefetch(PrefetchTreeNode prefetch)
      Merges prefetch into the query prefetch tree.
      Returns:
      this object
    • prefetch

      public S prefetch(String path, int semantics)
      Merges a prefetch path with specified semantics into the query prefetch tree.
      Returns:
      this object
    • limit

      public S limit(int fetchLimit)
      Resets query fetch limit - a parameter that defines max number of objects that should be ever be fetched from the database.
    • offset

      public S offset(int fetchOffset)
      Resets query fetch offset - a parameter that defines how many objects should be skipped when reading data from the database.
    • pageSize

      public S pageSize(int pageSize)
      Resets query page size. A non-negative page size enables query result pagination that saves memory and processing time for large lists if only parts of the result are ever going to be accessed.
    • statementFetchSize

      public S statementFetchSize(int size)
      Sets fetch size of the PreparedStatement generated for this query. Any non-zero value is passed to the driver as is; 0 (the default) leaves the fetch size to the adapter and the driver. A negative value is only meaningful for drivers that give it a special meaning, such as Integer.MIN_VALUE for streaming on MySQL.
      See Also:
    • queryTimeout

      public S queryTimeout(int timeout)
      Sets query timeout of PreparedStatement generated for this query.
      See Also:
    • cacheStrategy

      public S cacheStrategy(QueryCacheStrategy strategy)
    • cacheStrategy

      public S cacheStrategy(QueryCacheStrategy strategy, String cacheGroup)
    • cacheGroup

      public S cacheGroup(String cacheGroup)
    • localCache

      public S localCache(String cacheGroup)
      Instructs Cayenne to look for query results in the "local" cache when running the query. This is a short-hand notation for:

      query.cacheStrategy(QueryCacheStrategy.LOCAL_CACHE, cacheGroup);
      
    • localCache

      public S localCache()
      Instructs Cayenne to look for query results in the "local" cache when running the query. This is a short-hand notation for:

      query.cacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
      
    • sharedCache

      public S sharedCache(String cacheGroup)
      Instructs Cayenne to look for query results in the "shared" cache when running the query. This is a short-hand notation for:

      query.cacheStrategy(QueryCacheStrategy.SHARED_CACHE, cacheGroup);
      
    • sharedCache

      public S sharedCache()
      Instructs Cayenne to look for query results in the "shared" cache when running the query. This is a short-hand notation for:

      query.cacheStrategy(QueryCacheStrategy.SHARED_CACHE);
      
    • getCacheStrategy

      public QueryCacheStrategy getCacheStrategy()
      Since:
      3.0
    • getCacheGroup

      public String getCacheGroup()
      Since:
      4.0
    • setCacheStrategy

      @Deprecated(since="5.0", forRemoval=true) public void setCacheStrategy(QueryCacheStrategy strategy)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Note that unlike cacheStrategy(QueryCacheStrategy) this method preserves the existing cache group. To reset it, use cacheStrategy(QueryCacheStrategy, String).
      Since:
      3.0
    • setCacheGroup

      @Deprecated(since="5.0", forRemoval=true) public void setCacheGroup(String cacheGroup)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Since:
      4.0
    • useLocalCache

      @Deprecated(since="5.0", forRemoval=true) public void useLocalCache()
      Deprecated, for removal: This API element is subject to removal in a future version.
      Since:
      4.0
    • useLocalCache

      @Deprecated(since="5.0", forRemoval=true) public void useLocalCache(String cacheGroup)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Since:
      4.0
    • useSharedCache

      @Deprecated(since="5.0", forRemoval=true) public void useSharedCache()
      Deprecated, for removal: This API element is subject to removal in a future version.
      Since:
      4.0
    • useSharedCache

      @Deprecated(since="5.0", forRemoval=true) public void useSharedCache(String cacheGroup)
      Deprecated, for removal: This API element is subject to removal in a future version.
      Since:
      4.0
    • getStatementFetchSize

      public int getStatementFetchSize()
    • getQueryTimeout

      public int getQueryTimeout()
      Since:
      4.2
    • getPageSize

      public int getPageSize()
    • getLimit

      public int getLimit()
    • getOffset

      public int getOffset()
    • getEntityType

      public Class<?> getEntityType()
    • getEntityName

      public String getEntityName()
    • getDbEntityName

      public String getDbEntityName()
    • getWhere

      public Expression getWhere()
      Returns a WHERE clause Expression of this query.
    • getHaving

      public Expression getHaving()
      Returns a HAVING clause Expression of this query.
    • getOrderings

      public Collection<Ordering> getOrderings()
    • getPrefetches

      public PrefetchTreeNode getPrefetches()
    • setActiveExpression

      protected void setActiveExpression(Expression exp)
    • getActiveExpression

      protected Expression getActiveExpression()
    • createSQLAction

      public SQLAction createSQLAction(SQLActionVisitor visitor)
      Description copied from interface: Query
      A callback method invoked by Cayenne during the final execution phase of the query run. A concrete query implementation is given a chance to decide how it should be handled. Implementors can pick an appropriate method of the SQLActionVisitor to handle itself, create a custom SQLAction of its own, or substitute itself with another query that should be used for SQLAction construction.
      Specified by:
      createSQLAction in interface Query
    • route

      public void route(QueryRouter router, EntityResolver resolver, Query substitutedQuery)
      Description copied from interface: Query
      A callback method invoked by Cayenne during the routing phase of the query execution. Mapping of DataNodes is provided by QueryRouter. Query should use a QueryRouter.route(DataNode, Query, Query) callback method to route itself. Query can create one or more substitute queries or even provide its own DataNode to execute itself.

      The default implementation relies on the EntityResolver to find the DataMap based on the query metadata. This mechanism is sufficient for most queries that "know" their root.

      Specified by:
      route in interface Query
    • isFetchingDataRows

      public boolean isFetchingDataRows()
    • routePrefetches

      protected void routePrefetches(QueryRouter router, EntityResolver resolver)
    • getColumns

      public Collection<Property<?>> getColumns()
      Since:
      4.2
    • isDistinct

      public boolean isDistinct()
      Since:
      4.2
    • castSelf

      protected <E> E castSelf()
      Utility method to perform (re)cast this type, doesn't perform any checks, so use with caution.
      Type Parameters:
      E - type to cast to
      Returns:
      this casted to the type E
      Since:
      5.0