org.apache.cayenne.query

## Class ColumnSelect<T>

- java.lang.Object
- - org.apache.cayenne.query.IndirectQuery
  - - org.apache.cayenne.query.FluentSelect<T>
    - - org.apache.cayenne.query.ColumnSelect<T>

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

      public class ColumnSelect<T>
      extends FluentSelect<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, Property.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 inherited from class org.apache.cayenne.query.FluentSelect

      `cacheGroup, cacheStrategy, dbEntityName, entityName, entityType, limit, offset, orderings, pageSize, prefetches, statementFetchSize, where`

    - ### Fields inherited from class org.apache.cayenne.query.IndirectQuery

      `dataMap, lastResolver, name, replacementQuery`

  - ### Constructor Summary

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

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `ColumnSelect<T>` | `and(Collection<Expression> expressions)` AND's provided expressions to the existing WHERE or HAVING clause expression.
    `ColumnSelect<T>` | `and(Expression... expressions)` AND's provided expressions to the existing WHERE clause expression.
    `ColumnSelect<Object[]>` | `avg(Property<?> property)` Select average value of property
    `ColumnSelect<T>` | `cacheGroup(String cacheGroup)`
    `ColumnSelect<T>` | `cacheStrategy(QueryCacheStrategy strategy)`
    `ColumnSelect<T>` | `cacheStrategy(QueryCacheStrategy strategy, String cacheGroup)`
    `protected <E> ColumnSelect<E>` | `column(Property<E> property)`
    `ColumnSelect<Object[]>` | `columns(Collection<Property<?>> properties)` Add properties to select.
    `ColumnSelect<Object[]>` | `columns(Property<?> firstProperty, Property<?>... otherProperties)` Add properties to select.
    `ColumnSelect<Object[]>` | `count()` Shortcut for `columns(Property, Property[])` columns}(Property.COUNT)
    `ColumnSelect<Object[]>` | `count(Property<?> property)` Select COUNT(property)
    `protected Query` | `createReplacementQuery(EntityResolver resolver)` Translates self to a SelectQuery.
    `ColumnSelect<T>` | `dbEntityName(String dbEntityName)` Sets the `DbEntity` name to fetch without changing the return type of the query.
    `ColumnSelect<T>` | `distinct()` Explicitly request distinct in 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.
    `Collection<Property<?>>` | `getColumns()`
    `Expression` | `getHaving()` Returns a HAVING clause Expression of this query.
    `ColumnSelect<T>` | `having(Expression expression)` Appends a having qualifier expression of this query.
    `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>` | `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<Object[]>` | `max(Property<?> property)` Select maximum value of property
    `ColumnSelect<Object[]>` | `min(Property<?> property)` Select minimum value of property
    `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 or HAVING clause expression.
    `ColumnSelect<T>` | `or(Expression... expressions)` OR's provided expressions to the existing WHERE clause expression.
    `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>` | `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>` | `pageSize(int pageSize)` Resets query page size.
    `ColumnSelect<T>` | `prefetch(PrefetchTreeNode prefetch)` Merges prefetch into the query prefetch tree.
    `ColumnSelect<T>` | `prefetch(String path, int semantics)` Merges a prefetch path with specified semantics into the query prefetch tree.
    `T` | `selectFirst(ObjectContext context)` Selects a single object using provided context.
    `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.
    `<E extends Number> ColumnSelect<Object[]>` | `sum(Property<E> property)` Select sum of values
    `ColumnSelect<T>` | `suppressDistinct()` Explicitly suppress distinct in query.
    `ColumnSelect<T>` | `where(Expression expression)` Appends a qualifier expression of 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.

    - ### Methods inherited from class org.apache.cayenne.query.FluentSelect

      `batchIterator, getCacheGroup, getCacheStrategy, getDbEntityName, getEntityName, getEntityType, getLimit, getOffset, getOrderings, getPageSize, getPrefetches, getStatementFetchSize, getWhere, iterate, iterator, select, selectOne`

    - ### Methods inherited from class org.apache.cayenne.query.IndirectQuery

      `createSQLAction, getDataMap, getMetaData, getName, getReplacementQuery, route, setDataMap, setName`

    - ### Methods inherited from class java.lang.Object

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

    - ### Methods inherited from interface org.apache.cayenne.query.Query

      `createSQLAction, getDataMap, getMetaData, getName, route`

- - ### Constructor Detail

    - #### ColumnSelect

          protected ColumnSelect()

    - #### ColumnSelect

          protected ColumnSelect(ObjectSelect<T> select)

      Copy constructor to convert ObjectSelect to ColumnSelect

  - ### Method Detail

    - #### createReplacementQuery

          protected Query createReplacementQuery(EntityResolver resolver)

      Description copied from class: `FluentSelect`
      Translates self to a SelectQuery.
      Overrides:
        `createReplacementQuery` in class `FluentSelect<T>`

    - #### entityType

          public ColumnSelect<T> entityType(Class<?> entityType)

      Sets the type of the entity to fetch without changing the return type of the query.
      Returns:
        this object

    - #### entityName

          public ColumnSelect<T> 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 ColumnSelect<T> 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 ColumnSelect<T> 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 ColumnSelect<T> 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

    - #### and

          public ColumnSelect<T> and(Expression... expressions)

      AND's provided expressions to the existing WHERE clause expression.
      Returns:
        this object

    - #### or

          public ColumnSelect<T> or(Expression... expressions)

      OR's provided expressions to the existing WHERE clause expression.
      Returns:
        this object

    - #### orderBy

          public ColumnSelect<T> 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 ColumnSelect<T> 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 ColumnSelect<T> orderBy(Ordering... orderings)

      Add one or more orderings to this query.
      Returns:
        this object

    - #### orderBy

          public ColumnSelect<T> orderBy(Collection<Ordering> orderings)

      Adds a list of orderings to this query.
      Returns:
        this object

    - #### prefetch

          public ColumnSelect<T> prefetch(PrefetchTreeNode prefetch)

      Merges prefetch into the query prefetch tree.
      Returns:
        this object

    - #### prefetch

          public ColumnSelect<T> prefetch(String path,
                                          int semantics)

      Merges a prefetch path with specified semantics into the query prefetch tree.
      Returns:
        this object

    - #### limit

          public 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.

    - #### offset

          public 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.

    - #### pageSize

          public ColumnSelect<T> 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 ColumnSelect<T> statementFetchSize(int size)

      Sets fetch size of the PreparedStatement generated for this query. Only non-negative values would change the default size.
      See Also:
        `Statement.setFetchSize(int)`

    - #### cacheStrategy

          public ColumnSelect<T> cacheStrategy(QueryCacheStrategy strategy)

    - #### cacheStrategy

          public ColumnSelect<T> cacheStrategy(QueryCacheStrategy strategy,
                                               String cacheGroup)

    - #### cacheGroup

          public ColumnSelect<T> cacheGroup(String cacheGroup)

    - #### localCache

          public ColumnSelect<T> 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 ColumnSelect<T> 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 ColumnSelect<T> 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 ColumnSelect<T> 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);

    - #### columns

          public ColumnSelect<Object[]> columns(Property<?> firstProperty,
                                                Property<?>... otherProperties)

      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:
        `firstProperty` - first property
        `otherProperties` - 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, Property[])`

    - #### column

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

    - #### count

          public ColumnSelect<Object[]> count()

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

    - #### count

          public ColumnSelect<Object[]> count(Property<?> 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(Property<?> property)

      Select minimum value of property

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

    - #### max

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

      Select maximum value of property

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

    - #### avg

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

      Select average value of property

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

    - #### sum

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

      Select sum of values

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

    - #### having

          public ColumnSelect<T> having(Expression expression)

      Appends a having qualifier expression of this query. An equivalent to `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

    - #### and

          public ColumnSelect<T> and(Collection<Expression> expressions)

      AND's provided expressions to the existing WHERE or HAVING clause expression.
      Returns:
        this object

    - #### or

          public ColumnSelect<T> or(Collection<Expression> expressions)

      OR's provided expressions to the existing WHERE or HAVING clause expression.
      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()

    - #### getHaving

          public Expression getHaving()

      Returns a HAVING clause Expression of this query.

    - #### 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.

Copyright © 2001–2018 Apache Cayenne. All rights reserved.