com.bixuebihui.jdbc.event.BaseAfterChangeEventHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of c-dbtools Show documentation
Show all versions of c-dbtools Show documentation
a fast small database connection pool and a active record flavor mini framework
package com.bixuebihui.jdbc.event;
import com.bixuebihui.jdbc.IPrimaryKeyedTable;
import java.util.Arrays;
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 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(String query, Object[] param, ChangeType changeType) {
formEvent(changeType, null, null);
}
@Override
public void afterChange(String query, Object[] param, ChangeType changeType, String shardTableName) {
formEvent(changeType, shardTableName, null);
}
@Override
public void afterChangeByKey(V v, ChangeType changeType) {
formEventByIds(changeType, null, v);
}
@Override
public void afterChangeByKey(V[] id, ChangeType changeType) {
formEventByIds(changeType, null, id);
}
}