Search in sources :

Example 31 with IntrospectedColumn

use of org.mybatis.generator.api.IntrospectedColumn 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 32 with IntrospectedColumn

use of org.mybatis.generator.api.IntrospectedColumn 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 33 with IntrospectedColumn

use of org.mybatis.generator.api.IntrospectedColumn 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 34 with IntrospectedColumn

use of org.mybatis.generator.api.IntrospectedColumn 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 35 with IntrospectedColumn

use of org.mybatis.generator.api.IntrospectedColumn 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

IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)82 XmlElement (org.mybatis.generator.api.dom.xml.XmlElement)34 FullyQualifiedJavaType (org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)32 Attribute (org.mybatis.generator.api.dom.xml.Attribute)30 Method (org.mybatis.generator.api.dom.java.Method)29 TextElement (org.mybatis.generator.api.dom.xml.TextElement)27 ArrayList (java.util.ArrayList)17 Parameter (org.mybatis.generator.api.dom.java.Parameter)17 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)12 TreeSet (java.util.TreeSet)9 Field (org.mybatis.generator.api.dom.java.Field)9 FullyQualifiedTable (org.mybatis.generator.api.FullyQualifiedTable)8 CommentGenerator (org.mybatis.generator.api.CommentGenerator)7 Plugin (org.mybatis.generator.api.Plugin)7 CompilationUnit (org.mybatis.generator.api.dom.java.CompilationUnit)7 TopLevelClass (org.mybatis.generator.api.dom.java.TopLevelClass)7 JavaBeansUtil.getJavaBeansField (org.mybatis.generator.internal.util.JavaBeansUtil.getJavaBeansField)7 GeneratedKey (org.mybatis.generator.config.GeneratedKey)6 HashMap (java.util.HashMap)5 List (java.util.List)5