Search in sources :

Example 66 with IntrospectedColumn

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

the class SelectByPrimaryKeyElementGenerator method addElements.

@Override
public void addElements(XmlElement parentElement) {
    //$NON-NLS-1$
    XmlElement answer = new XmlElement("select");
    answer.addAttribute(new Attribute("id", //$NON-NLS-1$
    introspectedTable.getSelectByPrimaryKeyStatementId()));
    if (introspectedTable.getRules().generateResultMapWithBLOBs()) {
        answer.addAttribute(new //$NON-NLS-1$
        Attribute(//$NON-NLS-1$
        "resultMap", introspectedTable.getResultMapWithBLOBsId()));
    } else {
        answer.addAttribute(new //$NON-NLS-1$
        Attribute(//$NON-NLS-1$
        "resultMap", introspectedTable.getBaseResultMapId()));
    }
    String parameterType;
    if (introspectedTable.getRules().generatePrimaryKeyClass()) {
        parameterType = introspectedTable.getPrimaryKeyType();
    } else {
        // field, then they are coming in a map.
        if (introspectedTable.getPrimaryKeyColumns().size() > 1) {
            //$NON-NLS-1$
            parameterType = "map";
        } else {
            parameterType = introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType().toString();
        }
    }
    answer.addAttribute(new //$NON-NLS-1$
    Attribute(//$NON-NLS-1$
    "parameterType", parameterType));
    context.getCommentGenerator().addComment(answer);
    StringBuilder sb = new StringBuilder();
    //$NON-NLS-1$
    sb.append("select ");
    if (stringHasValue(introspectedTable.getSelectByPrimaryKeyQueryId())) {
        sb.append('\'');
        sb.append(introspectedTable.getSelectByPrimaryKeyQueryId());
        //$NON-NLS-1$
        sb.append("' as QUERYID,");
    }
    answer.addElement(new TextElement(sb.toString()));
    answer.addElement(getBaseColumnListElement());
    if (introspectedTable.hasBLOBColumns()) {
        //$NON-NLS-1$
        answer.addElement(new TextElement(","));
        answer.addElement(getBlobColumnListElement());
    }
    sb.setLength(0);
    //$NON-NLS-1$
    sb.append("from ");
    sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
    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.getAliasedEscapedColumnName(introspectedColumn));
        //$NON-NLS-1$
        sb.append(" = ");
        sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
        answer.addElement(new TextElement(sb.toString()));
    }
    if (context.getPlugins().sqlMapSelectByPrimaryKeyElementGenerated(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 67 with IntrospectedColumn

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

the class SimpleSelectAllElementGenerator method addElements.

@Override
public void addElements(XmlElement parentElement) {
    //$NON-NLS-1$
    XmlElement answer = new XmlElement("select");
    answer.addAttribute(new Attribute("id", //$NON-NLS-1$
    introspectedTable.getSelectAllStatementId()));
    answer.addAttribute(new //$NON-NLS-1$
    Attribute(//$NON-NLS-1$
    "resultMap", introspectedTable.getBaseResultMapId()));
    context.getCommentGenerator().addComment(answer);
    StringBuilder sb = new StringBuilder();
    //$NON-NLS-1$
    sb.append("select ");
    Iterator<IntrospectedColumn> iter = introspectedTable.getAllColumns().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()));
    }
    sb.setLength(0);
    //$NON-NLS-1$
    sb.append("from ");
    sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
    answer.addElement(new TextElement(sb.toString()));
    String orderByClause = introspectedTable.getTableConfigurationProperty(PropertyRegistry.TABLE_SELECT_ALL_ORDER_BY_CLAUSE);
    boolean hasOrderBy = StringUtility.stringHasValue(orderByClause);
    if (hasOrderBy) {
        sb.setLength(0);
        //$NON-NLS-1$
        sb.append("order by ");
        sb.append(orderByClause);
        answer.addElement(new TextElement(sb.toString()));
    }
    if (context.getPlugins().sqlMapSelectAllElementGenerated(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 68 with IntrospectedColumn

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

the class UpdateByExampleSelectiveElementGenerator 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.getUpdateByExampleSelectiveStatementId()));
    //$NON-NLS-1$ //$NON-NLS-2$
    answer.addAttribute(new Attribute("parameterType", "map"));
    context.getCommentGenerator().addComment(answer);
    StringBuilder sb = new StringBuilder();
    //$NON-NLS-1$
    sb.append("update ");
    sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
    answer.addElement(new TextElement(sb.toString()));
    //$NON-NLS-1$
    XmlElement dynamicElement = new XmlElement("set");
    answer.addElement(dynamicElement);
    for (IntrospectedColumn introspectedColumn : ListUtilities.removeGeneratedAlwaysColumns(introspectedTable.getAllColumns())) {
        //$NON-NLS-1$
        XmlElement isNotNullElement = new XmlElement("if");
        sb.setLength(0);
        //$NON-NLS-1$
        sb.append(introspectedColumn.getJavaProperty("record."));
        //$NON-NLS-1$
        sb.append(" != null");
        //$NON-NLS-1$
        isNotNullElement.addAttribute(new Attribute("test", sb.toString()));
        dynamicElement.addElement(isNotNullElement);
        sb.setLength(0);
        sb.append(MyBatis3FormattingUtilities.getAliasedEscapedColumnName(introspectedColumn));
        //$NON-NLS-1$
        sb.append(" = ");
        sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn, //$NON-NLS-1$
        "record."));
        sb.append(',');
        isNotNullElement.addElement(new TextElement(sb.toString()));
    }
    answer.addElement(getUpdateByExampleIncludeElement());
    if (context.getPlugins().sqlMapUpdateByExampleSelectiveElementGenerated(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 69 with IntrospectedColumn

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

the class CaseInsensitiveLikePlugin method modelExampleClassGenerated.

@Override
public boolean modelExampleClassGenerated(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
    InnerClass criteria = null;
    // first, find the Criteria inner class
    for (InnerClass innerClass : topLevelClass.getInnerClasses()) {
        if ("GeneratedCriteria".equals(innerClass.getType().getShortName())) {
            //$NON-NLS-1$
            criteria = innerClass;
            break;
        }
    }
    if (criteria == null) {
        // can't find the inner class for some reason, bail out.
        return true;
    }
    for (IntrospectedColumn introspectedColumn : introspectedTable.getNonBLOBColumns()) {
        if (!introspectedColumn.isJdbcCharacterColumn() || !introspectedColumn.isStringColumn()) {
            continue;
        }
        Method method = new Method();
        method.setVisibility(JavaVisibility.PUBLIC);
        method.addParameter(new Parameter(introspectedColumn.getFullyQualifiedJavaType(), //$NON-NLS-1$
        "value"));
        StringBuilder sb = new StringBuilder();
        sb.append(introspectedColumn.getJavaProperty());
        sb.setCharAt(0, Character.toUpperCase(sb.charAt(0)));
        //$NON-NLS-1$
        sb.insert(0, "and");
        //$NON-NLS-1$
        sb.append("LikeInsensitive");
        method.setName(sb.toString());
        method.setReturnType(FullyQualifiedJavaType.getCriteriaInstance());
        sb.setLength(0);
        //$NON-NLS-1$
        sb.append("addCriterion(\"upper(");
        sb.append(Ibatis2FormattingUtilities.getAliasedActualColumnName(introspectedColumn));
        //$NON-NLS-1$
        sb.append(") like\", value.toUpperCase(), \"");
        sb.append(introspectedColumn.getJavaProperty());
        //$NON-NLS-1$
        sb.append("\");");
        method.addBodyLine(sb.toString());
        //$NON-NLS-1$
        method.addBodyLine("return (Criteria) this;");
        criteria.addMethod(method);
    }
    return true;
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) InnerClass(org.mybatis.generator.api.dom.java.InnerClass) Parameter(org.mybatis.generator.api.dom.java.Parameter) Method(org.mybatis.generator.api.dom.java.Method)

Example 70 with IntrospectedColumn

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

the class EqualsHashCodePlugin method generateEquals.

/**
     * Generates an <tt>equals</tt> method that does a comparison of all fields.
     * <p>
     * The generated <tt>equals</tt> method will be correct unless:
     * <ul>
     * <li>Other fields have been added to the generated classes</li>
     * <li>A <tt>rootClass</tt> 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) {
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getBooleanPrimitiveInstance());
    //$NON-NLS-1$
    method.setName("equals");
    method.addParameter(new Parameter(FullyQualifiedJavaType.getObjectInstance(), //$NON-NLS-1$
    "that"));
    if (introspectedTable.isJava5Targeted()) {
        //$NON-NLS-1$
        method.addAnnotation("@Override");
    }
    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() != null) {
        //$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)

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