Search in sources :

Example 91 with IntrospectedColumn

use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.

the class KotlinDynamicSqlSupportClassGenerator method generate.

private void generate() {
    FullyQualifiedJavaType type = new FullyQualifiedJavaType(introspectedTable.getMyBatisDynamicSqlSupportType());
    kotlinFile = buildBasicFile(type);
    outerObject = buildOuterObject(kotlinFile, type);
    tableProperty = calculateTableProperty();
    outerObject.addNamedItem(tableProperty);
    innerClass = buildInnerClass();
    List<IntrospectedColumn> columns = introspectedTable.getAllColumns();
    for (IntrospectedColumn column : columns) {
        handleColumn(kotlinFile, outerObject, innerClass, getTablePropertyName(), column);
    }
    outerObject.addNamedItem(innerClass);
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)

Example 92 with IntrospectedColumn

use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.

the class EqualsHashCodePlugin method generateHashCode.

/**
 * Generates a <code>hashCode</code> method that includes all fields.
 *
 * <p>Note that this implementation is based on the eclipse foundation hashCode
 * generator.
 *
 * @param topLevelClass
 *            the class to which the method will be added
 * @param introspectedColumns
 *            column definitions of this class and any superclass of this
 *            class
 * @param introspectedTable
 *            the table corresponding to this class
 */
protected void generateHashCode(TopLevelClass topLevelClass, List<IntrospectedColumn> introspectedColumns, IntrospectedTable introspectedTable) {
    // $NON-NLS-1$
    Method method = new Method("hashCode");
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getIntInstance());
    // $NON-NLS-1$
    method.addAnnotation("@Override");
    if (introspectedTable.getTargetRuntime() == TargetRuntime.MYBATIS3_DSQL) {
        context.getCommentGenerator().addGeneralMethodAnnotation(method, introspectedTable, topLevelClass.getImportedTypes());
    } else {
        context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
    }
    // $NON-NLS-1$
    method.addBodyLine("final int prime = 31;");
    // $NON-NLS-1$
    method.addBodyLine("int result = 1;");
    if (useEqualsHashCodeFromRoot && topLevelClass.getSuperClass().isPresent()) {
        // $NON-NLS-1$
        method.addBodyLine("result = prime * result + super.hashCode();");
    }
    StringBuilder sb = new StringBuilder();
    boolean hasTemp = false;
    for (IntrospectedColumn introspectedColumn : introspectedColumns) {
        FullyQualifiedJavaType fqjt = introspectedColumn.getFullyQualifiedJavaType();
        String getterMethod = getGetterMethodName(introspectedColumn.getJavaProperty(), fqjt);
        sb.setLength(0);
        if (fqjt.isPrimitive()) {
            if ("boolean".equals(fqjt.getFullyQualifiedName())) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                sb.append("result = prime * result + (");
                sb.append(getterMethod);
                // $NON-NLS-1$
                sb.append("() ? 1231 : 1237);");
                method.addBodyLine(sb.toString());
            } else if ("byte".equals(fqjt.getFullyQualifiedName())) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                sb.append("result = prime * result + ");
                sb.append(getterMethod);
                // $NON-NLS-1$
                sb.append("();");
                method.addBodyLine(sb.toString());
            } else if ("char".equals(fqjt.getFullyQualifiedName())) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                sb.append("result = prime * result + ");
                sb.append(getterMethod);
                // $NON-NLS-1$
                sb.append("();");
                method.addBodyLine(sb.toString());
            } else if ("double".equals(fqjt.getFullyQualifiedName())) {
                // $NON-NLS-1$
                if (!hasTemp) {
                    // $NON-NLS-1$
                    method.addBodyLine("long temp;");
                    hasTemp = true;
                }
                // $NON-NLS-1$
                sb.append("temp = Double.doubleToLongBits(");
                sb.append(getterMethod);
                // $NON-NLS-1$
                sb.append("());");
                method.addBodyLine(sb.toString());
                method.addBodyLine(// $NON-NLS-1$
                "result = prime * result + (int) (temp ^ (temp >>> 32));");
            } else if ("float".equals(fqjt.getFullyQualifiedName())) {
                // $NON-NLS-1$
                sb.append(// $NON-NLS-1$
                "result = prime * result + Float.floatToIntBits(");
                sb.append(getterMethod);
                // $NON-NLS-1$
                sb.append("());");
                method.addBodyLine(sb.toString());
            } else if ("int".equals(fqjt.getFullyQualifiedName())) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                sb.append("result = prime * result + ");
                sb.append(getterMethod);
                // $NON-NLS-1$
                sb.append("();");
                method.addBodyLine(sb.toString());
            } else if ("long".equals(fqjt.getFullyQualifiedName())) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                sb.append("result = prime * result + (int) (");
                sb.append(getterMethod);
                // $NON-NLS-1$
                sb.append("() ^ (");
                sb.append(getterMethod);
                // $NON-NLS-1$
                sb.append("() >>> 32));");
                method.addBodyLine(sb.toString());
            } else if ("short".equals(fqjt.getFullyQualifiedName())) {
                // $NON-NLS-1$
                // $NON-NLS-1$
                sb.append("result = prime * result + ");
                sb.append(getterMethod);
                // $NON-NLS-1$
                sb.append("();");
                method.addBodyLine(sb.toString());
            }
        } else if (fqjt.isArray()) {
            // Arrays is already imported by the generateEquals method, we don't need
            // to do it again
            // $NON-NLS-1$
            sb.append("result = prime * result + (Arrays.hashCode(");
            sb.append(getterMethod);
            // $NON-NLS-1$
            sb.append("()));");
            method.addBodyLine(sb.toString());
        } else {
            // $NON-NLS-1$
            sb.append("result = prime * result + ((");
            sb.append(getterMethod);
            // $NON-NLS-1$
            sb.append("() == null) ? 0 : ");
            sb.append(getterMethod);
            // $NON-NLS-1$
            sb.append("().hashCode());");
            method.addBodyLine(sb.toString());
        }
    }
    // $NON-NLS-1$
    method.addBodyLine("return result;");
    topLevelClass.addMethod(method);
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) Method(org.mybatis.generator.api.dom.java.Method)

