All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mybatis.jpa.meta.ColumnMetaResolver Maven / Gradle / Ivy

package com.mybatis.jpa.meta;

import org.apache.ibatis.builder.BuilderException;
import org.apache.ibatis.type.*;

import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import java.lang.reflect.Field;

/**
 * @author svili
 **/
public class ColumnMetaResolver {

    public static JdbcType resolveJdbcType(String alias) {
        if (alias == null) {
            return null;
        }
        try {
            return JdbcType.valueOf(alias);
        } catch (IllegalArgumentException e) {
            throw new BuilderException("Error resolving JdbcType. Cause: " + e, e);
        }
    }

    public static String resolveJdbcAlias(Field field) {

        Class fieldType = field.getType();
        if (field.getType().isEnum()) {
            if (field.isAnnotationPresent(Enumerated.class)) {
                Enumerated enumerated = field.getAnnotation(Enumerated.class);
                if (enumerated.value() == EnumType.ORDINAL) {
                    return "INTEGER";
                }
            }
            return "VARCHAR";
        }
        if (Integer.class.equals(fieldType)) {
            return "INTEGER";
        }
        if (Double.class.equals(fieldType)) {
            return "DOUBLE";
        }
        if (Float.class.equals(fieldType)) {
            return "FLOAT";
        }
        if (String.class.equals(fieldType)) {
            return "VARCHAR";
        }
        if (java.util.Date.class.isAssignableFrom(fieldType)) {
            return "TIMESTAMP";
        }
        return null;
    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    public static Class> resolveTypeHandler(Field field) {

        if (field.getType().isEnum()) {
            if (field.isAnnotationPresent(Enumerated.class)) {
                Enumerated enumerated = field.getAnnotation(Enumerated.class);
                if (enumerated.value() == EnumType.ORDINAL) {
                    EnumOrdinalTypeHandler> typeHandler = new EnumOrdinalTypeHandler(
                    		field.getType());
                    return (Class>) typeHandler.getClass();

                }
            }
            EnumTypeHandler> typeHandler = new EnumTypeHandler(field.getType());
            return (Class>) typeHandler.getClass();
        }

        Class> typeHandlerClass = null;
        if (field.getType().equals(Boolean.class)) {
            typeHandlerClass = BooleanTypeHandler.class;
        }
        return typeHandlerClass;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy