package com.eactive.eai.rms.kakao.rolling;

import org.hibernate.EmptyInterceptor;
import org.hibernate.Interceptor;

import java.util.ArrayList;
import java.util.List;

public class CompositeHibernateInterceptorImpl extends EmptyInterceptor implements CompositeHibernateInterceptor {

    private final List<Interceptor> interceptors = new ArrayList<>();

    public void addInterceptor(Interceptor interceptor) {
        interceptors.add(interceptor);
    }

    @Override
    public String onPrepareStatement(String sql) {
        return interceptors.stream()
                .reduce(sql,
                        (currentSql, interceptor) -> interceptor.onPrepareStatement(currentSql),
                        (sql1, sql2) -> sql2);

    }

}
