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

io.github.afezeria.freedao.classic.runtime.context.DaoContext Maven / Gradle / Ivy

package io.github.afezeria.freedao.classic.runtime.context;

import io.github.afezeria.freedao.classic.runtime.ResultHandler;
import io.github.afezeria.freedao.classic.runtime.SqlExecutor;
import io.github.afezeria.freedao.classic.runtime.SqlSignature;

import javax.sql.DataSource;
import java.sql.Connection;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;

/**
 *
 */
public abstract class DaoContext {
    private DaoContext delegate;

    public synchronized void setDelegate(DaoContext delegate) {
        Objects.requireNonNull(delegate);
        if (this.delegate == null) {
            this.delegate = delegate;
        }
    }

    public DaoContext getDelegate() {
        if (delegate == null) {
            throw new IllegalStateException("uninitialized context");
        }
        return delegate;
    }

    public  T withConnection(Function supplier) {
        return getDelegate().withConnection(supplier);
    }


    public Object[] buildSql(SqlSignature signature, Object[] args, Function buildSqlClosure) {
        return getDelegate().buildSql(signature, args, buildSqlClosure);
    }

    public  T execute(SqlSignature signature, Object[] methodArgs, String sql, List sqlArgs, SqlExecutor executor, ResultHandler resultHandler) {
        return getDelegate().execute(signature, methodArgs, sql, sqlArgs, executor, resultHandler);
    }

    public  T proxy(SqlSignature signature, Object... methodArgs) {
        return delegate.proxy(signature, methodArgs);
    }

    public static DaoContext create(DataSource dataSource) {
        return create(
                new TransactionContext(dataSource),
                new ExecutorContext(),
                new JoinQueryContext(),
                new PageQueryContext(),
                new ParameterContext(null),
                new ProxyContext()
        );
    }

    public static DaoContext create(DaoContext... contexts) {
        if (contexts.length == 0) {
            throw new IllegalArgumentException("contexts.length must be greater than 0");
        }
        if (contexts.length == 1) {
            return contexts[0];
        }
        for (int i = 1; i < contexts.length; i++) {
            contexts[i].setDelegate(contexts[i - 1]);
        }
        return contexts[contexts.length - 1];
    }

}