Search in sources :

Example 71 with IntrospectedColumn

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

the class EqualsHashCodePlugin method generateHashCode.

/**
     * Generates a <tt>hashCode</tt> method that includes all fields.
     * <p>
     * Note that this implementation is based on the eclipse foundation hashCode
     * generator.
     * 
     * @param topLevelClass
     *            the class to which the method will be added
     * @param introspectedColumns
     *            column definitions of this class and any superclass of this
     *            class
     * @param introspectedTable
     *            the table corresponding to this class
     */
protected void generateHashCode(TopLevelClass topLevelClass, List<IntrospectedColumn> introspectedColumns, IntrospectedTable introspectedTable) {
    Method method = new Method();
    method.setVisibility(JavaVisibility.PUBLIC);
    method.setReturnType(FullyQualifiedJavaType.getIntInstance());
    //$NON-NLS-1$
    method.setName("hashCode");
    if (introspectedTable.isJava5Targeted()) {
        //$NON-NLS-1$
        method.addAnnotation("@Override");
    }
    context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
    //$NON-NLS-1$
    method.addBodyLine("final int prime = 31;");
    //$NON-NLS-1$
    method.addBodyLine("int result = 1;");
    if (useEqualsHashCodeFromRoot && topLevelClass.getSuperClass() != null) {
        //$NON-NLS-1$
        method.addBodyLine("result = prime * result + super.hashCode();");
    }
    StringBuilder sb = new StringBuilder();
    boolean hasTemp = false;
    Iterator<IntrospectedColumn> iter = introspectedColumns.iterator();
    while (iter.hasNext()) {
        IntrospectedColumn introspectedColumn = iter.next();
        FullyQualifiedJavaType fqjt = introspectedColumn.getFullyQualifiedJavaType();
        String getterMethod = getGetterMethodName(introspectedColumn.getJavaProperty(), fqjt);
        sb.setLength(0);
        if (fqjt.isPrimitive()) {
            if ("boolean".equals(fqjt.getFullyQualifiedName())) {
                //$NON-NLS-1$
                //$NON-NLS-1$
                sb.append("result = prime * result + (");
                sb.append(getterMethod);
                //$NON-NLS-1$
                sb.append("() ? 1231 : 1237);");
                method.addBodyLine(sb.toString());
            } else if ("byte".equals(fqjt.getFullyQualifiedName())) {
                //$NON-NLS-1$
                //$NON-NLS-1$
                sb.append("result = prime * result + ");
                sb.append(getterMethod);
                //$NON-NLS-1$
                sb.append("();");
                method.addBodyLine(sb.toString());
            } else if ("char".equals(fqjt.getFullyQualifiedName())) {
                //$NON-NLS-1$
                //$NON-NLS-1$
                sb.append("result = prime * result + ");
                sb.append(getterMethod);
                //$NON-NLS-1$
                sb.append("();");
                method.addBodyLine(sb.toString());
            } else if ("double".equals(fqjt.getFullyQualifiedName())) {
                //$NON-NLS-1$
                if (!hasTemp) {
                    //$NON-NLS-1$
                    method.addBodyLine("long temp;");
                    hasTemp = true;
                }
                //$NON-NLS-1$
                sb.append("temp = Double.doubleToLongBits(");
                sb.append(getterMethod);
                //$NON-NLS-1$
                sb.append("());");
                method.addBodyLine(sb.toString());
                method.addBodyLine(//$NON-NLS-1$
                "result = prime * result + (int) (temp ^ (temp >>> 32));");
            } else if ("float".equals(fqjt.getFullyQualifiedName())) {
                //$NON-NLS-1$
                sb.append(//$NON-NLS-1$
                "result = prime * result + Float.floatToIntBits(");
                sb.append(getterMethod);
                //$NON-NLS-1$
                sb.append("());");
                method.addBodyLine(sb.toString());
            } else if ("int".equals(fqjt.getFullyQualifiedName())) {
                //$NON-NLS-1$
                //$NON-NLS-1$
                sb.append("result = prime * result + ");
                sb.append(getterMethod);
                //$NON-NLS-1$
                sb.append("();");
                method.addBodyLine(sb.toString());
            } else if ("long".equals(fqjt.getFullyQualifiedName())) {
                //$NON-NLS-1$
                //$NON-NLS-1$
                sb.append("result = prime * result + (int) (");
                sb.append(getterMethod);
                //$NON-NLS-1$
                sb.append("() ^ (");
                sb.append(getterMethod);
                //$NON-NLS-1$
                sb.append("() >>> 32));");
                method.addBodyLine(sb.toString());
            } else if ("short".equals(fqjt.getFullyQualifiedName())) {
                //$NON-NLS-1$
                //$NON-NLS-1$
                sb.append("result = prime * result + ");
                sb.append(getterMethod);
                //$NON-NLS-1$
                sb.append("();");
                method.addBodyLine(sb.toString());
            } else {
                // should never happen
                continue;
            }
        } else if (fqjt.isArray()) {
            // Arrays is already imported by the generateEquals method, we don't need
            // to do it again
            //$NON-NLS-1$
            sb.append("result = prime * result + (Arrays.hashCode(");
            sb.append(getterMethod);
            //$NON-NLS-1$
            sb.append("()));");
            method.addBodyLine(sb.toString());
        } else {
            //$NON-NLS-1$
            sb.append("result = prime * result + ((");
            sb.append(getterMethod);
            //$NON-NLS-1$
            sb.append("() == null) ? 0 : ");
            sb.append(getterMethod);
            //$NON-NLS-1$
            sb.append("().hashCode());");
            method.addBodyLine(sb.toString());
        }
    }
    //$NON-NLS-1$
    method.addBodyLine("return result;");
    topLevelClass.addMethod(method);
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) Method(org.mybatis.generator.api.dom.java.Method)

