Search in sources :

Example 21 with IntrospectedColumn

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

the class ProviderUpdateByExampleSelectiveMethodGenerator method addClassElements.

@Override
public void addClassElements(TopLevelClass topLevelClass) {
    Set<String> staticImports = new TreeSet<String>();
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    if (useLegacyBuilder) {
        //$NON-NLS-1$
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.BEGIN");
        //$NON-NLS-1$
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.UPDATE");
        //$NON-NLS-1$
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SET");
        //$NON-NLS-1$
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SQL");
    } else {
        importedTypes.add(NEW_BUILDER_IMPORT);
    }
    //$NON-NLS-1$
    importedTypes.add(new FullyQualifiedJavaType("java.util.Map"));
    Method method = new Method(introspectedTable.getUpdateByExampleSelectiveStatementId());
    method.setReturnType(FullyQualifiedJavaType.getStringInstance());
    method.setVisibility(JavaVisibility.PUBLIC);
    method.addParameter(new //$NON-NLS-1$
    Parameter(//$NON-NLS-1$
    new FullyQualifiedJavaType("java.util.Map<java.lang.String, java.lang.Object>"), //$NON-NLS-1$
    "parameter"));
    FullyQualifiedJavaType record = introspectedTable.getRules().calculateAllFieldsClass();
    importedTypes.add(record);
    method.addBodyLine(//$NON-NLS-1$
    String.format(//$NON-NLS-1$
    "%s record = (%s) parameter.get(\"record\");", record.getShortName(), record.getShortName()));
    FullyQualifiedJavaType example = new FullyQualifiedJavaType(introspectedTable.getExampleType());
    importedTypes.add(example);
    method.addBodyLine(//$NON-NLS-1$
    String.format(//$NON-NLS-1$
    "%s example = (%s) parameter.get(\"example\");", example.getShortName(), example.getShortName()));
    context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
    //$NON-NLS-1$
    method.addBodyLine("");
    if (useLegacyBuilder) {
        //$NON-NLS-1$
        method.addBodyLine("BEGIN();");
    } else {
        //$NON-NLS-1$
        method.addBodyLine("SQL sql = new SQL();");
    }
    method.addBodyLine(//$NON-NLS-1$
    String.format(//$NON-NLS-1$
    "%sUPDATE(\"%s\");", builderPrefix, escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
    //$NON-NLS-1$
    method.addBodyLine("");
    for (IntrospectedColumn introspectedColumn : ListUtilities.removeGeneratedAlwaysColumns(introspectedTable.getAllColumns())) {
        if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
            method.addBodyLine(//$NON-NLS-1$
            String.format(//$NON-NLS-1$
            "if (record.%s() != null) {", getGetterMethodName(introspectedColumn.getJavaProperty(), introspectedColumn.getFullyQualifiedJavaType())));
        }
        StringBuilder sb = new StringBuilder();
        sb.append(getParameterClause(introspectedColumn));
        //$NON-NLS-1$
        sb.insert(2, "record.");
        method.addBodyLine(//$NON-NLS-1$
        String.format(//$NON-NLS-1$
        "%sSET(\"%s = %s\");", builderPrefix, escapeStringForJava(getAliasedEscapedColumnName(introspectedColumn)), sb.toString()));
        if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
            //$NON-NLS-1$
            method.addBodyLine("}");
        }
        //$NON-NLS-1$
        method.addBodyLine("");
    }
    if (useLegacyBuilder) {
        //$NON-NLS-1$
        method.addBodyLine("applyWhere(example, true);");
        //$NON-NLS-1$
        method.addBodyLine("return SQL();");
    } else {
        //$NON-NLS-1$
        method.addBodyLine("applyWhere(sql, example, true);");
        //$NON-NLS-1$
        method.addBodyLine("return sql.toString();");
    }
    if (context.getPlugins().providerUpdateByExampleSelectiveMethodGenerated(method, topLevelClass, introspectedTable)) {
        topLevelClass.addStaticImports(staticImports);
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) TreeSet(java.util.TreeSet) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) Method(org.mybatis.generator.api.dom.java.Method)

Example 22 with IntrospectedColumn

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

the class ProviderUpdateByExampleWithoutBLOBsMethodGenerator method addClassElements.

