JavaToMetaTypeImpl.java :  » Database-ORM » ODAL » com » completex » objective » components » persistency » Java Open Source

Java Open Source » Database ORM » ODAL 
ODAL » com » completex » objective » components » persistency » JavaToMetaTypeImpl.java
/**
 *  Objective Database Abstraction Layer (ODAL)
 *  Copyright (c) 2004, The ODAL Development Group
 *  All rights reserved.
 *  For definition of the ODAL Development Group please refer to LICENCE.txt file
 *
 *  Distributable under LGPL license.
 *  See terms of license at gnu.org.
 */
package com.completex.objective.components.persistency;

import java.sql.Types;
import java.util.HashMap;

/**
 * @author Gennady Krizhevsky
 */
public class JavaToMetaTypeImpl implements JavaToMetaType {

    private boolean usePrimitives;
    private boolean usePrimitivesForRequired;
    private boolean char1boolean;

    public JavaToMetaTypeImpl(boolean usePrimitives, boolean usePrimitivesForRequired, boolean char1boolean) {
        this.usePrimitives = usePrimitives;
        this.usePrimitivesForRequired = usePrimitivesForRequired;
        this.char1boolean = char1boolean;
    }

    public JavaToMetaTypeImpl() {
        this(false, false, false);
    }

    public ColumnType dataType(String dataType, int columnSize, int decimalDigits, boolean required) {
        int dbType = Integer.parseInt(dataType);
        return dataType(dbType, decimalDigits, columnSize, required);

    }

    public ColumnType dataType(int jdbcType, int decimalDigits, int columnSize, boolean required) {
        switch (jdbcType) {
            case Types.BIT:
            case Types.TINYINT:
            case Types.SMALLINT:
            case Types.INTEGER:
            case Types.BIGINT:
                return resolveLong(usePrimitives, usePrimitivesForRequired, required);

            case Types.FLOAT:
                return resolveDouble(usePrimitives, usePrimitivesForRequired, required);
            case Types.REAL:
                return resolveDouble(usePrimitives, usePrimitivesForRequired, required);
            case Types.DOUBLE:
                return resolveDouble(usePrimitives, usePrimitivesForRequired, required);

            case Types.NUMERIC:
            case Types.DECIMAL:
                if (decimalDigits == 0) {
                    return resolveLong(usePrimitives, usePrimitivesForRequired, required);
                } else {
                    return resolveDouble(usePrimitives, usePrimitivesForRequired, required);
                }
            case Types.CHAR:
                return resolveBoolean(char1boolean, columnSize, usePrimitives, usePrimitivesForRequired, required);
            case Types.VARCHAR:
            case Types.LONGVARCHAR:
                return ColumnType.STRING;
            case Types.CLOB:
                return ColumnType.CLOB_STRING;

            case Types.DATE:
            case Types.TIME:
            case Types.TIMESTAMP:
                return ColumnType.DATE;

            case Types.BINARY:
            case Types.VARBINARY:
            case Types.LONGVARBINARY:
            case Types.OTHER:

                return ColumnType.BINARY;
            case Types.BLOB:
                return ColumnType.BLOB;
            case Types.STRUCT:
            case Types.ARRAY:
            case Types.DISTINCT:
            case Types.JAVA_OBJECT:
            case Types.REF:
            case Types.DATALINK:
                return ColumnType.OBJECT;
            default:
                return ColumnType.OBJECT;
        }
    }

    protected ColumnType resolveLong( boolean usePrimitives,
                                        boolean usePrimitivesForRequired,
                                        boolean required) {
        if (isPrimitive(usePrimitives, usePrimitivesForRequired, required)) {
            return ColumnType.LONG_PRIMITIVE;
        } else {
            return ColumnType.LONG;
        }
    }

    private boolean isPrimitive(boolean usePrimitives, boolean usePrimitivesForRequired, boolean required) {
        return usePrimitives || (usePrimitivesForRequired && required);
    }

    protected ColumnType resolveDouble( boolean usePrimitives,
                                        boolean usePrimitivesForRequired,
                                        boolean required) {
        if (isPrimitive(usePrimitives, usePrimitivesForRequired, required)) {
            return ColumnType.DOUBLE_PRIMITIVE;
        } else {
            return ColumnType.BIGDECIMAL;
        }
    }
    
