当前位置:网站首页>Use case of highgo database trigger (APP)

Use case of highgo database trigger (APP)

2021-01-06 12:03:24 Hangao PG Laboratory

Catalog
Environmental Science
Document purpose
Details
 
Environmental Science
system platform :Microsoft Windows (64-bit) 10
edition :5.6.4
 
Document purpose

This paper introduces the method of querying trigger information in Hangao database and the use case of trigger .

 
Details

One 、 Trigger introduction

  HighGo Database Trigger is to create trigger function first , Then create the trigger . Triggers must be used in conjunction with trigger functions .

  1)pg_trigger Some fields in the table show     

Name

Type

References

Description

oid

oid

 

Row identifier (hidden attribute; must be explicitly selected)

tgrelid

oid

pg_class.oid

The table this trigger is on

tgname

name

 

Trigger name (must be unique among triggers of same table)

tgfoid

oid

pg_proc.oid

The function to be called

tgisinternal

bool

 

True if trigger is internally generated (usually, to enforce   the constraint identified by tgconstraint)

2) List all current database triggers

 

highgo=# select * from pg_trigger;

3) List the triggers for a specific table

 

highgo=# select pt.* from pg_class pc join pg_trigger pt on pt.tgrelid=pc.oid where relname='table_name';

4) Query the trigger in the specified mode , And its function , The trigger function used

 

highgo=# select pc.oid tableoid, pc.relname " Table name ",

           pt.oid triggeroid, pt.tgname " Trigger Name ",

           pp.oid functionoid, pp.proname " Trigger function name "

from pg_trigger pt

join pg_class pc on pc.oid=pt.tgrelid

join pg_namespace pn on pc.relnamespace = pn.oid

left join pg_proc pp on pt.tgfoid=pp.oid

where tgisinternal=false --  Not created by the system

and pn.nspname ='schema_name';

Two 、 Trigger use cases

1)Mysql ON UPDATE CURRENT_TIMESTAMP stay hgdb Realization

mysql:

CREATE TABLE tab_hgdb_tri_update_yf (

  tab_id int(11) NOT NULL AUTO_INCREMENT,

  tab_name VARCHAR(10),

  insert_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,

  update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

  PRIMARY KEY (tab_id)

);

 

mysql> insert into tab_hgdb_tri_update_yf(tab_name) VALUES ('12');

mysql> select * from tab_hgdb_tri_update_yf;

+--------+----------+---------------------+---------------------+

| tab_id | tab_name | insert_time    | update_time     |

+--------+----------+---------------------+---------------------+

|   1 | 12     | 2019-11-08 13:40:56 | 2019-11-08 13:40:56 |

+--------+----------+---------------------+---------------------+

mysql> update tab_hgdb_tri_update_yf set tab_name='34' where tab_id=1 ;

Query OK, 1 row affected (0.01 sec)

Rows matched: 1  Changed: 1  Warnings: 0

 

mysql> select * from tab_hgdb_tri_update_yf;

+--------+----------+---------------------+---------------------+

| tab_id | tab_name | insert_time      | update_time      |

+--------+----------+---------------------+---------------------+

|    1 | 34     | 2019-11-08 13:40:56 | 2019-11-08 13:43:02 |

+--------+----------+---------------------+---------------------+

hgdb:

highgo=# CREATE TABLE tab_hgdb_tri_update_yf (

    tab_id bigserial NOT NULL,

    tab_name varchar(10) NULL,

    insert_time timestamp NULL DEFAULT CURRENT_TIMESTAMP::timestamp(0) without time zone,

    update_time timestamp NULL,

    CONSTRAINT tab_update_default_pkey PRIMARY KEY (tab_id)

);

 

highgo=# insert into tab_hgdb_tri_update_yf(tab_name) VALUES ('12');

highgo=# select * from tab_hgdb_tri_update_yf;

 tab_id | tab_name |      insert_time       | update_time

--------+----------+------------------------+-------------

      1 | 12       | 2019-11-08 14:07:02 |

 

highgo=# CREATE FUNCTION update_timestamp() RETURNS trigger AS u p d a t e t i m e s t a m p update_timestamp

      BEGIN

       NEW.update_time := current_timestamp::timestamp(0) without time zone;

       RETURN NEW;

      END;

      u p d a t e t i m e s t a m p update_timestamp LANGUAGE plpgsql;

 

highgo=# CREATE TRIGGER update_timestamp 

      BEFORE INSERT OR UPDATE ON tab_hgdb_tri_update_yf

      FOR EACH ROW EXECUTE PROCEDURE update_timestamp();

   

highgo=# insert into tab_hgdb_tri_update_yf(tab_name) VALUES ('56');

highgo=# select * from tab_hgdb_tri_update_yf;

 tab_id | tab_name |  insert_time   |  update_time

--------+----------+------------------------+------------------------

    1 | 12    | 2019-11-08 14:07:02 |

    2 | 56    | 2019-11-08 14:08:00 | 2019-11-08 14:08:00

 

highgo=# update tab_hgdb_tri_update_yf set tab_name='01' where tab_id=2;

highgo=# select * from tab_hgdb_tri_update_yf;

 tab_id | tab_name |      insert_time       |      update_time

--------+----------+------------------------+------------------------

      1 | 12       | 2019-11-08 14:07:02 |

      2 | 01       | 2019-11-08 14:08:00 | 2019-11-08 14:08:27

   2) Trigger realizes the primary key auto increment id And exception Handle

CREATE TABLE tab_emp_seq_yf (

    emp_id int,

    emp_name varchar(50)

);

 

create sequence seq_tab_emp_seq_yf_emp_id

minvalue 1

start with 1

increment by 1;

 

create or replace FUNCTION fun_tri_tab_emp_seq_yf_before_insert() RETURNS trigger AS t r i f u n tri_fun           

BEGIN

    new.emp_id := nextval('seq_tab_emp_seq_yf_emp_id');

    RETURN NEW;

    exception

      when others then

       raise notice '%', ' Exception number :' || substr(to_char(SQLSTATE), 1, 200) || '; Abnormal information :' || coalesce(sqlerrm::text,'');

    return null; -- Be careful here , According to their own business logic ; If it is return null, The data is not inserted into the table ;return new, be emp_id Columns are not inserted , Data from other columns is inserted into the table .

END;

t r i f u n tri_fun LANGUAGE plpgsql;

 

Simulate anomalies :

drop sequence seq_tab_emp_seq_yf_emp_id;

Exception output information :

Exception number :42P01; Abnormal information :relation "seq_tab_emp_seq_yf_emp_id" does not exist

 3)before/after insert/update/delete operation

For more details, please visit 【 Hangao technology support platform 】 see https://support.highgo.com/#/index/docContentHighgo/529c561c03b6a1d2

版权声明
本文为[Hangao PG Laboratory]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/01/20210106120215100w.html

随机推荐