Example 93 with IntrospectedColumn

use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.

the class EqualsHashCodePlugin method generateEquals.

/**
 * Generates an <code>equals</code> method that does a comparison of all fields.
 *
 * <p>The generated <code>equals</code> method will be correct unless:
 *
 * <ul>
 *   <li>Other fields have been added to the generated classes</li>
 *   <li>A <code>rootClass</code> is specified that holds state</li>
 * </ul>
 *
 * @param topLevelClass
 *            the class to which the method will be added
 * @param introspectedColumns
 *            column definitions of this class and any superclass of this
 *            class
 * @param introspectedTable
 *            the table corresponding to this class
 */
protected void generateEquals(TopLevelClass topLevelClass, List<IntrospectedColumn> introspectedColumns, IntrospectedTable introspectedTable) {
    // $NON-NLS-1$
    Method method = new Method("equals");
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
    method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), // $NON-NLS-1$
    "that"));
    // $NON-NLS-1$
    method.addAnnotation("@Override");
    if (introspectedTable.getTargetRuntime() == TargetRuntime.MYBATIS3_DSQL) {
        context.getCommentGenerator().addGeneralMethodAnnotation(method, introspectedTable, topLevelClass.getImportedTypes());
    } else {
        context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
    }
    // $NON-NLS-1$
    method.addBodyLine("if (this == that) {");
    // $NON-NLS-1$
    method.addBodyLine("return true;");
    // $NON-NLS-1$
    method.addBodyLine("}");
    // $NON-NLS-1$
    method.addBodyLine("if (that == null) {");
    // $NON-NLS-1$
    method.addBodyLine("return false;");
    // $NON-NLS-1$
    method.addBodyLine("}");
    // $NON-NLS-1$
    method.addBodyLine("if (getClass() != that.getClass()) {");
    // $NON-NLS-1$
    method.addBodyLine("return false;");
    // $NON-NLS-1$
    method.addBodyLine("}");
    StringBuilder sb = new StringBuilder();
    sb.append(topLevelClass.getType().getShortName());
    // $NON-NLS-1$
    sb.append(" other = (");
    sb.append(topLevelClass.getType().getShortName());
    // $NON-NLS-1$
    sb.append(") that;");
    method.addBodyLine(sb.toString());
    if (useEqualsHashCodeFromRoot && topLevelClass.getSuperClass().isPresent()) {
        // $NON-NLS-1$
        method.addBodyLine("if (!super.equals(other)) {");
        // $NON-NLS-1$
        method.addBodyLine("return false;");
        // $NON-NLS-1$
        method.addBodyLine("}");
    }
    boolean first = true;
    Iterator<IntrospectedColumn> iter = introspectedColumns.iterator();
    while (iter.hasNext()) {
        IntrospectedColumn introspectedColumn = iter.next();
        sb.setLength(0);
        if (first) {
            // $NON-NLS-1$
            sb.append("return (");
            first = false;
        } else {
            OutputUtilities.javaIndent(sb, 1);
            // $NON-NLS-1$
            sb.append("&& (");
        }
        String getterMethod = getGetterMethodName(introspectedColumn.getJavaProperty(), introspectedColumn.getFullyQualifiedJavaType());
        if (introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
            // $NON-NLS-1$
            sb.append("this.");
            sb.append(getterMethod);
            // $NON-NLS-1$
            sb.append("() == ");
            // $NON-NLS-1$
            sb.append("other.");
            sb.append(getterMethod);
            // $NON-NLS-1$
            sb.append("())");
        } else if (introspectedColumn.getFullyQualifiedJavaType().isArray()) {
            // $NON-NLS-1$
            topLevelClass.addImportedType("java.util.Arrays");
            // $NON-NLS-1$
            sb.append("Arrays.equals(this.");
            sb.append(getterMethod);
            // $NON-NLS-1$
            sb.append("(), ");
            // $NON-NLS-1$
            sb.append("other.");
            sb.append(getterMethod);
            // $NON-NLS-1$
            sb.append("()))");
        } else {
            // $NON-NLS-1$
            sb.append("this.");
            sb.append(getterMethod);
            // $NON-NLS-1$
            sb.append("() == null ? other.");
            sb.append(getterMethod);
            // $NON-NLS-1$
            sb.append("() == null : this.");
            sb.append(getterMethod);
            // $NON-NLS-1$
            sb.append("().equals(other.");
            sb.append(getterMethod);
            // $NON-NLS-1$
            sb.append("()))");
        }
        if (!iter.hasNext()) {
            sb.append(';');
        }
        method.addBodyLine(sb.toString());
    }
    topLevelClass.addMethod(method);
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) Parameter(org.mybatis.generator.api.dom.java.Parameter) Method(org.mybatis.generator.api.dom.java.Method)

