2009-02-22

Ibator plugin to add LIMIT clause to the example class

This Ibator plugin adds setLimitClause() method to example class.


import java.util.Iterator;
import java.util.List;

import org.apache.ibatis.ibator.api.IbatorPluginAdapter;
import org.apache.ibatis.ibator.api.IntrospectedTable;
import org.apache.ibatis.ibator.api.dom.java.Field;
import org.apache.ibatis.ibator.api.dom.java.FullyQualifiedJavaType;
import org.apache.ibatis.ibator.api.dom.java.JavaVisibility;
import org.apache.ibatis.ibator.api.dom.java.Method;
import org.apache.ibatis.ibator.api.dom.java.Parameter;
import org.apache.ibatis.ibator.api.dom.java.TopLevelClass;
import org.apache.ibatis.ibator.api.dom.xml.Attribute;
import org.apache.ibatis.ibator.api.dom.xml.Element;
import org.apache.ibatis.ibator.api.dom.xml.TextElement;
import org.apache.ibatis.ibator.api.dom.xml.XmlElement;

public class AddLimitClausePlugin extends IbatorPluginAdapter
{

  public boolean validate(List<String> warnings)
  {
    return true;
  }

  @Override
  public boolean modelExampleClassGenerated(TopLevelClass topLevelClass,
    IntrospectedTable introspectedTable)
  {
    Iterator<Method> it = topLevelClass.getMethods().iterator();
    while (it.hasNext())
    {
      Method m = it.next();
      // This logic will be broken if another constructor is added
      if (m.isConstructor() && m.getParameters() != null && m.getParameters().size() > 0)
      {
        m.addBodyLine("this.limitClause = example.limitClause;");
      }
    }

    // add field, getter, setter for limit clause
    Field field = new Field();
    field.setVisibility(JavaVisibility.PROTECTED);
    field.setType(FullyQualifiedJavaType.getStringInstance());
    field.setName("limitClause");
    ibatorContext.getCommentGenerator().addFieldComment(field,
      introspectedTable.getFullyQualifiedTable());
    topLevelClass.addField(field);

    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setName("setLimitClause");
    method.addParameter(new Parameter(FullyQualifiedJavaType.getStringInstance(),
      "limitClause"));
    method.addBodyLine("this.limitClause = limitClause;");
    ibatorContext.getCommentGenerator().addGeneralMethodComment(method,
      introspectedTable.getFullyQualifiedTable());
    topLevelClass.addMethod(method);

    method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getStringInstance());
    method.setName("getLimitClause");
    method.addBodyLine("return limitClause;");
    ibatorContext.getCommentGenerator().addGeneralMethodComment(method,
      introspectedTable.getFullyQualifiedTable());
    topLevelClass.addMethod(method);

    return true;
  }

  @Override
  public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element,
    IntrospectedTable introspectedTable)
  {
    Iterator<Element> it = element.getElements().iterator();
    while (it.hasNext())
    {
      Element elem = it.next();
      if (elem instanceof XmlElement)
      {
        XmlElement xmlElem = (XmlElement)elem;
        if ("isParameterPresent".equals(xmlElem.getName()))
        {
          XmlElement isNotNullElement = new XmlElement("isNotNull");
          isNotNullElement.addAttribute(new Attribute("property", "limitClause"));
          isNotNullElement.addElement(new TextElement("limit $limitClause$"));
          xmlElem.addElement(isNotNullElement);
        }
      }
    }
    return true;
  }

}

For example, to display 10 results from offset 15, you can write as follows.

- MySQL


PersonExample example = new PersonExample();
// other conditions...
example.setLimitClause("15, 10");
- PostgreSQL

PersonExample example = new PersonExample();
// other conditions...
example.setLimitClause("10 offset 15");

No comments:

Post a Comment