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

io.zero88.jooqx.BindBatchValues Maven / Gradle / Ivy

The newest version!
package io.zero88.jooqx;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.jooq.Field;
import org.jooq.InsertSetStep;
import org.jooq.MergeMatchedSetStep;
import org.jooq.Name;
import org.jooq.Record;
import org.jooq.UpdateSetStep;

import lombok.NonNull;

/**
 * Represents a holder keep dummy value and list of binding records
 *
 * @apiNote With {@code dummy value records}, you can predefine a field value, then if a bind record is missing this
 *     field value then system will auto detect and fallback to the predefined value. See: {@link #registerValue(Field,
 *     Object)}
 * @see JDBC batch operations
 * @see InsertSetStep#set(Map)
 * @see UpdateSetStep#set(Map)
 * @see MergeMatchedSetStep#set(Map)
 * @since 1.0.0
 */
public final class BindBatchValues {

    private final Map dummyValues = new LinkedHashMap<>();
    private final List records = new ArrayList<>();

    public BindBatchValues register(@NonNull String... fields) {
        Arrays.stream(fields).filter(Objects::nonNull).forEach(f -> this.dummyValues.put(f, null));
        return this;
    }

    public BindBatchValues register(@NonNull Field... fields) {
        Arrays.stream(fields).filter(Objects::nonNull).forEach(f -> this.dummyValues.put(f, null));
        return this;
    }

    public BindBatchValues register(@NonNull Name... fields) {
        Arrays.stream(fields).filter(Objects::nonNull).forEach(f -> this.dummyValues.put(f, null));
        return this;
    }

    public  BindBatchValues registerValue(@NonNull Field field, Object value) {
        this.dummyValues.put(field, value);
        return this;
    }

    public BindBatchValues add(Record... recs) {
        return this.add(Arrays.asList(recs));
    }

    public BindBatchValues add(Collection recs) {
        recs.stream().filter(Objects::nonNull).forEachOrdered(records::add);
        return this;
    }

    @NonNull
    public List getMappingFields() {
        return dummyValues.keySet().stream().map(this::checkAndReturnField).collect(Collectors.toList());
    }

    @NonNull
    public List getMappingValues() {
        return new ArrayList<>(dummyValues.values());
    }

    @NonNull
    public Map getDummyValues() {
        final LinkedHashMap map = new LinkedHashMap<>();
        this.dummyValues.forEach((k, v) -> map.put(k, null));
        return map;
    }

    @NonNull
    public List getRecords() {
        return Collections.unmodifiableList(this.records);
    }

    /**
     * Batch record size
     *
     * @return total record
     */
    public int size() {
        return this.records.size();
    }

    private String checkAndReturnField(Object field) {
        if (field instanceof String) {
            return (String) field;
        }
        if (field instanceof Field) {
            return ((Field) field).getName();
        }
        return field.toString();
    }

}