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

io.zero88.jooqx.adapter.SQLResultAdapter Maven / Gradle / Ivy

package io.zero88.jooqx.adapter;

import java.util.Collection;
import java.util.List;

import org.jooq.DSLContext;
import org.jooq.Field;
import org.jooq.Record;
import org.jooq.Table;
import org.jooq.TableLike;
import org.jooq.TableRecord;
import org.jooq.impl.DSL;

import io.zero88.jooqx.JsonRecord;
import io.zero88.jooqx.SQLResultCollector;
import io.zero88.jooqx.adapter.SQLCollectorPart.IdentityCollectorPart;
import io.zero88.jooqx.datatype.DataTypeMapperRegistry;

import lombok.NonNull;

/**
 * SQL Result adapter receives Result set then mapping to expected result
 *
 * @param  Type of jOOQ Table in Query context
 * @param  Type of an expectation output
 * @see TableLike
 * @see Record
 * @see SQLResultCollector
 * @since 1.0.0
 */
public interface SQLResultAdapter extends HasStrategy {

    @SuppressWarnings( {"rawtypes", "unchecked"})
    static IdentityCollectorPart> byJson() {
        return new IdentityCollectorPart<>((dsl, queryTbl) -> JsonRecord.create((TableLike) queryTbl));
    }

    static  IdentityCollectorPart byRecord(R record) {
        return new IdentityCollectorPart<>((dsl, queryTbl) -> dsl.newRecord(DSL.table(record)));
    }

    static IdentityCollectorPart byFields(Collection> fields) {
        return new IdentityCollectorPart<>((dsl, queryTbl) -> dsl.newRecord(fields));
    }

    static  SQLCollectorPart, R> byClass(Class outputClass) {
        return byJson().andThen(r -> r.into(outputClass));
    }

    @SuppressWarnings("unchecked")
    static > IdentityCollectorPart byTable(T table) {
        return new IdentityCollectorPart<>((dsl, queryTbl) -> table instanceof Table
                                                              ? ((Table) table).newRecord()
                                                              : dsl.newRecord(table.asTable()));
    }

    /**
     * A current context holder
     *
     * @return jOOQ table
     * @see TableLike
     */
    @NonNull T table();

    /**
     * Collect result set to expected result
     *
     * @param resultSet result set
     * @param collector result collector
     * @param dsl       jOOQ DSL context
     * @param registry  SQL data type mapper registry
     * @return an expected result
     * @see DataTypeMapperRegistry
     */
    @NonNull  R collect(@NonNull RS resultSet, @NonNull SQLResultCollector collector, @NonNull DSLContext dsl,
                            @NonNull DataTypeMapperRegistry registry);

    /**
     * Indicates select many row
     *
     * @see SQLResultAdapter
     * @since 1.0.0
     */
    interface SQLResultListAdapter extends SQLResultAdapter> {

        @Override
        @NonNull  List collect(@NonNull RS resultSet, @NonNull SQLResultCollector collector,
                                      @NonNull DSLContext dsl, @NonNull DataTypeMapperRegistry registry);

        @Override
        default @NonNull SelectStrategy strategy() {
            return SelectStrategy.MANY;
        }

    }


    /**
     * Indicates select only one row
     *
     * @since 1.0.0
     */
    interface SQLResultOneAdapter extends SQLResultAdapter {

        @Override
        default @NonNull SelectStrategy strategy() {
            return SelectStrategy.FIRST_ONE;
        }

        @Override
        @NonNull  R collect(@NonNull RS resultSet, @NonNull SQLResultCollector collector,
                                @NonNull DSLContext dsl, @NonNull DataTypeMapperRegistry registry);

    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy