Skip to content

Instantly share code, notes, and snippets.

@harawata
Created March 15, 2014 17:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save harawata/9570954 to your computer and use it in GitHub Desktop.
Save harawata/9570954 to your computer and use it in GitHub Desktop.
A custom Velocity directive for MyBatis to generate column aliases. Related to https://github.com/mybatis/mybatis-3/pull/136
/*
* Copyright 2014 MyBatis.org.
*
* Licensed 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
*
* http://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.
*/
package org.mybatis.scripting.velocity.use;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import org.apache.velocity.context.InternalContextAdapter;
import org.apache.velocity.exception.MethodInvocationException;
import org.apache.velocity.exception.ParseErrorException;
import org.apache.velocity.exception.ResourceNotFoundException;
import org.apache.velocity.runtime.directive.Directive;
import org.apache.velocity.runtime.parser.node.ASTBlock;
import org.apache.velocity.runtime.parser.node.Node;
public class ColumnAliasDirective extends Directive {
@Override
public String getName() {
return "columnalias";
}
@Override
public int getType() {
return BLOCK;
}
@Override
public boolean render(InternalContextAdapter context, Writer writer, Node node) throws IOException, ResourceNotFoundException, ParseErrorException, MethodInvocationException {
String body = null;
String table = null;
String prefix = null;
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
if (node.jjtGetChild(i) != null) {
if (node.jjtGetChild(i) instanceof ASTBlock) {
StringWriter stringWriter = new StringWriter();
node.jjtGetChild(i).render(context, stringWriter);
body = stringWriter.toString();
break;
} else {
if (i == 0) {
table = String.valueOf(node.jjtGetChild(i).value(context));
} else if (i == 1) {
prefix = String.valueOf(node.jjtGetChild(i).value(context));
} else {
break;
}
}
}
}
writer.write(translate(body, table, prefix));
return true;
}
private String translate(String body, String table, String prefix) {
if (body == null || body.length() == 0 || (table == null && prefix == null))
return body;
StringBuilder buffer = new StringBuilder();
String tablePrefix = table != null && table.length() > 0 ? table + "." : "";
String columnPrefix = prefix != null && prefix.length() > 0 ? prefix : "";
String[] columns = body.split(",");
for (String column : columns) {
String trimmed = column.trim();
if (trimmed.length() > 0) {
if (buffer.length() > 0)
buffer.append(", ");
buffer.append(tablePrefix).append(trimmed);
if (columnPrefix.length() > 0) {
buffer.append(" AS ").append(columnPrefix).append(trimmed);
}
}
}
return buffer.toString();
}
}
select
comment.a AS comment_a, comment.b AS comment_b, comment.c AS comment_c
from tab comment
<sql id="cols">a, b, c</sql>
<select lang="velocity">
select
#columnalias("comment", "comment_")
<include refid="cols" />
#end
from tab comment
</select>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment