Search in sources :

Example 46 with IntrospectedColumn

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

the class BlobColumnListElementGenerator method addElements.

@Override
public void addElements(XmlElement parentElement) {
    //$NON-NLS-1$
    XmlElement answer = new XmlElement("sql");
    answer.addAttribute(new //$NON-NLS-1$
    Attribute(//$NON-NLS-1$
    "id", introspectedTable.getBlobColumnListId()));
    context.getCommentGenerator().addComment(answer);
    StringBuilder sb = new StringBuilder();
    Iterator<IntrospectedColumn> iter = introspectedTable.getBLOBColumns().iterator();
    while (iter.hasNext()) {
        sb.append(Ibatis2FormattingUtilities.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()));
    }
    if (context.getPlugins().sqlMapBlobColumnListElementGenerated(answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) TextElement(org.mybatis.generator.api.dom.xml.TextElement) XmlElement(org.mybatis.generator.api.dom.xml.XmlElement)

Example 47 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 (introspectedTable.getRules().generatePrimaryKeyClass()) {
        parameterClass = introspectedTable.getPrimaryKeyType();
    } else {
        parameterClass = introspectedTable.getBaseRecordType();
    }
    answer.addAttribute(new //$NON-NLS-1$
    Attribute(//$NON-NLS-1$
    "parameterClass", 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(Ibatis2FormattingUtilities.getEscapedColumnName(introspectedColumn));
        //$NON-NLS-1$
        sb.append(" = ");
        sb.append(Ibatis2FormattingUtilities.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 48 with IntrospectedColumn

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

the class ExampleWhereClauseElementGenerator method addElements.

@Override
public void addElements(XmlElement parentElement) {
    //$NON-NLS-1$
    XmlElement answer = new XmlElement("sql");
    answer.addAttribute(new Attribute("id", //$NON-NLS-1$
    introspectedTable.getExampleWhereClauseId()));
    context.getCommentGenerator().addComment(answer);
    //$NON-NLS-1$
    XmlElement outerIterateElement = new XmlElement("iterate");
    outerIterateElement.addAttribute(new Attribute("property", //$NON-NLS-1$ //$NON-NLS-2$
    "oredCriteria"));
    //$NON-NLS-1$ //$NON-NLS-2$
    outerIterateElement.addAttribute(new Attribute("conjunction", "or"));
    //$NON-NLS-1$ //$NON-NLS-2$
    outerIterateElement.addAttribute(new Attribute("prepend", "where"));
    outerIterateElement.addAttribute(new Attribute("removeFirstPrepend", //$NON-NLS-1$ //$NON-NLS-2$
    "iterate"));
    answer.addElement(outerIterateElement);
    //$NON-NLS-1$
    XmlElement isEqualElement = new XmlElement("isEqual");
    isEqualElement.addAttribute(new Attribute("property", //$NON-NLS-1$ //$NON-NLS-2$
    "oredCriteria[].valid"));
    //$NON-NLS-1$ //$NON-NLS-2$
    isEqualElement.addAttribute(new Attribute("compareValue", "true"));
    outerIterateElement.addElement(isEqualElement);
    //$NON-NLS-1$
    isEqualElement.addElement(new TextElement("("));
    //$NON-NLS-1$
    XmlElement innerIterateElement = new XmlElement("iterate");
    //$NON-NLS-1$ //$NON-NLS-2$
    innerIterateElement.addAttribute(new Attribute("prepend", "and"));
    innerIterateElement.addAttribute(new Attribute("property", //$NON-NLS-1$ //$NON-NLS-2$
    "oredCriteria[].criteriaWithoutValue"));
    //$NON-NLS-1$ //$NON-NLS-2$
    innerIterateElement.addAttribute(new Attribute("conjunction", "and"));
    innerIterateElement.addElement(new TextElement(//$NON-NLS-1$
    "$oredCriteria[].criteriaWithoutValue[]$"));
    isEqualElement.addElement(innerIterateElement);
    //$NON-NLS-1$
    innerIterateElement = new XmlElement("iterate");
    //$NON-NLS-1$ //$NON-NLS-2$
    innerIterateElement.addAttribute(new Attribute("prepend", "and"));
    innerIterateElement.addAttribute(new Attribute("property", //$NON-NLS-1$ //$NON-NLS-2$
    "oredCriteria[].criteriaWithSingleValue"));
    //$NON-NLS-1$ //$NON-NLS-2$
    innerIterateElement.addAttribute(new Attribute("conjunction", "and"));
    innerIterateElement.addElement(new TextElement(//$NON-NLS-1$
    "$oredCriteria[].criteriaWithSingleValue[].condition$ #oredCriteria[].criteriaWithSingleValue[].value#"));
    isEqualElement.addElement(innerIterateElement);
    //$NON-NLS-1$
    innerIterateElement = new XmlElement("iterate");
    //$NON-NLS-1$ //$NON-NLS-2$
    innerIterateElement.addAttribute(new Attribute("prepend", "and"));
    innerIterateElement.addAttribute(new Attribute("property", //$NON-NLS-1$ //$NON-NLS-2$
    "oredCriteria[].criteriaWithListValue"));
    //$NON-NLS-1$ //$NON-NLS-2$
    innerIterateElement.addAttribute(new Attribute("conjunction", "and"));
    innerIterateElement.addElement(new TextElement(//$NON-NLS-1$
    "$oredCriteria[].criteriaWithListValue[].condition$"));
    //$NON-NLS-1$
    XmlElement innerInnerIterateElement = new XmlElement("iterate");
    innerInnerIterateElement.addAttribute(new //$NON-NLS-1$
    Attribute(//$NON-NLS-1$
    "property", //$NON-NLS-1$
    "oredCriteria[].criteriaWithListValue[].values"));
    //$NON-NLS-1$ //$NON-NLS-2$
    innerInnerIterateElement.addAttribute(new Attribute("open", "("));
    //$NON-NLS-1$ //$NON-NLS-2$
    innerInnerIterateElement.addAttribute(new Attribute("close", ")"));
    innerInnerIterateElement.addAttribute(//$NON-NLS-1$ //$NON-NLS-2$
    new Attribute("conjunction", ","));
    innerInnerIterateElement.addElement(new TextElement(//$NON-NLS-1$
    "#oredCriteria[].criteriaWithListValue[].values[]#"));
    innerIterateElement.addElement(innerInnerIterateElement);
    isEqualElement.addElement(innerIterateElement);
    //$NON-NLS-1$
    innerIterateElement = new XmlElement("iterate");
    //$NON-NLS-1$ //$NON-NLS-2$
    innerIterateElement.addAttribute(new Attribute("prepend", "and"));
    innerIterateElement.addAttribute(new Attribute("property", //$NON-NLS-1$ //$NON-NLS-2$
    "oredCriteria[].criteriaWithBetweenValue"));
    //$NON-NLS-1$ //$NON-NLS-2$
    innerIterateElement.addAttribute(new Attribute("conjunction", "and"));
    innerIterateElement.addElement(new TextElement(//$NON-NLS-1$
    "$oredCriteria[].criteriaWithBetweenValue[].condition$"));
    innerIterateElement.addElement(new TextElement(//$NON-NLS-1$
    "#oredCriteria[].criteriaWithBetweenValue[].values[0]# and"));
    innerIterateElement.addElement(new TextElement(//$NON-NLS-1$
    "#oredCriteria[].criteriaWithBetweenValue[].values[1]#"));
    isEqualElement.addElement(innerIterateElement);
    // handler
    for (IntrospectedColumn introspectedColumn : introspectedTable.getNonBLOBColumns()) {
        if (stringHasValue(introspectedColumn.getTypeHandler())) {
            // name the property based on the column name, then
            // add the type handler to the parameter declaration
            StringBuilder sb1 = new StringBuilder();
            StringBuilder sb2 = new StringBuilder();
            //$NON-NLS-1$
            innerIterateElement = new XmlElement("iterate");
            innerIterateElement.addAttribute(//$NON-NLS-1$ //$NON-NLS-2$
            new Attribute("prepend", "and"));
            //$NON-NLS-1$
            sb1.append("oredCriteria[].");
            sb1.append(introspectedColumn.getJavaProperty());
            //$NON-NLS-1$
            sb1.append("CriteriaWithSingleValue");
            innerIterateElement.addAttribute(new Attribute("property", //$NON-NLS-1$
            sb1.toString()));
            innerIterateElement.addAttribute(new Attribute("conjunction", //$NON-NLS-1$ //$NON-NLS-2$
            "and"));
            sb2.append(sb1);
            sb1.insert(0, '$');
            //$NON-NLS-1$
            sb1.append("[].condition$ ");
            sb2.insert(0, '#');
            //$NON-NLS-1$
            sb2.append("[].value,handler=");
            sb2.append(introspectedColumn.getTypeHandler());
            sb2.append('#');
            sb1.append(sb2);
            innerIterateElement.addElement(new TextElement(sb1.toString()));
            isEqualElement.addElement(innerIterateElement);
            sb1.setLength(0);
            sb2.setLength(0);
            //$NON-NLS-1$
            sb1.append("oredCriteria[].");
            sb1.append(introspectedColumn.getJavaProperty());
            //$NON-NLS-1$
            sb1.append("CriteriaWithListValue");
            //$NON-NLS-1$
            innerIterateElement = new XmlElement("iterate");
            innerIterateElement.addAttribute(//$NON-NLS-1$ //$NON-NLS-2$
            new Attribute("prepend", "and"));
            innerIterateElement.addAttribute(new Attribute("property", //$NON-NLS-1$
            sb1.toString()));
            innerIterateElement.addAttribute(new Attribute("conjunction", //$NON-NLS-1$ //$NON-NLS-2$
            "and"));
            sb2.append('$');
            sb2.append(sb1);
            //$NON-NLS-1$
            sb2.append("[].condition$");
            innerIterateElement.addElement(new TextElement(sb2.toString()));
            sb2.setLength(0);
            sb2.append(sb1);
            //$NON-NLS-1$
            sb2.append("[].values");
            //$NON-NLS-1$
            innerInnerIterateElement = new XmlElement("iterate");
            innerInnerIterateElement.addAttribute(new //$NON-NLS-1$
            Attribute(//$NON-NLS-1$
            "property", sb2.toString()));
            innerInnerIterateElement.addAttribute(//$NON-NLS-1$ //$NON-NLS-2$
            new Attribute("open", "("));
            innerInnerIterateElement.addAttribute(new Attribute("close", //$NON-NLS-1$ //$NON-NLS-2$
            ")"));
            innerInnerIterateElement.addAttribute(new Attribute("conjunction", //$NON-NLS-1$ //$NON-NLS-2$
            ","));
            sb2.setLength(0);
            sb2.append('#');
            sb2.append(sb1);
            //$NON-NLS-1$
            sb2.append("[].values[],handler=");
            sb2.append(introspectedColumn.getTypeHandler());
            sb2.append('#');
            innerInnerIterateElement.addElement(new TextElement(sb2.toString()));
            innerIterateElement.addElement(innerInnerIterateElement);
            isEqualElement.addElement(innerIterateElement);
            sb1.setLength(0);
            sb2.setLength(0);
            //$NON-NLS-1$
            sb1.append("oredCriteria[].");
            sb1.append(introspectedColumn.getJavaProperty());
            //$NON-NLS-1$
            sb1.append("CriteriaWithBetweenValue");
            //$NON-NLS-1$
            innerIterateElement = new XmlElement("iterate");
            innerIterateElement.addAttribute(//$NON-NLS-1$ //$NON-NLS-2$
            new Attribute("prepend", "and"));
            innerIterateElement.addAttribute(new Attribute("property", //$NON-NLS-1$
            sb1.toString()));
            innerIterateElement.addAttribute(new Attribute("conjunction", //$NON-NLS-1$ //$NON-NLS-2$
            "and"));
            sb2.append('$');
            sb2.append(sb1);
            //$NON-NLS-1$
            sb2.append("[].condition$");
            innerIterateElement.addElement(new TextElement(sb2.toString()));
            sb2.setLength(0);
            sb2.append(sb1);
            sb1.insert(0, '#');
            //$NON-NLS-1$
            sb1.append("[].values[0],handler=");
            sb1.append(introspectedColumn.getTypeHandler());
            //$NON-NLS-1$
            sb1.append("# and");
            sb2.insert(0, '#');
            //$NON-NLS-1$
            sb2.append("[].values[1],handler=");
            sb2.append(introspectedColumn.getTypeHandler());
            sb2.append('#');
            innerIterateElement.addElement(new TextElement(sb1.toString()));
            innerIterateElement.addElement(new TextElement(sb2.toString()));
            isEqualElement.addElement(innerIterateElement);
        }
    }
    //$NON-NLS-1$
    isEqualElement.addElement(new TextElement(")"));
    if (context.getPlugins().sqlMapExampleWhereClauseElementGenerated(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 49 with IntrospectedColumn

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

the class SelectByPrimaryKeyMethodGenerator method addImplementationElements.

@Override
public void addImplementationElements(TopLevelClass topLevelClass) {
    Set<FullyQualifiedJavaType> importedTypes = new TreeSet<FullyQualifiedJavaType>();
    Method method = getMethodShell(importedTypes);
    // generate the implementation method
    StringBuilder sb = new StringBuilder();
    if (!introspectedTable.getRules().generatePrimaryKeyClass()) {
        // no primary key class, but primary key is enabled. Primary
        // key columns must be in the base class.
        FullyQualifiedJavaType keyType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
        topLevelClass.addImportedType(keyType);
        sb.setLength(0);
        sb.append(keyType.getShortName());
        //$NON-NLS-1$
        sb.append(" _key = new ");
        sb.append(keyType.getShortName());
        //$NON-NLS-1$
        sb.append("();");
        method.addBodyLine(sb.toString());
        for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
            sb.setLength(0);
            //$NON-NLS-1$
            sb.append("_key.");
            sb.append(getSetterMethodName(introspectedColumn.getJavaProperty()));
            sb.append('(');
            sb.append(introspectedColumn.getJavaProperty());
            //$NON-NLS-1$
            sb.append(");");
            method.addBodyLine(sb.toString());
        }
    }
    FullyQualifiedJavaType returnType = method.getReturnType();
    sb.setLength(0);
    sb.append(returnType.getShortName());
    //$NON-NLS-1$
    sb.append(" record = (");
    sb.append(returnType.getShortName());
    //$NON-NLS-1$
    sb.append(") ");
    sb.append(daoTemplate.getQueryForObjectMethod(introspectedTable.getIbatis2SqlMapNamespace(), introspectedTable.getSelectByPrimaryKeyStatementId(), //$NON-NLS-1$
    "_key"));
    method.addBodyLine(sb.toString());
    //$NON-NLS-1$
    method.addBodyLine("return record;");
    if (context.getPlugins().clientSelectByPrimaryKeyMethodGenerated(method, topLevelClass, introspectedTable)) {
        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 50 with IntrospectedColumn

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

the class ProviderUpdateByPrimaryKeySelectiveMethodGenerator 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");
        //$NON-NLS-1$
        staticImports.add("org.apache.ibatis.jdbc.SqlBuilder.WHERE");
    } else {
        importedTypes.add(NEW_BUILDER_IMPORT);
    }
    FullyQualifiedJavaType fqjt = introspectedTable.getRules().calculateAllFieldsClass();
    importedTypes.add(fqjt);
    Method method = new Method(introspectedTable.getUpdateByPrimaryKeySelectiveStatementId());
    method.setReturnType(FullyQualifiedJavaType.getStringInstance());
    method.setVisibility(JavaVisibility.PUBLIC);
    //$NON-NLS-1$
    method.addParameter(new Parameter(fqjt, "record"));
    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.getFullyQualifiedTableNameAtRuntime())));
    //$NON-NLS-1$
    method.addBodyLine("");
    for (IntrospectedColumn introspectedColumn : ListUtilities.removeGeneratedAlwaysColumns(introspectedTable.getNonPrimaryKeyColumns())) {
        if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
            method.addBodyLine(//$NON-NLS-1$
            String.format(//$NON-NLS-1$
            "if (record.%s() != null) {", getGetterMethodName(introspectedColumn.getJavaProperty(), introspectedColumn.getFullyQualifiedJavaType())));
        }
        method.addBodyLine(//$NON-NLS-1$
        String.format(//$NON-NLS-1$
        "%sSET(\"%s = %s\");", builderPrefix, escapeStringForJava(getEscapedColumnName(introspectedColumn)), getParameterClause(introspectedColumn)));
        if (!introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
            //$NON-NLS-1$
            method.addBodyLine("}");
        }
        //$NON-NLS-1$
        method.addBodyLine("");
    }
    for (IntrospectedColumn introspectedColumn : introspectedTable.getPrimaryKeyColumns()) {
        method.addBodyLine(//$NON-NLS-1$
        String.format(//$NON-NLS-1$
        "%sWHERE(\"%s = %s\");", builderPrefix, escapeStringForJava(getEscapedColumnName(introspectedColumn)), getParameterClause(introspectedColumn)));
    }
    //$NON-NLS-1$
    method.addBodyLine("");
    if (useLegacyBuilder) {
        //$NON-NLS-1$
        method.addBodyLine("return SQL();");
    } else {
        //$NON-NLS-1$
        method.addBodyLine("return sql.toString();");
    }
    if (context.getPlugins().providerUpdateByPrimaryKeySelectiveMethodGenerated(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) 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