sql-trigger

delay queue based on redis

License

License

GroupId

GroupId

com.github.bingoohuang
ArtifactId

ArtifactId

sql-trigger
Last Version

Last Version

0.0.4
Release Date

Release Date

Type

Type

jar
Description

Description

sql-trigger
delay queue based on redis
Project URL

Project URL

http://github.com/bingoohuang/sql-trigger
Source Code Management

Source Code Management

http://github.com/bingoohuang/sql-trigger

Download sql-trigger

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.bingoohuang/sql-trigger/ -->
<dependency>
    <groupId>com.github.bingoohuang</groupId>
    <artifactId>sql-trigger</artifactId>
    <version>0.0.4</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.bingoohuang/sql-trigger/
implementation 'com.github.bingoohuang:sql-trigger:0.0.4'
// https://jarcasting.com/artifacts/com.github.bingoohuang/sql-trigger/
implementation ("com.github.bingoohuang:sql-trigger:0.0.4")
'com.github.bingoohuang:sql-trigger:jar:0.0.4'
<dependency org="com.github.bingoohuang" name="sql-trigger" rev="0.0.4">
  <artifact name="sql-trigger" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.bingoohuang', module='sql-trigger', version='0.0.4')
)
libraryDependencies += "com.github.bingoohuang" % "sql-trigger" % "0.0.4"
[com.github.bingoohuang/sql-trigger "0.0.4"]

Dependencies

compile (9)

Group / Artifact Type Version
org.springframework : spring-context Optional jar 5.0.8.RELEASE
com.github.ben-manes.caffeine : caffeine jar 2.6.2
com.google.auto.service : auto-service jar 1.0-rc4
com.github.bingoohuang : java-utils jar 0.0.12
org.jooq : joor jar 0.9.6
com.alibaba : druid jar 1.1.10
com.github.bingoohuang : eql jar 0.1.3
org.apache.commons : commons-lang3 jar 3.7
org.slf4j : slf4j-api jar 1.7.25

provided (1)

Group / Artifact Type Version
org.projectlombok : lombok Optional jar 1.18.2

test (4)

Group / Artifact Type Version
junit : junit jar 4.12
com.google.truth : truth jar 0.42
com.h2database : h2 jar 1.4.197
ch.qos.logback : logback-classic jar 1.2.3

Project Modules

There are no modules declared in this project.

sql-trigger

Build Status Coverage Status Maven Central License

jdbc sql filter to sql postprocessing like sql rewriting, crud trigger and etc.

Intention

At some cases, we want to trigger extra actions when do add/update/delete in some specified tables, like tt_f_mbr_card which stands for member cards, but java code which execute those sqls will be invoked in more than once places. We won't to copy and paste extra processing here and there. So I create this tiny library, which can intercept sql executions to trigger some extra actions without adding invoking code more than one places.

一个非常实际的场景是,排课,我们需要在排课(增加/复制/修改/删除)的时候,添加额外处理:

  1. 课程开始前3小时,给老师和学员发送即将上课提醒消息;
  2. 课程开始前1小时,执行最少开课人数检查,如果少于最少订课人数,自动取消排课;
  3. 课程开始后,给上课老师计算本课程的课时费用;
  4. 课程结束后,给学员发送课后点评提示消息,点评本节课。
  5. ...

Usage

  1. Add dependency to pom.xml.

    <dependency>
        <groupId>com.github.bingoohuang</groupId>
        <artifactId>sql-trigger</artifactId>
        <version>{sql-trigger-version}</version>
    </dependency>
  2. Use SqlTriggerDriver, just replace jdbc url like jdbc:h2:./src/test/resources/test to jdbc:sqltrigger:h2:./src/test/resources/test.

  3. Or manually coding like:

    private SqlTriggerProxy sqlTriggerProxy = SqlTriggerProxy.createByRegisteredTriggerBeans();
    
    public Connection getConnection() {
        val connection = dataSource.getConnection();
        return sqlTriggerProxy.proxy(connection);
    }
  4. Register the trigger like

    @Data
    public class MbrCardLastActivateDayVo {
        private String mbrCardId;
        private Object latestActivateDay;
    }
    
    @AutoService(SqlTriggerAware.class)
    public class MbrCardTrigger implements SqlTriggerAware {
        /**
         * 新增会员卡时,增加最迟激活日期任务。
         */
        @SqlTrigger(table = {"TT_F_MBR_CARD", "TG_F_MEMBER_CARD"}, type = TriggerType.INSERT)
        public void insert(MbrCardLastActivateDayVo vo) {
            val day = vo.getLatestActivateDay();
            val task = Springs.getBean(MemberCardLatestActivateTask.class);
            val dt = day instanceof DateTime ? (DateTime) day : DateTimes.parse((String) day);
            task.submit(vo.getMbrCardId(), dt);
        }
    
        /**
         * 更新会员卡时,增加最迟激活日期任务。
         */
        @SqlTrigger(table = {"TT_F_MBR_CARD", "TG_F_MEMBER_CARD"}, type = TriggerType.UPDATE)
        public void update(MbrCardLastActivateDayVo old, MbrCardLastActivateDayVo newOne) {
            val day = newOne.getLatestActivateDay();
            val mbrCardId = old.getMbrCardId();
    
            val dt = day instanceof DateTime ? (DateTime) day : DateTimes.parse((String) day);
            val oldCard = Springs.getBean(TtfMemberCardDao.class).findMemberCard(mbrCardId);
            // 最迟激活日期没有变化时,不处理
            if (DateTimes.isSameDay(oldCard.getLatestActivateDay(), dt)) return;
    
            val task = Springs.getBean(MemberCardLatestActivateTask.class);
            task.cancel(mbrCardId);
            task.submit(mbrCardId, dt);
        }
    }
  5. Then the insert/update sql like the following will be caught to trigger the methods of auto-serviced class.

insert into TT_F_MBR_CARD(mbr_card_id, latest_activiate_day) values(?, ?);

update TT_F_MBR_CARD set latest_activiate_day = ? where mbr_card_id = ?;

Versions

Version
0.0.4
0.0.3
0.0.2
0.0.1