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

de.zalando.sprocwrapper.proxy.OtherStoredProcedureParameter Maven / Gradle / Ivy

package de.zalando.sprocwrapper.proxy;

import java.lang.reflect.Method;

import java.sql.Connection;
import java.sql.SQLException;

import org.postgresql.util.PGobject;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.zalando.typemapper.postgres.PgTypeHelper;

/**
 * @author  jmussler
 */
class OtherStoredProcedureParameter extends StoredProcedureParameter {
    private static final Logger LOG = LoggerFactory.getLogger(OtherStoredProcedureParameter.class);

    public OtherStoredProcedureParameter(final Class clazz, final Method m, final String typeName, final int sqlType,
            final int javaPosition, final boolean sensitive) {
        super(clazz, m, typeName, sqlType, javaPosition, sensitive);
    }

    @Override
    public Object mapParam(final Object value, final Connection connection) {
        if (value == null) {
            return null;
        }

        Object result = value;
        if (clazz.isEnum()) {

            // HACK: should be implemented in PgTypeHelper
            final PGobject pgobj = new PGobject();
            pgobj.setType(typeName);
            try {
                pgobj.setValue(((Enum) value).name());
            } catch (final SQLException ex) {
                if (sensitive) {
                    LOG.error("Failed to set PG object value (sensitive parameter, stacktrace hidden)");
                } else {
                    LOG.error("Failed to set PG object value", ex);
                }
            }

            result = pgobj;
        } else {
            try {
                result = PgTypeHelper.asPGobject(value, typeName, connection);
            } catch (final SQLException ex) {
                if (sensitive) {
                    LOG.error("Failed to serialize PG object (sensitive parameter, stacktrace hidden)");
                } else {
                    LOG.error("Failed to serialize PG object", ex);
                }
            }
        }

        return result;
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy