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

org.babyfish.jimmer.sql.runtime.Selectors Maven / Gradle / Ivy

There is a newer version: 0.9.19
Show newest version
package org.babyfish.jimmer.sql.runtime;

import org.babyfish.jimmer.runtime.Internal;
import org.babyfish.jimmer.sql.ast.Selection;
import org.babyfish.jimmer.sql.fetcher.impl.FetcherUtil;
import org.jetbrains.annotations.Nullable;

import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.Consumer;

public class Selectors {

    private static final AtomicLong CURSOR_ID_SEQUENCE = new AtomicLong();

    private Selectors() {}

    @SuppressWarnings("unchecked")
    public static  List select(
            JSqlClientImplementor sqlClient,
            Connection con,
            String sql,
            List variables,
            @Nullable List variablePositions,
            List> selections,
            ExecutionPurpose purpose
    ) {
        List rows = sqlClient.getExecutor().execute(
                new Executor.Args<>(
                        sqlClient,
                        con,
                        sql,
                        variables,
                        variablePositions,
                        purpose,
                        null,
                        stmt -> {
                            Reader reader = Readers.createReader(sqlClient, selections);
                            return Internal.usingSqlDraftContext(draftCtx -> {
                                Reader.Context ctx = new Reader.Context(draftCtx, sqlClient);
                                List results = new ArrayList<>();
                                try (ResultSet resultSet = stmt.executeQuery()) {
                                    while (resultSet.next()) {
                                        results.add((R)reader.read(resultSet, ctx));
                                        ctx.resetCol();
                                    }
                                }
                                return results;
                            });
                        }
                )
        );
        FetcherUtil.fetch(sqlClient, con, selections, rows);
        return rows;
    }

    @SuppressWarnings("unchecked")
    public static  void forEach(
            JSqlClientImplementor sqlClient,
            Connection con,
            String sql,
            List variables,
            @Nullable List variablePositions,
            List> selections,
            ExecutionPurpose purpose,
            int batchSize,
            Consumer consumer
    ) {
        Executor executor = sqlClient.getExecutor();
        long cursorId = CURSOR_ID_SEQUENCE.incrementAndGet();
        Executor.Args args = new Executor.Args<>(
                sqlClient,
                con,
                sql,
                variables,
                variablePositions,
                purpose,
                null,
                stmt -> {
                    Reader reader = Readers.createReader(sqlClient, selections);
                    return Internal.usingSqlDraftContext((draftContext) -> {
                        Reader.Context ctx = new Reader.Context(draftContext, sqlClient);
                        List results = new ArrayList<>();
                        try (ResultSet resultSet = stmt.executeQuery()) {
                            while (resultSet.next()) {
                                results.add((R) reader.read(resultSet, ctx));
                                ctx.resetCol();
                                if (results.size() >= batchSize) {
                                    FetcherUtil.fetch(sqlClient, con, selections, results);
                                    for (R result : results) {
                                        consumer.accept(result);
                                    }
                                    results.clear();
                                }
                            }
                        }
                        FetcherUtil.fetch(sqlClient, con, selections, results);
                        for (R result : results) {
                            consumer.accept(result);
                        }
                        return null;
                    });
                },
                cursorId
        );
        executor.openCursor(cursorId, sql, variables, variablePositions, purpose, args.ctx, sqlClient);
        Long oldCursorId = Cursors.setCurrentCursorId(cursorId);
        try {
            executor.execute(args);
        } finally {
            Cursors.setCurrentCursorId(oldCursorId);
        }
    }
}