org.apache.cayenne.query
Class SQLSelect<T>

java.lang.Object
  extended by org.apache.cayenne.query.IndirectQuery
      extended by org.apache.cayenne.query.SQLSelect<T>
All Implemented Interfaces:
Serializable, ConfigurationNode, Query, Select<T>

public class SQLSelect<T>
extends IndirectQuery
implements Select<T>

A selecting query based on raw SQL and featuring fluent API.

Since:
3.2
See Also:
Serialized Form

Field Summary
protected  String[] cacheGroups
           
protected  QueryCacheStrategy cacheStrategy
           
protected  CapsStrategy columnNameCaps
           
protected  String dataMapName
           
protected  int limit
           
protected  int offset
           
protected  int pageSize
           
protected  Map<String,Object> parameters
           
protected  Class<T> persistentType
           
protected  Class<T> scalarType
           
protected  StringBuilder sqlBuffer
           
protected  int statementFetchSize
           
 
Fields inherited from class org.apache.cayenne.query.IndirectQuery
dataMap, lastResolver, name, replacementQuery
 
Constructor Summary
SQLSelect(Class<T> persistentType, String sql)
           
SQLSelect(String sql)
           
 
Method Summary
 SQLSelect<T> append(String sqlChunk)
          Appends a piece of SQL to the previously stored SQL template.
 SQLSelect<T> bind(String name, Object value)
           
 SQLSelect<T> cacheGroups(String... cacheGroups)
           
 SQLSelect<T> cacheStrategy(QueryCacheStrategy strategy)
           
 SQLSelect<T> columnNameCaps(CapsStrategy columnNameCaps)
          Sets a column name capitalization policy applied to selecting queries.
protected  Query createReplacementQuery(EntityResolver resolver)
          Creates a substitute query.
static SQLSelect<DataRow> dataRowQuery(String sql)
          Creates a query that selects DataRows and uses default routing.
static SQLSelect<DataRow> dataRowQuery(String dataMapName, String sql)
          Creates a query that selects DataRows and uses routing based on the provided DataMap name.
 String[] getCacheGroups()
           
 QueryCacheStrategy getCacheStrategy()
           
 CapsStrategy getColumnNameCaps()
          Returns a column name capitalization policy applied to selecting queries.
 int getLimit()
           
 int getOffset()
           
 int getPageSize()
           
 Map<String,Object> getParameters()
          Returns mutable map of parameters that will be bound to SQL.
 String getSql()
           
 int getStatementFetchSize()
           
 boolean isFetchingDataRows()
           
 boolean isFetchingScalars()
           
 SQLSelect<T> limit(int fetchLimit)
           
 SQLSelect<T> lowerColumnNames()
          Equivalent of setting CapsStrategy.LOWER
 SQLSelect<T> offset(int fetchOffset)
           
 SQLSelect<T> pageSize(int pageSize)
           
static
<T> SQLSelect<T>
query(Class<T> type, String sql)
          Creates a query that selects DataObjects.
static
<T> SQLSelect<T>
scalarQuery(Class<T> type, String sql)
          Creates a query that selects scalar values and uses default routing.
static
<T> SQLSelect<T>
scalarQuery(Class<T> type, String dataMapName, String sql)
          Creates a query that selects scalar values and uses routing based on the provided DataMap name.
 List<T> select(ObjectContext context)
          Selects objects using provided context.
 T selectOne(ObjectContext context)
          Selects a single object using provided context.
 SQLSelect<T> statementFetchSize(int size)
          Sets JDBC statement's fetch size (0 for no default size)
 SQLSelect<T> upperColumnNames()
          Equivalent of setting CapsStrategy.UPPER
 void useLocalCache(String... cacheGroups)
          Instructs Cayenne to look for query results in the "local" cache when running the query.
 SQLSelect<T> useSharedCache(String... cacheGroups)
          Instructs Cayenne to look for query results in the "shared" cache when running the query.
 
Methods inherited from class org.apache.cayenne.query.IndirectQuery
acceptVisitor, 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
 
Methods inherited from interface org.apache.cayenne.configuration.ConfigurationNode
acceptVisitor
 

Field Detail

persistentType

protected Class<T> persistentType

scalarType

protected Class<T> scalarType

dataMapName

protected String dataMapName

sqlBuffer

protected StringBuilder sqlBuffer

cacheStrategy

protected QueryCacheStrategy cacheStrategy

cacheGroups

protected String[] cacheGroups

parameters

protected Map<String,Object> parameters

columnNameCaps

protected CapsStrategy columnNameCaps

limit

protected int limit

offset

protected int offset

pageSize

protected int pageSize

statementFetchSize

protected int statementFetchSize
Constructor Detail

