Search in sources :

Example 81 with XmlElement

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

the class SqlMapConfigPlugin method contextGenerateAdditionalXmlFiles.

@Override
public List<GeneratedXmlFile> contextGenerateAdditionalXmlFiles() {
    Document document = new Document(XmlConstants.IBATIS2_SQL_MAP_CONFIG_PUBLIC_ID, XmlConstants.IBATIS2_SQL_MAP_CONFIG_SYSTEM_ID);
    //$NON-NLS-1$
    XmlElement root = new XmlElement("sqlMapConfig");
    document.setRootElement(root);
    //$NON-NLS-1$
    root.addElement(new TextElement("<!--"));
    root.addElement(new TextElement(//$NON-NLS-1$
    "  This file is generated by MyBatis Generator."));
    root.addElement(new TextElement(//$NON-NLS-1$
    "  This file is the shell of an SqlMapConfig file - in many cases you will need to add"));
    root.addElement(new TextElement(//$NON-NLS-1$
    "    to this file before it is usable by iBATIS."));
    StringBuilder sb = new StringBuilder();
    //$NON-NLS-1$
    sb.append("  This file was generated on ");
    sb.append(new Date());
    sb.append('.');
    root.addElement(new TextElement(sb.toString()));
    //$NON-NLS-1$
    root.addElement(new TextElement("-->"));
    //$NON-NLS-1$
    XmlElement settings = new XmlElement("settings");
    //$NON-NLS-1$ //$NON-NLS-2$
    settings.addAttribute(new Attribute("useStatementNamespaces", "true"));
    root.addElement(settings);
    XmlElement sqlMap;
    for (String sqlMapFile : sqlMapFiles) {
        //$NON-NLS-1$
        sqlMap = new XmlElement("sqlMap");
        //$NON-NLS-1$
        sqlMap.addAttribute(new Attribute("resource", sqlMapFile));
        root.addElement(sqlMap);
    }
    GeneratedXmlFile gxf = new GeneratedXmlFile(document, properties.getProperty("fileName", //$NON-NLS-1$ //$NON-NLS-2$
    "SqlMapConfig.xml"), //$NON-NLS-1$
    properties.getProperty("targetPackage"), //$NON-NLS-1$
    properties.getProperty("targetProject"), false, context.getXmlFormatter());
    List<GeneratedXmlFile> answer = new ArrayList<GeneratedXmlFile>(1);
    answer.add(gxf);
    return answer;
}
Also used : TextElement(org.mybatis.generator.api.dom.xml.TextElement) Attribute(org.mybatis.generator.api.dom.xml.Attribute) ArrayList(java.util.ArrayList) XmlElement(org.mybatis.generator.api.dom.xml.XmlElement) Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString) Document(org.mybatis.generator.api.dom.xml.Document) Date(java.util.Date) GeneratedXmlFile(org.mybatis.generator.api.GeneratedXmlFile)

Example 82 with XmlElement

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

the class XmlFileMergerTest method addInsertElement.

private void addInsertElement(CommentGenerator commentGenerator, XmlElement parentElement) {
    XmlElement answer = new XmlElement("insert");
    answer.addAttribute(new Attribute("id", "insert"));
    FullyQualifiedJavaType parameterType;
    parameterType = new FullyQualifiedJavaType("org.mybatis.test.TestRecord");
    answer.addAttribute(new Attribute("parameterType", parameterType.getFullyQualifiedName()));
    commentGenerator.addComment(answer);
    StringBuilder insertClause = new StringBuilder();
    StringBuilder valuesClause = new StringBuilder();
    insertClause.append("insert into ");
    insertClause.append("myschema.mytable");
    insertClause.append(" (id, description)");
    valuesClause.append("values (#{id}, #{description})");
    answer.addElement(new TextElement(insertClause.toString()));
    answer.addElement(new TextElement(valuesClause.toString()));
    parentElement.addElement(answer);
}
Also used : 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 83 with XmlElement

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