Example 94 with IntrospectedColumn

use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.

the class InsertElementGenerator method addElements.

@Override
public void addElements(XmlElement parentElement) {
    FullyQualifiedJavaType parameterType;
    if (isSimple) {
        parameterType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
    } else {
        parameterType = introspectedTable.getRules().calculateAllFieldsClass();
    }
    XmlElement answer = buildInitialInsert(introspectedTable.getInsertStatementId(), parameterType);
    StringBuilder insertClause = new StringBuilder();
    // $NON-NLS-1$
    insertClause.append("insert into ");
    insertClause.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
    // $NON-NLS-1$
    insertClause.append(" (");
    StringBuilder valuesClause = new StringBuilder();
    // $NON-NLS-1$
    valuesClause.append("values (");
    List<String> valuesClauses = new ArrayList<>();
    List<IntrospectedColumn> columns = ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns());
    for (int i = 0; i < columns.size(); i++) {
        IntrospectedColumn introspectedColumn = columns.get(i);
        insertClause.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
        valuesClause.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
        if (i + 1 < columns.size()) {
            // $NON-NLS-1$
            insertClause.append(", ");
            // $NON-NLS-1$
            valuesClause.append(", ");
        }
        if (valuesClause.length() > 80) {
            answer.addElement(new TextElement(insertClause.toString()));
            insertClause.setLength(0);
            OutputUtilities.xmlIndent(insertClause, 1);
            valuesClauses.add(valuesClause.toString());
            valuesClause.setLength(0);
            OutputUtilities.xmlIndent(valuesClause, 1);
        }
    }
    insertClause.append(')');
    answer.addElement(new TextElement(insertClause.toString()));
    valuesClause.append(')');
    valuesClauses.add(valuesClause.toString());
    for (String clause : valuesClauses) {
        answer.addElement(new TextElement(clause));
    }
    if (context.getPlugins().sqlMapInsertElementGenerated(answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) TextElement(org.mybatis.generator.api.dom.xml.TextElement) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) ArrayList(java.util.ArrayList) XmlElement(org.mybatis.generator.api.dom.xml.XmlElement)

Example 95 with IntrospectedColumn

use of org.mybatis.generator.api.IntrospectedColumn in project generator by mybatis.

the class UpdateByPrimaryKeySelectiveElementGenerator method addElements.

@Override
public void addElements(XmlElement parentElement) {
    // $NON-NLS-1$
    XmlElement answer = new XmlElement("update");
    answer.addAttribute(new Attribute("id", // $NON-NLS-1$
    introspectedTable.getUpdateByPrimaryKeySelectiveStatementId()));
    String parameterType;
    if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
        parameterType = introspectedTable.getRecordWithBLOBsType();
    } else {
        parameterType = introspectedTable.getBaseRecordType();
    }
    // $NON-NLS-1$
    answer.addAttribute(new Attribute("parameterType", parameterType));
    context.getCommentGenerator().addComment(answer);
    StringBuilder sb = new StringBuilder();
    // $NON-NLS-1$
    sb.append("update ");
    sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
    answer.addElement(new TextElement(sb.toString()));
    // $NON-NLS-1$
    XmlElement dynamicElement = new XmlElement("set");
    answer.addElement(dynamicElement);
    for (IntrospectedColumn introspectedColumn : ListUtilities.removeGeneratedAlwaysColumns(introspectedTable.getNonPrimaryKeyColumns())) {
        sb.setLength(0);
        sb.append(introspectedColumn.getJavaProperty());
        // $NON-NLS-1$
        sb.append(" != null");
        // $NON-NLS-1$
        XmlElement isNotNullElement = new XmlElement("if");
        // $NON-NLS-1$
        isNotNullElement.addAttribute(new Attribute("test", sb.toString()));
        dynamicElement.addElement(isNotNullElement);
        sb.setLength(0);
        sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
        // $NON-NLS-1$
        sb.append(" = ");
        sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
        sb.append(',');
        isNotNullElement.addElement(new TextElement(sb.toString()));
    }
    buildPrimaryKeyWhereClause().forEach(answer::addElement);
    if (context.getPlugins().sqlMapUpdateByPrimaryKeySelectiveElementGenerated(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)

Aggregations

IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)107 FullyQualifiedJavaType (org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)36 Method (org.mybatis.generator.api.dom.java.Method)33 XmlElement (org.mybatis.generator.api.dom.xml.XmlElement)32 Attribute (org.mybatis.generator.api.dom.xml.Attribute)28 ArrayList (java.util.ArrayList)26 Parameter (org.mybatis.generator.api.dom.java.Parameter)24 TextElement (org.mybatis.generator.api.dom.xml.TextElement)24 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)14 Field (org.mybatis.generator.api.dom.java.Field)11 FullyQualifiedTable (org.mybatis.generator.api.FullyQualifiedTable)10 CommentGenerator (org.mybatis.generator.api.CommentGenerator)9 TopLevelClass (org.mybatis.generator.api.dom.java.TopLevelClass)9 Plugin (org.mybatis.generator.api.Plugin)8 CompilationUnit (org.mybatis.generator.api.dom.java.CompilationUnit)8 JavaBeansUtil.getJavaBeansField (org.mybatis.generator.internal.util.JavaBeansUtil.getJavaBeansField)7 HashSet (java.util.HashSet)6 List (java.util.List)6 HashMap (java.util.HashMap)5 KotlinFunction (org.mybatis.generator.api.dom.kotlin.KotlinFunction)5