SQLSelect

public SQLSelect(String sql)

SQLSelect

public SQLSelect(Class<T> persistentType,
                 String sql)
Method Detail

dataRowQuery

public static SQLSelect<DataRow> dataRowQuery(String sql)
Creates a query that selects DataRows and uses default routing.


dataRowQuery

public static SQLSelect<DataRow> dataRowQuery(String dataMapName,
                                              String sql)
Creates a query that selects DataRows and uses routing based on the provided DataMap name.


query

public static <T> SQLSelect<T> query(Class<T> type,
                                     String sql)
Creates a query that selects DataObjects.


scalarQuery

public static <T> SQLSelect<T> scalarQuery(Class<T> type,
                                           String sql)
Creates a query that selects scalar values and uses default routing.


scalarQuery

public static <T> SQLSelect<T> scalarQuery(Class<T> type,
                                           String dataMapName,
                                           String sql)
Creates a query that selects scalar values and uses routing based on the provided DataMap name.


select

public List<T> select(ObjectContext context)
Selects objects using provided context. Essentially the inversion of "context.select(query)".


selectOne

public T selectOne(ObjectContext context)
Selects a single object using provided context. Essentially the inversion of "Cayenne.objectForSelect(context, query)".


isFetchingDataRows

public boolean isFetchingDataRows()

isFetchingScalars

public boolean isFetchingScalars()

getSql

public String getSql()

append

public SQLSelect<T> append(String sqlChunk)
Appends a piece of SQL to the previously stored SQL template.


bind

public SQLSelect<T> bind(String name,
                         Object value)

getParameters

public Map<String,Object> getParameters()
Returns mutable map of parameters that will be bound to SQL. A caller is free to add/remove parameters from the returned map as needed. Alternatively one should use chained bind(String, Object)


createReplacementQuery

protected Query createReplacementQuery(EntityResolver resolver)
Description copied from class: IndirectQuery
Creates a substitute query. An implementor is free to provide an arbitrary replacement query.

Specified by:
createReplacementQuery in class IndirectQuery

useLocalCache

public void useLocalCache(String... cacheGroups)
Instructs Cayenne to look for query results in the "local" cache when running the query. This is a short-hand notation for:
 query.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
 query.setCacheGroups("group1", "group2");
 

Since:
3.2

useSharedCache

public SQLSelect<T> useSharedCache(String... cacheGroups)
Instructs Cayenne to look for query results in the "shared" cache when running the query. This is a short-hand notation for:
 query.setCacheStrategy(QueryCacheStrategy.SHARED_CACHE);
 query.setCacheGroups("group1", "group2");
 


getCacheStrategy

public QueryCacheStrategy getCacheStrategy()

cacheStrategy

public SQLSelect<T> cacheStrategy(QueryCacheStrategy strategy)

getCacheGroups

public String[] getCacheGroups()

cacheGroups

public SQLSelect<T> cacheGroups(String... cacheGroups)

getColumnNameCaps

public CapsStrategy getColumnNameCaps()
Returns a column name capitalization policy applied to selecting queries. This is used to simplify mapping of the queries like "SELECT * FROM ...", ensuring that a chosen Cayenne column mapping strategy (e.g. all column names in uppercase) is portable across database engines that can have varying default capitalization. Default (null) value indicates that column names provided in result set are used unchanged.


columnNameCaps

public SQLSelect<T> columnNameCaps(CapsStrategy columnNameCaps)
Sets a column name capitalization policy applied to selecting queries. This is used to simplify mapping of the queries like "SELECT * FROM ...", ensuring that a chosen Cayenne column mapping strategy (e.g. all column names in uppercase) is portable across database engines that can have varying default capitalization. Default (null) value indicates that column names provided in result set are used unchanged.

Note that while a non-default setting is useful for queries that do not rely on a #result directive to describe columns, it works for all SQLTemplates the same way.


upperColumnNames

public SQLSelect<T> upperColumnNames()
Equivalent of setting CapsStrategy.UPPER


lowerColumnNames

public SQLSelect<T> lowerColumnNames()
Equivalent of setting CapsStrategy.LOWER


getLimit

public int getLimit()

limit

public SQLSelect<T> limit(int fetchLimit)

getOffset

public int getOffset()

offset

public SQLSelect<T> offset(int fetchOffset)

getPageSize

public int getPageSize()

pageSize

public SQLSelect<T> pageSize(int pageSize)

statementFetchSize

public SQLSelect<T> statementFetchSize(int size)
Sets JDBC statement's fetch size (0 for no default size)


getStatementFetchSize

public int getStatementFetchSize()
Returns:
JBDC statement's fetch size


Copyright © 2001-2013 Apache Cayenne. All Rights Reserved.