Search in sources :

Example 41 with Attribute

use of org.mybatis.generator.api.dom.xml.Attribute 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 {
        // select by primary key, but no primary key class. Fields
        // must be in the base record
        parameterType = introspectedTable.getBaseRecordType();
    }
    answer.addAttribute(new //$NON-NLS-1$
    Attribute(//$NON-NLS-1$
    "parameterClass", 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(Ibatis2FormattingUtilities.getAliasedEscapedColumnName(introspectedColumn));
        //$NON-NLS-1$
        sb.append(" = ");
        sb.append(Ibatis2FormattingUtilities.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 42 with Attribute

use of org.mybatis.generator.api.dom.xml.Attribute in project generator by mybatis.

the class CountByExampleElementGenerator 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.getCountByExampleStatementId()));
    answer.addAttribute(new Attribute("parameterClass", //$NON-NLS-1$
    introspectedTable.getExampleType()));
    //$NON-NLS-1$ //$NON-NLS-2$
    answer.addAttribute(new Attribute("resultClass", "java.lang.Long"));
    context.getCommentGenerator().addComment(answer);
    StringBuilder sb = new StringBuilder();
    //$NON-NLS-1$
    sb.append("select count(*) from ");
    sb.append(introspectedTable.getAliasedFullyQualifiedTableNameAtRuntime());
    answer.addElement(new TextElement(sb.toString()));
    //$NON-NLS-1$
    XmlElement includeElement = new XmlElement("include");
    sb.setLength(0);
    sb.append(introspectedTable.getIbatis2SqlMapNamespace());
    sb.append('.');
    sb.append(introspectedTable.getExampleWhereClauseId());
    includeElement.addAttribute(new //$NON-NLS-1$
    Attribute(//$NON-NLS-1$
    "refid", sb.toString()));
    answer.addElement(includeElement);
    if (context.getPlugins().sqlMapCountByExampleElementGenerated(answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
Also used : 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 43 with Attribute

use of org.mybatis.generator.api.dom.xml.Attribute 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 44 with Attribute

use of org.mybatis.generator.api.dom.xml.Attribute 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 45 with Attribute

use of org.mybatis.generator.api.dom.xml.Attribute in project generator by mybatis.

the class AbstractXmlElementGenerator method getSelectKey.

/**
     * This method should return an XmlElement for the select key used to
     * automatically generate keys.
     * 
     * @param introspectedColumn
     *            the column related to the select key statement
     * @param generatedKey
     *            the generated key for the current table
     * @return the selectKey element
     */
protected XmlElement getSelectKey(IntrospectedColumn introspectedColumn, GeneratedKey generatedKey) {
    String identityColumnType = introspectedColumn.getFullyQualifiedJavaType().getFullyQualifiedName();
    //$NON-NLS-1$
    XmlElement answer = new XmlElement("selectKey");
    //$NON-NLS-1$
    answer.addAttribute(new Attribute("resultType", identityColumnType));
    answer.addAttribute(new Attribute("keyProperty", //$NON-NLS-1$
    introspectedColumn.getJavaProperty()));
    answer.addAttribute(new //$NON-NLS-1$
    Attribute(//$NON-NLS-1$
    "order", generatedKey.getMyBatis3Order()));
    answer.addElement(new TextElement(generatedKey.getRuntimeSqlStatement()));
    return answer;
}
Also used : 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

Attribute (org.mybatis.generator.api.dom.xml.Attribute)74 XmlElement (org.mybatis.generator.api.dom.xml.XmlElement)74 TextElement (org.mybatis.generator.api.dom.xml.TextElement)40 IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)30 FullyQualifiedJavaType (org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)5 ArrayList (java.util.ArrayList)4 Document (org.mybatis.generator.api.dom.xml.Document)4 GeneratedKey (org.mybatis.generator.config.GeneratedKey)4 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)3 Date (java.util.Date)2 GeneratedXmlFile (org.mybatis.generator.api.GeneratedXmlFile)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1