Package com.digitolio.jdbi

Source Code of com.digitolio.jdbi.MapAsBeanFactory

package com.digitolio.jdbi;

import org.skife.jdbi.v2.Query;
import org.skife.jdbi.v2.SQLStatement;
import org.skife.jdbi.v2.sqlobject.SqlStatementCustomizer;
import org.skife.jdbi.v2.sqlobject.SqlStatementCustomizerFactory;
import org.skife.jdbi.v2.sqlobject.SqlStatementCustomizingAnnotation;

import java.lang.annotation.*;
import java.lang.reflect.Method;
import java.sql.SQLException;

@Retention(RetentionPolicy.RUNTIME)
@SqlStatementCustomizingAnnotation(MapResultForSnakeCaseAsBean.MapAsBeanFactory.class)
@Target(ElementType.METHOD)
public @interface MapResultForSnakeCaseAsBean
{
    NamingStrategy dbNamingStrategy() default NamingStrategy.UPPER_UNDERSCORE;

    NamingStrategy fieldNamingStrategy() default NamingStrategy.IDENTICAL;

    public static class MapAsBeanFactory implements SqlStatementCustomizerFactory
    {

        @Override
        public SqlStatementCustomizer createForMethod(final Annotation annotation, Class sqlObjectType, Method method)
        {
            return new SqlStatementCustomizer()
            {
                @Override
                public void apply(SQLStatement s) throws SQLException
                {
                   Query q = (Query) s;
                   NamingStrategy dbNamingStrategy = ((MapResultForSnakeCaseAsBean) annotation).dbNamingStrategy();
                   NamingStrategy fieldNamingStrategy = ((MapResultForSnakeCaseAsBean) annotation).fieldNamingStrategy();
                   q.registerMapper(new FormatterAwareBeanMapperFactory(dbNamingStrategy,fieldNamingStrategy));
                }
            };
        }

        @Override
        public SqlStatementCustomizer createForType(Annotation annotation, Class sqlObjectType)
        {
            throw new UnsupportedOperationException("Not allowed on type");
        }

        @Override
        public SqlStatementCustomizer createForParameter(Annotation annotation, Class sqlObjectType, Method method, Object arg)
        {
            throw new UnsupportedOperationException("Not allowed on parameter");
        }
    }
}
TOP

Related Classes of com.digitolio.jdbi.MapAsBeanFactory

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.