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

com.blazebit.persistence.impl.hibernate.PreparedStatementInvocationHandler Maven / Gradle / Ivy

There is a newer version: 1.1.1
Show newest version
package com.blazebit.persistence.impl.hibernate;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Map;


public class PreparedStatementInvocationHandler implements InvocationHandler {

    private final PreparedStatement delegate;
    private final Map aliasIndex;
    private final HibernateReturningResult returningResult;

    public PreparedStatementInvocationHandler(PreparedStatement delegate, String[][] columns, HibernateReturningResult returningResult) {
        this.delegate = delegate;
        this.aliasIndex = new HashMap(columns.length);
        this.returningResult = returningResult;
        
        for (int i = 0; i < columns.length; i++) {
            aliasIndex.put(columns[i][1], i + 1);
        }
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        if ("executeQuery".equals(method.getName()) && method.getParameterTypes().length == 0) {
            ResultSet rs;
            HibernateReturningResult result;
            
            if (delegate.execute()) {
                rs = delegate.getResultSet();
                result = returningResult;
            } else {
                result = null;
                returningResult.setUpdateCount(delegate.getUpdateCount());
                rs = delegate.getGeneratedKeys();
            }
            
            return Proxy.newProxyInstance(rs.getClass().getClassLoader(), new Class[]{ ResultSet.class }, new ResultSetInvocationHandler(rs, aliasIndex, result));
        }
        
        return method.invoke(delegate, args);
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy