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

Java Open Source » Database ORM » ODAL 
ODAL » com » completex » objective » components » persistency » type » DefaultTypeHandler.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.type;

import com.completex.objective.components.persistency.ColumnType;
import com.completex.objective.components.persistency.OdalPersistencyException;
import com.completex.objective.components.persistency.OdalRuntimePersistencyException;
import com.completex.objective.components.persistency.PersistentObject;
import com.completex.objective.components.persistency.core.DatabasePolicy;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Basic TypeHandler that can be used as a base class for majority of implementations
 *
 * @author Gennady Krizhevsky
 */
public class DefaultTypeHandler implements TypeHandler {

    public DefaultTypeHandler() {
    }

    /**
     * Transformes value read from the database
     *
     * @param data value read from the database
     * @return transformed value
     * @throws SQLException
     */
    public Object transformRead(Object data) throws SQLException {
        return data;
    }

    protected Object readValue(ResultSet resultSet, int index, DatabasePolicy databasePolicy) throws SQLException {
        return resultSet.getObject(index);
    }

    protected Object readValue(ResultSet resultSet, String key, DatabasePolicy databasePolicy) throws SQLException {
        return resultSet.getObject(key);
    }

    /**
     * @see TypeHandler
     */
    public Object handleRead(ResultSet resultSet, int index, DatabasePolicy databasePolicy) throws SQLException {
        return transformRead(readValue(resultSet, index, databasePolicy));
    }

    /**
     * @see TypeHandler
     */
    public Object handleRead(ResultSet resultSet, String key, DatabasePolicy databasePolicy) throws SQLException {
        return transformRead(readValue(resultSet, key, databasePolicy));
    }

    /**
     * @see TypeHandler
     */
    public Object handleOutParamRead(CallableStatement statement, int index, DatabasePolicy databasePolicy) throws SQLException {
        return statement.getObject(index);
    }

    /**
     * @see TypeHandler
     */
    public Object handleOutParamRead(CallableStatement statement, String key, DatabasePolicy databasePolicy) throws SQLException {
        return statement.getObject(key);
    }

    /**
     * Transforms data before binding it
     *
     * @param data value to bind
     * @return transformed value
     * @throws SQLException
     */
    protected Object transformBind(Object data) throws SQLException {
        return data;
    }

    /**
     * @see TypeHandler
     */
    public void handleBind(PreparedStatement statement, int index, Object paramValue) throws SQLException {
        handleBind(statement, index, paramValue, 0, false);
    }

    protected void handleBind(PreparedStatement statement, int index, Object paramValue, int jdbcType, boolean useType) throws SQLException {
        if (paramValue != null) {
            Object value = transformBind(paramValue);
            handleBindSetValue(statement, index, value, jdbcType, useType);
        }
    }

    protected void handleBindSetValue(PreparedStatement statement, int index, Object value, int jdbcType, boolean useType) throws SQLException {
        if (value != null) {
            try {
                if (useType && jdbcType != 0) {
                    statement.setObject(index, value, jdbcType);
                } else {
                    statement.setObject(index, value);
                }
            } catch (SQLException e) {
                String message = "Error while binding parameter with index [" + index
                        + "], value [" + value + "] "
                        + "; using " + getClass().getName() + " type handler";
                throw new OdalPersistencyException(message, e);
            }
        } else {
            throw new OdalRuntimePersistencyException("handleBindSetValue is used instead of handleBindNull " +
                    "while binding parameterIndex [" +
                    index + "] to bind NULL value using " + getClass().getName() + " type handler");
        }

    }

    /**
     * Used for binding null values in PreparedStatement
     *
     * @param statement        PreparedStatement
     * @param parameterIndex   1-based binding index
     * @param columnType       column type
     * @param columnIdentifier string identifying the column. It is used in error message if bind fails
     */
    public void handleBindNull(PreparedStatement statement,
                               int parameterIndex,
                               ColumnType columnType,
                               String columnIdentifier) throws OdalPersistencyException {
        if (columnType == null) {
            throw new OdalRuntimePersistencyException("columnType is null value " +
                    "when binding null parameter;  parameterIndex " + parameterIndex + "] using "
                    + getClass().getName() + " type handler");
        }
        try {
            if (columnType.getDefaultJdbcTypeName() == null) {
                statement.setNull(parameterIndex, getJdbcTypeSafe(columnType));
            } else {
                statement.setNull(parameterIndex, getJdbcTypeSafe(columnType), columnType.getDefaultJdbcTypeName());
            }
        } catch (SQLException e) {
            String message = "Error (setNull) while binding null value with "
                    + "index [" + parameterIndex + "] "
                    + "; columnType [" + columnType + "] "
                    + "; using " + getClass().getName() + " type handler";
            throw new OdalPersistencyException(message, e);
        }
    }

    private int getJdbcTypeSafe(ColumnType columnType) throws SQLException {
        int defaultJdbcType = columnType.getDefaultJdbcType();
        if (defaultJdbcType == 0) {
            throw new SQLException("JDBC type is not set for setNull operation");
        }
        return defaultJdbcType;
    }

    /**
     * @see TypeHandler
     */
    public void handleBind(PreparedStatement statement,
                           int index,
                           Object value,
                           DatabasePolicy databasePolicy,
                           LobPostProcessings postProcessings,
                           PersistentObject persistentObject,
                           int fieldIndex) throws SQLException {
        handleBind(statement, index, value);
    }

    public boolean useTwoStepBinaryUpdate(DatabasePolicy databasePolicy) {
        return databasePolicy.useTwoStepBinaryUpdate();
    }
}
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.