@Override
public void addClassElements(TopLevelClass topLevelClass) {
    Set<String> staticImports = new TreeSet<String>();
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    if (useLegacyBuilder) {
        //$NON-NLS-1$
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.BEGIN");
        //$NON-NLS-1$
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.UPDATE");
        //$NON-NLS-1$
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SET");
        //$NON-NLS-1$
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.SQL");
    } else {
        importedTypes.add(NEW_BUILDER_IMPORT);
    }
    //$NON-NLS-1$
    importedTypes.add(new FullyQualifiedJavaType("java.util.Map"));
    Method method = new Method(getMethodName());
    method.setReturnType(FullyQualifiedJavaType.getStringInstance());
    method.setVisibility(JavaVisibility.PUBLIC);
    method.addParameter(new //$NON-NLS-1$
    Parameter(//$NON-NLS-1$
    new FullyQualifiedJavaType("java.util.Map<java.lang.String, java.lang.Object>"), //$NON-NLS-1$
    "parameter"));
    context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
    if (useLegacyBuilder) {
        //$NON-NLS-1$
        method.addBodyLine("BEGIN();");
    } else {
        //$NON-NLS-1$
        method.addBodyLine("SQL sql = new SQL();");
    }
    method.addBodyLine(//$NON-NLS-1$
    String.format(//$NON-NLS-1$
    "%sUPDATE(\"%s\");", builderPrefix, escapeStringForJava(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime())));
    //$NON-NLS-1$
    method.addBodyLine("");
    for (IntrospectedColumn introspectedColumn : ListUtilities.removeGeneratedAlwaysColumns(getColumns())) {
        StringBuilder sb = new StringBuilder();
        sb.append(getParameterClause(introspectedColumn));
        //$NON-NLS-1$
        sb.insert(2, "record.");
        method.addBodyLine(//$NON-NLS-1$
        String.format(//$NON-NLS-1$
        "%sSET(\"%s = %s\");", builderPrefix, escapeStringForJava(getAliasedEscapedColumnName(introspectedColumn)), sb.toString()));
    }
    //$NON-NLS-1$
    method.addBodyLine("");
    FullyQualifiedJavaType example = new FullyQualifiedJavaType(introspectedTable.getExampleType());
    importedTypes.add(example);
    method.addBodyLine(//$NON-NLS-1$
    String.format(//$NON-NLS-1$
    "%s example = (%s) parameter.get(\"example\");", example.getShortName(), example.getShortName()));
    if (useLegacyBuilder) {
        //$NON-NLS-1$
        method.addBodyLine("applyWhere(example, true);");
        //$NON-NLS-1$
        method.addBodyLine("return SQL();");
    } else {
        //$NON-NLS-1$
        method.addBodyLine("applyWhere(sql, example, true);");
        //$NON-NLS-1$
        method.addBodyLine("return sql.toString();");
    }
    if (callPlugins(method, topLevelClass)) {
        topLevelClass.addStaticImports(staticImports);
        topLevelClass.addImportedTypes(importedTypes);
        topLevelClass.addMethod(method);
    }
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) TreeSet(java.util.TreeSet) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) Method(org.mybatis.generator.api.dom.java.Method)

Example 23 with IntrospectedColumn

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

the class AnnotatedDeleteByPrimaryKeyMethodGenerator method addMapperAnnotations.

@Override
public void addMapperAnnotations(Method method) {
    //$NON-NLS-1$
    method.addAnnotation("@Delete({");
    StringBuilder sb = new StringBuilder();
    javaIndent(sb, 1);
    //$NON-NLS-1$
    sb.append("\"delete from ");
    sb.append(escapeStringForJava(introspectedTable.getFullyQualifiedTableNameAtRuntime()));
    //$NON-NLS-1$
    sb.append("\",");
    method.addAnnotation(sb.toString());
    boolean and = false;
    Iterator<IntrospectedColumn> iter = introspectedTable.getPrimaryKeyColumns().iterator();
    while (iter.hasNext()) {
        IntrospectedColumn introspectedColumn = iter.next();
        sb.setLength(0);
        javaIndent(sb, 1);
        if (and) {
            //$NON-NLS-1$
            sb.append("  \"and ");
        } else {
            //$NON-NLS-1$
            sb.append("\"where ");
            and = true;
        }
        sb.append(escapeStringForJava(getEscapedColumnName(introspectedColumn)));
        //$NON-NLS-1$
        sb.append(" = ");
        sb.append(getParameterClause(introspectedColumn));
        sb.append('\"');
        if (iter.hasNext()) {
            sb.append(',');
        }
        method.addAnnotation(sb.toString());
    }
    //$NON-NLS-1$
    method.addAnnotation("})");
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn)

Example 24 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$
    "parameterClass", parameterType.getFullyQualifiedName()));
    context.getCommentGenerator().addComment(answer);
    GeneratedKey gk = introspectedTable.getGeneratedKey();
    if (gk != null && gk.isPlacedBeforeInsertInIbatis2()) {
        IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
        // warning has already been reported
        if (introspectedColumn != null) {
            // pre-generated key
            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 insertElement = new XmlElement("dynamic");
    //$NON-NLS-1$ //$NON-NLS-2$
    insertElement.addAttribute(new Attribute("prepend", "("));
    answer.addElement(insertElement);
    //$NON-NLS-1$
    answer.addElement(new TextElement("values"));
    //$NON-NLS-1$
    XmlElement valuesElement = new XmlElement("dynamic");
    //$NON-NLS-1$ //$NON-NLS-2$
    valuesElement.addAttribute(new Attribute("prepend", "("));
    answer.addElement(valuesElement);
    for (IntrospectedColumn introspectedColumn : introspectedTable.getAllColumns()) {
        if (introspectedColumn.isIdentity()) {
            // cannot set values on identity fields
            continue;
        }
        //$NON-NLS-1$
        XmlElement insertNotNullElement = new XmlElement("isNotNull");
        //$NON-NLS-1$ //$NON-NLS-2$
        insertNotNullElement.addAttribute(new Attribute("prepend", ","));
        insertNotNullElement.addAttribute(new Attribute("property", //$NON-NLS-1$
        introspectedColumn.getJavaProperty()));
        insertNotNullElement.addElement(new TextElement(Ibatis2FormattingUtilities.getEscapedColumnName(introspectedColumn)));
        insertElement.addElement(insertNotNullElement);
        //$NON-NLS-1$
        XmlElement valuesNotNullElement = new XmlElement("isNotNull");
        //$NON-NLS-1$ //$NON-NLS-2$
        valuesNotNullElement.addAttribute(new Attribute("prepend", ","));
        valuesNotNullElement.addAttribute(new Attribute("property", //$NON-NLS-1$
        introspectedColumn.getJavaProperty()));
        valuesNotNullElement.addElement(new TextElement(Ibatis2FormattingUtilities.getParameterClause(introspectedColumn)));
        valuesElement.addElement(valuesNotNullElement);
    }
    //$NON-NLS-1$
    insertElement.addElement(new TextElement(")"));
    //$NON-NLS-1$
    valuesElement.addElement(new TextElement(")"));
    if (gk != null && !gk.isPlacedBeforeInsertInIbatis2()) {
        IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
        // warning has already been reported
        if (introspectedColumn != null) {
            // pre-generated key
            answer.addElement(getSelectKey(introspectedColumn, gk));
        }
    }
    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 25 with IntrospectedColumn

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

the class ResultMapWithoutBLOBsElementGenerator method addElements.

@Override
public void addElements(XmlElement parentElement) {
    boolean useColumnIndex = isTrue(introspectedTable.getTableConfigurationProperty(PropertyRegistry.TABLE_USE_COLUMN_INDEXES));
    //$NON-NLS-1$
    XmlElement answer = new XmlElement("resultMap");
    answer.addAttribute(new //$NON-NLS-1$
    Attribute(//$NON-NLS-1$
    "id", introspectedTable.getBaseResultMapId()));
    String returnType;
    if (introspectedTable.getRules().generateBaseRecordClass()) {
        returnType = introspectedTable.getBaseRecordType();
    } else {
        returnType = introspectedTable.getPrimaryKeyType();
    }
    answer.addAttribute(new //$NON-NLS-1$
    Attribute(//$NON-NLS-1$
    "class", returnType));
    context.getCommentGenerator().addComment(answer);
    int i = 1;
    if (stringHasValue(introspectedTable.getSelectByPrimaryKeyQueryId()) || stringHasValue(introspectedTable.getSelectByExampleQueryId())) {
        i++;
    }
    for (IntrospectedColumn introspectedColumn : introspectedTable.getNonBLOBColumns()) {
        //$NON-NLS-1$
        XmlElement resultElement = new XmlElement("result");
        if (useColumnIndex) {
            resultElement.addAttribute(new Attribute("columnIndex", //$NON-NLS-1$
            Integer.toString(i++)));
        } else {
            resultElement.addAttribute(new Attribute("column", //$NON-NLS-1$
            Ibatis2FormattingUtilities.getRenamedColumnNameForResultMap(introspectedColumn)));
        }
        resultElement.addAttribute(new Attribute("property", //$NON-NLS-1$
        introspectedColumn.getJavaProperty()));
        resultElement.addAttribute(new //$NON-NLS-1$
        Attribute(//$NON-NLS-1$
        "jdbcType", introspectedColumn.getJdbcTypeName()));
        if (stringHasValue(introspectedColumn.getTypeHandler())) {
            resultElement.addAttribute(new Attribute("typeHandler", //$NON-NLS-1$
            introspectedColumn.getTypeHandler()));
        }
        answer.addElement(resultElement);
    }
    if (context.getPlugins().sqlMapResultMapWithoutBLOBsElementGenerated(answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
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