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

io.github.sinri.keel.mysql.matrix.ResultMatrix Maven / Gradle / Ivy

Go to download

A website framework with VERT.X for ex-PHP-ers, exactly Ark Framework Users.

The newest version!
package io.github.sinri.keel.mysql.matrix;


import io.github.sinri.keel.helper.KeelHelpersInterface;
import io.github.sinri.keel.mysql.exception.KeelSQLResultRowIndexError;
import io.vertx.core.Future;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.sqlclient.Row;
import io.vertx.sqlclient.RowSet;
import io.vertx.sqlclient.data.Numeric;

import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.function.BiConsumer;
import java.util.function.Function;

/**
 * @since 1.1
 * @since 1.8 becomes interface
 * May overrides this class to get Customized Data Matrix
 */
public interface ResultMatrix {

    /**
     * @since 1.10
     */
    static  T buildTableRow(JsonObject row, Class classOfTableRow) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        return classOfTableRow.getConstructor(JsonObject.class).newInstance(row);
    }

    /**
     * @since 1.10
     */
    static  List buildTableRowList(List rowList, Class classOfTableRow) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        ArrayList list = new ArrayList<>();
        for (var x : rowList) {
            list.add(ResultMatrix.buildTableRow(x, classOfTableRow));
        }
        return list;
    }

    /**
     * @since 2.8
     */
    static ResultMatrix create(RowSet rowSet) {
        return new ResultMatrixImpl(rowSet);
    }

    List getRowList();

    int getTotalFetchedRows();

    int getTotalAffectedRows();

    long getLastInsertedID();

    JsonArray toJsonArray();

    JsonObject getFirstRow() throws KeelSQLResultRowIndexError;

    JsonObject getRowByIndex(int index) throws KeelSQLResultRowIndexError;

    /**
     * @since 1.10
     */
     T buildTableRowByIndex(int index, Class classOfTableRow) throws KeelSQLResultRowIndexError;

    String getOneColumnOfFirstRowAsDateTime(String columnName) throws KeelSQLResultRowIndexError;

    String getOneColumnOfFirstRowAsString(String columnName) throws KeelSQLResultRowIndexError;

    Numeric getOneColumnOfFirstRowAsNumeric(String columnName) throws KeelSQLResultRowIndexError;

    Integer getOneColumnOfFirstRowAsInteger(String columnName) throws KeelSQLResultRowIndexError;

    Long getOneColumnOfFirstRowAsLong(String columnName) throws KeelSQLResultRowIndexError;

    List getOneColumnAsDateTime(String columnName);

    List getOneColumnAsString(String columnName);

    List getOneColumnAsNumeric(String columnName);

    List getOneColumnAsLong(String columnName);

    List getOneColumnAsInteger(String columnName);

    /**
     * @throws RuntimeException 封装类的时候可能会抛出异常
     * @since 1.10
     */
     List buildTableRowList(Class classOfTableRow);

    /**
     * @since 2.9.4
     */
    default  Future>> buildCategorizedRowsMap(Function categoryGenerator) {
        Map> map = new HashMap<>();
        var list = getRowList();
        list.forEach(item -> {
            K category = categoryGenerator.apply(item);
            map.computeIfAbsent(category, k -> new ArrayList<>()).add(item);
        });
        return Future.succeededFuture(map);
    }

    /**
     * @since 2.9.4
     */
    default  Future> buildUniqueKeyBoundRowMap(Function uniqueKeyGenerator) {
        Map map = new HashMap<>();
        var list = getRowList();
        list.forEach(item -> {
            K uniqueKey = uniqueKeyGenerator.apply(item);
            map.put(uniqueKey, item);
        });
        return Future.succeededFuture(map);
    }

    /**
     * Categorized Rows Map, i.e. category mapping to a list of rows.
     *
     * @since 2.9.4
     */
    default  Future>> buildCategorizedRowsMap(Class classOfTableRow, Function categoryGenerator) {
        Map> map = new HashMap<>();
        var list = buildTableRowList(classOfTableRow);
        list.forEach(item -> {
            K category = categoryGenerator.apply(item);
            map.computeIfAbsent(category, k -> new ArrayList<>()).add(item);
        });
        return Future.succeededFuture(map);
    }

    /**
     * Unique key bound rows map, i.e. One unique Key mapping to one result row.
     * WARNING: if the uniqueKeyGenerator provides duplicated key, the mapped value would be uncertainly single.
     */
    default  Future> buildUniqueKeyBoundRowMap(Class classOfTableRow, Function uniqueKeyGenerator) {
        Map map = new HashMap<>();
        var list = buildTableRowList(classOfTableRow);
        list.forEach(item -> {
            K category = uniqueKeyGenerator.apply(item);
            map.put(category, item);
        });
        return Future.succeededFuture(map);
    }

    /**
     * 类似矩阵转置的玩意。
     *
     * @since 2.9.4
     */
    default  Future> buildCustomizedMap(
            BiConsumer, JsonObject> rowToMapHandler
    ) {
        Map map = new HashMap<>();
        var list = getRowList();
        list.forEach(item -> {
            rowToMapHandler.accept(map, item);
        });
        return Future.succeededFuture(map);
    }

    /**
     * Shrink a result matrix of rows by a set of rows.
     * Yang Rui needs it.
     *
     * @param shrinkByKeys      The keys of fields that would not be shrunk.
     * @param shrinkBodyListKey The key of the shrunk body in result.
     * @since 3.2.2
     */
    default Future> buildShrinkList(
            Collection shrinkByKeys,
            String shrinkBodyListKey
    ) {
        Map keyMap = new HashMap<>();
        Map> bodyMap = new HashMap<>();
        List rowList = getRowList();
        rowList.forEach(item -> {
            JsonObject keyEntity = new JsonObject();
            JsonObject bodyEntity = new JsonObject();

            item.forEach(entry -> {
                if (shrinkByKeys.contains(entry.getKey())) {
                    keyEntity.put(entry.getKey(), entry.getValue());
                } else {
                    bodyEntity.put(entry.getKey(), entry.getValue());
                }
            });

            shrinkByKeys.forEach(sk -> {
                if (!keyEntity.containsKey(sk)) {
                    keyEntity.putNull(sk);
                }
            });
            String skEntityHash = KeelHelpersInterface.KeelHelpers.jsonHelper().getJsonForObjectWhoseItemKeysSorted(keyEntity);

            keyMap.put(skEntityHash, keyEntity);
            bodyMap.computeIfAbsent(skEntityHash, s -> new ArrayList<>())
                    .add(bodyEntity);
        });
        List resultList = new ArrayList<>();
        new TreeMap<>(bodyMap).forEach((k, v) -> {
            JsonObject x = new JsonObject();
            JsonObject keyEntity = keyMap.get(k);
            keyEntity.forEach(e -> {
                x.put(e.getKey(), e.getValue());
            });
            List jsonObjects = bodyMap.get(k);
            x.put(shrinkBodyListKey, jsonObjects);
        });
        return Future.succeededFuture(resultList);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy