Hi Benjamin,
i attached the plugin class file to this email.

Comments from everyone are welcome! :-)

bye
Marco


2009/4/11  <benja...@bar54.de>:
> Hi marco,
> Definitly yes!I am currently working on the same kind of plugin and it would 
> be great to be able to safe this effort.
>
> Cheers
> Benjamin
package org.apache.ibatis.ibator.plugins;

import java.util.List;
import org.apache.ibatis.ibator.api.FullyQualifiedTable;
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.PrimitiveTypeWrapper;
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.TextElement;
import org.apache.ibatis.ibator.api.dom.xml.XmlElement;
import org.apache.ibatis.ibator.generator.ibatis2.XmlConstants;

/**
 * This plugin adds limit and offset clause to the example class and 
 * to the sqlMap selectByExample map.
 *
 * @author Marco Musu
 *
 */
public class AddLimitOffsetPlugin extends IbatorPluginAdapter {

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

    @Override
    public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
        PrimitiveTypeWrapper integerWrapper = FullyQualifiedJavaType.getIntInstance().getPrimitiveTypeWrapper();

        Field limit = new Field();
        limit.setName("limit");
        limit.setVisibility(JavaVisibility.PRIVATE);
        limit.setType(integerWrapper);
        topLevelClass.addField(limit);

        Method limitSet = new Method();
        limitSet.setVisibility(JavaVisibility.PUBLIC);
        limitSet.setName("setLimit"); 
        limitSet.addParameter(new Parameter(integerWrapper, "limit")); 
        limitSet.addBodyLine("this.limit = limit;"); 
        topLevelClass.addMethod(limitSet);

        Method limitGet = new Method();
        limitGet.setVisibility(JavaVisibility.PUBLIC);
        limitGet.setReturnType(integerWrapper);
        limitGet.setName("getLimit"); 
        limitGet.addBodyLine("return limit;"); 
        topLevelClass.addMethod(limitGet);

        Field offset = new Field();
        offset.setName("offset");
        offset.setVisibility(JavaVisibility.PRIVATE);
        offset.setType(integerWrapper);
        topLevelClass.addField(offset);

        Method offsetSet = new Method();
        offsetSet.setVisibility(JavaVisibility.PUBLIC);
        offsetSet.setName("setOffset"); 
        offsetSet.addParameter(new Parameter(integerWrapper, "offset")); 
        offsetSet.addBodyLine("this.offset = offset;"); 
        topLevelClass.addMethod(offsetSet);

        Method offsetGet = new Method();
        offsetGet.setVisibility(JavaVisibility.PUBLIC);
        offsetGet.setReturnType(integerWrapper);
        offsetGet.setName("getOffset"); 
        offsetGet.addBodyLine("return offset;"); 
        topLevelClass.addMethod(offsetGet);

        return true;
    }

    @Override
    public boolean sqlMapSelectByExampleWithoutBLOBsElementGenerated(XmlElement element, IntrospectedTable introspectedTable) {
        FullyQualifiedTable table = introspectedTable.getFullyQualifiedTable();
        element.getElements().remove(element.getElements().size() - 1);
        XmlElement isParameterPresenteElement =
                new XmlElement("isParameterPresent"); 
        element.addElement(isParameterPresenteElement);

        XmlElement includeElement = new XmlElement("include"); 
        includeElement.addAttribute(new Attribute("refid", 
                table.getSqlMapNamespace() + "." + XmlConstants.EXAMPLE_WHERE_CLAUSE_ID)); 
        isParameterPresenteElement.addElement(includeElement);

        XmlElement isNotNullElement = new XmlElement("isNotNull"); 
        isNotNullElement.addAttribute(new Attribute("property", "orderByClause"));  
        isNotNullElement.addElement(new TextElement("order by $orderByClause$")); 
        isParameterPresenteElement.addElement(isNotNullElement);

        isNotNullElement = new XmlElement("isNotNull"); 
        isNotNullElement.addAttribute(new Attribute("property", "limit"));  
        isNotNullElement.addElement(new TextElement("limit $limit$")); 
        isParameterPresenteElement.addElement(isNotNullElement);


        isNotNullElement = new XmlElement("isNotNull"); 
        isNotNullElement.addAttribute(new Attribute("property", "offset"));  
        isNotNullElement.addElement(new TextElement("offset $offset$")); 
        isParameterPresenteElement.addElement(isNotNullElement);
        return true;
    }
}

Reply via email to