public class SQLTemplate extends AbstractQuery implements ParameterizedQuery, XMLSerializable
SQLTemplate stores a dynamic template for the SQL query that supports parameters and customization using Velocity scripting language. The most straightforward use of scripting abilities is to build parameterized queries. For example:
SELECT ID, NAME FROM SOME_TABLE WHERE NAME LIKE $a
For advanced scripting options see "Scripting SQLTemplate" chapter in the User Guide.
SQLTemplate has a default template script
, but
also it allows to configure multiple templates and switch them dynamically.
This way a single query can have multiple "dialects" specific to a given
database.
Modifier and Type | Field and Description |
---|---|
protected CapsStrategy |
columnNamesCapitalization |
protected String |
defaultTemplate |
protected Map<String,?>[] |
parameters |
protected List<Object> |
positionalParams |
protected SQLResult |
result |
protected Map<String,String> |
templates |
dataMap, name, root
Constructor and Description |
---|
SQLTemplate()
Creates an empty SQLTemplate.
|
SQLTemplate(Class<?> rootClass,
String defaultTemplate) |
SQLTemplate(DataMap rootMap,
String defaultTemplate,
boolean isFetchingDataRows) |
SQLTemplate(DbEntity rootEntity,
String defaultTemplate) |
SQLTemplate(ObjEntity rootEntity,
String defaultTemplate) |
SQLTemplate(String defaultTemplate,
boolean isFetchingDataRows)
Creates a SQLTemplate without an explicit root.
|
SQLTemplate(String objEntityName,
String defaultTemplate) |
Modifier and Type | Method and Description |
---|---|
PrefetchTreeNode |
addPrefetch(String prefetchPath)
Adds a prefetch.
|
void |
addPrefetches(Collection<String> prefetches)
Adds all prefetches from a provided collection.
|
void |
clearPrefetches()
Clears all prefetches.
|
Query |
createQuery(Map<String,?> parameters)
Creates and returns a new SQLTemplate built using this query as a
prototype and substituting template parameters with the values from the
map.
|
SQLAction |
createSQLAction(SQLActionVisitor visitor)
Calls sqlAction(this) on the visitor.
|
void |
encodeAsXML(XMLEncoder encoder)
Prints itself as XML to the provided PrintWriter.
|
String[] |
getCacheGroups() |
QueryCacheStrategy |
getCacheStrategy() |
CapsStrategy |
getColumnNamesCapitalization()
Returns a column name capitalization policy applied to selecting queries.
|
String |
getCustomTemplate(String key)
Returns template for key, or null if there is no template configured for
this key.
|
String |
getDataNodeName()
Returns a name of the DataNode to use with this SQLTemplate.
|
String |
getDefaultTemplate()
Returns default SQL template for this query.
|
int |
getFetchLimit() |
int |
getFetchOffset() |
QueryMetadata |
getMetaData(EntityResolver resolver)
Returns default select parameters.
|
int |
getPageSize() |
Map<String,?> |
getParameters()
Deprecated.
since 4.0 in favor of
getParams() , as multiple
batches of parameters are superseded by the use of
QueryChain . |
Map<String,?> |
getParams()
Returns a map of named parameters that will be bound to SQL.
|
List<Object> |
getPositionalParams()
Returns a list of positional parameters that will be bound to SQL.
|
PrefetchTreeNode |
getPrefetchTree() |
SQLResult |
getResult() |
int |
getStatementFetchSize() |
String |
getTemplate(String key)
Returns a template for key, or a default template if a template for key
is not found.
|
Collection<String> |
getTemplateKeys()
Returns a collection of configured template keys.
|
void |
initWithProperties(Map<String,?> properties)
Initializes query parameters using a set of properties.
|
boolean |
isFetchingDataRows() |
Iterator<Map<String,?>> |
parametersIterator()
Deprecated.
since 4.0 as multiple batches of parameters are superseded by
the use of
QueryChain . |
int |
parametersSize()
Deprecated.
since 4.0 as multiple batches of parameters are superseded by
the use of
QueryChain . |
SQLTemplate |
queryWithParameters(Map<String,?>... parameters)
Deprecated.
since 4.0 as multiple batches of parameters are superseded by
the use of
QueryChain . For an alternative use
createQuery(Map) . |
void |
removePrefetch(String prefetch) |
void |
removeTemplate(String key) |
void |
route(QueryRouter router,
EntityResolver resolver,
Query substitutedQuery)
Implements default routing mechanism relying on the EntityResolver to find DataMap
based on the query root.
|
void |
setCacheGroups(String... cacheGroups) |
void |
setCacheStrategy(QueryCacheStrategy strategy) |
void |
setColumnNamesCapitalization(CapsStrategy columnNameCapitalization)
Sets a column name capitalization policy applied to selecting queries.
|
void |
setDataNodeName(String dataNodeName)
Sets a name of the DataNode to use with this SQLTemplate.
|
void |
setDefaultTemplate(String string)
Sets default SQL template for this query.
|
void |
setFetchingDataRows(boolean flag) |
void |
setFetchLimit(int fetchLimit) |
void |
setFetchOffset(int fetchOffset) |
void |
setPageSize(int pageSize) |
void |
setParameters(Map<String,?>... parameters)
Deprecated.
since 4.0 in favor of
setParams(Map) , as multiple
batches of parameters are superseded by the use of
QueryChain . |
void |
setParams(Map<String,?> params)
Initializes named parameter of this query.
|
void |
setParamsArray(Object... params)
Initializes positional parameters of the query.
|
void |
setParamsList(List<Object> params)
Initializes positional parameters of the query.
|
void |
setResult(SQLResult resultSet)
Sets an optional explicit mapping of the result set.
|
void |
setRoot(Object value)
Sets the root of the query
|
void |
setStatementFetchSize(int size)
Sets statement's fetch size (0 for no default size)
|
void |
setTemplate(String key,
String template)
Adds a SQL template string for a given key.
|
void |
useLocalCache(String... cacheGroups)
Instructs Cayenne to look for query results in the "local" cache when
running the query.
|
void |
useSharedCache(String... cacheGroups)
Instructs Cayenne to look for query results in the "shared" cache when
running the query.
|
acceptVisitor, getDataMap, getName, getRoot, setDataMap, setName, toString
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
getDataMap, getName
acceptVisitor
protected String defaultTemplate
protected CapsStrategy columnNamesCapitalization
protected SQLResult result
public SQLTemplate()
public SQLTemplate(String defaultTemplate, boolean isFetchingDataRows)
public SQLTemplate(DataMap rootMap, String defaultTemplate, boolean isFetchingDataRows)
public void setRoot(Object value)
AbstractQuery
setRoot
in class AbstractQuery
value
- The new rootpublic void route(QueryRouter router, EntityResolver resolver, Query substitutedQuery)
AbstractQuery
route
in interface Query
route
in class AbstractQuery
public QueryMetadata getMetaData(EntityResolver resolver)
AbstractQuery
getMetaData
in interface Query
getMetaData
in class AbstractQuery
public SQLAction createSQLAction(SQLActionVisitor visitor)
createSQLAction
in interface Query
createSQLAction
in class AbstractQuery
public void encodeAsXML(XMLEncoder encoder)
encodeAsXML
in interface XMLSerializable
public void initWithProperties(Map<String,?> properties)
@Deprecated public Iterator<Map<String,?>> parametersIterator()
QueryChain
.@Deprecated public int parametersSize()
QueryChain
.public void setParams(Map<String,?> params)
public void setParamsArray(Object... params)
Note that calling this method will reset any previously set *named* parameters.
public void setParamsList(List<Object> params)
Note that calling this method will reset any previously set *named* parameters.
@Deprecated public SQLTemplate queryWithParameters(Map<String,?>... parameters)
QueryChain
. For an alternative use
createQuery(Map)
.public Query createQuery(Map<String,?> parameters)
createQuery
in interface ParameterizedQuery
public QueryCacheStrategy getCacheStrategy()
public void setCacheStrategy(QueryCacheStrategy strategy)
public String[] getCacheGroups()
public void setCacheGroups(String... cacheGroups)
public void useLocalCache(String... cacheGroups)
query.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE); query.setCacheGroups("group1", "group2");
public void useSharedCache(String... cacheGroups)
query.setCacheStrategy(QueryCacheStrategy.SHARED_CACHE); query.setCacheGroups("group1", "group2");
public int getFetchLimit()
public void setFetchLimit(int fetchLimit)
public int getFetchOffset()
public void setFetchOffset(int fetchOffset)
public int getPageSize()
public void setPageSize(int pageSize)
public void setFetchingDataRows(boolean flag)
public boolean isFetchingDataRows()
public String getDefaultTemplate()
public void setDefaultTemplate(String string)
public String getTemplate(String key)
public String getCustomTemplate(String key)
getTemplate(String)
this method does not return
a default template as a failover strategy, rather it returns null.public void setTemplate(String key, String template)
setDefaultTemplate(String)
public void removeTemplate(String key)
public Collection<String> getTemplateKeys()
public Map<String,?> getParams()
public List<Object> getPositionalParams()
@Deprecated public Map<String,?> getParameters()
getParams()
, as multiple
batches of parameters are superseded by the use of
QueryChain
.@Deprecated public void setParameters(Map<String,?>... parameters)
setParams(Map)
, as multiple
batches of parameters are superseded by the use of
QueryChain
.public PrefetchTreeNode getPrefetchTree()
public PrefetchTreeNode addPrefetch(String prefetchPath)
public void removePrefetch(String prefetch)
public void addPrefetches(Collection<String> prefetches)
public void clearPrefetches()
public CapsStrategy getColumnNamesCapitalization()
public void setColumnNamesCapitalization(CapsStrategy columnNameCapitalization)
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.
public void setResult(SQLResult resultSet)
SQLResult
rules.public SQLResult getResult()
public void setStatementFetchSize(int size)
public int getStatementFetchSize()
public String getDataNodeName()
public void setDataNodeName(String dataNodeName)
Copyright © 2001–2015 Apache Cayenne. All rights reserved.