Search in sources :

Example 1 with ColumnOverride

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

the class DatabaseIntrospector method applyColumnOverrides.

/**
     * Apply column overrides.
     *
     * @param tc
     *            the tc
     * @param columns
     *            the columns
     */
private void applyColumnOverrides(TableConfiguration tc, Map<ActualTableName, List<IntrospectedColumn>> columns) {
    for (Map.Entry<ActualTableName, List<IntrospectedColumn>> entry : columns.entrySet()) {
        for (IntrospectedColumn introspectedColumn : entry.getValue()) {
            ColumnOverride columnOverride = tc.getColumnOverride(introspectedColumn.getActualColumnName());
            if (columnOverride != null) {
                if (logger.isDebugEnabled()) {
                    logger.debug(getString(//$NON-NLS-1$
                    "Tracing.4", introspectedColumn.getActualColumnName(), entry.getKey().toString()));
                }
                if (stringHasValue(columnOverride.getJavaProperty())) {
                    introspectedColumn.setJavaProperty(columnOverride.getJavaProperty());
                }
                if (stringHasValue(columnOverride.getJavaType())) {
                    introspectedColumn.setFullyQualifiedJavaType(new FullyQualifiedJavaType(columnOverride.getJavaType()));
                }
                if (stringHasValue(columnOverride.getJdbcType())) {
                    introspectedColumn.setJdbcTypeName(columnOverride.getJdbcType());
                }
                if (stringHasValue(columnOverride.getTypeHandler())) {
                    introspectedColumn.setTypeHandler(columnOverride.getTypeHandler());
                }
                if (columnOverride.isColumnNameDelimited()) {
                    introspectedColumn.setColumnNameDelimited(true);
                }
                introspectedColumn.setGeneratedAlways(columnOverride.isGeneratedAlways());
                introspectedColumn.setProperties(columnOverride.getProperties());
            }
        }
    }
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) ColumnOverride(org.mybatis.generator.config.ColumnOverride) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 2 with ColumnOverride

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

the class MyBatisGeneratorConfigurationParser method parseColumnOverride.

private void parseColumnOverride(TableConfiguration tc, Node node) {
    Properties attributes = parseAttributes(node);
    //$NON-NLS-1$
    String column = attributes.getProperty("column");
    //$NON-NLS-1$
    String property = attributes.getProperty("property");
    //$NON-NLS-1$
    String javaType = attributes.getProperty("javaType");
    //$NON-NLS-1$
    String jdbcType = attributes.getProperty("jdbcType");
    //$NON-NLS-1$
    String typeHandler = attributes.getProperty("typeHandler");
    String delimitedColumnName = attributes.getProperty(//$NON-NLS-1$
    "delimitedColumnName");
    //$NON-NLS-1$
    String isGeneratedAlways = attributes.getProperty("isGeneratedAlways");
    ColumnOverride co = new ColumnOverride(column);
    if (stringHasValue(property)) {
        co.setJavaProperty(property);
    }
    if (stringHasValue(javaType)) {
        co.setJavaType(javaType);
    }
    if (stringHasValue(jdbcType)) {
        co.setJdbcType(jdbcType);
    }
    if (stringHasValue(typeHandler)) {
        co.setTypeHandler(typeHandler);
    }
    if (stringHasValue(delimitedColumnName)) {
        co.setColumnNameDelimited(isTrue(delimitedColumnName));
    }
    if (stringHasValue(isGeneratedAlways)) {
        co.setGeneratedAlways(Boolean.parseBoolean(isGeneratedAlways));
    }
    NodeList nodeList = node.getChildNodes();
    for (int i = 0; i < nodeList.getLength(); i++) {
        Node childNode = nodeList.item(i);
        if (childNode.getNodeType() != Node.ELEMENT_NODE) {
            continue;
        }
        if ("property".equals(childNode.getNodeName())) {
            //$NON-NLS-1$
            parseProperty(co, childNode);
        }
    }
    tc.addColumnOverride(co);
}
Also used : ColumnOverride(org.mybatis.generator.config.ColumnOverride) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString) Properties(java.util.Properties)

Example 3 with ColumnOverride

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

the class DatabaseIntrospector method calculateExtraColumnInformation.

/**
     * Calculate extra column information.
     *
     * @param tc
     *            the tc
     * @param columns
     *            the columns
     */
private void calculateExtraColumnInformation(TableConfiguration tc, Map<ActualTableName, List<IntrospectedColumn>> columns) {
    StringBuilder sb = new StringBuilder();
    Pattern pattern = null;
    String replaceString = null;
    if (tc.getColumnRenamingRule() != null) {
        pattern = Pattern.compile(tc.getColumnRenamingRule().getSearchString());
        replaceString = tc.getColumnRenamingRule().getReplaceString();
        //$NON-NLS-1$
        replaceString = replaceString == null ? "" : replaceString;
    }
    for (Map.Entry<ActualTableName, List<IntrospectedColumn>> entry : columns.entrySet()) {
        for (IntrospectedColumn introspectedColumn : entry.getValue()) {
            String calculatedColumnName;
            if (pattern == null) {
                calculatedColumnName = introspectedColumn.getActualColumnName();
            } else {
                Matcher matcher = pattern.matcher(introspectedColumn.getActualColumnName());
                calculatedColumnName = matcher.replaceAll(replaceString);
            }
            if (isTrue(tc.getProperty(PropertyRegistry.TABLE_USE_ACTUAL_COLUMN_NAMES))) {
                introspectedColumn.setJavaProperty(getValidPropertyName(calculatedColumnName));
            } else if (isTrue(tc.getProperty(PropertyRegistry.TABLE_USE_COMPOUND_PROPERTY_NAMES))) {
                sb.setLength(0);
                sb.append(calculatedColumnName);
                sb.append('_');
                sb.append(getCamelCaseString(introspectedColumn.getRemarks(), true));
                introspectedColumn.setJavaProperty(getValidPropertyName(sb.toString()));
            } else {
                introspectedColumn.setJavaProperty(getCamelCaseString(calculatedColumnName, false));
            }
            FullyQualifiedJavaType fullyQualifiedJavaType = javaTypeResolver.calculateJavaType(introspectedColumn);
            if (fullyQualifiedJavaType != null) {
                introspectedColumn.setFullyQualifiedJavaType(fullyQualifiedJavaType);
                introspectedColumn.setJdbcTypeName(javaTypeResolver.calculateJdbcTypeName(introspectedColumn));
            } else {
                // type cannot be resolved. Check for ignored or overridden
                boolean warn = true;
                if (tc.isColumnIgnored(introspectedColumn.getActualColumnName())) {
                    warn = false;
                }
                ColumnOverride co = tc.getColumnOverride(introspectedColumn.getActualColumnName());
                if (co != null && stringHasValue(co.getJavaType()) && stringHasValue(co.getJavaType())) {
                    warn = false;
                }
                // if the type is not supported, then we'll report a warning
                if (warn) {
                    introspectedColumn.setFullyQualifiedJavaType(FullyQualifiedJavaType.getObjectInstance());
                    //$NON-NLS-1$
                    introspectedColumn.setJdbcTypeName("OTHER");
                    String warning = getString(//$NON-NLS-1$
                    "Warning.14", Integer.toString(introspectedColumn.getJdbcType()), entry.getKey().toString(), introspectedColumn.getActualColumnName());
                    warnings.add(warning);
                }
            }
            if (context.autoDelimitKeywords() && SqlReservedWords.containsWord(introspectedColumn.getActualColumnName())) {
                introspectedColumn.setColumnNameDelimited(true);
            }
            if (tc.isAllColumnDelimitingEnabled()) {
                introspectedColumn.setColumnNameDelimited(true);
            }
        }
    }
}
Also used : Pattern(java.util.regex.Pattern) IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) ColumnOverride(org.mybatis.generator.config.ColumnOverride) Matcher(java.util.regex.Matcher) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) ArrayList(java.util.ArrayList) List(java.util.List) Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString) JavaBeansUtil.getCamelCaseString(org.mybatis.generator.internal.util.JavaBeansUtil.getCamelCaseString) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Aggregations

ColumnOverride (org.mybatis.generator.config.ColumnOverride)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 TreeMap (java.util.TreeMap)2 IntrospectedColumn (org.mybatis.generator.api.IntrospectedColumn)2 FullyQualifiedJavaType (org.mybatis.generator.api.dom.java.FullyQualifiedJavaType)2 Messages.getString (org.mybatis.generator.internal.util.messages.Messages.getString)2 Properties (java.util.Properties)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 JavaBeansUtil.getCamelCaseString (org.mybatis.generator.internal.util.JavaBeansUtil.getCamelCaseString)1 Node (org.w3c.dom.Node)1 NodeList (org.w3c.dom.NodeList)1