Search in sources :

Example 16 with TextElement

use of org.mybatis.generator.api.dom.xml.TextElement in project generator by mybatis.

the class BaseColumnListElementGenerator method addElements.

@Override
public void addElements(XmlElement parentElement) {
    //$NON-NLS-1$
    XmlElement answer = new XmlElement("sql");
    answer.addAttribute(new //$NON-NLS-1$
    Attribute(//$NON-NLS-1$
    "id", introspectedTable.getBaseColumnListId()));
    context.getCommentGenerator().addComment(answer);
    StringBuilder sb = new StringBuilder();
    Iterator<IntrospectedColumn> iter = introspectedTable.getNonBLOBColumns().iterator();
    while (iter.hasNext()) {
        sb.append(MyBatis3FormattingUtilities.getSelectListPhrase(iter.next()));
        if (iter.hasNext()) {
            //$NON-NLS-1$
            sb.append(", ");
        }
        if (sb.length() > 80) {
            answer.addElement(new TextElement(sb.toString()));
            sb.setLength(0);
        }
    }
    if (sb.length() > 0) {
        answer.addElement(new TextElement(sb.toString()));
    }
    if (context.getPlugins().sqlMapBaseColumnListElementGenerated(answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) TextElement(org.mybatis.generator.api.dom.xml.TextElement) XmlElement(org.mybatis.generator.api.dom.xml.XmlElement)

Example 17 with TextElement

use of org.mybatis.generator.api.dom.xml.TextElement in project generator by mybatis.

the class CountByExampleElementGenerator method addElements.

@Override
public void addElements(XmlElement parentElement) {
    //$NON-NLS-1$
    XmlElement answer = new XmlElement("select");
    String fqjt = introspectedTable.getExampleType();
    answer.addAttribute(new Attribute("id", //$NON-NLS-1$
    introspectedTable.getCountByExampleStatementId()));
    //$NON-NLS-1$
    answer.addAttribute(new Attribute("parameterType", fqjt));
    //$NON-NLS-1$ //$NON-NLS-2$
    answer.addAttribute(new Attribute("resultType", "java.lang.Long"));
    context.getCommentGenerator().addComment(answer);
    StringBuilder sb = new StringBuilder();
    //$NON-NLS-1$
    sb.append("select count(*) from ");
    sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
    answer.addElement(new TextElement(sb.toString()));
    answer.addElement(getExampleIncludeElement());
    if (context.getPlugins().sqlMapCountByExampleElementGenerated(answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
Also used : TextElement(org.mybatis.generator.api.dom.xml.TextElement) Attribute(org.mybatis.generator.api.dom.xml.Attribute) XmlElement(org.mybatis.generator.api.dom.xml.XmlElement)

Example 18 with TextElement

use of org.mybatis.generator.api.dom.xml.TextElement in project generator by mybatis.

the class DeleteByPrimaryKeyElementGenerator method addElements.

@Override
public void addElements(XmlElement parentElement) {
    //$NON-NLS-1$
    XmlElement answer = new XmlElement("delete");
    answer.addAttribute(new Attribute("id", //$NON-NLS-1$
    introspectedTable.getDeleteByPrimaryKeyStatementId()));
    String parameterClass;
    if (!isSimple && introspectedTable.getRules().generatePrimaryKeyClass()) {
        parameterClass = introspectedTable.getPrimaryKeyType();
    } else {
        // field, then they are coming in a map.
        if (introspectedTable.getPrimaryKeyColumns().size() > 1) {
            //$NON-NLS-1$
            parameterClass = "map";
        } else {
            parameterClass = introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType().toString();
        }
    }
    answer.addAttribute(new //$NON-NLS-1$
    Attribute(//$NON-NLS-1$
    "parameterType", parameterClass));
    context.getCommentGenerator().addComment(answer);
    StringBuilder sb = new StringBuilder();
    //$NON-NLS-1$
    sb.append("delete from ");
    sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
    answer.addElement(new TextElement(sb.toString()));
    boolean and = false;
    for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
        sb.setLength(0);
        if (and) {
            //$NON-NLS-1$
            sb.append("  and ");
        } else {
            //$NON-NLS-1$
            sb.append("where ");
            and = true;
        }
        sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
        //$NON-NLS-1$
        sb.append(" = ");
        sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
        answer.addElement(new TextElement(sb.toString()));
    }
    if (context.getPlugins().sqlMapDeleteByPrimaryKeyElementGenerated(answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) TextElement(org.mybatis.generator.api.dom.xml.TextElement) Attribute(org.mybatis.generator.api.dom.xml.Attribute) XmlElement(org.mybatis.generator.api.dom.xml.XmlElement)

Example 19 with TextElement

use of org.mybatis.generator.api.dom.xml.TextElement in project generator by mybatis.

the class ExampleWhereClauseElementGenerator method getMiddleForEachElement.

private XmlElement getMiddleForEachElement(IntrospectedColumn introspectedColumn) {
    StringBuilder sb = new StringBuilder();
    String criteriaAttribute;
    boolean typeHandled;
    String typeHandlerString;
    if (introspectedColumn == null) {
        //$NON-NLS-1$
        criteriaAttribute = "criteria.criteria";
        typeHandled = false;
        typeHandlerString = null;
    } else {
        sb.setLength(0);
        //$NON-NLS-1$
        sb.append("criteria.");
        sb.append(introspectedColumn.getJavaProperty());
        //$NON-NLS-1$
        sb.append("Criteria");
        criteriaAttribute = sb.toString();
        typeHandled = true;
        sb.setLength(0);
        //$NON-NLS-1$
        sb.append(",typeHandler=");
        sb.append(introspectedColumn.getTypeHandler());
        typeHandlerString = sb.toString();
    }
    //$NON-NLS-1$
    XmlElement middleForEachElement = new XmlElement("foreach");
    middleForEachElement.addAttribute(new Attribute("collection", //$NON-NLS-1$
    criteriaAttribute));
    //$NON-NLS-1$ //$NON-NLS-2$
    middleForEachElement.addAttribute(new Attribute("item", "criterion"));
    //$NON-NLS-1$
    XmlElement chooseElement = new XmlElement("choose");
    middleForEachElement.addElement(chooseElement);
    //$NON-NLS-1$
    XmlElement when = new XmlElement("when");
    //$NON-NLS-1$ //$NON-NLS-2$
    when.addAttribute(new Attribute("test", "criterion.noValue"));
    //$NON-NLS-1$
    when.addElement(new TextElement("and ${criterion.condition}"));
    chooseElement.addElement(when);
    //$NON-NLS-1$
    when = new XmlElement("when");
    //$NON-NLS-1$ //$NON-NLS-2$
    when.addAttribute(new Attribute("test", "criterion.singleValue"));
    sb.setLength(0);
    //$NON-NLS-1$
    sb.append("and ${criterion.condition} #{criterion.value");
    if (typeHandled) {
        sb.append(typeHandlerString);
    }
    sb.append('}');
    when.addElement(new TextElement(sb.toString()));
    chooseElement.addElement(when);
    //$NON-NLS-1$
    when = new XmlElement("when");
    //$NON-NLS-1$ //$NON-NLS-2$
    when.addAttribute(new Attribute("test", "criterion.betweenValue"));
    sb.setLength(0);
    //$NON-NLS-1$
    sb.append("and ${criterion.condition} #{criterion.value");
    if (typeHandled) {
        sb.append(typeHandlerString);
    }
    //$NON-NLS-1$
    sb.append("} and #{criterion.secondValue");
    if (typeHandled) {
        sb.append(typeHandlerString);
    }
    sb.append('}');
    when.addElement(new TextElement(sb.toString()));
    chooseElement.addElement(when);
    //$NON-NLS-1$
    when = new XmlElement("when");
    //$NON-NLS-1$ //$NON-NLS-2$
    when.addAttribute(new Attribute("test", "criterion.listValue"));
    //$NON-NLS-1$
    when.addElement(new TextElement("and ${criterion.condition}"));
    //$NON-NLS-1$
    XmlElement innerForEach = new XmlElement("foreach");
    innerForEach.addAttribute(//$NON-NLS-1$ //$NON-NLS-2$
    new Attribute("collection", "criterion.value"));
    //$NON-NLS-1$ //$NON-NLS-2$
    innerForEach.addAttribute(new Attribute("item", "listItem"));
    //$NON-NLS-1$ //$NON-NLS-2$
    innerForEach.addAttribute(new Attribute("open", "("));
    //$NON-NLS-1$ //$NON-NLS-2$
    innerForEach.addAttribute(new Attribute("close", ")"));
    //$NON-NLS-1$ //$NON-NLS-2$
    innerForEach.addAttribute(new Attribute("separator", ","));
    sb.setLength(0);
    //$NON-NLS-1$
    sb.append("#{listItem");
    if (typeHandled) {
        sb.append(typeHandlerString);
    }
    sb.append('}');
    innerForEach.addElement(new TextElement(sb.toString()));
    when.addElement(innerForEach);
    chooseElement.addElement(when);
    return middleForEachElement;
}
Also used : TextElement(org.mybatis.generator.api.dom.xml.TextElement) Attribute(org.mybatis.generator.api.dom.xml.Attribute) XmlElement(org.mybatis.generator.api.dom.xml.XmlElement)

Example 20 with TextElement

use of org.mybatis.generator.api.dom.xml.TextElement in project generator by mybatis.

the class InsertSelectiveElementGenerator method addElements.

@Override
public void addElements(XmlElement parentElement) {
    //$NON-NLS-1$
    XmlElement answer = new XmlElement("insert");
    answer.addAttribute(new Attribute("id", //$NON-NLS-1$
    introspectedTable.getInsertSelectiveStatementId()));
    FullyQualifiedJavaType parameterType = introspectedTable.getRules().calculateAllFieldsClass();
    answer.addAttribute(new //$NON-NLS-1$
    Attribute(//$NON-NLS-1$
    "parameterType", parameterType.getFullyQualifiedName()));
    context.getCommentGenerator().addComment(answer);
    GeneratedKey gk = introspectedTable.getGeneratedKey();
    if (gk != null) {
        IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
        // warning has already been reported
        if (introspectedColumn != null) {
            if (gk.isJdbcStandard()) {
                //$NON-NLS-1$ //$NON-NLS-2$
                answer.addAttribute(new Attribute("useGeneratedKeys", "true"));
                //$NON-NLS-1$
                answer.addAttribute(new Attribute("keyProperty", introspectedColumn.getJavaProperty()));
                //$NON-NLS-1$
                answer.addAttribute(new Attribute("keyColumn", introspectedColumn.getActualColumnName()));
            } else {
                answer.addElement(getSelectKey(introspectedColumn, gk));
            }
        }
    }
    StringBuilder sb = new StringBuilder();
    //$NON-NLS-1$
    sb.append("insert into ");
    sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
    answer.addElement(new TextElement(sb.toString()));
    //$NON-NLS-1$
    XmlElement insertTrimElement = new XmlElement("trim");
    //$NON-NLS-1$ //$NON-NLS-2$
    insertTrimElement.addAttribute(new Attribute("prefix", "("));
    //$NON-NLS-1$ //$NON-NLS-2$
    insertTrimElement.addAttribute(new Attribute("suffix", ")"));
    //$NON-NLS-1$ //$NON-NLS-2$
    insertTrimElement.addAttribute(new Attribute("suffixOverrides", ","));
    answer.addElement(insertTrimElement);
    //$NON-NLS-1$
    XmlElement valuesTrimElement = new XmlElement("trim");
    //$NON-NLS-1$ //$NON-NLS-2$
    valuesTrimElement.addAttribute(new Attribute("prefix", "values ("));
    //$NON-NLS-1$ //$NON-NLS-2$
    valuesTrimElement.addAttribute(new Attribute("suffix", ")"));
    //$NON-NLS-1$ //$NON-NLS-2$
    valuesTrimElement.addAttribute(new Attribute("suffixOverrides", ","));
    answer.addElement(valuesTrimElement);
    for (IntrospectedColumn introspectedColumn : ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns())) {
        if (introspectedColumn.isSequenceColumn() || introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
            // if it is a sequence column, it is not optional
            // This is required for MyBatis3 because MyBatis3 parses
            // and calculates the SQL before executing the selectKey
            // if it is primitive, we cannot do a null check
            sb.setLength(0);
            sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
            sb.append(',');
            insertTrimElement.addElement(new TextElement(sb.toString()));
            sb.setLength(0);
            sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
            sb.append(',');
            valuesTrimElement.addElement(new TextElement(sb.toString()));
            continue;
        }
        //$NON-NLS-1$
        XmlElement insertNotNullElement = new XmlElement("if");
        sb.setLength(0);
        sb.append(introspectedColumn.getJavaProperty());
        //$NON-NLS-1$
        sb.append(" != null");
        insertNotNullElement.addAttribute(new Attribute("test", //$NON-NLS-1$
        sb.toString()));
        sb.setLength(0);
        sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
        sb.append(',');
        insertNotNullElement.addElement(new TextElement(sb.toString()));
        insertTrimElement.addElement(insertNotNullElement);
        //$NON-NLS-1$
        XmlElement valuesNotNullElement = new XmlElement("if");
        sb.setLength(0);
        sb.append(introspectedColumn.getJavaProperty());
        //$NON-NLS-1$
        sb.append(" != null");
        valuesNotNullElement.addAttribute(new Attribute("test", //$NON-NLS-1$
        sb.toString()));
        sb.setLength(0);
        sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
        sb.append(',');
        valuesNotNullElement.addElement(new TextElement(sb.toString()));
        valuesTrimElement.addElement(valuesNotNullElement);
    }
    if (context.getPlugins().sqlMapInsertSelectiveElementGenerated(answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
Also used : GeneratedKey(org.mybatis.generator.config.GeneratedKey) IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) TextElement(org.mybatis.generator.api.dom.xml.TextElement) Attribute(org.mybatis.generator.api.dom.xml.Attribute) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) XmlElement(org.mybatis.generator.api.dom.xml.XmlElement)

Aggregations

TextElement (org.mybatis.generator.api.dom.xml.TextElement)45 XmlElement (org.mybatis.generator.api.dom.xml.XmlElement)44 Attribute (org.mybatis.generator.api.dom.xml.Attribute)40 IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)27 FullyQualifiedJavaType (org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)5 ArrayList (java.util.ArrayList)4 GeneratedKey (org.mybatis.generator.config.GeneratedKey)4 Date (java.util.Date)2 GeneratedXmlFile (org.mybatis.generator.api.GeneratedXmlFile)2 Document (org.mybatis.generator.api.dom.xml.Document)2 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)2