ColumnSelect (cayenne-doc: Cayenne Documentation 5.0-M2 API)
Description: declaration: package: org.apache.cayenne.query, class: ColumnSelect

# Class ColumnSelect<T>

java.lang.Object
org.apache.cayenne.query.CacheableQuery
org.apache.cayenne.query.AbstractQuery
org.apache.cayenne.query.FluentSelect<T, ColumnSelect<T>>
org.apache.cayenne.query.ColumnSelect<T>

All Implemented Interfaces:
  `Serializable, Query, Select<T>`

public class ColumnSelect<T> extends FluentSelect<T, ColumnSelect<T>>

A helper builder for queries selecting individual properties based on the root object.

It can be used to select properties of the object itself, properties of related entities or some function calls (including aggregate functions).

Usage examples:

         // select list of names:
         List<String> names = ObjectSelect.columnQuery(Artist.class, Artist.ARTIST_NAME).select(context);

         // select count:
         long count = ObjectSelect.columnQuery(Artist.class, PropertyFactory.COUNT).selectOne();

         // select only required properties of an entity:
         List<Object[]> data = ObjectSelect.columnQuery(Artist.class, Artist.ARTIST_NAME, Artist.DATE_OF_BIRTH)
                                     .where(Artist.ARTIST_NAME.like("Picasso%))
                                     .select(context);

Note: this class can't be instantiated directly. Use `ObjectSelect`.

Since:
  4.0
See Also:
  - `ObjectSelect.columnQuery(Class, Property)`
  - Serialized Form

- ## Field Summary

  Fields
  Modifier and Type
  Field
  Description
  `protected Collection<Property<?>>`
  `columns`
  `protected boolean`
  `singleColumn`

  ### Fields inherited from class FluentSelect

  `dbEntityName, distinct, entityName, entityType, having, havingExpressionIsActive, metaData, orderings, where`
  Modifier and Type
  Field
  Description
  `protected String`
  `dbEntityName`
  `protected boolean`
  `distinct`
  `protected String`
  `entityName`
  `protected Class<?>`
  `entityType`
  `protected Expression`
  `having`
  `protected boolean`
  `havingExpressionIsActive`
  `protected org.apache.cayenne.query.ObjectSelectMetadata`
  `metaData`
  `protected Collection<Ordering>`
  `orderings`
  `protected Expression`
  `where`

  ### Fields inherited from class AbstractQuery

  `root`
  Modifier and Type
  Field
  Description
  `protected Object`
  `root`
  The root object this query.

  ### Fields inherited from class CacheableQuery

  `logger`
  Modifier and Type
  Field
  Description
  `protected static final org.slf4j.Logger`
  `logger`

- ## Constructor Summary

  Constructors
  Modifier
  Constructor
  Description
  `protected`
  `ColumnSelect()`
  `protected`
  `ColumnSelect(ObjectSelect<T> select)`
  Copy constructor to convert ObjectSelect to ColumnSelect

- ## Method Summary

  Modifier and Type
  Method
  Description
  `<E> ColumnSelect<Object[]>`
  `aggregate(BaseProperty<E> property, String function, Class<E> type)`
  Select result of some function, that aggregates values.
  `ColumnSelect<Object[]>`
  `avg(NumericProperty<?> property)`
  Select average value of property
  `protected <E> ColumnSelect<E>`
  `column(Property<E> property)`
  `ColumnSelect<Object[]>`
  `columns(Collection<Property<?>> properties)`
  Add properties to select.
  `ColumnSelect<Object[]>`
  `columns(Property<?>... properties)`
  Add properties to select.
  `ColumnSelect<Object[]>`
  `count()`
  Shortcut for `columns(Property[])` columns}(Property.COUNT)
  `ColumnSelect<Object[]>`
  `count(BaseProperty<?> property)`
  Select COUNT(property)
  `protected org.apache.cayenne.query.ColumnSelectMetadata`
  `createMetadata()`
  `ColumnSelect<T>`
  `distinct()`
  Explicitly request distinct in query.
  `protected org.apache.cayenne.query.ColumnSelectMetadata`
  `getBaseMetaData()`
  `Collection<Property<?>>`
  `getColumns()`
  `QueryMetadata`
  `getMetaData(EntityResolver resolver)`
  Returns default select parameters.
  `ColumnSelect<T>`
  `having(String expressionString, Object... parameters)`
  Appends a having qualifier expression of this query, using provided expression String and an array of position parameters.
  `ColumnSelect<T>`
  `having(Expression expression)`
  Appends a having qualifier expression of this query.
  `<E> ColumnSelect<E>`
  `map(Function<T,E> mapper)`
  Maps result of this query by processing with a given function.
  `ColumnSelect<Object[]>`
  `max(ComparableProperty<?> property)`
  Select maximum value of property
  `ColumnSelect<Object[]>`
  `min(ComparableProperty<?> property)`
  Select minimum value of property
  `T`
  `selectFirst(ObjectContext context)`
  Selects a single object using provided context.
  `<E extends Number>
  ColumnSelect<Object[]>`
  `sum(NumericProperty<E> property)`
  Select sum of values
  `ColumnSelect<T>`
  `suppressDistinct()`
  Explicitly suppress distinct in query.

  ### Methods inherited from class FluentSelect

  `and, and, batchIterator, cacheGroup, cacheStrategy, cacheStrategy, castSelf, createSQLAction, dbEntityName, entityName, entityType, getActiveExpression, getDbEntityName, getEntityName, getEntityType, getHaving, getLimit, getOffset, getOrderings, getPageSize, getPrefetches, getQueryTimeout, getStatementFetchSize, getWhere, initWithProperties, isDistinct, isFetchingDataRows, iterate, iterator, joinExpression, limit, localCache, localCache, offset, or, or, orderBy, orderBy, orderBy, orderBy, pageSize, prefetch, prefetch, queryTimeout, resolveRoot, route, routePrefetches, select, selectOne, setActiveExpression, sharedCache, sharedCache, statementFetchSize, where, where`
  Modifier and Type
  Method
  Description
  `ColumnSelect<T>`
  `and(Collection<Expression> expressions)`
  AND's provided expressions to the existing WHERE clause expression.
  `ColumnSelect<T>`
  `and(Expression... expressions)`
  AND's provided expressions to the existing WHERE clause expression.
  `ResultBatchIterator<T>`
  `batchIterator(ObjectContext context, int size)`
  Creates a ResultBatchIterator based on the provided context and batch size.
  `ColumnSelect<T>`
  `cacheGroup(String cacheGroup)`
  `ColumnSelect<T>`
  `cacheStrategy(QueryCacheStrategy strategy)`
  `ColumnSelect<T>`
  `cacheStrategy(QueryCacheStrategy strategy, String cacheGroup)`
  `protected <E> E`
  `castSelf()`
  Utility method to perform (re)cast this type, doesn't perform any checks, so use with caution.
  `SQLAction`
  `createSQLAction(SQLActionVisitor visitor)`
  A callback method invoked by Cayenne during the final execution phase of the query run.
  `ColumnSelect<T>`
  `dbEntityName(String dbEntityName)`
  Sets the `DbEntity` name to fetch without changing the return type of the query.
  `ColumnSelect<T>`
  `entityName(String entityName)`
  Sets the `ObjEntity` name to fetch without changing the return type of the query.
  `ColumnSelect<T>`
  `entityType(Class<?> entityType)`
  Sets the type of the entity to fetch without changing the return type of the query.
  `protected Expression`
  `getActiveExpression()`
  `String`
  `getDbEntityName()`
  `String`
  `getEntityName()`
  `Class<?>`
  `getEntityType()`
  `Expression`
  `getHaving()`
  Returns a HAVING clause Expression of this query.
  `int`
  `getLimit()`
  `int`
  `getOffset()`
  `Collection<Ordering>`
  `getOrderings()`
  `int`
  `getPageSize()`
  `PrefetchTreeNode`
  `getPrefetches()`
  `int`
  `getQueryTimeout()`
  `int`
  `getStatementFetchSize()`
  `Expression`
  `getWhere()`
  Returns a WHERE clause Expression of this query.
  `void`
  `initWithProperties(Map<String,String> properties)`
  `boolean`
  `isDistinct()`
  `boolean`
  `isFetchingDataRows()`
  `void`
  `iterate(ObjectContext context, ResultIteratorCallback<T> callback)`
  Creates a ResultIterator based on the provided context and passes it to a callback for processing.
  `ResultIterator<T>`
  `iterator(ObjectContext context)`
  Creates a ResultIterator based on the provided context.
  `protected ColumnSelect<T>`
  `joinExpression(Collection<Expression> expressions, Function<Collection<Expression>, Expression> joiner)`
  `ColumnSelect<T>`
  `limit(int fetchLimit)`
  Resets query fetch limit - a parameter that defines max number of objects that should be ever be fetched from the database.
  `ColumnSelect<T>`
  `localCache()`
  Instructs Cayenne to look for query results in the "local" cache when running the query.
  `ColumnSelect<T>`
  `localCache(String cacheGroup)`
  Instructs Cayenne to look for query results in the "local" cache when running the query.
  `ColumnSelect<T>`
  `offset(int fetchOffset)`
  Resets query fetch offset - a parameter that defines how many objects should be skipped when reading data from the database.
  `ColumnSelect<T>`
  `or(Collection<Expression> expressions)`
  OR's provided expressions to the existing WHERE clause expression.
  `ColumnSelect<T>`
  `or(Expression... expressions)`
  OR's provided expressions to the existing WHERE clause expression.
  `ColumnSelect<T>`
  `orderBy(String property)`
  Add an ascending ordering on the given property.
  `ColumnSelect<T>`
  `orderBy(String property, SortOrder sortOrder)`
  Add an ordering on the given property.
  `ColumnSelect<T>`
  `orderBy(Collection<Ordering> orderings)`
  Adds a list of orderings to this query.
  `ColumnSelect<T>`
  `orderBy(Ordering... orderings)`
  Add one or more orderings to this query.
  `ColumnSelect<T>`
  `pageSize(int pageSize)`
  Resets query page size.
  `ColumnSelect<T>`
  `prefetch(String path, int semantics)`
  Merges a prefetch path with specified semantics into the query prefetch tree.
  `ColumnSelect<T>`
  `prefetch(PrefetchTreeNode prefetch)`
  Merges prefetch into the query prefetch tree.
  `ColumnSelect<T>`
  `queryTimeout(int timeout)`
  Sets query timeout of PreparedStatement generated for this query.
  `protected Object`
  `resolveRoot(EntityResolver resolver)`
  `void`
  `route(QueryRouter router, EntityResolver resolver, Query substitutedQuery)`
  Implements default routing mechanism relying on the EntityResolver to find DataMap based on the query root.
  `protected void`
  `routePrefetches(QueryRouter router, EntityResolver resolver)`
  `List<T>`
  `select(ObjectContext context)`
  Selects objects using provided context.
  `T`
  `selectOne(ObjectContext context)`
  Selects a single object using provided context.
  `protected void`
  `setActiveExpression(Expression exp)`
  `ColumnSelect<T>`
  `sharedCache()`
  Instructs Cayenne to look for query results in the "shared" cache when running the query.
  `ColumnSelect<T>`
  `sharedCache(String cacheGroup)`
  Instructs Cayenne to look for query results in the "shared" cache when running the query.
  `ColumnSelect<T>`
  `statementFetchSize(int size)`
  Sets fetch size of the PreparedStatement generated for this query.
  `ColumnSelect<T>`
  `where(String expressionString, Object... parameters)`
  Appends a qualifier expression of this query, using provided expression String and an array of position parameters.
  `ColumnSelect<T>`
  `where(Expression expression)`
  Appends a qualifier expression of this query.

  ### Methods inherited from class AbstractQuery

  `getRoot, setRoot, toString`
  Modifier and Type
  Method
  Description
  `Object`
  `getRoot()`
  Returns the root of this query.
  `void`
  `setRoot(Object value)`
  Sets the root of the query
  `String`
  `toString()`

  ### Methods inherited from class CacheableQuery

  `getCacheGroup, getCacheStrategy, setCacheGroup, setCacheStrategy, useLocalCache, useLocalCache, useSharedCache, useSharedCache`
  Modifier and Type
  Method
  Description
  `String`
  `getCacheGroup()`
  `QueryCacheStrategy`
  `getCacheStrategy()`
  `void`
  `setCacheGroup(String cacheGroup)`
  `void`
  `setCacheStrategy(QueryCacheStrategy strategy)`
  `void`
  `useLocalCache()`
  Instructs Cayenne to look for query results in the "local" cache when running the query.
  `void`
  `useLocalCache(String cacheGroup)`
  Instructs Cayenne to look for query results in the "local" cache when running the query.
  `void`
  `useSharedCache()`
  Instructs Cayenne to look for query results in the "shared" cache when running the query.
  `void`
  `useSharedCache(String cacheGroup)`
  Instructs Cayenne to look for query results in the "shared" cache when running the query.

  ### Methods inherited from class Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- ## Field Details

  - ### columns

    protected Collection<Property<?>> columns

  - ### singleColumn

    protected boolean singleColumn

- ## Constructor Details

  - ### ColumnSelect

    protected ColumnSelect()

  - ### ColumnSelect

    protected ColumnSelect(ObjectSelect<T> select)
    Copy constructor to convert ObjectSelect to ColumnSelect

- ## Method Details

  - ### createMetadata

    protected org.apache.cayenne.query.ColumnSelectMetadata createMetadata()
    Specified by:
      `createMetadata` in class `FluentSelect<T, ColumnSelect<T>>`

  - ### columns

    public ColumnSelect<Object[]> columns(Property<?>... properties)

    Add properties to select.

    Can be any properties that can be resolved against root entity type (root entity properties, function call expressions, properties of relationships, etc).

        List<Object[]> columns = ObjectSelect.columnQuery(Artist.class, Artist.ARTIST_NAME)
                                           .columns(Artist.ARTIST_SALARY, Artist.DATE_OF_BIRTH)
                                           .select(context);

    Parameters:
      `properties` - array of properties to select
    See Also:
      - `column(Property)`
      - `columns(Collection)`

  - ### columns

    public ColumnSelect<Object[]> columns(Collection<Property<?>> properties)

    Add properties to select.

    Can be any properties that can be resolved against root entity type (root entity properties, function call expressions, properties of relationships, etc).

    Parameters:
      `properties` - collection of properties, must contain at least one element
    See Also:
      - `columns(Property[])`

  - ### column

    protected <E> ColumnSelect<E> column(Property<E> property)

  - ### count

    public ColumnSelect<Object[]> count()

    Shortcut for `columns(Property[])` columns}(Property.COUNT)

  - ### count

    public ColumnSelect<Object[]> count(BaseProperty<?> property)

    Select COUNT(property)

    Can return different result than COUNT(*) as it will count only non null values

    See Also:
      - `count()`

  - ### min

    public ColumnSelect<Object[]> min(ComparableProperty<?> property)

    Select minimum value of property

    See Also:
      - `columns(Property[])`

  - ### max

    public ColumnSelect<Object[]> max(ComparableProperty<?> property)

    Select maximum value of property

    See Also:
      - `columns(Property[])`

  - ### avg

    public ColumnSelect<Object[]> avg(NumericProperty<?> property)

    Select average value of property

    See Also:
      - `columns(Property[])`

  - ### sum

    public <E extends Number> ColumnSelect<Object[]> sum(NumericProperty<E> property)

    Select sum of values

    See Also:
      - `columns(Property[])`

  - ### aggregate

    public <E> ColumnSelect<Object[]> aggregate(BaseProperty<E> property, String function, Class<E> type)

    Select result of some function, that aggregates values.

    Since:
      5.0
    See Also:
      - `columns(Property[])`

  - ### having

    public ColumnSelect<T> having(Expression expression)
    Appends a having qualifier expression of this query. An equivalent to `FluentSelect.and(Expression...)` that can be used a syntactic sugar.
    Returns:
      this object

  - ### having

    public ColumnSelect<T> having(String expressionString, Object... parameters)
    Appends a having 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

  - ### distinct

    public ColumnSelect<T> distinct()
    Explicitly request distinct in query.

  - ### suppressDistinct

    public ColumnSelect<T> suppressDistinct()
    Explicitly suppress distinct in query.

  - ### getColumns

    public Collection<Property<?>> getColumns()
    Overrides:
      `getColumns` in class `FluentSelect<T, ColumnSelect<T>>`

  - ### selectFirst

    public T selectFirst(ObjectContext context)
    Description copied from interface: `Select`
    Selects a single object using provided context. The query itself can match any number of objects, but will return only the first one. It returns null if no objects were matched.

    If it matched more than one object, the first object from the list is returned. This makes 'selectFirst' different from `Select.selectOne(ObjectContext)`, which would throw in this situation. 'selectFirst' is useful e.g. when the query is ordered and we only want to see the first object (e.g. "most recent news article"), etc.

    Selecting the first object via "Select.selectFirst(ObjectContext)" is more comprehensible than selecting via "ObjectContext.selectFirst(Select)", because implementations of "Select" set fetch size limit to one.

  - ### getMetaData

    public QueryMetadata getMetaData(EntityResolver resolver)
    Description copied from class: `AbstractQuery`
    Returns default select parameters.
    Specified by:
      `getMetaData` in interface `Query`
    Overrides:
      `getMetaData` in class `AbstractQuery`

  - ### getBaseMetaData

    protected org.apache.cayenne.query.ColumnSelectMetadata getBaseMetaData()
    Specified by:
      `getBaseMetaData` in class `CacheableQuery`

  - ### map

    public <E> ColumnSelect<E> map(Function<T,E> mapper)
    Maps result of this query by processing with a given function.
    Could be used to map plain Object[] to some domain-specific object.
    Note: this method could be called multiple time, result will be mapped by all functions in the call order.
    Type Parameters:
      `E` - new result type
    Parameters:
      `mapper` - function that maps result to the required type.
    Returns:
      this query with changed result type
    Since:
      4.2