blob: 08ec73e0c3d400e1485d8951a2b1a6af7e067220 [file] [log] [blame]
---
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
title: "Cayenne Guide 5.0 · Apache Velocity Extension"
description: "Cayenne Guide 5.0, Apache Velocity Extension"
cayenneVersion: "5.0"
cayenneNoToc: true
---
<div class="sect2">
<h3 id="ext-velocity"><a class="anchor" href="#ext-velocity"></a>4.6. Apache Velocity Extension</h3>
<div class="paragraph">
<p>Enables usage of full featured Apache Velocity templates in <code>SQLSelect</code> / <code>SQLExec</code> queries.</p>
</div>
<div class="sect3">
<h4 id="maven-7"><a class="anchor" href="#maven-7"></a>4.6.1. Maven</h4>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-XML XML" data-lang="XML">&lt;dependency&gt;
&lt;groupId&gt;org.apache.cayenne&lt;/groupId&gt;
&lt;artifactId&gt;cayenne-velocity&lt;/artifactId&gt;
&lt;version&gt;5.0-SNAPSHOT&lt;/version&gt;
&lt;/dependency&gt;</code></pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="gradle-7"><a class="anchor" href="#gradle-7"></a>4.6.2. Gradle</h4>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code class="language-Groovy Groovy" data-lang="Groovy">compile 'org.apache.cayenne:cayenne-velocity:5.0-SNAPSHOT'</code></pre>
</div>
</div>
</div>
<div class="sect3">
<h4 id="usage-6"><a class="anchor" href="#usage-6"></a>4.6.3. Usage</h4>
<div class="paragraph">
<p>This module doesn’t require any additional setup. In addition of directives mentioned in <a href="/docs/5.0/cayenne-guide/queries#directives">this chapter</a>, this module also adds <code>#chain</code> and <code>#chunk</code> directives.</p>
</div>
<div class="paragraph">
<p><code>#chain</code> and <code>#chunk</code> directives are used for conditional inclusion of SQL code. They are used together with <code>#chain</code> wrapping multiple <code>#chunks</code>. 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.</p>
</div>
<div class="paragraph">
<p>Semantics:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code>#chain(operator) ... #end
#chain(operator prefix) ... #end
#chunk() ... #end
#chunk(param) ... #end</code></pre>
</div>
</div>
<div class="paragraph">
<p>Full example:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="highlight"><code>#chain('OR' 'WHERE')
#chunk($name) NAME LIKE #bind($name) #end
#chunk($id) ARTIST_ID &gt; #bind($id) #end
#end"</code></pre>
</div>
</div>
</div>
</div>