    protected ColumnType resolveBoolean(boolean char1boolean, int columnSize,
                                        boolean usePrimitives,
                                        boolean usePrimitivesForRequired,
                                        boolean required) {
        if (char1boolean && columnSize == 1) {
            if (isPrimitive(usePrimitives, usePrimitivesForRequired, required)) {
                return ColumnType.BOOLEAN_PRIMITIVE;
            } else {
                return ColumnType.BOOLEAN;
            }
        } else {
            return ColumnType.CHAR;
        }
    }

    public String jdbcTypeToString(int dataType) {
        switch (dataType) {
            case Types.BIT:
                return "BIT";
            case Types.TINYINT:
                return "TINYINT";
            case Types.SMALLINT:
                return "SMALLINT";
            case Types.INTEGER:
                return "INTEGER";
            case Types.BIGINT:
                return "BIGINT";
            case Types.FLOAT:
                return "FLOAT";
            case Types.REAL:
                return "REAL";
            case Types.DOUBLE:
                return "DOUBLE";
            case Types.NUMERIC:
                return "NUMERIC";
            case Types.DECIMAL:
                return "DECIMAL";
            case Types.CHAR:
                return "CHAR";
            case Types.VARCHAR:
                return "VARCHAR";
            case Types.LONGVARCHAR:
                return "LONGVARCHAR";
            case Types.DATE:
                return "DATE";
            case Types.TIME:
                return "TIME";
            case Types.TIMESTAMP:
                return "TIMESTAMP";
            case Types.BINARY:
                return "BINARY";
            case Types.VARBINARY:
                return "VARBINARY";
            case Types.LONGVARBINARY:
                return "LONGVARBINARY";
            case Types.OTHER:
                return "OTHER";
            case Types.BLOB:
                return "BLOB";
            default:
                return "OBJECT";
        }
    }

    public static final HashMap DB_2_JAVA_TYPES = new HashMap();

    static {
        DB_2_JAVA_TYPES.put("NUMBER", new Integer(Types.DECIMAL));
        DB_2_JAVA_TYPES.put("CHAR", new Integer(Types.CHAR));
        DB_2_JAVA_TYPES.put("INTEGER", new Integer(Types.INTEGER));
        DB_2_JAVA_TYPES.put("VARCHAR", new Integer(Types.VARCHAR));
        DB_2_JAVA_TYPES.put("VARCHAR2", new Integer(Types.VARCHAR));
        DB_2_JAVA_TYPES.put("DECIMAL", new Integer(Types.DECIMAL));
        DB_2_JAVA_TYPES.put("DATE", new Integer(Types.DATE));
        DB_2_JAVA_TYPES.put("TIME", new Integer(Types.TIME));
        DB_2_JAVA_TYPES.put("TIMESTAMP", new Integer(Types.TIMESTAMP));
        DB_2_JAVA_TYPES.put("BLOB", new Integer(Types.BLOB));
        DB_2_JAVA_TYPES.put("CLOB", new Integer(Types.CLOB));
        DB_2_JAVA_TYPES.put("BIT", new Integer(Types.BIT));
        DB_2_JAVA_TYPES.put("LONGVARCHAR", new Integer(Types.LONGVARCHAR));
        DB_2_JAVA_TYPES.put("OBJECT", new Integer(0));
    }

    public int jdbcTypeByDatabaseOne(String databaseType) {
        Integer type = (Integer) DB_2_JAVA_TYPES.get(databaseType);
        return type == null ? 0 : type.intValue();
    }

    public ColumnType dataTypeByDatabaseOne(String databaseType, int decimalDigits, int columnSize, boolean required) {
        int jdbcApproximmation = jdbcTypeByDatabaseOne(databaseType);
        return dataType(jdbcApproximmation, decimalDigits, columnSize, required);
    }

    public boolean isUsePrimitives() {
        return usePrimitives;
    }

    public boolean isUsePrimitivesForRequired() {
        return usePrimitivesForRequired;
    }

    public boolean isChar1boolean() {
        return char1boolean;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.