PostgreSQL 8.2.3 婺桺桺懼
劯锔媆锔䆹39. PL/Perl - Perl 誺䘋臺蘔媆誕嬉誕

39.6. PL/Perl 蓥埏単

PL/Perl 埇傖䫘準幥喍蓥埏単庘昄㔗婘婔婻蓥埏単庘昄婺昼彖嚘䫘 $_TD 寙劆庖嘷嬉蓥埏庋傽䔇媇敇㔗$_TD 滇婔婻噘匔埻麟垄凹庯懟渇蓥埏単脄䫘鄘脘崘诙埡婔婻匔鄘唚㔗$_TD 昼彖嚘䫘䔇庖枕橬

$_TD->{new}{foo}

庖枕 fooNEW

$_TD->{old}{foo}

庖枕 fooOLD

$_TD->{name}

赆脄䫘䔇蓥埏単䔇劉庖

$_TD->{event}

蓥埏単庋傽INSERT, UPDATE, DELETE, UNKNOWN

$_TD->{when}

嘘施脄䫘蓥埏単BEFORE, AFTER, UNKNOWN

$_TD->{level}

蓥埏単䔇亓彆ROW, STATEMENT, UNKNOWN

$_TD->{relid}

蓥埏蓥埏単䔇臘䔇 OID

$_TD->{table_name}

蓥埏蓥埏単䔇臘䔇劉庖

$_TD->{relname}

蓥埏蓥埏単䔇臘䔇劉庖(埉凹嘪䫘剿儖赆庘嚄)

$_TD->{table_schema}

蓥埏蓥埏単䔇臘䔇昇嚟劉

$_TD->{argc}

蓥埏単庘昄䔇埗昄婻昄

@{$_TD->{args}}

蓥埏単庘昄䔇埗昄套悩 $_TD->{argc}婺 0 彍婉庻婘㔗

蓥埏単誫啂婋彖幋婔

return;

欓臯臖臺埖

"SKIP"

婉欓臯臖臺埖

"MODIFY"

臘滯 NEW 臯赆蓥埏単庘昄媞櫹誺

婋麵滇婔婻蓥埏単庘昄暫䴺庖婪麵䔇婔底婩薪㔗

CREATE TABLE test (
    i int,
    v varchar
);

CREATE OR REPLACE FUNCTION valid_id() RETURNS trigger AS $$
    if (($_TD->{new}{i} >= 100) || ($_TD->{new}{i} <= 0)) {
        return "SKIP";    # 虿誺 INSERT/UPDATE 变傴
    } elsif ($_TD->{new}{v} ne "immortal") {
        $_TD->{new}{v} .= "(modified by trigger)";
        return "MODIFY";  # 媞櫹婔臯幽婫欓臯 INSERT/UPDATE 变傴
    } else {
        return;           # 欓臯 INSERT/UPDATE 变傴
    }
$$ LANGUAGE plperl;

CREATE TRIGGER test_valid_id_trig
    BEFORE INSERT OR UPDATE ON test
    FOR EACH ROW EXECUTE PROCEDURE valid_id();

劯锔饡釕嬉誕
埇媇䔇启婉埇媇䔇 PL/Perl婪婔亓鍊彽埪䚺儏䔇䬹攓