|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.cayenne.query.AbstractQuery
org.apache.cayenne.query.SQLTemplate
public class SQLTemplate
A query that executes unchanged (except for template preprocessing) "raw" SQL specified by the user.
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.
SQLTemplate supports multiple sets of parameters, so a single query can be executed
multiple times with different parameters. "Scrolling" through parameter list is done by
calling parametersIterator()
. This iterator goes over parameter sets,
returning a Map on each call to "next()"
Field Summary | |
---|---|
protected String |
columnNamesCapitalization
|
protected String |
defaultTemplate
|
static String |
LOWERCASE_COLUMN_NAMES
|
protected Map[] |
parameters
|
protected Map |
templates
|
static String |
UPPERCASE_COLUMN_NAMES
|
Fields inherited from class org.apache.cayenne.query.AbstractQuery |
---|
name, root |
Constructor Summary | |
---|---|
SQLTemplate()
Creates an empty SQLTemplate. |
|
SQLTemplate(Class rootClass,
String defaultTemplate)
|
|
SQLTemplate(DataMap rootMap,
String defaultTemplate)
|
|
SQLTemplate(DbEntity rootEntity,
String defaultTemplate)
|
|
SQLTemplate(ObjEntity rootEntity,
String defaultTemplate)
|
|
SQLTemplate(String objEntityName,
String defaultTemplate)
|
Method Summary | |
---|---|
PrefetchTreeNode |
addPrefetch(String prefetchPath)
Adds a prefetch. |
void |
addPrefetches(Collection prefetches)
Adds all prefetches from a provided collection. |
void |
clearPrefetches()
Clears all prefetches. |
Query |
createQuery(Map 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()
|
String |
getCachePolicy()
|
String |
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 |
getDefaultTemplate()
Returns default SQL template for this query. |
int |
getFetchLimit()
|
QueryMetadata |
getMetaData(EntityResolver resolver)
Returns default select parameters. |
int |
getPageSize()
|
Map |
getParameters()
Utility method to get the first set of parameters, since most queries will only have one. |
PrefetchTreeNode |
getPrefetchTree()
|
String |
getTemplate(String key)
Returns a template for key, or a default template if a template for key is not found. |
Collection |
getTemplateKeys()
Returns a collection of configured template keys. |
void |
initWithProperties(Map properties)
Initializes query parameters using a set of properties. |
boolean |
isFetchingDataRows()
|
boolean |
isRefreshingObjects()
|
boolean |
isResolvingInherited()
|
Iterator |
parametersIterator()
Returns an iterator over parameter sets. |
int |
parametersSize()
Returns the number of parameter sets. |
SQLTemplate |
queryWithParameters(Map parameters)
Returns a new query built using this query as a prototype and a new set of parameters. |
SQLTemplate |
queryWithParameters(Map[] parameters)
Returns a new query built using this query as a prototype and a new set of parameters. |
void |
removePrefetch(String prefetch)
|
void |
removeTemplate(String key)
|
void |
setCacheGroups(String[] cachGroups)
|
void |
setCachePolicy(String policy)
|
void |
setColumnNamesCapitalization(String columnNameCapitalization)
Sets a column name capitalization policy applied to selecting queries. |
void |
setDefaultTemplate(String string)
Sets default SQL template for this query. |
void |
setFetchingDataRows(boolean flag)
|
void |
setFetchLimit(int fetchLimit)
|
void |
setPageSize(int pageSize)
|
void |
setParameters(Map map)
Utility method to initialize query with only a single set of parameters. |
void |
setParameters(Map[] parameters)
|
void |
setRefreshingObjects(boolean flag)
|
void |
setResolvingInherited(boolean b)
|
void |
setResultSetMapping(SQLResultSetMapping resultSetMapping)
Sets an optional explicit mapping of the result set. |
void |
setTemplate(String key,
String template)
Adds a SQL template string for a given key. |
Methods inherited from class org.apache.cayenne.query.AbstractQuery |
---|
getName, getRoot, route, setName, setRoot, toString |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Methods inherited from interface org.apache.cayenne.query.Query |
---|
getName, route |
Field Detail |
---|
public static final String UPPERCASE_COLUMN_NAMES
public static final String LOWERCASE_COLUMN_NAMES
protected String defaultTemplate
protected Map templates
protected Map[] parameters
protected String columnNamesCapitalization
Constructor Detail |
---|
public SQLTemplate()
public SQLTemplate(DataMap rootMap, String defaultTemplate)
public SQLTemplate(ObjEntity rootEntity, String defaultTemplate)
public SQLTemplate(Class rootClass, String defaultTemplate)
public SQLTemplate(DbEntity rootEntity, String defaultTemplate)
public SQLTemplate(String objEntityName, String defaultTemplate)
Method Detail |
---|
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 properties)
public Iterator parametersIterator()
public int parametersSize()
public SQLTemplate queryWithParameters(Map parameters)
public SQLTemplate queryWithParameters(Map[] parameters)
public Query createQuery(Map parameters)
createQuery
in interface ParameterizedQuery
public String getCachePolicy()
public void setCachePolicy(String policy)
public String[] getCacheGroups()
public void setCacheGroups(String[] cachGroups)
public int getFetchLimit()
public void setFetchLimit(int fetchLimit)
public int getPageSize()
public void setPageSize(int pageSize)
public void setFetchingDataRows(boolean flag)
public boolean isFetchingDataRows()
public boolean isRefreshingObjects()
public void setRefreshingObjects(boolean flag)
public boolean isResolvingInherited()
public void setResolvingInherited(boolean b)
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 getTemplateKeys()
public Map getParameters()
public void setParameters(Map map)
setParameters(Map[])
.
public void setParameters(Map[] parameters)
public PrefetchTreeNode getPrefetchTree()
public PrefetchTreeNode addPrefetch(String prefetchPath)
public void removePrefetch(String prefetch)
public void addPrefetches(Collection prefetches)
public void clearPrefetches()
public String getColumnNamesCapitalization()
public void setColumnNamesCapitalization(String columnNameCapitalization)
columnNameCapitalization
- Can be null of one of
LOWERCASE_COLUMN_NAMES
or UPPERCASE_COLUMN_NAMES
.public void setResultSetMapping(SQLResultSetMapping resultSetMapping)
SQLResultSetMapping
rules.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |