Search in sources :

Example 1 with GeneratedKey

use of org.mybatis.generator.config.GeneratedKey in project generator by mybatis.

the class DatabaseIntrospector method calculateIdentityColumns.

/**
     * Calculate identity columns.
     *
     * @param tc
     *            the tc
     * @param columns
     *            the columns
     */
private void calculateIdentityColumns(TableConfiguration tc, Map<ActualTableName, List<IntrospectedColumn>> columns) {
    GeneratedKey gk = tc.getGeneratedKey();
    if (gk == null) {
        // no generated key, then no identity or sequence columns
        return;
    }
    for (Map.Entry<ActualTableName, List<IntrospectedColumn>> entry : columns.entrySet()) {
        for (IntrospectedColumn introspectedColumn : entry.getValue()) {
            if (isMatchedColumn(introspectedColumn, gk)) {
                if (gk.isIdentity() || gk.isJdbcStandard()) {
                    introspectedColumn.setIdentity(true);
                    introspectedColumn.setSequenceColumn(false);
                } else {
                    introspectedColumn.setIdentity(false);
                    introspectedColumn.setSequenceColumn(true);
                }
            }
        }
    }
}
Also used : GeneratedKey(org.mybatis.generator.config.GeneratedKey) IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 2 with GeneratedKey

use of org.mybatis.generator.config.GeneratedKey in project generator by mybatis.

the class AnnotatedInsertMethodGenerator method addExtraImports.

@Override
public void addExtraImports(Interface interfaze) {
    GeneratedKey gk = introspectedTable.getGeneratedKey();
    if (gk != null) {
        addGeneratedKeyImports(interfaze, gk);
    }
    //$NON-NLS-1$
    interfaze.addImportedType(new FullyQualifiedJavaType("org.apache.ibatis.annotations.Insert"));
}
Also used : GeneratedKey(org.mybatis.generator.config.GeneratedKey) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)

Example 3 with GeneratedKey

use of org.mybatis.generator.config.GeneratedKey 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 4 with GeneratedKey

use of org.mybatis.generator.config.GeneratedKey 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$
    "parameterType", parameterType.getFullyQualifiedName()));
    context.getCommentGenerator().addComment(answer);
    GeneratedKey gk = introspectedTable.getGeneratedKey();
    if (gk != null) {
        IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
        // warning has already been reported
        if (introspectedColumn != null) {
            if (gk.isJdbcStandard()) {
                //$NON-NLS-1$ //$NON-NLS-2$
                answer.addAttribute(new Attribute("useGeneratedKeys", "true"));
                //$NON-NLS-1$
                answer.addAttribute(new Attribute("keyProperty", introspectedColumn.getJavaProperty()));
                //$NON-NLS-1$
                answer.addAttribute(new Attribute("keyColumn", introspectedColumn.getActualColumnName()));
            } else {
                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 insertTrimElement = new XmlElement("trim");
    //$NON-NLS-1$ //$NON-NLS-2$
    insertTrimElement.addAttribute(new Attribute("prefix", "("));
    //$NON-NLS-1$ //$NON-NLS-2$
    insertTrimElement.addAttribute(new Attribute("suffix", ")"));
    //$NON-NLS-1$ //$NON-NLS-2$
    insertTrimElement.addAttribute(new Attribute("suffixOverrides", ","));
    answer.addElement(insertTrimElement);
    //$NON-NLS-1$
    XmlElement valuesTrimElement = new XmlElement("trim");
    //$NON-NLS-1$ //$NON-NLS-2$
    valuesTrimElement.addAttribute(new Attribute("prefix", "values ("));
    //$NON-NLS-1$ //$NON-NLS-2$
    valuesTrimElement.addAttribute(new Attribute("suffix", ")"));
    //$NON-NLS-1$ //$NON-NLS-2$
    valuesTrimElement.addAttribute(new Attribute("suffixOverrides", ","));
    answer.addElement(valuesTrimElement);
    for (IntrospectedColumn introspectedColumn : ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns())) {
        if (introspectedColumn.isSequenceColumn() || introspectedColumn.getFullyQualifiedJavaType().isPrimitive()) {
            // if it is a sequence column, it is not optional
            // This is required for MyBatis3 because MyBatis3 parses
            // and calculates the SQL before executing the selectKey
            // if it is primitive, we cannot do a null check
            sb.setLength(0);
            sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
            sb.append(',');
            insertTrimElement.addElement(new TextElement(sb.toString()));
            sb.setLength(0);
            sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
            sb.append(',');
            valuesTrimElement.addElement(new TextElement(sb.toString()));
            continue;
        }
        //$NON-NLS-1$
        XmlElement insertNotNullElement = new XmlElement("if");
        sb.setLength(0);
        sb.append(introspectedColumn.getJavaProperty());
        //$NON-NLS-1$
        sb.append(" != null");
        insertNotNullElement.addAttribute(new Attribute("test", //$NON-NLS-1$
        sb.toString()));
        sb.setLength(0);
        sb.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
        sb.append(',');
        insertNotNullElement.addElement(new TextElement(sb.toString()));
        insertTrimElement.addElement(insertNotNullElement);
        //$NON-NLS-1$
        XmlElement valuesNotNullElement = new XmlElement("if");
        sb.setLength(0);
        sb.append(introspectedColumn.getJavaProperty());
        //$NON-NLS-1$
        sb.append(" != null");
        valuesNotNullElement.addAttribute(new Attribute("test", //$NON-NLS-1$
        sb.toString()));
        sb.setLength(0);
        sb.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
        sb.append(',');
        valuesNotNullElement.addElement(new TextElement(sb.toString()));
        valuesTrimElement.addElement(valuesNotNullElement);
    }
    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 5 with GeneratedKey

use of org.mybatis.generator.config.GeneratedKey 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;
    if (isSimple) {
        parameterType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
    } else {
        parameterType = introspectedTable.getRules().calculateAllFieldsClass();
    }
    answer.addAttribute(new //$NON-NLS-1$
    Attribute(//$NON-NLS-1$
    "parameterType", parameterType.getFullyQualifiedName()));
    context.getCommentGenerator().addComment(answer);
    GeneratedKey gk = introspectedTable.getGeneratedKey();
    if (gk != null) {
        IntrospectedColumn introspectedColumn = introspectedTable.getColumn(gk.getColumn());
        // warning has already been reported
        if (introspectedColumn != null) {
            if (gk.isJdbcStandard()) {
                answer.addAttribute(new Attribute("useGeneratedKeys", //$NON-NLS-1$ //$NON-NLS-2$
                "true"));
                answer.addAttribute(new Attribute("keyProperty", //$NON-NLS-1$
                introspectedColumn.getJavaProperty()));
                answer.addAttribute(new Attribute("keyColumn", //$NON-NLS-1$
                introspectedColumn.getActualColumnName()));
            } else {
                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>();
    List<IntrospectedColumn> columns = ListUtilities.removeIdentityAndGeneratedAlwaysColumns(introspectedTable.getAllColumns());
    for (int i = 0; i < columns.size(); i++) {
        IntrospectedColumn introspectedColumn = columns.get(i);
        insertClause.append(MyBatis3FormattingUtilities.getEscapedColumnName(introspectedColumn));
        valuesClause.append(MyBatis3FormattingUtilities.getParameterClause(introspectedColumn));
        if (i + 1 < columns.size()) {
            //$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 (context.getPlugins().sqlMapInsertElementGenerated(answer, introspectedTable)) {
        parentElement.addElement(answer);
    }
}
Also used : Attribute(org.mybatis.generator.api.dom.xml.Attribute) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) ArrayList(java.util.ArrayList) GeneratedKey(org.mybatis.generator.config.GeneratedKey) IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) TextElement(org.mybatis.generator.api.dom.xml.TextElement) XmlElement(org.mybatis.generator.api.dom.xml.XmlElement)

Aggregations

GeneratedKey (org.mybatis.generator.config.GeneratedKey)10 FullyQualifiedJavaType (org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)7 IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)6 ArrayList (java.util.ArrayList)4 Attribute (org.mybatis.generator.api.dom.xml.Attribute)4 TextElement (org.mybatis.generator.api.dom.xml.TextElement)4 XmlElement (org.mybatis.generator.api.dom.xml.XmlElement)4 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Properties (java.util.Properties)1 TreeMap (java.util.TreeMap)1 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)1