Chapter 29. Apache Velocity extension

Table of Contents

Description
Including in a project
Usage

Description

This module enables usage of full featured Apache Velocity templates in org.apache.cayenne.query.SQLTemplate queries.

Including in a project

Maven

<dependency>
    <groupId>org.apache.cayenne</groupId>
    <artifactId>cayenne-velocity</artifactId>
    <version>4.1.M1</version>
</dependency>

Gradle

compile 'org.apache.cayenne:cayenne-velocity:4.1.M1'

Usage

This module doesn't require any additional setup.

In addition of directives mentioned in this chapter, this module enables #chain and #chunk directives.

#chain and #chunk directives are used for conditional inclusion of SQL code. They are used together with #chain wrapping multiple #chunks. A chunk evaluates its parameter expression and if it is NULL suppresses rendering of the enclosed SQL block. A chain renders its prefix and its chunks joined by the operator. If all the chunks are suppressed, the chain itself is suppressed. This allows to work with otherwise hard to script SQL semantics. E.g. a WHERE clause can contain multiple conditions joined with AND or OR. Application code would like to exclude a condition if its right-hand parameter is not present (similar to Expression pruning discussed above). If all conditions are excluded, the entire WHERE clause should be excluded. chain/chunk allows to do that.

Semantics:

#chain(operator) ... #end
#chain(operator prefix) ... #end
#chunk() ... #end
#chunk(param) ... #end 

Full example:

#chain('OR' 'WHERE')
    #chunk($name) NAME LIKE #bind($name) #end
    #chunk($id) ARTIST_ID > #bind($id) #end
#end"