the class AntFileGenerator method addSqlTaskIfNecessary.

private void addSqlTaskIfNecessary(XmlElement parent) {
    String sqlFileFullPath = getSqlFile();
    if (sqlFileFullPath != null) {
        //$NON-NLS-1$
        XmlElement sqlTask = new XmlElement("sql");
        //$NON-NLS-1$
        sqlTask.addAttribute(new Attribute("driver", getTextOrBlank(configuration, ATTR_SQL_SCRIPT_DRIVER_CLASS)));
        //$NON-NLS-1$
        sqlTask.addAttribute(new Attribute("url", getTextOrBlank(configuration, ATTR_SQL_SCRIPT_CONNECTION_URL)));
        //$NON-NLS-1$
        sqlTask.addAttribute(new Attribute("userid", LauncherUtils.getUserId(configuration)));
        //$NON-NLS-1$
        sqlTask.addAttribute(new Attribute("password", LauncherUtils.getPassword(configuration)));
        //$NON-NLS-1$
        sqlTask.addAttribute(new Attribute("src", sqlFileFullPath));
        parent.addElement(sqlTask);
    }
}
Also used : Attribute(org.mybatis.generator.api.dom.xml.Attribute) XmlElement(org.mybatis.generator.api.dom.xml.XmlElement)

Example 84 with XmlElement

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

the class UpdateByPrimaryKeyWithBLOBsElementGenerator 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.getUpdateByPrimaryKeyWithBLOBsStatementId()));
    String parameterType;
    if (introspectedTable.getRules().generateRecordWithBLOBsClass()) {
        parameterType = introspectedTable.getRecordWithBLOBsType();
    } else {
        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("update ");
    sb.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
    answer.addElement(new TextElement(sb.toString()));
    // set up for first column
    sb.setLength(0);
    //$NON-NLS-1$
    sb.append("set ");
    Iterator<IntrospectedColumn> iter = introspectedTable.getNonPrimaryKeyColumns().iterator();
    while (iter.hasNext()) {
        IntrospectedColumn introspectedColumn = iter.next();
        sb.append(Ibatis2FormattingUtilities.getEscapedColumnName(introspectedColumn));
        //$NON-NLS-1$
        sb.append(" = ");
        sb.append(Ibatis2FormattingUtilities.getParameterClause(introspectedColumn));
        if (iter.hasNext()) {
            sb.append(',');
        }
        answer.addElement(new TextElement(sb.toString()));
        // set up for the next column
        if (iter.hasNext()) {
            sb.setLength(0);
            OutputUtilities.xmlIndent(sb, 1);
        }
    }
    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().sqlMapUpdateByPrimaryKeyWithBLOBsElementGenerated(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 85 with XmlElement

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

the class InsertElementGenerator 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.getInsertStatementId()));
    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 insertClause = new StringBuilder();
    StringBuilder valuesClause = new StringBuilder();
    //$NON-NLS-1$
    insertClause.append("insert into ");
    insertClause.append(introspectedTable.getFullyQualifiedTableNameAtRuntime());
    //$NON-NLS-1$
    insertClause.append(" (");
    //$NON-NLS-1$
    valuesClause.append("values (");
    List<String> valuesClauses = new ArrayList<String>();
    Iterator<IntrospectedColumn> iter = introspectedTable.getAllColumns().iterator();
    while (iter.hasNext()) {
        IntrospectedColumn introspectedColumn = iter.next();
        if (introspectedColumn.isIdentity()) {
            // cannot set values on identity fields
            continue;
        }
        insertClause.append(Ibatis2FormattingUtilities.getEscapedColumnName(introspectedColumn));
        valuesClause.append(Ibatis2FormattingUtilities.getParameterClause(introspectedColumn));
        if (iter.hasNext()) {
            //$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 (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().sqlMapInsertElementGenerated(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) ArrayList(java.util.ArrayList) XmlElement(org.mybatis.generator.api.dom.xml.XmlElement)

Aggregations

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