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

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

Go to download

Library to make PostgreSQL stored procedures available through simple Java "*SProcService" interfaces including automatic object serialization and deserialization (using typemapper and convention-over-configuration). Supports sharding, advisory locking, statement timeouts and PostgreSQL types such as enums and hstore.

There is a newer version: 2.0.0
Show newest version
package de.zalando.sprocwrapper.proxy;

import java.lang.reflect.Method;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.UUID;

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 if (clazz.isAssignableFrom(UUID.class)) {
            final PGobject pgobj = new PGobject();
            pgobj.setType(typeName);
            try {
                pgobj.setValue(value.toString());
            } catch (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);
                }
            }
        } 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 - 2024 Weber Informatics LLC | Privacy Policy