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

io.github.sinri.keel.mysql.matrix.ResultRow 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.core.json.JsonifiableEntity;
import io.github.sinri.keel.mysql.NamedMySQLConnection;
import io.github.sinri.keel.mysql.exception.KeelSQLResultRowIndexError;
import io.github.sinri.keel.mysql.statement.AbstractReadStatement;
import io.vertx.core.Future;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;
import io.vertx.sqlclient.SqlConnection;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.function.Function;

/**
 * @since 2.7
 */
public interface ResultRow extends JsonifiableEntity {
    /**
     * @since 3.0.11
     * @since 3.0.18 Finished Technical Preview.
     */
    static  Future>> fetchResultRowsToCategorizedMap(
            @Nonnull NamedMySQLConnection namedSqlConnection,
            @Nonnull AbstractReadStatement readStatement,
            @Nonnull Class classOfTableRow,
            @Nonnull Function categoryGenerator
    ) {
        return fetchResultRowsToCategorizedMap(namedSqlConnection.getSqlConnection(), readStatement, classOfTableRow, categoryGenerator);
    }

    /**
     * @since 3.0.11
     * @since 3.0.18 Finished Technical Preview.
     */
    static  Future> fetchResultRowsToUniqueKeyBoundMap(
            @Nonnull NamedMySQLConnection namedMySQLConnection,
            @Nonnull AbstractReadStatement readStatement,
            @Nonnull Class classOfTableRow,
            @Nonnull Function uniqueKeyGenerator
    ) {
        return fetchResultRowsToUniqueKeyBoundMap(namedMySQLConnection.getSqlConnection(), readStatement, classOfTableRow, uniqueKeyGenerator);
    }

    /**
     * @since 3.0.11
     * @since 3.0.18 Finished Technical Preview.
     */
    static  Future> fetchResultRows(
            @Nonnull NamedMySQLConnection namedMySQLConnection,
            @Nonnull AbstractReadStatement readStatement,
            @Nonnull Class classOfTableRow
    ) {
        return fetchResultRows(namedMySQLConnection.getSqlConnection(), readStatement, classOfTableRow);
    }

    /**
     * @since 3.0.11
     * @since 3.0.18 Finished Technical Preview.
     */
    static  Future fetchResultRow(
            @Nonnull NamedMySQLConnection namedMySQLConnection,
            @Nonnull AbstractReadStatement readStatement,
            @Nonnull Class classOfTableRow
    ) {
        return fetchResultRow(namedMySQLConnection.getSqlConnection(), readStatement, classOfTableRow);
    }


    /**
     * @since 2.9.4
     */
    static  Future>> fetchResultRowsToCategorizedMap(
            @Nonnull SqlConnection sqlConnection,
            @Nonnull AbstractReadStatement readStatement,
            @Nonnull Class classOfTableRow,
            @Nonnull Function categoryGenerator
    ) {
        Map> map = new HashMap<>();
        return fetchResultRows(sqlConnection, readStatement, classOfTableRow)
                .compose(list -> {
                    list.forEach(item -> {
                        K category = categoryGenerator.apply(item);
                        map.computeIfAbsent(category, k -> new ArrayList<>()).add(item);
                    });
                    return Future.succeededFuture(map);
                });
    }

    /**
     * @since 2.9.4
     */
    static  Future> fetchResultRowsToUniqueKeyBoundMap(
            @Nonnull SqlConnection sqlConnection,
            @Nonnull AbstractReadStatement readStatement,
            @Nonnull Class classOfTableRow,
            @Nonnull Function uniqueKeyGenerator
    ) {
        Map map = new HashMap<>();
        return fetchResultRows(sqlConnection, readStatement, classOfTableRow)
                .compose(list -> {
                    list.forEach(item -> {
                        K uniqueKey = uniqueKeyGenerator.apply(item);
                        map.put(uniqueKey, item);
                    });
                    return Future.succeededFuture(map);
                });
    }

    /**
     * 在给定的SqlConnection上利用指定的AbstractReadStatement进行SQL查询(查询失败时异步报错);
     * 尝试获取查询结果;
     * 如果存在,将所有行以classOfTableRow指定的类进行封装,异步返回此实例构成的List。
     */
    static  Future> fetchResultRows(
            @Nonnull SqlConnection sqlConnection,
            @Nonnull AbstractReadStatement readStatement,
            @Nonnull Class classOfTableRow
    ) {
        return readStatement.execute(sqlConnection)
                .compose(resultMatrix -> {
                    List ts = resultMatrix.buildTableRowList(classOfTableRow);
                    return Future.succeededFuture(ts);
                });
    }

    /**
     * 在给定的SqlConnection上利用指定的AbstractReadStatement进行SQL查询(查询失败时异步报错);
     * 尝试获取查询结果;
     * 如果不存在,异步返回null;
     * 如果存在,将第一行以classOfTableRow指定的类进行封装,异步返回此实例。
     */
    static  Future fetchResultRow(
            @Nonnull SqlConnection sqlConnection,
            @Nonnull AbstractReadStatement readStatement,
            @Nonnull Class classOfTableRow
    ) {
        return readStatement.execute(sqlConnection)
                .compose(resultMatrix -> {
                    try {
                        T t = resultMatrix.buildTableRowByIndex(0, classOfTableRow);
                        return Future.succeededFuture(t);
                    } catch (KeelSQLResultRowIndexError e) {
                        return Future.succeededFuture(null);
                    }
                });
    }

    static JsonArray batchToJsonArray(@Nonnull Collection rows) {
        JsonArray array = new JsonArray();
        rows.forEach(row -> array.add(row.getRow()));
        return array;
    }

    static JsonArray batchToJsonArray(@Nonnull Collection rows, @Nonnull Function transformer) {
        JsonArray array = new JsonArray();
        rows.forEach(row -> array.add(transformer.apply(row)));
        return array;
    }

    default JsonObject getRow() {
        return toJsonObject();
    }

    /**
     * @since 2.8
     * @since 2.9.4 fix null field error
     */
    @Nullable
    default String readDateTime(@Nonnull  String field) {
        String s = readString(field);
        if (s == null) return null;
        return LocalDateTime.parse(s)
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    @Nullable
    default String readDate(@Nonnull String field) {
        return readString(field);
    }

    /**
     * @since 2.9.4 fix null field error
     */
    @Nullable
    default String readTime(@Nonnull String field) {
        var s = readString(field);
        if (s == null) return null;
        return s
                .replaceAll("[PTS]+", "")
                .replaceAll("[HM]", ":");
    }

    @Nullable
    default String readTimestamp(@Nonnull String field) {
        return readDateTime(field);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy