Search in sources :

Example 11 with Attribute

use of org.mybatis.generator.api.dom.xml.Attribute 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 12 with Attribute

use of org.mybatis.generator.api.dom.xml.Attribute 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)

Example 13 with Attribute

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

the class ResultMapWithBLOBsElementGenerator method addResultMapElements.

private void addResultMapElements(XmlElement answer) {
    for (IntrospectedColumn introspectedColumn : introspectedTable.getBLOBColumns()) {
        //$NON-NLS-1$
        XmlElement resultElement = new XmlElement("result");
        resultElement.addAttribute(new Attribute("column", //$NON-NLS-1$
        MyBatis3FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn)));
        resultElement.addAttribute(new Attribute("property", //$NON-NLS-1$
        introspectedColumn.getJavaProperty()));
        resultElement.addAttribute(new Attribute("jdbcType", //$NON-NLS-1$
        introspectedColumn.getJdbcTypeName()));
        if (stringHasValue(introspectedColumn.getTypeHandler())) {
            resultElement.addAttribute(new Attribute("typeHandler", //$NON-NLS-1$
            introspectedColumn.getTypeHandler()));
        }
        answer.addElement(resultElement);
    }
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) Attribute(org.mybatis.generator.api.dom.xml.Attribute) XmlElement(org.mybatis.generator.api.dom.xml.XmlElement)

Example 14 with Attribute

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

the class ResultMapWithBLOBsElementGenerator method addResultMapConstructorElements.

private void addResultMapConstructorElements(XmlElement answer) {
    //$NON-NLS-1$
    XmlElement constructor = new XmlElement("constructor");
    for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
        //$NON-NLS-1$
        XmlElement resultElement = new XmlElement("idArg");
        resultElement.addAttribute(new Attribute("column", //$NON-NLS-1$
        MyBatis3FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn)));
        resultElement.addAttribute(new Attribute("jdbcType", //$NON-NLS-1$
        introspectedColumn.getJdbcTypeName()));
        resultElement.addAttribute(new //$NON-NLS-1$
        Attribute(//$NON-NLS-1$
        "javaType", introspectedColumn.getFullyQualifiedJavaType().getFullyQualifiedName()));
        if (stringHasValue(introspectedColumn.getTypeHandler())) {
            resultElement.addAttribute(new Attribute("typeHandler", //$NON-NLS-1$
            introspectedColumn.getTypeHandler()));
        }
        constructor.addElement(resultElement);
    }
    for (IntrospectedColumn introspectedColumn : introspectedTable.getNonPrimaryKeyColumns()) {
        //$NON-NLS-1$
        XmlElement resultElement = new XmlElement("arg");
        resultElement.addAttribute(new Attribute("column", //$NON-NLS-1$
        MyBatis3FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn)));
        resultElement.addAttribute(new Attribute("jdbcType", //$NON-NLS-1$
        introspectedColumn.getJdbcTypeName()));
        if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
            // need to use the MyBatis type alias for a primitive byte
            StringBuilder sb = new StringBuilder();
            sb.append('_');
            sb.append(introspectedColumn.getFullyQualifiedJavaType().getShortName());
            resultElement.addAttribute(new //$NON-NLS-1$
            Attribute(//$NON-NLS-1$
            "javaType", sb.toString()));
        } else if ("byte[]".equals(//$NON-NLS-1$
        introspectedColumn.getFullyQualifiedJavaType().getFullyQualifiedName())) {
            // need to use the MyBatis type alias for a primitive byte arry
            resultElement.addAttribute(new //$NON-NLS-1$
            Attribute(//$NON-NLS-1$
            "javaType", //$NON-NLS-1$
            "_byte[]"));
        } else {
            resultElement.addAttribute(new //$NON-NLS-1$
            Attribute(//$NON-NLS-1$
            "javaType", introspectedColumn.getFullyQualifiedJavaType().getFullyQualifiedName()));
        }
        if (stringHasValue(introspectedColumn.getTypeHandler())) {
            resultElement.addAttribute(new Attribute("typeHandler", //$NON-NLS-1$
            introspectedColumn.getTypeHandler()));
        }
        constructor.addElement(resultElement);
    }
    answer.addElement(constructor);
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) Attribute(org.mybatis.generator.api.dom.xml.Attribute) XmlElement(org.mybatis.generator.api.dom.xml.XmlElement)

Example 15 with Attribute

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

the class ResultMapWithoutBLOBsElementGenerator method addResultMapConstructorElements.

private void addResultMapConstructorElements(XmlElement answer) {
    //$NON-NLS-1$
    XmlElement constructor = new XmlElement("constructor");
    for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
        //$NON-NLS-1$
        XmlElement resultElement = new XmlElement("idArg");
        resultElement.addAttribute(new Attribute("column", //$NON-NLS-1$
        MyBatis3FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn)));
        resultElement.addAttribute(new //$NON-NLS-1$
        Attribute(//$NON-NLS-1$
        "jdbcType", introspectedColumn.getJdbcTypeName()));
        resultElement.addAttribute(new //$NON-NLS-1$
        Attribute(//$NON-NLS-1$
        "javaType", introspectedColumn.getFullyQualifiedJavaType().getFullyQualifiedName()));
        if (stringHasValue(introspectedColumn.getTypeHandler())) {
            resultElement.addAttribute(new Attribute("typeHandler", //$NON-NLS-1$
            introspectedColumn.getTypeHandler()));
        }
        constructor.addElement(resultElement);
    }
    List<IntrospectedColumn> columns;
    if (isSimple) {
        columns = introspectedTable.getNonPrimaryKeyColumns();
    } else {
        columns = introspectedTable.getBaseColumns();
    }
    for (IntrospectedColumn introspectedColumn : columns) {
        //$NON-NLS-1$
        XmlElement resultElement = new XmlElement("arg");
        resultElement.addAttribute(new Attribute("column", //$NON-NLS-1$
        MyBatis3FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn)));
        resultElement.addAttribute(new //$NON-NLS-1$
        Attribute(//$NON-NLS-1$
        "jdbcType", introspectedColumn.getJdbcTypeName()));
        resultElement.addAttribute(new //$NON-NLS-1$
        Attribute(//$NON-NLS-1$
        "javaType", introspectedColumn.getFullyQualifiedJavaType().getFullyQualifiedName()));
        if (stringHasValue(introspectedColumn.getTypeHandler())) {
            resultElement.addAttribute(new Attribute("typeHandler", //$NON-NLS-1$
            introspectedColumn.getTypeHandler()));
        }
        constructor.addElement(resultElement);
    }
    answer.addElement(constructor);
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) Attribute(org.mybatis.generator.api.dom.xml.Attribute) XmlElement(org.mybatis.generator.api.dom.xml.XmlElement)

Aggregations

Attribute (org.mybatis.generator.api.dom.xml.Attribute)73 XmlElement (org.mybatis.generator.api.dom.xml.XmlElement)73 TextElement (org.mybatis.generator.api.dom.xml.TextElement)39 IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)30 FullyQualifiedJavaType (org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)5 ArrayList (java.util.ArrayList)4 Document (org.mybatis.generator.api.dom.xml.Document)4 GeneratedKey (org.mybatis.generator.config.GeneratedKey)4 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)3 Date (java.util.Date)2 GeneratedXmlFile (org.mybatis.generator.api.GeneratedXmlFile)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1