Example 72 with IntrospectedColumn

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

the class ObjectFactory method createIntrospectedColumn.

/**
     * Creates a new Object object.
     *
     * @param context
     *            the context
     * @return the introspected column
     */
public static IntrospectedColumn createIntrospectedColumn(Context context) {
    String type = context.getIntrospectedColumnImpl();
    if (!stringHasValue(type)) {
        type = IntrospectedColumn.class.getName();
    }
    IntrospectedColumn answer = (IntrospectedColumn) createInternalObject(type);
    answer.setContext(context);
    return answer;
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString)

Example 73 with IntrospectedColumn

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

the class DatabaseIntrospector method calculateIntrospectedTables.

/**
     * Calculate introspected tables.
     *
     * @param tc
     *            the tc
     * @param columns
     *            the columns
     * @return the list
     */
private List<IntrospectedTable> calculateIntrospectedTables(TableConfiguration tc, Map<ActualTableName, List<IntrospectedColumn>> columns) {
    boolean delimitIdentifiers = tc.isDelimitIdentifiers() || stringContainsSpace(tc.getCatalog()) || stringContainsSpace(tc.getSchema()) || stringContainsSpace(tc.getTableName());
    List<IntrospectedTable> answer = new ArrayList<IntrospectedTable>();
    for (Map.Entry<ActualTableName, List<IntrospectedColumn>> entry : columns.entrySet()) {
        ActualTableName atn = entry.getKey();
        // we only use the returned catalog and schema if something was
        // actually
        // specified on the table configuration. If something was returned
        // from the DB for these fields, but nothing was specified on the
        // table
        // configuration, then some sort of DB default is being returned
        // and we don't want that in our SQL
        FullyQualifiedTable table = new FullyQualifiedTable(stringHasValue(tc.getCatalog()) ? atn.getCatalog() : null, stringHasValue(tc.getSchema()) ? atn.getSchema() : null, atn.getTableName(), tc.getDomainObjectName(), tc.getAlias(), isTrue(tc.getProperty(PropertyRegistry.TABLE_IGNORE_QUALIFIERS_AT_RUNTIME)), tc.getProperty(PropertyRegistry.TABLE_RUNTIME_CATALOG), tc.getProperty(PropertyRegistry.TABLE_RUNTIME_SCHEMA), tc.getProperty(PropertyRegistry.TABLE_RUNTIME_TABLE_NAME), delimitIdentifiers, context);
        IntrospectedTable introspectedTable = ObjectFactory.createIntrospectedTable(tc, table, context);
        for (IntrospectedColumn introspectedColumn : entry.getValue()) {
            introspectedTable.addColumn(introspectedColumn);
        }
        calculatePrimaryKey(table, introspectedTable);
        enhanceIntrospectedTable(introspectedTable);
        answer.add(introspectedTable);
    }
    return answer;
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) FullyQualifiedTable(org.mybatis.generator.api.FullyQualifiedTable) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) IntrospectedTable(org.mybatis.generator.api.IntrospectedTable) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 74 with IntrospectedColumn

use of org.mybatis.generator.api.IntrospectedColumn 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)

Example 75 with IntrospectedColumn

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

the class InsertMethodGenerator method getMethodShell.

private Method getMethodShell(Set<FullyQualifiedJavaType> importedTypes) {
    Method method = new Method();
    FullyQualifiedJavaType returnType;
    if (introspectedTable.getGeneratedKey() != null) {
        IntrospectedColumn introspectedColumn = introspectedTable.getColumn(introspectedTable.getGeneratedKey().getColumn());
        if (introspectedColumn == null) {
            // the specified column doesn't exist, so don't do the generated
            // key
            // (the warning has already been reported)
            returnType = null;
        } else {
            returnType = introspectedColumn.getFullyQualifiedJavaType();
            importedTypes.add(returnType);
        }
    } else {
        returnType = null;
    }
    method.setReturnType(returnType);
    method.setVisibility(JavaVisibility.PUBLIC);
    DAOMethodNameCalculator methodNameCalculator = getDAOMethodNameCalculator();
    method.setName(methodNameCalculator.getInsertMethodName(introspectedTable));
    FullyQualifiedJavaType parameterType = introspectedTable.getRules().calculateAllFieldsClass();
    importedTypes.add(parameterType);
    //$NON-NLS-1$
    method.addParameter(new Parameter(parameterType, "record"));
    for (FullyQualifiedJavaType fqjt : daoTemplate.getCheckedExceptions()) {
        method.addException(fqjt);
        importedTypes.add(fqjt);
    }
    context.getCommentGenerator().addGeneralMethodComment(method, introspectedTable);
    return method;
}
Also used : IntrospectedColumn(org.mybatis.generator.api.IntrospectedColumn) FullyQualifiedJavaType(org.mybatis.generator.api.dom.java.FullyQualifiedJavaType) DAOMethodNameCalculator(org.mybatis.generator.api.DAOMethodNameCalculator) 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