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

com.bixuebihui.jdbc.event.BaseAfterChangeEventHandler Maven / Gradle / Ivy

Go to download

a fast small database connection pool and a active record flavor mini framework

The newest version!
package com.bixuebihui.jdbc.event;

import com.bixuebihui.jdbc.IPrimaryKeyedTable;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.Collections.emptyList;

/**
 * @author xwx
 */
public abstract class BaseAfterChangeEventHandler implements AfterChangeEventHandler{
    IPrimaryKeyedTable dbOperator;

    public BaseAfterChangeEventHandler(IPrimaryKeyedTable dbOperator) {
        this.dbOperator = dbOperator;
    }

    public abstract void sendEvent(ChangeEvent e);

    void formEvent(ChangeType changeType, String tableName, T[] infos){
        sendEvent( new ChangeEvent<>(dbOperator.getDatabaseName(),
                dbOperator.getTableName(tableName),
                dbOperator.getKeyName(),
                infos==null? emptyList(): Arrays.stream(infos).map(info -> dbOperator.getId(info)).collect(Collectors.toList()),
                changeType));
    }
    void formEvent(ChangeType changeType, String tableName, List infos){
        sendEvent( new ChangeEvent<>(dbOperator.getDatabaseName(),
                dbOperator.getTableName(tableName),
                dbOperator.getKeyName(),
                infos.stream().map(info -> dbOperator.getId(info)).collect(Collectors.toList()),
                changeType));
    }
    void formEvent(ChangeType changeType, String tableName, T info){
        V id = dbOperator.getId(info);
        sendEvent( new ChangeEvent<>(dbOperator.getDatabaseName(),
                dbOperator.getTableName(tableName),
                dbOperator.getKeyName(),
                id==null? emptyList(): List.of(id),
                changeType));
    }

    void formEvent(ChangeType changeType, String tableName, int influnceRecordCount, String query, Object[] params){
        sendEvent( new ChangeEvent<>(dbOperator.getDatabaseName(),
                dbOperator.getTableName(tableName),
                dbOperator.getKeyName(),
                changeType, influnceRecordCount, query + summary(params)));
    }

    protected String summary(Object[] params) {
        return Arrays.stream(params).map(p -> StringUtils.abbreviate(p==null?"": p.toString(), 250)).collect(Collectors.joining(","));
    }

    void formEventByIds(ChangeType changeType, String tableName, V... ids){
        sendEvent( new ChangeEvent<>(dbOperator.getDatabaseName(),
                dbOperator.getTableName(tableName),
                dbOperator.getKeyName(),
                ids==null? emptyList() : Arrays.stream(ids).collect(Collectors.toList()),
                changeType ));
    }

    @Override
    public void afterChange(T info, ChangeType changeType) {
        formEvent(changeType, null, info);
    }

    @Override
    public void afterChange(T info, ChangeType changeType, String shardTableName) {
        formEvent(changeType, shardTableName, info);
    }

    @Override
    public void afterChange(T[] infos, ChangeType changeType) {
        formEvent(changeType, null, infos);
    }

    @Override
    public void afterChange(List infos, ChangeType changeType) {
        formEvent(changeType, null, infos);
    }
    @Override
    public void afterChange(String query, Object[] param, ChangeType changeType, int influenceRecordCount) {
        formEvent(changeType, null, influenceRecordCount, query, param);
    }

    @Override
    public void afterChange(String query, Object[] param, ChangeType changeType, String shardTableName, int influenceRecordCount) {
        formEvent(changeType, shardTableName, influenceRecordCount, query, param);
    }

    @Override
    public void afterChangeByKey(V v, ChangeType changeType) {
        formEventByIds(changeType, null, v);
    }

    @Override
    public void afterChangeByKey(V[] id, ChangeType changeType) {
        formEventByIds(changeType, null, id);
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy